diff --git a/src/main/java/org/opensearch/geospatial/constants/IndexSetting.java b/src/main/java/org/opensearch/geospatial/constants/IndexSetting.java new file mode 100644 index 00000000..c2869029 --- /dev/null +++ b/src/main/java/org/opensearch/geospatial/constants/IndexSetting.java @@ -0,0 +1,15 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.geospatial.constants; + +public class IndexSetting { + public static final String NUMBER_OF_SHARDS = "index.number_of_shards"; + public static final String NUMBER_OF_REPLICAS = "index.number_of_replicas"; + public static final String REFRESH_INTERVAL = "index.refresh_interval"; + public static final String AUTO_EXPAND_REPLICAS = "index.auto_expand_replicas"; + public static final String HIDDEN = "index.hidden"; + public static final String BLOCKS_WRITE = "index.blocks.write"; +} 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 4b6d9fe9..669686c3 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/common/GeoIpDataFacade.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/common/GeoIpDataFacade.java @@ -18,7 +18,6 @@ import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -51,7 +50,6 @@ import org.opensearch.client.Requests; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.SuppressForbidden; -import org.opensearch.common.collect.Tuple; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.unit.TimeValue; import org.opensearch.common.xcontent.XContentFactory; @@ -59,6 +57,7 @@ import org.opensearch.common.xcontent.XContentType; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.geospatial.annotation.VisibleForTesting; +import org.opensearch.geospatial.constants.IndexSetting; import org.opensearch.geospatial.shared.Constants; import org.opensearch.geospatial.shared.StashedThreadContext; import org.opensearch.index.query.QueryBuilders; @@ -70,12 +69,22 @@ public class GeoIpDataFacade { private static final String IP_RANGE_FIELD_NAME = "_cidr"; 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_NUM_OF_REPLICAS = new Tuple<>("index.number_of_replicas", 0); - private static final Tuple INDEX_SETTING_REFRESH_INTERVAL = new Tuple<>("index.refresh_interval", -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_BLOCKS_WRITE = new Tuple<>("index.blocks.write", true); + private static final Map INDEX_SETTING_TO_CREATE = Map.of( + IndexSetting.NUMBER_OF_SHARDS, + 1, + IndexSetting.NUMBER_OF_REPLICAS, + 0, + IndexSetting.REFRESH_INTERVAL, + -1, + IndexSetting.HIDDEN, + true + ); + private static final Map INDEX_SETTING_TO_FREEZE = Map.of( + IndexSetting.AUTO_EXPAND_REPLICAS, + "0-all", + IndexSetting.BLOCKS_WRITE, + true + ); private final ClusterService clusterService; private final ClusterSettings clusterSettings; private final Client client; @@ -100,12 +109,8 @@ public void createIndexIfNotExists(final String indexName) { if (clusterService.state().metadata().hasIndex(indexName) == true) { return; } - final Map indexSettings = new HashMap<>(); - indexSettings.put(INDEX_SETTING_NUM_OF_SHARDS.v1(), INDEX_SETTING_NUM_OF_SHARDS.v2()); - indexSettings.put(INDEX_SETTING_REFRESH_INTERVAL.v1(), INDEX_SETTING_REFRESH_INTERVAL.v2()); - indexSettings.put(INDEX_SETTING_NUM_OF_REPLICAS.v1(), INDEX_SETTING_NUM_OF_REPLICAS.v2()); - indexSettings.put(INDEX_SETTING_HIDDEN.v1(), INDEX_SETTING_HIDDEN.v2()); - final CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName).settings(indexSettings).mapping(getIndexMapping()); + final CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName).settings(INDEX_SETTING_TO_CREATE) + .mapping(getIndexMapping()); StashedThreadContext.run( client, () -> client.admin().indices().create(createIndexRequest).actionGet(clusterSettings.get(Ip2GeoSettings.TIMEOUT)) @@ -117,13 +122,10 @@ private void freezeIndex(final String indexName) { StashedThreadContext.run(client, () -> { client.admin().indices().prepareRefresh(indexName).execute().actionGet(timeout); client.admin().indices().prepareForceMerge(indexName).setMaxNumSegments(1).execute().actionGet(timeout); - Map settings = new HashMap<>(); - settings.put(INDEX_SETTING_BLOCKS_WRITE.v1(), INDEX_SETTING_BLOCKS_WRITE.v2()); - settings.put(INDEX_SETTING_AUTO_EXPAND_REPLICAS.v1(), INDEX_SETTING_AUTO_EXPAND_REPLICAS.v2()); client.admin() .indices() .prepareUpdateSettings(indexName) - .setSettings(settings) + .setSettings(INDEX_SETTING_TO_FREEZE) .execute() .actionGet(clusterSettings.get(Ip2GeoSettings.TIMEOUT)); }); diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/jobscheduler/DatasourceExtension.java b/src/main/java/org/opensearch/geospatial/ip2geo/jobscheduler/DatasourceExtension.java index 1cf51338..ce610ebe 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/jobscheduler/DatasourceExtension.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/jobscheduler/DatasourceExtension.java @@ -5,6 +5,10 @@ package org.opensearch.geospatial.ip2geo.jobscheduler; +import static org.opensearch.geospatial.constants.IndexSetting.AUTO_EXPAND_REPLICAS; +import static org.opensearch.geospatial.constants.IndexSetting.HIDDEN; +import static org.opensearch.geospatial.constants.IndexSetting.NUMBER_OF_SHARDS; + import java.util.Map; import org.opensearch.jobscheduler.spi.JobSchedulerExtension; @@ -29,14 +33,7 @@ public class DatasourceExtension implements JobSchedulerExtension { * We want it to be single shard so that job can be run only in a single node by job scheduler. * We want it to expand to all replicas so that querying to this index can be done locally to reduce latency. */ - public static final Map INDEX_SETTING = Map.of( - "index.number_of_shards", - 1, - "index.auto_expand_replicas", - "0-all", - "index.hidden", - true - ); + public static final Map INDEX_SETTING = Map.of(NUMBER_OF_SHARDS, 1, AUTO_EXPAND_REPLICAS, "0-all", HIDDEN, true); @Override public String getJobType() {