forked from ServiceStack/ServiceStack.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisResolver.cs
More file actions
156 lines (129 loc) · 5.58 KB
/
RedisResolver.cs
File metadata and controls
156 lines (129 loc) · 5.58 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using ServiceStack.Logging;
using ServiceStack.Text;
namespace ServiceStack.Redis
{
public class RedisResolver : IRedisResolver, IRedisResolverExtended
{
static ILog log = LogManager.GetLogger(typeof(RedisResolver));
public Func<RedisEndpoint, RedisClient> ClientFactory { get; set; }
public int ReadWriteHostsCount { get; private set; }
public int ReadOnlyHostsCount { get; private set; }
HashSet<RedisEndpoint> allHosts = new HashSet<RedisEndpoint>();
private RedisEndpoint[] masters;
private RedisEndpoint[] slaves;
public RedisEndpoint[] Masters
{
get { return masters; }
}
public RedisEndpoint[] Slaves
{
get { return slaves; }
}
public RedisResolver()
: this(TypeConstants<RedisEndpoint>.EmptyArray, TypeConstants<RedisEndpoint>.EmptyArray) {}
public RedisResolver(IEnumerable<string> masters, IEnumerable<string> slaves)
: this(masters.ToRedisEndPoints(), slaves.ToRedisEndPoints()){}
public RedisResolver(IEnumerable<RedisEndpoint> masters, IEnumerable<RedisEndpoint> slaves)
{
ResetMasters(masters.ToList());
ResetSlaves(slaves.ToList());
ClientFactory = RedisConfig.ClientFactory;
}
public virtual void ResetMasters(IEnumerable<string> hosts)
{
ResetMasters(hosts.ToRedisEndPoints());
}
public virtual void ResetMasters(List<RedisEndpoint> newMasters)
{
if (newMasters == null || newMasters.Count == 0)
throw new Exception("Must provide at least 1 master");
masters = newMasters.ToArray();
ReadWriteHostsCount = masters.Length;
newMasters.Each(x => allHosts.Add(x));
if (log.IsDebugEnabled)
log.Debug("New Redis Masters: " + string.Join(", ", masters.Map(x => x.GetHostString())));
}
public virtual void ResetSlaves(IEnumerable<string> hosts)
{
ResetSlaves(hosts.ToRedisEndPoints());
}
public virtual void ResetSlaves(List<RedisEndpoint> newSlaves)
{
slaves = (newSlaves ?? new List<RedisEndpoint>()).ToArray();
ReadOnlyHostsCount = slaves.Length;
newSlaves.Each(x => allHosts.Add(x));
if (log.IsDebugEnabled)
log.Debug("New Redis Slaves: " + string.Join(", ", slaves.Map(x => x.GetHostString())));
}
public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
{
var client = ClientFactory(config);
if (master && RedisConfig.VerifyMasterConnections)
{
var role = client.GetServerRole();
if (role != RedisServerRole.Master)
{
Interlocked.Increment(ref RedisState.TotalInvalidMasters);
log.Error("Redis Master Host '{0}' is {1}. Resetting allHosts...".Fmt(config.GetHostString(), role));
var newMasters = new List<RedisEndpoint>();
var newSlaves = new List<RedisEndpoint>();
RedisClient masterClient = null;
foreach (var hostConfig in allHosts)
{
try
{
var testClient = ClientFactory(hostConfig);
testClient.ConnectTimeout = RedisConfig.HostLookupTimeoutMs;
var testRole = testClient.GetServerRole();
switch (testRole)
{
case RedisServerRole.Master:
newMasters.Add(hostConfig);
if (masterClient == null)
masterClient = testClient;
break;
case RedisServerRole.Slave:
newSlaves.Add(hostConfig);
break;
}
}
catch { /* skip */ }
}
if (masterClient == null)
{
Interlocked.Increment(ref RedisState.TotalNoMastersFound);
var errorMsg = "No master found in: " + string.Join(", ", allHosts.Map(x => x.GetHostString()));
log.Error(errorMsg);
throw new Exception(errorMsg);
}
ResetMasters(newMasters);
ResetSlaves(newSlaves);
return masterClient;
}
}
return client;
}
public RedisEndpoint GetReadWriteHost(int desiredIndex)
{
return masters[desiredIndex % masters.Length];
}
public RedisEndpoint GetReadOnlyHost(int desiredIndex)
{
return ReadOnlyHostsCount > 0
? slaves[desiredIndex % slaves.Length]
: GetReadWriteHost(desiredIndex);
}
public RedisClient CreateMasterClient(int desiredIndex)
{
return CreateRedisClient(GetReadWriteHost(desiredIndex), master: true);
}
public RedisClient CreateSlaveClient(int desiredIndex)
{
return CreateRedisClient(GetReadOnlyHost(desiredIndex), master: false);
}
}
}