|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading; |
| 6 | + |
| 7 | +namespace Enyim.Caching.Memcached |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// This is a ketama-like consistent hashing based node locator. Used when no other <see cref="T:IMemcachedNodeLocator"/> is specified for the pool. |
| 11 | + /// </summary> |
| 12 | + public sealed class LegacyNodeLocator : IMemcachedNodeLocator, IDisposable |
| 13 | + { |
| 14 | + private readonly int _serverAddressMutations; |
| 15 | + |
| 16 | + // holds all server keys for mapping an item key to the server consistently |
| 17 | + private uint[] _keys; |
| 18 | + // used to lookup a server based on its key |
| 19 | + private Dictionary<uint, IMemcachedNode> _servers; |
| 20 | + private Dictionary<IMemcachedNode, bool> _deadServers; |
| 21 | + private List<IMemcachedNode> _allServers; |
| 22 | + private ReaderWriterLockSlim _serverAccessLock; |
| 23 | + |
| 24 | + public LegacyNodeLocator() : this(100) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + public LegacyNodeLocator(int serverAddressMutations) |
| 29 | + { |
| 30 | + _servers = new Dictionary<uint, IMemcachedNode>(new UIntEqualityComparer()); |
| 31 | + _deadServers = new Dictionary<IMemcachedNode, bool>(); |
| 32 | + _allServers = new List<IMemcachedNode>(); |
| 33 | + _serverAccessLock = new ReaderWriterLockSlim(); |
| 34 | + _serverAddressMutations = serverAddressMutations; |
| 35 | + } |
| 36 | + |
| 37 | + private void BuildIndex(List<IMemcachedNode> nodes) |
| 38 | + { |
| 39 | + var keys = new uint[nodes.Count * _serverAddressMutations]; |
| 40 | + |
| 41 | + int nodeIdx = 0; |
| 42 | + |
| 43 | + foreach (IMemcachedNode node in nodes) |
| 44 | + { |
| 45 | + var tmpKeys = GenerateKeys(node, _serverAddressMutations); |
| 46 | + |
| 47 | + for (var i = 0; i < tmpKeys.Length; i++) |
| 48 | + { |
| 49 | + _servers[tmpKeys[i]] = node; |
| 50 | + } |
| 51 | + |
| 52 | + tmpKeys.CopyTo(keys, nodeIdx); |
| 53 | + nodeIdx += _serverAddressMutations; |
| 54 | + } |
| 55 | + |
| 56 | + Array.Sort<uint>(keys); |
| 57 | + Interlocked.Exchange(ref _keys, keys); |
| 58 | + } |
| 59 | + |
| 60 | + void IMemcachedNodeLocator.Initialize(IList<IMemcachedNode> nodes) |
| 61 | + { |
| 62 | + _serverAccessLock.EnterWriteLock(); |
| 63 | + |
| 64 | + try |
| 65 | + { |
| 66 | + _allServers = nodes.ToList(); |
| 67 | + BuildIndex(_allServers); |
| 68 | + } |
| 69 | + finally |
| 70 | + { |
| 71 | + _serverAccessLock.ExitWriteLock(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + IMemcachedNode IMemcachedNodeLocator.Locate(string key) |
| 76 | + { |
| 77 | + if (key == null) throw new ArgumentNullException("key"); |
| 78 | + |
| 79 | + _serverAccessLock.EnterUpgradeableReadLock(); |
| 80 | + |
| 81 | + try { return Locate(key); } |
| 82 | + finally { _serverAccessLock.ExitUpgradeableReadLock(); } |
| 83 | + } |
| 84 | + |
| 85 | + IEnumerable<IMemcachedNode> IMemcachedNodeLocator.GetWorkingNodes() |
| 86 | + { |
| 87 | + _serverAccessLock.EnterReadLock(); |
| 88 | + |
| 89 | + try { return _allServers.Except(_deadServers.Keys).ToArray(); } |
| 90 | + finally { _serverAccessLock.ExitReadLock(); } |
| 91 | + } |
| 92 | + |
| 93 | + private IMemcachedNode Locate(string key) |
| 94 | + { |
| 95 | + var node = FindNode(key); |
| 96 | + if (node == null || node.IsAlive) |
| 97 | + return node; |
| 98 | + |
| 99 | + // move the current node to the dead list and rebuild the indexes |
| 100 | + _serverAccessLock.EnterWriteLock(); |
| 101 | + |
| 102 | + try |
| 103 | + { |
| 104 | + // check if it's still dead or it came back |
| 105 | + // while waiting for the write lock |
| 106 | + if (!node.IsAlive) |
| 107 | + _deadServers[node] = true; |
| 108 | + |
| 109 | + BuildIndex(_allServers.Except(_deadServers.Keys).ToList()); |
| 110 | + } |
| 111 | + finally |
| 112 | + { |
| 113 | + _serverAccessLock.ExitWriteLock(); |
| 114 | + } |
| 115 | + |
| 116 | + // try again with the dead server removed from the lists |
| 117 | + return Locate(key); |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// locates a node by its key |
| 122 | + /// </summary> |
| 123 | + /// <param name="key"></param> |
| 124 | + /// <returns></returns> |
| 125 | + private IMemcachedNode FindNode(string key) |
| 126 | + { |
| 127 | + if (_keys.Length == 0) return null; |
| 128 | + |
| 129 | + uint itemKeyHash = BitConverter.ToUInt32(new FNV1a(false).ComputeHash(Encoding.UTF8.GetBytes(key)), 0); |
| 130 | + // get the index of the server assigned to this hash |
| 131 | + int foundIndex = Array.BinarySearch<uint>(_keys, itemKeyHash); |
| 132 | + |
| 133 | + // no exact match |
| 134 | + if (foundIndex < 0) |
| 135 | + { |
| 136 | + // this is the nearest server in the list |
| 137 | + foundIndex = ~foundIndex; |
| 138 | + |
| 139 | + if (foundIndex == 0) |
| 140 | + { |
| 141 | + // it's smaller than everything, so use the last server (with the highest key) |
| 142 | + foundIndex = _keys.Length - 1; |
| 143 | + } |
| 144 | + else if (foundIndex >= _keys.Length) |
| 145 | + { |
| 146 | + // the key was larger than all server keys, so return the first server |
| 147 | + foundIndex = 0; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if (foundIndex < 0 || foundIndex > _keys.Length) |
| 152 | + return null; |
| 153 | + |
| 154 | + return _servers[_keys[foundIndex]]; |
| 155 | + } |
| 156 | + |
| 157 | + private static uint[] GenerateKeys(IMemcachedNode node, int numberOfKeys) |
| 158 | + { |
| 159 | + const int KeyLength = 4; |
| 160 | + const int PartCount = 1; // (ModifiedFNV.HashSize / 8) / KeyLength; // HashSize is in bits, uint is 4 byte long |
| 161 | + |
| 162 | + var k = new uint[PartCount * numberOfKeys]; |
| 163 | + |
| 164 | + // every server is registered numberOfKeys times |
| 165 | + // using UInt32s generated from the different parts of the hash |
| 166 | + // i.e. hash is 64 bit: |
| 167 | + // 00 00 aa bb 00 00 cc dd |
| 168 | + // server will be stored with keys 0x0000aabb & 0x0000ccdd |
| 169 | + // (or a bit differently based on the little/big indianness of the host) |
| 170 | + string address = node.EndPoint.ToString(); |
| 171 | + var fnv = new FNV1a(false); |
| 172 | + |
| 173 | + for (int i = 0; i < numberOfKeys; i++) |
| 174 | + { |
| 175 | + byte[] data = fnv.ComputeHash(Encoding.UTF8.GetBytes(string.Concat(address, "-", i))); |
| 176 | + |
| 177 | + for (int h = 0; h < PartCount; h++) |
| 178 | + { |
| 179 | + k[i * PartCount + h] = BitConverter.ToUInt32(data, h * KeyLength); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return k; |
| 184 | + } |
| 185 | + |
| 186 | + #region [ IDisposable ] |
| 187 | + |
| 188 | + void IDisposable.Dispose() |
| 189 | + { |
| 190 | + using (_serverAccessLock) |
| 191 | + { |
| 192 | + _serverAccessLock.EnterWriteLock(); |
| 193 | + |
| 194 | + try |
| 195 | + { |
| 196 | + // kill all pending operations (with an exception) |
| 197 | + // it's not nice, but disposeing an instance while being used is bad practice |
| 198 | + _allServers = null; |
| 199 | + _servers = null; |
| 200 | + _keys = null; |
| 201 | + _deadServers = null; |
| 202 | + } |
| 203 | + finally |
| 204 | + { |
| 205 | + _serverAccessLock.ExitWriteLock(); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + _serverAccessLock = null; |
| 210 | + } |
| 211 | + |
| 212 | + #endregion |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +#region [ License information ] |
| 217 | +/* ************************************************************ |
| 218 | + * |
| 219 | + * Copyright (c) 2010 Attila Kisk? enyim.com |
| 220 | + * |
| 221 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 222 | + * you may not use this file except in compliance with the License. |
| 223 | + * You may obtain a copy of the License at |
| 224 | + * |
| 225 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 226 | + * |
| 227 | + * Unless required by applicable law or agreed to in writing, software |
| 228 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 229 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 230 | + * See the License for the specific language governing permissions and |
| 231 | + * limitations under the License. |
| 232 | + * |
| 233 | + * ************************************************************/ |
| 234 | +#endregion |
0 commit comments