<commit_before>/*
 * Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.map.impl.eviction;

import com.hazelcast.config.Config;
import com.hazelcast.map.impl.PartitionContainer;
import com.hazelcast.map.impl.operation.ClearExpiredOperation;
import com.hazelcast.map.impl.recordstore.RecordStore;
import com.hazelcast.nio.Address;
import com.hazelcast.spi.ExecutionService;
import com.hazelcast.spi.NodeEngine;
import com.hazelcast.spi.Operation;
import com.hazelcast.spi.impl.operationservice.InternalOperationService;
import com.hazelcast.spi.partition.IPartition;
import com.hazelcast.spi.partition.IPartitionService;
import com.hazelcast.util.Clock;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentMap;

import static com.hazelcast.map.impl.MapService.SERVICE_NAME;
import static com.hazelcast.util.CollectionUtil.isEmpty;
import static com.hazelcast.util.Preconditions.checkPositive;
import static com.hazelcast.util.Preconditions.checkTrue;
import static java.lang.Integer.parseInt;
import static java.lang.Math.min;
import static java.util.Collections.sort;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * Responsible for gradual cleanup of expired entries in partitions.
 * By using these system properties, one can accelerate or slow down background expiration process.
 * <li>
 * <ul>{@value SYS_PROP_EXPIRATION_TASK_PERIOD_SECONDS}</ul>
 * <ul>{@value SYS_PROP_EXPIRATION_CLEANUP_PERCENTAGE}</ul>
 * <ul>{@value SYS_PROP_EXPIRATION_CLEANUP_OPERATION_COUNT}</ul>
 * </li>
 *
 * @since 3.3
 */
public final class ExpirationManager {

    // These are `default` for testing purposes.
    static final String SYS_PROP_EXPIRATION_TASK_PERIOD_SECONDS = "hazelcast.internal.map.expiration.task.period.seconds";
    static final String SYS_PROP_EXPIRATION_CLEANUP_PERCENTAGE = "hazelcast.internal.map.expiration.cleanup.percentage";
    @SuppressWarnings("checkstyle:linelength")
    static final String SYS_PROP_EXPIRATION_CLEANUP_OPERATION_COUNT = "hazelcast.internal.map.expiration.cleanup.operation.count";

    private static final int DEFAULT_EXPIRATION_TASK_PERIOD_SECONDS = 5;
    private static final int DEFAULT_EXPIRATION_CLEANUP_PERCENTAGE = 10;
    private static final int DIFFERENCE_BETWEEN_TWO_SUBSEQUENT_PARTITION_CLEANUP_MILLIS = 1000;

    private final NodeEngine nodeEngine;
    private final PartitionContainer[] partitionContainers;
    private final Address thisAddress;
    private final IPartitionService partitionService;
    private final ExecutionService executionService;
    private final InternalOperationService operationService;
    private final int partitionCount;
    private final int taskPeriodSeconds;
    private final int cleanupPercentage;
    private final int cleanupOperationCount;

    @SuppressWarnings("checkstyle:magicnumber")
    @SuppressFBWarnings({"EI_EXPOSE_REP2"})
    public ExpirationManager(PartitionContainer[] partitionContainers, NodeEngine nodeEngine) {
        this.nodeEngine = nodeEngine;
        this.partitionContainers = partitionContainers;
        this.thisAddress = nodeEngine.getThisAddress();
        this.partitionService = nodeEngine.getPartitionService();
        this.executionService = nodeEngine.getExecutionService();
        this.operationService = (InternalOperationService) nodeEngine.getOperationService();
        this.partitionCount = partitionService.getPartitionCount();

        nodeEngine.getConfig().getProperty(SYS_PROP_EXPIRATION_TASK_PERIOD_SECONDS);

        this.taskPeriodSeconds = getInteger(SYS_PROP_EXPIRATION_TASK_PERIOD_SECONDS, DEFAULT_EXPIRATION_TASK_PERIOD_SECONDS);
        checkPositive(taskPeriodSeconds, "taskPeriodSeconds should be a positive number");

        this.cleanupPercentage = getInteger(SYS_PROP_EXPIRATION_CLEANUP_PERCENTAGE, DEFAULT_EXPIRATION_CLEANUP_PERCENTAGE);<commit_msg>Removed unused line<commit_after>/*
 * Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.map.impl.eviction;

import com.hazelcast.config.Config;
import com.hazelcast.map.impl.PartitionContainer;
import com.hazelcast.map.impl.operation.ClearExpiredOperation;
import com.hazelcast.map.impl.recordstore.RecordStore;
import com.hazelcast.nio.Address;
import com.hazelcast.spi.ExecutionService;
import com.hazelcast.spi.NodeEngine;
import com.hazelcast.spi.Operation;
import com.hazelcast.spi.impl.operationservice.InternalOperationService;
import com.hazelcast.spi.partition.IPartition;
import com.hazelcast.spi.partition.IPartitionService;
import com.hazelcast.util.Clock;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentMap;

import static com.hazelcast.map.impl.MapService.SERVICE_NAME;
import static com.hazelcast.util.CollectionUtil.isEmpty;
import static com.hazelcast.util.Preconditions.checkPositive;
import static com.hazelcast.util.Preconditions.checkTrue;
import static java.lang.Integer.parseInt;
import static java.lang.Math.min;
import static java.util.Collections.sort;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * Responsible for gradual cleanup of expired entries in partitions.
 * By using these system properties, one can accelerate or slow down background expiration process.
 * <li>
 * <ul>{@value SYS_PROP_EXPIRATION_TASK_PERIOD_SECONDS}</ul>
 * <ul>{@value SYS_PROP_EXPIRATION_CLEANUP_PERCENTAGE}</ul>
 * <ul>{@value SYS_PROP_EXPIRATION_CLEANUP_OPERATION_COUNT}</ul>
 * </li>
 *
 * @since 3.3
 */
public final class ExpirationManager {

    // These are `default` for testing purposes.
    static final String SYS_PROP_EXPIRATION_TASK_PERIOD_SECONDS = "hazelcast.internal.map.expiration.task.period.seconds";
    static final String SYS_PROP_EXPIRATION_CLEANUP_PERCENTAGE = "hazelcast.internal.map.expiration.cleanup.percentage";
    @SuppressWarnings("checkstyle:linelength")
    static final String SYS_PROP_EXPIRATION_CLEANUP_OPERATION_COUNT = "hazelcast.internal.map.expiration.cleanup.operation.count";

    private static final int DEFAULT_EXPIRATION_TASK_PERIOD_SECONDS = 5;
    private static final int DEFAULT_EXPIRATION_CLEANUP_PERCENTAGE = 10;
    private static final int DIFFERENCE_BETWEEN_TWO_SUBSEQUENT_PARTITION_CLEANUP_MILLIS = 1000;

    private final NodeEngine nodeEngine;
    private final PartitionContainer[] partitionContainers;
    private final Address thisAddress;
    private final IPartitionService partitionService;
    private final ExecutionService executionService;
    private final InternalOperationService operationService;
    private final int partitionCount;
    private final int taskPeriodSeconds;
    private final int cleanupPercentage;
    private final int cleanupOperationCount;

    @SuppressWarnings("checkstyle:magicnumber")
    @SuppressFBWarnings({"EI_EXPOSE_REP2"})
    public ExpirationManager(PartitionContainer[] partitionContainers, NodeEngine nodeEngine) {
        this.nodeEngine = nodeEngine;
        this.partitionContainers = partitionContainers;
        this.thisAddress = nodeEngine.getThisAddress();
        this.partitionService = nodeEngine.getPartitionService();
        this.executionService = nodeEngine.getExecutionService();
        this.operationService = (InternalOperationService) nodeEngine.getOperationService();
        this.partitionCount = partitionService.getPartitionCount();

        this.taskPeriodSeconds = getInteger(SYS_PROP_EXPIRATION_TASK_PERIOD_SECONDS, DEFAULT_EXPIRATION_TASK_PERIOD_SECONDS);
        checkPositive(taskPeriodSeconds, "taskPeriodSeconds should be a positive number");

        this.cleanupPercentage = getInteger(SYS_PROP_EXPIRATION_CLEANUP_PERCENTAGE, DEFAULT_EXPIRATION_CLEANUP_PERCENTAGE);
{'fraction_non_alphanumeric': 0.0790527018012008, 'fraction_numerical': 0.004892150322437181, 'mean_word_length': 5.4204139900071375, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3753839', 'n_tokens_mistral': 2681, 'n_tokens_neox': 2615, 'n_words': 600}