public object Clone()
        {
            HashMapBucket hmBucket = new HashMapBucket(_permanentAddress, _bucketId);

            hmBucket.TempAddress = _tempAddress;
            hmBucket.Status      = Status;
            return(hmBucket);
        }
Exemple #2
0
        public override bool Equals(object obj)
        {
            HashMapBucket bucket = obj as HashMapBucket;

            if (bucket != null)
            {
                return(this.BucketId == bucket.BucketId);
            }
            return(false);
        }
        /// <summary>
        /// A new map is required when a member leaves or joins the cluster.
        /// This method returns a new map based on the input paramameters.
        /// </summary>
        /// <param name="member">Address of the member that has either left
        /// or joined the cluster</param>
        /// <param name="isNew">A flag. True if the member has joined otherwise false.</param>
        /// <returns>A new hashmap instance</returns>
        public virtual DistributionMaps GetMaps(DistributionInfoData distInfoData)
        {
            ArrayList tmpMap = null;
            Hashtable bucketsOwnershipMap = null;
            ArrayList partitionNodes = new ArrayList();

            _sync.AcquireWriterLock(Timeout.Infinite);
            try
            {
                if (_installedHashMap == null)
                {
                    tmpMap = new ArrayList(TotalBuckets);
                    for (int i = 0; i < TotalBuckets; i++)
                    {
                        HashMapBucket bucket = new HashMapBucket(distInfoData.AffectedNode.NodeAddress, i, BucketStatus.Functional);
                        tmpMap.Add(bucket);

                    }

                    _existingMembers.Add(distInfoData.AffectedNode.NodeAddress);

                    _lastCreatedHashMap = tmpMap.Clone() as ArrayList;

                    bucketsOwnershipMap = GetBucketsOwnershipMap(_lastCreatedHashMap);
                    return new DistributionMaps(_lastCreatedHashMap, bucketsOwnershipMap);
                }
                else if (_lastCreatedHashMap == null)
                {
                    _lastCreatedHashMap = _installedHashMap.Clone() as ArrayList;
                }

                switch (distInfoData.ClustActivity)
                {
                    case ClusterActivity.NodeJoin:
                        try
                        {
                            return GetMapsOnNodeJoining(distInfoData);
                        }
                        catch (Exception e)
                        {
                            if (NCacheLog.IsErrorEnabled) NCacheLog.Error("DistributionMgr.GetMaps()", e.ToString());
                            break;
                        }

                    case ClusterActivity.NodeLeave:

                        try
                        {
                            return GetMapsOnNodeLeaving(distInfoData);
                        }
                        catch (Exception e)
                        {
                            if (NCacheLog.IsErrorEnabled) NCacheLog.Error("DistributionMgr.GetMaps()", e.ToString());
                            break;
                        }

                    case ClusterActivity.None:
                        BalanceNodeMgr bnMgr = new BalanceNodeMgr(null);
                        DistributionMaps result = bnMgr.BalanceNodes(distInfoData, _lastCreatedHashMap, _bucketsStats, _existingMembers);
                        if (result.Hashmap != null)
                        {
                            _lastCreatedHashMap = result.Hashmap.Clone() as ArrayList;
                            result.BucketsOwnershipMap = GetBucketsOwnershipMap(_lastCreatedHashMap);
                        }
                        return result;

                    default:
                        break;
                }
            }
            finally
            {
                _sync.ReleaseWriterLock();
            }
            return null;
        }
Exemple #4
0
 public object Clone()
 {
     HashMapBucket hmBucket = new HashMapBucket(_permanentAddress, _bucketId);
     hmBucket.TempAddress = _tempAddress;
     hmBucket.Status = Status;
     return hmBucket;
 }
        /// <summary>
        /// Updates the state transfer task in synchronus way. It adds/remove buckets
        /// to be transferred by the state transfer task.
        /// </summary>
        /// <param name="myBuckets"></param>
        public bool UpdateStateTransfer(ArrayList myBuckets, int updateId)
        {
            if (_parent.HasDisposed)
                return false;

            StringBuilder sb = new StringBuilder();
            lock (_updateIdMutex)
            {
                if (updateId != updateCount)
                {
                    if (_parent.NCacheLog.IsErrorEnabled) _parent.Context.NCacheLog.CriticalInfo(this.Name + "UpdateStateTxfr", " Do not need to update the task as update id does not match; provided id :" + updateId + " currentId :" + updateCount);
                    return false;
                }
            }

            lock (_stateTxfrMutex)
            {
                try
                {
                    if (myBuckets != null)
                    {
                        if (_parent.Context.NCacheLog.IsInfoEnabled) _parent.Context.NCacheLog.Info(Name + ".UpdateStateTxfr", " my buckets " + myBuckets.Count);
                        //we work on the copy of the map.
                        ArrayList buckets = myBuckets.Clone() as ArrayList;
                        ArrayList leavingNodes = new ArrayList();


                        if (_sparsedBuckets != null && _sparsedBuckets.Count > 0)
                        {
                            IEnumerator e = _sparsedBuckets.GetEnumerator();

                            lock (_sparsedBuckets.SyncRoot)
                            {
                                while (e.MoveNext())
                                {
                                    BucketsPack bPack = (BucketsPack)e.Current;
                                    ArrayList bucketIds = bPack.BucketIds.Clone() as ArrayList;
                                    foreach (int bucketId in bucketIds)
                                    {
                                        HashMapBucket current = new HashMapBucket(null, bucketId);

                                        if (!buckets.Contains(current))
                                        {
                                            ((BucketsPack)e.Current).BucketIds.Remove(bucketId);
                                        }
                                        else
                                        {
                                            HashMapBucket bucket = buckets[buckets.IndexOf(current)] as HashMapBucket;
                                            if (!bPack.Owner.Equals(bucket.PermanentAddress))
                                            {
                                                //either i have become owner of the bucket or 
                                                //some one else for e.g a replica node 
                                                if (_parent.Context.NCacheLog.IsInfoEnabled) _parent.Context.NCacheLog.Info(Name + ".UpdateStateTxfer", bucket.BucketId + "bucket owner changed old :" + bPack.Owner + " new :" + bucket.PermanentAddress);
                                                bPack.BucketIds.Remove(bucketId);
                                            }
                                        }
                                    }
                                    if (bPack.BucketIds.Count == 0)
                                    {
                                        //This owner has left.
                                        leavingNodes.Add(bPack.Owner);
                                    }

                                }
                                foreach (Address leavigNode in leavingNodes)
                                {
                                    BucketsPack bPack = new BucketsPack(null, leavigNode);
                                    _sparsedBuckets.Remove(bPack);
                                }
                                leavingNodes.Clear();
                            }
                        }

                        if (_filledBuckets != null && _filledBuckets.Count > 0)
                        {
                            IEnumerator e = _filledBuckets.GetEnumerator();
                            lock (_filledBuckets.SyncRoot)
                            {
                                while (e.MoveNext())
                                {
                                    BucketsPack bPack = (BucketsPack)e.Current;
                                    ArrayList bucketIds = bPack.BucketIds.Clone() as ArrayList;
                                    foreach (int bucketId in bucketIds)
                                    {
                                        HashMapBucket current = new HashMapBucket(null, bucketId);
                                        if (!buckets.Contains(current))
                                        {
                                            ((BucketsPack)e.Current).BucketIds.Remove(bucketId);
                                        }
                                        else
                                        {
                                            HashMapBucket bucket = buckets[buckets.IndexOf(current)] as HashMapBucket;
                                            if (!bPack.Owner.Equals(bucket.PermanentAddress))
                                            {
                                                //either i have become owner of the bucket or 
                                                //some one else for e.g a replica node 
                                                bPack.BucketIds.Remove(bucketId);
                                            }
                                        }
                                    }

                                    if (bPack.BucketIds.Count == 0)
                                    {
                                        //This owner has left.
                                        leavingNodes.Add(bPack.Owner);
                                    }

                                }
                                foreach (Address leavigNode in leavingNodes)
                                {
                                    BucketsPack bPack = new BucketsPack(null, leavigNode);
                                    _filledBuckets.Remove(bPack);
                                }
                                leavingNodes.Clear();
                            }
                        }

                        //Now we add those buckets which we have to be state transferred
                        //and are not currently in our list
                        IEnumerator ie = buckets.GetEnumerator();

                        while (ie.MoveNext())
                        {
                            HashMapBucket bucket = ie.Current as HashMapBucket;
                            if (_localAddress.Equals(bucket.TempAddress) && !_localAddress.Equals(bucket.PermanentAddress))
                            {
                                BucketsPack bPack = new BucketsPack(null, bucket.PermanentAddress);

                                if (IsSparsedBucket(bucket.BucketId, bucket.PermanentAddress))
                                {
                                    int index = _sparsedBuckets.IndexOf(bPack);
                                    if (index != -1)
                                    {
                                        bPack = _sparsedBuckets[index] as BucketsPack;
                                    }
                                    else
                                        _sparsedBuckets.Add(bPack);

                                    if (!bPack.BucketIds.Contains(bucket.BucketId))
                                    {
                                        bPack.BucketIds.Add(bucket.BucketId);
                                    }

                                }
                                else
                                {
                                    int index = _filledBuckets.IndexOf(bPack);
                                    if (index != -1)
                                    {
                                        bPack = _filledBuckets[index] as BucketsPack;
                                    }
                                    else
                                    {
                                        _filledBuckets.Add(bPack);
                                    }

                                    if (!bPack.BucketIds.Contains(bucket.BucketId))
                                    {
                                        bPack.BucketIds.Add(bucket.BucketId);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (NullReferenceException ex)
                {
                    _parent.Context.NCacheLog.Error(Name + ".UpdateStateTxfr", ex.ToString());
                }
                catch (Exception e)
                {
                    _parent.Context.NCacheLog.Error(Name + ".UpdateStateTxfr", e.ToString());
                }
                finally
                {
                    if (_parent.Context.NCacheLog.IsInfoEnabled) _parent.Context.NCacheLog.Info(Name + ".UpdateStateTxfr", " Pulsing waiting thread");
                    System.Threading.Monitor.PulseAll(_stateTxfrMutex);
                }
            }

            return true;

        }