Search in sources :

Example 1 with MergeAndUpsertEntityResponse

use of org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityResponse 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);
        }
    }
}
Also used : ByIdRequest(org.hypertrace.entity.data.service.v1.ByIdRequest) ServiceException(com.google.protobuf.ServiceException) LoggerFactory(org.slf4j.LoggerFactory) Channel(io.grpc.Channel) EntityTypeClient(org.hypertrace.entity.type.service.rxclient.EntityTypeClient) StreamObserver(io.grpc.stub.StreamObserver) Map(java.util.Map) Objects.isNull(java.util.Objects.isNull) ENRICHED_ENTITIES_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.ENRICHED_ENTITIES_COLLECTION) DocStoreConverter(org.hypertrace.entity.service.util.DocStoreConverter) InvalidRequestException(org.hypertrace.entity.service.exception.InvalidRequestException) StringUtils(org.hypertrace.entity.service.util.StringUtils) Streams(com.google.common.collect.Streams) Empty(org.hypertrace.entity.data.service.v1.Empty) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) EntityRelationship(org.hypertrace.entity.data.service.v1.EntityRelationship) EntityRelationships(org.hypertrace.entity.data.service.v1.EntityRelationships) MergeAndUpsertEntityRequest(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityRequest) Optional(java.util.Optional) EntityChangeEventGenerator(org.hypertrace.entity.service.change.event.api.EntityChangeEventGenerator) EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Builder(org.hypertrace.entity.data.service.v1.Entity.Builder) Document(org.hypertrace.core.documentstore.Document) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) ByTypeAndIdentifyingAttributes(org.hypertrace.entity.data.service.v1.ByTypeAndIdentifyingAttributes) DocStoreJsonFormat(org.hypertrace.entity.service.util.DocStoreJsonFormat) Descriptors(com.google.protobuf.Descriptors) Filter(org.hypertrace.core.documentstore.Filter) HashMap(java.util.HashMap) Function(java.util.function.Function) Collection(org.hypertrace.core.documentstore.Collection) Entities(org.hypertrace.entity.data.service.v1.Entities) ArrayList(java.util.ArrayList) Datastore(org.hypertrace.core.documentstore.Datastore) Key(org.hypertrace.core.documentstore.Key) EntityServiceConstants(org.hypertrace.entity.service.constants.EntityServiceConstants) Entity(org.hypertrace.entity.data.service.v1.Entity) EntityDataServiceImplBase(org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceImplBase) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) IOException(java.io.IOException) ENTITY_RELATIONSHIPS_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.ENTITY_RELATIONSHIPS_COLLECTION) EnrichedEntities(org.hypertrace.entity.data.service.v1.EnrichedEntities) Query(org.hypertrace.entity.data.service.v1.Query) RAW_ENTITIES_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.RAW_ENTITIES_COLLECTION) Message(com.google.protobuf.Message) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) MergeAndUpsertEntityResponse(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityResponse) Collections(java.util.Collections) RelationshipsQuery(org.hypertrace.entity.data.service.v1.RelationshipsQuery) EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Entity(org.hypertrace.entity.data.service.v1.Entity) ServiceException(com.google.protobuf.ServiceException) List(java.util.List) ArrayList(java.util.ArrayList) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) IOException(java.io.IOException)

Example 2 with MergeAndUpsertEntityResponse

use of org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityResponse in project entity-service by hypertrace.

the class EntityDataCachingClientTest method beforeEach.

@BeforeEach
void beforeEach() throws IOException {
    String uniqueName = InProcessServerBuilder.generateName();
    this.grpcServer = InProcessServerBuilder.forName(uniqueName).directExecutor().addService(this.mockDataService).build().start();
    this.grpcChannel = InProcessChannelBuilder.forName(uniqueName).directExecutor().build();
    this.dataClient = EntityDataClient.builder(this.grpcChannel).build();
    this.possibleResponseEntities = List.of(this.defaultResponseEntity);
    this.responseError = Optional.empty();
    doAnswer(invocation -> {
        StreamObserver<MergeAndUpsertEntityResponse> observer = invocation.getArgument(1, StreamObserver.class);
        Entity inputEntity = invocation.getArgument(0, MergeAndUpsertEntityRequest.class).getEntity();
        responseError.ifPresentOrElse(observer::onError, () -> {
            observer.onNext(MergeAndUpsertEntityResponse.newBuilder().setEntity(this.possibleResponseEntities.stream().filter(entity -> entity.getEntityId().equals(inputEntity.getEntityId())).findFirst().orElse(inputEntity)).build());
            observer.onCompleted();
        });
        return null;
    }).when(this.mockDataService).mergeAndUpsertEntity(any(), any());
    this.testScheduler = new TestScheduler();
    RxJavaPlugins.setComputationSchedulerHandler(ignored -> testScheduler);
}
Also used : MergeAndUpsertEntityResponse(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityResponse) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) ManagedChannel(io.grpc.ManagedChannel) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) InProcessServerBuilder(io.grpc.inprocess.InProcessServerBuilder) StreamObserver(io.grpc.stub.StreamObserver) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Duration(java.time.Duration) Entity(org.hypertrace.entity.data.service.v1.Entity) Server(io.grpc.Server) InProcessChannelBuilder(io.grpc.inprocess.InProcessChannelBuilder) Value(org.hypertrace.entity.data.service.v1.Value) EntityDataServiceImplBase(org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceImplBase) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) UpsertCondition(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityRequest.UpsertCondition) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) MergeAndUpsertEntityRequest(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityRequest) Optional(java.util.Optional) MergeAndUpsertEntityResponse(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityResponse) Collections(java.util.Collections) RxJavaPlugins(io.reactivex.rxjava3.plugins.RxJavaPlugins) Entity(org.hypertrace.entity.data.service.v1.Entity) MergeAndUpsertEntityRequest(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityRequest) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

StreamObserver (io.grpc.stub.StreamObserver)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 List (java.util.List)2 Optional (java.util.Optional)2 RequestContext (org.hypertrace.core.grpcutils.context.RequestContext)2 Entity (org.hypertrace.entity.data.service.v1.Entity)2 EntityDataServiceImplBase (org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceImplBase)2 MergeAndUpsertEntityRequest (org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityRequest)2 MergeAndUpsertEntityResponse (org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityResponse)2 Streams (com.google.common.collect.Streams)1 Descriptors (com.google.protobuf.Descriptors)1 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)1 Message (com.google.protobuf.Message)1 ServiceException (com.google.protobuf.ServiceException)1 Channel (io.grpc.Channel)1 ManagedChannel (io.grpc.ManagedChannel)1 Server (io.grpc.Server)1 InProcessChannelBuilder (io.grpc.inprocess.InProcessChannelBuilder)1 InProcessServerBuilder (io.grpc.inprocess.InProcessServerBuilder)1