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;
}
}
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());
}
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();
}
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));
}
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));
}
Aggregations