Skip to content

Commit

Permalink
Move index setting keys to constants (#321)
Browse files Browse the repository at this point in the history
Signed-off-by: Heemin Kim <[email protected]>
  • Loading branch information
heemin32 authored May 24, 2023
1 parent c9e40fa commit 617f355
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.geospatial.constants;

/**
* Collection of keys for index setting
*/
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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,14 +51,14 @@
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;
import org.opensearch.common.xcontent.XContentHelper;
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;
Expand All @@ -71,12 +70,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<String, Integer> INDEX_SETTING_NUM_OF_SHARDS = new Tuple<>("index.number_of_shards", 1);
private static final Tuple<String, Integer> INDEX_SETTING_NUM_OF_REPLICAS = new Tuple<>("index.number_of_replicas", 0);
private static final Tuple<String, Integer> INDEX_SETTING_REFRESH_INTERVAL = new Tuple<>("index.refresh_interval", -1);
private static final Tuple<String, String> INDEX_SETTING_AUTO_EXPAND_REPLICAS = new Tuple<>("index.auto_expand_replicas", "0-all");
private static final Tuple<String, Boolean> INDEX_SETTING_HIDDEN = new Tuple<>("index.hidden", true);
private static final Tuple<String, Boolean> INDEX_SETTING_BLOCKS_WRITE = new Tuple<>("index.blocks.write", true);
private static final Map<String, Object> 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<String, Object> 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;
Expand All @@ -101,12 +110,8 @@ public void createIndexIfNotExists(final String indexName) {
if (clusterService.state().metadata().hasIndex(indexName) == true) {
return;
}
final Map<String, Object> 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))
Expand All @@ -118,13 +123,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<String, Object> 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));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, Object> INDEX_SETTING = Map.of(
"index.number_of_shards",
1,
"index.auto_expand_replicas",
"0-all",
"index.hidden",
true
);
public static final Map<String, Object> INDEX_SETTING = Map.of(NUMBER_OF_SHARDS, 1, AUTO_EXPAND_REPLICAS, "0-all", HIDDEN, true);

@Override
public String getJobType() {
Expand Down

0 comments on commit 617f355

Please sign in to comment.