-
Notifications
You must be signed in to change notification settings - Fork 146
/
CloudPubSubSinkTask.java
465 lines (436 loc) · 18.5 KB
/
CloudPubSubSinkTask.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Copyright 2016 Google Inc.
//
// Licensed 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 com.google.pubsub.kafka.sink;
import static com.google.pubsub.kafka.common.ConnectorUtils.getSystemExecutor;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.core.SettableApiFuture;
import com.google.api.gax.batching.BatchingSettings;
import com.google.api.gax.core.FixedExecutorProvider;
import com.google.api.gax.batching.FlowControlSettings;
import com.google.api.gax.batching.FlowController;
import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.common.annotations.VisibleForTesting;
import com.google.protobuf.ByteString;
import com.google.pubsub.kafka.common.ConnectorUtils;
import com.google.pubsub.kafka.common.ConnectorCredentialsProvider;
import com.google.pubsub.kafka.sink.CloudPubSubSinkConnector.OrderingKeySource;
import com.google.pubsub.v1.ProjectTopicName;
import com.google.pubsub.v1.PubsubMessage;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.connect.data.Field;
import org.apache.kafka.connect.data.Schema;
import org.apache.kafka.connect.data.Schema.Type;
import org.apache.kafka.connect.data.Struct;
import org.apache.kafka.connect.errors.DataException;
import org.apache.kafka.connect.header.ConnectHeaders;
import org.apache.kafka.connect.header.Header;
import org.apache.kafka.connect.sink.SinkRecord;
import org.apache.kafka.connect.sink.SinkTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.threeten.bp.Duration;
/**
* A {@link SinkTask} used by a {@link CloudPubSubSinkConnector} to write messages to <a
* href="https://cloud.google.com/pubsub">Google Cloud Pub/Sub</a>.
*/
public class CloudPubSubSinkTask extends SinkTask {
private static final Logger log = LoggerFactory.getLogger(CloudPubSubSinkTask.class);
// Maps a topic to another map which contains the outstanding futures per partition
private Map<String, Map<Integer, OutstandingFuturesForPartition>> allOutstandingFutures =
new HashMap<>();
private String cpsProject;
private String cpsTopic;
private String cpsEndpoint;
private String messageBodyName;
private long maxBufferSize;
private long maxBufferBytes;
private long maxOutstandingRequestBytes;
private long maxOutstandingMessages;
private int maxDelayThresholdMs;
private int maxRequestTimeoutMs;
private int maxTotalTimeoutMs;
private int maxShutdownTimeoutMs;
private boolean includeMetadata;
private boolean includeHeaders;
private OrderingKeySource orderingKeySource;
private ConnectorCredentialsProvider gcpCredentialsProvider;
private com.google.cloud.pubsub.v1.Publisher publisher;
/** Holds a list of the publishing futures that have not been processed for a single partition. */
private class OutstandingFuturesForPartition {
public List<ApiFuture<String>> futures = new ArrayList<>();
}
/**
* Holds a list of the unpublished messages for a single partition and the total size in bytes of
* the messages in the list.
*/
private class UnpublishedMessagesForPartition {
public List<PubsubMessage> messages = new ArrayList<>();
public int size = 0;
}
public CloudPubSubSinkTask() {}
@VisibleForTesting
public CloudPubSubSinkTask(Publisher publisher) {
this.publisher = publisher;
}
@Override
public String version() {
return new CloudPubSubSinkConnector().version();
}
@Override
public void start(Map<String, String> props) {
Map<String, Object> validatedProps = new CloudPubSubSinkConnector().config().parse(props);
cpsProject = validatedProps.get(ConnectorUtils.CPS_PROJECT_CONFIG).toString();
cpsTopic = validatedProps.get(ConnectorUtils.CPS_TOPIC_CONFIG).toString();
cpsEndpoint = validatedProps.get(ConnectorUtils.CPS_ENDPOINT).toString();
maxBufferSize = (Integer) validatedProps.get(CloudPubSubSinkConnector.MAX_BUFFER_SIZE_CONFIG);
maxBufferBytes = (Long) validatedProps.get(CloudPubSubSinkConnector.MAX_BUFFER_BYTES_CONFIG);
maxOutstandingRequestBytes =
(Long) validatedProps.get(CloudPubSubSinkConnector.MAX_OUTSTANDING_REQUEST_BYTES);
maxOutstandingMessages =
(Long) validatedProps.get(CloudPubSubSinkConnector.MAX_OUTSTANDING_MESSAGES);
maxDelayThresholdMs =
(Integer) validatedProps.get(CloudPubSubSinkConnector.MAX_DELAY_THRESHOLD_MS);
maxRequestTimeoutMs =
(Integer) validatedProps.get(CloudPubSubSinkConnector.MAX_REQUEST_TIMEOUT_MS);
maxTotalTimeoutMs =
(Integer) validatedProps.get(CloudPubSubSinkConnector.MAX_TOTAL_TIMEOUT_MS);
maxShutdownTimeoutMs =
(Integer) validatedProps.get(CloudPubSubSinkConnector.MAX_SHUTDOWN_TIMEOUT_MS);
messageBodyName = (String) validatedProps.get(CloudPubSubSinkConnector.CPS_MESSAGE_BODY_NAME);
includeMetadata = (Boolean) validatedProps.get(CloudPubSubSinkConnector.PUBLISH_KAFKA_METADATA);
includeHeaders = (Boolean) validatedProps.get(CloudPubSubSinkConnector.PUBLISH_KAFKA_HEADERS);
orderingKeySource =
OrderingKeySource.getEnum(
(String) validatedProps.get(CloudPubSubSinkConnector.ORDERING_KEY_SOURCE));
gcpCredentialsProvider = new ConnectorCredentialsProvider();
String credentialsPath = (String) validatedProps.get(ConnectorUtils.GCP_CREDENTIALS_FILE_PATH_CONFIG);
String credentialsJson = (String) validatedProps.get(ConnectorUtils.GCP_CREDENTIALS_JSON_CONFIG);
if (credentialsPath != null) {
try {
gcpCredentialsProvider.loadFromFile(credentialsPath);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else if (credentialsJson != null) {
try {
gcpCredentialsProvider.loadJson(credentialsJson);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (publisher == null) {
// Only do this if we did not use the constructor.
createPublisher();
}
log.info("Start CloudPubSubSinkTask");
}
@Override
public void put(Collection<SinkRecord> sinkRecords) {
log.debug("Received " + sinkRecords.size() + " messages to send to CPS.");
for (SinkRecord record : sinkRecords) {
log.trace("Received record: " + record.toString());
Map<String, String> attributes = new HashMap<>();
ByteString value = handleValue(record.valueSchema(), record.value(), attributes);
String key = null;
String partition = record.kafkaPartition().toString();
if (record.key() != null) {
key = record.key().toString();
attributes.put(ConnectorUtils.CPS_MESSAGE_KEY_ATTRIBUTE, key);
}
if (includeMetadata) {
attributes.put(ConnectorUtils.KAFKA_TOPIC_ATTRIBUTE, record.topic());
attributes.put(
ConnectorUtils.KAFKA_PARTITION_ATTRIBUTE, partition);
attributes.put(ConnectorUtils.KAFKA_OFFSET_ATTRIBUTE, Long.toString(record.kafkaOffset()));
if (record.timestamp() != null) {
attributes.put(ConnectorUtils.KAFKA_TIMESTAMP_ATTRIBUTE, record.timestamp().toString());
}
}
if (includeHeaders) {
for (Header header : getRecordHeaders(record)) {
attributes.put(header.key(), header.value().toString());
}
}
if (attributes.size() == 0 && value == null) {
log.warn("Message received with no value and no attributes. Not publishing message");
SettableApiFuture<String> nullMessageFuture = SettableApiFuture.<String>create();
nullMessageFuture.set("No message");
addPendingMessageFuture(record.topic(), record.kafkaPartition(), nullMessageFuture);
continue;
}
PubsubMessage.Builder builder = PubsubMessage.newBuilder();
builder.putAllAttributes(attributes);
if (value != null) {
builder.setData(value);
}
if (orderingKeySource == OrderingKeySource.KEY && key != null && !key.isEmpty()) {
builder.setOrderingKey(key);
} else if (orderingKeySource == OrderingKeySource.PARTITION) {
builder.setOrderingKey(partition);
}
PubsubMessage message = builder.build();
publishMessage(record.topic(), record.kafkaPartition(), message);
}
}
private Iterable<? extends Header> getRecordHeaders(SinkRecord record) {
ConnectHeaders headers = new ConnectHeaders();
if(record.headers() != null) {
int headerCount = 0;
for (Header header : record.headers()) {
if (header.key().getBytes().length < 257 &&
String.valueOf(header.value()).getBytes().length < 1025) {
headers.add(header);
headerCount++;
}
if (headerCount > 100) {
break;
}
}
}
return headers;
}
private ByteString handleValue(Schema schema, Object value, Map<String, String> attributes) {
if (value == null) {
return null;
}
if (schema == null) {
String str = value.toString();
return ByteString.copyFromUtf8(str);
}
Schema.Type t = schema.type();
switch (t) {
case INT8:
byte b = (Byte) value;
byte[] arr = {b};
return ByteString.copyFrom(arr);
case INT16:
ByteBuffer shortBuf = ByteBuffer.allocate(2);
shortBuf.putShort((Short) value);
return ByteString.copyFrom(shortBuf);
case INT32:
ByteBuffer intBuf = ByteBuffer.allocate(4);
intBuf.putInt((Integer) value);
return ByteString.copyFrom(intBuf);
case INT64:
ByteBuffer longBuf = ByteBuffer.allocate(8);
longBuf.putLong((Long) value);
return ByteString.copyFrom(longBuf);
case FLOAT32:
ByteBuffer floatBuf = ByteBuffer.allocate(4);
floatBuf.putFloat((Float) value);
return ByteString.copyFrom(floatBuf);
case FLOAT64:
ByteBuffer doubleBuf = ByteBuffer.allocate(8);
doubleBuf.putDouble((Double) value);
return ByteString.copyFrom(doubleBuf);
case BOOLEAN:
byte bool = (byte) ((Boolean) value ? 1 : 0);
byte[] boolArr = {bool};
return ByteString.copyFrom(boolArr);
case STRING:
String str = (String) value;
return ByteString.copyFromUtf8(str);
case BYTES:
if (value instanceof ByteString) {
return (ByteString) value;
} else if (value instanceof byte[]) {
return ByteString.copyFrom((byte[]) value);
} else if (value instanceof ByteBuffer) {
return ByteString.copyFrom((ByteBuffer) value);
} else {
throw new DataException("Unexpected value class with BYTES schema type.");
}
case STRUCT:
Struct struct = (Struct) value;
ByteString msgBody = null;
for (Field f : schema.fields()) {
Schema.Type fieldType = f.schema().type();
if (fieldType == Type.MAP || fieldType == Type.STRUCT) {
throw new DataException("Struct type does not support nested Map or Struct types, " +
"present in field " + f.name());
}
Object val = struct.get(f);
if (val == null) {
if (!f.schema().isOptional()) {
throw new DataException("Struct message missing required field " + f.name());
} else {
continue;
}
}
if (f.name().equals(messageBodyName)) {
Schema bodySchema = f.schema();
msgBody = handleValue(bodySchema, val, null);
} else {
attributes.put(f.name(), val.toString());
}
}
if (msgBody != null) {
return msgBody;
} else {
return ByteString.EMPTY;
}
case MAP:
Map<Object, Object> map = (Map<Object, Object>) value;
Set<Object> keys = map.keySet();
ByteString mapBody = null;
for (Object key : keys) {
if (key.equals(messageBodyName)) {
mapBody = ByteString.copyFromUtf8(map.get(key).toString());
} else {
attributes.put(key.toString(), map.get(key).toString());
}
}
if (mapBody != null) {
return mapBody;
} else {
return ByteString.EMPTY;
}
case ARRAY:
Schema.Type arrType = schema.valueSchema().type();
if (arrType == Type.MAP || arrType == Type.STRUCT) {
throw new DataException("Array type does not support Map or Struct types.");
}
ByteString out = ByteString.EMPTY;
Object[] objArr = (Object[]) value;
for (Object o : objArr) {
out = out.concat(handleValue(schema.valueSchema(), o, null));
}
return out;
}
return ByteString.EMPTY;
}
@Override
public void flush(Map<TopicPartition, OffsetAndMetadata> partitionOffsets) {
log.debug("Flushing...");
// Process results of all the outstanding futures specified by each TopicPartition.
for (Map.Entry<TopicPartition, OffsetAndMetadata> partitionOffset :
partitionOffsets.entrySet()) {
log.trace("Received flush for partition " + partitionOffset.getKey().toString());
Map<Integer, OutstandingFuturesForPartition> outstandingFuturesForTopic =
allOutstandingFutures.get(partitionOffset.getKey().topic());
if (outstandingFuturesForTopic == null) {
continue;
}
OutstandingFuturesForPartition outstandingFutures =
outstandingFuturesForTopic.get(partitionOffset.getKey().partition());
if (outstandingFutures == null) {
continue;
}
try {
ApiFutures.allAsList(outstandingFutures.futures).get();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
outstandingFutures.futures.clear();
}
}
allOutstandingFutures.clear();
}
/** Publish all the messages in a partition and store the Future's for each publish request. */
private void publishMessage(String topic, Integer partition, PubsubMessage message) {
addPendingMessageFuture(topic, partition, publisher.publish(message));
}
private void addPendingMessageFuture(String topic, Integer partition, ApiFuture<String> future) {
// Get a map containing all futures per partition for the passed in topic.
Map<Integer, OutstandingFuturesForPartition> outstandingFuturesForTopic =
allOutstandingFutures.get(topic);
if (outstandingFuturesForTopic == null) {
outstandingFuturesForTopic = new HashMap<>();
allOutstandingFutures.put(topic, outstandingFuturesForTopic);
}
// Get the object containing the outstanding futures for this topic and partition..
OutstandingFuturesForPartition outstandingFutures = outstandingFuturesForTopic.get(partition);
if (outstandingFutures == null) {
outstandingFutures = new OutstandingFuturesForPartition();
outstandingFuturesForTopic.put(partition, outstandingFutures);
}
outstandingFutures.futures.add(future);
}
private void createPublisher() {
ProjectTopicName fullTopic = ProjectTopicName.of(cpsProject, cpsTopic);
BatchingSettings.Builder batchingSettings = BatchingSettings.newBuilder()
.setDelayThreshold(Duration.ofMillis(maxDelayThresholdMs))
.setElementCountThreshold(maxBufferSize)
.setRequestByteThreshold(maxBufferBytes);
if (useFlowControl()) {
batchingSettings.setFlowControlSettings(FlowControlSettings.newBuilder()
.setMaxOutstandingRequestBytes(maxOutstandingRequestBytes)
.setMaxOutstandingElementCount(maxOutstandingMessages)
.setLimitExceededBehavior(FlowController.LimitExceededBehavior.Block)
.build());
}
com.google.cloud.pubsub.v1.Publisher.Builder builder =
com.google.cloud.pubsub.v1.Publisher.newBuilder(fullTopic)
.setCredentialsProvider(gcpCredentialsProvider)
.setBatchingSettings(batchingSettings.build())
.setRetrySettings(
RetrySettings.newBuilder()
// All values that are not configurable come from the defaults for the publisher
// client library.
.setTotalTimeout(Duration.ofMillis(maxTotalTimeoutMs))
.setMaxRpcTimeout(Duration.ofMillis(maxRequestTimeoutMs))
.setInitialRetryDelay(Duration.ofMillis(5))
.setRetryDelayMultiplier(2)
.setMaxRetryDelay(Duration.ofMillis(Long.MAX_VALUE))
.setInitialRpcTimeout(Duration.ofSeconds(10))
.setRpcTimeoutMultiplier(2)
.build())
.setExecutorProvider(FixedExecutorProvider.create(getSystemExecutor()))
.setEndpoint(cpsEndpoint);
if (orderingKeySource != OrderingKeySource.NONE) {
builder.setEnableMessageOrdering(true);
}
try {
publisher = builder.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private boolean useFlowControl() {
// only enable flow control if at least one flow control config has been set
return maxOutstandingRequestBytes != CloudPubSubSinkConnector.DEFAULT_MAX_OUTSTANDING_REQUEST_BYTES
|| maxOutstandingRequestBytes != CloudPubSubSinkConnector.DEFAULT_MAX_OUTSTANDING_MESSAGES;
}
@Override
public void stop() {
log.info("Stopping CloudPubSubSinkTask");
if (publisher != null) {
log.info("Shutting down PubSub publisher");
try {
publisher.shutdown();
boolean terminated = publisher.awaitTermination(maxShutdownTimeoutMs, TimeUnit.MILLISECONDS);
if (!terminated) {
log.warn(String.format("PubSub publisher did not terminate cleanly in %d ms", maxShutdownTimeoutMs));
}
} catch (Exception e) {
// There is not much we can do here besides logging it as an error
log.error("An exception occurred while shutting down PubSub publisher", e);
}
}
}
}