コード例 #1
0
        /// <summary>
        /// Process the request in a thread from the thread pool.
        /// It not only process the current request, but also any pending request
        /// for the same SyncKey(Key)
        /// </summary>
        /// <param name="request"></param>
        public void ProcessRequest(object request)
        {
            AsyncRequst synRequest      = null;
            ArrayList   pendingRequests = null;
            AsyncRequst pendingRequest  = null;
            object      result          = null;

            try
            {
                if (request != null && request is AsyncRequst)
                {
                    synRequest = request as AsyncRequst;
                    result     = _cluster.handleFunction(synRequest.Src, (Function)synRequest.Operation);
                }
            }
            catch (Exception e)
            {
                result = e;
            }
            finally
            {
                if (synRequest != null)
                {
                    if (synRequest.RequsetId >= 0)
                    {
                        _cluster.SendResponse(synRequest.Src, result, synRequest.RequsetId);
                    }


                    if (synRequest.SyncKey != null)
                    {
                        lock (_sync)
                        {
                            if (_pendingRequests.Contains(synRequest.SyncKey))
                            {
                                pendingRequests = (ArrayList)_pendingRequests[synRequest.SyncKey];
                                pendingRequest  = (AsyncRequst)pendingRequests[0];
                                _lockTable.Add(synRequest.SyncKey, pendingRequest.RequsetId);
                                pendingRequests.RemoveAt(0);
                                if (pendingRequests.Count == 0)
                                {
                                    _pendingRequests.Remove(synRequest.SyncKey);
                                }
                            }
                            else
                            {
                                _lockTable.Remove(synRequest.SyncKey);
                            }
                        }

                        if (pendingRequest != null)
                        {
                            ProcessRequest(pendingRequest);
                        }
                    }
                }
            }
        }
コード例 #2
0
        public void HandleRequest(AsyncRequst operation)
        {
            bool      pooled         = false;
            bool      poolReq        = false;
            bool      allowOperation = false;
            ArrayList pendingOperations;

            if (operation != null)
            {
                if (operation.SyncKey != null)
                {
                    lock (_sync)
                    {
                        if (!_lockTable.Contains(operation.SyncKey))
                        {
                            _lockTable.Add(operation.SyncKey, operation.RequsetId);
                            allowOperation = true;
                        }
                        else
                        {
                            if (_pendingRequests.Contains(operation.SyncKey))
                            {
                                pendingOperations = (ArrayList)_pendingRequests[operation.SyncKey];
                            }
                            else
                            {
                                pendingOperations = new ArrayList();
                                _pendingRequests.Add(operation.SyncKey, pendingOperations);
                            }
                            pendingOperations.Add(operation);
                            _pendingRequests[operation.SyncKey] = pendingOperations;
                        }
                    }
                }
                else
                {
                    allowOperation = true;
                }
            }
            if (allowOperation)
            {
                ProcessRequest(operation);
            }
        }
コード例 #3
0
        public void HandleRequest(AsyncRequst operation)
        {
            bool pooled = false;
            bool poolReq = false;
            bool allowOperation = false;
            ArrayList pendingOperations;

            if (operation != null)
            {
                if (operation.SyncKey != null)
                {
                    lock (_sync)
                    {
                        if (!_lockTable.Contains(operation.SyncKey))
                        {
                            _lockTable.Add(operation.SyncKey, operation.RequsetId);
                            allowOperation = true;
                        }
                        else
                        {
                            if (_pendingRequests.Contains(operation.SyncKey))
                            {
                                pendingOperations = (ArrayList)_pendingRequests[operation.SyncKey];
                            }
                            else
                            {
                                pendingOperations = new ArrayList();
                                _pendingRequests.Add(operation.SyncKey, pendingOperations);
                            }
                            pendingOperations.Add(operation);
                            _pendingRequests[operation.SyncKey] = pendingOperations;
                        }
                    }
                }
                else
                {
                    allowOperation = true;
                }
            }
            if (allowOperation)
                ProcessRequest(operation);
        }
コード例 #4
0
ファイル: ClusterService.cs プロジェクト: javithalion/NCache
        object RequestHandler.handleNHopRequest(Message req, out Address destination, out Message replicationMsg)
        {
            destination = null;
            replicationMsg = null;

            if (req == null || req.Length == 0)
                return null;

            try
            {
                bool isLocalReq = LocalAddress.CompareTo(req.Src) == 0;
                object body = req.getFlatObject();
                try
                {
                    if (body is Byte[])
                        body = CompactBinaryFormatter.FromByteBuffer((byte[])body, _context.SerializationContext);
                }
                catch (Exception e)
                {
                    return e;
                }

                object result = null;
                if (body is Function)
                {
                    Function func = (Function)body;
                    func.UserPayload = req.Payload;
                    if (isLocalReq && func.ExcludeSelf)
                    {
                        if (req.HandledAysnc && req.RequestId > 0)
                            SendResponse(req.Src, null, req.RequestId);
                        return null;
                    }
                    if (req.HandledAysnc)
                    {
                        AsyncRequst asyncReq = new AsyncRequst(func, func.SyncKey);
                        asyncReq.Src = req.Src;
                        asyncReq.RequsetId = req.RequestId;
                        _asynHandler.HandleRequest(asyncReq);
                        return null;
                    }
                    else
                    {
                        result = handleFunction(req.Src, func, out destination, out replicationMsg);
                    }
                }
                else if (body is AggregateFunction)
                {
                    AggregateFunction funcs = (AggregateFunction)body;
                    object[] results = new object[funcs.Functions.Length];
                    for (int i = 0; i < results.Length; i++)
                    {
                        Function func = (Function)funcs.Functions.GetValue(i);
                        if (isLocalReq && func.ExcludeSelf)
                        {
                            if (req.HandledAysnc && req.RequestId > 0)
                            {
                                SendResponse(req.Src, null, req.RequestId);
                                continue;
                            }
                            results[i] = null;
                        }
                        else
                        {
                            if (req.HandledAysnc)
                            {
                                AsyncRequst asyncReq = new AsyncRequst(func, func.SyncKey);
                                asyncReq.Src = req.Src;
                                asyncReq.RequsetId = req.RequestId;
                                _asynHandler.HandleRequest(asyncReq);
                                continue;
                            }
                            results[i] = handleFunction(req.Src, func);
                        }
                    }
                    result = results;
                }

                if (result is OperationResponse)
                {
                    ((OperationResponse)result).SerializablePayload = CompactBinaryFormatter.ToByteBuffer(((OperationResponse)result).SerializablePayload, _context.SerializationContext);
                }
                else
                {
                    result = CompactBinaryFormatter.ToByteBuffer(result, _context.SerializationContext);
                }

                return result;
            }
            catch (Exception e)
            {
                return e;
            }

            return null;
        }