Search in sources :

Example 1 with RequestContext

use of org.hypertrace.core.grpcutils.context.RequestContext in project hypertrace-ingester by hypertrace.

the class AbstractBackendEntityEnricher method createBackendIfMissing.

@Nullable
private Entity createBackendIfMissing(Entity backendEntity) {
    RequestContext requestContext = RequestContext.forTenantId(backendEntity.getTenantId());
    try {
        Optional<Entity> backendFromCache = entityCache.getBackendIdAttrsToEntityCache().get(requestContext.buildContextualKey(backendEntity.getIdentifyingAttributesMap()));
        return backendFromCache.orElseGet(() -> {
            Entity result = this.upsertBackend(backendEntity);
            LOGGER.info("Created backend:{}", result);
            return result;
        });
    } catch (ExecutionException ex) {
        LOGGER.error("Error trying to load backend from cache for backend:{}", backendEntity);
        return null;
    }
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) ExecutionException(java.util.concurrent.ExecutionException) Nullable(javax.annotation.Nullable)

Example 2 with RequestContext

use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.

the class EntityQueryServiceImplTest method testBulkUpdateEntityArrayAttribute.

@Test
void testBulkUpdateEntityArrayAttribute() throws Exception {
    List<String> entityIds = IntStream.rangeClosed(1, 5).mapToObj(i -> UUID.randomUUID().toString()).collect(Collectors.toList());
    BulkEntityArrayAttributeUpdateRequest request = BulkEntityArrayAttributeUpdateRequest.newBuilder().addAllEntityIds(entityIds).setAttribute(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID3).build()).setEntityType(TEST_ENTITY_TYPE).setOperation(BulkEntityArrayAttributeUpdateRequest.Operation.OPERATION_ADD).addAllValues(List.of(LiteralConstant.newBuilder().setValue(Value.newBuilder().setString("Label1")).build(), LiteralConstant.newBuilder().setValue(Value.newBuilder().setString("Label2")).build())).build();
    when(mockAttributeMapping.getDocStorePathByAttributeId(requestContext, ATTRIBUTE_ID3)).thenReturn(Optional.of(EDS_COLUMN_NAME3));
    StreamObserver<BulkEntityArrayAttributeUpdateResponse> mockResponseObserver = mock(StreamObserver.class);
    Context.current().withValue(RequestContext.CURRENT, mockRequestContextWithTenantId()).call(() -> {
        EntityQueryServiceImpl eqs = new EntityQueryServiceImpl(entitiesCollection, mockAttributeMapping, 1, false);
        eqs.bulkUpdateEntityArrayAttribute(request, mockResponseObserver);
        return null;
    });
    ArgumentCaptor<BulkArrayValueUpdateRequest> argumentCaptor = ArgumentCaptor.forClass(BulkArrayValueUpdateRequest.class);
    verify(entitiesCollection, times(1)).bulkOperationOnArrayValue(argumentCaptor.capture());
    BulkArrayValueUpdateRequest bulkArrayValueUpdateRequest = argumentCaptor.getValue();
    assertEquals(entityIds.stream().map(entityId -> new SingleValueKey("tenant1", entityId)).collect(Collectors.toCollection(LinkedHashSet::new)), bulkArrayValueUpdateRequest.getKeys());
    assertEquals(BulkArrayValueUpdateRequest.Operation.ADD, bulkArrayValueUpdateRequest.getOperation());
    assertEquals(EDS_COLUMN_NAME3 + ".valueList.values", bulkArrayValueUpdateRequest.getSubDocPath());
    List<Document> subDocuments = bulkArrayValueUpdateRequest.getSubDocuments();
    assertEquals(2, subDocuments.size());
    assertEquals("{\"value\":{\"string\":\"Label1\"}}", subDocuments.get(0).toString());
    assertEquals("{\"value\":{\"string\":\"Label2\"}}", subDocuments.get(1).toString());
}
Also used : Context(io.grpc.Context) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Builder(org.hypertrace.entity.query.service.v1.LiteralConstant.Builder) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) UpdateOperation(org.hypertrace.entity.query.service.v1.UpdateOperation) ResultSetMetadata(org.hypertrace.entity.query.service.v1.ResultSetMetadata) Query(org.hypertrace.core.documentstore.Query) Disabled(org.junit.jupiter.api.Disabled) ArgumentMatcher(org.mockito.ArgumentMatcher) StreamObserver(io.grpc.stub.StreamObserver) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) BulkEntityArrayAttributeUpdateResponse(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateResponse) BulkEntityArrayAttributeUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateRequest) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Value(org.hypertrace.entity.query.service.v1.Value) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) BulkEntityUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ColumnMetadata(org.hypertrace.entity.query.service.v1.ColumnMetadata) Test(org.junit.jupiter.api.Test) List(java.util.List) JsonFormat(com.google.protobuf.util.JsonFormat) TotalEntitiesRequest(org.hypertrace.entity.query.service.v1.TotalEntitiesRequest) Optional(java.util.Optional) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) LiteralConstant(org.hypertrace.entity.query.service.v1.LiteralConstant) Mockito.mock(org.mockito.Mockito.mock) SetAttribute(org.hypertrace.entity.query.service.v1.SetAttribute) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) Row(org.hypertrace.entity.query.service.v1.Row) Document(org.hypertrace.core.documentstore.Document) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) DocStoreJsonFormat(org.hypertrace.entity.service.util.DocStoreJsonFormat) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) Filter(org.hypertrace.core.documentstore.Filter) Mock(org.mockito.Mock) EntityUpdateRequest(org.hypertrace.entity.query.service.v1.EntityUpdateRequest) LENIENT(org.mockito.quality.Strictness.LENIENT) Nested(org.junit.jupiter.api.Nested) Collection(org.hypertrace.core.documentstore.Collection) ArgumentCaptor(org.mockito.ArgumentCaptor) EntityUpdateInfo(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest.EntityUpdateInfo) AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) Entity(org.hypertrace.entity.data.service.v1.Entity) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) LinkedHashSet(java.util.LinkedHashSet) TotalEntitiesResponse(org.hypertrace.entity.query.service.v1.TotalEntitiesResponse) BulkArrayValueUpdateRequest(org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest) ColumnIdentifier(org.hypertrace.entity.query.service.v1.ColumnIdentifier) BulkUpdateResult(org.hypertrace.core.documentstore.BulkUpdateResult) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) DisplayName(org.junit.jupiter.api.DisplayName) Mockito(org.mockito.Mockito) TestUtils.convertToCloseableIterator(org.hypertrace.entity.TestUtils.convertToCloseableIterator) Expression(org.hypertrace.entity.query.service.v1.Expression) ValueType(org.hypertrace.entity.query.service.v1.ValueType) OrderByExpression(org.hypertrace.entity.query.service.v1.OrderByExpression) LinkedHashSet(java.util.LinkedHashSet) BulkEntityArrayAttributeUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateRequest) BulkEntityArrayAttributeUpdateResponse(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateResponse) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) BulkArrayValueUpdateRequest(org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Test(org.junit.jupiter.api.Test)

Example 3 with RequestContext

use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.

the class TenantBasedCacheKeyTest method matchesAnotherContextWithSameTenant.

@Test
void matchesAnotherContextWithSameTenant() {
    RequestContext matchingContext = mock(RequestContext.class);
    when(this.mockRequestContext.getTenantId()).thenReturn(Optional.of("tenant id 1"));
    when(matchingContext.getTenantId()).thenReturn(Optional.of("tenant id 1"));
    assertEquals(new TenantBasedCacheKey(this.mockRequestContext), new TenantBasedCacheKey(matchingContext));
    verify(mockRequestContext, never()).getRequestHeaders();
}
Also used : RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) Test(org.junit.jupiter.api.Test)

Example 4 with RequestContext

use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.

the class TenantBasedCacheKeyTest method doesNotMatchContextFromDifferentTenant.

@Test
void doesNotMatchContextFromDifferentTenant() {
    RequestContext matchingContext = mock(RequestContext.class);
    when(this.mockRequestContext.getTenantId()).thenReturn(Optional.of("tenant id 1"));
    when(matchingContext.getTenantId()).thenReturn(Optional.of("tenant id 2"));
    assertNotEquals(new TenantBasedCacheKey(this.mockRequestContext), new TenantBasedCacheKey(matchingContext));
}
Also used : RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) Test(org.junit.jupiter.api.Test)

Example 5 with RequestContext

use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.

the class EntityKeyTest method matchesSameEntityIdSameRequestContext.

@Test
void matchesSameEntityIdSameRequestContext() {
    RequestContext matchingContext = RequestContext.forTenantId(TENANT_ID_1);
    // Change some attributes to make sure it's not a deep compare
    Entity matchingEntity = this.entityV2.toBuilder().clearAttributes().build();
    assertEquals(new EntityKey(REQUEST_CONTEXT_1, this.entityV2), new EntityKey(matchingContext, matchingEntity));
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) Test(org.junit.jupiter.api.Test)

Aggregations

RequestContext (org.hypertrace.core.grpcutils.context.RequestContext)17 Test (org.junit.jupiter.api.Test)9 Entity (org.hypertrace.entity.data.service.v1.Entity)8 ServiceException (com.google.protobuf.ServiceException)6 Document (org.hypertrace.core.documentstore.Document)5 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)5 Optional (java.util.Optional)4 Query (org.hypertrace.entity.data.service.v1.Query)4 ConversionException (org.hypertrace.entity.query.service.converter.ConversionException)4 ColumnIdentifier (org.hypertrace.entity.query.service.v1.ColumnIdentifier)4 EntityQueryRequest (org.hypertrace.entity.query.service.v1.EntityQueryRequest)4 StreamObserver (io.grpc.stub.StreamObserver)3 List (java.util.List)3 Map (java.util.Map)3 Objects.isNull (java.util.Objects.isNull)3 Collectors (java.util.stream.Collectors)3 Collection (org.hypertrace.core.documentstore.Collection)3 IdentifierExpression (org.hypertrace.core.documentstore.expression.impl.IdentifierExpression)3 EntityUpdateInfo (org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest.EntityUpdateInfo)3 Expression (org.hypertrace.entity.query.service.v1.Expression)3