46
46
*/
47
47
public class MessageProcessorRunnable implements Runnable {
48
48
private static final Logger logger = LogManager .getLogger (MessageProcessorRunnable .class );
49
+ private static final String ID = "_id" ;
50
+ private static final String OP_TYPE = "_op_type" ;
51
+ private static final String SOURCE = "_source" ;
52
+
53
+ private volatile IngestionErrorStrategy errorStrategy ;
54
+ private volatile boolean isWriterPaused ;
49
55
50
56
private final BlockingQueue <IngestionShardConsumer .ReadResult <? extends IngestionShardPointer , ? extends Message >> blockingQueue ;
51
57
private final MessageProcessor messageProcessor ;
52
58
private final CounterMetric stats = new CounterMetric ();
53
- private IngestionErrorStrategy errorStrategy ;
54
-
55
- private static final String ID = "_id" ;
56
- private static final String OP_TYPE = "_op_type" ;
57
- private static final String SOURCE = "_source" ;
58
59
59
60
/**
60
61
* Constructor.
@@ -226,23 +227,39 @@ private static BytesReference convertToBytes(Object object) throws IOException {
226
227
@ Override
227
228
public void run () {
228
229
while (!(Thread .currentThread ().isInterrupted ())) {
229
- IngestionShardConsumer .ReadResult <? extends IngestionShardPointer , ? extends Message > result = null ;
230
- try {
231
- result = blockingQueue .poll (1000 , TimeUnit .MILLISECONDS );
232
- } catch (InterruptedException e ) {
233
- // TODO: add metric
234
- logger .debug ("MessageProcessorRunnable poll interruptedException" , e );
235
- Thread .currentThread ().interrupt (); // Restore interrupt status
236
- }
237
- if (result != null ) {
230
+ if (isWriterPaused ) {
238
231
try {
239
- stats .inc ();
240
- messageProcessor .process (result .getMessage (), result .getPointer ());
241
- } catch (Exception e ) {
242
- errorStrategy .handleError (e , IngestionErrorStrategy .ErrorStage .PROCESSING );
243
- if (errorStrategy .shouldPauseIngestion (e , IngestionErrorStrategy .ErrorStage .PROCESSING )) {
244
- Thread .currentThread ().interrupt ();
245
- }
232
+ // TODO: make sleep time configurable
233
+ Thread .sleep (5000 );
234
+ } catch (InterruptedException e ) {
235
+ Thread .currentThread ().interrupt (); // Restore interrupt status
236
+ } catch (Throwable e ) {
237
+ logger .error ("Error in pausing the ingestion writer thread" , e );
238
+ }
239
+ continue ;
240
+ }
241
+
242
+ pollAndProcessMessage ();
243
+ }
244
+ }
245
+
246
+ private void pollAndProcessMessage () {
247
+ IngestionShardConsumer .ReadResult <? extends IngestionShardPointer , ? extends Message > result = null ;
248
+ try {
249
+ result = blockingQueue .poll (1000 , TimeUnit .MILLISECONDS );
250
+ } catch (InterruptedException e ) {
251
+ // TODO: add metric
252
+ logger .debug ("MessageProcessorRunnable poll interruptedException" , e );
253
+ Thread .currentThread ().interrupt (); // Restore interrupt status
254
+ }
255
+ if (result != null ) {
256
+ try {
257
+ stats .inc ();
258
+ messageProcessor .process (result .getMessage (), result .getPointer ());
259
+ } catch (Exception e ) {
260
+ errorStrategy .handleError (e , IngestionErrorStrategy .ErrorStage .PROCESSING );
261
+ if (errorStrategy .shouldPauseIngestion (e , IngestionErrorStrategy .ErrorStage .PROCESSING )) {
262
+ isWriterPaused = true ;
246
263
}
247
264
}
248
265
}
@@ -251,4 +268,20 @@ public void run() {
251
268
public CounterMetric getStats () {
252
269
return stats ;
253
270
}
271
+
272
+ public IngestionErrorStrategy getErrorStrategy () {
273
+ return this .errorStrategy ;
274
+ }
275
+
276
+ public void setErrorStrategy (IngestionErrorStrategy errorStrategy ) {
277
+ this .errorStrategy = errorStrategy ;
278
+ }
279
+
280
+ public void pauseWriter () {
281
+ this .isWriterPaused = true ;
282
+ }
283
+
284
+ public void resumeWriter () {
285
+ this .isWriterPaused = false ;
286
+ }
254
287
}
0 commit comments