Example #1
0
 public void addMachine(String machineName, ZNodesDataStructures.MachineNode newData)
 {
     if (machinesData.ContainsKey(machineName))
     {
         Console.WriteLine("*WARNING* Trying to add machine " + machineName + " which is already contained in local view");
         machinesData[machineName] = newData;
     }
     else
     {
         machinesData.Add(machineName, newData);
     }
 }
Example #2
0
 public ZNodesDataStructures.MachineNode updateMachine(String machineName, ZNodesDataStructures.MachineNode newData)
 {
     ZNodesDataStructures.MachineNode oldData = null;
     if (!machinesData.ContainsKey(machineName))
     {
         Console.WriteLine("*WARNING* Trying to update machine " + machineName + " which isn't contained in local view");
     }
     if (machinesData.ContainsKey(machineName))
     {
         oldData = machinesData[machineName];
         machinesData[machineName] = newData;
     }
     return(oldData);
 }
        private void machinesNodeChanged()
        {
            if (!Connected)
            {
                Console.WriteLine("[" + MachineName + "] Ignoring machinesNodeChanged because connection dropped");
                return;
            }
            List <String> machines        = zk.GetChildren(MachinesPath, false);
            var           machinesDropped = tree.Machines.Keys.Except(machines);
            var           machinesJoined  = machines.Except(tree.Machines.Keys);

            if ((machinesJoined.Count() + machinesDropped.Count()) > 2)
            {
                Console.WriteLine("[" + MachineName + "] *WARNING* - more changes than expected, ignoring all but first:");
                Console.WriteLine("[" + MachineName + "] *WARNING* - joined  : " + String.Join(" ", machinesJoined.ToArray()));
                Console.WriteLine("[" + MachineName + "] *WARNING* - dropped : " + String.Join(" ", machinesJoined.ToArray()));
            }

            if (!machinesJoined.IsEmpty())
            {
                // 1. Add new nodes to tree
                // 2. Run callback if exists
                String joined = machinesJoined.First();
                ZNodesDataStructures.MachineNode joinedData = zk.GetData <ZNodesDataStructures.MachineNode>(MachinesPath + "/" + joined, new MachineNodeWatch(this)); // Set watch on new node
                tree.addMachine(joined, joinedData);

                if (null != mJoined)
                {
                    printRemoteAndLocalTree("PRE-CALLBACKS");
                    mJoined(joined, tree.Machines[joined].originalSellerName, tree.Machines[joined].uri);
                    printRemoteAndLocalTree("POST-CALLBACKS");
                }
            }
            if (!machinesDropped.IsEmpty())
            {
                Console.WriteLine("[" + MachineName + "] Machine dropped - " + String.Join(" ", machinesDropped));
                // - Dropped clients should be dealt exclusively by the machineNodeChange callback and not here

                // 1. Remove dropped machine from tree
                // 2. Run callback if exists
                //String dropped = machinesDropped.First();
                //ZNodesDataStructures.MachineNode droppedData = tree.Machines[dropped];
                //tree.removeMachine(dropped);

                //if (null != mDropped)
                //{
                //    mDropped(droppedData.primaryOf, droppedData.backsUp);
                //}
            }
        }
 /// <summary>
 /// Create a node under /cluster/MACHINES for this
 /// </summary>
 private void registerMachinesNode()
 {
     try
     {
         var node = new ZNodesDataStructures.MachineNode();
         node.uri = intraClusterService;
         node.originalSellerName = originalSeller;
         node.primaryOf.Add(originalSeller);
         byte[] serializedNode = ZNodesDataStructures.serialize(node);
         id = zk.Create(MachinesPath + "/" + MACHINE_PREFIX, serializedNode, Ids.OPEN_ACL_UNSAFE, CreateMode.EphemeralSequential);
         gotName.Set();
     }
     catch (KeeperException e)
     {
         Console.WriteLine("Registering machine node failed\n" + e.Message);
         throw e;
     }
 }
        internal void machineNodeChanged(WatchedEvent @event)
        {
            Console.WriteLine("[" + MachineName + "] machineNodeDataChanged event: " + @event.Type + " on " + @event.Path);
            Console.WriteLine("[" + MachineName + "] Event fired on " + id);
            if (@event.Type == EventType.NodeDataChanged)
            {
                if (@event.Path == id)
                {
                    Console.WriteLine("[" + MachineName + "] Got update message to my own machine node - ignoring...");
                }
            }

            if (@event.State != KeeperState.Disconnected)
            {
                if (@event.Type != EventType.NodeDeleted) // If it wasn't node deletion - don't re-add listener to dead machine nodes
                {
                    zk.GetData <ZNodesDataStructures.MachineNode>(@event.Path, new MachineNodeWatch(this));
                }
                else
                {
                    if (@event.Type == EventType.NodeDeleted) // Some machine was deleted
                    {
                        if (@event.Path != id)                // Deleted machine is not me
                        {
                            String machineName = @event.Path.Substring(@event.Path.LastIndexOf('/') + 1);
                            ZNodesDataStructures.MachineNode machineData = tree.Machines[machineName];
                            Console.WriteLine("[" + MachineName + "] MachineData of " + machineName + ": ");
                            Console.WriteLine(machineData.ToString());
                            tree.removeMachine(machineName);
                            if (mDropped != null)
                            {
                                printRemoteAndLocalTree("PRE-DROP-CALLBACK");

                                Console.WriteLine("[" + MachineName + "] Calling machine dropped callback");
                                mDropped(machineData.primaryOf, machineData.backsUp);

                                printRemoteAndLocalTree("POST-DROP-CALLBACK");
                            }
                        }
                    }
                }
            }
        }