Ejemplo n.º 1
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;
                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);
                    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();
            }
        }