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