-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Alexander K <[email protected]> Co-authored-by: Alexander Klyuev <[email protected]>
- Loading branch information
1 parent
ca3cdfc
commit f03fa5f
Showing
7 changed files
with
321 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
Copyright 2020 YANDEX LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package hasql | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strings" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// CollectedErrors are errors collected when checking node statuses | ||
type CollectedErrors struct { | ||
Errors []NodeError | ||
} | ||
|
||
func (e *CollectedErrors) Error() string { | ||
if len(e.Errors) == 1 { | ||
return e.Errors[0].Error() | ||
} | ||
|
||
errs := make([]string, len(e.Errors)) | ||
for i, ne := range e.Errors { | ||
errs[i] = ne.Error() | ||
} | ||
/* | ||
I don't believe there exist 'best join separator' that fit all cases (cli output, JSON, .. etc), | ||
so we use newline as error.Join did it. | ||
In difficult cases (as suggested in https://github.com/yandex/go-hasql/pull/14), | ||
the user should be able to receive "raw" errors and format them as it suits him. | ||
*/ | ||
return strings.Join(errs, "\n") | ||
} | ||
|
||
// NodeError is error that background goroutine got while check given node | ||
type NodeError struct { | ||
Addr string | ||
Err error | ||
OccurredAt time.Time | ||
} | ||
|
||
func (e *NodeError) Error() string { | ||
// 'foo.db' node error occurred at '2009-11-10..': FATAL: terminating connection due to ... | ||
return fmt.Sprintf("%q node error occurred at %q: %s", e.Addr, e.OccurredAt, e.Err) | ||
} | ||
|
||
type errorsCollector struct { | ||
store map[string]NodeError | ||
mu sync.Mutex | ||
} | ||
|
||
func newErrorsCollector() errorsCollector { | ||
return errorsCollector{store: make(map[string]NodeError)} | ||
} | ||
|
||
func (e *errorsCollector) Add(addr string, err error, occurredAt time.Time) { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
e.store[addr] = NodeError{ | ||
Addr: addr, | ||
Err: err, | ||
OccurredAt: occurredAt, | ||
} | ||
} | ||
|
||
func (e *errorsCollector) Remove(addr string) { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
delete(e.store, addr) | ||
} | ||
|
||
func (e *errorsCollector) Err() error { | ||
e.mu.Lock() | ||
errList := make([]NodeError, 0, len(e.store)) | ||
for _, nErr := range e.store { | ||
errList = append(errList, nErr) | ||
} | ||
e.mu.Unlock() | ||
|
||
if len(errList) == 0 { | ||
return nil | ||
} | ||
|
||
sort.Slice(errList, func(i, j int) bool { | ||
return errList[i].OccurredAt.Before(errList[j].OccurredAt) | ||
}) | ||
return &CollectedErrors{Errors: errList} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
Copyright 2020 YANDEX LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package hasql | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestErrorsCollector(t *testing.T) { | ||
nodesCount := 10 | ||
errCollector := newErrorsCollector() | ||
require.NoError(t, errCollector.Err()) | ||
|
||
connErr := errors.New("node connection error") | ||
occurredAt := time.Now() | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(nodesCount) | ||
for i := 1; i <= nodesCount; i++ { | ||
go func(i int) { | ||
defer wg.Done() | ||
errCollector.Add( | ||
fmt.Sprintf("node-%d", i), | ||
connErr, | ||
occurredAt, | ||
) | ||
}(i) | ||
} | ||
|
||
errCollectDone := make(chan struct{}) | ||
go func() { | ||
for { | ||
select { | ||
case <-errCollectDone: | ||
return | ||
default: | ||
// there are no assertions here, because that logic expected to run with -race, | ||
// otherwise it doesn't test anything, just eat CPU. | ||
_ = errCollector.Err() | ||
} | ||
} | ||
}() | ||
|
||
wg.Wait() | ||
close(errCollectDone) | ||
|
||
err := errCollector.Err() | ||
for i := 1; i <= nodesCount; i++ { | ||
assert.ErrorContains(t, err, fmt.Sprintf("\"node-%d\" node error occurred at", i)) | ||
} | ||
assert.ErrorContains(t, err, connErr.Error()) | ||
|
||
} |