Skip to content

Commit

Permalink
Reverting #18 (#20)
Browse files Browse the repository at this point in the history
* Revert "Try to avoid `synchronized` in `Sensor#record`, which is a hot path"

This reverts commit 2220ffd.

* bump version
  • Loading branch information
ssen-li authored Dec 10, 2020
1 parent 8b102c9 commit 83d8012
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 39 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
group=io.tehuti
archivesBaseName=tehuti
version=0.8.7
version=0.8.8

signing.enabled=false
signing.keyId=
Expand Down
43 changes: 5 additions & 38 deletions src/main/java/io/tehuti/metrics/Sensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public final class Sensor {
private final List<TehutiMetric> metrics;
private final MetricConfig config;
private final Time time;
private boolean hasQuotaConfig;

private final Map<Stat, MetricConfig> statConfigs;

Expand All @@ -54,27 +53,6 @@ public final class Sensor {
this.time = time;
this.statConfigs = new HashMap<Stat, MetricConfig>();
checkForest(new HashSet<Sensor>());

this.hasQuotaConfig = false;
checkWhetherQuotaConfigPresent();
}

/**
* This function is used to check whether the attached metrics have quota config or not.
* We only need to trigger this function when there are new metric additions.
*/
private void checkWhetherQuotaConfigPresent() {
if (hasQuotaConfig) {
return;
}
for (TehutiMetric metric : metrics) {
MetricConfig metricConfig = metric.config();
if (metricConfig != null && metricConfig.quota() != null) {
hasQuotaConfig = true;
return;

}
}
}

/** Validate that this sensor doesn't end up referencing itself */
Expand Down Expand Up @@ -118,23 +96,15 @@ public void record(double value) {
* bound
*/
public void record(double value, long timeMs) {
if (hasQuotaConfig) {
// Try to avoid synchronized section as much as possible
synchronized (this) {
// Check quota before recording usage if needed.
checkQuotas(timeMs, true, value);
// increment all the stats
for (int i = 0; i < this.stats.size(); i++) {
Stat stat = this.stats.get(i);
stat.record(statConfigs.get(stat), value, timeMs);
}
checkQuotas(timeMs, false, value);
}
} else {
synchronized (this) {
// Check quota before recording usage if needed.
checkQuotas(timeMs, true, value);
// increment all the stats
for (int i = 0; i < this.stats.size(); i++) {
Stat stat = this.stats.get(i);
stat.record(statConfigs.get(stat), value, timeMs);
}
checkQuotas(timeMs, false, value);
}
for (int i = 0; i < parents.length; i++)
parents[i].record(value, timeMs);
Expand Down Expand Up @@ -196,7 +166,6 @@ public synchronized Map<String, Metric> add(CompoundStat stat, MetricConfig conf
this.metrics.add(metric);
addedMetrics.put(metric.name(), metric);
}
checkWhetherQuotaConfigPresent();
return addedMetrics;
}

Expand Down Expand Up @@ -244,8 +213,6 @@ public synchronized Metric add(String name, String description, MeasurableStat s
this.metrics.add(metric);
this.stats.add(stat);
this.statConfigs.put(stat, statConfig);
checkWhetherQuotaConfigPresent();

return metric;
}

Expand Down

0 comments on commit 83d8012

Please sign in to comment.