Skip to content

Commit

Permalink
Merge pull request #4096 from Agnul97/fix-delete_refresh_Indexes_impl…
Browse files Browse the repository at this point in the history
…ementation

FIX - deleteAllIndexes & refreshAllIndexes methods re-factoring
  • Loading branch information
Coduz authored Aug 28, 2024
2 parents f81d0f6 + 72ef2e7 commit bb70da1
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ public interface ElasticsearchClient<C extends Closeable> {
*/
void refreshAllIndexes() throws ClientException;

/**
* Forces the Elasticsearch to refresh a specific index.
*
* @throws ClientException if error occurs while refreshing.
* @since 2.1.0
*/
void refreshIndex(String index) throws ClientException;

/**
* Deletes all indexes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@ public void refreshAllIndexes() {
}
}

public void refreshIndex(String indexExp) {
try {
this.indexUpserted.invalidateAll();
elasticsearchClientProviderInstance.getElasticsearchClient().refreshIndex(indexExp);
} catch (ClientException e) {
throw new RuntimeException(e);
}
}

@Override
public void deleteAllIndexes() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ public static String refreshAllIndexes() {
return "/_all/_refresh";
}

/**
* @since 2.1.0
*/
public static String refreshIndex(@NotNull String index) {
return String.format("/%s/_refresh", index);
}

/**
* @since 1.0.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,16 @@ public void refreshAllIndexes() throws ClientException {
}
}

public void refreshIndex(String index) throws ClientException {
LOG.debug("Refresh index: {}", index);
Request request = new Request(ElasticsearchKeywords.ACTION_POST, ElasticsearchResourcePaths.refreshIndex(index));
Response refreshIndexResponse = restCallTimeoutHandler(() -> getClient().performRequest(request), index, "REFRESH INDEX");

if (!isRequestSuccessful(refreshIndexResponse)) {
throw buildExceptionFromUnsuccessfulResponse("Refresh indexes", refreshIndexResponse);
}
}

@Override
public void deleteAllIndexes() throws ClientException {
LOG.debug("Delete all indexes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public interface StorableRepository<

void refreshAllIndexes();

void refreshIndex(String indexExp);

void deleteAllIndexes();

void deleteIndexes(String indexExp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ protected String indexResolver(KapuaId scopeId) {
protected JsonNode getIndexSchema() throws MappingException {
return ChannelInfoSchema.getChannelTypeSchema();
}

@Override
public void refreshAllIndexes() {
super.refreshIndex(datastoreUtils.getChannelIndexName(KapuaId.ANY));
}

@Override
public void deleteAllIndexes() {
super.deleteIndexes(datastoreUtils.getChannelIndexName(KapuaId.ANY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,13 @@ protected StorableId idExtractor(ClientInfo storable) {
return storable.getId();
}

@Override
public void refreshAllIndexes() {
super.refreshIndex(datastoreUtils.getClientIndexName(KapuaId.ANY));
}

@Override
public void deleteAllIndexes() {
super.deleteIndexes(datastoreUtils.getClientIndexName(KapuaId.ANY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ private ObjectNode getNewMessageMappingsBuilder(Map<String, Metric> esMetrics) t

@Override
public void refreshAllIndexes() {
super.refreshAllIndexes();
super.refreshIndex(datastoreUtils.getDataIndexName(KapuaId.ANY));
this.metricsByIndex.invalidateAll();
}

@Override
public void deleteAllIndexes() {
super.deleteAllIndexes();
super.deleteIndexes(datastoreUtils.getDataIndexName(KapuaId.ANY));
this.metricsByIndex.invalidateAll();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ protected String indexResolver(KapuaId scopeId) {
protected StorableId idExtractor(MetricInfo storable) {
return storable.getId();
}

@Override
public void refreshAllIndexes() {
super.refreshIndex(datastoreUtils.getMetricIndexName(KapuaId.ANY));
}

@Override
public void deleteAllIndexes() {
super.deleteIndexes(datastoreUtils.getMetricIndexName(KapuaId.ANY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ public String getDataIndexName(KapuaId scopeId) {
if (StringUtils.isNotEmpty(prefix)) {
sb.append(prefix).append("-");
}
String indexName = normalizedIndexName(scopeId.toStringId());
String indexName;
if (KapuaId.ANY.equals(scopeId)) {
indexName = "*";
} else {
indexName = normalizedIndexName(scopeId.toStringId());
}
sb.append(indexName).append("-").append("data-message").append("-*");
return sb.toString();
}
Expand Down Expand Up @@ -198,7 +203,12 @@ private String getRegistryIndexName(KapuaId scopeId, IndexType indexType) {
if (StringUtils.isNotEmpty(prefix)) {
sb.append(prefix).append("-");
}
String indexName = normalizedIndexName(scopeId.toStringId());
String indexName;
if (KapuaId.ANY.equals(scopeId)) {
indexName = "*";
} else {
indexName = normalizedIndexName(scopeId.toStringId());
}
sb.append(indexName);
sb.append("-data-").append(indexType.name().toLowerCase());
return sb.toString();
Expand Down

0 comments on commit bb70da1

Please sign in to comment.