Skip to content

Commit f366e7f

Browse files
committed
Use volatile int in MemoryAccountingBytesRefCounted
1 parent 6c3a59d commit f366e7f

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

server/src/main/java/org/elasticsearch/common/MemoryAccountingBytesRefCounted.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212
import org.elasticsearch.common.breaker.CircuitBreaker;
1313
import org.elasticsearch.core.AbstractRefCounted;
1414

15-
import java.util.concurrent.atomic.AtomicInteger;
16-
1715
/**
1816
* A ref counted object that accounts for memory usage in bytes and releases the
1917
* accounted memory from the circuit breaker when the reference count reaches zero.
2018
*/
2119
public final class MemoryAccountingBytesRefCounted extends AbstractRefCounted {
2220

23-
private final AtomicInteger bytes = new AtomicInteger(0);
21+
private volatile int bytes;
2422
private final CircuitBreaker breaker;
2523

2624
private MemoryAccountingBytesRefCounted(CircuitBreaker breaker) {
@@ -31,13 +29,13 @@ public static MemoryAccountingBytesRefCounted create(CircuitBreaker breaker) {
3129
return new MemoryAccountingBytesRefCounted(breaker);
3230
}
3331

34-
public void account(int bytes, String label) {
32+
public void setBytesAndAccount(int bytes, String label) {
3533
breaker.addEstimateBytesAndMaybeBreak(bytes, label);
36-
this.bytes.addAndGet(bytes);
34+
this.bytes = bytes;
3735
}
3836

3937
@Override
4038
protected void closeInternal() {
41-
breaker.addWithoutBreaking(-bytes.get());
39+
breaker.addWithoutBreaking(-bytes);
4240
}
4341
}

server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ private static HitContext prepareNonNestedHitContext(
368368
source = sourceLoader.source(leafStoredFieldLoader, subDocId);
369369
int accumulatedInLeaf = memoryUsageAccumulator.apply(source.internalSourceRef().length(), submitToCB);
370370
if (submitToCB) {
371-
memAccountingRefCounted.account(accumulatedInLeaf, "fetch phase source loader");
371+
memAccountingRefCounted.setBytesAndAccount(accumulatedInLeaf, "fetch phase source loader");
372372
}
373373
} catch (CircuitBreakingException e) {
374374
hit.decRef();
@@ -410,7 +410,7 @@ private static Supplier<Source> lazyStoredSourceLoader(
410410
BytesReference source = leafRootLoader.source();
411411
int accumulatedInLeaf = memoryUsageAccumulator.apply(source.length(), submitToCB);
412412
if (submitToCB) {
413-
memAccountingRefCounted.account(accumulatedInLeaf, "lazy fetch phase source loader");
413+
memAccountingRefCounted.setBytesAndAccount(accumulatedInLeaf, "lazy fetch phase source loader");
414414
}
415415
return Source.fromBytes(source);
416416
} catch (IOException e) {

server/src/test/java/org/elasticsearch/common/MemoryAccountingBytesRefCountedTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ public void testNoMemoryAccounted() {
2626
public void testMemoryAccounted() {
2727
CircuitBreaker breaker = new MockBigArrays.LimitedBreaker("test", ByteSizeValue.ofGb(1));
2828
MemoryAccountingBytesRefCounted refCounted = MemoryAccountingBytesRefCounted.create(breaker);
29-
refCounted.account(10, "test");
29+
refCounted.setBytesAndAccount(10, "test");
3030
assertEquals(10, breaker.getUsed());
3131
}
3232

3333
public void testCloseInternalDecrementsBreaker() {
3434
CircuitBreaker breaker = new MockBigArrays.LimitedBreaker("test", ByteSizeValue.ofGb(1));
3535
MemoryAccountingBytesRefCounted refCounted = MemoryAccountingBytesRefCounted.create(breaker);
36-
refCounted.account(10, "test");
36+
refCounted.setBytesAndAccount(10, "test");
3737
refCounted.decRef();
3838
assertEquals(0, breaker.getUsed());
3939
}
4040

4141
public void testBreakerNotDecrementedIfRefsRemaining() {
4242
CircuitBreaker breaker = new MockBigArrays.LimitedBreaker("test", ByteSizeValue.ofGb(1));
4343
MemoryAccountingBytesRefCounted refCounted = MemoryAccountingBytesRefCounted.create(breaker);
44-
refCounted.account(10, "test");
44+
refCounted.setBytesAndAccount(10, "test");
4545
refCounted.incRef(); // 2 refs
4646
assertEquals(10, breaker.getUsed());
4747
refCounted.decRef(); // 1 ref remaining so no decrementing is executed

0 commit comments

Comments
 (0)