Пример #1
0
        private void ConveyToRegisteredNodes()
        {
            byte[] userId = null;
            byte[] paswd  = null;
            if (UserId != string.Empty && Password != string.Empty)
            {
                userId = EncryptionUtil.Encrypt(UserId);
                paswd  = EncryptionUtil.Encrypt(Password);
            }


            string pId = "";
            NewCacheRegisterationInfo info = cacheServer.GetNewUpdatedCacheConfiguration(CacheName.ToLower(), pId, CacheServer, true);
            // Now update the cache configurations on all the servers where the cache
            //is registered...
            IPAddress address;
            string    clusterIp;
            bool      reregister = false;

            config.ConfigVersion++;
            foreach (string serverName in info.AffectedNodes)
            {
                if (info.AffectedPartitions.Count > 0)
                {
                    foreach (string partId in info.AffectedPartitions)
                    {
                        try
                        {
                            NCache            = new NCacheRPCService(serverName);
                            NCache.ServerName = serverName;
                            if (!IPAddress.TryParse(NCache.ServerName, out address))
                            {
                                clusterIp = cacheServer.GetClusterIP();
                                if (clusterIp != null && clusterIp != string.Empty)
                                {
                                    NCache.ServerName = clusterIp;
                                }
                            }
                            reregister  = true;
                            cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30));
                            cacheServer.ConfigureBridgeToCache(config, userId, paswd, true);
                            cacheServer.HotApplyBridgeReplicator(CacheName, false);
                        }
                        catch (Exception ex)
                        {
                            OutputProvider.WriteErrorLine("Failed to Create Cache on '{0}'. ", NCache.ServerName);
                            OutputProvider.WriteErrorLine("Error Detail: '{0}'. ", ex.Message);
                        }
                        finally
                        {
                            cacheServer.Dispose();
                        }
                    }
                }
                else
                {
                    try
                    {
                        NCache.ServerName = serverName;
                        if (!IPAddress.TryParse(NCache.ServerName, out address))
                        {
                            clusterIp = cacheServer.GetClusterIP();
                            if (clusterIp != null && clusterIp != string.Empty)
                            {
                                NCache.ServerName = clusterIp;
                            }
                        }
                        reregister  = true;
                        cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30));
                        cacheServer.ConfigureBridgeToCache(config, userId, paswd, true);
                        cacheServer.HotApplyBridgeReplicator(CacheName, false);
                    }
                    catch (Exception ex)
                    {
                        OutputProvider.WriteErrorLine("Failed to Create Cache on '{0}'. ", NCache.ServerName);
                        OutputProvider.WriteErrorLine("Error Detail: '{0}'. ", ex.Message);


                        NCache.Dispose();
                        return;
                    }
                    finally
                    {
                        cacheServer.Dispose();
                    }
                }
            }

            /*
             *
             * byte[] userId = null;
             * byte[] paswd = null;
             * if (UserId != string.Empty && Password != string.Empty)
             * {
             *  userId = EncryptionUtil.Encrypt(UserId);
             *  paswd = EncryptionUtil.Encrypt(Password);
             * }
             * Alachisoft.NCache.Bridging.Configuration.BridgeConfiguration bridgeConfig = _bridgeServer.GetBridgeConfiguration(BridgeId);
             * List<TargetCacheCofiguration> previouslyAddedCaches = bridgeConfig.TargetCacheConfigList;
             * char[] separater = { ',' };
             * foreach (TargetCacheCofiguration pCache in previouslyAddedCaches)
             * {
             *  if (pCache.CacheID.ToLower().Equals(CacheId.ToLower()))
             *  {
             *      //if exists than remove
             *      foreach (string server in pCache.Servers.Split(separater).ToList())
             *      {
             *          NCacheRPCService nNCache = new NCacheRPCService(server);
             *          cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30));
             *          cacheServer.ConfigureBridgeToCache(config, userId, paswd, true);
             *          cacheServer.HotApplyBridgeReplicator(CacheId, false);
             *
             *      }
             *  }
             *
             * }
             * */
        }
Пример #2
0
        /// <summary>
        /// The main entry point for the tool.
        /// </summary>
        static public void Run(string[] args)
        {
            try
            {
                object param = new AddNodeParam();
                CommandLineArgumentParser.CommandLineParser(ref param, args);
                cParam         = (AddNodeParam)param;
                cParam.CacheId = cParam.CacheId.ToLower();
                if (cParam.IsUsage)
                {
                    AssemblyUsage.PrintLogo(cParam.IsLogo);
                    AssemblyUsage.PrintUsage();
                    return;
                }

                if (!ValidateParameters())
                {
                    return;
                }
                if (cParam.Port != -1)
                {
                    NCache.Port = cParam.Port;
                }
                if (cParam.Port == -1)
                {
                    NCache.Port = NCache.UseTcp ? CacheConfigManager.NCacheTcpPort : CacheConfigManager.HttpPort;
                }


                try
                {
                    if (cParam.ExistingServer != null || cParam.ExistingServer != string.Empty)
                    {
                        NCache.ServerName = cParam.ExistingServer;
                    }
                    IPAddress address;
                    string    clusterIp;

                    ICacheServer m = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30));
                    if (!IPAddress.TryParse(cParam.NewServer, out address))
                    {
                        clusterIp = m.GetClusterIP();
                        if (clusterIp != null && clusterIp != string.Empty)
                        {
                            cParam.NewServer = clusterIp;
                        }
                    }

                    NCache.ServerName = cParam.ExistingServer;
                    m = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30));

                    if (!IPAddress.TryParse(cParam.ExistingServer, out address))
                    {
                        clusterIp = m.GetClusterIP();
                        if (clusterIp != null && clusterIp != string.Empty)
                        {
                            cParam.ExistingServer = clusterIp;
                        }
                    }
                    CacheStatusOnServerContainer isClustered = m.IsClusteredCache(cParam.CacheId.ToLower());
                    CacheStatusOnServer          result      = isClustered.cacheStatus;

                    if (result == CacheStatusOnServer.Unregistered)
                    {
                        throw new Exception("The requested cache is not registered on the source node.");
                    }
                    else if (result == CacheStatusOnServer.LocalCache)
                    {
                        throw new Exception("The AddNode Tool can be used with clustered caches only");
                    }

                    Console.WriteLine("Adding node '{0}' to cache '{1}'.", cParam.NewServer, cParam.CacheId);

                    NewCacheRegisterationInfo info = m.GetNewUpdatedCacheConfiguration(cParam.CacheId, _partId, cParam.NewServer, true);

                    //muds:
                    //first of all try to register the cache on the destination server.
                    try
                    {
                        NCache.ServerName = cParam.NewServer;
                        m = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30));

                        if (!IPAddress.TryParse(cParam.NewServer, out address))
                        {
                            clusterIp = m.GetClusterIP();
                            if (clusterIp != null && clusterIp != string.Empty)
                            {
                                cParam.NewServer = clusterIp;
                            }
                        }

                        m.RegisterCache(cParam.CacheId, info.UpdatedCacheConfig, _partId, true, false);
                    }
                    catch (Exception ex)
                    {
                        Console.Error.WriteLine("Failed to Create Cache on '{0}'. ", NCache.ServerName);
                        Console.Error.WriteLine("Error Detail: '{0}'. ", ex.Message);

                        LogEvent(ex.Message);
                        NCache.Dispose();
                        return;
                    }
                    finally
                    {
                        m.Dispose();
                    }
                    //muds:
                    // Now update the cache configurations on all the servers where the cache
                    //is registered.
                    foreach (string serverName in info.AffectedNodes)
                    {
                        if (info.AffectedPartitions.Count > 0)
                        {
                            foreach (string partId in info.AffectedPartitions)
                            {
                                try
                                {
                                    NCache.ServerName = serverName;
                                    if (!IPAddress.TryParse(NCache.ServerName, out address))
                                    {
                                        clusterIp = m.GetClusterIP();
                                        if (clusterIp != null && clusterIp != string.Empty)
                                        {
                                            NCache.ServerName = clusterIp;
                                        }
                                    }
                                    reregister = true;
                                    m          = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30));

                                    m.RegisterCache(cParam.CacheId, info.UpdatedCacheConfig, _partId, true, false);
                                }
                                catch (Exception ex)
                                {
                                    Console.Error.WriteLine("Failed to Create Cache on '{0}'. ", NCache.ServerName);
                                    Console.Error.WriteLine("Error Detail: '{0}'. ", ex.Message);

                                    LogEvent(ex.Message);
                                }
                                finally
                                {
                                    m.Dispose();
                                }
                            }
                        }
                        else
                        {
                            try
                            {
                                NCache.ServerName = serverName;
                                if (!IPAddress.TryParse(NCache.ServerName, out address))
                                {
                                    clusterIp = m.GetClusterIP();
                                    if (clusterIp != null && clusterIp != string.Empty)
                                    {
                                        NCache.ServerName = clusterIp;
                                    }
                                }
                                reregister = true;
                                m          = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30));

                                m.RegisterCache(cParam.CacheId, info.UpdatedCacheConfig, _partId, true, false);
                            }
                            catch (Exception ex)
                            {
                                Console.Error.WriteLine("Failed to Create Cache on '{0}'. ", NCache.ServerName);
                                Console.Error.WriteLine("Error Detail: '{0}'. ", ex.Message);

                                LogEvent(ex.Message);
                                NCache.Dispose();
                                return;
                            }
                            finally
                            {
                                m.Dispose();
                            }
                        }
                    }
                    List <string> serversToUpdate = new List <string>();
                    foreach (Config.NewDom.ServerNode node in info.UpdatedCacheConfig.CacheDeployment.Servers.ServerNodeList)
                    {
                        serversToUpdate.Add(node.IP);
                    }
                    Management.Management.Util.ManagementWorkFlow.UpdateServerMappingConfig(serversToUpdate.ToArray());
                    //Add in client.ncconf
                    Dictionary <string, Dictionary <int, Management.ClientConfiguration.Dom.CacheServer> > serversPriorityList = new Dictionary <string, Dictionary <int, Alachisoft.NCache.Management.ClientConfiguration.Dom.CacheServer> >();
                    ToolServerOperations.ClientConfigUtil _clientConfigUtil = new ToolServerOperations.ClientConfigUtil();
                    try
                    {
                        ArrayList clusterNodes = info.UpdatedCacheConfig.CacheDeployment.Servers.NodesList;

                        foreach (Alachisoft.NCache.Config.NewDom.ServerNode nodei in clusterNodes)
                        {
                            serversPriorityList.Add(nodei.IP, _clientConfigUtil.GetPrioritizedServerListForClient(nodei.IP, cParam.CacheId, clusterNodes));
                        }

                        Alachisoft.NCache.Config.NewDom.ServerNode nodeForClientList = new Config.NewDom.ServerNode();   //Hack: priority list requires a serverIP and client Ip cant be used hence!!
                        foreach (Alachisoft.NCache.Config.NewDom.ServerNode node in clusterNodes)
                        {
                            NCache.ServerName = node.IP;
                            ICacheServer _cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30));
                            Management.ClientConfiguration.CacheServerList _cacheServerList = new Management.ClientConfiguration.CacheServerList(serversPriorityList[node.IP]);
                            _cacheServer.UpdateClientServersList(cParam.CacheId, _cacheServerList, RtContextValue.NCACHE.ToString());
                            nodeForClientList = node;
                        }


                        List <Alachisoft.NCache.Config.Dom.ClientNode> clientNodeList = info.UpdatedCacheConfig.CacheDeployment.ClientNodes.NodesList;
                        foreach (Alachisoft.NCache.Config.Dom.ClientNode node in clientNodeList)
                        {
                            NCache.ServerName = node.Name;
                            ICacheServer _cacheServer = NCache.GetCacheServer(new TimeSpan(0, 0, 0, 30));
                            Management.ClientConfiguration.CacheServerList _cacheServerList = new Management.ClientConfiguration.CacheServerList(serversPriorityList[nodeForClientList.IP]);
                            _cacheServer.UpdateClientServersList(cParam.CacheId, _cacheServerList, RtContextValue.NCACHE.ToString());
                        }
                    }
                    catch (Exception e)
                    {
                    }

                    Console.WriteLine("'{0}' successfully added to cache '{1}'.\n", cParam.NewServer,
                                      cParam.CacheId);
                }

                catch (ConfigurationException e)
                {
                    Console.Error.WriteLine("Failed to add '{0}' to '{1}'. Error: {2} ", NCache.ServerName.ToLower(), cParam.CacheId, e.Message);
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("Failed to add '{0}' to '{1}'. Error: {2} ", NCache.ServerName.ToLower(), cParam.CacheId, e.Message);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error : {0}", e.Message);
            }
            finally
            {
                NCache.Dispose();
            }
        }
Пример #3
0
        static public void Run(string[] args)
        {
            try
            {
                object param = new RemoveNodeParam();
                CommandLineArgumentParser.CommandLineParser(ref param, args);
                cParam = (RemoveNodeParam)param;
                if (cParam.IsUsage)
                {
                    AssemblyUsage.PrintLogo(cParam.IsLogo);
                    AssemblyUsage.PrintUsage();
                    return;
                }

                if (!ValidateParameters())
                {
                    return;
                }

                if (cParam.Port != -1)
                {
                    ncacheService.Port = cParam.Port;
                }

                if (cParam.Port == -1)
                {
                    ncacheService.Port = ncacheService.UseTcp ? CacheConfigManager.NCacheTcpPort : CacheConfigManager.HttpPort;
                }

                if (cParam.Server != null || cParam.Server != string.Empty)
                {
                    ncacheService.ServerName = cParam.Server;
                }
                else
                {
                    cParam.Server = ncacheService.ServerName;
                }

                ICacheServer m = ncacheService.GetCacheServer(new TimeSpan(0, 0, 0, 30));
                if (m != null)
                {
                    try
                    {
                        IPAddress address;
                        if (!IPAddress.TryParse(ncacheService.ServerName, out address))
                        {
                            string clusterIp = m.GetClusterIP();
                            if (clusterIp != null && clusterIp != string.Empty)
                            {
                                ncacheService.ServerName = clusterIp;
                            }
                        }
                        CacheStatusOnServerContainer isClustered = m.IsClusteredCache(cParam.CacheId);

                        CacheStatusOnServer result = isClustered.cacheStatus;
                        if (result == CacheStatusOnServer.Unregistered)
                        {
                            throw new Exception("The requested cache is not registered on the specified server.");
                        }
                        else if (result == CacheStatusOnServer.LocalCache)
                        {
                            throw new Exception("RemoveNode Tool can be used with clustered caches only.");
                        }


                        Console.WriteLine("Removing '{0}' from the cache '{1}'.", ncacheService.ServerName.ToLower(), cParam.CacheId);
                        NewCacheRegisterationInfo info = m.GetNewUpdatedCacheConfiguration(cParam.CacheId, null, ncacheService.ServerName, false);    //.GetNewUpdatedCacheConfiguration(cParam.CacheId, null, NCache.ServerName, false);

                        m.UnregisterCache(cParam.CacheId, string.Empty, true);

                        foreach (string serverName in info.AffectedNodes)
                        {
                            ncacheService.ServerName = serverName;
                            m = ncacheService.GetCacheServer(new TimeSpan(0, 0, 0, 30));

                            m.RegisterCache(cParam.CacheId, info.UpdatedCacheConfig, null, true, false);
                        }

                        //Remove from client.ncconf
                        Dictionary <string, Dictionary <int, Management.ClientConfiguration.Dom.CacheServer> > serversPriorityList = new Dictionary <string, Dictionary <int, Alachisoft.NCache.Management.ClientConfiguration.Dom.CacheServer> >();

                        try
                        {
                            ArrayList clusterNodes = info.UpdatedCacheConfig.CacheDeployment.Servers.NodesList;
                            ToolServerOperations.ClientConfigUtil _clientConfigUtil = new ToolServerOperations.ClientConfigUtil();

                            foreach (Alachisoft.NCache.Config.NewDom.ServerNode nodei in clusterNodes)
                            {
                                serversPriorityList.Add(nodei.IP, _clientConfigUtil.GetPrioritizedServerListForClient(nodei.IP, cParam.CacheId, clusterNodes));
                            }
                            foreach (Alachisoft.NCache.Config.NewDom.ServerNode node in clusterNodes)
                            {
                                ncacheService.ServerName = node.IP;
                                ICacheServer _cacheServer = ncacheService.GetCacheServer(new TimeSpan(0, 0, 0, 30));
                                Management.ClientConfiguration.CacheServerList _cacheServerList = new Management.ClientConfiguration.CacheServerList(serversPriorityList[node.IP]);
                                _cacheServer.UpdateClientServersList(cParam.CacheId, _cacheServerList, RtContextValue.NCACHE.ToString());
                            }
                            serversPriorityList.Clear();
                            List <ClientNode> clusterClientNodes = info.UpdatedCacheConfig.CacheDeployment.ClientNodes.NodesList;
                            foreach (Alachisoft.NCache.Config.Dom.ClientNode nodei in clusterClientNodes)
                            {
                                serversPriorityList.Add(nodei.Name, _clientConfigUtil.GetPrioritizedServerListForClient(nodei.Name, cParam.CacheId, clusterNodes));
                            }
                            foreach (Alachisoft.NCache.Config.Dom.ClientNode node in clusterClientNodes)
                            {
                                ncacheService.ServerName = node.Name;
                                ICacheServer _cacheServer = ncacheService.GetCacheServer(new TimeSpan(0, 0, 0, 30));
                                Management.ClientConfiguration.CacheServerList _cacheServerList = new Management.ClientConfiguration.CacheServerList(serversPriorityList[node.Name]);
                                _cacheServer.UpdateClientServersList(cParam.CacheId, _cacheServerList, node.RuntimeContextString);
                            }
                        }
                        catch (Exception e)
                        {
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine("Failed to remove '{0}' from '{1}'. Error: {2} ", ncacheService.ServerName.ToLower(), cParam.CacheId, e.Message);
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error : {0}", e.Message);
            }
            finally
            {
                if (ncacheService != null)
                {
                    ncacheService.Dispose();
                }
            }
        }