Skip to content

Commit de156a9

Browse files
committed
Fix dynamic index refresh to target written indices instead of global refresh
Signed-off-by: Sotaro Hikita <bering1814@gmail.com>
1 parent 7128205 commit de156a9

6 files changed

Lines changed: 101 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1212
### Removed
1313

1414
### Fixed
15+
- Fixed global refresh when using dynamic index patterns ([#324](https://github.com/opensearch-project/opensearch-hadoop/issues/324))
1516
- Fixed build failures when downloading Apache project dependencies (Hadoop, Hive, Spark) ([#595](https://github.com/opensearch-project/opensearch-hadoop/pull/595))
1617

1718
### Security

mr/src/main/java/org/opensearch/hadoop/rest/RestClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ public void refresh(Resource resource) {
299299
execute(POST, resource.refresh());
300300
}
301301

302+
public void refreshIndex(String index) {
303+
execute(POST, index + "/_refresh");
304+
}
305+
302306
public List<List<Map<String, Object>>> targetShards(String index, String routing) {
303307
List<List<Map<String, Object>>> shardsJson = null;
304308

mr/src/main/java/org/opensearch/hadoop/rest/bulk/BulkProcessor.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
import java.io.Closeable;
3333
import java.util.ArrayList;
3434
import java.util.Iterator;
35+
import java.util.LinkedHashSet;
3536
import java.util.List;
3637
import java.util.Map;
38+
import java.util.Set;
3739

3840
import org.apache.commons.logging.Log;
3941
import org.apache.commons.logging.LogFactory;
@@ -90,6 +92,7 @@ public class BulkProcessor implements Closeable, StatsAware {
9092
private boolean executedBulkWrite = false;
9193
private boolean hadWriteErrors = false;
9294
private boolean requiresRefreshAfterBulk = false;
95+
private final Set<String> writtenIndices = new LinkedHashSet<>();
9396

9497
// Bulk write error handlers.
9598
private List<IBulkWriteErrorHandler> documentBulkErrorHandlers;
@@ -275,6 +278,10 @@ public BulkResponse tryFlush() {
275278

276279
if (error == null){
277280
// Write operation for this entry succeeded
281+
String idx = (String) values.get("_index");
282+
if (idx != null) {
283+
writtenIndices.add(idx);
284+
}
278285
stats.bytesAccepted += data.length(trackingBytesPosition);
279286
stats.docsAccepted += 1;
280287
docsSent += 1;
@@ -574,12 +581,13 @@ public void close() {
574581
}
575582
}
576583

577-
if (requiresRefreshAfterBulk && executedBulkWrite) {
578-
// refresh batch
579-
restClient.refresh(resource);
584+
if (requiresRefreshAfterBulk && executedBulkWrite && !writtenIndices.isEmpty()) {
585+
for (String idx : writtenIndices) {
586+
restClient.refreshIndex(idx);
587+
}
580588

581589
if (LOG.isDebugEnabled()) {
582-
LOG.debug(String.format("Refreshing index [%s]", resource));
590+
LOG.debug(String.format("Refreshing index %s", writtenIndices));
583591
}
584592
}
585593
} finally {

mr/src/test/java/org/opensearch/hadoop/rest/bulk/BulkOutputGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public interface BulkOutputGenerator {
4343

4444
public BulkOutputGenerator addSuccess(String operation, int status);
4545

46+
public BulkOutputGenerator addSuccess(String operation, int status, String index);
47+
4648
public BulkOutputGenerator addFailure(String operation, int status, String type, String errorMessage);
4749

4850
public BulkOutputGenerator addRejection(String operation);

mr/src/test/java/org/opensearch/hadoop/rest/bulk/BulkProcessorTest.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,4 +967,80 @@ private static byte[] copyDocumentBytes(BulkWriteFailure entry) throws IOExcepti
967967
assertEquals(-1, inputStream.read());
968968
return copyDoc;
969969
}
970+
971+
@Test
972+
public void testBulk10_RefreshSingleIndex() throws Exception {
973+
RestClient.BulkActionResponse response = generator.setInfo(resource, 56)
974+
.addSuccess("index", 201)
975+
.addSuccess("index", 201)
976+
.addSuccess("index", 201)
977+
.generate();
978+
979+
RestClient mockClient = mockClientResponses(response);
980+
BulkProcessor processor = new BulkProcessor(mockClient, resource, testSettings);
981+
982+
BytesRef data = new BytesRef();
983+
data.add(renderEntry("A"));
984+
processor.add(data);
985+
data.reset();
986+
data.add(renderEntry("B"));
987+
processor.add(data);
988+
data.reset();
989+
data.add(renderEntry("C"));
990+
processor.add(data);
991+
992+
processor.close();
993+
994+
Mockito.verify(mockClient).refreshIndex("foo");
995+
Mockito.verify(mockClient, Mockito.never()).refresh(Mockito.any(Resource.class));
996+
}
997+
998+
@Test
999+
public void testBulk10_RefreshMultipleDynamicIndices() throws Exception {
1000+
RestClient.BulkActionResponse response = generator.setInfo(resource, 56)
1001+
.addSuccess("index", 201, "logs-2026-01")
1002+
.addSuccess("index", 201, "logs-2026-02")
1003+
.addSuccess("index", 201, "logs-2026-01")
1004+
.addSuccess("index", 201, "logs-2026-03")
1005+
.addSuccess("index", 201, "logs-2026-02")
1006+
.generate();
1007+
1008+
RestClient mockClient = mockClientResponses(response);
1009+
BulkProcessor processor = new BulkProcessor(mockClient, resource, testSettings);
1010+
1011+
processData(processor);
1012+
1013+
processor.close();
1014+
1015+
Mockito.verify(mockClient).refreshIndex("logs-2026-01");
1016+
Mockito.verify(mockClient).refreshIndex("logs-2026-02");
1017+
Mockito.verify(mockClient).refreshIndex("logs-2026-03");
1018+
Mockito.verify(mockClient, Mockito.times(3)).refreshIndex(Mockito.anyString());
1019+
Mockito.verify(mockClient, Mockito.never()).refresh(Mockito.any(Resource.class));
1020+
}
1021+
1022+
@Test
1023+
public void testBulk10_NoRefreshWhenDisabled() throws Exception {
1024+
testSettings.setProperty(ConfigurationOptions.OPENSEARCH_BATCH_WRITE_REFRESH, "false");
1025+
1026+
RestClient.BulkActionResponse response = generator.setInfo(resource, 56)
1027+
.addSuccess("index", 201)
1028+
.addSuccess("index", 201)
1029+
.generate();
1030+
1031+
RestClient mockClient = mockClientResponses(response);
1032+
BulkProcessor processor = new BulkProcessor(mockClient, resource, testSettings);
1033+
1034+
BytesRef data = new BytesRef();
1035+
data.add(renderEntry("A"));
1036+
processor.add(data);
1037+
data.reset();
1038+
data.add(renderEntry("B"));
1039+
processor.add(data);
1040+
1041+
processor.close();
1042+
1043+
Mockito.verify(mockClient, Mockito.never()).refreshIndex(Mockito.anyString());
1044+
Mockito.verify(mockClient, Mockito.never()).refresh(Mockito.any(Resource.class));
1045+
}
9701046
}

mr/src/test/java/org/opensearch/hadoop/rest/bulk/bwc/BulkOutputGeneratorBase.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,15 @@ public BulkOutputGenerator setInfo(Resource resource, long took) {
115115

116116
@Override
117117
public BulkOutputGenerator addSuccess(String operation, int status) {
118+
return addSuccess(operation, status, resource.index());
119+
}
120+
121+
@Override
122+
public BulkOutputGenerator addSuccess(String operation, int status, String index) {
118123
Assert.notNull(resource);
119124
items.add(getSuccess()
120125
.replace(OP, operation)
121-
.replace(IDX, resource.index())
126+
.replace(IDX, index)
122127
.replace(TYPE, resource.type())
123128
.replace(ID, UUID.randomUUID().toString())
124129
.replace(VER, "1")

0 commit comments

Comments
 (0)