forked from apache/camel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kinesis2Consumer.java
364 lines (312 loc) · 14.2 KB
/
Kinesis2Consumer.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
/*
* 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.camel.component.aws2.kinesis;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.camel.AsyncCallback;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.component.aws2.kinesis.consumer.KinesisResumeAdapter;
import org.apache.camel.resume.ResumeAware;
import org.apache.camel.resume.ResumeStrategy;
import org.apache.camel.support.ScheduledBatchPollingConsumer;
import org.apache.camel.util.CastUtils;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.services.kinesis.model.DescribeStreamRequest;
import software.amazon.awssdk.services.kinesis.model.DescribeStreamResponse;
import software.amazon.awssdk.services.kinesis.model.GetRecordsRequest;
import software.amazon.awssdk.services.kinesis.model.GetRecordsResponse;
import software.amazon.awssdk.services.kinesis.model.GetShardIteratorRequest;
import software.amazon.awssdk.services.kinesis.model.GetShardIteratorResponse;
import software.amazon.awssdk.services.kinesis.model.ListShardsRequest;
import software.amazon.awssdk.services.kinesis.model.Record;
import software.amazon.awssdk.services.kinesis.model.Shard;
import software.amazon.awssdk.services.kinesis.model.ShardIteratorType;
public class Kinesis2Consumer extends ScheduledBatchPollingConsumer implements ResumeAware<ResumeStrategy> {
private static final Logger LOG = LoggerFactory.getLogger(Kinesis2Consumer.class);
private KinesisConnection connection;
private ResumeStrategy resumeStrategy;
private Map<String, String> currentShardIterators = new java.util.HashMap<>();
public Kinesis2Consumer(Kinesis2Endpoint endpoint,
Processor processor) {
super(endpoint, processor);
}
public KinesisConnection getConnection() {
return connection;
}
public void setConnection(KinesisConnection connection) {
this.connection = connection;
}
@Override
protected int poll() throws Exception {
var processedExchangeCount = new AtomicInteger(0);
if (!getEndpoint().getConfiguration().getShardId().isEmpty()) {
var request = DescribeStreamRequest
.builder()
.streamName(getEndpoint().getConfiguration().getStreamName())
.build();
DescribeStreamResponse response;
if (getEndpoint().getConfiguration().isAsyncClient()) {
try {
response = connection
.getAsyncClient(getEndpoint())
.describeStream(request)
.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
} else {
response = connection
.getClient(getEndpoint())
.describeStream(request);
}
var shard = response
.streamDescription()
.shards()
.stream()
.filter(shardItem -> shardItem
.shardId()
.equalsIgnoreCase(getEndpoint()
.getConfiguration()
.getShardId()))
.findFirst()
.orElseThrow(() -> new IllegalStateException("The shard can't be found"));
fetchAndPrepareRecordsForCamel(shard, connection, processedExchangeCount);
} else {
getShardList(connection)
.parallelStream()
.forEach(shard -> {
fetchAndPrepareRecordsForCamel(shard, connection, processedExchangeCount);
});
}
// okay we have some response from aws so lets mark the consumer as ready
forceConsumerAsReady();
return processedExchangeCount.get();
}
private void fetchAndPrepareRecordsForCamel(
final Shard shard,
final KinesisConnection kinesisConnection,
AtomicInteger processedExchangeCount) {
String shardIterator;
try {
shardIterator = getShardIterator(shard, kinesisConnection);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
if (shardIterator == null) {
// Unable to get an interator so shard must be closed
processedExchangeCount.set(0);
return;
}
GetRecordsRequest req = GetRecordsRequest
.builder()
.shardIterator(shardIterator)
.limit(getEndpoint()
.getConfiguration()
.getMaxResultsPerRequest())
.build();
GetRecordsResponse result;
if (getEndpoint().getConfiguration().isAsyncClient()) {
try {
result = kinesisConnection
.getAsyncClient(getEndpoint())
.getRecords(req)
.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
} else {
result = kinesisConnection
.getClient(getEndpoint())
.getRecords(req);
}
try {
Queue<Exchange> exchanges = createExchanges(shard, result.records());
processedExchangeCount.getAndSet(processBatch(CastUtils.cast(exchanges)));
} catch (Exception e) {
throw new RuntimeException(e);
}
// May cache the last successful sequence number, and pass it to the
// getRecords request. That way, on the next poll, we start from where
// we left off, however, I don't know what happens to subsequent
// exchanges when an earlier exchange fails.
updateShardIterator(shard, result.nextShardIterator());
}
private void updateShardIterator(Shard shard, String nextShardIterator) {
currentShardIterators.put(shard.shardId(), nextShardIterator);
}
@Override
public int processBatch(Queue<Object> exchanges) throws Exception {
int processedExchanges = 0;
while (!exchanges.isEmpty()) {
final Exchange exchange = ObjectHelper.cast(Exchange.class, exchanges.poll());
// use default consumer callback
AsyncCallback cb = defaultConsumerCallback(exchange, true);
getAsyncProcessor().process(exchange, cb);
processedExchanges++;
}
return processedExchanges;
}
@Override
public Kinesis2Endpoint getEndpoint() {
return (Kinesis2Endpoint) super.getEndpoint();
}
private String getShardIterator(
final Shard shard,
final KinesisConnection kinesisConnection)
throws ExecutionException, InterruptedException {
// either return a cached one or get a new one via a GetShardIterator
// request.
var shardId = shard.shardId();
if (currentShardIterators.get(shardId) == null) {
if (currentShardIterators.containsKey(shardId)) {
// There was previously a shardIterator but shard is now closed
handleClosedShard(shardId);
}
GetShardIteratorRequest.Builder request = GetShardIteratorRequest.builder()
.streamName(getEndpoint().getConfiguration().getStreamName()).shardId(shardId)
.shardIteratorType(getEndpoint().getConfiguration().getIteratorType());
if (hasSequenceNumber()) {
request.startingSequenceNumber(getEndpoint().getConfiguration().getSequenceNumber());
}
resume(shardId, request);
GetShardIteratorResponse result;
if (getEndpoint().getConfiguration().isAsyncClient()) {
try {
result = kinesisConnection
.getAsyncClient(getEndpoint())
.getShardIterator(request.build())
.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
} else {
result = kinesisConnection
.getClient(getEndpoint())
.getShardIterator(request.build());
}
currentShardIterators.put(shardId, result.shardIterator());
LOG.debug("Obtained new ShardIterator {} for shard {} on stream {}", result.shardIterator(), shardId,
getEndpoint().getConfiguration().getStreamName());
}
return currentShardIterators.get(shardId);
}
private void handleClosedShard(String shardId) {
switch (getEndpoint().getConfiguration().getShardClosed()) {
case ignore:
LOG.warn("The shard with id={} on stream {} reached CLOSE status",
shardId, getEndpoint().getConfiguration().getStreamName());
break;
case silent:
break;
case fail:
LOG.info("The shard with id={} on stream {} reached CLOSE status",
shardId, getEndpoint().getConfiguration().getStreamName());
throw new IllegalStateException(
new ReachedClosedStatusException(
getEndpoint().getConfiguration().getStreamName(), shardId));
default:
throw new IllegalArgumentException("Unsupported shard closed strategy");
}
}
private void resume(String shardId, GetShardIteratorRequest.Builder req) {
if (resumeStrategy == null) {
return;
}
KinesisResumeAdapter adapter = resumeStrategy.getAdapter(KinesisResumeAdapter.class);
if (adapter == null) {
LOG.warn("There is a resume strategy setup, but no adapter configured or the type is incorrect");
return;
}
adapter.configureGetShardIteratorRequest(req, getEndpoint().getConfiguration().getStreamName(), shardId);
}
private Queue<Exchange> createExchanges(Shard shard, List<Record> records) {
Queue<Exchange> exchanges = new ArrayDeque<>();
for (Record dataRecord : records) {
exchanges.add(createExchange(shard, dataRecord));
}
return exchanges;
}
protected Exchange createExchange(Shard shard, Record dataRecord) {
LOG.debug("Received Kinesis record with partition_key={}", dataRecord.partitionKey());
Exchange exchange = createExchange(true);
exchange.getIn().setBody(dataRecord.data().asInputStream());
exchange.getIn().setHeader(Kinesis2Constants.APPROX_ARRIVAL_TIME, dataRecord.approximateArrivalTimestamp());
exchange.getIn().setHeader(Kinesis2Constants.PARTITION_KEY, dataRecord.partitionKey());
exchange.getIn().setHeader(Kinesis2Constants.SEQUENCE_NUMBER, dataRecord.sequenceNumber());
exchange.getIn().setHeader(Kinesis2Constants.SHARD_ID, shard.shardId());
if (dataRecord.approximateArrivalTimestamp() != null) {
long ts = dataRecord.approximateArrivalTimestamp().getEpochSecond() * 1000;
exchange.getIn().setHeader(Kinesis2Constants.MESSAGE_TIMESTAMP, ts);
}
return exchange;
}
@Override
public void setResumeStrategy(ResumeStrategy resumeStrategy) {
this.resumeStrategy = resumeStrategy;
}
@Override
public ResumeStrategy getResumeStrategy() {
return resumeStrategy;
}
private boolean hasSequenceNumber() {
return !getEndpoint().getConfiguration().getSequenceNumber().isEmpty()
&& (getEndpoint().getConfiguration().getIteratorType().equals(ShardIteratorType.AFTER_SEQUENCE_NUMBER)
|| getEndpoint().getConfiguration().getIteratorType().equals(ShardIteratorType.AT_SEQUENCE_NUMBER));
}
@Override
protected void doStart() throws Exception {
super.doStart();
ObjectHelper.notNull(connection, "connection", this);
if (resumeStrategy != null) {
resumeStrategy.loadCache();
}
}
protected Kinesis2Configuration getConfiguration() {
return getEndpoint().getConfiguration();
}
private List<Shard> getShardList(final KinesisConnection kinesisConnection) {
var request = ListShardsRequest
.builder()
.streamName(getEndpoint().getConfiguration().getStreamName())
.build();
List<Shard> shardList;
if (getEndpoint().getConfiguration().isAsyncClient()) {
try {
shardList = kinesisConnection
.getAsyncClient(getEndpoint())
.listShards(request)
.get()
.shards();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
} else {
shardList = kinesisConnection
.getClient(getEndpoint())
.listShards(request)
.shards();
}
return shardList;
}
}