internal void Initialize(IDictionary properties, bool inProc, bool isStartingAsMirror, bool twoPhaseInitialization) { //Just to initialze the HP time HPTime time = HPTime.Now; for (int i = 1; i < 1000; i++) time = HPTime.Now; _inProc = inProc; if (properties == null) throw new ArgumentNullException("properties"); try { lock (this) { if (!properties.Contains("cache")) throw new ConfigurationException("Missing configuration attribute 'cache'"); IDictionary cacheConfig = (IDictionary)properties["cache"]; if (properties["server-end-point"] != null) cacheConfig.Add("server-end-point", (IDictionary)properties["server-end-point"]); if (cacheConfig.Contains("name")) _cacheInfo.Name = Convert.ToString(cacheConfig["name"]).Trim(); _cacheInfo.CurrentPartitionId = GetCurrentPartitionId(_cacheInfo.Name, cacheConfig); if (cacheConfig.Contains("log")) { _context.NCacheLog = new NCacheLogger(); _context.NCacheLog.Initialize(cacheConfig["log"] as IDictionary, _cacheInfo.CurrentPartitionId, _cacheInfo.Name, isStartingAsMirror, inProc); } else { _context.NCacheLog = new NCacheLogger(); _context.NCacheLog.Initialize(null, _cacheInfo.CurrentPartitionId, _cacheInfo.Name); } _context.SerializationContext = _cacheInfo.Name; _context.TimeSched = new TimeScheduler(); _context.TimeSched.AddTask(new SystemMemoryTask(_context.NCacheLog)); _context.AsyncProc = new AsyncProcessor(_context.NCacheLog); if (!inProc) { if (_cacheInfo.CurrentPartitionId != string.Empty) { _context.PerfStatsColl = new PerfStatsCollector(Name + "-" + _cacheInfo.CurrentPartitionId, inProc); } else { _context.PerfStatsColl = new PerfStatsCollector(Name, inProc); } _context.PerfStatsColl.NCacheLog = _context.NCacheLog; } else { _channels = new RemotingChannels(); _channels.RegisterTcpChannels("InProc", 0); if (_cacheInfo.CurrentPartitionId != string.Empty) RemotingServices.Marshal(this, _cacheInfo.Name + ":" + _cacheInfo.CurrentPartitionId); else RemotingServices.Marshal(this, _cacheInfo.Name); //get the port number selected. TcpServerChannel serverChannel = (TcpServerChannel)_channels.TcpServerChannel; string uri = serverChannel.GetChannelUri(); int port = Convert.ToInt32(uri.Substring(uri.LastIndexOf(":") + 1)); if (isStartingAsMirror)// this is a special case for Partitioned Mirror Topology. _context.PerfStatsColl = new PerfStatsCollector(Name + "-" + "replica", false); else _context.PerfStatsColl = new PerfStatsCollector(Name, port, inProc); _context.PerfStatsColl.NCacheLog = _context.NCacheLog; } _context.IsStartedAsMirror = isStartingAsMirror; CreateInternalCache(cacheConfig, isStartingAsMirror, twoPhaseInitialization); //setting cache Impl instance if (inProc && _context.CacheImpl != null) { //we keep unserialized user objects in case of local inproc caches... _context.CacheImpl.KeepDeflattedValues = false;// we no longer keep seralized data in inproc cache } // we bother about perf stats only if the user has read/write rights over counters. _context.PerfStatsColl.IncrementCountStats(CacheHelper.GetLocalCount(_context.CacheImpl)); _cacheInfo.ConfigString = ConfigHelper.CreatePropertyString(properties); } _context.NCacheLog.CriticalInfo("Cache '" + _context.CacheRoot.Name + "' started successfully."); } catch (ConfigurationException e) { Dispose(); throw; } catch (Exception e) { Dispose(); throw new ConfigurationException("Configuration Error: " + e.Message, e); } }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or /// resetting unmanaged resources. /// </summary> /// <param name="disposing"></param> private void Dispose(bool disposing) { lock (this) { if (_cacheStopped != null && !CacheType.Equals("mirror-server")) { Delegate[] invocationList = this._cacheStopped.GetInvocationList(); foreach (Delegate subscriber in invocationList) { CacheStoppedCallback callback = (CacheStoppedCallback)subscriber; try { callback(this._cacheInfo.Name, null); } catch (Exception e) { NCacheLog.Error("Cache.Dispose", "Error occurred while invoking cache stopped event: " + e.ToString()); //Ignore and move on to fire next } finally { this._cacheStopped -= callback; } } } try { if (_inProc) RemotingServices.Disconnect(this); } catch (Exception) { } if (_context.CacheImpl != null) _context.CacheImpl.StopServices(); if (_connectedClients != null) lock (_connectedClients.SyncRoot) { _connectedClients.Clear(); } _cacheStopped = null; _sponsor = null; if (NCacheLog != null) { NCacheLog.CriticalInfo("Cache.Dispose", "Cache stopped successfully"); NCacheLog.Flush(); NCacheLog.Close(); } try { if (_channels != null) _channels.UnregisterTcpChannels(); } catch (Exception) { } _channels = null; GC.Collect(); if (disposing) { GC.SuppressFinalize(this); } if (_context != null) { _context.Dispose(); } //Dispose snaphot pool for this cache. if (disposing) { CacheSnapshotPool.Instance.DisposePool(_context.CacheRoot.Name); } } }