forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 7
/
TestSink.java
401 lines (317 loc) · 13.6 KB
/
TestSink.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* 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.streaming.runtime.operators.sink;
import org.apache.flink.api.common.eventtime.Watermark;
import org.apache.flink.api.connector.sink.Committer;
import org.apache.flink.api.connector.sink.GlobalCommitter;
import org.apache.flink.api.connector.sink.Sink;
import org.apache.flink.api.connector.sink.SinkWriter;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.core.io.SimpleVersionedSerializer;
import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.SimpleVersionedStringSerializer;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import static org.apache.flink.util.Preconditions.checkNotNull;
import static org.junit.Assert.assertNotNull;
/** A {@link Sink TestSink} for all the sink related tests. */
public class TestSink implements Sink<Integer, String, String, String> {
private final DefaultSinkWriter writer;
@Nullable private final SimpleVersionedSerializer<String> writerStateSerializer;
@Nullable private final Committer<String> committer;
@Nullable private final SimpleVersionedSerializer<String> committableSerializer;
@Nullable private final GlobalCommitter<String, String> globalCommitter;
@Nullable private final SimpleVersionedSerializer<String> globalCommittableSerializer;
private TestSink(
DefaultSinkWriter writer,
@Nullable SimpleVersionedSerializer<String> writerStateSerializer,
@Nullable Committer<String> committer,
@Nullable SimpleVersionedSerializer<String> committableSerializer,
@Nullable GlobalCommitter<String, String> globalCommitter,
@Nullable SimpleVersionedSerializer<String> globalCommittableSerializer) {
this.writer = writer;
this.writerStateSerializer = writerStateSerializer;
this.committer = committer;
this.committableSerializer = committableSerializer;
this.globalCommitter = globalCommitter;
this.globalCommittableSerializer = globalCommittableSerializer;
}
@Override
public SinkWriter<Integer, String, String> createWriter(
InitContext context, List<String> states) {
writer.restoredFrom(states);
writer.setProcessingTimerService(context.getProcessingTimeService());
return writer;
}
@Override
public Optional<Committer<String>> createCommitter() {
return Optional.ofNullable(committer);
}
@Override
public Optional<GlobalCommitter<String, String>> createGlobalCommitter() {
return Optional.ofNullable(globalCommitter);
}
@Override
public Optional<SimpleVersionedSerializer<String>> getCommittableSerializer() {
return Optional.ofNullable(committableSerializer);
}
@Override
public Optional<SimpleVersionedSerializer<String>> getGlobalCommittableSerializer() {
return Optional.ofNullable(globalCommittableSerializer);
}
@Override
public Optional<SimpleVersionedSerializer<String>> getWriterStateSerializer() {
return Optional.ofNullable(writerStateSerializer);
}
public static Builder newBuilder() {
return new Builder();
}
/** A builder class for {@link TestSink}. */
public static class Builder {
private DefaultSinkWriter writer = new DefaultSinkWriter();
private SimpleVersionedSerializer<String> writerStateSerializer;
private Committer<String> committer;
private SimpleVersionedSerializer<String> committableSerializer;
private GlobalCommitter<String, String> globalCommitter;
private SimpleVersionedSerializer<String> globalCommittableSerializer;
public Builder setWriter(DefaultSinkWriter writer) {
this.writer = checkNotNull(writer);
return this;
}
public Builder withWriterState() {
this.writerStateSerializer = StringCommittableSerializer.INSTANCE;
return this;
}
public Builder setCommitter(Committer<String> committer) {
this.committer = committer;
return this;
}
public Builder setCommittableSerializer(
SimpleVersionedSerializer<String> committableSerializer) {
this.committableSerializer = committableSerializer;
return this;
}
public Builder setDefaultCommitter() {
this.committer = new DefaultCommitter();
this.committableSerializer = StringCommittableSerializer.INSTANCE;
return this;
}
public Builder setDefaultCommitter(Supplier<Queue<String>> queueSupplier) {
this.committer = new DefaultCommitter(queueSupplier);
this.committableSerializer = StringCommittableSerializer.INSTANCE;
return this;
}
public Builder setGlobalCommitter(GlobalCommitter<String, String> globalCommitter) {
this.globalCommitter = globalCommitter;
return this;
}
public Builder setGlobalCommittableSerializer(
SimpleVersionedSerializer<String> globalCommittableSerializer) {
this.globalCommittableSerializer = globalCommittableSerializer;
return this;
}
public Builder setDefaultGlobalCommitter() {
this.globalCommitter = new DefaultGlobalCommitter("");
this.globalCommittableSerializer = StringCommittableSerializer.INSTANCE;
return this;
}
public Builder setGlobalCommitter(Supplier<Queue<String>> queueSupplier) {
this.globalCommitter = new DefaultGlobalCommitter(queueSupplier);
this.globalCommittableSerializer = StringCommittableSerializer.INSTANCE;
return this;
}
public TestSink build() {
return new TestSink(
writer,
writerStateSerializer,
committer,
committableSerializer,
globalCommitter,
globalCommittableSerializer);
}
}
// -------------------------------------- Sink Writer ------------------------------------------
/** Base class for out testing {@link SinkWriter Writers}. */
static class DefaultSinkWriter implements SinkWriter<Integer, String, String>, Serializable {
protected List<String> elements;
protected List<Watermark> watermarks;
protected ProcessingTimeService processingTimerService;
DefaultSinkWriter() {
this.elements = new ArrayList<>();
this.watermarks = new ArrayList<>();
}
@Override
public void write(Integer element, Context context) {
elements.add(
Tuple3.of(element, context.timestamp(), context.currentWatermark()).toString());
}
@Override
public void writeWatermark(Watermark watermark) throws IOException {
watermarks.add(watermark);
}
@Override
public List<String> prepareCommit(boolean flush) {
List<String> result = elements;
elements = new ArrayList<>();
return result;
}
@Override
public List<String> snapshotState() {
return Collections.emptyList();
}
@Override
public void close() throws Exception {}
void restoredFrom(List<String> states) {}
void setProcessingTimerService(ProcessingTimeService processingTimerService) {
this.processingTimerService = processingTimerService;
}
}
// -------------------------------------- Sink Committer ---------------------------------------
/** Base class for testing {@link Committer} and {@link GlobalCommitter}. */
static class DefaultCommitter implements Committer<String>, Serializable {
@Nullable private Queue<String> committedData;
private boolean isClosed;
@Nullable private final Supplier<Queue<String>> queueSupplier;
public DefaultCommitter() {
this.committedData = new ConcurrentLinkedQueue<>();
this.isClosed = false;
this.queueSupplier = null;
}
public DefaultCommitter(@Nullable Supplier<Queue<String>> queueSupplier) {
this.queueSupplier = queueSupplier;
this.isClosed = false;
this.committedData = null;
}
public List<String> getCommittedData() {
if (committedData != null) {
return new ArrayList<>(committedData);
} else {
return Collections.emptyList();
}
}
@Override
public List<String> commit(List<String> committables) {
if (committedData == null) {
assertNotNull(queueSupplier);
committedData = queueSupplier.get();
}
committedData.addAll(committables);
return Collections.emptyList();
}
public void close() throws Exception {
isClosed = true;
}
public boolean isClosed() {
return isClosed;
}
}
/** A {@link Committer} that always re-commits the committables data it received. */
static class AlwaysRetryCommitter extends DefaultCommitter implements Committer<String> {
@Override
public List<String> commit(List<String> committables) {
return committables;
}
}
// ------------------------------------- Sink Global Committer ---------------------------------
/** A {@link GlobalCommitter} that always commits global committables successfully. */
static class DefaultGlobalCommitter extends DefaultCommitter
implements GlobalCommitter<String, String> {
static final Function<List<String>, String> COMBINER =
strings -> {
// we sort here because we want to have a deterministic result during the unit
// test
Collections.sort(strings);
return String.join("+", strings);
};
private final String committedSuccessData;
DefaultGlobalCommitter() {
this("");
}
DefaultGlobalCommitter(String committedSuccessData) {
this.committedSuccessData = committedSuccessData;
}
DefaultGlobalCommitter(Supplier<Queue<String>> queueSupplier) {
super(queueSupplier);
committedSuccessData = "";
}
@Override
public List<String> filterRecoveredCommittables(List<String> globalCommittables) {
if (committedSuccessData == null) {
return globalCommittables;
}
return globalCommittables.stream()
.filter(s -> !s.equals(committedSuccessData))
.collect(Collectors.toList());
}
@Override
public String combine(List<String> committables) {
return COMBINER.apply(committables);
}
@Override
public void endOfInput() {
commit(Collections.singletonList("end of input"));
}
}
/** A {@link GlobalCommitter} that always re-commits global committables it received. */
static class AlwaysRetryGlobalCommitter extends DefaultGlobalCommitter {
@Override
public List<String> filterRecoveredCommittables(List<String> globalCommittables) {
return Collections.emptyList();
}
@Override
public String combine(List<String> committables) {
return String.join("|", committables);
}
@Override
public void endOfInput() {}
@Override
public List<String> commit(List<String> committables) {
return committables;
}
}
/**
* We introduce this {@link StringCommittableSerializer} is because that all the fields of
* {@link TestSink} should be serializable.
*/
public static class StringCommittableSerializer
implements SimpleVersionedSerializer<String>, Serializable {
public static final StringCommittableSerializer INSTANCE =
new StringCommittableSerializer();
@Override
public int getVersion() {
return SimpleVersionedStringSerializer.INSTANCE.getVersion();
}
@Override
public byte[] serialize(String obj) throws IOException {
return SimpleVersionedStringSerializer.INSTANCE.serialize(obj);
}
@Override
public String deserialize(int version, byte[] serialized) throws IOException {
return SimpleVersionedStringSerializer.INSTANCE.deserialize(version, serialized);
}
}
}