Skip to content

Commit 9f7c33f

Browse files
committed
Add auto_expand to index template settings
Signed-off-by: Chenyang Ji <cyji@amazon.com>
1 parent 9503023 commit 9f7c33f

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/main/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporter.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
import org.opensearch.action.bulk.BulkRequestBuilder;
3535
import org.opensearch.action.bulk.BulkResponse;
3636
import org.opensearch.action.index.IndexRequest;
37-
// CS-SUPPRESS-SINGLE: RegexpSingleline It is not possible to use phrase "cluster manager" instead of master here
38-
import org.opensearch.action.support.master.AcknowledgedResponse;
3937
import org.opensearch.client.Client;
4038
import org.opensearch.cluster.ClusterState;
4139
import org.opensearch.cluster.metadata.ComposableIndexTemplate;
@@ -65,8 +63,8 @@ public class LocalIndexExporter implements QueryInsightsExporter {
6563
private DateTimeFormatter indexPattern;
6664
private int deleteAfter;
6765
private final String id;
68-
private static final int DEFAULT_NUMBER_OF_REPLICA = 0;
6966
private static final int DEFAULT_NUMBER_OF_SHARDS = 1;
67+
private static final String DEFAULT_AUTO_EXPAND_REPLICAS = "0-2";
7068
private static final String TEMPLATE_NAME = "query_insights_top_queries_template";
7169
private long templatePriority;
7270

@@ -180,7 +178,7 @@ void createIndexAndBulk(String indexName, List<SearchQueryRecord> records) throw
180178
createIndexRequest.settings(
181179
Settings.builder()
182180
.put("index.number_of_shards", DEFAULT_NUMBER_OF_SHARDS)
183-
.put("index.number_of_replicas", DEFAULT_NUMBER_OF_REPLICA)
181+
.put("index.auto_expand_replicas", DEFAULT_AUTO_EXPAND_REPLICAS)
184182
);
185183
createIndexRequest.mapping(readIndexMappings());
186184

@@ -380,7 +378,7 @@ void createTemplate(CompletableFuture<Boolean> future) {
380378
org.opensearch.cluster.metadata.Template template = new org.opensearch.cluster.metadata.Template(
381379
Settings.builder()
382380
.put("index.number_of_shards", DEFAULT_NUMBER_OF_SHARDS)
383-
.put("index.number_of_replicas", DEFAULT_NUMBER_OF_REPLICA)
381+
.put("index.auto_expand_replicas", DEFAULT_AUTO_EXPAND_REPLICAS)
384382
.build(),
385383
compressedMapping,
386384
null
@@ -403,7 +401,8 @@ void createTemplate(CompletableFuture<Boolean> future) {
403401

404402
client.execute(PutComposableIndexTemplateAction.INSTANCE, request, new ActionListener<>() {
405403
@Override
406-
public void onResponse(AcknowledgedResponse response) {
404+
// CS-SUPPRESS-SINGLE: RegexpSingleline It is not possible to use phrase "cluster manager" instead of master here
405+
public void onResponse(org.opensearch.action.support.master.AcknowledgedResponse response) {
407406
if (response.isAcknowledged()) {
408407
logger.info("Successfully created or updated template [{}] with priority {}", TEMPLATE_NAME, templatePriority);
409408
future.complete(true);

src/test/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporterTests.java

+34
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import static org.mockito.ArgumentMatchers.anyString;
1313
import static org.mockito.ArgumentMatchers.eq;
1414
import static org.mockito.Mockito.doAnswer;
15+
import static org.mockito.Mockito.doNothing;
1516
import static org.mockito.Mockito.doReturn;
1617
import static org.mockito.Mockito.doThrow;
1718
import static org.mockito.Mockito.mock;
@@ -26,13 +27,16 @@
2627
import java.time.ZoneOffset;
2728
import java.time.ZonedDateTime;
2829
import java.time.format.DateTimeFormatter;
30+
import java.util.Collections;
2931
import java.util.List;
3032
import java.util.Locale;
3133
import java.util.Map;
3234
import java.util.concurrent.CompletableFuture;
3335
import java.util.concurrent.CountDownLatch;
3436
import java.util.concurrent.TimeUnit;
3537
import org.junit.Before;
38+
import org.mockito.ArgumentCaptor;
39+
import org.opensearch.action.admin.indices.create.CreateIndexRequest;
3640
import org.opensearch.action.admin.indices.exists.indices.IndicesExistsRequest;
3741
import org.opensearch.action.admin.indices.template.get.GetComposableIndexTemplateAction;
3842
import org.opensearch.action.admin.indices.template.put.PutComposableIndexTemplateAction;
@@ -371,4 +375,34 @@ public void testReadIndexMappingsWithEmptyMapping() throws IOException {
371375
LocalIndexExporter exporter = new LocalIndexExporter(client, clusterService, format, "", "id");
372376
assertEquals("{}", exporter.readIndexMappings());
373377
}
378+
379+
/**
380+
* Test that createIndex correctly sets auto_expand_replicas
381+
*/
382+
public void testCreateIndexWithAutoExpandReplicas() throws IOException {
383+
LocalIndexExporter exporterSpy = spy(new LocalIndexExporter(client, clusterService, format, "{}", "id"));
384+
385+
ArgumentCaptor<CreateIndexRequest> requestCaptor = ArgumentCaptor.forClass(CreateIndexRequest.class);
386+
387+
// Mock the client.admin().indices().create() call
388+
AdminClient adminClient = mock(AdminClient.class);
389+
IndicesAdminClient indicesClient = mock(IndicesAdminClient.class);
390+
when(client.admin()).thenReturn(adminClient);
391+
when(adminClient.indices()).thenReturn(indicesClient);
392+
393+
doNothing().when(indicesClient).create(requestCaptor.capture(), any(ActionListener.class));
394+
exporterSpy.createIndexAndBulk("test-index", Collections.emptyList());
395+
396+
// Verify the captured request
397+
CreateIndexRequest capturedRequest = requestCaptor.getValue();
398+
Settings settings = capturedRequest.settings();
399+
400+
// Verify the settings use auto_expand_replicas
401+
assertEquals("test-index", capturedRequest.index());
402+
assertEquals("0-2", settings.get("index.auto_expand_replicas"));
403+
assertEquals("1", settings.get("index.number_of_shards"));
404+
405+
// Verify there is no number_of_replicas setting.
406+
assertNull(settings.get("index.number_of_replicas"));
407+
}
374408
}

0 commit comments

Comments
 (0)