-
Notifications
You must be signed in to change notification settings - Fork 215
/
SearchSource.java
244 lines (219 loc) · 10.3 KB
/
SearchSource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.search;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletionStage;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.commands.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.internal.utils.akka.controlflow.ResumeSource;
import org.eclipse.ditto.internal.utils.akka.controlflow.ResumeSourceBuilder;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonCollectors;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingResponse;
import org.eclipse.ditto.thingsearch.model.signals.commands.query.StreamThings;
import org.eclipse.ditto.thingsearch.model.signals.events.ThingsOutOfSync;
import akka.NotUsed;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.japi.Pair;
import akka.japi.pf.PFBuilder;
import akka.pattern.Patterns;
import akka.stream.Graph;
import akka.stream.RemoteStreamRefActorTerminatedException;
import akka.stream.SourceRef;
import akka.stream.SourceShape;
import akka.stream.javadsl.Flow;
import akka.stream.javadsl.Source;
/**
* Source of search results for one query.
*/
public final class SearchSource {
private static final ThreadSafeDittoLogger LOGGER = DittoLoggerFactory.getThreadSafeLogger(SearchSource.class);
private final ActorRef pubSubMediator;
private final ActorSelection conciergeForwarder;
private final Duration thingsAskTimeout;
private final Duration searchAskTimeout;
@Nullable private final JsonFieldSelector fields;
private final JsonFieldSelector sortFields;
private final StreamThings streamThings;
private final boolean thingIdOnly;
private final String lastThingId;
SearchSource(final ActorRef pubSubMediator,
final ActorSelection conciergeForwarder,
final Duration thingsAskTimeout,
final Duration searchAskTimeout,
@Nullable final JsonFieldSelector fields,
final JsonFieldSelector sortFields,
final StreamThings streamThings,
final String lastThingId) {
this.pubSubMediator = pubSubMediator;
this.conciergeForwarder = conciergeForwarder;
this.thingsAskTimeout = thingsAskTimeout;
this.searchAskTimeout = searchAskTimeout;
this.fields = fields;
this.sortFields = sortFields;
this.streamThings = streamThings;
this.thingIdOnly = fields != null && fields.getSize() == 1 &&
fields.getPointers().contains(Thing.JsonFields.ID.getPointer());
this.lastThingId = lastThingId;
}
/**
* Create a new builder.
*
* @return a new builder.
*/
public static SearchSourceBuilder newBuilder() {
return new SearchSourceBuilder();
}
/**
* Start a source of search results.
*
* @return the source of search results.
*/
public Source<JsonObject, NotUsed> start(final Consumer<ResumeSourceBuilder<?, ?>> configurer) {
return startAsPair(configurer).map(Pair::second);
}
/**
* Start a robust source of search results paired with their IDs.
*
* @param configurer consumer to configure the resume source.
* @return source of pair of ID-result pairs where the ID can be used for resumption.
*/
public Source<Pair<String, JsonObject>, NotUsed> startAsPair(final Consumer<ResumeSourceBuilder<?, ?>> configurer) {
// start with a ResumeSourceBuilder with useful defaults.
final ResumeSourceBuilder<String, Pair<String, JsonObject>> builder =
ResumeSource.<String, Pair<String, JsonObject>>newBuilder()
.minBackoff(Duration.ofSeconds(1L))
.maxBackoff(Duration.ofSeconds(20L))
.recovery(Duration.ofSeconds(60L))
.maxRestarts(25)
.initialSeed(lastThingId)
.resume(this::resume)
.nextSeed(this::nextSeed)
.mapError(this::mapError);
configurer.accept(builder);
return builder.build();
}
/**
* Decide whether an error is recoverable.
*
* @param error error from upstream.
* @return an empty optional if the error is recoverable, or a DittoRuntimeException if the error is not
* recoverable.
*/
private Optional<Throwable> mapError(final Throwable error) {
if (error instanceof RemoteStreamRefActorTerminatedException) {
LOGGER.withCorrelationId(streamThings).info("Resuming from: {}", error.toString());
return Optional.empty();
} else {
return Optional.of(DittoRuntimeException.asDittoRuntimeException(error, e -> {
LOGGER.withCorrelationId(streamThings).error("Unexpected error", e);
return DittoInternalErrorException.newBuilder().build();
}));
}
}
private Source<Pair<String, JsonObject>, NotUsed> resume(final String lastThingId) {
return streamThingsFrom(lastThingId)
.mapAsync(1, streamThings -> Patterns.ask(conciergeForwarder, streamThings, searchAskTimeout))
.via(expectMsgClass(SourceRef.class))
.flatMapConcat(SourceRef::source)
.flatMapConcat(thingId -> retrieveThingForElement((String) thingId));
}
private String nextSeed(final List<Pair<String, JsonObject>> finalElements) {
return finalElements.isEmpty()
? lastThingId
: finalElements.get(finalElements.size() - 1).first();
}
private Source<StreamThings, NotUsed> streamThingsFrom(final String lastThingId) {
if (lastThingId.isEmpty()) {
return Source.single(streamThings);
} else {
return retrieveSortValues(lastThingId).map(streamThings::setSortValues);
}
}
private Source<JsonArray, NotUsed> retrieveSortValues(final String thingId) {
return retrieveThing(thingId, sortFields)
.map(thingJson -> sortFields.getPointers()
.stream()
.map(pointer -> thingJson.getValue(pointer).orElse(JsonFactory.nullLiteral()))
.collect(JsonCollectors.valuesToArray())
);
}
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()
);
}
}
private Source<JsonObject, NotUsed> retrieveThing(final String thingId,
@Nullable final JsonFieldSelector selector) {
final RetrieveThing retrieveThing = RetrieveThing.getBuilder(ThingId.of(thingId), getDittoHeaders())
.withSelectedFields(selector)
.build();
final CompletionStage<Object> responseFuture =
Patterns.ask(conciergeForwarder, retrieveThing, thingsAskTimeout);
return Source.completionStage(responseFuture)
.via(expectMsgClass(RetrieveThingResponse.class))
.map(response -> response.getEntity().asObject());
}
private DittoHeaders getDittoHeaders() {
return streamThings.getDittoHeaders();
}
private <T> Flow<Object, T, NotUsed> expectMsgClass(final Class<T> clazz) {
return Flow.create()
.flatMapConcat(element -> {
if (clazz.isInstance(element)) {
return Source.single(clazz.cast(element));
} else if (element instanceof Throwable) {
return Source.failed((Throwable) element);
} else {
final String message =
String.format("Expect <%s>, got <%s>", clazz.getCanonicalName(), element);
return Source.failed(new ClassCastException(message));
}
});
}
}