Exemplo n.º 1
0
        /// <summary>
        /// Thread function, keeps running.
        /// </summary>
        protected void Run()
        {
            while (this._worker != null && !this._isDisposing)
            {
                WriteOperation operation = null;
                try
                {
                    lock (this)
                    {
                        if (this._updateQueue.Count < 1)
                        {
                            if (_shutdownStatusLatch.IsAnyBitsSet(ShutDownStatus.SHUTDOWN_INPROGRESS))
                            {
                                _shutdownStatusLatch.SetStatusBit(ShutDownStatus.SHUTDOWN_COMPLETED, ShutDownStatus.SHUTDOWN_INPROGRESS);
                                return;
                            }

                            Monitor.Wait(this);
                        }

                        if (this._updateQueue.Count > 0)
                        {
                            operation = this._updateQueue.Dequeue();
                        }
                        else if (_updateQueue.Count == 0 && _shutdownStatusLatch.IsAnyBitsSet(ShutDownStatus.SHUTDOWN_INPROGRESS))
                        {
                            break;
                        }
                    }

                    if (operation == null)
                    {
                        continue;
                    }
                    lock (_processMutex)
                    {
                        if (!_isDisposing)
                        {
                            if (_dsMgr != null)
                            {
                                _dsMgr.DSAsyncUpdateInCache(operation);
                            }
                        }
                    }
                }
                catch (ThreadAbortException e)
                {
                    break;
                }
                catch (ThreadInterruptedException e)
                {
                    break;
                }
                catch (Exception e)
                {
                }
            }
        }
Exemplo n.º 2
0
        internal void AssureSend(byte[] buffer, Socket client, bool checkConnected)
        {
            int dataSent = 0, dataLeft = buffer.Length;

            lock (_connectionMutex)
            {
                if (checkConnected &&
                    _connectionStatusLatch.IsAnyBitsSet(ConnectionStatus.Disconnected | ConnectionStatus.Connecting))
                {
                    throw new ConnectionException(this._serverAddress.IpAddress, this._serverAddress.Port);
                }

                while (dataSent < buffer.Length)
                {
                    try
                    {
                        dataLeft  = buffer.Length - dataSent;
                        dataSent += client.Send(buffer, dataSent, dataLeft, SocketFlags.None);
                    }
                    catch (SocketException se)
                    {
                        if (se.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
                        {
                        }
                        else
                        {
                            if (_logger.IsErrorLogsEnabled)
                            {
                                _logger.NCacheLog.Error("Connection.AssureSend().SocketException ", se.ToString());
                            }
                            _connectionStatusLatch.SetStatusBit(ConnectionStatus.Disconnected,
                                                                ConnectionStatus.Connected);
                            throw new ConnectionException(this._serverAddress.IpAddress, this._serverAddress.Port);
                        }
                    }
                    catch (ObjectDisposedException)
                    {
                        if (_logger.IsErrorLogsEnabled)
                        {
                            _logger.NCacheLog.Error("Connection.AssureSend", "socket is already closed");
                        }
                        if (_connectionStatusLatch.IsAnyBitsSet(ConnectionStatus.Connected))
                        {
                            _connectionStatusLatch.SetStatusBit(ConnectionStatus.Disconnected,
                                                                ConnectionStatus.Connected);
                        }
                        throw new ConnectionException(this._serverAddress.IpAddress, this._serverAddress.Port);
                    }
                }
            }
        }
Exemplo n.º 3
0
 public void WaitForStatus(string tmpOwner, byte status)
 {
     if (tmpOwner != null)
     {
         while (tmpOwner == _finalShard)
         {
             if (_stateTxfrLatch.IsAnyBitsSet(status))
             {
                 return;
             }
             lock (_status_wait_mutex)
             {
                 if ((tmpOwner == _finalShard) || _stateTxfrLatch.IsAnyBitsSet(status))
                 {
                     return;
                 }
                 Monitor.Wait(_status_wait_mutex);
             }
         }
     }
 }
Exemplo n.º 4
0
 public void WaitForStatus(Address tmpOwner, byte status)
 {
     if (tmpOwner != null)
     {
         while (tmpOwner == _tempAddress)
         {
             if (_stateTxfrLatch.IsAnyBitsSet(status))
             {
                 return;
             }
             lock (_status_wait_mutex)
             {
                 if ((tmpOwner == _tempAddress) || _stateTxfrLatch.IsAnyBitsSet(status))
                 {
                     return;
                 }
                 Monitor.Wait(_status_wait_mutex);
             }
         }
     }
 }
Exemplo n.º 5
0
        public bool Send(byte[] buffer, int offset, int count)
        {
            bool sent = false;

            //If we want to wait for re-connect
            _statusLatch.WaitForAny((byte)Status.Disconnected | (byte)Status.Connected);

            if (!_statusLatch.IsAnyBitsSet((byte)Status.Connected))
            {
                throw new ConnectionException();
            }



            lock (_sync_lock)
            {
                if (_connected)
                {
                    int dataSent = 0;

                    while (count > 0)
                    {
                        try
                        {
                            dataSent = _socket.Send(buffer, offset, count, SocketFlags.None);
                            offset  += dataSent;
                            count    = count - dataSent;
                        }
                        catch (SocketException se)
                        {
                            _statusLatch.SetStatusBit((byte)Status.Disconnected, (byte)Status.Connected | (byte)Status.Reconnecting);
                            _connected = false;
                            throw new ConnectionException(se.Message, se);
                        }
                    }
                    sent = true;
                }
                else
                {
                    throw new ConnectionException();
                }
            }

            AddToClientsBytesSent(buffer.Length);

            return(sent);
        }
Exemplo n.º 6
0
        /// <summary>
        /// replication thread function.
        /// note: While replicating operations, a dummy '0' sequence id is passed.
        /// this sequence id is totally ignored by asynchronous POR, but we are keeping it
        /// to maintain the symmetry in API.
        /// </summary>
        public void Run()
        {                    //reload threshold value from service config, consider the probability that values would have been changed by user
            _bulkKeysToReplicate = ServiceConfiguration.BulkItemsToReplicate;

            IList opCodesToBeReplicated = new ClusteredArrayList(_bulkKeysToReplicate);
            IList infoToBeReplicated    = new ClusteredArrayList(_bulkKeysToReplicate);
            IList compilationInfo       = new ClusteredArrayList(_bulkKeysToReplicate);
            IList userPayLoad           = new ClusteredArrayList();

            try
            {
                while (!stopped || _queue.Count > 0)
                {
                    DateTime startedAt  = DateTime.Now;
                    DateTime finishedAt = DateTime.Now;

                    try
                    {
                        for (int i = 0; _queue.Count > 0 && i < _bulkKeysToReplicate; i++)
                        {
                            IOptimizedQueueOperation operation = null;
                            operation = _queue.Dequeue();

                            DictionaryEntry entry = (DictionaryEntry)operation.Data;
                            opCodesToBeReplicated.Add(entry.Key);
                            infoToBeReplicated.Add(entry.Value);

                            if (operation.UserPayLoad != null)
                            {
                                if (userPayLoad == null)
                                {
                                    userPayLoad = new ArrayList();
                                }
                                for (int j = 0; j < operation.UserPayLoad.Length; j++)
                                {
                                    userPayLoad.Add(operation.UserPayLoad.GetValue(j));
                                }
                            }

                            compilationInfo.Add(operation.PayLoadSize);
                        }
                        object[] updateIndexKeys = GetIndexOperations();

                        if (!stopped)
                        {
                            if (opCodesToBeReplicated.Count > 0 || updateIndexKeys != null)
                            {
                                if (updateIndexKeys != null)
                                {
                                    opCodesToBeReplicated.Add((int)ClusterCacheBase.OpCodes.UpdateIndice);
                                    infoToBeReplicated.Add(updateIndexKeys);
                                }
                                _context.CacheImpl.ReplicateOperations(opCodesToBeReplicated, infoToBeReplicated, userPayLoad, compilationInfo, _context.CacheImpl.OperationSequenceId, _context.CacheImpl.CurrentViewId);
                            }
                        }

                        if (!stopped && _context.PerfStatsColl != null)
                        {
                            _context.PerfStatsColl.IncrementMirrorQueueSizeStats(_queue.Count);
                        }
                    }
                    catch (Exception e)
                    {
                        if (e.Message.IndexOf("operation timeout", StringComparison.OrdinalIgnoreCase) >= 0 && !_shutdownStatusLatch.IsAnyBitsSet(ShutDownStatus.SHUTDOWN_INPROGRESS))
                        {
                            _context.NCacheLog.CriticalInfo("AsyncReplicator.Run", "Bulk operation timeout. Retrying the operation.");
                            try
                            {
                                if (!stopped)
                                {
                                    _context.CacheImpl.ReplicateOperations(opCodesToBeReplicated, infoToBeReplicated, userPayLoad, compilationInfo, 0, 0);
                                    _context.NCacheLog.CriticalInfo("AsyncReplicator.Run", "RETRY is successfully.");
                                }
                            }
                            catch (Exception ex)
                            {
                                if (_context.NCacheLog.IsErrorEnabled)
                                {
                                    _context.NCacheLog.Error("AsyncReplicator.RUN", "Error occurred while retrying operation. " + ex.ToString());
                                }
                            }
                        }
                        else
                        if (_context.NCacheLog.IsErrorEnabled)
                        {
                            _context.NCacheLog.Error("AsyncReplicator.RUN", e.ToString());
                        }
                    }
                    finally
                    {
                        opCodesToBeReplicated.Clear();
                        infoToBeReplicated.Clear();
                        compilationInfo.Clear();
                        if (userPayLoad != null)
                        {
                            userPayLoad.Clear();
                        }
                        finishedAt = DateTime.Now;
                    }

                    if (_queue.Count > 0)
                    {
                        continue;
                    }
                    else if (_queue.Count == 0 && _shutdownStatusLatch.IsAnyBitsSet(ShutDownStatus.SHUTDOWN_INPROGRESS))
                    {
                        _shutdownStatusLatch.SetStatusBit(ShutDownStatus.SHUTDOWN_COMPLETED, ShutDownStatus.SHUTDOWN_INPROGRESS);
                        return;
                    }

                    if ((finishedAt.Ticks - startedAt.Ticks) < _interval.Ticks)
                    {
                        Thread.Sleep(_interval.Subtract(finishedAt.Subtract(startedAt)));
                    }
                    else
                    {
                        Thread.Sleep(_interval);
                    }
                }
            }
            catch (ThreadAbortException ta)
            {
                //_context.CacheTrace.error("AsyncReplicator.RUN", "Async replicator stopped. " + ta.ToString());
            }
            catch (ThreadInterruptedException ti)
            {
                //_context.CacheTrace.error("AsyncReplicator.RUN", "Async replicator stopped. " + ti.ToString());
            }
            catch (NullReferenceException)
            {
            }
            catch (Exception e)
            {
                if (!stopped)
                {
                    _context.NCacheLog.Error("AsyncReplicator.RUN", "Async replicator stopped. " + e.ToString());
                }
            }
        }