Skip to content

Commit

Permalink
Reduce memory consumption and snapshot extraction latency for special…
Browse files Browse the repository at this point in the history
… case when histogram was unused for a long time
  • Loading branch information
vladimir-bukhtoyarov committed Oct 23, 2016
1 parent f3233f5 commit 4c2c774
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.github.metricscore.hdr.histogram.accumulator;

import com.github.metricscore.hdr.histogram.util.EmptySnapshot;
import com.github.metricscore.hdr.util.ResilientExecutionUtil;
import com.github.metricscore.hdr.util.Clock;
import com.codahale.metrics.Snapshot;
Expand Down Expand Up @@ -92,17 +93,25 @@ public void recordSingleValueWithExpectedInterval(long value, long expectedInter
private synchronized void rotate(long currentTimeMillis, Phase currentPhase, Phase nextPhase) {
try {
currentPhase.intervalHistogram = currentPhase.recorder.getIntervalHistogram(currentPhase.intervalHistogram);
currentPhase.totalsHistogram.add(currentPhase.intervalHistogram);
if (currentPhase.intervalHistogram.getTotalCount() > 0) {
currentPhase.totalsHistogram.add(currentPhase.intervalHistogram);
}
if (historySupported) {
// move values from recorder to correspondent archived histogram
long currentPhaseNumber = (currentPhase.proposedInvalidationTimestamp - creationTimestamp) / intervalBetweenResettingMillis;
int correspondentArchiveIndex = (int) (currentPhaseNumber - 1) % archive.length;
ArchivedHistogram correspondentArchivedHistogram = archive[correspondentArchiveIndex];
correspondentArchivedHistogram.histogram.reset();
correspondentArchivedHistogram.histogram.add(currentPhase.totalsHistogram);
if (correspondentArchivedHistogram.histogram.getTotalCount() > 0) {
correspondentArchivedHistogram.histogram.reset();
}
if (currentPhase.totalsHistogram.getTotalCount() > 0) {
correspondentArchivedHistogram.histogram.add(currentPhase.totalsHistogram);
}
correspondentArchivedHistogram.proposedInvalidationTimestamp = currentPhase.proposedInvalidationTimestamp + (archive.length - 1) * intervalBetweenResettingMillis;
}
currentPhase.totalsHistogram.reset();
if (currentPhase.totalsHistogram.getTotalCount() > 0) {
currentPhase.totalsHistogram.reset();
}
} finally {
long millisSinceCreation = currentTimeMillis - creationTimestamp;
long intervalsSinceCreation = millisSinceCreation / intervalBetweenResettingMillis;
Expand All @@ -113,23 +122,33 @@ private synchronized void rotate(long currentTimeMillis, Phase currentPhase, Pha

@Override
public final synchronized Snapshot getSnapshot(Function<Histogram, Snapshot> snapshotTaker) {
temporarySnapshotHistogram.reset();
if (temporarySnapshotHistogram.getTotalCount() > 0) {
temporarySnapshotHistogram.reset();
}
long currentTimeMillis = clock.currentTimeMillis();

for (Phase phase : phases) {
if (phase.isNeedToBeReportedToSnapshot(currentTimeMillis)) {
phase.intervalHistogram = phase.recorder.getIntervalHistogram(phase.intervalHistogram);
phase.totalsHistogram.add(phase.intervalHistogram);
temporarySnapshotHistogram.add(phase.totalsHistogram);
if (phase.intervalHistogram.getTotalCount() > 0) {
phase.totalsHistogram.add(phase.intervalHistogram);
}
if (phase.totalsHistogram.getTotalCount() > 0) {
temporarySnapshotHistogram.add(phase.totalsHistogram);
}
}
}
for (ArchivedHistogram archivedHistogram : archive) {
if (archivedHistogram.proposedInvalidationTimestamp > currentTimeMillis) {
if (archivedHistogram.proposedInvalidationTimestamp > currentTimeMillis && archivedHistogram.histogram.getTotalCount() > 0) {
temporarySnapshotHistogram.add(archivedHistogram.histogram);
}
}

return snapshotTaker.apply(temporarySnapshotHistogram);
if (temporarySnapshotHistogram.getTotalCount() > 0) {
return snapshotTaker.apply(temporarySnapshotHistogram);
} else {
return EmptySnapshot.INSTANCE;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.github.metricscore.hdr.histogram.accumulator;

import com.codahale.metrics.Snapshot;
import com.github.metricscore.hdr.histogram.util.EmptySnapshot;
import com.github.metricscore.hdr.histogram.util.Printer;
import org.HdrHistogram.Histogram;
import org.HdrHistogram.Recorder;
Expand All @@ -42,7 +43,11 @@ public void recordSingleValueWithExpectedInterval(long value, long expectedInter
@Override
synchronized public final Snapshot getSnapshot(Function<Histogram, Snapshot> snapshotTaker) {
intervalHistogram = recorder.getIntervalHistogram(intervalHistogram);
return snapshotTaker.apply(intervalHistogram);
if (intervalHistogram.getTotalCount() > 0) {
return snapshotTaker.apply(intervalHistogram);
} else {
return EmptySnapshot.INSTANCE;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.github.metricscore.hdr.histogram.accumulator;

import com.codahale.metrics.Snapshot;
import com.github.metricscore.hdr.histogram.util.EmptySnapshot;
import com.github.metricscore.hdr.histogram.util.Printer;
import org.HdrHistogram.Histogram;
import org.HdrHistogram.Recorder;
Expand Down Expand Up @@ -45,8 +46,14 @@ public void recordSingleValueWithExpectedInterval(long value, long expectedInter
@Override
public final synchronized Snapshot getSnapshot(Function<Histogram, Snapshot> snapshotTaker) {
intervalHistogram = recorder.getIntervalHistogram(intervalHistogram);
uniformHistogram.add(intervalHistogram);
return snapshotTaker.apply(uniformHistogram);
if (intervalHistogram.getTotalCount() > 0) {
uniformHistogram.add(intervalHistogram);
}
if (uniformHistogram.getTotalCount() > 0) {
return snapshotTaker.apply(uniformHistogram);
} else {
return EmptySnapshot.INSTANCE;
}
}

@Override
Expand Down

0 comments on commit 4c2c774

Please sign in to comment.