Skip to content

Commit

Permalink
GEODE-5679: test waits for all stats changes to be written (#2502)
Browse files Browse the repository at this point in the history
Test waits for stats sampler to run _twice_ before reading
archive (file). Prevents reading before all stat changes arrive in file.

Co-authored-by: Ken Howe <[email protected]>
Co-authored-by: Bill Burcham <[email protected]>
  • Loading branch information
3 people committed Sep 21, 2018
1 parent cd17350 commit 23c27c7
Showing 1 changed file with 31 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -69,8 +68,8 @@ public class StatSamplerIntegrationTest {

@Before
public void setUp() {
this.statisticTypes = new HashMap<String, String>();
this.allStatistics = new HashMap<String, Map<String, Number>>();
this.statisticTypes = new HashMap<>();
this.allStatistics = new HashMap<>();
}

@After
Expand Down Expand Up @@ -150,7 +149,7 @@ public RuntimeException generateCancelledException(Throwable e) {
incLong(st1_1, "long_counter_9", 9);
incLong(st1_1, "long_gauge_11", 11);

awaitStatSample(samplerStats);
waitForStatSamplerToRun(samplerStats);

incDouble(st1_1, "double_counter_1", 1);
incDouble(st1_1, "double_counter_2", 1);
Expand All @@ -159,7 +158,7 @@ public RuntimeException generateCancelledException(Throwable e) {
incInt(st1_1, "int_counter_5", 1);
incInt(st1_1, "int_counter_6", 1);

awaitStatSample(samplerStats);
waitForStatSamplerToRun(samplerStats);

incDouble(st1_1, "double_counter_1", 1);
incDouble(st1_1, "double_counter_2", 1);
Expand All @@ -174,7 +173,7 @@ public RuntimeException generateCancelledException(Throwable e) {
incLong(st1_1, "long_gauge_11", 1);
incLong(st1_1, "long_gauge_12", 1);

awaitStatSample(samplerStats);
waitForStatSamplerToRun(samplerStats);

incDouble(st1_1, "double_counter_1", 1);
incDouble(st1_1, "double_counter_2", 1);
Expand All @@ -189,7 +188,7 @@ public RuntimeException generateCancelledException(Throwable e) {
incLong(st1_1, "long_gauge_11", -1);
incLong(st1_1, "long_gauge_12", 1);

awaitStatSample(samplerStats);
waitForStatSamplerToRun(samplerStats);

incDouble(st1_1, "double_counter_1", 1);
incDouble(st1_1, "double_counter_2", 1);
Expand All @@ -204,9 +203,9 @@ public RuntimeException generateCancelledException(Throwable e) {
incLong(st1_1, "long_gauge_11", 1);
incLong(st1_1, "long_gauge_12", 1);

awaitStatSample(samplerStats);
awaitStatSample(samplerStats);
awaitStatSample(samplerStats);
waitForStatSamplerToRun(samplerStats);
waitForStatSamplerToRun(samplerStats);
waitForStatSamplerToRun(samplerStats);

incDouble(st1_1, "double_counter_1", 1);
incDouble(st1_1, "double_gauge_3", 3);
Expand All @@ -215,7 +214,7 @@ public RuntimeException generateCancelledException(Throwable e) {
incLong(st1_1, "long_counter_9", 9);
incLong(st1_1, "long_gauge_11", 11);

awaitStatSample(samplerStats);
waitForStatSamplerToRun(samplerStats);

incDouble(st1_1, "double_counter_1", 1);
incDouble(st1_1, "double_counter_2", 1);
Expand All @@ -224,7 +223,7 @@ public RuntimeException generateCancelledException(Throwable e) {
incInt(st1_1, "int_counter_5", 1);
incInt(st1_1, "int_counter_6", 1);

awaitStatSample(samplerStats);
waitForStatSamplerToRun(samplerStats);

incDouble(st1_1, "double_counter_1", 1);
incDouble(st1_1, "double_counter_2", 1);
Expand All @@ -239,7 +238,7 @@ public RuntimeException generateCancelledException(Throwable e) {
incLong(st1_1, "long_gauge_11", 1);
incLong(st1_1, "long_gauge_12", 1);

awaitStatSample(samplerStats);
waitForStatSamplerToRun(samplerStats);

incDouble(st1_1, "double_counter_1", 1);
incDouble(st1_1, "double_counter_2", 1);
Expand All @@ -254,7 +253,7 @@ public RuntimeException generateCancelledException(Throwable e) {
incLong(st1_1, "long_gauge_11", -1);
incLong(st1_1, "long_gauge_12", 1);

awaitStatSample(samplerStats);
waitForStatSamplerToRun(samplerStats);

incDouble(st1_1, "double_counter_1", 1);
incDouble(st1_1, "double_counter_2", 1);
Expand All @@ -269,7 +268,13 @@ public RuntimeException generateCancelledException(Throwable e) {
incLong(st1_1, "long_gauge_11", 1);
incLong(st1_1, "long_gauge_12", 1);

awaitStatSample(samplerStats);
/*
* After updating all stats we should wait for the stat sampler to run at least twice. The
* first statSampler iteration may be running as the stats are being updated. The second
* statSampler iteration
* makes sure that all the stats we care about have been sampled and flush to the stat archive
*/
waitForStatSamplerToRun(samplerStats, 2);

factory.close();

Expand All @@ -278,8 +283,8 @@ public RuntimeException generateCancelledException(Throwable e) {
final StatArchiveReader reader = new StatArchiveReader(new File[] {archiveFile}, null, false);

List resources = reader.getResourceInstList();
for (Iterator iter = resources.iterator(); iter.hasNext();) {
StatArchiveReader.ResourceInst ri = (StatArchiveReader.ResourceInst) iter.next();
for (Object resource : resources) {
StatArchiveReader.ResourceInst ri = (StatArchiveReader.ResourceInst) resource;
String resourceName = ri.getName();
assertNotNull(resourceName);

Expand Down Expand Up @@ -317,10 +322,14 @@ private boolean hasSamplerStatsInstances(final LocalStatisticsFactory factory) {
return samplerStatsInstances != null && samplerStatsInstances.length > 0;
}

private void awaitStatSample(final Statistics samplerStats) throws InterruptedException {
int startSampleCount = samplerStats.getInt("sampleCount");
await("awaiting stat sample").atMost(30, SECONDS)
.until(() -> samplerStats.getInt("sampleCount") > startSampleCount);
private void waitForStatSamplerToRun(final Statistics samplerStats, final int timesToRun) {
final int startSampleCount = samplerStats.getInt("sampleCount");
await("waiting for the StatSampler to run").atMost(30, SECONDS)
.until(() -> samplerStats.getInt("sampleCount") >= startSampleCount + timesToRun);
}

private void waitForStatSamplerToRun(final Statistics samplerStats) {
waitForStatSamplerToRun(samplerStats, 1);
}

private void incDouble(Statistics statistics, String stat, double value) {
Expand All @@ -344,12 +353,7 @@ private void incInt(Statistics statistics, String stat, int value) {
}

private Map<String, Number> getOrCreateExpectedValueMap(final Statistics statistics) {
Map<String, Number> statValues = this.allStatistics.get(statistics.getTextId());
if (statValues == null) {
statValues = new HashMap<String, Number>();
this.allStatistics.put(statistics.getTextId(), statValues);
}
return statValues;
return this.allStatistics.computeIfAbsent(statistics.getTextId(), k -> new HashMap<>());
}

private void incLong(Statistics statistics, String stat, long value) {
Expand Down

0 comments on commit 23c27c7

Please sign in to comment.