Skip to content

Commit 01ffe0f

Browse files
committed
Address comments, perform deep copies and support synchronization for mutable BoundedTrieData
1 parent 38ae8e9 commit 01ffe0f

File tree

9 files changed

+430
-315
lines changed

9 files changed

+430
-315
lines changed

runners/core-java/src/main/java/org/apache/beam/runners/core/metrics/BoundedTrieCell.java

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919

2020
import java.util.Arrays;
2121
import java.util.Objects;
22-
import java.util.concurrent.atomic.AtomicReference;
2322
import org.apache.beam.sdk.metrics.BoundedTrie;
2423
import org.apache.beam.sdk.metrics.MetricName;
25-
import org.apache.beam.sdk.metrics.MetricsContainer;
2624
import org.checkerframework.checker.nullness.qual.Nullable;
2725

2826
/**
@@ -33,44 +31,37 @@
3331
* In that case retrieving the underlying cell and reporting directly to it avoids a step of
3432
* indirection.
3533
*/
34+
// TODO: Write multi-threaded test in MetricContainerImp for this Cell class too.
3635
public class BoundedTrieCell implements BoundedTrie, MetricCell<BoundedTrieData> {
3736

3837
private final DirtyState dirty = new DirtyState();
39-
private final AtomicReference<BoundedTrieData> setValue =
40-
new AtomicReference<>(BoundedTrieData.empty());
38+
private final BoundedTrieData value;
4139
private final MetricName name;
4240

43-
/**
44-
* Generally, runners should construct instances using the methods in {@link
45-
* MetricsContainerImpl}, unless they need to define their own version of {@link
46-
* MetricsContainer}. These constructors are *only* public so runners can instantiate.
47-
*/
4841
public BoundedTrieCell(MetricName name) {
4942
this.name = name;
43+
this.value = new BoundedTrieData();
44+
}
45+
46+
public void update(BoundedTrieCell other) {
47+
this.value.combine(other.value);
48+
dirty.afterModification();
5049
}
5150

5251
@Override
5352
public void reset() {
54-
setValue.set(BoundedTrieData.empty());
53+
value.clear();
5554
dirty.reset();
5655
}
5756

58-
void update(BoundedTrieData data) {
59-
BoundedTrieData original;
60-
do {
61-
original = setValue.get();
62-
} while (!setValue.compareAndSet(original, original.combine(data)));
63-
dirty.afterModification();
64-
}
65-
6657
@Override
6758
public DirtyState getDirty() {
6859
return dirty;
6960
}
7061

7162
@Override
7263
public BoundedTrieData getCumulative() {
73-
return setValue.get();
64+
return value.getCumulative();
7465
}
7566

7667
@Override
@@ -83,23 +74,20 @@ public boolean equals(@Nullable Object object) {
8374
if (object instanceof BoundedTrieCell) {
8475
BoundedTrieCell boundedTrieCell = (BoundedTrieCell) object;
8576
return Objects.equals(dirty, boundedTrieCell.dirty)
86-
&& Objects.equals(setValue.get(), boundedTrieCell.setValue.get())
77+
&& Objects.equals(value, boundedTrieCell.value)
8778
&& Objects.equals(name, boundedTrieCell.name);
8879
}
8980
return false;
9081
}
9182

9283
@Override
9384
public int hashCode() {
94-
return Objects.hash(dirty, setValue.get(), name);
85+
return Objects.hash(dirty, value, name);
9586
}
9687

9788
@Override
9889
public void add(Iterable<String> values) {
99-
BoundedTrieData original;
100-
do {
101-
original = setValue.get();
102-
} while (!setValue.compareAndSet(original, original.add(values)));
90+
this.value.add(values);
10391
dirty.afterModification();
10492
}
10593

0 commit comments

Comments
 (0)