Skip to content

Commit 25f08a9

Browse files
adnan-cdenyeart
authored andcommitted
[FAB-2600] fix race condition in peer and CouchDB
https://jira.hyperledger.org/browse/FAB-2600 Fixing race codition between peer startup and couchDB startup by updating rety timeouts using a backoff approach,logging warning messages for retries. This will fix docker environments where CouchDB and peer start at same time. Post-beta, retry logic will be enhanced to allow configurable retries and allow clients to specify retry behavior, since eventually we'll want Endorser vs Committer vs peer startup Verification to use different retry logic. Change-Id: I340aebe4165d2e2c27433f75b097f1256029e1c6 Signed-off-by: Adnan Choudhury <[email protected]>
1 parent 9ab37a5 commit 25f08a9

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

core/ledger/util/couchdb/couchdb.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ import (
4242
var logger = logging.MustGetLogger("couchdb")
4343

4444
//maximum number of retry attempts
45-
const maxRetries = 3
45+
const maxRetries = 10
4646

4747
//time between retry attempts in milliseconds
48-
const retryWaitTime = 100
48+
const retryWaitTime = 125
4949

5050
// DBOperationResponse is body for successful database calls.
5151
type DBOperationResponse struct {
@@ -994,6 +994,9 @@ func (couchInstance *CouchInstance) handleRequest(method, connectURL string, dat
994994
var errResp error
995995
couchDBReturn := &DBReturn{}
996996

997+
//set initial wait duration for retries
998+
waitDuration := retryWaitTime * time.Millisecond
999+
9971000
//attempt the http request for the max number of retries
9981001
for attempts := 0; attempts < maxRetries; attempts++ {
9991002

@@ -1009,8 +1012,8 @@ func (couchInstance *CouchInstance) handleRequest(method, connectURL string, dat
10091012
if errResp != nil {
10101013

10111014
//Log the error with the retry count and continue
1012-
logger.Debugf("Retrying couchdb request. Retry:%v Error:%v",
1013-
attempts+1, errResp.Error())
1015+
logger.Warningf("Retrying couchdb request in %s. Attempt:%v Error:%v",
1016+
waitDuration.String(), attempts+1, errResp.Error())
10141017

10151018
} else {
10161019

@@ -1026,13 +1029,16 @@ func (couchInstance *CouchInstance) handleRequest(method, connectURL string, dat
10261029
json.Unmarshal(errorBytes, &couchDBReturn)
10271030

10281031
//Log the 500 error with the retry count and continue
1029-
logger.Debugf("Retrying couchdb request. Retry:%v Couch DB Error:%s, Status Code:%v Reason:%v",
1030-
attempts+1, couchDBReturn.Error, resp.Status, couchDBReturn.Reason)
1032+
logger.Warningf("Retrying couchdb request in %s. Attempt:%v Couch DB Error:%s, Status Code:%v Reason:%v",
1033+
waitDuration.String(), attempts+1, couchDBReturn.Error, resp.Status, couchDBReturn.Reason)
10311034

10321035
}
10331036

10341037
//sleep for specified sleep time, then retry
1035-
time.Sleep(retryWaitTime * time.Millisecond)
1038+
time.Sleep(waitDuration)
1039+
1040+
//backoff, doubling the retry time for next attempt
1041+
waitDuration *= 2
10361042

10371043
}
10381044

0 commit comments

Comments
 (0)