From 281dc5c73ab8a73b9ac622dfb4f2fa05207bb451 Mon Sep 17 00:00:00 2001 From: Heemin Kim Date: Thu, 4 May 2023 15:07:03 -0700 Subject: [PATCH] Update index option (#284) 1. Make geodata index as hidden 2. Make geodata index as read only allow delete after creation is done 3. Refresh datasource index immediately after update Signed-off-by: Heemin Kim --- .../geospatial/ip2geo/common/DatasourceFacade.java | 2 ++ .../geospatial/ip2geo/common/GeoIpDataFacade.java | 12 ++++++++++++ .../ip2geo/common/DatasourceFacadeTests.java | 2 ++ .../ip2geo/common/GeoIpDataFacadeTests.java | 11 ++++++++++- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/common/DatasourceFacade.java b/src/main/java/org/opensearch/geospatial/ip2geo/common/DatasourceFacade.java index 25d2f5af..c3be7fef 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/common/DatasourceFacade.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/common/DatasourceFacade.java @@ -27,6 +27,7 @@ import org.opensearch.action.index.IndexRequestBuilder; import org.opensearch.action.index.IndexResponse; import org.opensearch.action.search.SearchResponse; +import org.opensearch.action.support.WriteRequest; import org.opensearch.client.Client; import org.opensearch.common.bytes.BytesReference; import org.opensearch.common.settings.ClusterSettings; @@ -67,6 +68,7 @@ public IndexResponse updateDatasource(final Datasource datasource) throws IOExce IndexRequestBuilder requestBuilder = client.prepareIndex(DatasourceExtension.JOB_INDEX_NAME); requestBuilder.setId(datasource.getName()); requestBuilder.setOpType(DocWriteRequest.OpType.INDEX); + requestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); requestBuilder.setSource(datasource.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS)); return client.index(requestBuilder.request()).actionGet(clusterSettings.get(Ip2GeoSettings.TIMEOUT)); } diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/common/GeoIpDataFacade.java b/src/main/java/org/opensearch/geospatial/ip2geo/common/GeoIpDataFacade.java index c1fb3a70..92650020 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/common/GeoIpDataFacade.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/common/GeoIpDataFacade.java @@ -66,6 +66,11 @@ public class GeoIpDataFacade { private static final String DATA_FIELD_NAME = "_data"; private static final Tuple INDEX_SETTING_NUM_OF_SHARDS = new Tuple<>("index.number_of_shards", 1); private static final Tuple INDEX_SETTING_AUTO_EXPAND_REPLICAS = new Tuple<>("index.auto_expand_replicas", "0-all"); + private static final Tuple INDEX_SETTING_HIDDEN = new Tuple<>("index.hidden", true); + private static final Tuple INDEX_SETTING_READ_ONLY_ALLOW_DELETE = new Tuple<>( + "index.blocks.read_only_allow_delete", + true + ); private final ClusterService clusterService; private final ClusterSettings clusterSettings; private final Client client; @@ -88,6 +93,7 @@ public void createIndexIfNotExists(final String indexName) { final Map indexSettings = new HashMap<>(); indexSettings.put(INDEX_SETTING_NUM_OF_SHARDS.v1(), INDEX_SETTING_NUM_OF_SHARDS.v2()); indexSettings.put(INDEX_SETTING_AUTO_EXPAND_REPLICAS.v1(), INDEX_SETTING_AUTO_EXPAND_REPLICAS.v2()); + indexSettings.put(INDEX_SETTING_HIDDEN.v1(), INDEX_SETTING_HIDDEN.v2()); final CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName).settings(indexSettings).mapping(getIndexMapping()); client.admin().indices().create(createIndexRequest).actionGet(clusterSettings.get(Ip2GeoSettings.TIMEOUT)); } @@ -345,6 +351,12 @@ public void putGeoIpData(final String indexName, final String[] fields, final It } client.admin().indices().prepareRefresh(indexName).execute().actionGet(timeout); client.admin().indices().prepareForceMerge(indexName).setMaxNumSegments(1).execute().actionGet(timeout); + client.admin() + .indices() + .prepareUpdateSettings(indexName) + .setSettings(Map.of(INDEX_SETTING_READ_ONLY_ALLOW_DELETE.v1(), INDEX_SETTING_READ_ONLY_ALLOW_DELETE.v2())) + .execute() + .actionGet(timeout); } public AcknowledgedResponse deleteIp2GeoDataIndex(final String index) { diff --git a/src/test/java/org/opensearch/geospatial/ip2geo/common/DatasourceFacadeTests.java b/src/test/java/org/opensearch/geospatial/ip2geo/common/DatasourceFacadeTests.java index aacd940a..9f114751 100644 --- a/src/test/java/org/opensearch/geospatial/ip2geo/common/DatasourceFacadeTests.java +++ b/src/test/java/org/opensearch/geospatial/ip2geo/common/DatasourceFacadeTests.java @@ -33,6 +33,7 @@ import org.opensearch.action.index.IndexRequest; import org.opensearch.action.search.SearchRequest; import org.opensearch.action.search.SearchResponse; +import org.opensearch.action.support.WriteRequest; import org.opensearch.common.Randomness; import org.opensearch.common.bytes.BytesReference; import org.opensearch.common.settings.ClusterSettings; @@ -75,6 +76,7 @@ public void testUpdateDatasource_whenValidInput_thenSucceed() throws Exception { assertEquals(datasource.getName(), request.id()); assertEquals(DocWriteRequest.OpType.INDEX, request.opType()); assertEquals(DatasourceExtension.JOB_INDEX_NAME, request.index()); + assertEquals(WriteRequest.RefreshPolicy.IMMEDIATE, request.getRefreshPolicy()); return null; }); diff --git a/src/test/java/org/opensearch/geospatial/ip2geo/common/GeoIpDataFacadeTests.java b/src/test/java/org/opensearch/geospatial/ip2geo/common/GeoIpDataFacadeTests.java index 786fc6f9..dd54bf88 100644 --- a/src/test/java/org/opensearch/geospatial/ip2geo/common/GeoIpDataFacadeTests.java +++ b/src/test/java/org/opensearch/geospatial/ip2geo/common/GeoIpDataFacadeTests.java @@ -42,6 +42,7 @@ import org.opensearch.action.admin.indices.delete.DeleteIndexRequest; import org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest; import org.opensearch.action.admin.indices.refresh.RefreshRequest; +import org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest; import org.opensearch.action.bulk.BulkRequest; import org.opensearch.action.bulk.BulkResponse; import org.opensearch.action.search.MultiSearchRequest; @@ -86,8 +87,10 @@ public void testCreateIndexIfNotExistsWithoutExistingIndex() { assertTrue(actionRequest instanceof CreateIndexRequest); CreateIndexRequest request = (CreateIndexRequest) actionRequest; assertEquals(index, request.index()); - assertEquals("1", request.settings().get("index.number_of_shards")); + assertEquals(1, (int) request.settings().getAsInt("index.number_of_shards", 2)); assertEquals("0-all", request.settings().get("index.auto_expand_replicas")); + assertEquals(true, request.settings().getAsBoolean("index.hidden", false)); + assertEquals( "{\"dynamic\": false,\"properties\": {\"_cidr\": {\"type\": \"ip_range\",\"doc_values\": false}}}", request.mappings() @@ -178,6 +181,12 @@ public void testPutGeoIpData() throws Exception { assertEquals(index, request.indices()[0]); assertEquals(1, request.maxNumSegments()); return null; + } else if (actionRequest instanceof UpdateSettingsRequest) { + UpdateSettingsRequest request = (UpdateSettingsRequest) actionRequest; + assertEquals(1, request.indices().length); + assertEquals(index, request.indices()[0]); + assertEquals(true, request.settings().getAsBoolean("index.blocks.read_only_allow_delete", false)); + return null; } else { throw new RuntimeException("invalid request is called"); }