Skip to content

Commit d8b3c15

Browse files
committed
fixed bug
1 parent 020bae7 commit d8b3c15

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

server/src/main/java/org/elasticsearch/index/codec/tsdb/ES87TSDBDocValuesConsumer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,13 @@ private long[] writeField(FieldInfo field, DocValuesProducer valuesProducer, lon
176176
final TSDBDocValuesEncoder encoder = new TSDBDocValuesEncoder(ES87TSDBDocValuesFormat.NUMERIC_BLOCK_SIZE);
177177
values = valuesProducer.getSortedNumeric(field);
178178
final int bitsPerOrd = maxOrd >= 0 ? PackedInts.bitsRequired(maxOrd - 1) : -1;
179+
180+
// Reset and recompute. The value gathered from TsdbDocValuesProducer may not be accurate if one of the leaves was singleton
181+
// This could cause failures when writing addresses in writeSortedNumericField(...)
182+
numDocsWithValue = 0;
183+
179184
for (int doc = values.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = values.nextDoc()) {
185+
numDocsWithValue++;
180186
final int count = values.docValueCount();
181187
for (int i = 0; i < count; ++i) {
182188
buffer[bufferSize++] = values.nextValue();

server/src/test/java/org/elasticsearch/index/codec/tsdb/ES87TSDBDocValuesFormatTests.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,11 @@ public void testWithNoValueMultiValue() throws Exception {
369369
String timestampField = "@timestamp";
370370
String hostnameField = "host.name";
371371
long baseTimestamp = 1704067200000L;
372+
int numRounds = 32 + random().nextInt(32);
373+
int numDocsPerRound = 64 + random().nextInt(64);
372374

373375
var config = getTimeSeriesIndexWriterConfig(hostnameField, timestampField);
374376
try (var dir = newDirectory(); var iw = new IndexWriter(dir, config)) {
375-
int numRounds = 4 + random().nextInt(28);
376-
int numDocsPerRound = 8 + random().nextInt(56);
377377
long[] gauge1Values = new long[] { 2, 4, 6, 8, 10, 12, 14, 16 };
378378
String[] tags = new String[] { "tag_1", "tag_2", "tag_3", "tag_4", "tag_5", "tag_6", "tag_7", "tag_8" };
379379
{
@@ -382,26 +382,27 @@ public void testWithNoValueMultiValue() throws Exception {
382382
int r = random().nextInt(10);
383383
for (int j = 0; j < numDocsPerRound; j++) {
384384
var d = new Document();
385-
String hostName = String.format(Locale.ROOT, "host-%03d", i);
385+
// host in reverse, otherwise merging will detect that segments are already ordered and will use sequential docid
386+
// merger:
387+
String hostName = String.format(Locale.ROOT, "host-%03d", numRounds - i);
386388
d.add(new SortedDocValuesField(hostnameField, new BytesRef(hostName)));
387389
// Index sorting doesn't work with NumericDocValuesField:
388390
d.add(new SortedNumericDocValuesField(timestampField, timestamp++));
389391

390392
if (r % 10 == 5) {
391393
// sometimes no values
392394
} else if (r % 10 > 5) {
393-
// often multiple values:
395+
// often single value:
396+
d.add(new SortedNumericDocValuesField("gauge_1", gauge1Values[j % gauge1Values.length]));
397+
d.add(new SortedSetDocValuesField("tags", new BytesRef(tags[j % tags.length])));
398+
} else {
399+
// otherwise multiple values:
394400
int numValues = 2 + random().nextInt(4);
395401
for (int k = 0; k < numValues; k++) {
396402
d.add(new SortedNumericDocValuesField("gauge_1", gauge1Values[(j + k) % gauge1Values.length]));
397403
d.add(new SortedSetDocValuesField("tags", new BytesRef(tags[(j + k) % tags.length])));
398404
}
399-
} else {
400-
// otherwise single value:
401-
d.add(new SortedNumericDocValuesField("gauge_1", gauge1Values[j % gauge1Values.length]));
402-
d.add(new SortedSetDocValuesField("tags", new BytesRef(tags[j % tags.length])));
403405
}
404-
405406
iw.addDocument(d);
406407
}
407408
iw.commit();
@@ -424,10 +425,8 @@ public void testWithNoValueMultiValue() throws Exception {
424425
assertNotNull(tagsDV);
425426
for (int i = 0; i < numDocs; i++) {
426427
assertEquals(i, hostNameDV.nextDoc());
427-
int round = i / numDocsPerRound;
428-
String expectedHostName = String.format(Locale.ROOT, "host-%03d", round);
429428
String actualHostName = hostNameDV.lookupOrd(hostNameDV.ordValue()).utf8ToString();
430-
assertEquals(expectedHostName, actualHostName);
429+
assertTrue("unexpected host name:" + actualHostName, actualHostName.startsWith("host-"));
431430

432431
assertEquals(i, timestampDV.nextDoc());
433432
long timestamp = timestampDV.longValue();

0 commit comments

Comments
 (0)