<commit_before>/*
 * Copyright 2012 Netflix, Inc.
 *
 *    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.netflix.logging.messaging;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.netflix.blitz4j.BlitzConfig;
import com.netflix.blitz4j.LoggingConfiguration;
import com.netflix.config.DynamicBooleanProperty;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.servo.annotations.DataSourceType;
import com.netflix.servo.annotations.Monitor;
import com.netflix.servo.monitor.Counter;
import com.netflix.servo.monitor.Monitors;
import com.netflix.servo.monitor.Stopwatch;
import com.netflix.servo.monitor.Timer;

/**
 * A general purpose batcher that combines messages into batches. Callers of
 * process don't block. Configurable parameters control the number of messages
 * that may be queued awaiting processing, the maximum size of a batch, the
 * maximum time a message waits to be combined with others in a batch and the
 * size of the pool of threads that process batches.
 * 
 * The implementation aims to avoid congestion, by working more efficiently as
 * load increases. As messages arrive faster, the collector executes less code
 * and batch sizes increase (up to the configured maximum). It should be more
 * efficient to process a batch than to process the messages individually.
 * 
 * Here's how it works. Arriving messages are added to the queue. The collector
 * thread takes messages from the queue and collects them into batches. When a
 * batch is big enough or old enough, the collector passes it to the processor,
 * which passes the batch to the target stream.
 * 
 * The processor maintains a thread pool. If there's more work than threads, the
 * collector participates in processing by default, and consequently stops
 * collecting more batches.
 * 
 * @author <NAME>
 */
public class MessageBatcher<T> {

    private static final BlitzConfig CONFIGURATION = LoggingConfiguration.getInstance().getConfiguration();

    private static final String DOT = ".";

    private static final String BATCHER_PREFIX = "batcher.";

    private static final String COLLECTOR_SUFFIX = ".collector";

    private boolean shouldCollectorShutdown;

    List<Object> batch;

    protected String name;

    protected BlockingQueue queue;

    protected int maxMessages;

    protected static long maxDelay; // in nsec

    protected Collector collector;
<commit_msg>Formatting javadoc comments.<commit_after>/*
 * Copyright 2012 Netflix, Inc.
 *
 *    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.netflix.logging.messaging;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.netflix.blitz4j.BlitzConfig;
import com.netflix.blitz4j.LoggingConfiguration;
import com.netflix.config.DynamicBooleanProperty;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.servo.annotations.DataSourceType;
import com.netflix.servo.annotations.Monitor;
import com.netflix.servo.monitor.Counter;
import com.netflix.servo.monitor.Monitors;
import com.netflix.servo.monitor.Stopwatch;
import com.netflix.servo.monitor.Timer;

/**
 * A general purpose batcher that combines messages into batches. Callers of
 * process don't block. Configurable parameters control the number of messages
 * that may be queued awaiting processing, the maximum size of a batch, the
 * maximum time a message waits to be combined with others in a batch and the
 * size of the pool of threads that process batches.
 * 
 * <p>
 * The implementation aims to avoid congestion, by working more efficiently as
 * load increases. As messages arrive faster, the collector executes less code
 * and batch sizes increase (up to the configured maximum). It should be more
 * efficient to process a batch than to process the messages individually.
 * </p>
 * 
 * </p>
 * The implementation works by adding the arriving messages to a queue. The collector
 * thread takes messages from the queue and collects them into batches. When a
 * batch is big enough or old enough, the collector passes it to the processor,
 * which passes the batch to the target stream.
 * </p>
 * 
 * <p>
 * The processor maintains a thread pool. If there's more work than threads, the
 * collector participates in processing by default, and consequently stops
 * collecting more batches.
 * </p>
 * 
 * @author <NAME>
 */
public class MessageBatcher<T> {

    private static final BlitzConfig CONFIGURATION = LoggingConfiguration.getInstance().getConfiguration();

    private static final String DOT = ".";

    private static final String BATCHER_PREFIX = "batcher.";

    private static final String COLLECTOR_SUFFIX = ".collector";

    private boolean shouldCollectorShutdown;

    List<Object> batch;

    protected String name;

    protected BlockingQueue queue;

    protected int maxMessages;

    protected static long maxDelay; // in nsec

    protected Collector collector;

{'fraction_non_alphanumeric': 0.07133592736705577, 'fraction_numerical': 0.002882259691598213, 'mean_word_length': 4.285605483625286, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '2495961', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1747, 'n_words': 722}