Skip to content

Commit

Permalink
Updated security-auditlog index creation with "0-all" replica setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ganesh Bombatkar committed Jan 23, 2025
1 parent 5939aa0 commit 7d60d06
Showing 1 changed file with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

import com.google.common.collect.ImmutableMap;

import org.opensearch.ResourceAlreadyExistsException;
import org.opensearch.action.DocWriteRequest;
import org.opensearch.action.admin.indices.create.CreateIndexRequest;
Expand All @@ -34,6 +36,8 @@ public abstract class AbstractInternalOpenSearchSink extends AuditLogSink {
protected final Client clientProvider;
private final ThreadPool threadPool;
private final DocWriteRequest.OpType storeOpType;
private String lastUsedIndexName;
final Map<String, Object> indexSettings = ImmutableMap.of("index.number_of_shards", 1, "index.auto_expand_replicas", "0-1");

public AbstractInternalOpenSearchSink(
final String name,
Expand All @@ -48,28 +52,23 @@ public AbstractInternalOpenSearchSink(
this.clientProvider = clientProvider;
this.threadPool = threadPool;
this.storeOpType = storeOpType;
this.lastUsedIndexName = null;
}

@Override
public void close() throws IOException {

}

private boolean indexExists(String indexName) {
return clientProvider
.admin()
.indices()
.prepareExists(indexName)
.execute()
.actionGet()
.isExists();
}
private boolean createIndexIfAbsent(String indexName) {
if (Objects.equals(indexName, lastUsedIndexName)) {
return false;
}

private boolean createIndexIfAbsent(String indexName){
try {
final Map<String, Object> indexSettings = ImmutableMap.of("index.number_of_shards", 1, "index.auto_expand_replicas", "0-all");
final CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName).settings(indexSettings);
final boolean ok = clientProvider.admin().indices().create(createIndexRequest).actionGet().isAcknowledged();
lastUsedIndexName = indexName;
log.info("Index {} created?: {}", indexName, ok);
return ok;
} catch (ResourceAlreadyExistsException resourceAlreadyExistsException) {
Expand All @@ -89,9 +88,7 @@ public boolean doStore(final AuditMessage msg, String indexName) {
return true;
}

if(!indexExists(indexName)){
createIndexIfAbsent(indexName);
}
createIndexIfAbsent(indexName);

try (StoredContext ctx = threadPool.getThreadContext().stashContext()) {
try {
Expand Down

0 comments on commit 7d60d06

Please sign in to comment.