Skip to content

Commit

Permalink
Update index option (opensearch-project#284)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
heemin32 committed Jul 21, 2023
1 parent df6de87 commit 6d5ffa5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public class GeoIpDataFacade {
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, 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_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;
Expand All @@ -88,6 +93,7 @@ public void createIndexIfNotExists(final String indexName) {
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_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));
}
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit 6d5ffa5

Please sign in to comment.