Skip to content

Commit 0463cf0

Browse files
committed
Add auto_expand to index template settings
Signed-off-by: Chenyang Ji <cyji@amazon.com>
1 parent 59b059a commit 0463cf0

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
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;
37+
import org.opensearch.action.support.clustermanager.AcknowledgedResponse;
3938
import org.opensearch.client.Client;
4039
import org.opensearch.cluster.ClusterState;
4140
import org.opensearch.cluster.metadata.ComposableIndexTemplate;
@@ -65,8 +64,8 @@ public class LocalIndexExporter implements QueryInsightsExporter {
6564
private DateTimeFormatter indexPattern;
6665
private int deleteAfter;
6766
private final String id;
68-
private static final int DEFAULT_NUMBER_OF_REPLICA = 1;
6967
private static final int DEFAULT_NUMBER_OF_SHARDS = 1;
68+
private static final String DEFAULT_AUTO_EXPAND_REPLICAS = "0-2";
7069
private static final String TEMPLATE_NAME = "query_insights_top_queries_template";
7170
private long templatePriority;
7271

@@ -180,7 +179,7 @@ void createIndexAndBulk(String indexName, List<SearchQueryRecord> records) throw
180179
createIndexRequest.settings(
181180
Settings.builder()
182181
.put("index.number_of_shards", DEFAULT_NUMBER_OF_SHARDS)
183-
.put("index.number_of_replicas", DEFAULT_NUMBER_OF_REPLICA)
182+
.put("index.auto_expand_replicas", DEFAULT_AUTO_EXPAND_REPLICAS)
184183
);
185184
createIndexRequest.mapping(readIndexMappings());
186185

@@ -380,7 +379,7 @@ void createTemplate(CompletableFuture<Boolean> future) {
380379
org.opensearch.cluster.metadata.Template template = new org.opensearch.cluster.metadata.Template(
381380
Settings.builder()
382381
.put("index.number_of_shards", DEFAULT_NUMBER_OF_SHARDS)
383-
.put("index.number_of_replicas", DEFAULT_NUMBER_OF_REPLICA)
382+
.put("index.auto_expand_replicas", DEFAULT_AUTO_EXPAND_REPLICAS)
384383
.build(),
385384
compressedMapping,
386385
null
@@ -403,7 +402,8 @@ void createTemplate(CompletableFuture<Boolean> future) {
403402

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

src/main/java/org/opensearch/plugin/insights/settings/QueryInsightsSettings.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class QueryInsightsSettings {
6161
/** Default N size for top N queries */
6262
public static final int MAX_N_SIZE = 100;
6363
/** Default window size in seconds to keep the top N queries with latency data in query insight store */
64-
public static final TimeValue DEFAULT_WINDOW_SIZE = new TimeValue(5, TimeUnit.MINUTES);
64+
public static final TimeValue DEFAULT_WINDOW_SIZE = new TimeValue(1, TimeUnit.MINUTES);
6565
/** Default top N size to keep the data in query insight store */
6666
public static final int DEFAULT_TOP_N_SIZE = 10;
6767
/**

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)