Esempio n. 1
0
        public static StartTaskCommand TryParseCommand(Guid partitionId, Queue <string> tokens)
        {
            if (tokens.Count <= 0)
            {
                Trace.WriteWarning(TraceType, "Command missing Task Id");
                return(null);
            }

            string taskId = tokens.Dequeue();

            if (tokens.Count <= 0)
            {
                Trace.WriteWarning(TraceType, "Command missing Task Timeout");
                return(null);
            }

            var nodeTasks = new List <NodeTaskDescription>();

            while (tokens.Count > 0)
            {
                var nodeTask = new NodeTaskDescription {
                    NodeName = tokens.Dequeue()
                };

                if (tokens.Count <= 0)
                {
                    Trace.WriteWarning(TraceType, "Command missing Node Task Type");
                    return(null);
                }

                string command = tokens.Dequeue();

                if (command == InfrastructureServiceCommand.RestartCommand)
                {
                    nodeTask.TaskType = NodeTask.Restart;
                }
                else if (command == InfrastructureServiceCommand.RelocateCommand)
                {
                    nodeTask.TaskType = NodeTask.Relocate;
                }
                else if (command == InfrastructureServiceCommand.RemoveCommand)
                {
                    nodeTask.TaskType = NodeTask.Remove;
                }
                else
                {
                    Trace.WriteWarning(TraceType, "Invalid Node Task Type '{0}'", command);
                    return(null);
                }

                nodeTasks.Add(nodeTask);
            }

            var description = new InfrastructureTaskDescription(
                partitionId,
                taskId,
                DateTime.UtcNow.Ticks,
                new ReadOnlyCollection <NodeTaskDescription>(nodeTasks));

            return(new StartTaskCommand(description));
        }
Esempio n. 2
0
        private void VerifyQueryResults(
            PendingCommandContext commandContext,
            string taskId,
            long instanceId,
            ReadOnlyCollection <NodeTaskDescription> nodeTasks)
        {
            var task = this.infrastructureServiceAgent.QueryInfrastructureTaskAsync(
                commandContext.RemainingTimeout,
                commandContext.CancelToken);

            var queryResult = task.Result;

            bool isTaskFound = false;

            StringBuilder resultBuilder = new StringBuilder();

            foreach (var it in queryResult.Items)
            {
                if (taskId == it.Description.TaskId && instanceId <= it.Description.InstanceId)
                {
                    isTaskFound = true;

                    if (nodeTasks != null && !NodeTaskDescription.AreEqual(nodeTasks, it.Description.NodeTasks))
                    {
                        isTaskFound = false;
                    }
                }

                StringBuilder nodeBuilder = new StringBuilder();
                foreach (var node in it.Description.NodeTasks)
                {
                    nodeBuilder.AppendFormat(CultureInfo.InvariantCulture,
                                             " {0}:{1} ",
                                             node.NodeName,
                                             node.TaskType);
                }

                resultBuilder.AppendFormat(CultureInfo.InvariantCulture,
                                           "Task = '{0}:{1}({2})[{3}]'{4}",
                                           it.Description.TaskId,
                                           it.Description.InstanceId,
                                           it.State,
                                           nodeBuilder.ToString(),
                                           Environment.NewLine);
            }

            Trace.WriteInfo(
                TraceType,
                "QueryResults = {0}",
                resultBuilder.ToString());

            if (!isTaskFound)
            {
                throw Trace.CreateException(
                          TraceType,
                          NativeTypes.FABRIC_ERROR_CODE.E_FAIL,
                          "VerifyQueryResults({0}, {1}, {2}) failed",
                          taskId,
                          instanceId,
                          nodeTasks);
            }
        }
Esempio n. 3
0
        public static int OnVMRemove()
        {
            //step 1: create fabircClientLocal that connects to local node
            Logger.LogInfo(LogTag, "OnVMRemove: creating local FabricClient");
            FabricClient fabircClientLocal = null;

            for (int i = 1; i <= 5; i++)
            {
                try
                {
                    fabircClientLocal = new FabricClient();
                }
                catch (Exception e)
                {
                    if (i < 5)
                    {
                        Logger.LogInfo(LogTag, "creating local client throw execption: {0}. Retrying: {1}", e.ToString(), i);
                    }
                }
            }
            if (fabircClientLocal == null)
            {
                return((int)ErrorCode.Unexpected);
            }


            //step 2: query nodes in the cluster that is up, and use them to create fabricClient that actually sends infraStructureService Command.
            var nodeResult = fabircClientLocal.QueryManager.GetNodeListAsync().Result;

            IPHostEntry   host            = Dns.GetHostEntry(Dns.GetHostName());
            string        ipAddressLocal  = (host.AddressList.First(ip => ip.AddressFamily == AddressFamily.InterNetwork)).ToString();
            string        localNodeName   = "";
            List <string> ipAddressesList = new List <string>();

            for (int idx = 0; idx < nodeResult.Count; idx++)
            {
                if (nodeResult[idx].NodeStatus != NodeStatus.Up)
                {
                    continue;
                }

                string[] result          = nodeResult[idx].IpAddressOrFQDN.Split(':');
                string   IpAddressOrFQDN = result[0];

                Logger.LogInfo(LogTag, "FQDN is {0}, ipAddressLocal is {1}, IpAddressOrFQDN is {2}", VMMNodeList.GetFQDN(), ipAddressLocal, IpAddressOrFQDN);
                if (VMMNodeList.GetFQDN().Equals(IpAddressOrFQDN, StringComparison.CurrentCultureIgnoreCase)
                    ||
                    ipAddressLocal.Equals(IpAddressOrFQDN, StringComparison.CurrentCultureIgnoreCase))
                {
                    localNodeName = nodeResult[idx].NodeName;
                }
                else
                {
                    Logger.LogInfo(LogTag, "node {0} address is {1}", idx, IpAddressOrFQDN);
                    ipAddressesList.Add(IpAddressOrFQDN + ":19000");  //todo:  query clientconnectionendpoints when available.
                }
            }
            Logger.LogInfo(LogTag, "localNodeName: {0}", localNodeName);

            string[] ipAddressesUp = new string[ipAddressesList.Count];
            ipAddressesList.CopyTo(ipAddressesUp, 0);
            FabricClient fabricClient = null;

            for (int i = 1; i <= 5; i++)
            {
                try
                {
                    fabricClient = new FabricClient(ipAddressesUp);
                }
                catch (Exception e)
                {
                    if (i < 5)
                    {
                        Logger.LogInfo(LogTag, "creating client (connecting to cluster) throw execption: {0}. Retrying: {1}", e.ToString(), i);
                    }
                }
            }
            if (fabricClient == null)
            {
                return((int)ErrorCode.Unexpected);
            }


            //step 3: send StartInfrastructureTask
            NodeTaskDescription nodeTaskDescription = new NodeTaskDescription();

            nodeTaskDescription.NodeName = localNodeName;
            nodeTaskDescription.TaskType = NodeTask.Remove;

            string taskId     = "remove" + localNodeName;
            Int64  instanceId = DateTime.Now.Ticks;
            List <NodeTaskDescription> nodeTasks = new List <NodeTaskDescription>();

            nodeTasks.Add(nodeTaskDescription);
            ReadOnlyCollection <NodeTaskDescription> readonlyNodeTasks      = new ReadOnlyCollection <NodeTaskDescription>(nodeTasks);
            InfrastructureTaskDescription            infrastructureTaskDesp = new InfrastructureTaskDescription(taskId, instanceId, readonlyNodeTasks);

            bool taskCompleted = false;

            while (taskCompleted == false)
            {
                try
                {
                    Task <bool> taskStart = fabricClient.ClusterManager.StartInfrastructureTaskAsync(infrastructureTaskDesp);
                    taskCompleted = taskStart.Result;
                    if (taskCompleted == false)
                    {
                        Logger.LogInfo(LogTag, "StartInfrastructureTaskAsync returned false, sleep for 3 second and retry");
                        System.Threading.Thread.Sleep(3000);
                    }
                }
                catch (Exception e)
                {
                    Logger.LogInfo(LogTag, "StartInfrastructureTaskAsync throw execption: {0}. Retrying...", e.ToString());
                }
            }

            Logger.LogInfo(LogTag, "StartInfrastructureTaskAsync returned true");


            //step 4: kill local fabric.exe by stoping fabrichostsvc
            ServiceController service = new ServiceController("FabricHostSvc");

            try
            {
                TimeSpan timeout = TimeSpan.FromMilliseconds(10000);
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
            }
            catch (Exception e)
            {
                Logger.LogInfo(LogTag, "failed to stop service with exception {0}", e.ToString());
            }


            //step 5: send FinishInfrastructureTask
            taskCompleted = false;
            while (taskCompleted == false)
            {
                try
                {
                    Task <bool> taskFinish = fabricClient.ClusterManager.FinishInfrastructureTaskAsync(taskId, instanceId);
                    taskCompleted = taskFinish.Result;
                    if (taskCompleted == false)
                    {
                        Logger.LogInfo(LogTag, "FinishInfrastructureTaskAsync returned false, sleep for 3 second and retry");
                        System.Threading.Thread.Sleep(3000);
                    }
                }
                catch (Exception e)
                {
                    Logger.LogInfo(LogTag, "StartInfrastructureTaskAsync throw execption: {0}. Retrying...", e.ToString());
                }
            }

            Logger.LogInfo(LogTag, "FinishInfrastructureTaskAsync returned true");

            return((int)ErrorCode.Success);
        }