Search in sources :

Example 1 with Entities

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

the class EntityDataServiceTest method testEntityQueryAttributeWithExistsFiltering.

@Test
public void testEntityQueryAttributeWithExistsFiltering() {
    String stringRandomizer1 = UUID.randomUUID().toString();
    Entity entity1 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.K8S_POD.name()).setEntityName("Some Service").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_EXTERNAL_ID), AttributeValue.newBuilder().setValue(Value.newBuilder().setString("value1").build()).build()).putAttributes("simpleValue1" + "-" + stringRandomizer1, AttributeValue.newBuilder().setValue(Value.newBuilder().setString("StringValue1").build()).build()).build();
    Entity createdEntity1 = entityDataServiceClient.upsert(entity1);
    assertNotNull(createdEntity1.getEntityId());
    String stringRandomizer2 = UUID.randomUUID().toString();
    Entity entity2 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.K8S_POD.name()).setEntityName("Some Service").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_EXTERNAL_ID), AttributeValue.newBuilder().setValue(Value.newBuilder().setString("value2").build()).build()).putAttributes("simpleValue2" + "-" + stringRandomizer2, AttributeValue.newBuilder().setValue(Value.newBuilder().setString("StringValue2").build()).build()).putAttributes("test" + "-" + stringRandomizer2, AttributeValue.newBuilder().setValue(Value.newBuilder().setString("test").build()).build()).build();
    Entity createdEntity2 = entityDataServiceClient.upsert(entity2);
    assertNotNull(createdEntity2.getEntityId());
    // test for exists operator
    AttributeFilter existsFilter = AttributeFilter.newBuilder().setName(EntityConstants.attributeMapPathFor("simpleValue1" + "-" + stringRandomizer1)).setOperator(Operator.EXISTS).build();
    List<Entity> entities = entityDataServiceClient.query(TENANT_ID, Query.newBuilder().setFilter(existsFilter).build());
    assertEquals(1, entities.size());
    assertEquals(createdEntity1, entities.get(0));
    // test for not-exists operator
    AttributeFilter notExistsFilter = AttributeFilter.newBuilder().setName(EntityConstants.attributeMapPathFor("simpleValue3")).setOperator(Operator.NOT_EXISTS).build();
    entities = entityDataServiceClient.query(TENANT_ID, Query.newBuilder().setFilter(notExistsFilter).build());
    assertTrue(entities.size() > 0);
    // test with AND operator
    AttributeFilter eqFilter = AttributeFilter.newBuilder().setName(EntityConstants.attributeMapPathFor("test" + "-" + stringRandomizer2)).setOperator(Operator.EQ).setAttributeValue(AttributeValue.newBuilder().setValue(Value.newBuilder().setString("test").build()).build()).build();
    existsFilter = AttributeFilter.newBuilder().setName(EntityConstants.attributeMapPathFor("simpleValue2" + "-" + stringRandomizer2)).setOperator(Operator.EXISTS).build();
    AttributeFilter andFilter = AttributeFilter.newBuilder().setOperator(Operator.AND).addChildFilter(eqFilter).addChildFilter(existsFilter).build();
    entities = entityDataServiceClient.query(TENANT_ID, Query.newBuilder().setFilter(andFilter).build());
    assertEquals(1, entities.size());
    assertEquals(createdEntity2, entities.get(0));
    // exists with attribute value - discard the value
    existsFilter = AttributeFilter.newBuilder().setName(EntityConstants.attributeMapPathFor("simpleValue1" + "-" + stringRandomizer1)).setOperator(Operator.EXISTS).setAttributeValue(AttributeValue.newBuilder().setValue(Value.newBuilder().setString("StringValue").build()).build()).build();
    entities = entityDataServiceClient.query(TENANT_ID, Query.newBuilder().setFilter(existsFilter).build());
    assertEquals(1, entities.size());
    assertEquals(createdEntity1, entities.get(0));
}
Also used : EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Entity(org.hypertrace.entity.data.service.v1.Entity) AttributeFilter(org.hypertrace.entity.data.service.v1.AttributeFilter) Test(org.junit.jupiter.api.Test)

Example 2 with Entities

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

the class EntityDataServiceTest method testEntityNonAttributeQuery.

@Test
public void testEntityNonAttributeQuery() {
    Entity entity1 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.K8S_POD.name()).setEntityName("Some Service 1").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_EXTERNAL_ID), generateRandomUUIDAttrValue()).build();
    Entity createdEntity1 = entityDataServiceClient.upsert(entity1);
    assertNotNull(createdEntity1);
    assertFalse(createdEntity1.getEntityId().trim().isEmpty());
    Entity entity2 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.K8S_POD.name()).setEntityName("Some Service 2").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_EXTERNAL_ID), generateRandomUUIDAttrValue()).build();
    Entity createdEntity2 = entityDataServiceClient.upsert(entity2);
    assertNotNull(createdEntity2);
    assertFalse(createdEntity2.getEntityId().trim().isEmpty());
    long afterCreatedTime = Instant.now().toEpochMilli();
    Query createTimeQuery = Query.newBuilder().setEntityType(EntityType.K8S_POD.name()).setFilter(AttributeFilter.newBuilder().setOperator(Operator.LT).setName("createdTime").setAttributeValue(AttributeValue.newBuilder().setValue(Value.newBuilder().setLong(afterCreatedTime).build())).build()).build();
    // Query all entities that created time is less than now
    List<Entity> entitiesList = entityDataServiceClient.query(TENANT_ID, createTimeQuery);
    assertTrue(entitiesList.size() > 1);
}
Also used : EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Entity(org.hypertrace.entity.data.service.v1.Entity) Query(org.hypertrace.entity.data.service.v1.Query) Test(org.junit.jupiter.api.Test)

Example 3 with Entities

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

the class EntityQueryServiceTest method testExecute.

@Test
public void testExecute() {
    // create and upsert some entities
    Entity entity1 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 1").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity1 = entityDataServiceClient.upsert(entity1);
    assertNotNull(createdEntity1);
    assertFalse(createdEntity1.getEntityId().trim().isEmpty());
    Entity entity2 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 2").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity2 = entityDataServiceClient.upsert(entity2);
    assertNotNull(createdEntity2);
    assertFalse(createdEntity2.getEntityId().trim().isEmpty());
    Entity entity3 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 3").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity3 = entityDataServiceClient.upsert(entity3);
    assertNotNull(createdEntity3);
    assertFalse(createdEntity3.getEntityId().trim().isEmpty());
    Entity entity4 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 4").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity4 = entityDataServiceClient.upsert(entity4);
    assertNotNull(createdEntity4);
    assertFalse(createdEntity4.getEntityId().trim().isEmpty());
    Entity entity5 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 5").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity5 = entityDataServiceClient.upsert(entity5);
    assertNotNull(createdEntity5);
    assertFalse(createdEntity5.getEntityId().trim().isEmpty());
    EntityQueryRequest queryRequest = EntityQueryRequest.newBuilder().setEntityType(EntityType.SERVICE.name()).setFilter(Filter.newBuilder().setOperatorValue(Operator.LT.getNumber()).setLhs(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("SERVICE.createdTime").build()).build()).setRhs(Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setLong(Instant.now().toEpochMilli()).setValueType(ValueType.LONG).build()).build()).build()).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("SERVICE.id").build()).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("SERVICE.name").build()).build()).build();
    // this entity will be filtered out
    Entity entity6 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 6").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity6 = entityDataServiceClient.upsert(entity6);
    assertNotNull(createdEntity6);
    assertFalse(createdEntity6.getEntityId().trim().isEmpty());
    Iterator<ResultSetChunk> resultSetChunkIterator = GrpcClientRequestContextUtil.executeWithHeadersContext(HEADERS, () -> entityQueryServiceClient.execute(queryRequest));
    List<ResultSetChunk> list = Lists.newArrayList(resultSetChunkIterator);
    assertEquals(3, list.size());
    assertEquals(2, list.get(0).getRowCount());
    assertEquals(0, list.get(0).getChunkId());
    assertFalse(list.get(0).getIsLastChunk());
    assertEquals(2, list.get(1).getRowCount());
    assertEquals(1, list.get(1).getChunkId());
    assertFalse(list.get(1).getIsLastChunk());
    assertEquals(1, list.get(2).getRowCount());
    assertEquals(2, list.get(2).getChunkId());
    assertTrue(list.get(2).getIsLastChunk());
    assertEquals(createdEntity1.getEntityId(), list.get(0).getRow(0).getColumn(0).getString());
    assertEquals(createdEntity1.getEntityName(), list.get(0).getRow(0).getColumn(1).getString());
    assertEquals(createdEntity2.getEntityId(), list.get(0).getRow(1).getColumn(0).getString());
    assertEquals(createdEntity2.getEntityName(), list.get(0).getRow(1).getColumn(1).getString());
    assertEquals(createdEntity3.getEntityId(), list.get(1).getRow(0).getColumn(0).getString());
    assertEquals(createdEntity3.getEntityName(), list.get(1).getRow(0).getColumn(1).getString());
    assertEquals(createdEntity4.getEntityId(), list.get(1).getRow(1).getColumn(0).getString());
    assertEquals(createdEntity4.getEntityName(), list.get(1).getRow(1).getColumn(1).getString());
    assertEquals(createdEntity5.getEntityId(), list.get(2).getRow(0).getColumn(0).getString());
    assertEquals(createdEntity5.getEntityName(), list.get(2).getRow(0).getColumn(1).getString());
    // metadata sent for each chunk
    assertTrue(list.get(0).getResultSetMetadata().getColumnMetadataCount() > 0);
    assertTrue(list.get(1).getResultSetMetadata().getColumnMetadataCount() > 0);
    assertTrue(list.get(2).getResultSetMetadata().getColumnMetadataCount() > 0);
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Example 4 with Entities

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

the class EntityDataCachingClientTest method createOrUpdateEventuallyThrottlesAndMergesTheUpdateRequests.

@Test
void createOrUpdateEventuallyThrottlesAndMergesTheUpdateRequests() {
    // Just reflect entities in this test
    this.possibleResponseEntities = Collections.emptyList();
    Entity firstEntity = this.defaultResponseEntity.toBuilder().setEntityName("first name").putAttributes("key1", createAttribute("value1-1")).putAttributes("key2", createAttribute("value2-1")).build();
    Entity secondEntity = this.defaultResponseEntity.toBuilder().setEntityName("second name").putAttributes("key2", createAttribute("value2-2")).build();
    Entity thirdEntity = this.defaultResponseEntity.toBuilder().setEntityName("third name").putAttributes("key3", createAttribute("value3-3")).build();
    this.dataClient.createOrUpdateEntityEventually(REQUEST_CONTEXT, firstEntity, UpsertCondition.getDefaultInstance(), Duration.ofMillis(1000));
    testScheduler.advanceTimeBy(300, TimeUnit.MILLISECONDS);
    this.dataClient.createOrUpdateEntityEventually(REQUEST_CONTEXT, secondEntity, UpsertCondition.getDefaultInstance(), Duration.ofMillis(200));
    testScheduler.advanceTimeBy(150, TimeUnit.MILLISECONDS);
    this.dataClient.createOrUpdateEntityEventually(REQUEST_CONTEXT, thirdEntity, UpsertCondition.getDefaultInstance(), Duration.ofMillis(5000));
    testScheduler.advanceTimeBy(49, TimeUnit.MILLISECONDS);
    // All 3 should complete at 500ms (we're currently at 499ms)
    verifyNoInteractions(this.mockDataService);
    testScheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
    verify(this.mockDataService, times(1)).mergeAndUpsertEntity(eq(MergeAndUpsertEntityRequest.newBuilder().setEntity(Entity.newBuilder().setEntityId(thirdEntity.getEntityId()).setEntityType(thirdEntity.getEntityType()).setEntityName(thirdEntity.getEntityName()).putAttributes("key1", createAttribute("value1-1")).putAttributes("key2", createAttribute("value2-2")).putAttributes("key3", createAttribute("value3-3")).build()).setUpsertCondition(UpsertCondition.getDefaultInstance()).build()), any());
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) Test(org.junit.jupiter.api.Test)

Example 5 with Entities

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

the class EntityDataServiceImpl method upsertEntities.

@Override
public void upsertEntities(Entities request, StreamObserver<Empty> responseObserver) {
    String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null);
    if (tenantId == null) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    try {
        Map<Key, Entity> entities = request.getEntityList().stream().map(entity -> this.entityNormalizer.normalize(tenantId, entity)).collect(Collectors.toUnmodifiableMap(entity -> this.entityNormalizer.getEntityDocKey(tenantId, entity), Function.identity()));
        java.util.Collection<Entity> existingEntities = getExistingEntities(entities.values());
        upsertEntities(entities, entitiesCollection, responseObserver);
        entityChangeEventGenerator.sendChangeNotification(RequestContext.CURRENT.get(), existingEntities, entities.values());
    } catch (Throwable throwable) {
        LOG.warn("Failed to upsert: {}", request, throwable);
        responseObserver.onError(throwable);
    }
}
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) Key(org.hypertrace.core.documentstore.Key)

Aggregations

Entity (org.hypertrace.entity.data.service.v1.Entity)16 Test (org.junit.jupiter.api.Test)12 EnrichedEntity (org.hypertrace.entity.data.service.v1.EnrichedEntity)9 HashMap (java.util.HashMap)7 ArrayList (java.util.ArrayList)6 Query (org.hypertrace.entity.data.service.v1.Query)6 ServiceException (com.google.protobuf.ServiceException)5 Map (java.util.Map)5 Channel (io.grpc.Channel)4 IOException (java.io.IOException)4 Collections (java.util.Collections)4 Iterator (java.util.Iterator)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 Stream (java.util.stream.Stream)4 Datastore (org.hypertrace.core.documentstore.Datastore)4 Document (org.hypertrace.core.documentstore.Document)4 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)4 Key (org.hypertrace.core.documentstore.Key)4 RequestContext (org.hypertrace.core.grpcutils.context.RequestContext)4