/
FencedRpcEndpointTest.java
346 lines (273 loc) · 12.8 KB
/
FencedRpcEndpointTest.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.flink.runtime.rpc;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.core.testutils.OneShotLatch;
import org.apache.flink.runtime.messages.Acknowledge;
import org.apache.flink.runtime.rpc.exceptions.FencingTokenException;
import org.apache.flink.runtime.rpc.exceptions.RpcException;
import org.apache.flink.util.ExceptionUtils;
import org.apache.flink.util.FlinkException;
import org.apache.flink.util.TestLogger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class FencedRpcEndpointTest extends TestLogger {
private static final Time timeout = Time.seconds(10L);
private static RpcService rpcService;
@BeforeClass
public static void setup() {
rpcService = new TestingRpcService();
}
@AfterClass
public static void teardown() throws ExecutionException, InterruptedException, TimeoutException {
if (rpcService != null) {
RpcUtils.terminateRpcService(rpcService, timeout);
}
}
/**
* Tests that the fencing token can be retrieved from the FencedRpcEndpoint and self
* FencedRpcGateway. Moreover it tests that you can only set the fencing token from
* the main thread.
*/
@Test
public void testFencingTokenSetting() throws Exception {
final String value = "foobar";
FencedTestingEndpoint fencedTestingEndpoint = new FencedTestingEndpoint(rpcService, value);
FencedTestingGateway fencedGateway = fencedTestingEndpoint.getSelfGateway(FencedTestingGateway.class);
try {
fencedTestingEndpoint.start();
assertNull(fencedGateway.getFencingToken());
assertNull(fencedTestingEndpoint.getFencingToken());
final UUID newFencingToken = UUID.randomUUID();
boolean failed = false;
try {
fencedTestingEndpoint.setFencingToken(newFencingToken);
failed = true;
} catch (AssertionError ignored) {
// expected to fail
}
assertFalse("Setting fencing token from outside the main thread did not fail as expected.", failed);
assertNull(fencedTestingEndpoint.getFencingToken());
CompletableFuture<Acknowledge> setFencingFuture = fencedTestingEndpoint.setFencingTokenInMainThread(newFencingToken, timeout);
// wait for the completion of the set fencing token operation
setFencingFuture.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
// self gateway should adapt its fencing token
assertEquals(newFencingToken, fencedGateway.getFencingToken());
assertEquals(newFencingToken, fencedTestingEndpoint.getFencingToken());
} finally {
RpcUtils.terminateRpcEndpoint(fencedTestingEndpoint, timeout);
}
}
/**
* Tests that messages with the wrong fencing token are filtered out.
*/
@Test
public void testFencing() throws Exception {
final UUID fencingToken = UUID.randomUUID();
final UUID wrongFencingToken = UUID.randomUUID();
final String value = "barfoo";
FencedTestingEndpoint fencedTestingEndpoint = new FencedTestingEndpoint(rpcService, value, fencingToken);
try {
fencedTestingEndpoint.start();
final FencedTestingGateway properFencedGateway = rpcService.connect(fencedTestingEndpoint.getAddress(), fencingToken, FencedTestingGateway.class)
.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
final FencedTestingGateway wronglyFencedGateway = rpcService.connect(fencedTestingEndpoint.getAddress(), wrongFencingToken, FencedTestingGateway.class)
.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
assertEquals(value, properFencedGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS));
try {
wronglyFencedGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
fail("This should fail since we have the wrong fencing token.");
} catch (ExecutionException e) {
assertTrue(ExceptionUtils.stripExecutionException(e) instanceof FencingTokenException);
}
final UUID newFencingToken = UUID.randomUUID();
CompletableFuture<Acknowledge> newFencingTokenFuture = fencedTestingEndpoint.setFencingTokenInMainThread(newFencingToken, timeout);
// wait for the new fencing token to be set
newFencingTokenFuture.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
// this should no longer work because of the new fencing token
try {
properFencedGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
fail("This should fail since we have the wrong fencing token by now.");
} catch (ExecutionException e) {
assertTrue(ExceptionUtils.stripExecutionException(e) instanceof FencingTokenException);
}
} finally {
RpcUtils.terminateRpcEndpoint(fencedTestingEndpoint, timeout);
}
}
/**
* Tests that the self gateway always uses the current fencing token whereas the remote
* gateway has a fixed fencing token.
*/
@Test
public void testRemoteAndSelfGateways() throws Exception {
final UUID initialFencingToken = UUID.randomUUID();
final UUID newFencingToken = UUID.randomUUID();
final String value = "foobar";
final FencedTestingEndpoint fencedTestingEndpoint = new FencedTestingEndpoint(rpcService, value, initialFencingToken);
try {
fencedTestingEndpoint.start();
FencedTestingGateway selfGateway = fencedTestingEndpoint.getSelfGateway(FencedTestingGateway.class);
FencedTestingGateway remoteGateway = rpcService.connect(fencedTestingEndpoint.getAddress(), initialFencingToken, FencedTestingGateway.class)
.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
assertEquals(initialFencingToken, selfGateway.getFencingToken());
assertEquals(initialFencingToken, remoteGateway.getFencingToken());
assertEquals(value, selfGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS));
assertEquals(value, remoteGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS));
CompletableFuture<Acknowledge> newFencingTokenFuture = fencedTestingEndpoint.setFencingTokenInMainThread(newFencingToken, timeout);
// wait for the new fencing token to be set
newFencingTokenFuture.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
assertEquals(newFencingToken, selfGateway.getFencingToken());
assertNotEquals(newFencingToken, remoteGateway.getFencingToken());
assertEquals(value, selfGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS));
try {
remoteGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
fail("This should have failed because we don't have the right fencing token.");
} catch (ExecutionException e) {
assertTrue(ExceptionUtils.stripExecutionException(e) instanceof FencingTokenException);
}
} finally {
RpcUtils.terminateRpcEndpoint(fencedTestingEndpoint, timeout);
}
}
/**
* Tests that call via the MainThreadExecutor fail after the fencing token changes.
*/
@Test
public void testMainThreadExecutorUnderChangingFencingToken() throws Exception {
final Time shortTimeout = Time.milliseconds(100L);
final UUID initialFencingToken = UUID.randomUUID();
final String value = "foobar";
final FencedTestingEndpoint fencedTestingEndpoint = new FencedTestingEndpoint(rpcService, value, initialFencingToken);
try {
fencedTestingEndpoint.start();
FencedTestingGateway selfGateway = fencedTestingEndpoint.getSelfGateway(FencedTestingGateway.class);
CompletableFuture<Acknowledge> mainThreadExecutorComputation = selfGateway.triggerMainThreadExecutorComputation(timeout);
// we know that subsequent calls on the same gateway are executed sequentially
// therefore, we know that the change fencing token call is executed after the trigger MainThreadExecutor
// computation
final UUID newFencingToken = UUID.randomUUID();
CompletableFuture<Acknowledge> newFencingTokenFuture = fencedTestingEndpoint.setFencingTokenInMainThread(newFencingToken, timeout);
newFencingTokenFuture.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
// trigger the computation
CompletableFuture<Acknowledge> triggerFuture = selfGateway.triggerComputationLatch(timeout);
triggerFuture.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
// wait for the main thread executor computation to fail
try {
mainThreadExecutorComputation.get(shortTimeout.toMilliseconds(), TimeUnit.MILLISECONDS);
fail("The MainThreadExecutor computation should be able to complete because it was filtered out leading to a timeout exception.");
} catch (TimeoutException ignored) {
// as predicted
}
} finally {
RpcUtils.terminateRpcEndpoint(fencedTestingEndpoint, timeout);
}
}
/**
* Tests that all calls from an unfenced remote gateway are ignored and that one cannot obtain
* the fencing token from such a gateway.
*/
@Test
public void testUnfencedRemoteGateway() throws Exception {
final UUID initialFencingToken = UUID.randomUUID();
final String value = "foobar";
final FencedTestingEndpoint fencedTestingEndpoint = new FencedTestingEndpoint(rpcService, value, initialFencingToken);
try {
fencedTestingEndpoint.start();
FencedTestingGateway unfencedGateway = rpcService.connect(fencedTestingEndpoint.getAddress(), FencedTestingGateway.class)
.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
try {
unfencedGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS);
fail("This should have failed because we have an unfenced gateway.");
} catch (ExecutionException e) {
assertTrue(ExceptionUtils.stripExecutionException(e) instanceof RpcException);
}
try {
unfencedGateway.getFencingToken();
fail("We should not be able to call getFencingToken on an unfenced gateway.");
} catch (UnsupportedOperationException ignored) {
// we should not be able to call getFencingToken on an unfenced gateway
}
} finally {
RpcUtils.terminateRpcEndpoint(fencedTestingEndpoint, timeout);
}
}
public interface FencedTestingGateway extends FencedRpcGateway<UUID> {
CompletableFuture<String> foobar(@RpcTimeout Time timeout);
CompletableFuture<Acknowledge> triggerMainThreadExecutorComputation(@RpcTimeout Time timeout);
CompletableFuture<Acknowledge> triggerComputationLatch(@RpcTimeout Time timeout);
}
private static class FencedTestingEndpoint extends FencedRpcEndpoint<UUID> implements FencedTestingGateway {
private final OneShotLatch computationLatch;
private final String value;
protected FencedTestingEndpoint(RpcService rpcService, String value) {
this(rpcService, value, null);
}
protected FencedTestingEndpoint(RpcService rpcService, String value, UUID initialFencingToken) {
super(rpcService, initialFencingToken);
computationLatch = new OneShotLatch();
this.value = value;
}
@Override
public CompletableFuture<String> foobar(Time timeout) {
return CompletableFuture.completedFuture(value);
}
@Override
public CompletableFuture<Acknowledge> triggerMainThreadExecutorComputation(Time timeout) {
return CompletableFuture.supplyAsync(
() -> {
try {
computationLatch.await();
} catch (InterruptedException e) {
throw new CompletionException(new FlinkException("Waiting on latch failed.", e));
}
return value;
},
getRpcService().getExecutor())
.thenApplyAsync(
(String v) -> Acknowledge.get(),
getMainThreadExecutor());
}
@Override
public CompletableFuture<Acknowledge> triggerComputationLatch(Time timeout) {
computationLatch.trigger();
return CompletableFuture.completedFuture(Acknowledge.get());
}
public CompletableFuture<Acknowledge> setFencingTokenInMainThread(UUID fencingToken, Time timeout) {
return callAsyncWithoutFencing(
() -> {
setFencingToken(fencingToken);
return Acknowledge.get();
},
timeout);
}
}
}