Package com.github.loki4j.common
Class Batcher
- java.lang.Object
-
- com.github.loki4j.common.Batcher
-
public final class Batcher extends java.lang.ObjectA 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 whencheckSizeBeforeAdd()is called -
maxTimeoutMs- if this timeout is passed since the last batch was sended, applies only whendrain()is called
-
-
-
Constructor Summary
Constructors Constructor Description Batcher(int maxItems, int maxSizeBytes, long maxTimeoutMs)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(LogRecord input, LogRecordBatch destination)Adds given record to batch and returns a batch if max items limit is reached.voidcheckSizeBeforeAdd(LogRecord input, LogRecordBatch destination)Checks if given record can be added to batch without exceeding max bytes limit.voiddrain(long lastSentMs, LogRecordBatch destination)Returns a batch if max timeout since the last batch was sendedintgetCapacity()booleanvalidateLogRecordSize(LogRecord r)Checks if the given message is less or equal to max allowed size for a batch.
-
-
-
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 calladd()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 checkdestination- 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 adddestination- 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 sendeddestination- Resulting batch (if ready)
-
getCapacity
public int getCapacity()
-
-