Skip to content

Commit 855611f

Browse files
author
Ville Koskela
committed
Upgrade commons and use thread local builders in high traffic situations.
1 parent ead941d commit 855611f

29 files changed

+588
-293
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<akka.http.version>10.0.11</akka.http.version>
7676
<apache.httpclient.version>4.5.1</apache.httpclient.version>
7777
<apache.httpcore.version>4.4.3</apache.httpcore.version>
78-
<arpnetworking.commons.version>1.13.3</arpnetworking.commons.version>
78+
<arpnetworking.commons.version>1.14.0</arpnetworking.commons.version>
7979
<aspectjrt.version>1.8.9</aspectjrt.version>
8080
<cglib.version>3.2.5</cglib.version>
8181
<client.protocol.version>0.10.0</client.protocol.version>

src/main/java/com/arpnetworking/metrics/mad/Bucket.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.arpnetworking.metrics.mad;
1717

1818
import com.arpnetworking.commons.builder.OvalBuilder;
19+
import com.arpnetworking.commons.builder.ThreadLocalBuilder;
1920
import com.arpnetworking.logback.annotations.LogValue;
2021
import com.arpnetworking.metrics.mad.model.Metric;
2122
import com.arpnetworking.metrics.mad.model.Record;
@@ -85,12 +86,12 @@ public void close() {
8586
// -> This requires expressions. Otherwise, it's just a matter of changing the
8687
// alerts abstraction from a Sink to something more appropriate and hooking it in
8788
// here.
88-
final PeriodicData periodicData = new PeriodicData.Builder()
89-
.setData(data.build())
90-
.setDimensions(_key)
91-
.setPeriod(_period)
92-
.setStart(_start)
93-
.build();
89+
final PeriodicData periodicData = ThreadLocalBuilder.build(
90+
PeriodicData.Builder.class,
91+
b -> b.setData(data.build())
92+
.setDimensions(_key)
93+
.setPeriod(_period)
94+
.setStart(_start));
9495
_sink.recordAggregateData(periodicData);
9596
} finally {
9697
_addCloseLock.writeLock().unlock();
@@ -247,8 +248,6 @@ private void computeStatistics(
247248
final BiFunction<String, Statistic, Boolean> specified,
248249
final ImmutableMultimap.Builder<String, AggregatedData> data) {
249250

250-
final AggregatedData.Builder datumBuilder = new AggregatedData.Builder();
251-
252251
for (final Map.Entry<String, Collection<Calculator<?>>> entry : calculatorsByMetric.entrySet()) {
253252
final String metric = entry.getKey();
254253
final Collection<Calculator<?>> calculators = entry.getValue();
@@ -266,25 +265,32 @@ private void computeStatistics(
266265
}
267266
dependencies.put(calculator.getStatistic(), calculator);
268267
}
269-
CalculatedValue<?> populationSize = new CalculatedValue.Builder<>()
270-
.setValue(new Quantity.Builder().setValue(-1.0).build())
271-
.build();
268+
final CalculatedValue<?> populationSize;
272269
if (countStatisticCalculator.isPresent()) {
273270
populationSize = countStatisticCalculator.get().calculate(dependencies);
271+
} else {
272+
populationSize = ThreadLocalBuilder.<
273+
CalculatedValue<Object>,
274+
CalculatedValue.Builder<Object>>buildGeneric(
275+
CalculatedValue.Builder.class,
276+
b -> b.setValue(
277+
ThreadLocalBuilder.build(
278+
Quantity.Builder.class,
279+
builder -> builder.setValue(-1.0))));
274280
}
275281

276282
// Compute each calculated value requested by the client
277283
for (final Calculator<?> calculator : calculators) {
278-
datumBuilder.setSupportingData(null);
279284
final CalculatedValue<?> calculatedValue = calculator.calculate(dependencies);
280-
data.put(
281-
metric,
282-
datumBuilder.setValue(calculatedValue.getValue())
285+
final AggregatedData datum = ThreadLocalBuilder.build(
286+
AggregatedData.Builder.class,
287+
b -> b.setSupportingData(null)
288+
.setValue(calculatedValue.getValue())
283289
.setIsSpecified(specified.apply(metric, calculator.getStatistic()))
284290
.setPopulationSize((long) populationSize.getValue().getValue())
285291
.setSupportingData(calculatedValue.getData())
286-
.setStatistic(calculator.getStatistic())
287-
.build());
292+
.setStatistic(calculator.getStatistic()));
293+
data.put(metric, datum);
288294
}
289295
}
290296
}

src/main/java/com/arpnetworking/metrics/mad/model/DefaultMetric.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.arpnetworking.metrics.mad.model;
1717

18-
import com.arpnetworking.commons.builder.OvalBuilder;
18+
import com.arpnetworking.commons.builder.ThreadLocalBuilder;
1919
import com.arpnetworking.logback.annotations.LogValue;
2020
import com.arpnetworking.steno.LogValueMapFactory;
2121
import com.arpnetworking.tsdcore.model.MetricType;
@@ -105,7 +105,7 @@ private DefaultMetric(final Builder builder) {
105105
*
106106
* @author Ville Koskela (ville dot koskela at inscopemetrics dot com)
107107
*/
108-
public static final class Builder extends OvalBuilder<Metric> {
108+
public static final class Builder extends ThreadLocalBuilder<Metric> {
109109

110110
/**
111111
* Public constructor.
@@ -136,6 +136,12 @@ public Builder setType(final MetricType value) {
136136
return this;
137137
}
138138

139+
@Override
140+
protected void reset() {
141+
_values = null;
142+
_type = null;
143+
}
144+
139145
@NotNull
140146
private List<Quantity> _values;
141147
@NotNull

src/main/java/com/arpnetworking/metrics/mad/model/DefaultRecord.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.arpnetworking.metrics.mad.model;
1717

18-
import com.arpnetworking.commons.builder.OvalBuilder;
18+
import com.arpnetworking.commons.builder.ThreadLocalBuilder;
1919
import com.arpnetworking.logback.annotations.Loggable;
2020
import com.google.common.base.MoreObjects;
2121
import com.google.common.base.Objects;
@@ -114,7 +114,7 @@ private DefaultRecord(final Builder builder) {
114114
*
115115
* @author Ville Koskela (ville dot koskela at inscopemetrics dot com)
116116
*/
117-
public static final class Builder extends OvalBuilder<Record> {
117+
public static final class Builder extends ThreadLocalBuilder<Record> {
118118

119119
/**
120120
* Public constructor.
@@ -180,6 +180,15 @@ public Builder setDimensions(final ImmutableMap<String, String> value) {
180180
return this;
181181
}
182182

183+
@Override
184+
protected void reset() {
185+
_metrics = null;
186+
_id = null;
187+
_time = null;
188+
_annotations = ImmutableMap.of();
189+
_dimensions = ImmutableMap.of();
190+
}
191+
183192
@NotNull
184193
private ImmutableMap<String, ? extends Metric> _metrics;
185194
@NotNull

src/main/java/com/arpnetworking/metrics/mad/model/json/Telegraf.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.arpnetworking.metrics.mad.model.json;
1717

18-
import com.arpnetworking.commons.builder.OvalBuilder;
18+
import com.arpnetworking.commons.builder.ThreadLocalBuilder;
1919
import com.arpnetworking.logback.annotations.Loggable;
2020
import com.google.common.collect.ImmutableMap;
2121

@@ -63,7 +63,7 @@ private Telegraf(final Builder builder) {
6363
/**
6464
* Builder for the Telegraf class.
6565
*/
66-
public static final class Builder extends OvalBuilder<Telegraf> {
66+
public static final class Builder extends ThreadLocalBuilder<Telegraf> {
6767
/**
6868
* Public constructor.
6969
*/
@@ -115,6 +115,14 @@ public Builder setName(final String value) {
115115
return this;
116116
}
117117

118+
@Override
119+
protected void reset() {
120+
_tags = ImmutableMap.of();
121+
_timestamp = null;
122+
_fields = ImmutableMap.of();
123+
_name = null;
124+
}
125+
118126
@NotNull
119127
private ImmutableMap<String, String> _tags = ImmutableMap.of();
120128
@Min(0)

src/main/java/com/arpnetworking/metrics/mad/model/json/Version2c.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.arpnetworking.metrics.mad.model.json;
1717

18-
import com.arpnetworking.commons.builder.OvalBuilder;
18+
import com.arpnetworking.commons.builder.ThreadLocalBuilder;
1919
import com.arpnetworking.logback.annotations.Loggable;
2020
import com.fasterxml.jackson.annotation.JsonAnySetter;
2121
import com.google.common.collect.ImmutableMap;
@@ -75,7 +75,7 @@ private Version2c(final Builder builder) {
7575
/**
7676
* Builder for the Version2c class.
7777
*/
78-
public static final class Builder extends OvalBuilder<Version2c> {
78+
public static final class Builder extends ThreadLocalBuilder<Version2c> {
7979
/**
8080
* Public constructor.
8181
*/
@@ -138,6 +138,15 @@ public Builder setGauges(final Map<String, List<String>> value) {
138138
return this;
139139
}
140140

141+
@Override
142+
protected void reset() {
143+
_annotations = null;
144+
_counters = Collections.emptyMap();
145+
_gauges = Collections.emptyMap();
146+
_timers = Collections.emptyMap();
147+
_version = null;
148+
}
149+
141150
@NotNull
142151
private Annotations _annotations;
143152
@NotNull
@@ -181,7 +190,7 @@ private Annotations(final Annotations.Builder builder) {
181190
/**
182191
* Builder for the Annotations class.
183192
*/
184-
public static final class Builder extends OvalBuilder<Annotations> {
193+
public static final class Builder extends ThreadLocalBuilder<Annotations> {
185194
/**
186195
* Public constructor.
187196
*/
@@ -225,10 +234,17 @@ public void handleUnknown(final String key, final Object value) {
225234
}
226235
}
227236

237+
@Override
238+
protected void reset() {
239+
_finalTimestamp = null;
240+
_initTimestamp = null;
241+
_otherAnnotations = Maps.newHashMap();
242+
}
243+
228244
private String _finalTimestamp;
229245
private String _initTimestamp;
230246
@NotNull
231-
private final Map<String, String> _otherAnnotations = Maps.newHashMap();
247+
private Map<String, String> _otherAnnotations = Maps.newHashMap();
232248
}
233249
}
234250
}

src/main/java/com/arpnetworking/metrics/mad/model/json/Version2d.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.arpnetworking.metrics.mad.model.json;
1717

18-
import com.arpnetworking.commons.builder.OvalBuilder;
18+
import com.arpnetworking.commons.builder.ThreadLocalBuilder;
1919
import com.arpnetworking.logback.annotations.Loggable;
2020
import com.arpnetworking.tsdcore.model.Unit;
2121
import com.fasterxml.jackson.annotation.JsonAnySetter;
@@ -78,7 +78,7 @@ private Version2d(final Builder builder) {
7878
/**
7979
* Builder for the Version2d class.
8080
*/
81-
public static final class Builder extends OvalBuilder<Version2d> {
81+
public static final class Builder extends ThreadLocalBuilder<Version2d> {
8282
/**
8383
* Public constructor.
8484
*/
@@ -141,6 +141,15 @@ public Builder setGauges(final Map<String, Element> value) {
141141
return this;
142142
}
143143

144+
@Override
145+
protected void reset() {
146+
_annotations = null;
147+
_counters = Collections.emptyMap();
148+
_gauges = Collections.emptyMap();
149+
_timers = Collections.emptyMap();
150+
_version = null;
151+
}
152+
144153
@NotNull
145154
private Annotations _annotations;
146155
@NotNull
@@ -178,7 +187,7 @@ private Sample(final Builder builder) {
178187
/**
179188
* Builder for the Sample class.
180189
*/
181-
public static final class Builder extends OvalBuilder<Sample> {
190+
public static final class Builder extends ThreadLocalBuilder<Sample> {
182191
/**
183192
* Public constructor.
184193
*/
@@ -208,6 +217,12 @@ public Builder setValue(final Double value) {
208217
return this;
209218
}
210219

220+
@Override
221+
protected void reset() {
222+
_unit = null;
223+
_value = null;
224+
}
225+
211226
private Unit _unit;
212227
@NotNull
213228
private Double _value;
@@ -234,7 +249,7 @@ private Element(final Element.Builder builder) {
234249
/**
235250
* Builder for the Element class.
236251
*/
237-
public static final class Builder extends OvalBuilder<Element> {
252+
public static final class Builder extends ThreadLocalBuilder<Element> {
238253
/**
239254
* Public constructor.
240255
*/
@@ -253,6 +268,11 @@ public Element.Builder setValues(final List<Sample> value) {
253268
return this;
254269
}
255270

271+
@Override
272+
protected void reset() {
273+
_values = Collections.emptyList();
274+
}
275+
256276
@NotNull
257277
private List<Sample> _values = Collections.emptyList();
258278
}
@@ -288,7 +308,7 @@ private Annotations(final Annotations.Builder builder) {
288308
/**
289309
* Builder for the Annotations class.
290310
*/
291-
public static final class Builder extends OvalBuilder<Annotations> {
311+
public static final class Builder extends ThreadLocalBuilder<Annotations> {
292312
/**
293313
* Public constructor.
294314
*/
@@ -332,12 +352,19 @@ public void handleUnknown(final String key, final Object value) {
332352
}
333353
}
334354

355+
@Override
356+
protected void reset() {
357+
_finalTimestamp = null;
358+
_initTimestamp = null;
359+
_otherAnnotations = Maps.newHashMap();
360+
}
361+
335362
@NotNull
336363
private DateTime _finalTimestamp;
337364
@NotNull
338365
private DateTime _initTimestamp;
339366
@NotNull
340-
private final Map<String, String> _otherAnnotations = Maps.newHashMap();
367+
private Map<String, String> _otherAnnotations = Maps.newHashMap();
341368
}
342369
}
343370
}

0 commit comments

Comments
 (0)