Search in sources :

Example 6 with RequestContext

use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.

the class EntityQueryServiceImpl method execute.

@Override
public void execute(EntityQueryRequest request, StreamObserver<ResultSetChunk> responseObserver) {
    RequestContext requestContext = RequestContext.CURRENT.get();
    Optional<String> tenantId = requestContext.getTenantId();
    if (tenantId.isEmpty()) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    final Iterator<Document> documentIterator;
    if (queryAggregationEnabled) {
        final Converter<EntityQueryRequest, org.hypertrace.core.documentstore.query.Query> queryConverter = getQueryConverter();
        final org.hypertrace.core.documentstore.query.Query query;
        try {
            query = queryConverter.convert(request, requestContext);
            documentIterator = entitiesCollection.aggregate(query);
        } catch (final Exception e) {
            responseObserver.onError(new ServiceException(e));
            return;
        }
    } else {
        // TODO: Optimize this later. For now converting to EDS Query and then again to DocStore
        // Query.
        Query query = entityQueryConverter.convertToEDSQuery(requestContext, request);
        /**
         * {@link EntityQueryRequest} selections need to treated differently, since they don't
         * transform one to one to {@link org.hypertrace.entity.data.service.v1.EntityDataRequest}
         * selections
         */
        List<String> docStoreSelections = entityQueryConverter.convertSelectionsToDocStoreSelections(requestContext, request.getSelectionList());
        documentIterator = entitiesCollection.search(DocStoreConverter.transform(tenantId.get(), query, docStoreSelections));
    }
    final DocumentConverter rowConverter = injector.getInstance(DocumentConverter.class);
    ResultSetMetadata resultSetMetadata;
    try {
        resultSetMetadata = this.buildMetadataForSelections(request.getSelectionList());
    } catch (final ConversionException e) {
        responseObserver.onError(new ServiceException(e));
        return;
    }
    if (!documentIterator.hasNext()) {
        ResultSetChunk.Builder resultBuilder = ResultSetChunk.newBuilder();
        resultBuilder.setResultSetMetadata(resultSetMetadata);
        resultBuilder.setIsLastChunk(true);
        resultBuilder.setChunkId(0);
        responseObserver.onNext(resultBuilder.build());
        responseObserver.onCompleted();
        return;
    }
    boolean isNewChunk = true;
    int chunkId = 0, rowCount = 0;
    ResultSetChunk.Builder resultBuilder = ResultSetChunk.newBuilder();
    while (documentIterator.hasNext()) {
        // Set metadata for new chunk
        if (isNewChunk) {
            resultBuilder.setResultSetMetadata(resultSetMetadata);
            isNewChunk = false;
        }
        try {
            final Row row;
            if (queryAggregationEnabled) {
                row = rowConverter.convertToRow(documentIterator.next(), resultSetMetadata);
                resultBuilder.addRow(row);
                rowCount++;
            } else {
                Optional<Entity> entity = DOCUMENT_PARSER.parseOrLog(documentIterator.next(), Entity.newBuilder());
                if (entity.isPresent()) {
                    row = convertToEntityQueryResult(requestContext, entity.get(), request.getSelectionList());
                    resultBuilder.addRow(row);
                    rowCount++;
                }
            }
        } catch (final Exception e) {
            responseObserver.onError(new ServiceException(e));
            return;
        }
        // current chunk is complete
        if (rowCount >= CHUNK_SIZE || !documentIterator.hasNext()) {
            resultBuilder.setChunkId(chunkId++);
            resultBuilder.setIsLastChunk(!documentIterator.hasNext());
            responseObserver.onNext(resultBuilder.build());
            resultBuilder = ResultSetChunk.newBuilder();
            isNewChunk = true;
            rowCount = 0;
        }
    }
    responseObserver.onCompleted();
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) Query(org.hypertrace.entity.data.service.v1.Query) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) ResultSetMetadata(org.hypertrace.entity.query.service.v1.ResultSetMetadata) ServiceException(com.google.protobuf.ServiceException) ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) ServiceException(com.google.protobuf.ServiceException) Row(org.hypertrace.entity.query.service.v1.Row) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) DocumentConverter(org.hypertrace.entity.query.service.converter.response.DocumentConverter) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk)

Example 7 with RequestContext

use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.

the class EntityQueryServiceImpl method total.

@Override
public void total(TotalEntitiesRequest request, StreamObserver<TotalEntitiesResponse> responseObserver) {
    RequestContext requestContext = RequestContext.CURRENT.get();
    Optional<String> tenantId = requestContext.getTenantId();
    if (tenantId.isEmpty()) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    // converting total entities request to entity query request
    EntityQueryRequest entityQueryRequest = EntityQueryRequest.newBuilder().setEntityType(request.getEntityType()).setFilter(request.getFilter()).build();
    // converting entity query request to entity data service query
    Query query = entityQueryConverter.convertToEDSQuery(requestContext, entityQueryRequest);
    // TODO: Replace to use the new org.hypertrace.core.documentstore.query.Query DTO
    long total = entitiesCollection.total(DocStoreConverter.transform(tenantId.get(), query, emptyList()));
    responseObserver.onNext(TotalEntitiesResponse.newBuilder().setTotal(total).build());
    responseObserver.onCompleted();
}
Also used : ServiceException(com.google.protobuf.ServiceException) Query(org.hypertrace.entity.data.service.v1.Query) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest)

Example 8 with RequestContext

use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.

the class EntityQueryServiceImpl method bulkUpdate.

@Override
public void bulkUpdate(BulkEntityUpdateRequest request, StreamObserver<ResultSetChunk> responseObserver) {
    // Validations
    RequestContext requestContext = RequestContext.CURRENT.get();
    Optional<String> maybeTenantId = requestContext.getTenantId();
    if (maybeTenantId.isEmpty()) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    if (StringUtils.isEmpty(request.getEntityType())) {
        responseObserver.onError(new ServiceException("Entity type is missing in the request."));
        return;
    }
    if (request.getEntitiesCount() == 0) {
        responseObserver.onError(new ServiceException("Entities are missing in the request."));
    }
    Map<String, EntityUpdateInfo> entitiesMap = request.getEntitiesMap();
    try {
        doBulkUpdate(requestContext, entitiesMap);
        responseObserver.onCompleted();
    } catch (Exception e) {
        responseObserver.onError(new ServiceException("Error occurred while executing " + request, e));
    }
}
Also used : ServiceException(com.google.protobuf.ServiceException) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) EntityUpdateInfo(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest.EntityUpdateInfo) ServiceException(com.google.protobuf.ServiceException) ConversionException(org.hypertrace.entity.query.service.converter.ConversionException)

Example 9 with RequestContext

use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.

the class EntityQueryServiceImpl method bulkUpdateEntityArrayAttribute.

@Override
public void bulkUpdateEntityArrayAttribute(BulkEntityArrayAttributeUpdateRequest request, StreamObserver<BulkEntityArrayAttributeUpdateResponse> responseObserver) {
    RequestContext requestContext = RequestContext.CURRENT.get();
    String tenantId = requestContext.getTenantId().orElse(null);
    if (isNull(tenantId)) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    try {
        Set<Key> keys = request.getEntityIdsList().stream().map(entityId -> new SingleValueKey(tenantId, entityId)).collect(Collectors.toCollection(LinkedHashSet::new));
        String attributeId = request.getAttribute().getColumnName();
        String subDocPath = entityAttributeMapping.getDocStorePathByAttributeId(requestContext, attributeId).orElseThrow(() -> new IllegalArgumentException("Unknown attribute " + attributeId));
        List<Document> subDocuments = request.getValuesList().stream().map(this::convertToJsonDocument).collect(toUnmodifiableList());
        BulkArrayValueUpdateRequest bulkArrayValueUpdateRequest = new BulkArrayValueUpdateRequest(keys, subDocPath + ARRAY_VALUE_PATH_SUFFIX, getMatchingOperation(request.getOperation()), subDocuments);
        entitiesCollection.bulkOperationOnArrayValue(bulkArrayValueUpdateRequest);
        responseObserver.onNext(BulkEntityArrayAttributeUpdateResponse.newBuilder().build());
        responseObserver.onCompleted();
    } catch (Exception e) {
        responseObserver.onError(e);
    }
}
Also used : DocumentParser(org.hypertrace.entity.data.service.DocumentParser) Function(org.hypertrace.entity.query.service.v1.Function) EntityQueryServiceImplBase(org.hypertrace.entity.query.service.v1.EntityQueryServiceGrpc.EntityQueryServiceImplBase) SneakyThrows(lombok.SneakyThrows) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) UpdateOperation(org.hypertrace.entity.query.service.v1.UpdateOperation) ServiceException(com.google.protobuf.ServiceException) ResultSetMetadata(org.hypertrace.entity.query.service.v1.ResultSetMetadata) LoggerFactory(org.slf4j.LoggerFactory) ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) StreamObserver(io.grpc.stub.StreamObserver) Map(java.util.Map) RelationalExpression(org.hypertrace.core.documentstore.expression.impl.RelationalExpression) BulkEntityArrayAttributeUpdateResponse(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateResponse) BulkEntityArrayAttributeUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateRequest) Objects.isNull(java.util.Objects.isNull) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) DocStoreConverter(org.hypertrace.entity.service.util.DocStoreConverter) Filter(org.hypertrace.core.documentstore.query.Filter) Value(org.hypertrace.entity.query.service.v1.Value) Collections.emptyList(java.util.Collections.emptyList) BulkEntityUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest) Set(java.util.Set) StringUtils(org.hypertrace.entity.service.util.StringUtils) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) ColumnMetadata(org.hypertrace.entity.query.service.v1.ColumnMetadata) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Stream(java.util.stream.Stream) GrpcChannelRegistry(org.hypertrace.core.grpcutils.client.GrpcChannelRegistry) IN(org.hypertrace.core.documentstore.expression.operators.RelationalOperator.IN) TotalEntitiesRequest(org.hypertrace.entity.query.service.v1.TotalEntitiesRequest) Optional(java.util.Optional) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) TypeLiteral(com.google.inject.TypeLiteral) LiteralConstant(org.hypertrace.entity.query.service.v1.LiteralConstant) SetAttribute(org.hypertrace.entity.query.service.v1.SetAttribute) Row(org.hypertrace.entity.query.service.v1.Row) Document(org.hypertrace.core.documentstore.Document) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) DocStoreJsonFormat(org.hypertrace.entity.service.util.DocStoreJsonFormat) ENTITY_ATTRIBUTE_DOC_PREFIX(org.hypertrace.entity.query.service.EntityAttributeMapping.ENTITY_ATTRIBUTE_DOC_PREFIX) AliasProvider(org.hypertrace.entity.query.service.converter.AliasProvider) ConstantExpression(org.hypertrace.core.documentstore.expression.impl.ConstantExpression) EntityUpdateRequest(org.hypertrace.entity.query.service.v1.EntityUpdateRequest) HashMap(java.util.HashMap) Collection(org.hypertrace.core.documentstore.Collection) Collectors.toUnmodifiableList(java.util.stream.Collectors.toUnmodifiableList) ArrayList(java.util.ArrayList) Datastore(org.hypertrace.core.documentstore.Datastore) Converter(org.hypertrace.entity.query.service.converter.Converter) Key(org.hypertrace.core.documentstore.Key) ConverterModule(org.hypertrace.entity.query.service.converter.ConverterModule) EntityUpdateInfo(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest.EntityUpdateInfo) AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) EntityServiceConstants(org.hypertrace.entity.service.constants.EntityServiceConstants) VALUES_FIELD_NUMBER(org.hypertrace.entity.data.service.v1.AttributeValueList.VALUES_FIELD_NUMBER) Entity(org.hypertrace.entity.data.service.v1.Entity) DocumentConverter(org.hypertrace.entity.query.service.converter.response.DocumentConverter) LinkedHashSet(java.util.LinkedHashSet) TotalEntitiesResponse(org.hypertrace.entity.query.service.v1.TotalEntitiesResponse) BulkArrayValueUpdateRequest(org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest) IdentifierExpression(org.hypertrace.core.documentstore.expression.impl.IdentifierExpression) ColumnIdentifier(org.hypertrace.entity.query.service.v1.ColumnIdentifier) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Config(com.typesafe.config.Config) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Printer(org.hypertrace.entity.service.util.DocStoreJsonFormat.Printer) VALUE_LIST_FIELD_NUMBER(org.hypertrace.entity.data.service.v1.AttributeValue.VALUE_LIST_FIELD_NUMBER) AttributeValueList(org.hypertrace.entity.data.service.v1.AttributeValueList) Injector(com.google.inject.Injector) Selection(org.hypertrace.core.documentstore.query.Selection) Query(org.hypertrace.entity.data.service.v1.Query) Expression(org.hypertrace.entity.query.service.v1.Expression) RAW_ENTITIES_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.RAW_ENTITIES_COLLECTION) ValueType(org.hypertrace.entity.query.service.v1.ValueType) Guice(com.google.inject.Guice) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ValueCase(org.hypertrace.entity.query.service.v1.Expression.ValueCase) Collections(java.util.Collections) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) ServiceException(com.google.protobuf.ServiceException) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) BulkArrayValueUpdateRequest(org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Key(org.hypertrace.core.documentstore.Key) ServiceException(com.google.protobuf.ServiceException) ConversionException(org.hypertrace.entity.query.service.converter.ConversionException)

Example 10 with RequestContext

use of org.hypertrace.core.grpcutils.context.RequestContext in project entity-service by hypertrace.

the class EntityQueryServiceImpl method update.

@Override
public void update(EntityUpdateRequest request, StreamObserver<ResultSetChunk> responseObserver) {
    // Validations
    RequestContext requestContext = RequestContext.CURRENT.get();
    Optional<String> maybeTenantId = requestContext.getTenantId();
    if (maybeTenantId.isEmpty()) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    if (StringUtils.isEmpty(request.getEntityType())) {
        responseObserver.onError(new ServiceException("Entity type is missing in the request."));
        return;
    }
    if (request.getEntityIdsCount() == 0) {
        responseObserver.onError(new ServiceException("Entity IDs are missing in the request."));
    }
    if (!request.hasOperation()) {
        responseObserver.onError(new ServiceException("Operation is missing in the request."));
    }
    try {
        // Execute the update
        doUpdate(requestContext, request);
        // Finally, return the selections
        List<Document> documents = getProjectedDocuments(request.getEntityIdsList(), request.getSelectionList(), requestContext);
        responseObserver.onNext(convertDocumentsToResultSetChunk(documents, request.getSelectionList()));
        responseObserver.onCompleted();
    } catch (Exception e) {
        responseObserver.onError(new ServiceException("Error occurred while executing " + request, e));
    }
}
Also used : ServiceException(com.google.protobuf.ServiceException) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) ServiceException(com.google.protobuf.ServiceException) ConversionException(org.hypertrace.entity.query.service.converter.ConversionException)

Aggregations

RequestContext (org.hypertrace.core.grpcutils.context.RequestContext)17 Test (org.junit.jupiter.api.Test)9 Entity (org.hypertrace.entity.data.service.v1.Entity)8 ServiceException (com.google.protobuf.ServiceException)6 Document (org.hypertrace.core.documentstore.Document)5 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)5 Optional (java.util.Optional)4 Query (org.hypertrace.entity.data.service.v1.Query)4 ConversionException (org.hypertrace.entity.query.service.converter.ConversionException)4 ColumnIdentifier (org.hypertrace.entity.query.service.v1.ColumnIdentifier)4 EntityQueryRequest (org.hypertrace.entity.query.service.v1.EntityQueryRequest)4 StreamObserver (io.grpc.stub.StreamObserver)3 List (java.util.List)3 Map (java.util.Map)3 Objects.isNull (java.util.Objects.isNull)3 Collectors (java.util.stream.Collectors)3 Collection (org.hypertrace.core.documentstore.Collection)3 IdentifierExpression (org.hypertrace.core.documentstore.expression.impl.IdentifierExpression)3 EntityUpdateInfo (org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest.EntityUpdateInfo)3 Expression (org.hypertrace.entity.query.service.v1.Expression)3