Skip to content

Commit ba9aa64

Browse files
authored
ESQL: Fix flakiness in SessionUtilsTests (#135375)
Fix unreleased arrays in SessionUtilsTests. Closes #135377 Closes #135398
1 parent 56d0d43 commit ba9aa64

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

muted-tests.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,15 +594,9 @@ tests:
594594
- class: org.elasticsearch.test.rest.yaml.CcsCommonYamlTestSuiteIT
595595
method: test {p0=search/510_range_query_out_of_bounds/Test range query for half_float field with out of bounds upper limit}
596596
issue: https://github.com/elastic/elasticsearch/issues/135365
597-
- class: org.elasticsearch.xpack.esql.session.SessionUtilsTests
598-
method: testFromPages
599-
issue: https://github.com/elastic/elasticsearch/issues/135377
600597
- class: org.elasticsearch.xpack.esql.expression.function.scalar.score.DecayTests
601598
method: "testEvaluateBlockWithoutNulls {TestCase=<date_nanos>, <date_nanos>, <time_duration>, <_source> #12}"
602599
issue: https://github.com/elastic/elasticsearch/issues/135394
603-
- class: org.elasticsearch.xpack.esql.session.SessionUtilsTests
604-
method: testCheckPagesBelowSize
605-
issue: https://github.com/elastic/elasticsearch/issues/135398
606600
- class: org.elasticsearch.upgrades.DataStreamsUpgradeIT
607601
method: testDataStreamValidationDoesNotBreakUpgrade
608602
issue: https://github.com/elastic/elasticsearch/issues/135406

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/session/SessionUtilsTests.java

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -126,39 +126,45 @@ private static PagesRec generatePageSet(BlockFactory blockFactory) {
126126

127127
// Generates a list of Pages with one BytesRef block, each of different positions, filled with random bytes.
128128
private static PagesRec generatePages(int minBytes, int maxBytes, BlockFactory blockFactory) {
129-
BytesRefBlock.Builder builder = blockFactory.newBytesRefBlockBuilder(maxBytes);
130-
131-
byte[] buffer = new byte[maxBytes];
132-
List<Page> pages = new ArrayList<>();
133-
134-
int producedBytes = 0;
135-
int producedRows = 0;
136-
int rowsPerPage = randomIntBetween(1, 100);
137-
int rows = 0;
138-
while (producedBytes < maxBytes) {
139-
int rowBytes = Math.min(randomIntBetween(1, maxBytes / minBytes), maxBytes - producedBytes);
140-
byte[] rowValue = randomByteArrayOfLength(rowBytes);
141-
142-
builder.appendBytesRef(new BytesRef(rowValue));
143-
System.arraycopy(rowValue, 0, buffer, producedBytes, rowBytes);
144-
145-
producedBytes += rowBytes;
146-
rows++;
147-
148-
if (rows > rowsPerPage) {
129+
BytesRefBlock.Builder builder = null;
130+
try {
131+
builder = blockFactory.newBytesRefBlockBuilder(maxBytes);
132+
133+
byte[] buffer = new byte[maxBytes];
134+
List<Page> pages = new ArrayList<>();
135+
136+
int producedBytes = 0;
137+
int producedRows = 0;
138+
int rowsPerPage = randomIntBetween(1, 100);
139+
int rows = 0;
140+
while (producedBytes < maxBytes) {
141+
int rowBytes = Math.min(randomIntBetween(1, maxBytes / minBytes), maxBytes - producedBytes);
142+
byte[] rowValue = randomByteArrayOfLength(rowBytes);
143+
144+
builder.appendBytesRef(new BytesRef(rowValue));
145+
System.arraycopy(rowValue, 0, buffer, producedBytes, rowBytes);
146+
147+
producedBytes += rowBytes;
148+
rows++;
149+
150+
if (rows > rowsPerPage) {
151+
producedRows += rows;
152+
rows = 0;
153+
enqueueBlock(builder, pages);
154+
Releasables.close(builder);
155+
builder = blockFactory.newBytesRefBlockBuilder(maxBytes);
156+
rowsPerPage = randomIntBetween(1, 100);
157+
}
158+
}
159+
if (rows > 0) {
149160
producedRows += rows;
150-
rows = 0;
151161
enqueueBlock(builder, pages);
152-
builder = blockFactory.newBytesRefBlockBuilder(maxBytes);
153-
rowsPerPage = randomIntBetween(1, 100);
154162
}
155-
}
156-
if (rows > 0) {
157-
producedRows += rows;
158-
enqueueBlock(builder, pages);
159-
}
160163

161-
return new PagesRec(pages, buffer, producedBytes, producedRows);
164+
return new PagesRec(pages, buffer, producedBytes, producedRows);
165+
} finally {
166+
Releasables.close(builder);
167+
}
162168
}
163169

164170
private BlockFactory blockFactory(long maxBytes) {
@@ -172,6 +178,5 @@ private BlockFactory blockFactory(long maxBytes) {
172178
private static void enqueueBlock(BytesRefBlock.Builder builder, List<Page> pages) {
173179
Block block = builder.build();
174180
pages.add(new Page(block));
175-
Releasables.close(builder);
176181
}
177182
}

0 commit comments

Comments
 (0)