/
errors.go
223 lines (197 loc) · 5.42 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package client
import (
"fmt"
"sync"
"github.com/m3db/m3/src/dbnode/generated/thrift/rpc"
tterrors "github.com/m3db/m3/src/dbnode/network/server/tchannelthrift/errors"
xerrors "github.com/m3db/m3/src/x/errors"
)
// IsInternalServerError determines if the error is an internal server error.
func IsInternalServerError(err error) bool {
for err != nil {
if e, ok := err.(*rpc.Error); ok && tterrors.IsInternalError(e) {
return true
}
err = xerrors.InnerError(err)
}
return false
}
// IsBadRequestError determines if the error is a bad request error.
func IsBadRequestError(err error) bool {
for err != nil {
if e, ok := err.(*rpc.Error); ok && tterrors.IsBadRequestError(e) {
return true
}
if e := xerrors.GetInnerInvalidParamsError(err); e != nil {
return true
}
err = xerrors.InnerError(err)
}
return false
}
// IsConsistencyResultError determines if the error is a consistency result error.
func IsConsistencyResultError(err error) bool {
_, ok := err.(consistencyResultErr)
return ok
}
// NumResponded returns how many nodes responded for a given error
func NumResponded(err error) int {
for err != nil {
if e, ok := err.(consistencyResultError); ok {
return e.numResponded()
}
err = xerrors.InnerError(err)
}
return 0
}
// NumSuccess returns how many nodes responded with success for a given error
func NumSuccess(err error) int {
for err != nil {
if e, ok := err.(consistencyResultError); ok {
return e.numSuccess()
}
err = xerrors.InnerError(err)
}
return 0
}
// NumError returns how many nodes responded with error for a given error
func NumError(err error) int {
for err != nil {
if e, ok := err.(consistencyResultError); ok {
return e.numResponded() -
e.numSuccess()
}
err = xerrors.InnerError(err)
}
return 0
}
type hostNotAvailableError struct {
err error
}
func (h hostNotAvailableError) Error() string {
return h.err.Error()
}
func newHostNotAvailableError(err error) error {
return xerrors.NewNonRetryableError(hostNotAvailableError{err: err})
}
func isHostNotAvailableError(err error) bool {
inner := xerrors.GetInnerNonRetryableError(err)
if inner == nil {
return false
}
_, ok := inner.(hostNotAvailableError)
return ok
}
type consistencyResultError interface {
error
InnerError() error
numResponded() int
numSuccess() int
}
type consistencyResultErr struct {
level fmt.Stringer
success int
enqueued int
responded int
topLevelErr error
errs []error
}
func newConsistencyResultError(
level fmt.Stringer,
enqueued, responded int,
errs []error,
) consistencyResultError {
// NB(r): if any errors are bad request errors, encapsulate that error
// to ensure the error itself is wholly classified as a bad request error
var topLevelErr error
for i := 0; i < len(errs); i++ {
if topLevelErr == nil {
topLevelErr = errs[i]
continue
}
if IsBadRequestError(errs[i]) {
topLevelErr = errs[i]
break
}
}
return consistencyResultErr{
level: level,
success: enqueued - len(errs),
enqueued: enqueued,
responded: responded,
topLevelErr: topLevelErr,
errs: append([]error(nil), errs...),
}
}
func (e consistencyResultErr) InnerError() error {
return e.topLevelErr
}
func (e consistencyResultErr) Error() string {
return fmt.Sprintf(
"failed to meet consistency level %s with %d/%d success, "+
"%d nodes responded, errors: %v",
e.level.String(), e.success, e.enqueued, e.responded, e.errs)
}
func (e consistencyResultErr) numResponded() int {
return e.responded
}
func (e consistencyResultErr) numSuccess() int {
return e.success
}
type syncAbortableErrorsMap struct {
sync.RWMutex
errors map[int]error
abortError error
}
func newSyncAbortableErrorsMap() *syncAbortableErrorsMap {
return &syncAbortableErrorsMap{
errors: make(map[int]error),
}
}
func (e *syncAbortableErrorsMap) setError(idx int, err error) {
e.Lock()
e.errors[idx] = err
e.Unlock()
}
func (e *syncAbortableErrorsMap) getErrors() []error {
var result []error
e.RLock()
for _, err := range e.errors {
if err == nil {
continue
}
result = append(result, err)
}
e.RUnlock()
return result
}
func (e *syncAbortableErrorsMap) setAbortError(err error) {
e.Lock()
e.abortError = err
e.Unlock()
}
func (e *syncAbortableErrorsMap) getAbortError() error {
e.RLock()
result := e.abortError
e.RUnlock()
return result
}