Package org.agrona.concurrent.errors
Class DistinctErrorLog
- java.lang.Object
-
- org.agrona.concurrent.errors.DistinctErrorLog
-
public class DistinctErrorLog extends java.lang.Object
Distinct record of error observations. Rather than grow a record indefinitely when many errors of the same type are logged, this log takes the approach of only recording distinct errors of the same type and stack trace and keeping a count and time of observation so that the record only grows with new distinct observations.The provided
AtomicBuffer
can wrap a memory-mapped file so logging can be out of process. This provides the benefit that if a crash or lockup occurs then the log can be read externally without loss of data.Note: This class is threadsafe to be used from multiple logging threads.
The error records are recorded to the memory mapped buffer in the following format.
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |R| Length | +-+-------------------------------------------------------------+ |R| Observation Count | +-+-------------------------------------------------------------+ |R| Last Observation Timestamp | | | +-+-------------------------------------------------------------+ |R| First Observation Timestamp | | | +---------------------------------------------------------------+ | Encoded Error ... ... | +---------------------------------------------------------------+
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
DistinctErrorLog.DistinctObservation
-
Field Summary
Fields Modifier and Type Field Description private AtomicBuffer
buffer
private java.nio.charset.Charset
charset
private EpochClock
clock
private DistinctErrorLog.DistinctObservation[]
distinctObservations
static int
ENCODED_ERROR_OFFSET
Offset within a record at which the encoded exception field begins.static int
FIRST_OBSERVATION_TIMESTAMP_OFFSET
Offset within a record at which the first observation timestamp field begins.private static DistinctErrorLog.DistinctObservation
INSUFFICIENT_SPACE
static int
LAST_OBSERVATION_TIMESTAMP_OFFSET
Offset within a record at which the last observation timestamp field begins.static int
LENGTH_OFFSET
Offset within a record at which the length field begins.private int
nextOffset
static int
OBSERVATION_COUNT_OFFSET
Offset within a record at which the observation count field begins.static int
RECORD_ALIGNMENT
Alignment to be applied for record beginning.
-
Constructor Summary
Constructors Constructor Description DistinctErrorLog(AtomicBuffer buffer, EpochClock clock)
Create a new error log that will be written to a providedAtomicBuffer
.DistinctErrorLog(AtomicBuffer buffer, EpochClock clock, java.nio.charset.Charset charset)
Create a new error log that will be written to a providedAtomicBuffer
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AtomicBuffer
buffer()
Buffer the error log is written to.java.nio.charset.Charset
charset()
Charset
used to encode errors.private static boolean
equals(java.lang.StackTraceElement[] lhsStackTrace, java.lang.StackTraceElement[] rhsStackTrace)
private static boolean
equals(java.lang.Throwable lhs, java.lang.Throwable rhs)
private static DistinctErrorLog.DistinctObservation
find(DistinctErrorLog.DistinctObservation[] existingObservations, java.lang.Throwable observation)
private DistinctErrorLog.DistinctObservation
newObservation(long timestampMs, java.lang.Throwable observation)
private static DistinctErrorLog.DistinctObservation[]
prepend(DistinctErrorLog.DistinctObservation[] observations, DistinctErrorLog.DistinctObservation observation)
boolean
record(java.lang.Throwable observation)
Record an observation of an error.
-
-
-
Field Detail
-
LENGTH_OFFSET
public static final int LENGTH_OFFSET
Offset within a record at which the length field begins.- See Also:
- Constant Field Values
-
OBSERVATION_COUNT_OFFSET
public static final int OBSERVATION_COUNT_OFFSET
Offset within a record at which the observation count field begins.- See Also:
- Constant Field Values
-
LAST_OBSERVATION_TIMESTAMP_OFFSET
public static final int LAST_OBSERVATION_TIMESTAMP_OFFSET
Offset within a record at which the last observation timestamp field begins.- See Also:
- Constant Field Values
-
FIRST_OBSERVATION_TIMESTAMP_OFFSET
public static final int FIRST_OBSERVATION_TIMESTAMP_OFFSET
Offset within a record at which the first observation timestamp field begins.- See Also:
- Constant Field Values
-
ENCODED_ERROR_OFFSET
public static final int ENCODED_ERROR_OFFSET
Offset within a record at which the encoded exception field begins.- See Also:
- Constant Field Values
-
RECORD_ALIGNMENT
public static final int RECORD_ALIGNMENT
Alignment to be applied for record beginning.- See Also:
- Constant Field Values
-
INSUFFICIENT_SPACE
private static final DistinctErrorLog.DistinctObservation INSUFFICIENT_SPACE
-
nextOffset
private int nextOffset
-
clock
private final EpochClock clock
-
buffer
private final AtomicBuffer buffer
-
charset
private final java.nio.charset.Charset charset
-
distinctObservations
private DistinctErrorLog.DistinctObservation[] distinctObservations
-
-
Constructor Detail
-
DistinctErrorLog
public DistinctErrorLog(AtomicBuffer buffer, EpochClock clock)
Create a new error log that will be written to a providedAtomicBuffer
.The
Charset
with default toStandardCharsets.UTF_8
for encoding the exceptions.- Parameters:
buffer
- into which the observation records are recorded.clock
- to be used for time stamping records.
-
DistinctErrorLog
public DistinctErrorLog(AtomicBuffer buffer, EpochClock clock, java.nio.charset.Charset charset)
Create a new error log that will be written to a providedAtomicBuffer
.- Parameters:
buffer
- into which the observation records are recorded.clock
- to be used for time stamping records.charset
- for encoding the errors.
-
-
Method Detail
-
buffer
public AtomicBuffer buffer()
Buffer the error log is written to.- Returns:
- buffer the error log is written to.
-
charset
public java.nio.charset.Charset charset()
Charset
used to encode errors.- Returns:
- the
Charset
used to encode errors.
-
record
public boolean record(java.lang.Throwable observation)
Record an observation of an error. If it is the first observation of this error type for a stack trace then a new entry will be created. For subsequent observations of the same error type and stack trace a counter and time of last observation will be updated.- Parameters:
observation
- to be logged as an error observation.- Returns:
- true if successfully logged otherwise false if insufficient space remaining in the log.
-
find
private static DistinctErrorLog.DistinctObservation find(DistinctErrorLog.DistinctObservation[] existingObservations, java.lang.Throwable observation)
-
equals
private static boolean equals(java.lang.Throwable lhs, java.lang.Throwable rhs)
-
equals
private static boolean equals(java.lang.StackTraceElement[] lhsStackTrace, java.lang.StackTraceElement[] rhsStackTrace)
-
newObservation
private DistinctErrorLog.DistinctObservation newObservation(long timestampMs, java.lang.Throwable observation)
-
prepend
private static DistinctErrorLog.DistinctObservation[] prepend(DistinctErrorLog.DistinctObservation[] observations, DistinctErrorLog.DistinctObservation observation)
-
-