|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.IO; |
3 | 4 | using System.Linq; |
4 | 5 | using System.Threading; |
5 | 6 | using ServiceStack.Logging; |
@@ -77,51 +78,77 @@ public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master) |
77 | 78 |
|
78 | 79 | if (master && RedisConfig.VerifyMasterConnections) |
79 | 80 | { |
80 | | - var role = client.GetServerRole(); |
81 | | - if (role != RedisServerRole.Master) |
| 81 | + var firstAttempt = DateTime.UtcNow; |
| 82 | + Exception firstEx = null; |
| 83 | + var retryTimeSpan = TimeSpan.FromMilliseconds(config.RetryTimeout); |
| 84 | + var i = 0; |
| 85 | + while (DateTime.UtcNow - firstAttempt < retryTimeSpan) |
82 | 86 | { |
83 | | - Interlocked.Increment(ref RedisState.TotalInvalidMasters); |
84 | | - log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role)); |
85 | | - var newMasters = new List<RedisEndpoint>(); |
86 | | - var newReplicas = new List<RedisEndpoint>(); |
87 | | - RedisClient masterClient = null; |
88 | | - foreach (var hostConfig in allHosts) |
| 87 | + try |
89 | 88 | { |
90 | | - try |
91 | | - { |
92 | | - var testClient = ClientFactory(hostConfig); |
93 | | - testClient.ConnectTimeout = RedisConfig.HostLookupTimeoutMs; |
94 | | - var testRole = testClient.GetServerRole(); |
95 | | - switch (testRole) |
96 | | - { |
97 | | - case RedisServerRole.Master: |
98 | | - newMasters.Add(hostConfig); |
99 | | - if (masterClient == null) |
100 | | - masterClient = testClient; |
101 | | - break; |
102 | | - case RedisServerRole.Slave: |
103 | | - newReplicas.Add(hostConfig); |
104 | | - break; |
105 | | - } |
106 | | - |
107 | | - } |
108 | | - catch { /* skip */ } |
| 89 | + client = GetValidMaster(client, config); |
| 90 | + return client; |
| 91 | + } |
| 92 | + catch (Exception ex) |
| 93 | + { |
| 94 | + if (!RedisConfig.RetryReconnectOnFailedMasters) |
| 95 | + throw; |
| 96 | + |
| 97 | + firstEx ??= ex; |
| 98 | + ExecUtils.SleepBackOffMultiplier(++i); |
109 | 99 | } |
| 100 | + } |
| 101 | + throw new TimeoutException($"Could not resolve master instance within {config.RetryTimeout}ms RetryTimeout", firstEx); |
| 102 | + } |
| 103 | + |
| 104 | + return client; |
| 105 | + } |
110 | 106 |
|
111 | | - if (masterClient == null) |
| 107 | + protected RedisClient GetValidMaster(RedisClient client, RedisEndpoint config) |
| 108 | + { |
| 109 | + var role = client.GetServerRole(); |
| 110 | + if (role != RedisServerRole.Master) |
| 111 | + { |
| 112 | + Interlocked.Increment(ref RedisState.TotalInvalidMasters); |
| 113 | + log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role)); |
| 114 | + var newMasters = new List<RedisEndpoint>(); |
| 115 | + var newReplicas = new List<RedisEndpoint>(); |
| 116 | + RedisClient masterClient = null; |
| 117 | + foreach (var hostConfig in allHosts) |
| 118 | + { |
| 119 | + try |
112 | 120 | { |
113 | | - Interlocked.Increment(ref RedisState.TotalNoMastersFound); |
114 | | - var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString())); |
115 | | - log.Error(errorMsg); |
116 | | - throw new Exception(errorMsg); |
| 121 | + var testClient = ClientFactory(hostConfig); |
| 122 | + testClient.ConnectTimeout = RedisConfig.HostLookupTimeoutMs; |
| 123 | + var testRole = testClient.GetServerRole(); |
| 124 | + switch (testRole) |
| 125 | + { |
| 126 | + case RedisServerRole.Master: |
| 127 | + newMasters.Add(hostConfig); |
| 128 | + if (masterClient == null) |
| 129 | + masterClient = testClient; |
| 130 | + break; |
| 131 | + case RedisServerRole.Slave: |
| 132 | + newReplicas.Add(hostConfig); |
| 133 | + break; |
| 134 | + } |
| 135 | + |
117 | 136 | } |
| 137 | + catch { /* skip */ } |
| 138 | + } |
118 | 139 |
|
119 | | - ResetMasters(newMasters); |
120 | | - ResetSlaves(newReplicas); |
121 | | - return masterClient; |
| 140 | + if (masterClient == null) |
| 141 | + { |
| 142 | + Interlocked.Increment(ref RedisState.TotalNoMastersFound); |
| 143 | + var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString())); |
| 144 | + log.Error(errorMsg); |
| 145 | + throw new InvalidDataException(errorMsg); |
122 | 146 | } |
123 | | - } |
124 | 147 |
|
| 148 | + ResetMasters(newMasters); |
| 149 | + ResetSlaves(newReplicas); |
| 150 | + return masterClient; |
| 151 | + } |
125 | 152 | return client; |
126 | 153 | } |
127 | 154 |
|
|
0 commit comments