Class Batcher


  • public final class Batcher
    extends java.lang.Object
    A component that is responsible for splitting a stream of log events into batches. The batch is cut based on the following criteria:
    • maxItems - if number of records reaches this limit
    • maxSizeBytes - if size in bytes (as counted by Loki) reaches this limit, applies only when checkSizeBeforeAdd() is called
    • maxTimeoutMs - if this timeout is passed since the last batch was sended, applies only when drain() is called
    This class is not thread-safe.
    • Constructor Summary

      Constructors 
      Constructor Description
      Batcher​(int maxItems, int maxSizeBytes, long maxTimeoutMs)  
    • Constructor Detail

      • Batcher

        public Batcher​(int maxItems,
                       int maxSizeBytes,
                       long maxTimeoutMs)
    • Method Detail

      • validateLogRecordSize

        public boolean validateLogRecordSize​(LogRecord r)
        Checks if the given message is less or equal to max allowed size for a batch. This method doesn't affect the internal state of the Batcher. This method is thread-safe.
      • checkSizeBeforeAdd

        public void checkSizeBeforeAdd​(LogRecord input,
                                       LogRecordBatch destination)
        Checks if given record can be added to batch without exceeding max bytes limit. Note that this method never adds an input record to the batch, you must call add() for this purpose.

        If a valid record can not be added to batch without exceeding max bytes limit, batcher returns a completed batch without this record.

        Otherwise, no action is performed.

        Parameters:
        input - Log record to check
        destination - Resulting batch (if ready)
      • add

        public void add​(LogRecord input,
                        LogRecordBatch destination)
        Adds given record to batch and returns a batch if max items limit is reached.
        Parameters:
        input - Log record to add
        destination - Resulting batch (if ready)
      • drain

        public void drain​(long lastSentMs,
                          LogRecordBatch destination)
        Returns a batch if max timeout since the last batch was sended
        Parameters:
        lastSentMs - Timestamp when the last batch was sended
        destination - Resulting batch (if ready)
      • getCapacity

        public int getCapacity()