Commit 2c574684 authored by Yauheni Lesnikau's avatar Yauheni Lesnikau
Browse files

fix the issue with cross tenant search

parent 781f9984
Pipeline #34490 failed with stages
in 35 minutes and 29 seconds
......@@ -191,7 +191,7 @@ abstract class QueryBase {
}
String getIndex(Query request) {
return this.crossTenantUtils.getIndexName(request, this.dpsHeaders.getPartitionId());
return this.crossTenantUtils.getIndexName(request);
}
List<Map<String, Object>> getHitsFromSearchResponse(SearchResponse searchResponse) {
......
......@@ -194,7 +194,7 @@ abstract class QueryBase {
}
String getIndex(Query request) {
return this.crossTenantUtils.getIndexName(request, this.dpsHeaders.getPartitionId());
return this.crossTenantUtils.getIndexName(request);
}
List<Map<String, Object>> getHitsFromSearchResponse(SearchResponse searchResponse) {
......
......@@ -128,8 +128,7 @@ public class QueryServiceImplTest {
public void init() throws IOException {
Map<String, Object> hitFields = new HashMap<>();
doReturn(dataPartitionId).when(dpsHeaders).getPartitionId();
doReturn(indexName).when(crossTenantUtils).getIndexName(any(), eq(dataPartitionId));
doReturn(indexName).when(crossTenantUtils).getIndexName(any());
doReturn(client).when(elasticClientHandler).createRestClient();
doReturn(spatialFilter).when(searchRequest).getSpatialFilter();
doReturn(fieldName).when(spatialFilter).getField();
......
......@@ -100,8 +100,7 @@ public class ScrollQueryServiceImplTest {
@Before
public void init() {
doReturn(userId).when(dpsHeaders).getUserEmail();
doReturn(dataPartitionId).when(dpsHeaders).getPartitionId();
doReturn(indexName).when(crossTenantUtils).getIndexName(any(), eq(dataPartitionId));
doReturn(indexName).when(crossTenantUtils).getIndexName(any());
doReturn(cursorSettings).when(cursorCache).get(anyString());
doReturn(client).when(elasticClientHandler).createRestClient();
}
......
......@@ -91,7 +91,7 @@ public class QueryServiceImpl extends QueryBase implements IQueryService {
QueryRequest searchRequest = (QueryRequest) request;
// set the indexes to org.opengroup.osdu.search.search against
String index = this.crossTenantUtils.getIndexName(request, this.dpsHeaders.getPartitionId());
String index = this.crossTenantUtils.getIndexName(request);
SearchRequest elasticSearchRequest = new SearchRequest(index);
......
......@@ -141,7 +141,7 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
SearchRequest createElasticRequest(Query request) throws AppException {
// set the indexes to org.opengroup.osdu.search.search against
String index = this.crossTenantUtils.getIndexName(request, this.dpsHeaders.getPartitionId());
String index = this.crossTenantUtils.getIndexName(request);
SearchRequest elasticSearchRequest = new SearchRequest(index);
// build query
......
......@@ -197,7 +197,7 @@ abstract class QueryBase {
}
String getIndex(Query request) {
return this.crossTenantUtils.getIndexName(request, this.dpsHeaders.getPartitionId());
return this.crossTenantUtils.getIndexName(request);
}
List<Map<String, Object>> getHitsFromSearchResponse(SearchResponse searchResponse) {
......
......@@ -505,7 +505,7 @@ public class QueryServiceTest {
when(searchConfigurationProperties.getQueryLimitMaximum()).thenReturn(1000);
when(searchRequest.getReturnedFields()).thenReturn(returnedFields);
when(crossTenantUtils.getIndexName(any(), any())).thenReturn("tenant1-welldb-well-1.0.0,-.*");
when(crossTenantUtils.getIndexName(any())).thenReturn("tenant1-welldb-well-1.0.0,-.*");
SearchRequest elasticRequest = this.sut.createElasticRequest(searchRequest);
assertNotNull(elasticRequest);
......@@ -544,7 +544,7 @@ public class QueryServiceTest {
when(searchRequest.getKind()).thenReturn("tenant1:welldb:well:1.0.0");
when(searchRequest.getReturnedFields()).thenReturn(returnedFields);
when(crossTenantUtils.getIndexName(any(), any())).thenReturn("tenant1-welldb-well-1.0.0,-.*");
when(crossTenantUtils.getIndexName(any())).thenReturn("tenant1-welldb-well-1.0.0,-.*");
SearchRequest elasticRequest = this.sut.createElasticRequest(searchRequest);
assertNotNull(elasticRequest);
......@@ -573,7 +573,7 @@ public class QueryServiceTest {
public void should_return_correctElasticRequest_given_noReturnedField() throws IOException {
when(searchRequest.getKind()).thenReturn("tenant1:welldb:well:1.0.0");
when(crossTenantUtils.getIndexName(any(), any())).thenReturn("tenant1-welldb-well-1.0.0,-.*");
when(crossTenantUtils.getIndexName(any())).thenReturn("tenant1-welldb-well-1.0.0,-.*");
SearchRequest elasticRequest = this.sut.createElasticRequest(searchRequest);
assertNotNull(elasticRequest);
......@@ -610,7 +610,7 @@ public class QueryServiceTest {
sortOrders.add(SortOrder.DESC);
sort.setOrder(sortOrders);
when(searchRequest.getSort()).thenReturn(sort);
when(crossTenantUtils.getIndexName(any(), any())).thenReturn("tenant1-welldb-well-1.0.0,-.*");
when(crossTenantUtils.getIndexName(any())).thenReturn("tenant1-welldb-well-1.0.0,-.*");
SearchRequest elasticRequest = this.sut.createElasticRequest(searchRequest);
assertNotNull(elasticRequest);
......@@ -622,7 +622,7 @@ public class QueryServiceTest {
public void should_return_correctElasticRequest_given_groupByField() throws IOException {
when(searchRequest.getKind()).thenReturn("tenant1:welldb:well:1.0.0");
when(searchRequest.getAggregateBy()).thenReturn("namespace");
when(crossTenantUtils.getIndexName(any(), any())).thenReturn("tenant1-welldb-well-1.0.0,-.*");
when(crossTenantUtils.getIndexName(any())).thenReturn("tenant1-welldb-well-1.0.0,-.*");
when(searchConfigurationProperties.getAggregationSize()).thenReturn(1000);
doReturn(true).when(this.sut).isEnvironmentPreDemo();
......
......@@ -301,7 +301,7 @@ public class ScrollQueryServiceTest {
when(cursorQueryRequest.getReturnedFields()).thenReturn(returnedFields);
when(searchConfig.getQueryLimitMaximum()).thenReturn(1000);
Mockito.when(crossTenantUtils.getIndexName(any(), any()))
Mockito.when(crossTenantUtils.getIndexName(any()))
.thenReturn("tenant1-welldb-well-1.0.0,-.*");
SearchRequest elasticRequest = this.sut.createElasticRequest(cursorQueryRequest);
......
......@@ -181,7 +181,7 @@ abstract class QueryBase {
}
String getIndex(Query request) {
return this.crossTenantUtils.getIndexName(request, this.dpsHeaders.getPartitionId());
return this.crossTenantUtils.getIndexName(request);
}
List<Map<String, Object>> getHitsFromSearchResponse(SearchResponse searchResponse) {
......
......@@ -207,7 +207,7 @@ abstract class QueryBase {
}
String getIndex(Query request) {
return this.crossTenantUtils.getIndexName(request, this.dpsHeaders.getPartitionId());
return this.crossTenantUtils.getIndexName(request);
}
List<Map<String, Object>> getHitsFromSearchResponse(SearchResponse searchResponse) {
......
......@@ -14,44 +14,19 @@
package org.opengroup.osdu.search.util;
import org.opengroup.osdu.core.common.multitenancy.ITenantInfoService;
import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver;
import org.opengroup.osdu.core.common.model.search.Query;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.springframework.stereotype.Component;
import javax.inject.Inject;
import java.util.Arrays;
import java.util.List;
@Component
public class CrossTenantUtils {
@Inject
private ElasticIndexNameResolver elasticIndexNameResolver;
@Inject
private javax.inject.Provider<ITenantInfoService> tenantInfoServiceProvider;
/* TODO: get rid of this when elastic start supporting cross-cluster search natively - index should not be copied to tenant cluster
* kind slb account id/data partition response
* tenantId:datasource:type:version tenantId records from kind and tenantId tenant only
* *:datasource:type:version tenantId records from tenantId tenant only
* tenantId:datasource:type:version tenantId,common records from kind starting with tenantId only
* *:datasource:type:version tenantId,common records from tenantId & common tenant
*/
public String getIndexName(Query searchRequest, String accountHeader) {
// add tenant prefix if it's cross tenant search request and only one account id specified
if (searchRequest.getKind().startsWith("*:")) {
List<String> dataPartitions = Arrays.asList(accountHeader.trim().split("\\s*,\\s*"));
if (dataPartitions.size() == 1) {
TenantInfo tenantInfo = this.tenantInfoServiceProvider.get().getTenantInfo();
return String.format("%s%s,-.*", tenantInfo.getName(), this.elasticIndexNameResolver.getIndexNameFromKind(searchRequest.getKind()).substring(1));
}
}
public String getIndexName(Query searchRequest) {
return String.format("%s,-.*", this.elasticIndexNameResolver.getIndexNameFromKind(searchRequest.getKind()));
}
}
......@@ -15,19 +15,15 @@
package org.opengroup.osdu.search.util;
import org.opengroup.osdu.core.common.multitenancy.ITenantInfoService;
import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver;
import org.opengroup.osdu.core.common.model.search.QueryRequest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.search.QueryRequest;
import org.opengroup.osdu.core.common.search.ElasticIndexNameResolver;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
......@@ -37,52 +33,15 @@ public class CrossTenantUtilsTest {
private QueryRequest queryRequest;
@Mock
private ElasticIndexNameResolver elasticIndexNameResolver;
@Mock
private ITenantInfoService tenantInfoService;
@Mock
private javax.inject.Provider<ITenantInfoService> tenantInfoServiceProvider;
@InjectMocks
private CrossTenantUtils sut;
@Before
public void setup() {
when(this.tenantInfoServiceProvider.get()).thenReturn(this.tenantInfoService);
}
@Test
public void should_returnTenantPrefixedIndex_when_searchedCrossKind_given_tenantAccountId() {
String crmAccountId = "slb-tenant1";
when(queryRequest.getKind()).thenReturn("*:ihs:well:1.0.0");
when(this.elasticIndexNameResolver.getIndexNameFromKind(any())).thenReturn("*-ihs-well-1.0.0");
TenantInfo tenantInfo = new TenantInfo();
tenantInfo.setName("tenant1");
when(this.tenantInfoService.getTenantInfo()).thenReturn(tenantInfo);
assertEquals("tenant1-ihs-well-1.0.0,-.*", this.sut.getIndexName(queryRequest, crmAccountId));
}
@Test
public void should_returnIndexAsIs_when_searchedCrossKind_given_multipleAccountId() {
when(queryRequest.getKind()).thenReturn("*:ihs:well:1.0.0");
when(this.elasticIndexNameResolver.getIndexNameFromKind(any())).thenReturn("*-ihs-well-1.0.0");
assertEquals("*-ihs-well-1.0.0,-.*", this.sut.getIndexName(queryRequest, "tenant1,common"));
}
@Test
public void should_returnIndexAsIs2_when_searchedCrossKind_given_multipleAccountId() {
when(queryRequest.getKind()).thenReturn("tenant1:ihs:well:1.0.0");
when(this.elasticIndexNameResolver.getIndexNameFromKind(any())).thenReturn("tenant1-ihs-well-1.0.0");
assertEquals("tenant1-ihs-well-1.0.0,-.*", this.sut.getIndexName(queryRequest, "tenant1,common"));
}
@Test
public void should_returnIndexAsIs_when_searchedCrossKind_given_oneAccountId() {
when(queryRequest.getKind()).thenReturn("tenant1:ihs:well:1.0.0");
when(this.elasticIndexNameResolver.getIndexNameFromKind(any())).thenReturn("tenant1-ihs-well-1.0.0");
String kind = "*:ihs:well:1.0.0";
when(queryRequest.getKind()).thenReturn(kind);
when(this.elasticIndexNameResolver.getIndexNameFromKind(kind)).thenReturn("*-ihs-well-1.0.0");
assertEquals("tenant1-ihs-well-1.0.0,-.*", this.sut.getIndexName(queryRequest, "tenant1"));
assertEquals("*-ihs-well-1.0.0,-.*", this.sut.getIndexName(queryRequest));
}
}
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