Skip to content

Commit 8590015

Browse files
authored
Closes #53. Mark config as transient field (#55)
1 parent 9f6d338 commit 8590015

File tree

11 files changed

+34
-33
lines changed

11 files changed

+34
-33
lines changed

src/main/java/com/yahoo/bullet/aggregations/CountDistinct.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public CountDistinct(Aggregation aggregation, BulletConfig config) {
4545
super(aggregation, config);
4646
Map<String, Object> attributes = aggregation.getAttributes();
4747

48-
ResizeFactor resizeFactor = getResizeFactor(BulletConfig.COUNT_DISTINCT_AGGREGATION_SKETCH_RESIZE_FACTOR);
48+
ResizeFactor resizeFactor = getResizeFactor(config, BulletConfig.COUNT_DISTINCT_AGGREGATION_SKETCH_RESIZE_FACTOR);
4949
float samplingProbability = config.getAs(BulletConfig.COUNT_DISTINCT_AGGREGATION_SKETCH_SAMPLING, Float.class);
5050
Family family = getFamily(config.getAs(BulletConfig.COUNT_DISTINCT_AGGREGATION_SKETCH_FAMILY, String.class));
5151
int nominalEntries = config.getAs(BulletConfig.COUNT_DISTINCT_AGGREGATION_SKETCH_ENTRIES, Integer.class);

src/main/java/com/yahoo/bullet/aggregations/Distribution.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public boolean isMe(String name) {
7171

7272
// Copy of the aggregation
7373
private Aggregation aggregation;
74+
private BulletRecordProvider provider;
7475

7576
// Validation
7677
public static final Map<String, Type> SUPPORTED_DISTRIBUTION_TYPES = new HashMap<>();
@@ -108,6 +109,7 @@ public Distribution(Aggregation aggregation, BulletConfig config) {
108109
int pointLimit = config.getAs(BulletConfig.DISTRIBUTION_AGGREGATION_MAX_POINTS, Integer.class);
109110
maxPoints = Math.min(pointLimit, aggregation.getSize());
110111
this.aggregation = aggregation;
112+
this.provider = config.getBulletRecordProvider();
111113

112114
// The sketch is initialized in initialize!
113115
}
@@ -130,7 +132,7 @@ public Optional<List<BulletError>> initialize() {
130132
}
131133

132134
// Try to initialize sketch now
133-
sketch = getSketch(entries, maxPoints, rounding, type, attributes, config.getBulletRecordProvider());
135+
sketch = getSketch(entries, maxPoints, rounding, type, attributes, provider);
134136

135137
if (sketch == null) {
136138
return Optional.of(type == Type.QUANTILE ?

src/main/java/com/yahoo/bullet/aggregations/GroupAll.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.yahoo.bullet.common.Utilities;
1414
import com.yahoo.bullet.parsing.Aggregation;
1515
import com.yahoo.bullet.record.BulletRecord;
16+
import com.yahoo.bullet.record.BulletRecordProvider;
1617
import com.yahoo.bullet.result.Clip;
1718
import lombok.extern.slf4j.Slf4j;
1819

@@ -29,7 +30,7 @@ public class GroupAll implements Strategy {
2930
private GroupData data;
3031

3132
private Set<GroupOperation> operations;
32-
private BulletConfig config;
33+
private BulletRecordProvider provider;
3334
/**
3435
* Constructor that requires an {@link Aggregation}.
3536
*
@@ -40,7 +41,7 @@ public GroupAll(Aggregation aggregation, BulletConfig config) {
4041
// GroupOperations is all we care about - size etc. are meaningless for Group All since it's a single result
4142
operations = GroupOperation.getOperations(aggregation.getAttributes());
4243
data = new GroupData(operations);
43-
this.config = config;
44+
this.provider = config.getBulletRecordProvider();
4445
}
4546

4647
@Override
@@ -74,7 +75,7 @@ public Clip getResult() {
7475
@Override
7576
public List<BulletRecord> getRecords() {
7677
List<BulletRecord> list = new ArrayList<>();
77-
list.add(data.getMetricsAsBulletRecord(config.getBulletRecordProvider()));
78+
list.add(data.getMetricsAsBulletRecord(provider));
7879
return list;
7980
}
8081

src/main/java/com/yahoo/bullet/aggregations/GroupBy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public GroupBy(Aggregation aggregation, BulletConfig config) {
5353
Map<GroupOperation, Number> metrics = GroupData.makeInitialMetrics(operations);
5454
container = new CachingGroupData(null, metrics);
5555

56-
ResizeFactor resizeFactor = getResizeFactor(BulletConfig.GROUP_AGGREGATION_SKETCH_RESIZE_FACTOR);
56+
ResizeFactor resizeFactor = getResizeFactor(config, BulletConfig.GROUP_AGGREGATION_SKETCH_RESIZE_FACTOR);
5757
float samplingProbability = config.getAs(BulletConfig.GROUP_AGGREGATION_SKETCH_SAMPLING, Float.class);
5858

5959
// Default at 512 gives a 13.27% error rate at 99.73% confidence (3 SD). Irrelevant since we are using this to

src/main/java/com/yahoo/bullet/aggregations/KMVStrategy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ abstract class KMVStrategy<S extends KMVSketch> extends SketchingStrategy<S> {
2828
/**
2929
* Converts a integer representing the resizing for Sketches into a {@link ResizeFactor}.
3030
*
31+
* @param config The config that has relevant configs for this strategy.
3132
* @param key The key to get the configured resize factor from the configuration.
3233
* @return A {@link ResizeFactor} represented by the integer or {@link ResizeFactor#X8} otherwise.
3334
*/
3435
@SuppressWarnings("unchecked")
35-
ResizeFactor getResizeFactor(String key) {
36+
ResizeFactor getResizeFactor(BulletConfig config, String key) {
3637
return getResizeFactor(config.getAs(key, Integer.class));
3738
}
3839
/**

src/main/java/com/yahoo/bullet/aggregations/SketchingStrategy.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
public abstract class SketchingStrategy<S extends Sketch> implements Strategy {
3434
// The metadata concept to key mapping
3535
protected final Map<String, String> metadataKeys;
36-
// A copy of the configuration
37-
protected final BulletConfig config;
3836

3937
// Separator for multiple fields when inserting into the Sketch
4038
protected final String separator;
@@ -46,6 +44,8 @@ public abstract class SketchingStrategy<S extends Sketch> implements Strategy {
4644
// The Sketch that should be initialized by a child class
4745
protected S sketch;
4846

47+
private boolean shouldMeta;
48+
4949
/**
5050
* The constructor for creating a Sketch based strategy.
5151
*
@@ -54,9 +54,9 @@ public abstract class SketchingStrategy<S extends Sketch> implements Strategy {
5454
*/
5555
@SuppressWarnings("unchecked")
5656
public SketchingStrategy(Aggregation aggregation, BulletConfig config) {
57-
this.config = config;
5857
metadataKeys = (Map<String, String>) config.getAs(BulletConfig.RESULT_METADATA_METRICS, Map.class);
5958
separator = config.getAs(BulletConfig.AGGREGATION_COMPOSITE_FIELD_SEPARATOR, String.class);
59+
shouldMeta = config.getAs(BulletConfig.RESULT_METADATA_ENABLE, Boolean.class);
6060

6161
fieldsToNames = aggregation.getFields();
6262
fields = Utilities.isEmpty(fieldsToNames) ? Collections.emptyList() : new ArrayList<>(fieldsToNames.keySet());
@@ -123,7 +123,6 @@ List<String> decomposeField(String field) {
123123
}
124124

125125
private String getMetaKey() {
126-
boolean shouldMeta = config.getAs(BulletConfig.RESULT_METADATA_ENABLE, Boolean.class);
127126
return shouldMeta ? metadataKeys.getOrDefault(Meta.Concept.SKETCH_METADATA.getName(), null) : null;
128127
}
129128
}

src/main/java/com/yahoo/bullet/querying/Querier.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.yahoo.bullet.parsing.Window;
1717
import com.yahoo.bullet.postaggregations.PostStrategy;
1818
import com.yahoo.bullet.record.BulletRecord;
19+
import com.yahoo.bullet.record.BulletRecordProvider;
1920
import com.yahoo.bullet.result.Clip;
2021
import com.yahoo.bullet.result.Meta;
2122
import com.yahoo.bullet.result.Meta.Concept;
@@ -295,7 +296,9 @@ public enum Mode {
295296
@Getter(AccessLevel.PACKAGE)
296297
private RunningQuery runningQuery;
297298

298-
private BulletConfig config;
299+
// Transient field, DO NOT use it beyond constructor and initialize methods.
300+
private transient BulletConfig config;
301+
299302
private Map<String, String> metaKeys;
300303
private boolean hasNewData = false;
301304

@@ -307,6 +310,8 @@ public enum Mode {
307310

308311
private List<PostStrategy> postStrategies;
309312

313+
private BulletRecordProvider provider;
314+
310315
/**
311316
* Constructor that takes a String representation of the query and a configuration to use. This also starts the
312317
* query.
@@ -356,6 +361,7 @@ public Querier(Mode mode, RunningQuery query, BulletConfig config) {
356361
this.mode = mode;
357362
this.runningQuery = query;
358363
this.config = config;
364+
this.provider = config.getBulletRecordProvider();
359365
}
360366

361367
// ********************************* Monoidal Interface Overrides *********************************
@@ -604,7 +610,7 @@ public RateLimitError getRateLimitError() {
604610
if (rateLimit == null || !rateLimit.isExceededRate()) {
605611
return null;
606612
}
607-
return new RateLimitError(rateLimit.getCurrentRate(), config);
613+
return new RateLimitError(rateLimit.getCurrentRate(), rateLimit.getAbsoluteRateLimit());
608614
}
609615

610616
/**
@@ -650,7 +656,7 @@ private boolean filter(BulletRecord record) {
650656

651657
private BulletRecord project(BulletRecord record) {
652658
Projection projection = runningQuery.getQuery().getProjection();
653-
return projection != null ? ProjectionOperations.project(record, projection, config.getBulletRecordProvider()) : record;
659+
return projection != null ? ProjectionOperations.project(record, projection, provider) : record;
654660
}
655661

656662
private Clip postAggregate(Clip clip) {

src/main/java/com/yahoo/bullet/querying/RateLimitError.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
package com.yahoo.bullet.querying;
77

8-
import com.yahoo.bullet.common.BulletConfig;
98
import com.yahoo.bullet.common.BulletError;
109
import com.yahoo.bullet.result.Meta;
1110

@@ -24,13 +23,13 @@ public class RateLimitError extends BulletError {
2423

2524
/**
2625
/**
27-
* Creates an instance of this from a given absolute exceeded rate and a {@link BulletConfig}.
26+
* Creates an instance of this from a given absolute exceeded rate and a maximum rate limit.
2827
*
2928
* @param rate The exceeded rate that caused the error.
30-
* @param config The validated BulletConfig to use.
29+
* @param limit The maximum rate limit for the query.
3130
*/
32-
public RateLimitError(double rate, BulletConfig config) {
33-
super(String.format(ERROR_FORMAT, getRate(config), rate * RateLimiter.SECOND), RESOLUTIONS);
31+
public RateLimitError(double rate, double limit) {
32+
super(String.format(ERROR_FORMAT, limit * RateLimiter.SECOND, rate * RateLimiter.SECOND), RESOLUTIONS);
3433
}
3534

3635
/**
@@ -41,10 +40,4 @@ public RateLimitError(double rate, BulletConfig config) {
4140
public Meta makeMeta() {
4241
return new Meta().addErrors(Collections.singletonList(this));
4342
}
44-
45-
private static double getRate(BulletConfig config) {
46-
int maxCount = config.getAs(BulletConfig.RATE_LIMIT_MAX_EMIT_COUNT, Integer.class);
47-
int interval = config.getAs(BulletConfig.RATE_LIMIT_TIME_INTERVAL, Integer.class);
48-
return (maxCount / (double) interval) * RateLimiter.SECOND;
49-
}
5043
}

src/main/java/com/yahoo/bullet/querying/RateLimiter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class RateLimiter {
2323
private final int maximum;
2424
@Getter
2525
private final int timeInterval;
26+
@Getter
2627
private final double absoluteRateLimit;
2728

2829
private long count = 0;
@@ -57,6 +58,7 @@ public RateLimiter(int maximum, int timeInterval) throws IllegalArgumentExceptio
5758
this.maximum = maximum;
5859
this.timeInterval = timeInterval;
5960
this.absoluteRateLimit = maximum / (double) timeInterval;
61+
6062
lastCheckTime = System.currentTimeMillis();
6163
}
6264

src/main/java/com/yahoo/bullet/windowing/Scheme.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
public abstract class Scheme implements Monoidal {
2525
protected Strategy aggregation;
2626
protected Window window;
27-
protected BulletConfig config;
2827
protected Map<String, String> metadataKeys;
28+
private boolean shouldMeta;
2929

3030
/**
3131
* Creates an instance of this windowing scheme with the provided {@link Strategy}, {@link Window} and
@@ -38,8 +38,8 @@ public abstract class Scheme implements Monoidal {
3838
public Scheme(Strategy aggregation, Window window, BulletConfig config) {
3939
this.aggregation = aggregation;
4040
this.window = window;
41-
this.config = config;
4241
metadataKeys = (Map<String, String>) config.getAs(BulletConfig.RESULT_METADATA_METRICS, Map.class);
42+
shouldMeta = config.getAs(BulletConfig.RESULT_METADATA_ENABLE, Boolean.class);
4343
}
4444

4545
/**
@@ -73,7 +73,6 @@ public Scheme(Strategy aggregation, Window window, BulletConfig config) {
7373
@Override
7474
public Meta getMetadata() {
7575
Meta meta = new Meta();
76-
boolean shouldMeta = config.getAs(BulletConfig.RESULT_METADATA_ENABLE, Boolean.class);
7776
if (shouldMeta) {
7877
String key = getMetaKey();
7978
if (key != null) {

src/test/java/com/yahoo/bullet/querying/RateLimitErrorTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
package com.yahoo.bullet.querying;
77

8-
import com.yahoo.bullet.common.BulletConfig;
98
import com.yahoo.bullet.common.BulletError;
109
import com.yahoo.bullet.result.Meta;
1110
import org.testng.Assert;
@@ -21,16 +20,15 @@
2120
public class RateLimitErrorTest {
2221
@Test
2322
public void testMetaAndRateConversion() {
24-
BulletConfig config = new BulletConfig();
25-
RateLimitError error = new RateLimitError(19.34, config);
23+
double defaultRate = ((double) DEFAULT_RATE_LIMIT_MAX_EMIT_COUNT / DEFAULT_RATE_LIMIT_TIME_INTERVAL);
24+
RateLimitError error = new RateLimitError(19.34, defaultRate);
2625
Map<String, Object> actual = error.makeMeta().asMap();
2726
Assert.assertTrue(actual.containsKey(Meta.ERROR_KEY));
2827
Assert.assertTrue(((List<BulletError>) actual.get(Meta.ERROR_KEY)).get(0) == error);
2928

3029
String asJSON = error.asJSON();
31-
double defaultRate = ((double) DEFAULT_RATE_LIMIT_MAX_EMIT_COUNT / DEFAULT_RATE_LIMIT_TIME_INTERVAL) * RateLimiter.SECOND;
3230
double actualRate = 19.34 * RateLimiter.SECOND;
33-
assertJSONEquals(asJSON, "{'error': '" + String.format(RateLimitError.ERROR_FORMAT, defaultRate, actualRate) + "', " +
31+
assertJSONEquals(asJSON, "{'error': '" + String.format(RateLimitError.ERROR_FORMAT, defaultRate * RateLimiter.SECOND, actualRate) + "', " +
3432
"'resolutions': ['" + RateLimitError.NARROW_FILTER + "', '" + RateLimitError.TIME_WINDOW + "']" +
3533
"}");
3634
}

0 commit comments

Comments
 (0)