Skip to content

Commit

Permalink
Avoid process crash when elasticsearch response is nil
Browse files Browse the repository at this point in the history
Add nil pointer check in elasticsearch bulkprocessor's
after callback, defined inside pkg/es/config/config.go.
In some rare cases, elasticsearch request will return
error with nil response, and then nil response will
be passed into after callback, and using of nil pointer
will cause process crash.

Signed-off-by: Li Zhaoxing <lizhaoxing@4paradigm.com>
  • Loading branch information
Li Zhaoxing committed Apr 10, 2019
1 parent 64f8bce commit 3d131c9
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pkg/es/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (c *Configuration) NewClient(logger *zap.Logger, metricsFactory metrics.Fac
m.Delete(id)

// log individual errors, note that err might be false and these errors still present
if response.Errors {
if response != nil && response.Errors {
for _, it := range response.Items {
for key, val := range it {
if val.Error != nil {
Expand All @@ -127,13 +127,21 @@ func (c *Configuration) NewClient(logger *zap.Logger, metricsFactory metrics.Fac

sm.Emit(err, time.Since(start.(time.Time)))
if err != nil {
failed := len(response.Failed())
var failed int
var respval interface{}
if response == nil {
failed = 0
respval = "nil"
} else {
failed = len(response.Failed())
respval = response
}
total := len(requests)
logger.Error("Elasticsearch could not process bulk request",
zap.Int("request_count", total),
zap.Int("failed_count", failed),
zap.Error(err),
zap.Any("response", response))
zap.Any("response", respval))
}
}).
BulkSize(c.BulkSize).
Expand Down

0 comments on commit 3d131c9

Please sign in to comment.