-
Notifications
You must be signed in to change notification settings - Fork 5
feat(NA) - Discard older metrics on buffer overflow. #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,16 @@ | |
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.concurrent.ArrayBlockingQueue; | ||
| import java.util.logging.Logger; | ||
|
|
||
| /** | ||
| * Buffer to store metrics. | ||
| */ | ||
| public class StandardBuffer implements MetricsBuffer { | ||
|
|
||
| private static final Logger LOGGER = Logger.getLogger(StandardBuffer.class.getName()); | ||
| private ArrayBlockingQueue<String> buffer; | ||
| private int maxBufferSize; | ||
| private int flushSize; | ||
|
|
@@ -40,7 +43,19 @@ public final ArrayBlockingQueue<String> getBuffer() { | |
| * @return A {@link Boolean} with the success of the operation | ||
| */ | ||
| public final boolean addToBuffer(final String metric) { | ||
| return buffer.offer(metric); | ||
| boolean inserted = buffer.offer(metric); | ||
| if (!inserted) { | ||
| try { | ||
| LOGGER.fine("Buffer is full, trying to discard oldest metric."); | ||
| buffer.remove(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
| } catch (NoSuchElementException e) { | ||
| LOGGER.warning("Buffer has been emptied before trying to discard oldest entry."); | ||
| } finally { | ||
| inserted = buffer.offer(metric); | ||
| } | ||
| } | ||
|
|
||
| return inserted; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,7 +60,7 @@ public void tearDown() { | |
| } | ||
|
|
||
| @Test | ||
| public void shouldDiscardIfStandardBufferIsFull() { | ||
| public void shouldDiscardOlderMetricsIfStandardBufferIsFull() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should, somehow, test the behaviour of trying to empty it when it's empty, more so if we keep the current exception based implementation for when it's empty |
||
| // Given | ||
| when(configuration.getFlushSize()).thenReturn(10000); | ||
|
|
||
|
|
@@ -76,6 +76,7 @@ public void shouldDiscardIfStandardBufferIsFull() { | |
| // Then | ||
| List<String> buffer = subject.getStandardBuffer(); | ||
| assertEquals("MetricsBuffer should have 5000 metrics", 5000, buffer.size()); | ||
| assertTrue(buffer.contains("application.test_metric_overflow 500 123456789 100")); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -95,6 +96,7 @@ public void shouldDiscardIfAggregatedBufferIsFull() { | |
| // Then | ||
| Map<Aggregation, Map<AggregationFrequency, List<String>>> buffer = subject.getAggregatedBuffer(); | ||
| assertEquals("MetricsBuffer should have 5000 metrics", 5000, buffer.get(Aggregation.AVG).get(AggregationFrequency.FREQ_10).size()); | ||
| assertTrue(buffer.get(Aggregation.AVG).get(AggregationFrequency.FREQ_10).contains("application.test_metric_overflow 500 123456789 100")); | ||
| } | ||
|
|
||
| @Test | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of remove we could use pool ( https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#poll() ) which would allow us to log the discarded metric if we wanted, which I think we want, and doesn't throw exception if it's empty - returns null).
Thinking out loud, should we drop more then one metric?