Search in sources :

Example 1 with PFBuilder

use of akka.japi.pf.PFBuilder in project ditto by eclipse.

the class StreamingSessionActor method createOutgoingSignalBehavior.

private Receive createOutgoingSignalBehavior() {
    final PartialFunction<Object, Object> setCorrelationIdAndStartAckForwarder = new PFBuilder<>().match(Signal.class, this::startAckForwarder).match(DittoRuntimeException.class, x -> x).build();
    final Receive publishSignal = ReceiveBuilder.create().match(SubscriptionEvent.class, signal -> {
        logger.debug("Got SubscriptionEvent in <{}> session, publishing: {}", type, signal);
        eventAndResponsePublisher.offer(SessionedJsonifiable.subscription(signal));
    }).match(CommandResponse.class, this::publishResponseOrError).match(DittoRuntimeException.class, this::publishResponseOrError).match(Signal.class, this::isSameOrigin, signal -> logger.withCorrelationId(signal).debug("Got Signal of type <{}> in <{}> session, but this was issued by " + " this connection itself, not publishing", signal.getType(), type)).match(Signal.class, signal -> {
        // check if this session is "allowed" to receive the Signal
        final var streamingType = determineStreamingType(signal);
        @Nullable final var session = streamingSessions.get(streamingType);
        if (null != session && isSessionAllowedToReceiveSignal(signal, session, streamingType)) {
            final ThreadSafeDittoLoggingAdapter l = logger.withCorrelationId(signal);
            l.info("Publishing Signal of type <{}> in <{}> session", signal.getType(), type);
            l.debug("Publishing Signal of type <{}> in <{}> session: {}", type, signal.getType(), signal);
            final DittoHeaders sessionHeaders = DittoHeaders.newBuilder().authorizationContext(authorizationContext).schemaVersion(jsonSchemaVersion).build();
            final var sessionedJsonifiable = SessionedJsonifiable.signal(signal, sessionHeaders, session);
            eventAndResponsePublisher.offer(sessionedJsonifiable);
        }
    }).matchEquals(Done.getInstance(), done -> {
    /* already done, nothing to publish */
    }).build();
    return addPreprocessors(List.of(setCorrelationIdAndStartAckForwarder), publishSignal);
}
Also used : EntityIdPlaceholder(org.eclipse.ditto.connectivity.api.placeholders.EntityIdPlaceholder) PFBuilder(akka.japi.pf.PFBuilder) StreamingType(org.eclipse.ditto.internal.utils.pubsub.StreamingType) Terminated(akka.actor.Terminated) Acknowledgement(org.eclipse.ditto.base.model.signals.acks.Acknowledgement) Signal(org.eclipse.ditto.base.model.signals.Signal) DittoHeaderDefinition(org.eclipse.ditto.base.model.headers.DittoHeaderDefinition) RqlPredicateParser(org.eclipse.ditto.rql.parser.RqlPredicateParser) MessageCommandAckRequestSetter(org.eclipse.ditto.messages.model.signals.commands.acks.MessageCommandAckRequestSetter) DittoRuntimeException(org.eclipse.ditto.base.model.exceptions.DittoRuntimeException) Criteria(org.eclipse.ditto.rql.query.criteria.Criteria) DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) InvalidJwt(org.eclipse.ditto.gateway.service.streaming.InvalidJwt) DittoProtocolSub(org.eclipse.ditto.internal.utils.pubsub.DittoProtocolSub) ActorRef(akka.actor.ActorRef) Duration(java.time.Duration) Map(java.util.Map) RefreshSession(org.eclipse.ditto.gateway.service.streaming.RefreshSession) CommandResponse(org.eclipse.ditto.base.model.signals.commands.CommandResponse) PolicyAnnouncement(org.eclipse.ditto.policies.model.signals.announcements.PolicyAnnouncement) EnumSet(java.util.EnumSet) NamespaceReader(org.eclipse.ditto.base.model.namespaces.NamespaceReader) SubscriptionManager(org.eclipse.ditto.internal.utils.search.SubscriptionManager) SourceQueueWithComplete(akka.stream.javadsl.SourceQueueWithComplete) SubscriptionEvent(org.eclipse.ditto.thingsearch.model.signals.events.SubscriptionEvent) PartialFunction(scala.PartialFunction) EnumMap(java.util.EnumMap) SignalInformationPoint(org.eclipse.ditto.internal.models.signal.SignalInformationPoint) QueryFilterCriteriaFactory(org.eclipse.ditto.rql.query.filter.QueryFilterCriteriaFactory) Done(akka.Done) Collection(java.util.Collection) ThingLiveCommandAckRequestSetter(org.eclipse.ditto.things.model.signals.commands.acks.ThingLiveCommandAckRequestSetter) GatewayInternalErrorException(org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayInternalErrorException) Set(java.util.Set) GatewayWebsocketSessionExpiredException(org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayWebsocketSessionExpiredException) Instant(java.time.Instant) FatalPubSubException(org.eclipse.ditto.base.model.acks.FatalPubSubException) DittoLoggerFactory(org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory) HeaderTranslator(org.eclipse.ditto.protocol.HeaderTranslator) List(java.util.List) ResourcePlaceholder(org.eclipse.ditto.protocol.placeholders.ResourcePlaceholder) Optional(java.util.Optional) Props(akka.actor.Props) Connect(org.eclipse.ditto.gateway.service.streaming.Connect) StartStreaming(org.eclipse.ditto.gateway.service.streaming.StartStreaming) ThingSearchCommand(org.eclipse.ditto.thingsearch.model.signals.commands.ThingSearchCommand) Event(org.eclipse.ditto.base.model.signals.events.Event) AcknowledgementConfig(org.eclipse.ditto.internal.models.acks.config.AcknowledgementConfig) AcknowledgementLabel(org.eclipse.ditto.base.model.acks.AcknowledgementLabel) AuthorizationContext(org.eclipse.ditto.base.model.auth.AuthorizationContext) Jwt(org.eclipse.ditto.gateway.service.streaming.Jwt) DittoHeaderInvalidException(org.eclipse.ditto.base.model.exceptions.DittoHeaderInvalidException) ThreadSafeDittoLoggingAdapter(org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLoggingAdapter) GatewayWebsocketSessionClosedException(org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayWebsocketSessionClosedException) AbstractActorWithTimers(akka.actor.AbstractActorWithTimers) Nullable(javax.annotation.Nullable) JwtAuthenticationResultProvider(org.eclipse.ditto.gateway.service.security.authentication.jwt.JwtAuthenticationResultProvider) TopicPathPlaceholder(org.eclipse.ditto.protocol.placeholders.TopicPathPlaceholder) JsonSchemaVersion(org.eclipse.ditto.base.model.json.JsonSchemaVersion) AcknowledgementLabelNotDeclaredException(org.eclipse.ditto.base.model.acks.AcknowledgementLabelNotDeclaredException) AcknowledgementAggregatorActorStarter(org.eclipse.ditto.internal.models.acks.AcknowledgementAggregatorActorStarter) StopStreaming(org.eclipse.ditto.gateway.service.streaming.StopStreaming) ImmutableJsonWebToken(org.eclipse.ditto.jwt.model.ImmutableJsonWebToken) TimePlaceholder(org.eclipse.ditto.placeholders.TimePlaceholder) ThingModifyCommandAckRequestSetter(org.eclipse.ditto.things.model.signals.commands.acks.ThingModifyCommandAckRequestSetter) JwtValidator(org.eclipse.ditto.gateway.service.security.authentication.jwt.JwtValidator) IncomingSignal(org.eclipse.ditto.gateway.service.streaming.IncomingSignal) AcknowledgementForwarderActor(org.eclipse.ditto.internal.models.acks.AcknowledgementForwarderActor) ReceiveBuilder(akka.japi.pf.ReceiveBuilder) AcknowledgementLabelNotUniqueException(org.eclipse.ditto.base.model.acks.AcknowledgementLabelNotUniqueException) Signal(org.eclipse.ditto.base.model.signals.Signal) IncomingSignal(org.eclipse.ditto.gateway.service.streaming.IncomingSignal) DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) ThreadSafeDittoLoggingAdapter(org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLoggingAdapter) CommandResponse(org.eclipse.ditto.base.model.signals.commands.CommandResponse) DittoRuntimeException(org.eclipse.ditto.base.model.exceptions.DittoRuntimeException) Nullable(javax.annotation.Nullable)

Example 2 with PFBuilder

use of akka.japi.pf.PFBuilder in project ditto by eclipse.

the class SearchSource method retrieveThingForElement.

private Source<Pair<String, JsonObject>, NotUsed> retrieveThingForElement(final String thingId) {
    if (thingIdOnly) {
        final JsonObject idOnlyThingJson = JsonObject.newBuilder().set(Thing.JsonFields.ID, thingId).build();
        return Source.single(Pair.create(thingId, idOnlyThingJson));
    } else {
        return retrieveThing(thingId, fields).map(thingJson -> Pair.create(thingId, thingJson)).recoverWithRetries(1, new PFBuilder<Throwable, Graph<SourceShape<Pair<String, JsonObject>>, NotUsed>>().match(ThingNotAccessibleException.class, thingNotAccessible -> {
            // out-of-sync thing detected
            final ThingsOutOfSync thingsOutOfSync = ThingsOutOfSync.of(Collections.singletonList(ThingId.of(thingId)), getDittoHeaders());
            pubSubMediator.tell(DistPubSubAccess.publishViaGroup(ThingsOutOfSync.TYPE, thingsOutOfSync), ActorRef.noSender());
            return Source.empty();
        }).build());
    }
}
Also used : PFBuilder(akka.japi.pf.PFBuilder) RetrieveThingResponse(org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingResponse) ThingNotAccessibleException(org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException) SourceRef(akka.stream.SourceRef) ThingsOutOfSync(org.eclipse.ditto.thingsearch.model.signals.events.ThingsOutOfSync) Flow(akka.stream.javadsl.Flow) Source(akka.stream.javadsl.Source) ResumeSourceBuilder(org.eclipse.ditto.internal.utils.akka.controlflow.ResumeSourceBuilder) RetrieveThing(org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing) ActorSelection(akka.actor.ActorSelection) JsonObject(org.eclipse.ditto.json.JsonObject) DittoRuntimeException(org.eclipse.ditto.base.model.exceptions.DittoRuntimeException) DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) ThreadSafeDittoLogger(org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLogger) ActorRef(akka.actor.ActorRef) Duration(java.time.Duration) JsonArray(org.eclipse.ditto.json.JsonArray) Patterns(akka.pattern.Patterns) Graph(akka.stream.Graph) ResumeSource(org.eclipse.ditto.internal.utils.akka.controlflow.ResumeSource) StreamThings(org.eclipse.ditto.thingsearch.model.signals.commands.query.StreamThings) Nullable(javax.annotation.Nullable) Thing(org.eclipse.ditto.things.model.Thing) DistPubSubAccess(org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess) RemoteStreamRefActorTerminatedException(akka.stream.RemoteStreamRefActorTerminatedException) SourceShape(akka.stream.SourceShape) GatewayInternalErrorException(org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayInternalErrorException) ThingId(org.eclipse.ditto.things.model.ThingId) DittoLoggerFactory(org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory) Pair(akka.japi.Pair) Consumer(java.util.function.Consumer) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) JsonFactory(org.eclipse.ditto.json.JsonFactory) NotUsed(akka.NotUsed) JsonFieldSelector(org.eclipse.ditto.json.JsonFieldSelector) Optional(java.util.Optional) Collections(java.util.Collections) JsonCollectors(org.eclipse.ditto.json.JsonCollectors) ThingsOutOfSync(org.eclipse.ditto.thingsearch.model.signals.events.ThingsOutOfSync) Graph(akka.stream.Graph) JsonObject(org.eclipse.ditto.json.JsonObject) Pair(akka.japi.Pair)

Example 3 with PFBuilder

use of akka.japi.pf.PFBuilder in project ditto by eclipse.

the class WebSocketRoute method createOutgoing.

private Pair<Connect, Flow<DittoRuntimeException, Message, NotUsed>> createOutgoing(final JsonSchemaVersion version, final CharSequence connectionCorrelationId, final AuthorizationContext connectionAuthContext, final DittoHeaders additionalHeaders, final ProtocolAdapter adapter, final HttpRequest request, final WebsocketConfig websocketConfig, @Nullable final SignalEnrichmentFacade signalEnrichmentFacade, final ThreadSafeDittoLogger logger) {
    final Optional<JsonWebToken> optJsonWebToken = extractJwtFromRequestIfPresent(request);
    final Source<SessionedJsonifiable, SupervisedStream.WithQueue> publisherSource = SupervisedStream.sourceQueue(websocketConfig.getPublisherBackpressureBufferSize());
    final Source<SessionedJsonifiable, Connect> sourceToPreMaterialize = publisherSource.mapMaterializedValue(withQueue -> {
        webSocketSupervisor.supervise(withQueue.getSupervisedStream(), connectionCorrelationId, additionalHeaders);
        return new Connect(withQueue.getSourceQueue(), connectionCorrelationId, STREAMING_TYPE_WS, version, optJsonWebToken.map(JsonWebToken::getExpirationTime).orElse(null), readDeclaredAcknowledgementLabels(additionalHeaders), connectionAuthContext);
    }).recoverWithRetries(1, new PFBuilder<Throwable, Source<SessionedJsonifiable, NotUsed>>().match(GatewayWebsocketSessionExpiredException.class, ex -> {
        logger.info("WebSocket connection terminated because JWT expired!");
        return Source.empty();
    }).match(GatewayWebsocketSessionClosedException.class, ex -> {
        logger.info("WebSocket connection terminated because authorization context changed!");
        return Source.empty();
    }).match(DittoRuntimeException.class, ex -> Source.single(SessionedJsonifiable.error(ex))).build());
    final Pair<Connect, Source<SessionedJsonifiable, NotUsed>> sourcePair = sourceToPreMaterialize.preMaterialize(materializer);
    final Connect connect = sourcePair.first();
    final Source<SessionedJsonifiable, NotUsed> eventAndResponseSource = sourcePair.second();
    final Flow<DittoRuntimeException, SessionedJsonifiable, NotUsed> errorFlow = Flow.fromFunction(SessionedJsonifiable::error);
    final int signalEnrichmentParallelism = streamingConfig.getParallelism();
    final Flow<SessionedJsonifiable, Message, NotUsed> messageFlow = Flow.<SessionedJsonifiable>create().mapAsync(signalEnrichmentParallelism, postprocess(adapter, signalEnrichmentFacade, logger)).mapConcat(x -> x).via(Flow.fromFunction(result -> {
        logger.debug("Sending outgoing WebSocket message: {}", result);
        return result;
    })).via(outgoingMessageSniffer.toAsyncFlow(request)).<Message>map(TextMessage::create).via(Flow.fromFunction(msg -> {
        OUT_COUNTER.increment();
        return msg;
    }));
    return Pair.create(connect, joinOutgoingFlows(eventAndResponseSource, errorFlow, messageFlow));
}
Also used : PFBuilder(akka.japi.pf.PFBuilder) Acknowledgement(org.eclipse.ditto.base.model.signals.acks.Acknowledgement) Signal(org.eclipse.ditto.base.model.signals.Signal) Merge(akka.stream.javadsl.Merge) ConditionChecker.checkNotNull(org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull) DittoMetrics(org.eclipse.ditto.internal.utils.metrics.DittoMetrics) ThreadSafeDittoLogger(org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLogger) SearchErrorResponse(org.eclipse.ditto.thingsearch.model.signals.commands.SearchErrorResponse) ActorRef(akka.actor.ActorRef) Duration(java.time.Duration) Map(java.util.Map) PolicyException(org.eclipse.ditto.policies.model.PolicyException) Message(akka.http.javadsl.model.ws.Message) Set(java.util.Set) ThingId(org.eclipse.ditto.things.model.ThingId) DittoLoggerFactory(org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory) HttpHeader(org.eclipse.ditto.gateway.service.security.HttpHeader) HeaderTranslator(org.eclipse.ditto.protocol.HeaderTranslator) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) WithDittoHeaders(org.eclipse.ditto.base.model.headers.WithDittoHeaders) NotUsed(akka.NotUsed) Attributes(akka.stream.Attributes) Adaptable(org.eclipse.ditto.protocol.Adaptable) TextMessage(akka.http.javadsl.model.ws.TextMessage) Connect(org.eclipse.ditto.gateway.service.streaming.Connect) SessionedJsonifiable(org.eclipse.ditto.gateway.service.streaming.actors.SessionedJsonifiable) Jsonifiable(org.eclipse.ditto.base.model.json.Jsonifiable) DittoJsonException.wrapJsonRuntimeException(org.eclipse.ditto.base.model.exceptions.DittoJsonException.wrapJsonRuntimeException) Flow(akka.stream.javadsl.Flow) AcknowledgementLabel(org.eclipse.ditto.base.model.acks.AcknowledgementLabel) AuthorizationContext(org.eclipse.ditto.base.model.auth.AuthorizationContext) EventSniffer(org.eclipse.ditto.gateway.service.endpoints.utils.EventSniffer) FanOutShape2(akka.stream.FanOutShape2) GatewaySignalEnrichmentProvider(org.eclipse.ditto.gateway.service.endpoints.utils.GatewaySignalEnrichmentProvider) StartedTrace(org.eclipse.ditto.internal.utils.tracing.instruments.trace.StartedTrace) JsonObject(org.eclipse.ditto.json.JsonObject) WithEntityId(org.eclipse.ditto.base.model.entity.id.WithEntityId) SignalEnrichmentFailedException(org.eclipse.ditto.base.model.exceptions.SignalEnrichmentFailedException) TracingTags(org.eclipse.ditto.internal.utils.tracing.TracingTags) SinkShape(akka.stream.SinkShape) BiConsumer(java.util.function.BiConsumer) DittoJsonException(org.eclipse.ditto.base.model.exceptions.DittoJsonException) Either(scala.util.Either) Nullable(javax.annotation.Nullable) StreamingAck(org.eclipse.ditto.gateway.service.streaming.StreamingAck) JsonSchemaVersion(org.eclipse.ditto.base.model.json.JsonSchemaVersion) StreamingActor(org.eclipse.ditto.gateway.service.streaming.actors.StreamingActor) Filter(org.eclipse.ditto.internal.utils.akka.controlflow.Filter) Pair(akka.japi.Pair) ImmutableJsonWebToken(org.eclipse.ditto.jwt.model.ImmutableJsonWebToken) FlowShape(akka.stream.FlowShape) Directives(akka.http.javadsl.server.Directives) PolicyErrorResponse(org.eclipse.ditto.policies.model.signals.commands.PolicyErrorResponse) IncomingSignal(org.eclipse.ditto.gateway.service.streaming.IncomingSignal) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) JsonWebToken(org.eclipse.ditto.jwt.model.JsonWebToken) ThingSearchException(org.eclipse.ditto.thingsearch.model.ThingSearchException) StreamingType(org.eclipse.ditto.internal.utils.pubsub.StreamingType) AbstractRoute(org.eclipse.ditto.gateway.service.endpoints.routes.AbstractRoute) DittoHeaderDefinition(org.eclipse.ditto.base.model.headers.DittoHeaderDefinition) TooManyRequestsException(org.eclipse.ditto.base.model.exceptions.TooManyRequestsException) UniformFanInShape(akka.stream.UniformFanInShape) DittoRuntimeException(org.eclipse.ditto.base.model.exceptions.DittoRuntimeException) DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) LimitRateByRejection(org.eclipse.ditto.internal.utils.akka.controlflow.LimitRateByRejection) ThingErrorResponse(org.eclipse.ditto.things.model.signals.commands.ThingErrorResponse) GraphDSL(akka.stream.javadsl.GraphDSL) Left(scala.util.Left) Patterns(akka.pattern.Patterns) JsonValue(org.eclipse.ditto.json.JsonValue) StreamingConfig(org.eclipse.ditto.gateway.service.util.config.streaming.StreamingConfig) SupervisedStream(org.eclipse.ditto.gateway.service.streaming.actors.SupervisedStream) WebSocketUpgrade(akka.http.javadsl.model.ws.WebSocketUpgrade) Collection(java.util.Collection) GatewayInternalErrorException(org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayInternalErrorException) WebsocketConfig(org.eclipse.ditto.gateway.service.util.config.streaming.WebsocketConfig) GatewayWebsocketSessionExpiredException(org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayWebsocketSessionExpiredException) DittoTracing(org.eclipse.ditto.internal.utils.tracing.DittoTracing) Sink(akka.stream.javadsl.Sink) UUID(java.util.UUID) IllegalAdaptableException(org.eclipse.ditto.protocol.mappingstrategies.IllegalAdaptableException) Collectors(java.util.stream.Collectors) Optional(java.util.Optional) Function(akka.japi.function.Function) SignalEnrichmentFacade(org.eclipse.ditto.internal.models.signalenrichment.SignalEnrichmentFacade) Route(akka.http.javadsl.server.Route) Counter(org.eclipse.ditto.internal.utils.metrics.instruments.counter.Counter) Source(akka.stream.javadsl.Source) StreamControlMessage(org.eclipse.ditto.gateway.service.streaming.StreamControlMessage) CompletableFuture(java.util.concurrent.CompletableFuture) Materializer(akka.stream.Materializer) Logging(akka.event.Logging) ProtocolFactory(org.eclipse.ditto.protocol.ProtocolFactory) JsonifiableAdaptable(org.eclipse.ditto.protocol.JsonifiableAdaptable) EntityId(org.eclipse.ditto.base.model.entity.id.EntityId) GatewayWebsocketSessionClosedException(org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayWebsocketSessionClosedException) JsonArray(org.eclipse.ditto.json.JsonArray) Graph(akka.stream.Graph) Logger(org.slf4j.Logger) Right(scala.util.Right) ProtocolAdapter(org.eclipse.ditto.protocol.adapter.ProtocolAdapter) HttpRequest(akka.http.javadsl.model.HttpRequest) HttpResponse(akka.http.javadsl.model.HttpResponse) DittoHeadersBuilder(org.eclipse.ditto.base.model.headers.DittoHeadersBuilder) JsonFactory(org.eclipse.ditto.json.JsonFactory) JsonField(org.eclipse.ditto.json.JsonField) Collections(java.util.Collections) Message(akka.http.javadsl.model.ws.Message) TextMessage(akka.http.javadsl.model.ws.TextMessage) StreamControlMessage(org.eclipse.ditto.gateway.service.streaming.StreamControlMessage) Connect(org.eclipse.ditto.gateway.service.streaming.Connect) NotUsed(akka.NotUsed) ImmutableJsonWebToken(org.eclipse.ditto.jwt.model.ImmutableJsonWebToken) JsonWebToken(org.eclipse.ditto.jwt.model.JsonWebToken) Source(akka.stream.javadsl.Source) SessionedJsonifiable(org.eclipse.ditto.gateway.service.streaming.actors.SessionedJsonifiable) GatewayWebsocketSessionClosedException(org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayWebsocketSessionClosedException) DittoRuntimeException(org.eclipse.ditto.base.model.exceptions.DittoRuntimeException)

Example 4 with PFBuilder

use of akka.japi.pf.PFBuilder in project ditto by eclipse.

the class MongoOpsUtil method doDeleteByFilter.

private static Source<Optional<Throwable>, NotUsed> doDeleteByFilter(final MongoCollection<Document> collection, final Bson filter) {
    // https://stackoverflow.com/a/33164008
    // claims unordered bulk ops halve MongoDB load
    final List<WriteModel<Document>> writeModel = Collections.singletonList(new DeleteManyModel<>(filter));
    final BulkWriteOptions options = new BulkWriteOptions().ordered(false);
    return Source.fromPublisher(collection.bulkWrite(writeModel, options)).map(result -> {
        if (LOGGER.isDebugEnabled()) {
            // in contrast to Bson, BsonDocument has meaningful toString()
            final BsonDocument filterBsonDoc = BsonUtil.toBsonDocument(filter);
            LOGGER.debug("Deleted <{}> documents from collection <{}>. Filter was <{}>.", result.getDeletedCount(), collection.getNamespace(), filterBsonDoc);
        }
        return Optional.<Throwable>empty();
    }).recoverWithRetries(RETRY_ATTEMPTS, new PFBuilder<Throwable, Source<Optional<Throwable>, NotUsed>>().matchAny(throwable -> Source.single(Optional.of(throwable))).build());
}
Also used : Document(org.bson.Document) PFBuilder(akka.japi.pf.PFBuilder) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Source(akka.stream.javadsl.Source) ConditionChecker.checkNotNull(org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) DeleteManyModel(com.mongodb.client.model.DeleteManyModel) WriteModel(com.mongodb.client.model.WriteModel) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) BsonDocument(org.bson.BsonDocument) Bson(org.bson.conversions.Bson) List(java.util.List) BsonUtil(org.eclipse.ditto.internal.utils.persistence.mongo.BsonUtil) NotUsed(akka.NotUsed) Optional(java.util.Optional) Collections(java.util.Collections) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) BsonDocument(org.bson.BsonDocument) WriteModel(com.mongodb.client.model.WriteModel) Source(akka.stream.javadsl.Source)

Example 5 with PFBuilder

use of akka.japi.pf.PFBuilder in project ditto by eclipse.

the class MongoThingsSearchUpdaterPersistence method purge.

@Override
public Source<List<Throwable>, NotUsed> purge(final CharSequence namespace) {
    final Bson filter = thingNamespaceFilter(namespace);
    final Bson update = new BsonDocument().append(AbstractWriteModel.SET, new BsonDocument().append(PersistenceConstants.FIELD_DELETE_AT, new BsonDateTime(0L)));
    final UpdateOptions updateOptions = new UpdateOptions().bypassDocumentValidation(true);
    final WriteModel<Document> writeModel = new UpdateManyModel<>(filter, update, updateOptions);
    return Source.fromPublisher(collection.bulkWrite(Collections.singletonList(writeModel))).map(bulkWriteResult -> Collections.<Throwable>emptyList()).recoverWithRetries(1, new PFBuilder<Throwable, Source<List<Throwable>, NotUsed>>().matchAny(throwable -> Source.single(Collections.singletonList(throwable))).build());
}
Also used : Document(org.bson.Document) PFBuilder(akka.japi.pf.PFBuilder) Source(akka.stream.javadsl.Source) WriteModel(com.mongodb.client.model.WriteModel) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) BsonString(org.bson.BsonString) BsonDocument(org.bson.BsonDocument) Filters.in(com.mongodb.client.model.Filters.in) Bson(org.bson.conversions.Bson) BsonDateTime(org.bson.BsonDateTime) PersistenceConstants(org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants) Filters.and(com.mongodb.client.model.Filters.and) UpdaterPersistenceConfig(org.eclipse.ditto.thingsearch.service.common.config.UpdaterPersistenceConfig) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) AbstractWriteModel(org.eclipse.ditto.thingsearch.service.persistence.write.model.AbstractWriteModel) Map(java.util.Map) Filters.eq(com.mongodb.client.model.Filters.eq) UpdateOptions(com.mongodb.client.model.UpdateOptions) PolicyReferenceTag(org.eclipse.ditto.policies.api.PolicyReferenceTag) ThingsSearchUpdaterPersistence(org.eclipse.ditto.thingsearch.service.persistence.write.ThingsSearchUpdaterPersistence) UpdateManyModel(com.mongodb.client.model.UpdateManyModel) Publisher(org.reactivestreams.Publisher) PolicyId(org.eclipse.ditto.policies.model.PolicyId) ThingId(org.eclipse.ditto.things.model.ThingId) Collectors(java.util.stream.Collectors) List(java.util.List) NotUsed(akka.NotUsed) PolicyTag(org.eclipse.ditto.policies.api.PolicyTag) BsonInt32(org.bson.BsonInt32) Filters.lt(com.mongodb.client.model.Filters.lt) Collections(java.util.Collections) BsonDocument(org.bson.BsonDocument) BsonDateTime(org.bson.BsonDateTime) UpdateManyModel(com.mongodb.client.model.UpdateManyModel) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) UpdateOptions(com.mongodb.client.model.UpdateOptions) Source(akka.stream.javadsl.Source) Bson(org.bson.conversions.Bson)

Aggregations

PFBuilder (akka.japi.pf.PFBuilder)6 NotUsed (akka.NotUsed)5 Source (akka.stream.javadsl.Source)5 List (java.util.List)5 ActorRef (akka.actor.ActorRef)4 Duration (java.time.Duration)4 Collections (java.util.Collections)4 Optional (java.util.Optional)4 Nullable (javax.annotation.Nullable)4 DittoRuntimeException (org.eclipse.ditto.base.model.exceptions.DittoRuntimeException)4 DittoHeaders (org.eclipse.ditto.base.model.headers.DittoHeaders)4 GatewayInternalErrorException (org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayInternalErrorException)4 DittoLoggerFactory (org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory)4 Patterns (akka.pattern.Patterns)3 Map (java.util.Map)3 CompletionStage (java.util.concurrent.CompletionStage)3 Collectors (java.util.stream.Collectors)3 ConditionChecker.checkNotNull (org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull)3 ThingId (org.eclipse.ditto.things.model.ThingId)3 Props (akka.actor.Props)2