forked from ServiceStack/ServiceStack.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionUtils.cs
More file actions
449 lines (416 loc) · 21.1 KB
/
ConnectionUtils.cs
File metadata and controls
449 lines (416 loc) · 21.1 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//using System;
//using System.Collections.Generic;
//using System.Collections.Specialized;
//using System.IO;
//using System.Linq;
//using System.Text;
//namespace ServiceStack.Redis
//{
// /// <summary>
// /// Provides utility methods for managing connections to multiple (master/slave) redis servers (with the same
// /// information - not sharding).
// /// </summary>
// public static class ConnectionUtils
// {
// /// <summary>
// /// Inspect the provided configration, and connect to the available servers to report which server is the preferred/active node.
// /// </summary>
// public static string SelectConfiguration(string configuration, out string[] availableEndpoints, TextWriter log = null)
// {
// string selected;
// using (SelectAndCreateConnection(configuration, log, out selected, out availableEndpoints, false)) { }
// return selected;
// }
// /// <summary>
// /// Inspect the provided configration, and connect to the preferred/active node after checking what nodes are available.
// /// </summary>
// public static RedisConnection Connect(string configuration, TextWriter log = null)
// {
// string selectedConfiguration;
// string[] availableEndpoints;
// return SelectAndCreateConnection(configuration, log, out selectedConfiguration, out availableEndpoints, true);
// }
// /// <summary>
// /// Subscribe to perform some operation when a change to the preferred/active node is broadcast.
// /// </summary>
// public static void SubscribeToMasterSwitch(RedisSubscriberConnection connection, Action<string> handler)
// {
// if (connection == null) throw new ArgumentNullException("connection");
// if (handler == null) throw new ArgumentNullException("handler");
// connection.Subscribe(RedisMasterChangedChannel, (channel, message) => handler(Encoding.UTF8.GetString(message)));
// }
// /// <summary>
// /// Using the configuration available, and after checking which nodes are available, switch the master node and broadcast this change.
// /// </summary>
// public static void SwitchMaster(string configuration, string newMaster, TextWriter log = null)
// {
// string newConfig;
// string[] availableEndpoints;
// SelectAndCreateConnection(configuration, log, out newConfig, out availableEndpoints, false, newMaster);
// }
// const string RedisMasterChangedChannel = "__Booksleeve_MasterChanged", TieBreakerKey = "__Booksleeve_TieBreak";
// /// <summary>
// /// Prompt all clients to reconnect.
// /// </summary>
// public static void BroadcastReconnectMessage(RedisConnection connection)
// {
// if (connection == null) throw new ArgumentNullException("connection");
// connection.Wait(connection.Publish(RedisMasterChangedChannel, "*"));
// }
// private static RedisConnection SelectWithTieBreak(TextWriter log, List<RedisConnection> nodes, Dictionary<string, int> tiebreakers)
// {
// if (nodes.Count == 0) return null;
// if (nodes.Count == 1) return nodes[0];
// Func<string, int> valueOrDefault = key =>
// {
// int tmp;
// if (!tiebreakers.TryGetValue(key, out tmp)) tmp = 0;
// return tmp;
// };
// var tuples = (from node in nodes
// let key = node.Host + ":" + node.Port
// let count = valueOrDefault(key)
// select new { Node = node, Key = key, Count = count }).ToList();
// // check for uncontested scenario
// int contenderCount = tuples.Count(x => x.Count > 0);
// switch (contenderCount)
// {
// case 0:
// log.WriteLine("No tie-break contenders; selecting arbitrary node");
// return tuples[0].Node;
// case 1:
// log.WriteLine("Unaminous tie-break winner");
// return tuples.Single(x => x.Count > 0).Node;
// }
// // contested
// int maxCount = tuples.Max(x => x.Count);
// var competing = tuples.Where(x => x.Count == maxCount).ToList();
// switch (competing.Count)
// {
// case 0:
// return null; // impossible, but never rely on the impossible not happening ;p
// case 1:
// log.WriteLine("Contested, but clear, tie-break winner");
// break;
// default:
// log.WriteLine("Contested and ambiguous tie-break; selecting arbitrary node");
// break;
// }
// return competing[0].Node;
// }
// private static string[] GetConfigurationOptions(string configuration, out int syncTimeout, out bool allowAdmin)
// {
// syncTimeout = 1000;
// allowAdmin = false;
// // break it down by commas
// var arr = configuration.Split(',');
// var options = new List<string>();
// foreach (var option in arr)
// {
// var trimmed = option.Trim();
// if (trimmed.IsNullOrWhiteSpace() || options.Contains(trimmed)) continue;
// // check for special tokens
// int idx = trimmed.IndexOf('=');
// if (idx > 0)
// {
// if (option.StartsWith(SyncTimeoutPrefix))
// {
// int tmp;
// if (int.TryParse(option.Substring(idx + 1), out tmp)) syncTimeout = tmp;
// continue;
// }
// if (option.StartsWith(AllowAdminPrefix))
// {
// bool tmp;
// if (bool.TryParse(option.Substring(idx + 1), out tmp)) allowAdmin = tmp;
// continue;
// }
// }
// options.Add(trimmed);
// }
// return options.ToArray();
// }
// internal const string AllowAdminPrefix = "allowAdmin=", SyncTimeoutPrefix = "syncTimeout=";
// private static RedisConnection SelectAndCreateConnection(string configuration, TextWriter log, out string selectedConfiguration, out string[] availableEndpoints, bool autoMaster, string newMaster = null)
// {
// int syncTimeout;
// bool allowAdmin;
// if (log == null) log = new StringWriter();
// var arr = GetConfigurationOptions(configuration, out syncTimeout, out allowAdmin);
// if (!newMaster.IsNullOrWhiteSpace()) allowAdmin = true; // need this to diddle the slave/master config
// log.WriteLine("{0} unique nodes specified", arr.Length);
// log.WriteLine("sync timeout: {0}ms, admin commands: {1}", syncTimeout,
// allowAdmin ? "enabled" : "disabled");
// if (arr.Length == 0)
// {
// log.WriteLine("No nodes to consider");
// selectedConfiguration = null;
// availableEndpoints = new string[0];
// return null;
// }
// var connections = new List<RedisConnection>(arr.Length);
// RedisConnection preferred = null;
// try
// {
// var infos = new List<Task<string>>(arr.Length);
// var tiebreakers = new List<Task<string>>(arr.Length);
// foreach (var option in arr)
// {
// if (option.IsNullOrWhiteSpace()) continue;
// RedisConnection conn = null;
// try
// {
// var parts = option.Split(':');
// if (parts.Length == 0) continue;
// string host = parts[0].Trim();
// int port = 6379, tmp;
// if (parts.Length > 1 && int.TryParse(parts[1].Trim(), out tmp)) port = tmp;
// conn = new RedisConnection(host, port, syncTimeout: syncTimeout, allowAdmin: allowAdmin);
// log.WriteLine("Opening connection to {0}:{1}...", host, port);
// conn.Open();
// var info = conn.GetInfo();
// var tiebreak = conn.Strings.GetString(0, TieBreakerKey);
// connections.Add(conn);
// infos.Add(info);
// tiebreakers.Add(tiebreak);
// }
// catch (Exception ex)
// {
// if (conn == null)
// {
// log.WriteLine("Error parsing option \"{0}\": {1}", option, ex.Message);
// }
// else
// {
// log.WriteLine("Error connecting: {0}", ex.Message);
// }
// }
// }
// List<RedisConnection> masters = new List<RedisConnection>(), slaves = new List<RedisConnection>();
// var breakerScores = new Dictionary<string, int>();
// foreach (var tiebreak in tiebreakers)
// {
// try
// {
// if (tiebreak.Wait(syncTimeout))
// {
// string key = tiebreak.Result;
// if (key.IsNullOrWhiteSpace()) continue;
// int score;
// if (breakerScores.TryGetValue(key, out score)) breakerScores[key] = score + 1;
// else breakerScores.Add(key, 1);
// }
// }
// catch { /* if a node is down, that's fine too */ }
// }
// // check for tie-breakers (i.e. when we store which is the master)
// switch (breakerScores.Count)
// {
// case 0:
// log.WriteLine("No tie-breakers found");
// break;
// case 1:
// log.WriteLine("Tie-breaker is unanimous: {0}", breakerScores.Keys.Single());
// break;
// default:
// log.WriteLine("Ambiguous tie-breakers:");
// foreach (var kvp in breakerScores.OrderByDescending(x => x.Value))
// {
// log.WriteLine("\t{0}: {1}", kvp.Key, kvp.Value);
// }
// break;
// }
// for (int i = 0; i < connections.Count; i++)
// {
// log.WriteLine("Reading configuration from {0}:{1}...", connections[i].Host, connections[i].Port);
// try
// {
// if (!infos[i].Wait(syncTimeout))
// {
// log.WriteLine("\tTimeout fetching INFO");
// continue;
// }
// var infoPairs = new StringDictionary();
// using (var sr = new StringReader(infos[i].Result))
// {
// string line;
// while ((line = sr.ReadLine()) != null)
// {
// int idx = line.IndexOf(':');
// if (idx < 0) continue;
// string key = line.Substring(0, idx).Trim(),
// value = line.Substring(idx + 1, line.Length - (idx + 1)).Trim();
// infoPairs[key] = value;
// }
// }
// string role = infoPairs["role"];
// switch (role)
// {
// case "slave":
// log.WriteLine("\tServer is SLAVE of {0}:{1}",
// infoPairs["master_host"], infoPairs["master_port"]);
// log.Write("\tLink is {0}, seen {1} seconds ago",
// infoPairs["master_link_status"], infoPairs["master_last_io_seconds_ago"]);
// if (infoPairs["master_sync_in_progress"] == "1") log.Write(" (sync is in progress)");
// log.WriteLine();
// slaves.Add(connections[i]);
// break;
// case "master":
// log.WriteLine("\tServer is MASTER, with {0} slaves", infoPairs["connected_slaves"]);
// masters.Add(connections[i]);
// break;
// default:
// log.WriteLine("\tUnknown role: {0}", role);
// break;
// }
// string tmp = infoPairs["connected_clients"];
// int clientCount, channelCount, patternCount;
// if (tmp.IsNullOrWhiteSpace() || !int.TryParse(tmp, out clientCount)) clientCount = -1;
// tmp = infoPairs["pubsub_channels"];
// if (tmp.IsNullOrWhiteSpace(tmp) || !int.TryParse(tmp, out channelCount)) channelCount = -1;
// tmp = infoPairs["pubsub_patterns"];
// if (tmp.IsNullOrWhiteSpace(tmp) || !int.TryParse(tmp, out patternCount)) patternCount = -1;
// log.WriteLine("\tClients: {0}; channels: {1}; patterns: {2}", clientCount, channelCount, patternCount);
// }
// catch (Exception ex)
// {
// log.WriteLine("\tError reading INFO results: {0}", ex.Message);
// }
// }
// if (newMaster == null)
// {
// switch (masters.Count)
// {
// case 0:
// switch (slaves.Count)
// {
// case 0:
// log.WriteLine("No masters or slaves found");
// break;
// case 1:
// log.WriteLine("No masters found; selecting single slave");
// preferred = slaves[0];
// break;
// default:
// log.WriteLine("No masters found; considering {0} slaves...", slaves.Count);
// preferred = SelectWithTieBreak(log, slaves, breakerScores);
// break;
// }
// if (preferred != null)
// {
// if (autoMaster)
// {
// //LogException("Promoting redis SLAVE to MASTER");
// log.WriteLine("Promoting slave to master...");
// if (allowAdmin)
// { // can do on this connection
// preferred.Wait(preferred.Server.MakeMaster());
// }
// else
// { // need an admin connection for this
// using (var adminPreferred = new RedisConnection(preferred.Host, preferred.Port, allowAdmin: true, syncTimeout: syncTimeout))
// {
// adminPreferred.Open();
// adminPreferred.Wait(adminPreferred.Server.MakeMaster());
// }
// }
// }
// else
// {
// log.WriteLine("Slave should be promoted to master (but not done yet)...");
// }
// }
// break;
// case 1:
// log.WriteLine("One master found; selecting");
// preferred = masters[0];
// break;
// default:
// log.WriteLine("Considering {0} masters...", masters.Count);
// preferred = SelectWithTieBreak(log, masters, breakerScores);
// break;
// }
// }
// else
// { // we have been instructed to change master server
// preferred = masters.Concat(slaves).FirstOrDefault(conn => (conn.Host + ":" + conn.Port) == newMaster);
// if (preferred == null)
// {
// log.WriteLine("Selected new master not available: {0}", newMaster);
// }
// else
// {
// int errorCount = 0;
// try
// {
// log.WriteLine("Promoting to master: {0}:{1}...", preferred.Host, preferred.Port);
// preferred.Wait(preferred.Server.MakeMaster());
// preferred.Strings.Set(0, TieBreakerKey, newMaster);
// preferred.Wait(preferred.Publish(RedisMasterChangedChannel, newMaster));
// }
// catch (Exception ex)
// {
// log.WriteLine("\t{0}", ex.Message);
// errorCount++;
// }
// if (errorCount == 0) // only make slaves if the master was happy
// {
// foreach (var conn in masters.Concat(slaves))
// {
// if (conn == preferred) continue; // can't make self a slave!
// try
// {
// log.WriteLine("Enslaving: {0}:{1}...", conn.Host, conn.Port);
// // set the tie-breaker **first** in case of problems
// conn.Strings.Set(0, TieBreakerKey, newMaster);
// // and broadcast to anyone who thinks this is the master
// conn.Publish(RedisMasterChangedChannel, newMaster);
// // now make it a slave
// conn.Wait(conn.Server.MakeSlave(preferred.Host, preferred.Port));
// }
// catch (Exception ex)
// {
// log.WriteLine("\t{0}", ex.Message);
// errorCount++;
// }
// }
// }
// if (errorCount != 0)
// {
// log.WriteLine("Things didn't go smoothly; CHECK WHAT HAPPENED!");
// }
// // want the connection disposed etc
// preferred = null;
// }
// }
// if (preferred == null)
// {
// selectedConfiguration = null;
// }
// else
// {
// selectedConfiguration = preferred.Host + ":" + preferred.Port;
// log.WriteLine("Selected server {0}", selectedConfiguration);
// }
// availableEndpoints = (from conn in masters.Concat(slaves)
// select conn.Host + ":" + conn.Port).ToArray();
// return preferred;
// }
// finally
// {
// foreach (var conn in connections)
// {
// if (conn != null && conn != preferred) try { conn.Dispose(); }
// catch { }
// }
// }
// }
// }
// public static class ConnectionUtilsExtensions
// {
// public static bool IsNullOrWhiteSpace(this string str)
// {
// return str == null || str.Trim().Length == 0;
// }
// }
//}