Commit 3acb61bd authored by Sabarish K R E's avatar Sabarish K R E
Browse files

Fixes a scenario where an index created in DP 1, but when same index name's...

Fixes a scenario where an index created in DP 1, but when same index name's records are put in DP2, the index mapping doesn't get created due to cache hit.
parent 59e6c6df
Pipeline #77259 failed with stages
in 14 seconds
......@@ -37,6 +37,7 @@ import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.provider.interfaces.IRequestInfo;
import org.opengroup.osdu.core.common.search.IndicesService;
import org.opengroup.osdu.core.common.provider.interfaces.IIndexCache;
import org.opengroup.osdu.core.common.model.search.IndexInfo;
......@@ -47,6 +48,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope;
import javax.inject.Inject;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
......@@ -65,6 +67,12 @@ public class IndicesServiceImpl implements IndicesService {
private IIndexCache indicesExistCache;
@Autowired
private JaxRsDpsLog log;
@Inject
private IRequestInfo requestInfo;
private String getCacheKeyForIndex(String index) {
return this.requestInfo.getPartitionId() + ":" + index;
}
private TimeValue REQUEST_TIMEOUT = TimeValue.timeValueMinutes(1);
......@@ -99,14 +107,14 @@ public class IndicesServiceImpl implements IndicesService {
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
// cache the index status
boolean indexStatus = response.isAcknowledged() && response.isShardsAcknowledged();
if (indexStatus) this.indicesExistCache.put(index, true);
if (indexStatus) this.indicesExistCache.put(getCacheKeyForIndex(index), true);
return indexStatus;
} catch (ElasticsearchStatusException e) {
if (e.status() == RestStatus.BAD_REQUEST && (e.getMessage().contains("resource_already_exists_exception"))) {
log.info("Index already exists. Ignoring error...");
// cache the index status
this.indicesExistCache.put(index, true);
this.indicesExistCache.put(getCacheKeyForIndex(index), true);
return true;
}
throw e;
......@@ -123,15 +131,15 @@ public class IndicesServiceImpl implements IndicesService {
public boolean isIndexExist(RestHighLevelClient client, String index) throws IOException {
try {
try {
Boolean isIndexExist = (Boolean) this.indicesExistCache.get(index);
Boolean isIndexExist = (Boolean) this.indicesExistCache.get(getCacheKeyForIndex(index));
if (isIndexExist != null && isIndexExist) return true;
} catch (RedisException ex) {
//In case the format of cache changes then clean the cache
this.indicesExistCache.delete(index);
this.indicesExistCache.delete(getCacheKeyForIndex(index));
}
GetIndexRequest request = new GetIndexRequest(index);
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
if (exists) this.indicesExistCache.put(index, true);
if (exists) this.indicesExistCache.put(getCacheKeyForIndex(index), true);
return exists;
} catch (ElasticsearchException exception) {
if (exception.status() == RestStatus.NOT_FOUND) return false;
......@@ -152,7 +160,7 @@ public class IndicesServiceImpl implements IndicesService {
public boolean deleteIndex(RestHighLevelClient client, String index) throws ElasticsearchException, IOException, AppException {
boolean responseStatus = removeIndexInElasticsearch(client, index);
if (responseStatus) {
this.indicesExistCache.delete(index);
this.indicesExistCache.delete(getCacheKeyForIndex(index));
}
return responseStatus;
}
......
......@@ -35,6 +35,7 @@ import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.provider.interfaces.IIndexCache;
import org.opengroup.osdu.core.common.model.search.IndexInfo;
import org.opengroup.osdu.core.common.provider.interfaces.IRequestInfo;
import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver;
import org.opengroup.osdu.indexer.util.ElasticClientHandler;
import org.powermock.api.mockito.PowerMockito;
......@@ -61,6 +62,8 @@ public class IndicesServiceTest {
@Mock
private IIndexCache indicesExistCache;
@Mock
private IRequestInfo requestInfo;
@Mock
@Lazy
private JaxRsDpsLog log;
@Mock
......@@ -69,6 +72,7 @@ public class IndicesServiceTest {
private Response response;
@Mock
private HttpEntity httpEntity;
@InjectMocks
private IndicesServiceImpl sut;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment