From 07e11ecf49f5375cdb83c6bb1d9bf8f9c135dad1 Mon Sep 17 00:00:00 2001 From: Anderson Ribeiro Date: Mon, 6 Mar 2023 23:28:51 +0000 Subject: [PATCH 1/2] notify items if an error occurs in bulk indexer --- esutil/bulk_indexer.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/esutil/bulk_indexer.go b/esutil/bulk_indexer.go index 5ed206e9f6..d6e592077c 100644 --- a/esutil/bulk_indexer.go +++ b/esutil/bulk_indexer.go @@ -573,7 +573,8 @@ func (w *worker) flush(ctx context.Context) error { if w.bi.config.OnError != nil { w.bi.config.OnError(ctx, fmt.Errorf("flush: %s", err)) } - return fmt.Errorf("flush: %s", err) + w.notifyItemsOnError(ctx, err) + return fmt.Errorf("flush: %s", fmt.Errorf("flush: %s", err)) } if res.Body != nil { defer res.Body.Close() @@ -584,6 +585,7 @@ func (w *worker) flush(ctx context.Context) error { if w.bi.config.OnError != nil { w.bi.config.OnError(ctx, fmt.Errorf("flush: %s", res.String())) } + w.notifyItemsOnError(ctx, err) return fmt.Errorf("flush: %s", res.String()) } @@ -592,6 +594,7 @@ func (w *worker) flush(ctx context.Context) error { if w.bi.config.OnError != nil { w.bi.config.OnError(ctx, fmt.Errorf("flush: %s", err)) } + w.notifyItemsOnError(ctx, err) return fmt.Errorf("flush: error parsing response body: %s", err) } @@ -638,6 +641,14 @@ func (w *worker) flush(ctx context.Context) error { return err } +func (w *worker) notifyItemsOnError(ctx context.Context, err error) { + for _, item := range w.items { + if item.OnFailure != nil { + item.OnFailure(ctx, item, BulkIndexerResponseItem{}, fmt.Errorf("flush: %w", err)) + } + } +} + type defaultJSONDecoder struct{} func (d defaultJSONDecoder) UnmarshalFromReader(r io.Reader, blk *BulkIndexerResponse) error { From 9811779be56ed25c44e6e1f8374160e9eff0fd5d Mon Sep 17 00:00:00 2001 From: Anderson Ribeiro Date: Mon, 6 Mar 2023 23:48:16 +0000 Subject: [PATCH 2/2] fix message error on line 577 --- esutil/bulk_indexer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esutil/bulk_indexer.go b/esutil/bulk_indexer.go index d6e592077c..909492f669 100644 --- a/esutil/bulk_indexer.go +++ b/esutil/bulk_indexer.go @@ -574,7 +574,7 @@ func (w *worker) flush(ctx context.Context) error { w.bi.config.OnError(ctx, fmt.Errorf("flush: %s", err)) } w.notifyItemsOnError(ctx, err) - return fmt.Errorf("flush: %s", fmt.Errorf("flush: %s", err)) + return fmt.Errorf("flush: %s", err) } if res.Body != nil { defer res.Body.Close()