-
Notifications
You must be signed in to change notification settings - Fork 0
/
KinRecordProcess.java
348 lines (309 loc) · 14.3 KB
/
KinRecordProcess.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
package com.poly.consumer.kcl;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.services.kinesis.clientlibrary.exceptions.InvalidStateException;
import com.amazonaws.services.kinesis.clientlibrary.exceptions.ShutdownException;
import com.amazonaws.services.kinesis.clientlibrary.exceptions.ThrottlingException;
import com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessor;
import com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessorCheckpointer;
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.ShutdownReason;
import com.amazonaws.services.kinesis.model.Record;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.poly.consumer.utils.CompressWrite;
import com.poly.consumer.utils.ConfigProps;
import com.poly.consumer.utils.JSONTransformer;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import org.apache.commons.lang3.time.StopWatch;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class KinRecordProcess implements IRecordProcessor {
private static final long BACKOFF_TIME_IN_MILLIS = 1000L;
private static final int NUMBER_OF_RETRIES = 20;
// Checkpoint about once a minute
private static final long CHECKPOINT_INTERVAL_MILLIS = 1000L;
private static ConfigProps config = new ConfigProps();
// Backoff and retry settings
private final Log LOG = LogFactory.getLog(KinRecordProcess.class);
private final CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
private final LinkedBlockingQueue<Record> recordQueue = new LinkedBlockingQueue<Record>();
private AmazonS3 cli = AmazonS3ClientBuilder.standard().withCredentials(new DefaultAWSCredentialsProviderChain()).build();
private ObjectMetadata objectMetadata = new ObjectMetadata();
private ExecutorService processQueue = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
private SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
private SimpleDateFormat ftkey = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
private SimpleDateFormat dtc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private StringBuffer batchstring = new StringBuffer(50000000);
private String kinesisShardId;
private long nextCheckpointTimeInMillis;
/* need to synchronize HashSet to make thread safe in multi-threaded environment*/
private Set<String> batcharray;
/* need to synchronize HashSet to make thread safe in multi-threaded environment*/
private QueueConsumer queueConsumer;
private String s3key;
private StopWatch sw = new StopWatch();
public KinRecordProcess(Set<String> batcharray) {
this.batcharray = Collections.synchronizedSet(batcharray);
}
/*method validates if string is a JSON Object*/
private static boolean isValidJSONObject(Object j) {
try {
new JSONObject(j.toString());
} catch (JSONException jsonEx) {
return false;
}
return true;
}
/*method validates if string is a JSON Array*/
private static boolean isValidJSONArray(Object j) {
try {
new JSONArray(new JSONParser().parse(j.toString()).toString());
} catch (ParseException jsonEx) {
return false;
} catch (JSONException jsonEx) {
return false;
}
try {
new JSONArray(j.toString());
} catch (JSONException jsonEx) {
return false;
}
return true;
}
@Override
public void initialize(String shardId) {
this.kinesisShardId = shardId;
queueConsumer = new QueueConsumer();
processQueue.submit(queueConsumer);
processQueue.shutdown();
//shutdownAndAwaitTermination(processQueue);
}
@Override
public void processRecords(List<Record> records, IRecordProcessorCheckpointer checkpointer) {
//queueConsumer.setCheckpointer(checkpointer);
try {
// adds to the queue
records.stream().distinct().forEach(x -> recordQueue.add(x));
// Checkpoint once every checkpoint interval.
if (System.currentTimeMillis() > nextCheckpointTimeInMillis) {
queueConsumer.setCheckpointer(checkpointer);
checkpoint(checkpointer);
nextCheckpointTimeInMillis = System.currentTimeMillis() + CHECKPOINT_INTERVAL_MILLIS;
}
} catch (Exception e) {
LOG.info(e.getStackTrace() + "->" + e.getMessage());
}
}
private void processRecordsWithRetries(List<Record> records) {
records.stream().forEach(r -> {
boolean processedSuccessfully = false;
for (int i = 0; i < NUMBER_OF_RETRIES; i++) {
try {
processSingleRecord(r);
processedSuccessfully = true;
break;
} catch (Throwable t) {
LOG.error(t.getMessage());
LOG.error(t.getCause());
}
// backoff if we encounter an exception.
try {
Thread.sleep(BACKOFF_TIME_IN_MILLIS);
} catch (InterruptedException e) {
//System.out.println("Interrupted sleep: " + e);
LOG.error("Interrupted sleep: " + e);
}
}
if (!processedSuccessfully) {
LOG.info("Couldn't process record " + r + ". Skipping the record.");
}
});
}
private void processSingleRecord(Record record) throws Exception {
String raw = new String();
String payload;
/*ingest kineses stream to s3*/
try {
// For this app, we interpret the payload as UTF-8 chars.
CharBuffer cbuf = decoder.decode(record.getData());
raw = cbuf.toString().trim();
objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
if ((isValidJSONArray(raw) == true) || (isValidJSONObject(raw) == true)) {
sw.start();
LinkedHashMap<String, String> map = new JSONTransformer(raw).transform();
payload = map.values().stream().map(Object::toString).collect(Collectors.joining(","));
batcharray.add(payload);
sw.stop();
LOG.info("Runtime Completion " + sw.getTime(TimeUnit.MICROSECONDS) + "μs" + ": " + payload.split(",")[0].replace("\"", "") + ":" + payload.split(",")[1].replace("\"", "") + ":" + payload.split(",")[8].replace("\"", ""));
sw.reset();
}
} /*Catch Errors Go Into s3*/ catch (NumberFormatException e) {
LOG.error(e.getMessage());
LOG.error(e.getCause());
} catch (CharacterCodingException e) {
LOG.error(e.getMessage());
LOG.error(e.getCause());
} catch (Exception e) {
LOG.error(e.getMessage());
LOG.error(e.getCause());
}
}
@Override
public void shutdown(IRecordProcessorCheckpointer checkpointer, ShutdownReason reason) {
LOG.warn("Shutting down record processor for shard: " + kinesisShardId);
try {
if (reason == ShutdownReason.TERMINATE) {
checkpoint(checkpointer);
}
} catch (Exception e) {
//System.out.println(e.getMessage());
LOG.error(e.getMessage());
LOG.error(e.getCause());
}
}
private void checkpoint(IRecordProcessorCheckpointer checkpointer) throws Exception {
for (int i = 0; i < NUMBER_OF_RETRIES; i++) {
try {
checkpointer.checkpoint();
break;
} catch (ShutdownException se) {
// Ignore checkpoint if the processor instance has been shutdown (fail over).
LOG.warn(se);
//break;
} catch (ThrottlingException e) {
// Backoff and re-attempt checkpoint upon transient failures
if (i >= (NUMBER_OF_RETRIES - 1)) {
LOG.error(e.getMessage());
LOG.info(e);
break;
} else {
LOG.info("Transient issue when checkpointing - attempt " + (i + 1) + " of " + NUMBER_OF_RETRIES + " " + e);
}
} catch (InvalidStateException e) {
// This indicates an issue with the DynamoDB table (check for table, provisioned IOPS).
LOG.error(e.getMessage());
LOG.error(e);
break;
}
try {
Thread.sleep(BACKOFF_TIME_IN_MILLIS);
} catch (InterruptedException e) {
LOG.info("Interrupted sleep" + " " + e);
}
}
}
private void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(10, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(10, TimeUnit.SECONDS))
LOG.error("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
/**
* Asynchronous queue consumer to process records.
*/
private class QueueConsumer implements Runnable {
/**
* /**
* Flag to shutdown the queue consumer.
*/
volatile boolean shutdown = false;
/**
* The latest checkpointer. Is wrapped to protect user from calling default checkpoint method. All access should be synchronized on the instance of
* {@link QueueConsumer}.
*/
private volatile IRecordProcessorCheckpointer checkpointer;
public void setCheckpointer(IRecordProcessorCheckpointer checkpointer) {
this.checkpointer = checkpointer;
}
@Override
public void run() {
LOG.info("Starting queue consumer for shard: " + kinesisShardId);
while (!shutdown) {
try {
consumeQueue();
Thread.sleep(1000);
} catch (Exception e) {
LOG.error(e);
}
}
LOG.info("Queue consumer terminated for shard: " + kinesisShardId);
}
/**
* Processes the records in the queue using the wrapped {@link IRecordProcessor}.
*/
private void consumeQueue() throws Exception {
final List<Record> records = new ArrayList<Record>();
// Use blocking queue's poll with timeout to wait for new records
Record polled = null;
try {
polled = recordQueue.poll(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOG.error(e);
Thread.currentThread().interrupt();
}
// Check if queue contained records.
if (polled == null) {
processRecords(records /* Empty list */, checkpointer);
return;
}
records.add(polled);
// Drain the remaining of the records
recordQueue.drainTo(records);
processRecordsWithRetries(records);
LOG.info("Thread: " + Thread.currentThread().getName() + ": batcharray = " + batcharray.size());
if (batcharray.size() > Integer.valueOf(config.getPropValues("batch_scale"))) {
/** batch put into s3
* It is imperative that the user manually synchronize on the returned set when iterating over it, Failure to follow this advice may result in non-deterministic
* behavior. According to implementation. {@link batcharray}
*/
/* It is imperative that the user manually synchronize on the returned set when iterating over it */
synchronized (batcharray) {
batcharray.stream().distinct().forEach(i -> batchstring.append(i).append(System.lineSeparator()));
batcharray.clear();
}
ByteArrayOutputStream compress = new CompressWrite().writestream(batchstring.toString());
objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
objectMetadata.setContentLength(compress.size());
s3key = new String(config.getPropValues("s3ProdPrefix") + "/" + new String(ft.format(new Date())) + "/" + new String(ftkey.format(new Date())) + "-" + RandomStringUtils.randomAlphanumeric(8) + "-" + config.getPropValues("region") + ".gz");
PutObjectRequest putRequestCSV = new PutObjectRequest(config.getPropValues("s3Bucket"), s3key, new ByteArrayInputStream(compress.toByteArray()), objectMetadata);
putRequestCSV.setMetadata(objectMetadata);
cli.putObject(putRequestCSV);
LOG.info("s3 Put" + " : " + s3key);
/*resets StringBuilder and Array*/
batchstring.setLength(0);
compress.close();
}
processRecords(records, checkpointer /* Protected checkpointer */);
}
}
}