@@ -217,28 +217,38 @@ func (client *DataSetClient) isProcessingEvents() bool {
217
217
// - tries (with 2nd half of shutdownMaxTimeout period) to send processed events (buffers) into DataSet
218
218
func (client * DataSetClient ) Shutdown () error {
219
219
client .Logger .Info ("Shutting down - BEGIN" )
220
+ // start measuring processing time
221
+ processingStart := time .Now ()
222
+
220
223
// mark as finished to prevent processing of further events
221
224
client .finished .Store (true )
222
225
223
226
// log statistics when finish was called
224
227
client .logStatistics ()
225
228
229
+ retryShutdownTimeout := client .Config .BufferSettings .RetryShutdownTimeout
230
+ maxElapsedTime := retryShutdownTimeout / 2 - 100 * time .Millisecond
231
+ client .Logger .Info (
232
+ "Shutting down - waiting for events" ,
233
+ zap .Duration ("maxElapsedTime" , maxElapsedTime ),
234
+ zap .Duration ("retryShutdownTimeout" , retryShutdownTimeout ),
235
+ zap .Duration ("elapsedTime" , time .Since (processingStart )),
236
+ )
237
+
226
238
var lastError error = nil
227
- shutdownTimeout := minDuration (client .Config .BufferSettings .RetryMaxElapsedTime , client .Config .BufferSettings .RetryShutdownTimeout )
228
239
expBackoff := backoff.ExponentialBackOff {
229
240
InitialInterval : client .Config .BufferSettings .RetryInitialInterval ,
230
241
RandomizationFactor : client .Config .BufferSettings .RetryRandomizationFactor ,
231
242
Multiplier : client .Config .BufferSettings .RetryMultiplier ,
232
243
MaxInterval : client .Config .BufferSettings .RetryMaxInterval ,
233
- MaxElapsedTime : shutdownTimeout / 2 ,
244
+ MaxElapsedTime : maxElapsedTime ,
234
245
Stop : backoff .Stop ,
235
246
Clock : backoff .SystemClock ,
236
247
}
237
248
expBackoff .Reset ()
238
249
239
250
// try (with timeout) to process (add into buffers) events,
240
251
retryNum := 0
241
- processingStart := time .Now ()
242
252
for client .isProcessingEvents () {
243
253
// log statistics
244
254
client .logStatistics ()
@@ -250,38 +260,40 @@ func (client *DataSetClient) Shutdown() error {
250
260
zap .Duration ("backoffDelay" , backoffDelay ),
251
261
zap .Uint64 ("eventsEnqueued" , client .eventsEnqueued .Load ()),
252
262
zap .Uint64 ("eventsProcessed" , client .eventsProcessed .Load ()),
263
+ zap .Duration ("elapsedTime" , time .Since (processingStart )),
264
+ zap .Duration ("maxElapsedTime" , maxElapsedTime ),
253
265
)
254
266
if backoffDelay == expBackoff .Stop {
255
- lastError = fmt .Errorf (
256
- "not all events have been processed - %d" ,
257
- client .eventsEnqueued .Load ()- client .eventsProcessed .Load (),
258
- )
259
- client .Logger .Error (
260
- "Shutting down - not all events have been processed" ,
261
- zap .Int ("retryNum" , retryNum ),
262
- zap .Duration ("backoffDelay" , backoffDelay ),
263
- zap .Uint64 ("eventsEnqueued" , client .eventsEnqueued .Load ()),
264
- zap .Uint64 ("eventsProcessed" , client .eventsProcessed .Load ()),
265
- )
266
267
break
267
268
}
268
269
time .Sleep (backoffDelay )
269
270
retryNum ++
270
271
}
271
272
272
273
// send all buffers
273
- client .Logger .Info ("Shutting down - publishing all buffers" )
274
+ client .Logger .Info (
275
+ "Shutting down - publishing all buffers" ,
276
+ zap .Duration ("retryShutdownTimeout" , retryShutdownTimeout ),
277
+ zap .Duration ("elapsedTime" , time .Since (processingStart )),
278
+ )
274
279
client .publishAllBuffers ()
275
280
276
281
// reinitialize expBackoff with MaxElapsedTime based on actually elapsed time of processing (previous phase)
277
282
processingElapsed := time .Since (processingStart )
278
- remainingShutdownTimeout := maxDuration (shutdownTimeout - processingElapsed , shutdownTimeout / 2 )
283
+ maxElapsedTime = maxDuration (retryShutdownTimeout - processingElapsed , retryShutdownTimeout / 2 )
284
+ client .Logger .Info (
285
+ "Shutting down - waiting for buffers" ,
286
+ zap .Duration ("maxElapsedTime" , maxElapsedTime ),
287
+ zap .Duration ("retryShutdownTimeout" , retryShutdownTimeout ),
288
+ zap .Duration ("elapsedTime" , time .Since (processingStart )),
289
+ )
290
+
279
291
expBackoff = backoff.ExponentialBackOff {
280
292
InitialInterval : client .Config .BufferSettings .RetryInitialInterval ,
281
293
RandomizationFactor : client .Config .BufferSettings .RetryRandomizationFactor ,
282
294
Multiplier : client .Config .BufferSettings .RetryMultiplier ,
283
295
MaxInterval : client .Config .BufferSettings .RetryMaxInterval ,
284
- MaxElapsedTime : remainingShutdownTimeout ,
296
+ MaxElapsedTime : maxElapsedTime ,
285
297
Stop : backoff .Stop ,
286
298
Clock : backoff .SystemClock ,
287
299
}
@@ -301,25 +313,43 @@ func (client *DataSetClient) Shutdown() error {
301
313
zap .Uint64 ("buffersEnqueued" , client .buffersEnqueued .Load ()),
302
314
zap .Uint64 ("buffersProcessed" , client .buffersProcessed .Load ()),
303
315
zap .Uint64 ("buffersDropped" , client .buffersDropped .Load ()),
316
+ zap .Duration ("elapsedTime" , time .Since (processingStart )),
317
+ zap .Duration ("maxElapsedTime" , maxElapsedTime ),
304
318
)
305
319
if backoffDelay == expBackoff .Stop {
306
- lastError = fmt .Errorf (
307
- "not all buffers have been processed - %d" ,
308
- client .buffersEnqueued .Load ()- client .buffersProcessed .Load ()- client .buffersDropped .Load (),
309
- )
310
- client .Logger .Error (
311
- "Shutting down - not all buffers have been processed" ,
312
- zap .Int ("retryNum" , retryNum ),
313
- zap .Uint64 ("buffersEnqueued" , client .buffersEnqueued .Load ()),
314
- zap .Uint64 ("buffersProcessed" , client .buffersProcessed .Load ()),
315
- zap .Uint64 ("buffersDropped" , client .buffersDropped .Load ()),
316
- )
317
320
break
318
321
}
319
322
time .Sleep (backoffDelay )
320
323
retryNum ++
321
324
}
322
325
326
+ // construct error messages
327
+ if client .isProcessingEvents () {
328
+ lastError = fmt .Errorf (
329
+ "not all events have been processed - %d" ,
330
+ client .eventsEnqueued .Load ()- client .eventsProcessed .Load (),
331
+ )
332
+ client .Logger .Error (
333
+ "Shutting down - not all events have been processed" ,
334
+ zap .Uint64 ("eventsEnqueued" , client .eventsEnqueued .Load ()),
335
+ zap .Uint64 ("eventsProcessed" , client .eventsProcessed .Load ()),
336
+ )
337
+ }
338
+
339
+ if client .isProcessingBuffers () {
340
+ lastError = fmt .Errorf (
341
+ "not all buffers have been processed - %d" ,
342
+ client .buffersEnqueued .Load ()- client .buffersProcessed .Load ()- client .buffersDropped .Load (),
343
+ )
344
+ client .Logger .Error (
345
+ "Shutting down - not all buffers have been processed" ,
346
+ zap .Int ("retryNum" , retryNum ),
347
+ zap .Uint64 ("buffersEnqueued" , client .buffersEnqueued .Load ()),
348
+ zap .Uint64 ("buffersProcessed" , client .buffersProcessed .Load ()),
349
+ zap .Uint64 ("buffersDropped" , client .buffersDropped .Load ()),
350
+ )
351
+ }
352
+
323
353
buffersDropped := client .buffersDropped .Load () - initialDropped
324
354
if buffersDropped > 0 {
325
355
lastError = fmt .Errorf (
@@ -336,9 +366,17 @@ func (client *DataSetClient) Shutdown() error {
336
366
client .logStatistics ()
337
367
338
368
if lastError == nil {
339
- client .Logger .Info ("Shutting down - success" )
369
+ client .Logger .Info (
370
+ "Shutting down - success" ,
371
+ zap .Duration ("retryShutdownTimeout" , retryShutdownTimeout ),
372
+ zap .Duration ("elapsedTime" , time .Since (processingStart )),
373
+ )
340
374
} else {
341
- client .Logger .Error ("Shutting down - error" , zap .Error (lastError ))
375
+ client .Logger .Error (
376
+ "Shutting down - error" , zap .Error (lastError ),
377
+ zap .Duration ("retryShutdownTimeout" , retryShutdownTimeout ),
378
+ zap .Duration ("elapsedTime" , time .Since (processingStart )),
379
+ )
342
380
if client .LastError () == nil {
343
381
return lastError
344
382
}
@@ -475,13 +513,6 @@ func truncateText(text string, length int) string {
475
513
return text
476
514
}
477
515
478
- func minDuration (a , b time.Duration ) time.Duration {
479
- if a <= b {
480
- return a
481
- }
482
- return b
483
- }
484
-
485
516
func maxDuration (a , b time.Duration ) time.Duration {
486
517
if a >= b {
487
518
return a
0 commit comments