Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions kv/shard_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,32 @@ func (s *ShardRouter) process(reqs []*pb.Request, fn func(*routerGroup, []*pb.Re
}

var max uint64
var firstErr error
for gid, rs := range grouped {
g, ok := s.getGroup(gid)
if !ok {
return nil, errors.Wrapf(ErrInvalidRequest, "unknown group %d", gid)
err := errors.Wrapf(ErrInvalidRequest, "unknown group %d", gid)
if firstErr == nil {
firstErr = err
}
continue
}
r, err := fn(g, rs)
if err != nil {
return nil, errors.WithStack(err)
if firstErr == nil {
firstErr = errors.WithStack(err)
}
continue
}
if r.CommitIndex > max {
max = r.CommitIndex
}
}
return &TransactionResponse{CommitIndex: max}, nil
resp := &TransactionResponse{CommitIndex: max}
if firstErr != nil {
return resp, firstErr
}
return resp, nil
Comment on lines +62 to +87

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the change correctly ensures all groups are processed even if one fails and prevents a potential nil pointer panic, it only captures the first error encountered. Any subsequent errors from other groups are silently ignored. This can make debugging difficult when multiple shards fail.

To provide more comprehensive error information, I suggest collecting all errors and combining them. The cockroachdb/errors package, already used in this project, provides errors.CombineErrors which is well-suited for this. This also simplifies the code at the end of the function.

	var multiErr error
	for gid, rs := range grouped {
		g, ok := s.getGroup(gid)
		if !ok {
			err := errors.Wrapf(ErrInvalidRequest, "unknown group %d", gid)
			multiErr = errors.CombineErrors(multiErr, err)
			continue
		}
		r, err := fn(g, rs)
		if err != nil {
			multiErr = errors.CombineErrors(multiErr, errors.WithStack(err))
			continue
		}
		if r.CommitIndex > max {
			max = r.CommitIndex
		}
	}
	resp := &TransactionResponse{CommitIndex: max}
	return resp, multiErr

}

func (s *ShardRouter) getGroup(id uint64) (*routerGroup, bool) {
Expand Down
Loading