/// <summary> /// Serialize NewHashmap /// </summary> /// <param name="instance"></param> /// <param name="serializationContext">Serialization context used to serialize the object</param> public static void Serialize(NewHashmap instance, string serializationContext, bool updateClientMap) { Hashtable mapInfo = null; if (instance != null) { mapInfo = new Hashtable(); mapInfo.Add("ViewId", instance._lastViewId); mapInfo.Add("Members", instance._members); mapInfo.Add("Map", instance._map); mapInfo.Add("UpdateMap", updateClientMap); BinaryFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); formatter.Serialize(stream, mapInfo); instance._buffer = stream.ToArray(); } }
/// <summary> /// Deserialize NewHashmap /// </summary> /// <param name="serializationContext"></param> /// <returns></returns> public static NewHashmap Deserialize(byte[] buffer, string serializationContext) { NewHashmap hashmap = null; if (buffer != null && buffer.Length > 0) { BinaryFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(buffer); Hashtable map = formatter.Deserialize(stream) as Hashtable; if (map != null) { hashmap = new NewHashmap(); hashmap._lastViewId = (long)map["ViewId"]; hashmap._members = (ArrayList)map["Members"]; hashmap._map = (Hashtable)map["Map"]; hashmap._updateMap = (map["UpdateMap"] != null) ? (bool)map["UpdateMap"] : false; } } return(hashmap); }
public HashmapChangedEvent(string cacheId, string clientId, NewHashmap newHashmap) { this._cacheId = cacheId; this._clientId = clientId; this._newmap = newHashmap; }
/// <summary> /// Deserialize NewHashmap /// </summary> /// <param name="serializationContext"></param> /// <returns></returns> public static NewHashmap Deserialize(byte[] buffer, string serializationContext) { NewHashmap hashmap = null; if (buffer != null && buffer.Length > 0) { BinaryFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(buffer); Hashtable map = formatter.Deserialize(stream) as Hashtable; if (map != null) { hashmap = new NewHashmap(); hashmap._lastViewId = (long)map["ViewId"]; hashmap._members = (ArrayList)map["Members"]; hashmap._map = (Hashtable)map["Map"]; hashmap._updateMap = (map["UpdateMap"] != null) ? (bool)map["UpdateMap"] : false; } } return hashmap; }
/// <summary> /// Fire when hasmap changes when /// - new node joins /// - node leaves /// - manual/automatic load balance /// </summary> /// <param name="newHashmap">new hashmap</param> void ICacheEventsListener.OnHashmapChanged(NewHashmap newHashmap, bool updateClientMap) { }
void ICacheEventsListener.OnHashmapChanged(Alachisoft.NCache.Common.DataStructures.NewHashmap newHashmap, bool updateClientMap) { throw new Exception("The method or operation is not implemented."); }
private void HashmapChanged(NewHashmap newmap, EventContext eventContext) { lock (ConnectionManager.CallbackQueue) { if (_client != null) { ConnectionManager.CallbackQueue.Enqueue(new HashmapChangedEvent(_cacheId, _client.ClientID, newmap)); Monitor.Pulse(ConnectionManager.CallbackQueue); } } }
public AsyncLocalNotifyHashmapCallback(ICacheEventsListener listener, long lastViewid, Hashtable newmap, ArrayList members, bool updateClientMap) { this._listener = listener; this._hashMap = new NewHashmap(lastViewid, newmap, members); this._updateClientMap = updateClientMap; }
/// <summary> /// New hashmap recieved. Depending on new and old hashmap, some connections are /// disposed and some new connections are formed(not always as in some cases only buckets have /// transfered between servers). This method should be called asynchronously so the recieve thread /// will be free to recieve other command responces. /// </summary> /// <param name="newHashmap">new hashmap returned from primary server</param> private void NewHashmapRecieved(NewHashmap newHashmap) { if (newHashmap == null) { _logger.NCacheLog.CriticalInfo("Broker.NewHashmapReceived", "Hashmap is null... returning"); return; } lock (_hashmapUpdateMutex) { try { if (_logger.IsDetailedLogsEnabled) { _logger.NCacheLog.Debug("Broker.NewHashmapReceived", "Hashmap " + newHashmap.ToString()); } long oldId = _pool != null ? _pool.LastViewId : -2; if (newHashmap.LastViewId == this._pool.LastViewId) { _logger.NCacheLog.CriticalInfo("Broker.NewHashmapReceived", "Hashmap is same as current pool. Pool " + this._pool.ToString() + " New Hashmap " + newHashmap.ToString() + " ... returning"); return; } if (_clientConfig.IPMappingConfigured) { this.GetServerMapping(null, false); } for (int i = 0; i < newHashmap.Members.Count; i++) { string ip = (string) newHashmap.Members[i]; int serverPort = this._port; if (_clientConfig.IPMappingConfigured) { RemoteServer server = _clientConfig.GetMappedServer(ip, serverPort); ip = server.Name; serverPort = server.Port; newHashmap.Members[i] = ip + ":" + serverPort.ToString(); } Address addr = new Address(ip, serverPort); if (!this._pool.Contains(addr)) { try { IPAddress address = IPAddress.Parse(ip); Connection connection = new Connection(this._commandReieved, this._serverLost, this._logger, _perfStatsColl, _responseIntegrator, _clientConfig.BindIP); Exception exception = null; if (ConnectRemoteServer(connection, address, serverPort, false, false, false, ref exception)) { this._pool.Add(addr, connection); this._clientConfig.AddServer(new RemoteServer(address, serverPort)); if (_logger.IsDetailedLogsEnabled) _logger.NCacheLog.Debug("Broker.NewHashmapRecieved", "Connection made to " + ip + ", and added to pool"); } else { if (exception != null && _logger.IsErrorLogsEnabled) { _logger.NCacheLog.Error("Broker.NewHashmapRecieved", "Could not connect to " + ip + ". " + exception.ToString()); } } } catch (Exception exc) { if (_logger.IsErrorLogsEnabled) _logger.NCacheLog.Error("Broker.NewHashmapRecieved", exc.ToString()); } } else { Connection connection = this._pool[addr]; if (connection != null && !connection.IsConnected) { if (_logger.IsDetailedLogsEnabled) { _logger.NCacheLog.Debug("Broker.NewHashmapRecieved", "Not connected to " + ip + " in the pool"); } try { Exception exception = null; TryConnecting(connection, ref exception); } catch (Exception exc) { if (_logger.IsErrorLogsEnabled) _logger.NCacheLog.Error("Broker.NewHashmapRecieved", exc.ToString()); } } else { if (_logger.IsDetailedLogsEnabled) { _logger.NCacheLog.Debug("Broker.NewHashmapRecieved", "Already connected to " + ip + " in the pool"); } } } } if (this._importHashmap) { //List of Connection ip's which as per new hashmap are no more valid List<Address> invalidIPConnection = new List<Address>(); foreach (Address ipAddress in this._pool.Servers) { if (_clientConfig.IPMappingConfigured) { if ( !newHashmap.Members.Contains(ipAddress.IpAddress.ToString() + ":" + ipAddress.Port.ToString())) { invalidIPConnection.Add(ipAddress); } } else { if (!newHashmap.Members.Contains(ipAddress.IpAddress.ToString())) { invalidIPConnection.Add(ipAddress); } } } foreach (Address ip in invalidIPConnection) { this._pool[ip].Disconnect(); this._pool.Remove(ip); if (_logger.IsDetailedLogsEnabled) { _logger.NCacheLog.Debug("Broker.NewHashmapRecieved", "Disconnected from " + ip + ", and removed from pool"); } } } RemoteServer srvr = new RemoteServer(); string add = null; for (int key = 0; key < newHashmap.Map.Count; key++) { add = (string) newHashmap.Map[key]; srvr = _clientConfig.GetMappedServer(add, this._port); newHashmap.Map[key] = new Address(srvr.Name, srvr.Port); } this._pool.SetHashmap(newHashmap); if (_logger.IsDetailedLogsEnabled) { _logger.NCacheLog.Debug("Broker.NewHashmapReceived", "Hashmap applied " + newHashmap.ToString() + " Pool " + this._pool.ToString()); } } catch (Exception exc) { if (_logger.IsErrorLogsEnabled) _logger.NCacheLog.Error("Broker.NewHashmapRecieved", exc.Message); } } }
/// <summary> /// New hashmap recieved. Depending on new and old hashmap, some connections are /// disposed and some new connections are formed(not always as in some cases only buckets have /// transfered between servers). This method should be called asynchronously so the recieve thread /// will be free to recieve other command responces. /// </summary> /// <param name="newHashmap">new hashmap returned from primary server</param> /// <param name="bucketSize">bucket size</param> private void NewHashmapRecieved(NewHashmap newHashmap, int bucketSize) { if (newHashmap == null) return; this._pool.BucketSize = bucketSize; NewHashmapRecieved(newHashmap); }
/// <summary> /// Fire when hasmap changes when /// - new node joins /// - node leaves /// - manual/automatic load balance /// </summary> /// <param name="newHashmap">new hashmap</param> void ICacheEventsListener.OnHashmapChanged(NewHashmap newHashmap, bool updateClientMap) { if (this._hashmapChanged == null) return; Delegate[] dlgList = this._hashmapChanged.GetInvocationList(); NewHashmap.Serialize(newHashmap, this._context.SerializationContext, updateClientMap); foreach (HashmapChangedCallback subscriber in dlgList) { try { subscriber.BeginInvoke(newHashmap, null, new AsyncCallback(HashmapChangedAsyncCallbackHandler), subscriber); } catch (System.Net.Sockets.SocketException ex) { _context.NCacheLog.Error("Cache.OnHashmapChanged", ex.ToString()); this._hashmapChanged -= subscriber; } catch (Exception ex) { _context.NCacheLog.Error("Cache.OnHashmapChanged", ex.ToString()); } } }
/// <summary> /// Set new hashmap to this new hashmap /// </summary> /// <param name="hashMap">new hashmap</param> public void SetHashmap(NewHashmap hashMap) { this._lastViewId = hashMap.LastViewId; lock (this._hashMap.SyncRoot) this._hashMap = (!hashMap.Map.IsSynchronized) ? Hashtable.Synchronized(hashMap.Map) : hashMap.Map; }