private NodeStatus GetCacheStatistics(NCacheRPCService nCacheRPCService) { ICacheServer cacheServer = nCacheRPCService.GetCacheServer(new TimeSpan(0, 0, 0, 30)); NodeStatus nodeInfo = new NodeStatus(); nodeInfo.isRegistered = true; if (cacheServer.GetCacheConfiguration(CacheName) == null) { nodeInfo.isRegistered = false; return(nodeInfo); } var config = cacheServer.GetCacheConfiguration(CacheName); if (config.CacheType.Contains("local")) { nodeInfo.Topology = Topology.LOCAL; } else { if (config.Cluster.Topology.Contains("partitioned-replicas")) { nodeInfo.Topology = Topology.POR; } else { nodeInfo.Topology = Topology.OTHER; } } return(nodeInfo); }
public CacheServerConfig GetServerConfiguration(string server, int port, string cacheId) { CacheServerConfig serverConfig = null; if (port != -1) { NCache.Port = port; } if (port == -1) { NCache.Port = NCache.UseTcp ? CacheConfigManager.NCacheTcpPort: CacheConfigManager.HttpPort; } if (server != null && server != string.Empty) { NCache.ServerName = server; } cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30)); if (cacheServer != null) { serverConfig = cacheServer.GetCacheConfiguration(cacheId); if (serverConfig == null) { throw new Exception("Specified cache is not registered on given server."); } } return(serverConfig); }
public static void StopCache(string cacheId) { ICacheServer cs = null; CacheService cacheService = new NCacheRPCService(null); CacheServerConfig config = null; try { cacheService.ServerName = Environment.MachineName; cs = cacheService.GetCacheServer(new TimeSpan(0, 0, 0, 30)); if (cs != null) { config = cs.GetCacheConfiguration(cacheId); if (!config.InProc) { cs.StopCache(cacheId); } else { throw new ManagementException("Inproc caches cannot be stopped explicitly."); } } } finally { if (cs != null) { cs.Dispose(); } cacheService.Dispose(); } }
public static void StartCache(string cacheId, string serverName, int port, string userId, string password) { ICacheServer cs = null; CacheService cacheService = new NCacheRPCService(null); CacheServerConfig config = null; try { cacheService.ServerName = serverName; cacheService.Port = port; cs = cacheService.GetCacheServer(new TimeSpan(0, 0, 0, 30)); if (cs != null) { config = cs.GetCacheConfiguration(cacheId); if (config != null) { if (!config.InProc) { cs.StartCache(cacheId); } } else { throw new ManagementException("Unable to Start Cache. Specified cache is not registered."); } } } catch (SecurityException ex) { if (cs != null) { cs.Dispose(); cs = null; } throw ex; } catch (Exception ex) { if (cs != null) { cs.Dispose(); cs = null; } throw new ManagementException(ex.Message); } finally { if (cs != null) { cs.Dispose(); } cacheService.Dispose(); } }
void StartCacheOnServer() { OutputProvider.WriteLine("Licensed to use FREE of cost. Use As-is without support.\n"); string cacheIp = string.Empty; try { ICacheServer cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30)); CacheServerConfig config = null; if (cacheServer != null) { cacheIp = cacheServer.GetClusterIP(); foreach (string cache in CachesList) { try { config = cacheServer.GetCacheConfiguration(cache); if (config != null && config.InProc) { throw new Exception("InProc caches cannot be started explicitly."); } OutputProvider.WriteLine("Starting cache '{0}' on server {1}:{2}.", cache, cacheIp, NCache.Port); cacheServer.StartCache(cache, _partId); OutputProvider.WriteLine("'{0}' successfully started on server {1}:{2}.\n", cache, cacheIp, NCache.Port); } catch (Exception e) { OutputProvider.WriteErrorLine("Failed to start '{0}' on server {1}.", cache, cacheIp); OutputProvider.WriteErrorLine(e.ToString() + "\n"); } } } } catch (ManagementException ex) { OutputProvider.WriteErrorLine("Error : {0}", "NCache service could not be contacted on server"); OutputProvider.WriteErrorLine(ex.ToString()); } catch (Exception e) { OutputProvider.WriteErrorLine("Error : {0}", e.Message); OutputProvider.WriteErrorLine(e.ToString()); } finally { NCache.Dispose(); } }
public CacheServerConfig GetServerConfiguration(string server, int port, string cacheId) { CacheServerConfig serverConfig=null ; if (port != -1) { NCache.Port = port; } if (port==-1) NCache.Port = NCache.UseTcp ? CacheConfigManager.NCacheTcpPort: CacheConfigManager.HttpPort; if (server != null && server != string.Empty) { NCache.ServerName = server; } cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30)); if (cacheServer != null) { serverConfig = cacheServer.GetCacheConfiguration(cacheId); if (serverConfig == null) throw new Exception("Specified cache is not registered on given server."); } return serverConfig; }
/// <summary> /// The main entry point for the tool. /// </summary> public void RemoveBridgeCache() { try { _bridgeService = new NCBridgeRPCService(BridgeServer); _bridgeServer = _bridgeService.GetBridgeServer(TimeSpan.FromSeconds(30)); NCacheRPCService nService = new NCacheRPCService(CacheServer); cacheServer = nService.GetCacheServer(new TimeSpan(0, 0, 0, 30)); if (!ValidateParameters()) { return; } //********************************** config = cacheServer.GetCacheConfiguration(CacheName); if (config == null) { OutputProvider.WriteErrorLine("Error : The cache'{0}' does not exist on server {1}:{2} .", CacheName, NCache.ServerName, NCache.Port); return; } { //OutputProvider.WriteErrorLine("Error : The Bidge {0} is running on {1} , please stop Bridge and try again .", BridgeId, NCache.ServerName); //return; } if (config.CacheType.ToLower().Equals("local-cache")) { OutputProvider.WriteLine("Local Cache cannot be added as a bridge cache"); return; } //cacheServer = GetCacheServers(config.Cluster.GetAllConfiguredNodes()); if (_bridgeServer != null) { try { OutputProvider.WriteLine("Removing Cache To Bridge '{0}' on {1}:{2}.", BridgeId, _bridgeService.ServerName, _bridgeService.Port); Alachisoft.NCache.Bridging.Configuration.BridgeConfiguration bridgeConfig = _bridgeServer.GetBridgeConfiguration(BridgeId); ToolsUtil.VerifyBridgeConfigurations(bridgeConfig, BridgeId); if (bridgeConfig == null) { OutputProvider.WriteErrorLine("No Bridge with Bridge ID '{0} exists' on Server {1}:{2}.", BridgeId, _bridgeService.ServerName, _bridgeService.Port); return; } TargetCacheCofiguration targtCacheConfig = new TargetCacheCofiguration(); targtCacheConfig.CacheID = CacheName; targtCacheConfig.Servers = cacheServer.GetHostName(); targtCacheConfig.IsConnected = true; { if (!VerifyBridgeMasterCache(BridgeId, false, bridgeConfig)) { targtCacheConfig.IsMaster = true; } targtCacheConfig.Status = BridgeCacheStateParam.Active.ToString(); } List <TargetCacheCofiguration> previouslyAddedCaches = bridgeConfig.TargetCacheConfigList; int removedCacheIndex = -1; if (previouslyAddedCaches.Count >= 1) { int iteration = 0; //checking validations regarding bridge foreach (TargetCacheCofiguration pCache in previouslyAddedCaches) { //if exists than remove if (pCache.CacheID.ToLower().Equals(CacheName.ToLower())) { if (pCache.CacheAlias.ToLower().Equals(Alias.ToLower()) || string.IsNullOrEmpty(Alias)) { removedCacheIndex = iteration; if (pCache.IsMaster) { OutputProvider.WriteErrorLine("Failed to Remove Cache to bridge '{0}'. Error: Master cache cannot be removed ", BridgeId); return; } } } if (pCache.Status.Equals("passive")) { OutputProvider.WriteErrorLine("Failed to Remove Cache to bridge '{0}'. Error: No both bridge caches can be passive ", BridgeId); return; } iteration++; } } else { OutputProvider.WriteErrorLine("Failed to Remove Cache There is currently no Cache Added in Bridge {0} ", BridgeId); return; } // if (removedCacheIndex >= 0) { bridgeConfig.TargetCacheConfigList.RemoveAt(removedCacheIndex); } else { OutputProvider.WriteErrorLine("Bridge Cache Does not exists with name{0} in Bridge '{1}'", CacheName, BridgeId); return; } //Adding Bridge to config.ncconf BridgeConfig bridgeConf = config.Bridge; config.Bridge = null; byte[] userId = null; byte[] paswd = null; if (UserId != string.Empty && Password != string.Empty) { userId = EncryptionUtil.Encrypt(UserId); paswd = EncryptionUtil.Encrypt(Password); } //writing to config.ncconf config.ConfigVersion++; cacheServer.ConfigureBridgeToCache(config, userId, paswd, true); cacheServer.HotApplyBridgeReplicator(CacheName, false); ConveyToRegisteredNodes(); char[] separater = { ',' }; // write in all bridge nodes bridge.nconnf file bool write = false; foreach (string bridgeIp in bridgeConfig.BridgeNodes.Split(separater).ToList()) { try{ _bridgeService = new NCBridgeRPCService(bridgeIp); _bridgeServer = _bridgeService.GetBridgeServer(TimeSpan.FromSeconds(30)); _bridgeServer.RegisterBridge(bridgeConfig, true, true); write = true; OutputProvider.WriteLine("Removed Bridge Cache {0} From Bridge {1}", CacheName, BridgeId); } catch (Exception e) { OutputProvider.WriteErrorLine("Removing Bridge Cache {0} From Bridge Server{1} Gives Error: {2}", bridgeConf, bridgeIp, e.Message); } } } catch (SecurityException e) { OutputProvider.WriteErrorLine("Failed to Remove Cache to bridge '{0}'. Error: {1} ", BridgeId, e.Message); } catch (Exception e) { OutputProvider.WriteErrorLine("Failed to Remove Cache to bridge '{0}'. Error: {1} ", BridgeId, e.Message); } } } catch (Exception e) { OutputProvider.WriteErrorLine("Error: {0}", e.Message); } finally { if (_bridgeService != null) { _bridgeService.Dispose(); } if (NCache != null) { NCache.Dispose(); } } }
public void StopCache() { try { if (!ValidateParameters()) { return; } ICacheServer m = null; try { m = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30)); } catch (Exception e) { OutputProvider.WriteErrorLine("Error: NCache service could not be contacted on server."); } string getBindIp = string.Empty; CacheServerConfig config = null; if (m != null) { foreach (string cache in CachesList) { try { config = m.GetCacheConfiguration(cache); if (config != null && config.InProc) { throw new Exception("InProc caches cannot be stopped explicitly."); } getBindIp = m.GetClusterIP(); OutputProvider.WriteLine("Stopping cache '{0}' on server {1}:{2}.", cache, getBindIp, NCache.Port); m.StopCache(cache, _partId); m.StopCache(cache, string.Empty); OutputProvider.WriteLine("'{0}' successfully stopped on server {1}:{2}.\n", cache, getBindIp, NCache.Port); } catch (System.Security.SecurityException e) { OutputProvider.WriteErrorLine("Failed to stop '{0}'. Error: {1} ", cache, e.Message); OutputProvider.WriteErrorLine(e.ToString()); OutputProvider.WriteLine(Environment.NewLine); } catch (Exception e) { OutputProvider.WriteErrorLine("Failed to stop '{0}'.", cache); OutputProvider.WriteErrorLine("Error: " + e.ToString()); OutputProvider.WriteLine(Environment.NewLine); } } } } catch (Exception e) { OutputProvider.WriteErrorLine("Error : {0}", e.Message); OutputProvider.WriteErrorLine(e.ToString()); OutputProvider.WriteLine(Environment.NewLine); } finally { NCache.Dispose(); } }
public static Dictionary <string, TopicStats> GetTopicStat(string cacheName, string initialNodeName, Runtime.CacheManagement.CacheContext context, int port) { CacheService cacheService = GetCacheService(context); if (port != 0) { cacheService.Port = port; } string startingNode = initialNodeName; CacheServerConfig cacheServerConfig = null; ICacheServer cacheServer = null; try { if (initialNodeName.Equals(string.Empty)) { cacheServerConfig = GetCacheConfigThroughClientConfig(cacheName, port, context); if (cacheServerConfig == null) { throw new ManagementException("cache with name " + cacheName + " not found in " + config); } } else { cacheService.ServerName = initialNodeName; cacheServer = cacheService.GetCacheServer(new TimeSpan(0, 0, 0, 30)); if (cacheServer == null) { throw new ManagementException("provided initial node not available"); } cacheServerConfig = cacheServer.GetCacheConfiguration(cacheName); if (cacheServerConfig == null) { throw new ManagementException("cache with name " + cacheName + " not registered on specified node"); } } //For Local Cache if (cacheServerConfig.CacheType.Equals(LOCALCACHE, StringComparison.OrdinalIgnoreCase)) { if (cacheServerConfig.InProc) { throw new ArgumentException("API is not supported for Local Inproc Cache"); } cacheService.ServerName = Environment.MachineName; cacheServer = cacheService.GetCacheServer(new TimeSpan(0, 0, 0, 30)); if (cacheServer != null && cacheServer.IsRunning(cacheName)) { return(cacheServer.GetTopicStats(cacheServerConfig.Name)); } } //For Clustered Cache else { Dictionary <string, TopicStats> topicWiseStat = new Dictionary <string, TopicStats>(); ArrayList initialHost = InitialHostList(cacheServerConfig.Cluster.Channel.InitialHosts); foreach (object host in initialHost) { try { cacheService.ServerName = (string)host; cacheServer = cacheService.GetCacheServer(new TimeSpan(0, 0, 0, 30)); if (cacheServer.IsRunning(cacheName)) { Dictionary <string, TopicStats> NodeWisetopicStat = cacheServer.GetTopicStats(cacheServerConfig.Name); if (NodeWisetopicStat != null) { foreach (var item in NodeWisetopicStat) { if (!topicWiseStat.ContainsKey(item.Key)) { item.Value.TopicName = item.Key; topicWiseStat.Add(item.Key, ((TopicStats)item.Value.Clone())); } else { TopicStats topicStat = topicWiseStat[item.Key]; topicStat.CurrentMessageCount += item.Value.CurrentMessageCount; } } } } } catch (Exception e) { } } return(topicWiseStat); } } catch (Exception ex) { throw new ManagementException(ex.Message); } finally { if (cacheServer != null) { cacheServer.Dispose(); } cacheService.Dispose(); } return(null); }
private static CacheServerConfig GetCacheConfigThroughClientConfig(string cacheName, int port, Runtime.CacheManagement.CacheContext context) { CacheServerConfig cacheServerConfig = null; ClientConfiguration.Dom.CacheServer[] serverNodes = null; ICacheServer cacheServer = null; CacheService cacheService = GetCacheService(context); if (port != 0) { cacheService.Port = port; } try { //Get Server Info from Client.nconf for specified cacheName ClientConfiguration.Dom.ClientConfiguration clientConfiguration = ClientConfiguration.ClientConfigManager.GetClientConfiguration(cacheName); if (clientConfiguration != null) { Dictionary <string, CacheConfiguration> cacheConfigurationMap = clientConfiguration.CacheConfigurationsMap; CacheConfiguration cacheClientConfiguration = null; try { cacheClientConfiguration = cacheConfigurationMap[cacheName]; } catch (System.Collections.Generic.KeyNotFoundException ex) { } if (cacheClientConfiguration == null) { throw new ManagementException("cache not found in " + config); } serverNodes = cacheClientConfiguration.Servers; foreach (ClientConfiguration.Dom.CacheServer node in serverNodes) { try { cacheService.ServerName = node.ServerName; cacheServer = cacheService.GetCacheServer(new TimeSpan(0, 0, 0, 30)); if (cacheServer != null) { cacheServerConfig = cacheServer.GetCacheConfiguration(cacheName); //dispose the server cacheServer.Dispose(); cacheServer = null; if (cacheServerConfig != null) { break; } } } catch (Exception ex) { } } } else { throw new ManagementException("error while fetching info from " + config); } } finally { if (cacheServer != null) { cacheServer.Dispose(); } if (cacheService != null) { cacheService.Dispose(); } } return(cacheServerConfig); }
/// <summary> /// The main entry point for the tool. /// </summary> static public void Run(string[] args) { NCache = new NCacheRPCService(""); string cacheIp = string.Empty; try { object param = new StartCacheToolParam(); CommandLineArgumentParser.CommandLineParser(ref param, args); cParam = (StartCacheToolParam)param; if (cParam.IsUsage) { AssemblyUsage.PrintLogo(cParam.IsLogo); AssemblyUsage.PrintUsage(); return; } if (!ApplyParameters(args)) { return; } ICacheServer m = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30)); CacheServerConfig config = null; cacheIp = m.GetClusterIP(); if (m != null) { foreach (string cache in s_cacheId) { try { config = m.GetCacheConfiguration((string)cache); if (config != null && config.InProc) { throw new Exception("InProc caches cannot be started explicitly."); } Console.WriteLine("\nStarting cache '{0}' on server {1}:{2}.", cache, cacheIp, NCache.Port); m.StartCache(cache, string.Empty); Console.WriteLine("'{0}' successfully started on server {1}:{2}.\n", cache, cacheIp, NCache.Port); } catch (Exception e) { Console.Error.WriteLine("Failed to start '{0}' on server {1}.", cache, cacheIp); Console.Error.WriteLine(); Console.Error.WriteLine(e.ToString() + "\n"); } } } } catch (ManagementException ex) { Console.Error.WriteLine("Error : {0}", "NCache service could not be contacted on server."); Console.Error.WriteLine(); Console.Error.WriteLine(ex.ToString()); } catch (Exception e) { Console.Error.WriteLine("Error : {0}", e.Message); Console.Error.WriteLine(); Console.Error.WriteLine(e.ToString()); } finally { NCache.Dispose(); } }
public void AddBridgeNode() { try { _bridgeService = new NCBridgeRPCService(Server); if (!ValidateParameters()) { return; } _bridgeServer = _bridgeService.GetBridgeServer(TimeSpan.FromSeconds(30)); BridgeConfiguration bconfig = _bridgeServer.GetBridgeConfiguration(BridgeId); ToolsUtil.VerifyBridgeConfigurations(bconfig, BridgeId); byte[] userId = null; byte[] paswd = null; if (UserId != string.Empty && Password != string.Empty) { userId = EncryptionUtil.Encrypt(UserId); paswd = EncryptionUtil.Encrypt(Password); } if (bconfig != null && bconfig.NumberOfBridgeNodes >= 2) { OutputProvider.WriteErrorLine("You cannot add more than 2 nodes in a Bridge"); return; } if (!bconfig.GetBridgeNodeList().Contains(NewBridgeNode)) { if (bconfig.GetBridgeNodeList().Count != 0) { bconfig.DeploymentVersion++; bconfig.BridgeNodes = bconfig.BridgeNodes + "," + NewBridgeNode; _bridgeServer.RegisterBridge(bconfig, true, false); //for registering bridge on newly added node NCBridgeRPCService _bridgeNewService = new NCBridgeRPCService(NewBridgeNode); IBridgeServer _bridgeNewServer = _bridgeNewService.GetBridgeServer(TimeSpan.FromSeconds(30)); _bridgeNewServer.RegisterBridge(bconfig, true, false); OutputProvider.WriteLine(NewBridgeNode + " added to " + BridgeId); } else { bconfig.BridgeNodes = NewBridgeNode; bconfig.BridgeActive = NewBridgeNode; NCBridgeRPCService _bridgeNewService = new NCBridgeRPCService(NewBridgeNode); IBridgeServer _bridgeNewServer = _bridgeNewService.GetBridgeServer(TimeSpan.FromSeconds(30)); _bridgeNewServer.RegisterBridge(bconfig, true, false); OutputProvider.WriteLine(NewBridgeNode + " added to " + BridgeId); } //updating target cache config if (bconfig.TargetCacheConfigList != null) { foreach (TargetCacheCofiguration targetCacheConfig in bconfig.TargetCacheConfigList) { try { if (targetCacheConfig != null) { foreach (string server in targetCacheConfig.Servers.Split(',')) { NCacheRPCService NCache = new NCacheRPCService(server);; ICacheServer cacheServer = null; cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30)); string cacheID = targetCacheConfig.CacheID.ToString(); CacheServerConfig serverConfig = cacheServer.GetCacheConfiguration(cacheID); if (serverConfig.Bridge != null) { serverConfig.Bridge.Servers = bconfig.BridgeNodes; } serverConfig.ConfigVersion++; cacheServer.ConfigureBridgeToCache(serverConfig, userId, paswd, false); } } } catch (Exception e) { OutputProvider.WriteErrorLine(e.Message); return; } } } } else { OutputProvider.WriteErrorLine("Error: Node already exists in bridge"); return; } } catch (Exception e) { OutputProvider.WriteErrorLine("Error: Bridge not registered on server.\n"); OutputProvider.WriteErrorLine(e.Message); } }
public void RemoveBridge() { try { if (!ValidateParameters()) { return; } _bridgeService = new NCBridgeRPCService(Server); if (Port == -1) { _bridgeService.Port = _bridgeService.UseTcp ? BridgeConfigurationManager.NCacheTcpPort : BridgeConfigurationManager.NCacheHttpPort; } else { _bridgeService.Port = Port; } _bridgeService.ServerName = Server; _bridgeServer = _bridgeService.GetBridgeServer(TimeSpan.FromSeconds(30)); BridgeConfiguration bridgeConfig = _bridgeServer.GetBridgeConfiguration(BridgeId); byte[] userId = null; byte[] paswd = null; if (UserId != string.Empty && Password != string.Empty) { userId = EncryptionUtil.Encrypt(UserId); paswd = EncryptionUtil.Encrypt(Password); } if (bridgeConfig.GetBridgeNodeList().Count == 2) { _bridgeServer.UnRegisterBridge(BridgeId); if (bridgeConfig.BridgeNodes.Contains(_bridgeServer + ",")) { bridgeConfig.BridgeNodes = bridgeConfig.BridgeNodes.Replace(_bridgeServer + ",", ""); } else { bridgeConfig.BridgeNodes = bridgeConfig.BridgeNodes.Replace("," + _bridgeServer, ""); } bridgeConfig.BridgeActive = bridgeConfig.BridgeNodes; NCBridgeRPCService _bridge = new NCBridgeRPCService(bridgeConfig.BridgeActive); IBridgeServer _server = _bridge.GetBridgeServer(TimeSpan.FromSeconds(30)); _server.UnRegisterBridge(BridgeId); } else { _bridgeServer.UnRegisterBridge(BridgeId); } bridgeConfig.BridgeNodes = ""; //updating target cache config if (bridgeConfig.TargetCacheConfigList != null) { foreach (TargetCacheCofiguration targetCacheConfig in bridgeConfig.TargetCacheConfigList) { try { if (targetCacheConfig != null) { foreach (string server in targetCacheConfig.Servers.Split(',')) { NCacheRPCService NCache = new NCacheRPCService(server);; ICacheServer cacheServer = null; cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30)); string cacheID = targetCacheConfig.CacheID.ToString(); CacheServerConfig serverConfig = cacheServer.GetCacheConfiguration(cacheID); if (serverConfig.Bridge != null) { serverConfig.Bridge = null; } serverConfig.ConfigVersion++; cacheServer.ConfigureBridgeToCache(serverConfig, userId, paswd, false); } } } catch (Exception ex) { OutputProvider.WriteErrorLine(ex.Message); return; } } } OutputProvider.WriteLine("Bridge removed."); } catch (Exception e) { OutputProvider.WriteErrorLine("Bridge not registered on server.\n"); OutputProvider.WriteErrorLine("Error: " + e.Message); return; } }
/// <summary> /// The main entry point for the tool. /// </summary> public void SetBridgeCacheMode() { try { _bridgeService = new NCBridgeRPCService(BridgeServer); _bridgeServer = _bridgeService.GetBridgeServer(TimeSpan.FromSeconds(30)); NCacheRPCService nService = new NCacheRPCService(CacheServer); cacheServer = nService.GetCacheServer(new TimeSpan(0, 0, 0, 30)); if (!ValidateParameters()) { return; } //********************************** config = cacheServer.GetCacheConfiguration(CacheName); if (config == null) { OutputProvider.WriteErrorLine("Error : The cache'{0}' does not exist on server {1}:{2} .", CacheName, NCache.ServerName, NCache.Port); return; } if (config.CacheType.ToLower().Equals("local-cache")) { OutputProvider.WriteLine("Local Cache cannot be Set as a bridge cache"); return; } //cacheServer = GetCacheServers(config.Cluster.GetAllConfiguredNodes()); bool isNotMaster = false; int passiveCount = 0; TargetCacheCofiguration targtCacheConfig = null; if (_bridgeServer != null) { try { OutputProvider.WriteLine("Setting Cache Mode To Bridge '{0}' on {1}:{2}.", BridgeId, _bridgeService.ServerName, _bridgeService.Port); Alachisoft.NCache.Bridging.Configuration.BridgeConfiguration bridgeConfig = _bridgeServer.GetBridgeConfiguration(BridgeId); ToolsUtil.VerifyBridgeConfigurations(bridgeConfig, BridgeId); List <TargetCacheCofiguration> previouslyAddedCaches = bridgeConfig.TargetCacheConfigList; if (previouslyAddedCaches.Count >= 1) { //checking validations regarding bridge foreach (TargetCacheCofiguration pCache in previouslyAddedCaches) { if (pCache.CacheID.ToLower().Equals(CacheName.ToLower())) { isNotMaster = !pCache.IsMaster; targtCacheConfig = pCache; } if (pCache.Status.Equals("passive")) { passiveCount++; } } } else { OutputProvider.WriteErrorLine("No Cache Exists in Bridge {0}", BridgeId); } if (passiveCount >= 1 && State.Equals(BridgeCacheStateParam.Passive)) { OutputProvider.WriteErrorLine("There is already a Passive cache in Bridge", BridgeId); return; } if (!isNotMaster) { OutputProvider.WriteErrorLine("Bridge Cache '{0}' is a master Node,please change masterNode first", CacheName); return; } if (targtCacheConfig.Status.Equals(State.ToString())) { OutputProvider.WriteErrorLine("Bridge Cache '{0}' is already {1}", CacheName, State.ToString()); return; } // //Adding Bridge to config.ncconf BridgeConfig bridgeConf = config.Bridge; bridgeConf.Status = State.ToString(); config.Bridge = bridgeConf; WriteToClientConf(); WriteToBridgeConf(bridgeConfig, targtCacheConfig); OutputProvider.WriteLine("Cache '{0}' mode set to {1}", BridgeId, State.ToString()); } catch (SecurityException e) { OutputProvider.WriteErrorLine("Failed to Add Cache to bridge '{0}'. Error: {1} ", BridgeId, e.Message); } catch (Exception e) { OutputProvider.WriteErrorLine("Failed to Add Cache to bridge '{0}'. Error: {1} ", BridgeId, e.Message); } } } catch (Exception e) { OutputProvider.WriteErrorLine("Error: {0}", e.Message); } finally { if (_bridgeService != null) { _bridgeService.Dispose(); } if (NCache != null) { NCache.Dispose(); } } }
/// <summary> /// The main entry point for the tool. /// </summary> public void AddBridgeCache() { try { if (!ValidateParameters()) { return; } _bridgeService = new NCBridgeRPCService(BridgeServer); if (Port == -1) { _bridgeService.Port = _bridgeService.UseTcp ? BridgeConfigurationManager.NCacheTcpPort : BridgeConfigurationManager.NCacheHttpPort; } else { _bridgeService.Port = Port; } if (!string.IsNullOrEmpty(BridgeServer)) { _bridgeService.ServerName = BridgeServer; } _bridgeServer = _bridgeService.GetBridgeServer(TimeSpan.FromSeconds(30)); NCacheRPCService nService = new NCacheRPCService(CacheServer); cacheServer = nService.GetCacheServer(new TimeSpan(0, 0, 0, 30)); //********************************** config = cacheServer.GetCacheConfiguration(CacheName); if (config == null) { OutputProvider.WriteErrorLine("Error : The cache'{0}' does not exist on server {1}:{2} .", CacheName, NCache.ServerName, NCache.Port); return; } if (config.CacheType.ToLower().Equals("local-cache")) { OutputProvider.WriteLine("Local Cache cannot be added as a bridge cache"); return; } //cacheServer = GetCacheServers(config.Cluster.GetAllConfiguredNodes()); if (_bridgeServer != null) { try { OutputProvider.WriteLine("Adding Cache To Bridge '{0}' on {1}:{2}.", BridgeId, _bridgeService.ServerName, _bridgeService.Port); Alachisoft.NCache.Bridging.Configuration.BridgeConfiguration bridgeConfig = _bridgeServer.GetBridgeConfiguration(BridgeId); if (bridgeConfig == null) { OutputProvider.WriteErrorLine("No Bridge with Bridge ID '{0} exists' on Server {1}:{2}.", BridgeId, _bridgeService.ServerName, _bridgeService.Port); return; } TargetCacheCofiguration targtCacheConfig = new TargetCacheCofiguration(); targtCacheConfig.CacheID = CacheName; targtCacheConfig.CacheAlias = Alias; // set the Alias, null if name is different. Alachisoft.NCache.Config.NewDom.CacheServerConfig serverConfig = cacheServer.GetNewConfiguration(CacheName); string servers = String.Empty; foreach (Address node in serverConfig.CacheDeployment.Servers.GetAllConfiguredNodes()) { servers += node.IpAddress + ","; } servers = servers.Remove(servers.Length - 1); targtCacheConfig.Servers = servers; targtCacheConfig.IsConnected = true; if (State.Equals(BridgeCacheStateParam.Active)) { if (!VerifyBridgeMasterCache(BridgeId, false, bridgeConfig)) { targtCacheConfig.IsMaster = true; } targtCacheConfig.Status = BridgeCacheStateParam.Active.ToString(); } else { targtCacheConfig.Status = BridgeCacheStateParam.Passive.ToString(); } ToolsUtil.VerifyBridgeConfigurations(bridgeConfig, BridgeId); List <TargetCacheCofiguration> previouslyAddedCaches = bridgeConfig.TargetCacheConfigList; if (previouslyAddedCaches.Count <= 1) { //checking validations regarding bridge foreach (TargetCacheCofiguration pCache in previouslyAddedCaches) { if (pCache.CacheID.ToLower().Equals(targtCacheConfig.CacheID.ToLower())) { OutputProvider.WriteErrorLine("Failed to Add Cache to bridge '{0}'. Error:No Same Cache Can be Added Twice ", BridgeId); return; } if (pCache.Status.Equals("passive")) { OutputProvider.WriteErrorLine("Failed to Add Cache to bridge '{0}'. Error:No both bridge caches can be passive ", BridgeId); return; } } } else { OutputProvider.WriteErrorLine("Failed to Add Cache to bridge '{0}'. Error:No More than 2 caches can be add at a time ", BridgeId); return; } // //Adding Bridge to config.ncconf BridgeConfig bridgeConf = new BridgeConfig(); bridgeConf.CacheAlias = Alias; bridgeConf.Id = BridgeId; if (config != null) { bridgeConf.Servers = bridgeConfig.BridgeNodes; bridgeConf.Port = bridgeConfig.BridgePort; bridgeConf.Status = State.ToString(); } config.Bridge = bridgeConf; byte[] userId = null; byte[] paswd = null; if (UserId != string.Empty && Password != string.Empty) { userId = EncryptionUtil.Encrypt(UserId); paswd = EncryptionUtil.Encrypt(Password); } //writing to config.ncconf config.ConfigVersion++; cacheServer.ConfigureBridgeToCache(config, userId, paswd, true); cacheServer.HotApplyBridgeReplicator(CacheName, false); Alachisoft.NCache.Config.NewDom.CacheServerConfig nConfig = cacheServer.GetNewConfiguration(CacheName); foreach (Address node in nConfig.CacheDeployment.Servers.GetAllConfiguredNodes()) { NCache.ServerName = node.IpAddress.ToString(); ICacheServer server = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30)); server.ConfigureBridgeToCache(config, userId, paswd, true); server.HotApplyBridgeReplicator(CacheName, false); } ConveyToRegisteredNodes(); char [] separater = { ',' }; bridgeConfig.TargetCacheConfigList.Add(targtCacheConfig); // write in all bridge nodes bridge.nconnf file bridgeConfig.DeploymentVersion++; foreach (string bridgeIp in bridgeConfig.BridgeNodes.Split(separater).ToList()) { _bridgeService = new NCBridgeRPCService(bridgeIp); _bridgeServer = _bridgeService.GetBridgeServer(TimeSpan.FromSeconds(30)); _bridgeServer.RegisterBridge(bridgeConfig, true, true); } OutputProvider.WriteLine("{0} successfully added to BridgeID {1}", CacheName, BridgeId); } catch (SecurityException e) { OutputProvider.WriteErrorLine("Failed to Add Cache to bridge '{0}'. Error: {1} ", BridgeId, e.Message); } catch (Exception e) { OutputProvider.WriteErrorLine("Failed to Add Cache to bridge '{0}'. Error: {1} ", BridgeId, e.Message); } } } catch (Exception e) { OutputProvider.WriteErrorLine("Error: {0}", e.Message); } finally { if (_bridgeService != null) { _bridgeService.Dispose(); } if (NCache != null) { NCache.Dispose(); } } }
public void RemoveBridgeNode() { try { _bridgeService = new NCBridgeRPCService(Server); if (!ValidateParameters()) { return; } _bridgeServer = _bridgeService.GetBridgeServer(TimeSpan.FromSeconds(30)); BridgeConfiguration bridgeConfig = _bridgeServer.GetBridgeConfiguration(BridgeId); if (!ToolsUtil.VerifyBridgeConfigurations(bridgeConfig, BridgeId)) { OutputProvider.WriteErrorLine("Error: Configuration mismatch."); return; } byte[] userId = null; byte[] paswd = null; if (UserId != string.Empty && Password != string.Empty) { userId = EncryptionUtil.Encrypt(UserId); paswd = EncryptionUtil.Encrypt(Password); } if (DeleteBridgeNode == Server) { _bridgeServer.UnRegisterBridge(BridgeId); } else { NCBridgeRPCService _removeBridgeService = new NCBridgeRPCService(DeleteBridgeNode); IBridgeServer _removeServer = _removeBridgeService.GetBridgeServer(TimeSpan.FromSeconds(30)); _removeServer.UnRegisterBridge(BridgeId); } if (bridgeConfig != null) { if (bridgeConfig.GetBridgeNodeList().Count == 1) { bridgeConfig.BridgeNodes = ""; } else if (bridgeConfig.BridgeNodes.Contains(DeleteBridgeNode + ",")) { bridgeConfig.BridgeNodes = bridgeConfig.BridgeNodes.Replace(DeleteBridgeNode + ",", ""); } else { bridgeConfig.BridgeNodes = bridgeConfig.BridgeNodes.Replace("," + DeleteBridgeNode, ""); } if (bridgeConfig.GetBridgeNodeList().Count == 0) { bridgeConfig.BridgeActive = string.Empty; } else { bridgeConfig.BridgeActive = bridgeConfig.BridgeNodes; } bridgeConfig.DeploymentVersion++; if (bridgeConfig.GetBridgeNodeList().Count > 0 && !string.IsNullOrEmpty(bridgeConfig.BridgeNodes)) { NCBridgeRPCService _bridge = new NCBridgeRPCService(bridgeConfig.BridgeActive); IBridgeServer _server = _bridge.GetBridgeServer(TimeSpan.FromSeconds(30)); _server.RegisterBridge(bridgeConfig, true, false); } //updating target cache config if (bridgeConfig.TargetCacheConfigList != null) { foreach (TargetCacheCofiguration targetCacheConfig in bridgeConfig.TargetCacheConfigList) { try { if (targetCacheConfig != null) { foreach (string server in targetCacheConfig.Servers.Split(',')) { NCacheRPCService NCache = new NCacheRPCService(server);; ICacheServer cacheServer = null; cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30)); string cacheID = targetCacheConfig.CacheID.ToString(); CacheServerConfig serverConfig = cacheServer.GetCacheConfiguration(cacheID); if (serverConfig.Bridge != null) { serverConfig.Bridge.Servers = bridgeConfig.BridgeNodes; } serverConfig.ConfigVersion++; cacheServer.ConfigureBridgeToCache(serverConfig, userId, paswd, false); } } } catch (Exception ex) { OutputProvider.WriteErrorLine(ex.Message); return; } } } } OutputProvider.WriteLine("Bridge node" + DeleteBridgeNode.ToString() + " removed successfully from Bridge " + BridgeId); } catch (Exception e) { OutputProvider.WriteErrorLine("Bridge not registered on server.\n"); OutputProvider.WriteErrorLine(e.Message); return; } }