コード例 #1
0
ファイル: BucketsPack.cs プロジェクト: yaobos/NCache
        public object Clone()
        {
            BucketsPack pack = new BucketsPack(new ArrayList(_bucketIds), (Address)_owner.Clone());

            return(pack);
        }
コード例 #2
0
        /// <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;

        }
コード例 #3
0
        public void UpdateBuckets()
        {
            lock (_parent._internalCache.Statistics.LocalBuckets.SyncRoot)
            {
                IDictionaryEnumerator ide = _parent._internalCache.Statistics.LocalBuckets.GetEnumerator();
                while (ide.MoveNext())
                {
                    if (_isInStateTxfr)
                    {
                        if (_sparsedBuckets != null && _sparsedBuckets.Count > 0)
                        {
                            ArrayList tmp = _sparsedBuckets.Clone() as ArrayList;
                            IEnumerator e = tmp.GetEnumerator();
                            lock (_sparsedBuckets.SyncRoot)
                            {
                                while (e.MoveNext())
                                {
                                    ArrayList bucketIds = ((BucketsPack)e.Current).BucketIds.Clone() as ArrayList;
                                    foreach (int bucketId in bucketIds)
                                    {
                                        if (!_parent._internalCache.Statistics.LocalBuckets.Contains(bucketId))
                                        {
                                            if (((HashMapBucket)_parent.HashMap[bucketId]).Status != BucketStatus.UnderStateTxfr)
                                                ((BucketsPack)e.Current).BucketIds.Remove(bucketId);
                                        }
                                    }
                                }
                            }
                        }
                        if (_filledBuckets != null && _filledBuckets.Count > 0)
                        {
                            ArrayList tmp = _filledBuckets.Clone() as ArrayList;
                            IEnumerator e = tmp.GetEnumerator();
                            lock (_filledBuckets.SyncRoot)
                            {
                                while (e.MoveNext())
                                {
                                    ArrayList bucketIds = ((BucketsPack)e.Current).BucketIds.Clone() as ArrayList;
                                    foreach (int bucketId in bucketIds)
                                    {
                                        if (!_parent._internalCache.Statistics.LocalBuckets.Contains(bucketId))
                                        {
                                            if (((HashMapBucket)_parent.HashMap[bucketId]).Status != BucketStatus.UnderStateTxfr)
                                                ((BucketsPack)e.Current).BucketIds.Remove(bucketId);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Address owner = ((HashMapBucket)_parent.HashMap[(int)ide.Key]).PermanentAddress;
                        BucketsPack bPack = new BucketsPack(null, owner);

                        BucketStatistics bucketStats = GetBucketStats((int)ide.Key, owner);

                        if (bucketStats.Count > 0)
                        {
                            if (_parent.Context.NCacheLog.IsInfoEnabled) _parent.Context.NCacheLog.Info(Name + ".UpdateBuckets()", " Bucket : " + ide.Key + " has " + bucketStats.Count + " items");
                        }

                        if (bucketStats.DataSize < _threshold)
                        {
                            int index = _sparsedBuckets.IndexOf(bPack);
                            if (index != -1)
                            {
                                bPack = _sparsedBuckets[index] as BucketsPack;
                            }

                            bPack.BucketIds.Add(ide.Key);

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

                            bPack.BucketIds.Add(ide.Key);

                            if (!_filledBuckets.Contains(owner))
                            {
                                _filledBuckets.Add(bPack);
                            }
                        }
                    }
                }
            }
        }
コード例 #4
0
 /// <summary>
 /// Removes the buckets from the list of transferable buckets after we have
 /// transferred them.
 /// </summary>
 /// <param name="owner"></param>
 /// <param name="buckets"></param>
 /// <param name="sparsed"></param>
 protected void BucketsTransfered(Address owner, ArrayList buckets)
 {
     BucketsPack bPack = null;
     lock (_stateTxfrMutex)
     {
         if (_sparsedBuckets != null)
         {
             BucketsPack dummy = new BucketsPack(null, owner);
             int index = _sparsedBuckets.IndexOf(dummy);
             if (index != -1)
             {
                 bPack = _sparsedBuckets[index] as BucketsPack;
                 foreach (int bucket in buckets)
                 {
                     bPack.BucketIds.Remove(bucket);
                 }
                 if (bPack.BucketIds.Count == 0)
                     _sparsedBuckets.RemoveAt(index);
             }
         }
         if (_filledBuckets != null)
         {
             BucketsPack dummy = new BucketsPack(null, owner);
             int index = _filledBuckets.IndexOf(dummy);
             if (index != -1)
             {
                 bPack = _filledBuckets[index] as BucketsPack;
                 foreach (int bucket in buckets)
                 {
                     bPack.BucketIds.Remove(bucket);
                 }
                 if (bPack.BucketIds.Count == 0)
                     _filledBuckets.RemoveAt(index);
             }
         }
     }
 }
コード例 #5
0
ファイル: BucketsPack.cs プロジェクト: javithalion/NCache
 public object Clone()
 {
     BucketsPack pack = new BucketsPack(new ArrayList(_bucketIds), (Address)_owner.Clone());
     return pack;
 }