Search in sources :

Example 1 with EnrichedEntity

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

the class EntityDataServiceTest method upsertAndVerify.

private void upsertAndVerify(EnrichedEntity enrichedEntity) {
    EnrichedEntity createdEntity = entityDataServiceClient.upsertEnrichedEntity(enrichedEntity);
    assertNotNull(enrichedEntity);
    assertNotNull(createdEntity.getEntityId().trim());
    EnrichedEntity actualBackendEntity = entityDataServiceClient.getEnrichedEntityById(TENANT_ID, createdEntity.getEntityId());
    assertEquals(createdEntity, actualBackendEntity);
}
Also used : EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity)

Example 2 with EnrichedEntity

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

the class EntityDataServiceTest method testUpsertAndGetEnrichedEntity.

@Test
public void testUpsertAndGetEnrichedEntity() {
    EnrichedEntity entity = EnrichedEntity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.K8S_POD.name()).setEntityName("Some Service").setEntityId(UUID.randomUUID().toString()).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_EXTERNAL_ID), generateRandomUUIDAttrValue()).build();
    upsertAndVerify(entity);
}
Also used : EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Test(org.junit.jupiter.api.Test)

Example 3 with EnrichedEntity

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

the class EntityDataServiceImpl method upsertEnrichedEntities.

@Override
public void upsertEnrichedEntities(EnrichedEntities request, StreamObserver<Empty> responseObserver) {
    String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null);
    if (isNull(tenantId)) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    for (EnrichedEntity entity : request.getEntitiesList()) {
        if (StringUtils.isEmpty(entity.getEntityType())) {
            LOG.info("{}. Invalid upsertEnrichedEntities request:{}", entity, ErrorMessages.ENTITY_TYPE_EMPTY);
            responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_TYPE_EMPTY));
            return;
        }
        if (StringUtils.isEmpty(entity.getEntityId())) {
            LOG.info("{}. Invalid upsertEnrichedEntities request:{}", entity, ErrorMessages.ENTITY_ID_EMPTY);
            responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_ID_EMPTY));
            return;
        }
    }
    Map<Key, EnrichedEntity> entities = request.getEntitiesList().stream().collect(Collectors.toUnmodifiableMap(entity -> this.entityNormalizer.getEntityDocKey(tenantId, entity.getEntityType(), entity.getEntityId()), Function.identity()));
    upsertEntities(entities, enrichedEntitiesCollection, responseObserver);
}
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) ServiceException(com.google.protobuf.ServiceException) EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Key(org.hypertrace.core.documentstore.Key)

Example 4 with EnrichedEntity

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

the class EdsCacheClient method initCache.

private void initCache(EntityServiceClientCacheConfig cacheConfig, Executor executor) {
    this.enrichedEntityCache = CacheBuilder.newBuilder().refreshAfterWrite(cacheConfig.getEnrichedEntityCacheRefreshMs(), TimeUnit.MILLISECONDS).expireAfterWrite(cacheConfig.getEnrichedEntityCacheExpiryMs(), TimeUnit.MILLISECONDS).maximumSize(cacheConfig.getEnrichedEntityMaxCacheSize()).recordStats().build(CacheLoader.asyncReloading(new CacheLoader<>() {

        public EnrichedEntity load(@Nonnull EdsCacheKey key) throws Exception {
            EnrichedEntity enrichedEntity = client.getEnrichedEntityById(key.tenantId, key.entityId);
            if (enrichedEntity == null) {
                throw new NotFoundException("Enriched entity not found");
            }
            return enrichedEntity;
        }
    }, executor));
    this.entityCache = CacheBuilder.newBuilder().refreshAfterWrite(cacheConfig.getEntityCacheRefreshMs(), TimeUnit.MILLISECONDS).expireAfterWrite(cacheConfig.getEntityCacheExpiryMs(), TimeUnit.MILLISECONDS).maximumSize(cacheConfig.getEntityMaxCacheSize()).recordStats().build(CacheLoader.asyncReloading(new CacheLoader<>() {

        public Entity load(@Nonnull EdsCacheKey key) throws Exception {
            Entity entity = client.getById(key.tenantId, key.entityId);
            if (entity == null) {
                throw new NotFoundException("Entity not found");
            }
            return entity;
        }
    }, executor));
    this.entityIdsCache = CacheBuilder.newBuilder().refreshAfterWrite(cacheConfig.getEntityIdsCacheRefreshMs(), TimeUnit.MILLISECONDS).expireAfterWrite(cacheConfig.getEntityIdsCacheExpiryMs(), TimeUnit.MILLISECONDS).maximumSize(cacheConfig.getEntityIdsMaxCacheSize()).recordStats().build(CacheLoader.asyncReloading(new CacheLoader<>() {

        public String load(@Nonnull EdsTypeAndIdAttributesCacheKey key) throws Exception {
            Entity entity = client.getByTypeAndIdentifyingAttributes(key.tenantId, key.byTypeAndIdentifyingAttributes);
            if (entity == null) {
                throw new NotFoundException("Entity not found!!");
            }
            entityCache.put(new EdsCacheKey(entity.getTenantId(), entity.getEntityId()), entity);
            return entity.getEntityId();
        }
    }, executor));
    PlatformMetricsRegistry.registerCache(this.getClass().getName() + ".enrichedEntityCache", enrichedEntityCache, Collections.emptyMap());
    PlatformMetricsRegistry.registerCache(this.getClass().getName() + ".entityCache", entityCache, Collections.emptyMap());
    PlatformMetricsRegistry.registerCache(this.getClass().getName() + ".entityIdsCache", entityIdsCache, Collections.emptyMap());
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) NotFoundException(org.hypertrace.entity.data.service.client.exception.NotFoundException) NotFoundException(org.hypertrace.entity.data.service.client.exception.NotFoundException) ExecutionException(java.util.concurrent.ExecutionException) EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity)

Example 5 with EnrichedEntity

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

the class EdsCacheClientTest method testGetEnrichedEntityById.

@Test
public void testGetEnrichedEntityById() {
    String tenantId = "tenant";
    String enrichedEntityId = "enriched-12345";
    Map<String, AttributeValue> identifyingAttributesMap = new HashMap<>();
    identifyingAttributesMap.put("entity_name", AttributeValue.newBuilder().setValue(Value.newBuilder().setString("GET /products").build()).build());
    identifyingAttributesMap.put("is_active", AttributeValue.newBuilder().setValue(Value.newBuilder().setBoolean(true).build()).build());
    EnrichedEntity enrichedEntity = EnrichedEntity.newBuilder().setEntityId(enrichedEntityId).setEntityType("API").setEntityName("GET /products").putAllIdentifyingAttributes(identifyingAttributesMap).build();
    when(entityDataServiceClient.getEnrichedEntityById(anyString(), anyString())).thenReturn(enrichedEntity);
    edsCacheClient.getEnrichedEntityById(tenantId, enrichedEntityId);
    edsCacheClient.getEnrichedEntityById(tenantId, enrichedEntityId);
    verify(entityDataServiceClient, times(1)).getEnrichedEntityById("tenant", "enriched-12345");
}
Also used : AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Test(org.junit.jupiter.api.Test)

Aggregations

EnrichedEntity (org.hypertrace.entity.data.service.v1.EnrichedEntity)8 Test (org.junit.jupiter.api.Test)4 HashMap (java.util.HashMap)2 ByIdRequest (org.hypertrace.entity.data.service.v1.ByIdRequest)2 Entity (org.hypertrace.entity.data.service.v1.Entity)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 StreamObserver (io.grpc.stub.StreamObserver)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Objects.isNull (java.util.Objects.isNull)1 Optional (java.util.Optional)1