use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.
the class EntityDataServiceImpl method mergeAndUpsertEntity.
@Override
public void mergeAndUpsertEntity(MergeAndUpsertEntityRequest request, StreamObserver<MergeAndUpsertEntityResponse> responseObserver) {
RequestContext requestContext = RequestContext.CURRENT.get();
String tenantId = requestContext.getTenantId().orElse(null);
if (tenantId == null) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
Entity receivedEntity = this.entityNormalizer.normalize(tenantId, request.getEntity());
Optional<Entity> existingEntity = getExistingEntity(tenantId, receivedEntity.getEntityType(), receivedEntity.getEntityId());
boolean rejectUpsertForConditionMismatch = existingEntity.map(entity -> !this.upsertConditionMatcher.matches(entity, request.getUpsertCondition())).orElse(false);
if (rejectUpsertForConditionMismatch) {
// There's an existing entity and the update doesn't meet the condition, return existing
responseObserver.onNext(MergeAndUpsertEntityResponse.newBuilder().setEntity(existingEntity.get()).build());
responseObserver.onCompleted();
} else {
// There's either a new entity or a valid update to upsert
Entity entityToUpsert = existingEntity.map(Entity::toBuilder).map(Entity.Builder::clearCreatedTime).map(builder -> builder.mergeFrom(receivedEntity)).map(Builder::build).orElse(receivedEntity);
try {
Entity upsertedEntity = this.upsertEntity(tenantId, entityToUpsert);
responseObserver.onNext(MergeAndUpsertEntityResponse.newBuilder().setEntity(upsertedEntity).build());
responseObserver.onCompleted();
entityChangeEventGenerator.sendChangeNotification(requestContext, existingEntity.map(List::of).orElse(Collections.emptyList()), List.of(upsertedEntity));
} catch (IOException e) {
responseObserver.onError(e);
}
}
}
use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.
the class EntityDataCachingClient method createOrUpdateEntityEventually.
@Override
public void createOrUpdateEntityEventually(RequestContext requestContext, Entity entity, UpsertCondition condition, Duration maximumUpsertDelay) {
EntityKey entityKey = new EntityKey(requestContext, entity);
// Don't allow other update processing until finished
Lock lock = this.pendingUpdateStripedLock.get(entityKey);
try {
lock.lock();
this.pendingEntityUpdates.computeIfAbsent(entityKey, unused -> new PendingEntityUpdate()).addNewUpdate(entityKey, condition, maximumUpsertDelay);
} finally {
lock.unlock();
}
}
use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.
the class EntityKeyTest method matchesSameIdentifyingAttributesSameRequestContext.
@Test
void matchesSameIdentifyingAttributesSameRequestContext() {
RequestContext matchingContext = RequestContext.forTenantId(TENANT_ID_1);
// Change some attributes to make sure it's not a deep compare
Entity matchingEntity = this.entityV1.toBuilder().clearAttributes().build();
assertEquals(new EntityKey(REQUEST_CONTEXT_1, this.entityV1), new EntityKey(matchingContext, matchingEntity));
}
use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.
the class FromClauseConverterTest method testConvert1.
@Test
void testConvert1() throws Exception {
final ColumnIdentifier col1 = ColumnIdentifier.newBuilder().setColumnName("col1").build();
final ColumnIdentifier col2 = ColumnIdentifier.newBuilder().setColumnName("col2").build();
final List<Expression> expressions = List.of(Expression.newBuilder().setColumnIdentifier(col1).build(), Expression.newBuilder().setColumnIdentifier(col2).build());
final RequestContext requestContext = new RequestContext();
when(mockEntityAttributeMapping.isMultiValued(eq(requestContext), eq("col1"))).thenReturn(false);
when(mockEntityAttributeMapping.isMultiValued(eq(requestContext), eq("col2"))).thenReturn(true);
when(mockIdentifierExpressionConverter.convert(col2, requestContext)).thenReturn(IdentifierExpression.of("name"));
final List<FromTypeExpression> expected = List.of(UnnestExpression.of(IdentifierExpression.of("name"), false));
final List<FromTypeExpression> actual = fromClauseConverter.convert(expressions, requestContext);
assertEquals(expected, actual);
}
use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.
the class FromClauseConverterTest method testConvert1_ConverterThrowsConversionException.
@Test
void testConvert1_ConverterThrowsConversionException() {
final ColumnIdentifier col1 = ColumnIdentifier.newBuilder().setColumnName("col1").build();
final ColumnIdentifier col2 = ColumnIdentifier.newBuilder().setColumnName("col2").build();
final List<Expression> expressions = List.of(Expression.newBuilder().setColumnIdentifier(col1).build(), Expression.newBuilder().setFunction(Function.newBuilder().addArguments(Expression.newBuilder().setColumnIdentifier(col2))).build());
final RequestContext requestContext = new RequestContext();
assertThrows(ConversionException.class, () -> fromClauseConverter.convert(expressions, requestContext));
}
Aggregations