Skip to content

Commit

Permalink
add tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisHegarty committed Sep 20, 2023
1 parent c198d53 commit 3bbf9cc
Show file tree
Hide file tree
Showing 19 changed files with 627 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public BooleanBlock build() {
}
}
// update the breaker with the actual bytes used.
// TODO: verify that this can also give back
blockFactory.adjustBreaker(block.ramBytesUsed() - estimatedBytes, true);
return block;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public BooleanVector build() {
vector = new BooleanArrayVector(values, valueCount, blockFactory);
}
// update the breaker with the actual bytes used.
// TODO: verify that this can also give back
blockFactory.adjustBreaker(vector.ramBytesUsed() - estimatedBytes, true);
return vector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.RamUsageEstimator;
import org.elasticsearch.common.util.BytesRefArray;
import org.elasticsearch.core.Releasables;

import java.util.BitSet;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -110,6 +111,7 @@ public String toString() {

@Override
public void close() {
blockFactory.adjustBreaker(-ramBytesUsed(), true);
blockFactory.adjustBreaker(-(ramBytesUsed() - values.ramBytesUsed()), true);
Releasables.closeExpectNoException(values);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public String toString() {

@Override
public void close() {
blockFactory.adjustBreaker(-BASE_RAM_BYTES_USED, true);
Releasables.closeExpectNoException(values);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.BytesRefArray;
import org.elasticsearch.core.Releasables;

/**
* Block build of BytesRefBlocks.
Expand Down Expand Up @@ -195,16 +196,17 @@ public BytesRefBlock build() {
finish();
BytesRefBlock block;
if (hasNonNullValue && positionCount == 1 && valueCount == 1) {
block = new ConstantBytesRefVector(values.get(0, new BytesRef()), 1, blockFactory).asBlock();
block = new ConstantBytesRefVector(BytesRef.deepCopyOf(values.get(0, new BytesRef())), 1, blockFactory).asBlock();
Releasables.closeExpectNoException(values);
} else {
estimatedBytes += values.ramBytesUsed();
if (isDense() && singleValued()) {
block = new BytesRefArrayVector(values, positionCount, blockFactory).asBlock();
} else {
block = new BytesRefArrayBlock(values, positionCount, firstValueIndexes, nullsMask, mvOrdering, blockFactory);
}
}
// update the breaker with the actual bytes used.
// TODO: verify that this can also give back
blockFactory.adjustBreaker(block.ramBytesUsed() - estimatedBytes, true);
return block;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.BytesRefArray;
import org.elasticsearch.core.Releasables;

/**
* Block build of BytesRefBlocks.
Expand Down Expand Up @@ -55,12 +56,13 @@ protected void growValuesArray(int newSize) {
public BytesRefVector build() {
BytesRefVector vector;
if (valueCount == 1) {
vector = new ConstantBytesRefVector(values.get(0, new BytesRef()), 1, blockFactory);
vector = new ConstantBytesRefVector(BytesRef.deepCopyOf(values.get(0, new BytesRef())), 1, blockFactory);
Releasables.closeExpectNoException(values);
} else {
estimatedBytes = values.ramBytesUsed();
vector = new BytesRefArrayVector(values, valueCount, blockFactory);
}
// update the breaker with the actual bytes used.
// TODO: verify that this can also give back
blockFactory.adjustBreaker(vector.ramBytesUsed() - estimatedBytes, true);
return vector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public DoubleBlock build() {
}
}
// update the breaker with the actual bytes used.
// TODO: verify that this can also give back
blockFactory.adjustBreaker(block.ramBytesUsed() - estimatedBytes, true);
return block;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public DoubleVector build() {
vector = new DoubleArrayVector(values, valueCount, blockFactory);
}
// update the breaker with the actual bytes used.
// TODO: verify that this can also give back
blockFactory.adjustBreaker(vector.ramBytesUsed() - estimatedBytes, true);
return vector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public IntBlock build() {
}
}
// update the breaker with the actual bytes used.
// TODO: verify that this can also give back
blockFactory.adjustBreaker(block.ramBytesUsed() - estimatedBytes, true);
return block;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public IntVector build() {
vector = new IntArrayVector(values, valueCount, blockFactory);
}
// update the breaker with the actual bytes used.
// TODO: verify that this can also give back
blockFactory.adjustBreaker(vector.ramBytesUsed() - estimatedBytes, true);
return vector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public LongBlock build() {
}
}
// update the breaker with the actual bytes used.
// TODO: verify that this can also give back
blockFactory.adjustBreaker(block.ramBytesUsed() - estimatedBytes, true);
return block;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public LongVector build() {
vector = new LongArrayVector(values, valueCount, blockFactory);
}
// update the breaker with the actual bytes used.
// TODO: verify that this can also give back
blockFactory.adjustBreaker(vector.ramBytesUsed() - estimatedBytes, true);
return vector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public CircuitBreaker breaker() {
return breaker;
}

// For testing
public BigArrays bigArrays() {
return bigArrays;
}

/**
* Adjust the circuit breaker with the given delta, if the delta is
* negative, or checkBreaker is false, the breaker will be adjusted
Expand Down Expand Up @@ -238,7 +243,7 @@ public BytesRefBlock newBytesRefArrayBlock(
MvOrdering mvOrdering
) {
var b = new BytesRefArrayBlock(values, positionCount, firstValueIndexes, nulls, mvOrdering, this);
adjustBreaker(b.ramBytesUsed(), true);
adjustBreaker(b.ramBytesUsed() - values.ramBytesUsed(), true);
return b;
}

Expand All @@ -248,11 +253,13 @@ public BytesRefVector.Builder newBytesRefVectorBuilder(int estimatedSize) {

public BytesRefVector newBytesRefArrayVector(BytesRefArray values, int positionCount) {
var b = new BytesRefArrayVector(values, positionCount, this);
adjustBreaker(b.ramBytesUsed(), true);
adjustBreaker(b.ramBytesUsed() - values.ramBytesUsed(), true);
return b;
}

public BytesRefBlock newConstantBytesRefBlockWith(BytesRef value, int positions) {
return new ConstantBytesRefVector(value, positions, this).asBlock();
var b = new ConstantBytesRefVector(value, positions, this).asBlock();
adjustBreaker(b.ramBytesUsed(), true);
return b;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ $if(BytesRef)$
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.RamUsageEstimator;
import org.elasticsearch.common.util.BytesRefArray;
import org.elasticsearch.core.Releasables;

$else$
import org.apache.lucene.util.RamUsageEstimator;
Expand Down Expand Up @@ -131,6 +132,11 @@ $endif$

@Override
public void close() {
$if(BytesRef)$
blockFactory.adjustBreaker(-(ramBytesUsed() - values.ramBytesUsed()), true);
Releasables.closeExpectNoException(values);
$else$
blockFactory.adjustBreaker(-ramBytesUsed(), true);
$endif$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ $endif$
@Override
public void close() {
$if(BytesRef)$
blockFactory.adjustBreaker(-BASE_RAM_BYTES_USED, true);
Releasables.closeExpectNoException(values);
$else$
super.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ $if(BytesRef)$
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.BytesRefArray;
import org.elasticsearch.core.Releasables;

$else$
import java.util.Arrays;
Expand Down Expand Up @@ -249,8 +250,10 @@ $endif$
$Type$Block block;
if (hasNonNullValue && positionCount == 1 && valueCount == 1) {
$if(BytesRef)$
block = new ConstantBytesRefVector(values.get(0, new BytesRef()), 1, blockFactory).asBlock();
block = new ConstantBytesRefVector(BytesRef.deepCopyOf(values.get(0, new BytesRef())), 1, blockFactory).asBlock();
Releasables.closeExpectNoException(values);
} else {
estimatedBytes += values.ramBytesUsed();
$else$
block = new Constant$Type$Vector(values[0], 1, blockFactory).asBlock();
} else {
Expand All @@ -265,7 +268,6 @@ $endif$
}
}
// update the breaker with the actual bytes used.
// TODO: verify that this can also give back
blockFactory.adjustBreaker(block.ramBytesUsed() - estimatedBytes, true);
return block;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ $if(BytesRef)$
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.BytesRefArray;
import org.elasticsearch.core.Releasables;

$else$
import java.util.Arrays;
Expand Down Expand Up @@ -85,8 +86,10 @@ $endif$
$Type$Vector vector;
if (valueCount == 1) {
$if(BytesRef)$
vector = new ConstantBytesRefVector(values.get(0, new BytesRef()), 1, blockFactory);
vector = new ConstantBytesRefVector(BytesRef.deepCopyOf(values.get(0, new BytesRef())), 1, blockFactory);
Releasables.closeExpectNoException(values);
} else {
estimatedBytes = values.ramBytesUsed();
$else$
vector = new Constant$Type$Vector(values[0], 1, blockFactory);
} else {
Expand All @@ -97,7 +100,6 @@ $endif$
vector = new $Type$ArrayVector(values, valueCount, blockFactory);
}
// update the breaker with the actual bytes used.
// TODO: verify that this can also give back
blockFactory.adjustBreaker(vector.ramBytesUsed() - estimatedBytes, true);
return vector;
}
Expand Down
Loading

0 comments on commit 3bbf9cc

Please sign in to comment.