diff options
author | Vesselina Ratcheva <vesselina.ratcheva@10gen.com> | 2018-02-15 13:39:58 -0500 |
---|---|---|
committer | Vesselina Ratcheva <vesselina.ratcheva@10gen.com> | 2018-02-15 13:43:54 -0500 |
commit | 30698911d3bbde3e351c455c8bacc96bb916fac8 (patch) | |
tree | 2b0207497f4c2d26e792d19a3ef32b61f7d896b7 /src/mongo/shell | |
parent | 4c97e0cf511426a3d9df9f80bb9323468e8e59fe (diff) | |
download | mongo-30698911d3bbde3e351c455c8bacc96bb916fac8.tar.gz |
SERVER-32647 Retry connecting to replica set when given a seed node
Diffstat (limited to 'src/mongo/shell')
-rw-r--r-- | src/mongo/shell/replsettest.js | 7 | ||||
-rw-r--r-- | src/mongo/shell/utils.js | 26 |
2 files changed, 32 insertions, 1 deletions
diff --git a/src/mongo/shell/replsettest.js b/src/mongo/shell/replsettest.js index c696e102cf4..2f304e8e2e5 100644 --- a/src/mongo/shell/replsettest.js +++ b/src/mongo/shell/replsettest.js @@ -2114,7 +2114,12 @@ var ReplSetTest = function(opts) { } if (typeof opts === 'string' || opts instanceof String) { - _constructFromExistingSeedNode(opts); + retryOnNetworkError(function() { + // The primary may unexpectedly step down during startup if under heavy load + // and too slowly processing heartbeats. When it steps down, it closes all of + // its connections. + _constructFromExistingSeedNode(opts); + }, 10); } else { _constructStartNewInstances(opts); } diff --git a/src/mongo/shell/utils.js b/src/mongo/shell/utils.js index 1378dadcf75..c07aebac8d7 100644 --- a/src/mongo/shell/utils.js +++ b/src/mongo/shell/utils.js @@ -37,6 +37,32 @@ function _getErrorWithCode(codeOrObj, message) { return e; } +/** + * Executes the specified function and retries it if it fails due to exception related to network + * error. If it exhausts the number of allowed retries, it simply throws the last exception. + * + * Returns the return value of the input call. + */ +function retryOnNetworkError(func, numRetries, sleepMs) { + numRetries = numRetries || 1; + sleepMs = sleepMs || 1000; + + while (true) { + try { + return func(); + } catch (e) { + if (isNetworkError(e) && numRetries > 0) { + print("Network error occurred and the call will be retried: " + + tojson({error: e.toString(), stack: e.stack})); + numRetries--; + sleep(sleepMs); + } else { + throw e; + } + } + } +} + // Checks if a javascript exception is a network error. function isNetworkError(error) { return error.message.indexOf("network error") >= 0 || |