-
Notifications
You must be signed in to change notification settings - Fork 215
/
BulkWriteResultAckFlowTest.java
225 lines (197 loc) · 11.1 KB
/
BulkWriteResultAckFlowTest.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
/*
* 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.thingsearch.service.persistence.write.streaming;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.signals.ShardedMessageEnvelope;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgement;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.thingsearch.api.commands.sudo.UpdateThingResponse;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.AbstractWriteModel;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.Metadata;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.ThingDeleteModel;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.ThingWriteModel;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.WriteResultAndErrors;
import org.eclipse.ditto.thingsearch.service.updater.actors.MongoWriteModel;
import org.junit.After;
import org.junit.Test;
import com.mongodb.MongoBulkWriteException;
import com.mongodb.MongoSocketReadException;
import com.mongodb.ServerAddress;
import com.mongodb.bulk.BulkWriteError;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.bulk.BulkWriteUpsert;
import akka.actor.ActorSystem;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
/**
* Tests {@link BulkWriteResultAckFlow}.
*/
public final class BulkWriteResultAckFlowTest {
private final ActorSystem actorSystem = ActorSystem.create();
private final TestProbe updaterShardProbe = TestProbe.apply("updater", actorSystem);
private final BulkWriteResultAckFlow underTest = BulkWriteResultAckFlow.of(updaterShardProbe.ref());
@After
public void stopActorSystem() {
TestKit.shutdownActorSystem(actorSystem);
}
@Test
public void allSuccess() {
final List<MongoWriteModel> writeModels = generate5WriteModels();
final BulkWriteResult result = BulkWriteResult.acknowledged(0, 3, 1, 1,
List.of(new BulkWriteUpsert(0, new BsonString("upsert 0")),
new BulkWriteUpsert(4, new BsonString("upsert 4")))
);
// WHEN
final WriteResultAndErrors resultAndErrors = WriteResultAndErrors.success(writeModels, result, "correlation");
final String message = runBulkWriteResultAckFlowAndGetFirstLogEntry(resultAndErrors);
// THEN
actorSystem.log().info(message);
assertThat(message).contains("Acknowledged: Success");
}
@Test
public void partialSuccess() {
final List<MongoWriteModel> writeModels = generate5WriteModels();
final BulkWriteResult result = BulkWriteResult.acknowledged(1, 2, 1, 2, List.of(), List.of());
final List<BulkWriteError> updateFailure = List.of(
new BulkWriteError(11000, "E11000 duplicate key error", new BsonDocument(), 3),
new BulkWriteError(50, "E50 operation timed out", new BsonDocument(), 4)
);
// WHEN: BulkWriteResultAckFlow receives partial update success with errors, one of which is not duplicate key
final WriteResultAndErrors resultAndErrors = WriteResultAndErrors.failure(writeModels,
new MongoBulkWriteException(result, updateFailure, null, new ServerAddress(), Set.of()), "correlation");
final String message = runBulkWriteResultAckFlowAndGetFirstLogEntry(resultAndErrors);
// THEN: the non-duplicate-key error triggers a failure acknowledgement
actorSystem.log().info(message);
assertThat(expectUpdateThingResponse(writeModels.get(4).getDitto().getMetadata().getThingId()))
.describedAs("response is failure")
.returns(false, UpdateThingResponse::isSuccess);
assertThat(message).contains("Acknowledged: PartialSuccess");
}
@Test
public void unexpectedMongoSocketReadException() {
final List<MongoWriteModel> writeModels = generate5WriteModels();
// WHEN: BulkWriteResultAckFlow receives unexpected error
final WriteResultAndErrors resultAndErrors = WriteResultAndErrors.unexpectedError(writeModels,
new MongoSocketReadException("Gee, database is down. Whatever shall I do?", new ServerAddress(),
new IllegalMonitorStateException("Unsupported resolution")), "correlation");
final String message = runBulkWriteResultAckFlowAndGetFirstLogEntry(resultAndErrors);
// THEN: all ThingUpdaters receive negative acknowledgement.
actorSystem.log().info(message);
for (final var writeModel : writeModels) {
assertThat(expectUpdateThingResponse(writeModel.getDitto().getMetadata().getThingId()))
.describedAs("response is failure")
.returns(false, UpdateThingResponse::isSuccess);
}
assertThat(message).contains("NotAcknowledged: UnexpectedError", "MongoSocketReadException");
}
// test that indices in bulk write errors are all within bounds.
// upsert indexes are not checked since they do not participate in acknowledgement handling.
@Test
public void errorIndexOutOfBoundError() {
final List<MongoWriteModel> writeModels = generate5WriteModels();
final BulkWriteResult result = BulkWriteResult.acknowledged(1, 2, 1, 2, List.of(), List.of());
final List<BulkWriteError> updateFailure = List.of(
new BulkWriteError(11000, "E11000 duplicate key error", new BsonDocument(), 0),
new BulkWriteError(50, "E50 operation timed out", new BsonDocument(), 5)
);
// WHEN: BulkWriteResultAckFlow receives partial update success with at least 1 error with out-of-bound index
final WriteResultAndErrors resultAndErrors = WriteResultAndErrors.failure(writeModels,
new MongoBulkWriteException(result, updateFailure, null, new ServerAddress(), Set.of()), "correlation");
final String message = runBulkWriteResultAckFlowAndGetFirstLogEntry(resultAndErrors);
// THEN: All updates are considered failures
actorSystem.log().info(message);
for (final MongoWriteModel writeModel : writeModels) {
final UpdateThingResponse response =
expectUpdateThingResponse(writeModel.getDitto().getMetadata().getThingId());
assertThat(response).describedAs("response is failure").returns(false, UpdateThingResponse::isSuccess);
}
assertThat(message).contains("ConsistencyError[indexOutOfBound]");
}
@Test
public void acknowledgements() {
final List<TestProbe> probes =
IntStream.range(0, 5).mapToObj(i -> TestProbe.apply(actorSystem)).collect(Collectors.toList());
final List<MongoWriteModel> writeModels = generateWriteModels(probes);
final BulkWriteResult result = BulkWriteResult.acknowledged(1, 2, 1, 2, List.of(), List.of());
final List<BulkWriteError> updateFailure = List.of(
new BulkWriteError(11000, "E11000 duplicate key error", new BsonDocument(), 3),
new BulkWriteError(50, "E50 operation timed out", new BsonDocument(), 4)
);
// WHEN: BulkWriteResultAckFlow receives partial update success with errors, one of which is not duplicate key
final WriteResultAndErrors resultAndErrors = WriteResultAndErrors.failure(writeModels,
new MongoBulkWriteException(result, updateFailure, null, new ServerAddress(), Set.of()), "correlation");
runBulkWriteResultAckFlowAndGetFirstLogEntry(resultAndErrors);
// THEN: only the non-duplicate-key sender receives negative acknowledgement
assertThat(probes.get(0).expectMsgClass(Acknowledgement.class).getHttpStatus())
.isEqualTo(HttpStatus.NO_CONTENT);
assertThat(probes.get(1).expectMsgClass(Acknowledgement.class).getHttpStatus())
.isEqualTo(HttpStatus.NO_CONTENT);
assertThat(probes.get(2).expectMsgClass(Acknowledgement.class).getHttpStatus())
.isEqualTo(HttpStatus.NO_CONTENT);
assertThat(probes.get(3).expectMsgClass(Acknowledgement.class).getHttpStatus())
.isEqualTo(HttpStatus.NO_CONTENT);
assertThat(probes.get(4).expectMsgClass(Acknowledgement.class).getHttpStatus())
.isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
private String runBulkWriteResultAckFlowAndGetFirstLogEntry(final WriteResultAndErrors writeResultAndErrors) {
return Source.single(writeResultAndErrors)
.via(underTest.start(Duration.ZERO))
.runWith(Sink.head(), actorSystem)
.toCompletableFuture()
.join();
}
private List<MongoWriteModel> generate5WriteModels() {
return generateWriteModels(
IntStream.range(0, 5).mapToObj(i -> TestProbe.apply(actorSystem)).collect(Collectors.toList()));
}
private List<MongoWriteModel> generateWriteModels(final List<TestProbe> probes) {
final int howMany = probes.size();
final List<MongoWriteModel> writeModels = new ArrayList<>(howMany);
for (int i = 0; i < howMany; ++i) {
final ThingId thingId = ThingId.of("thing", String.valueOf(i));
final long thingRevision = i * 10;
final PolicyId policyId = i % 4 < 2 ? null : PolicyId.of("policy", String.valueOf(i));
final long policyRevision = i * 100;
final Metadata metadata =
Metadata.of(thingId, thingRevision, policyId, policyRevision, List.of(), null, probes.get(i).ref());
final AbstractWriteModel abstractModel;
if (i % 2 == 0) {
abstractModel = ThingDeleteModel.of(metadata);
} else {
abstractModel = ThingWriteModel.of(metadata, new BsonDocument());
}
writeModels.add(MongoWriteModel.of(abstractModel, abstractModel.toMongo(), false));
}
return writeModels;
}
private UpdateThingResponse expectUpdateThingResponse(final ThingId forWhom) {
final ShardedMessageEnvelope envelope = updaterShardProbe.expectMsgClass(ShardedMessageEnvelope.class);
assertThat(envelope.getType()).describedAs("ShardedMessageEnvelope#getType")
.isEqualTo(UpdateThingResponse.TYPE);
assertThat((CharSequence) envelope.getEntityId()).describedAs("ShardedMessageEnvelope#getEntityId")
.isEqualTo(forWhom);
return UpdateThingResponse.fromJson(envelope.getMessage(), envelope.getDittoHeaders());
}
}