Пример #1
0
 public void SetBackupOf(String seller, ZNodesDataStructures.SellerNode newBackup)
 {
     if (sellersData.ContainsKey(seller))
     {
         if (sellersData[seller].ContainsKey(ZNodesDataStructures.SellerNode.NodeRole.Backup))
         {
             sellersData[seller][ZNodesDataStructures.SellerNode.NodeRole.Backup] = newBackup;
         }
         else
         {
             sellersData[seller].Add(ZNodesDataStructures.SellerNode.NodeRole.Backup, newBackup);
         }
     }
 }
Пример #2
0
 public void SetPrimaryOf(String seller, ZNodesDataStructures.SellerNode newPrimary)
 {
     if (sellersData.ContainsKey(seller))
     {
         if (sellersData[seller].ContainsKey(ZNodesDataStructures.SellerNode.NodeRole.Main))
         {
             sellersData[seller][ZNodesDataStructures.SellerNode.NodeRole.Main] = newPrimary;
         }
         else
         {
             sellersData[seller].Add(ZNodesDataStructures.SellerNode.NodeRole.Main, newPrimary);
         }
     }
 }
        /// <summary>
        /// Load balancing algorithm is using this function to update the snapshot after its done
        /// </summary>
        /// <param name="snapshot"></param>

        public void updateMachineData(Dictionary <string, ZNodesDataStructures.MachineNode> snapshot)
        {
            Dictionary <String, ZNodesDataStructures.MachineNode> oldMachineData = new Dictionary <string, ZNodesDataStructures.MachineNode>(tree.Machines);

            calculatedSnapshot = snapshot; // TODO: remove this someday, (funny, huh ?)
            // Update Machines local tree
            foreach (var machine in snapshot)
            {
                var currentMachineRecord = machine.Value;
                var previousMachineValue = tree.Machines[machine.Key];

                bool updateZKTree = false;
                if (machine.Key == MachineName)
                {
                    updateZKTree = true;
                }


                if (updateZKTree)
                {
                    zk.SetData(id, currentMachineRecord);
                }
                tree.Machines[machine.Key] = currentMachineRecord;

                var addPrimaries    = currentMachineRecord.primaryOf.Except(previousMachineValue.primaryOf);
                var removePrimaries = previousMachineValue.primaryOf.Except(currentMachineRecord.primaryOf);

                var addBackup    = currentMachineRecord.backsUp.Except(previousMachineValue.backsUp);
                var removeBackup = previousMachineValue.backsUp.Except(currentMachineRecord.backsUp);

                foreach (var pToAdd in addPrimaries)
                {
                    ZNodesDataStructures.SellerNode primaryRecord = new ZNodesDataStructures.SellerNode()
                    {
                        machineName = machine.Key,
                        role        = ZNodesDataStructures.SellerNode.NodeRole.Main,
                        uri         = currentMachineRecord.uri,
                        version     = -1
                    };
                    tree.SetPrimaryOf(pToAdd, primaryRecord);
                    if (updateZKTree)
                    {
                        zk.Create(SellersPath + "/" + pToAdd + "/" + machine.Key, primaryRecord, Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral);
                    }
                }

                foreach (var pToRemove in removePrimaries)
                {
                    tree.RemovePrimaryOf(pToRemove, machine.Key);
                    if (updateZKTree)
                    {
                        zk.Delete(SellersPath + "/" + pToRemove + "/" + machine.Key);
                    }
                }

                foreach (var bToAdd in addBackup)
                {
                    ZNodesDataStructures.SellerNode primaryRecord = new ZNodesDataStructures.SellerNode()
                    {
                        machineName = machine.Key,
                        role        = ZNodesDataStructures.SellerNode.NodeRole.Backup,
                        uri         = currentMachineRecord.uri,
                        version     = -1
                    };
                    tree.SetBackupOf(bToAdd, primaryRecord);
                    if (updateZKTree)
                    {
                        zk.Create(SellersPath + "/" + bToAdd + "/" + machine.Key, primaryRecord, Ids.OPEN_ACL_UNSAFE, CreateMode.Ephemeral);
                    }
                }

                foreach (var bToRemove in removeBackup)
                {
                    tree.RemoveBackupOf(bToRemove, machine.Key);
                    if (updateZKTree)
                    {
                        zk.Delete(SellersPath + "/" + bToRemove + "/" + machine.Key);
                    }
                }


                //tree.Machines[machine.Key] = machine.Value;
            }
            // STOP HERE
        }