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; ...@@ -37,6 +37,7 @@ import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.RestStatus;
import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; 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.search.IndicesService;
import org.opengroup.osdu.core.common.provider.interfaces.IIndexCache; import org.opengroup.osdu.core.common.provider.interfaces.IIndexCache;
import org.opengroup.osdu.core.common.model.search.IndexInfo; import org.opengroup.osdu.core.common.model.search.IndexInfo;
...@@ -47,6 +48,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -47,6 +48,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope; import org.springframework.web.context.annotation.RequestScope;
import javax.inject.Inject;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.List; import java.util.List;
...@@ -65,6 +67,12 @@ public class IndicesServiceImpl implements IndicesService { ...@@ -65,6 +67,12 @@ public class IndicesServiceImpl implements IndicesService {
private IIndexCache indicesExistCache; private IIndexCache indicesExistCache;
@Autowired @Autowired
private JaxRsDpsLog log; private JaxRsDpsLog log;
@Inject
private IRequestInfo requestInfo;
private String getCacheKeyForIndex(String index) {
return this.requestInfo.getPartitionId() + ":" + index;
}
private TimeValue REQUEST_TIMEOUT = TimeValue.timeValueMinutes(1); private TimeValue REQUEST_TIMEOUT = TimeValue.timeValueMinutes(1);
...@@ -99,14 +107,14 @@ public class IndicesServiceImpl implements IndicesService { ...@@ -99,14 +107,14 @@ public class IndicesServiceImpl implements IndicesService {
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
// cache the index status // cache the index status
boolean indexStatus = response.isAcknowledged() && response.isShardsAcknowledged(); boolean indexStatus = response.isAcknowledged() && response.isShardsAcknowledged();
if (indexStatus) this.indicesExistCache.put(index, true); if (indexStatus) this.indicesExistCache.put(getCacheKeyForIndex(index), true);
return indexStatus; return indexStatus;
} catch (ElasticsearchStatusException e) { } catch (ElasticsearchStatusException e) {
if (e.status() == RestStatus.BAD_REQUEST && (e.getMessage().contains("resource_already_exists_exception"))) { if (e.status() == RestStatus.BAD_REQUEST && (e.getMessage().contains("resource_already_exists_exception"))) {
log.info("Index already exists. Ignoring error..."); log.info("Index already exists. Ignoring error...");
// cache the index status // cache the index status
this.indicesExistCache.put(index, true); this.indicesExistCache.put(getCacheKeyForIndex(index), true);
return true; return true;
} }
throw e; throw e;
...@@ -123,15 +131,15 @@ public class IndicesServiceImpl implements IndicesService { ...@@ -123,15 +131,15 @@ public class IndicesServiceImpl implements IndicesService {
public boolean isIndexExist(RestHighLevelClient client, String index) throws IOException { public boolean isIndexExist(RestHighLevelClient client, String index) throws IOException {
try { try {
try { try {
Boolean isIndexExist = (Boolean) this.indicesExistCache.get(index); Boolean isIndexExist = (Boolean) this.indicesExistCache.get(getCacheKeyForIndex(index));
if (isIndexExist != null && isIndexExist) return true; if (isIndexExist != null && isIndexExist) return true;
} catch (RedisException ex) { } catch (RedisException ex) {
//In case the format of cache changes then clean the cache //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); GetIndexRequest request = new GetIndexRequest(index);
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); 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; return exists;
} catch (ElasticsearchException exception) { } catch (ElasticsearchException exception) {
if (exception.status() == RestStatus.NOT_FOUND) return false; if (exception.status() == RestStatus.NOT_FOUND) return false;
...@@ -152,7 +160,7 @@ public class IndicesServiceImpl implements IndicesService { ...@@ -152,7 +160,7 @@ public class IndicesServiceImpl implements IndicesService {
public boolean deleteIndex(RestHighLevelClient client, String index) throws ElasticsearchException, IOException, AppException { public boolean deleteIndex(RestHighLevelClient client, String index) throws ElasticsearchException, IOException, AppException {
boolean responseStatus = removeIndexInElasticsearch(client, index); boolean responseStatus = removeIndexInElasticsearch(client, index);
if (responseStatus) { if (responseStatus) {
this.indicesExistCache.delete(index); this.indicesExistCache.delete(getCacheKeyForIndex(index));
} }
return responseStatus; return responseStatus;
} }
......
...@@ -35,6 +35,7 @@ import org.opengroup.osdu.core.common.model.http.AppException; ...@@ -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.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.provider.interfaces.IIndexCache; import org.opengroup.osdu.core.common.provider.interfaces.IIndexCache;
import org.opengroup.osdu.core.common.model.search.IndexInfo; 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.core.common.search.ElasticIndexNameResolver;
import org.opengroup.osdu.indexer.util.ElasticClientHandler; import org.opengroup.osdu.indexer.util.ElasticClientHandler;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
...@@ -61,6 +62,8 @@ public class IndicesServiceTest { ...@@ -61,6 +62,8 @@ public class IndicesServiceTest {
@Mock @Mock
private IIndexCache indicesExistCache; private IIndexCache indicesExistCache;
@Mock @Mock
private IRequestInfo requestInfo;
@Mock
@Lazy @Lazy
private JaxRsDpsLog log; private JaxRsDpsLog log;
@Mock @Mock
...@@ -69,6 +72,7 @@ public class IndicesServiceTest { ...@@ -69,6 +72,7 @@ public class IndicesServiceTest {
private Response response; private Response response;
@Mock @Mock
private HttpEntity httpEntity; private HttpEntity httpEntity;
@InjectMocks @InjectMocks
private IndicesServiceImpl sut; 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