use of org.opensearch.action.admin.indices.create.CreateIndexRequest in project anomaly-detection by opensearch-project.
the class InitAnomalyDetectionIndicesTests method fixedPrimaryShardsIndexCreationTemplate.
@SuppressWarnings("unchecked")
private void fixedPrimaryShardsIndexCreationTemplate(String index) throws IOException {
doAnswer(invocation -> {
CreateIndexRequest request = invocation.getArgument(0);
assertEquals(index, request.index());
ActionListener<CreateIndexResponse> listener = (ActionListener<CreateIndexResponse>) invocation.getArgument(1);
listener.onResponse(new CreateIndexResponse(true, true, index));
return null;
}).when(indicesClient).create(any(), any());
ActionListener<CreateIndexResponse> listener = mock(ActionListener.class);
if (index.equals(AnomalyDetector.ANOMALY_DETECTORS_INDEX)) {
adIndices.initAnomalyDetectorIndexIfAbsent(listener);
} else {
adIndices.initDetectionStateIndex(listener);
}
ArgumentCaptor<CreateIndexResponse> captor = ArgumentCaptor.forClass(CreateIndexResponse.class);
verify(listener).onResponse(captor.capture());
CreateIndexResponse result = captor.getValue();
assertEquals(index, result.index());
}
use of org.opensearch.action.admin.indices.create.CreateIndexRequest in project anomaly-detection by opensearch-project.
the class PreviewAnomalyDetectorTransportActionTests method testPreviewTransportActionWithIndex.
@Test
public void testPreviewTransportActionWithIndex() throws IOException, InterruptedException {
// When AD index exists, and detector does not exist
final CountDownLatch inProgressLatch = new CountDownLatch(1);
PreviewAnomalyDetectorRequest request = new PreviewAnomalyDetectorRequest(null, "1234", Instant.now(), Instant.now());
Settings indexSettings = Settings.builder().put("index.number_of_shards", 5).put("index.number_of_replicas", 1).build();
CreateIndexRequest indexRequest = new CreateIndexRequest(AnomalyDetector.ANOMALY_DETECTORS_INDEX, indexSettings);
client().admin().indices().create(indexRequest).actionGet();
ActionListener<PreviewAnomalyDetectorResponse> previewResponse = new ActionListener<PreviewAnomalyDetectorResponse>() {
@Override
public void onResponse(PreviewAnomalyDetectorResponse response) {
Assert.assertTrue(false);
}
@Override
public void onFailure(Exception e) {
Assert.assertTrue(e.getMessage().contains("Can't find anomaly detector with id:1234"));
inProgressLatch.countDown();
}
};
action.doExecute(task, request, previewResponse);
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
use of org.opensearch.action.admin.indices.create.CreateIndexRequest in project OpenSearch by opensearch-project.
the class TransportBulkAction method createIndex.
void createIndex(String index, TimeValue timeout, Version minNodeVersion, ActionListener<CreateIndexResponse> listener) {
CreateIndexRequest createIndexRequest = new CreateIndexRequest();
createIndexRequest.index(index);
createIndexRequest.cause("auto(bulk api)");
createIndexRequest.masterNodeTimeout(timeout);
if (minNodeVersion.onOrAfter(LegacyESVersion.V_7_8_0)) {
client.execute(AutoCreateAction.INSTANCE, createIndexRequest, listener);
} else {
client.admin().indices().create(createIndexRequest, listener);
}
}
use of org.opensearch.action.admin.indices.create.CreateIndexRequest in project OpenSearch by opensearch-project.
the class TransportResizeAction method prepareCreateIndexRequest.
// static for unittesting this method
static CreateIndexClusterStateUpdateRequest prepareCreateIndexRequest(final ResizeRequest resizeRequest, final ClusterState state, final IntFunction<DocsStats> perShardDocStats, String sourceIndexName, String targetIndexName) {
final CreateIndexRequest targetIndex = resizeRequest.getTargetIndexRequest();
final IndexMetadata metadata = state.metadata().index(sourceIndexName);
if (metadata == null) {
throw new IndexNotFoundException(sourceIndexName);
}
final Settings.Builder targetIndexSettingsBuilder = Settings.builder().put(targetIndex.settings()).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX);
targetIndexSettingsBuilder.remove(IndexMetadata.SETTING_HISTORY_UUID);
final Settings targetIndexSettings = targetIndexSettingsBuilder.build();
final int numShards;
if (IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.exists(targetIndexSettings)) {
numShards = IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.get(targetIndexSettings);
} else {
assert resizeRequest.getResizeType() != ResizeType.SPLIT : "split must specify the number of shards explicitly";
if (resizeRequest.getResizeType() == ResizeType.SHRINK) {
numShards = 1;
} else {
assert resizeRequest.getResizeType() == ResizeType.CLONE;
numShards = metadata.getNumberOfShards();
}
}
for (int i = 0; i < numShards; i++) {
if (resizeRequest.getResizeType() == ResizeType.SHRINK) {
Set<ShardId> shardIds = IndexMetadata.selectShrinkShards(i, metadata, numShards);
long count = 0;
for (ShardId id : shardIds) {
DocsStats docsStats = perShardDocStats.apply(id.id());
if (docsStats != null) {
count += docsStats.getCount();
}
if (count > IndexWriter.MAX_DOCS) {
throw new IllegalStateException("Can't merge index with more than [" + IndexWriter.MAX_DOCS + "] docs - too many documents in shards " + shardIds);
}
}
} else if (resizeRequest.getResizeType() == ResizeType.SPLIT) {
Objects.requireNonNull(IndexMetadata.selectSplitShard(i, metadata, numShards));
// we just execute this to ensure we get the right exceptions if the number of shards is wrong or less then etc.
} else {
Objects.requireNonNull(IndexMetadata.selectCloneShard(i, metadata, numShards));
// we just execute this to ensure we get the right exceptions if the number of shards is wrong etc.
}
}
if (IndexMetadata.INDEX_ROUTING_PARTITION_SIZE_SETTING.exists(targetIndexSettings)) {
throw new IllegalArgumentException("cannot provide a routing partition size value when resizing an index");
}
if (IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.exists(targetIndexSettings)) {
// if we have a source index with 1 shards it's legal to set this
final boolean splitFromSingleShards = resizeRequest.getResizeType() == ResizeType.SPLIT && metadata.getNumberOfShards() == 1;
if (splitFromSingleShards == false) {
throw new IllegalArgumentException("cannot provide index.number_of_routing_shards on resize");
}
}
if (IndexSettings.INDEX_SOFT_DELETES_SETTING.get(metadata.getSettings()) && IndexSettings.INDEX_SOFT_DELETES_SETTING.exists(targetIndexSettings) && IndexSettings.INDEX_SOFT_DELETES_SETTING.get(targetIndexSettings) == false) {
throw new IllegalArgumentException("Can't disable [index.soft_deletes.enabled] setting on resize");
}
String cause = resizeRequest.getResizeType().name().toLowerCase(Locale.ROOT) + "_index";
targetIndex.cause(cause);
Settings.Builder settingsBuilder = Settings.builder().put(targetIndexSettings);
settingsBuilder.put("index.number_of_shards", numShards);
targetIndex.settings(settingsBuilder);
return new CreateIndexClusterStateUpdateRequest(cause, targetIndex.index(), targetIndexName).ackTimeout(targetIndex.timeout()).masterNodeTimeout(targetIndex.masterNodeTimeout()).settings(targetIndex.settings()).aliases(targetIndex.aliases()).waitForActiveShards(targetIndex.waitForActiveShards()).recoverFrom(metadata.getIndex()).resizeType(resizeRequest.getResizeType()).copySettings(resizeRequest.getCopySettings() == null ? false : resizeRequest.getCopySettings());
}
use of org.opensearch.action.admin.indices.create.CreateIndexRequest in project OpenSearch by opensearch-project.
the class MetadataRolloverServiceTests method testAliasValidation.
public void testAliasValidation() {
String index1 = randomAlphaOfLength(10);
String aliasWithWriteIndex = randomAlphaOfLength(10);
String index2 = randomAlphaOfLength(10);
String aliasWithNoWriteIndex = randomAlphaOfLength(10);
Boolean firstIsWriteIndex = randomFrom(false, null);
final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build();
Metadata.Builder metadataBuilder = Metadata.builder().put(IndexMetadata.builder(index1).settings(settings).putAlias(AliasMetadata.builder(aliasWithWriteIndex)).putAlias(AliasMetadata.builder(aliasWithNoWriteIndex).writeIndex(firstIsWriteIndex)));
IndexMetadata.Builder indexTwoBuilder = IndexMetadata.builder(index2).settings(settings);
if (firstIsWriteIndex == null) {
indexTwoBuilder.putAlias(AliasMetadata.builder(aliasWithNoWriteIndex).writeIndex(randomFrom(false, null)));
}
metadataBuilder.put(indexTwoBuilder);
Metadata metadata = metadataBuilder.build();
CreateIndexRequest req = new CreateIndexRequest();
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> MetadataRolloverService.validate(metadata, aliasWithNoWriteIndex, randomAlphaOfLength(5), req));
assertThat(exception.getMessage(), equalTo("rollover target [" + aliasWithNoWriteIndex + "] does not point to a write index"));
exception = expectThrows(IllegalArgumentException.class, () -> MetadataRolloverService.validate(metadata, randomFrom(index1, index2), randomAlphaOfLength(5), req));
assertThat(exception.getMessage(), equalTo("rollover target is a [concrete index] but one of [alias,data_stream] was expected"));
final String aliasName = randomAlphaOfLength(5);
exception = expectThrows(IllegalArgumentException.class, () -> MetadataRolloverService.validate(metadata, aliasName, randomAlphaOfLength(5), req));
assertThat(exception.getMessage(), equalTo("rollover target [" + aliasName + "] does not exist"));
MetadataRolloverService.validate(metadata, aliasWithWriteIndex, randomAlphaOfLength(5), req);
}
Aggregations