This repository has been archived by the owner on Jul 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 360
/
BucketConfigListener.cs
276 lines (227 loc) · 7.86 KB
/
BucketConfigListener.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;
using System.Threading;
using System.Net;
using Enyim;
using Membase.Configuration;
namespace Membase
{
internal class BucketConfigListener
{
private static readonly Enyim.Caching.ILog log = Enyim.Caching.LogManager.GetLogger(typeof(BucketConfigListener));
private Uri[] poolUrls;
private string bucketName;
private NetworkCredential credential;
private int? lastHash;
private ManualResetEvent mre;
private MessageStreamListener listener;
public BucketConfigListener(Uri[] poolUrls, string bucketName, string bucketPassword)
{
this.poolUrls = poolUrls;
this.bucketName = String.IsNullOrEmpty(bucketName)
? "default"
: bucketName;
this.credential = bucketName == "default" || String.IsNullOrEmpty(bucketPassword)
? null
: new NetworkCredential(bucketName, bucketPassword);
this.Timeout = 10000;
this.DeadTimeout = 10000;
this.RetryCount = 0;
this.RetryTimeout = new TimeSpan(0, 0, 0, 0, 500);
}
/// <summary>
/// Connection timeout in milliseconds for connecting the pool.
/// </summary>
public int Timeout { get; set; }
public int RetryCount { get; set; }
public TimeSpan RetryTimeout { get; set; }
/// <summary>
/// Time to wait in milliseconds to reconnect to the pool when all nodes are down.
/// </summary>
public int DeadTimeout { get; set; }
/// <summary>
/// Raised when the pool's configuration changes.
/// </summary>
public event Action<ClusterConfig> ClusterConfigChanged;
/// <summary>
/// Starts listening for configuration data. This method blocks until the initial configuration is received. (Or until all pool urls fail.)
/// </summary>
public void Start()
{
var reset = this.mre = new ManualResetEvent(false);
// subscribe to the config url
this.listener = this.GetPooledListener();
// this will be signaled by the config changed event handler
reset.WaitOne();
// set to null, then dispose, so RaiseConfigChanged will not
// fail at Set when the config changes while we're cleaning up here
this.mre = null;
((IDisposable)reset).Dispose();
}
public void Stop()
{
this.ReleaseListener(this.listener);
this.listener = null;
}
private static readonly JavaScriptConverter[] KnownConverters = { ClusterNode.ConverterInstance };
private void HandleMessage(string message)
{
// everything failed
if (String.IsNullOrEmpty(message))
{
this.lastHash = null;
this.RaiseConfigChanged(null);
return;
}
// deserialize the buckets
var jss = new JavaScriptSerializer();
jss.RegisterConverters(KnownConverters);
var config = jss.Deserialize<ClusterConfig>(message);
// check if the config is the same as the previous
// we cannot compare the messages because they have more information than we deserialize from them
var configHash = config.GetHashCode();
if (lastHash != configHash)
{
lastHash = configHash;
this.RaiseConfigChanged(config);
}
else if (log.IsDebugEnabled)
log.Debug("Last message was the same as current, ignoring.");
}
private void RaiseConfigChanged(ClusterConfig config)
{
var ccc = this.ClusterConfigChanged;
// we got a new config, notify the pool to reload itself
if (ccc != null)
ccc(config);
// trigger the event so Start stops blocking
if (this.mre != null)
this.mre.Set();
}
#region [ message listener pooling ]
private static readonly object ListenerSync = new Object();
// we pool and refcount the listeners here so we can safely dispose them when all clients are destroyed
private static Dictionary<int, MessageStreamListener> listeners = new Dictionary<int, MessageStreamListener>();
private static Dictionary<MessageStreamListener, ListenerInfo> listenerRefs = new Dictionary<MessageStreamListener, ListenerInfo>();
private class ListenerInfo
{
public int RefCount;
public int HashKey;
}
/// <summary>
/// Unsubscibes from a pooled listener, and destrpys it if no additionals subscribers are present.
/// </summary>
/// <param name="listener"></param>
private void ReleaseListener(MessageStreamListener listener)
{
lock (ListenerSync)
{
listener.Unsubscribe(this.HandleMessage);
var info = listenerRefs[listener];
if (info.RefCount == 1)
{
listenerRefs.Remove(listener);
listeners.Remove(info.HashKey);
try { using (listener) listener.Stop(); }
catch { }
}
else
{
info.RefCount--;
}
}
}
/// <summary>
/// Returns a MessageStreamListener instance based on this instance's configuratino (timeout, bucket name etc.)
///
/// When multiple listeners are requested with the exact same parameters (usually when multiple clients are instantiated from the same configuration),
/// the same listener will be returned each time.
/// </summary>
/// <returns></returns>
private MessageStreamListener GetPooledListener()
{
// create a unique key based on the parameters
// to find out if we already have a listener attached to this pool
var hcc = new HashCodeCombiner();
hcc.Add(this.Timeout);
hcc.Add(this.DeadTimeout);
hcc.Add(this.RetryCount);
hcc.Add(this.RetryTimeout.GetHashCode());
hcc.Add(this.bucketName.GetHashCode());
if (credential != null)
{
hcc.Add((this.credential.UserName ?? String.Empty).GetHashCode());
hcc.Add((this.credential.Password ?? String.Empty).GetHashCode());
hcc.Add((this.credential.Domain ?? String.Empty).GetHashCode());
}
for (var i = 0; i < this.poolUrls.Length; i++)
hcc.Add(this.poolUrls[i].GetHashCode());
var hash = hcc.CurrentHash;
MessageStreamListener retval;
lock (ListenerSync)
if (listeners.TryGetValue(hash, out retval))
{
listenerRefs[retval].RefCount++;
retval.Subscribe(this.HandleMessage);
}
else
{
var name = this.bucketName;
// create a new listener for the pool urls
retval = new MessageStreamListener(poolUrls, (client, root) => ResolveBucketUri(client, root, name));
retval.ConnectionTimeout = this.Timeout;
retval.DeadTimeout = this.DeadTimeout;
retval.Credentials = this.credential;
retval.RetryCount = this.RetryCount;
retval.RetryTimeout = this.RetryTimeout;
retval.Subscribe(this.HandleMessage);
listeners[hash] = retval;
listenerRefs[retval] = new ListenerInfo { RefCount = 1, HashKey = hash };
retval.Start();
}
return retval;
}
private static Uri ResolveBucketUri(WebClientWithTimeout client, Uri root, string bucketName)
{
try
{
var bucket = ConfigHelper.ResolveBucket(client, root, bucketName);
if (bucket == null)
return null;
if (String.IsNullOrEmpty(bucket.streamingUri))
{
log.ErrorFormat("Url {0} for bucket {1} returned a config with no streamingUri", root, bucketName);
return null;
}
return new Uri(root, bucket.streamingUri);
}
catch (Exception e)
{
log.Error("Error resolving streaming uri: " + root, e);
return null;
}
}
#endregion
}
}
#region [ License information ]
/* ************************************************************
*
* Copyright (c) 2010 Attila Kiskó, enyim.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ************************************************************/
#endregion