Skip to content
Snippets Groups Projects
Commit 7d5b8618 authored by Zhibin Mai's avatar Zhibin Mai Committed by David Diederich
Browse files

Use HashMap in memory instead of Cache as the mapping from index to alias is intact

(cherry picked from commit 466cd657)
parent 4983a133
Branches
Tags
2 merge requests!575Delta changes m16 to m18,!434Cherry-pick 'Use alias to shorten the index names in multi-kind searchMulti kind alias' into release/0.19
// Copyright © Schlumberger
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.opengroup.osdu.search.cache;
import org.opengroup.osdu.core.common.cache.VmCache;
import org.springframework.stereotype.Component;
@Component
public class IndexAliasCache extends VmCache<String, String> {
public IndexAliasCache() {
super(7 * 24 * 3600, 2000);
}
}
......@@ -25,13 +25,13 @@ import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.rest.RestStatus;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver;
import org.opengroup.osdu.search.cache.IndexAliasCache;
import org.opengroup.osdu.search.util.ElasticClientHandler;
import org.springframework.stereotype.Component;
import javax.inject.Inject;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@Component
......@@ -41,10 +41,11 @@ public class IndexAliasServiceImpl implements IndexAliasService {
@Inject
private ElasticIndexNameResolver elasticIndexNameResolver;
@Inject
private IndexAliasCache indexAliasCache;
@Inject
private JaxRsDpsLog log;
private final Map<String, String> KIND_ALIAS_MAP = new ConcurrentHashMap();
@Override
public Map<String, String> getIndicesAliases(List<String> kinds) {
Map<String, String> aliases = new HashMap<>();
......@@ -52,7 +53,7 @@ public class IndexAliasServiceImpl implements IndexAliasService {
List<String> validKinds = kinds.stream().filter(k -> elasticIndexNameResolver.isIndexAliasSupported(k)).collect(Collectors.toList());
for(String kind: validKinds) {
String alias = indexAliasCache.get(kind);
String alias = KIND_ALIAS_MAP.get(kind);
if(!Strings.isNullOrEmpty(alias)) {
aliases.put(kind, alias);
}
......@@ -67,15 +68,20 @@ public class IndexAliasServiceImpl implements IndexAliasService {
for(String kind: unresolvedKinds) {
String alias = elasticIndexNameResolver.getIndexAliasFromKind(kind);
if(!allExistingAliases.contains(alias)) {
alias = createIndexAlias(restClient, kind);
try {
alias = createIndexAlias(restClient, kind);
}
catch(Exception e) {
log.error(String.format("Fail to create index alias for kind '%s'", kind), e);
}
}
if(!Strings.isNullOrEmpty(alias)) {
aliases.put(kind, alias);
indexAliasCache.put(kind, alias);
KIND_ALIAS_MAP.put(kind, alias);
}
}
} catch (Exception e) {
log.error(String.format("Fail to get or create index aliases for kinds"), e);
log.error(String.format("Fail to get index aliases for kinds"), e);
}
}
......@@ -86,7 +92,7 @@ public class IndexAliasServiceImpl implements IndexAliasService {
if(!elasticIndexNameResolver.isIndexAliasSupported(kind))
return null;
String alias = indexAliasCache.get(kind);
String alias = KIND_ALIAS_MAP.get(kind);
if(Strings.isNullOrEmpty(alias)) {
try (RestHighLevelClient restClient = this.elasticClientHandler.createRestClient()) {
if(isIndexAliasExistForKind(restClient, kind)) {
......@@ -97,7 +103,7 @@ public class IndexAliasServiceImpl implements IndexAliasService {
}
if(!Strings.isNullOrEmpty(alias)) {
indexAliasCache.put(kind, alias);
KIND_ALIAS_MAP.put(kind, alias);
}
}
catch (Exception e) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment