Ejemplo n.º 1
0
        private void HandleRemotingRequest(ReceiveContext receiveContext)
        {
            var remotingRequest = RemotingUtil.ParseRequest(receiveContext.ReceivedMessage);
            IRequestHandler requestHandler;
            if (!_requestHandlerDict.TryGetValue(remotingRequest.Code, out requestHandler))
            {
                var errorMessage = string.Format("No request handler found for remoting request, request code:{0}", remotingRequest.Code);
                _logger.Error(errorMessage);
                var remotingResponse = new RemotingResponse(-1, remotingRequest.Sequence, Encoding.UTF8.GetBytes(errorMessage));
                receiveContext.ReplyMessage = RemotingUtil.BuildResponseMessage(remotingResponse);
                receiveContext.MessageHandledCallback(receiveContext);
                return;
            }

            try
            {
                var remotingResponse = requestHandler.HandleRequest(new SocketRequestHandlerContext(receiveContext), remotingRequest);
                if (!remotingRequest.IsOneway && remotingResponse != null)
                {
                    receiveContext.ReplyMessage = RemotingUtil.BuildResponseMessage(remotingResponse);
                    receiveContext.MessageHandledCallback(receiveContext);
                }
            }
            catch (Exception ex)
            {
                var errorMessage = string.Format("Exception raised when handling remoting request, request code:{0}.", remotingRequest.Code);
                _logger.Error(errorMessage, ex);
                if (!remotingRequest.IsOneway)
                {
                    var remotingResponse = new RemotingResponse(-1, remotingRequest.Sequence, Encoding.UTF8.GetBytes(ex.Message));
                    receiveContext.ReplyMessage = RemotingUtil.BuildResponseMessage(remotingResponse);
                    receiveContext.MessageHandledCallback(receiveContext);
                }
            }
        }
Ejemplo n.º 2
0
        public static byte[] BuildResponseMessage(RemotingResponse response)
        {
            var requestSequenceBytes     = BitConverter.GetBytes(response.RequestSequence);
            var requestCodeBytes         = BitConverter.GetBytes(response.RequestCode);
            var requestTypeBytes         = BitConverter.GetBytes(response.RequestType);
            var requestTimeBytes         = ByteUtil.EncodeDateTime(response.RequestTime);
            var requestHeaderBytes       = HeaderUtil.EncodeHeader(response.RequestHeader);
            var requestHeaderLengthBytes = BitConverter.GetBytes(requestHeaderBytes.Length);

            var responseCodeBytes         = BitConverter.GetBytes(response.ResponseCode);
            var responseTimeBytes         = ByteUtil.EncodeDateTime(response.ResponseTime);
            var responseHeaderBytes       = HeaderUtil.EncodeHeader(response.ResponseHeader);
            var responseHeaderLengthBytes = BitConverter.GetBytes(requestHeaderBytes.Length);

            return(ByteUtil.Combine(
                       requestSequenceBytes,
                       requestCodeBytes,
                       requestTypeBytes,
                       requestTimeBytes,
                       requestHeaderLengthBytes,
                       requestHeaderBytes,
                       responseCodeBytes,
                       responseTimeBytes,
                       responseHeaderLengthBytes,
                       responseHeaderBytes,
                       response.ResponseBody));
        }
Ejemplo n.º 3
0
        private static byte[] BuildRemotingServerMessage(RemotingResponse remotingResponse)
        {
            byte[] remotingResponseData  = RemotingUtil.BuildResponseMessage(remotingResponse);
            var    remotingServerMessage = new RemotingServerMessage(
                RemotingServerMessageType.RemotingResponse,
                100,
                remotingResponseData,
                null);

            return(RemotingUtil.BuildRemotingServerMessage(remotingServerMessage));
        }
Ejemplo n.º 4
0
        public static byte[] BuildResponseMessage(RemotingResponse response)
        {
            var sequenceBytes = BitConverter.GetBytes(response.Sequence);
            var codeBytes     = BitConverter.GetBytes(response.Code);
            var message       = new byte[sequenceBytes.Length + codeBytes.Length + response.Body.Length];

            sequenceBytes.CopyTo(message, 0);
            codeBytes.CopyTo(message, sequenceBytes.Length);
            response.Body.CopyTo(message, sequenceBytes.Length + codeBytes.Length);

            return(message);
        }
Ejemplo n.º 5
0
        public static byte[] BuildResponseMessage(RemotingResponse response)
        {
            var sequenceBytes = BitConverter.GetBytes(response.Sequence);
            var codeBytes = BitConverter.GetBytes(response.Code);
            var message = new byte[sequenceBytes.Length + codeBytes.Length + response.Body.Length];

            sequenceBytes.CopyTo(message, 0);
            codeBytes.CopyTo(message, 8);
            response.Body.CopyTo(message, 12);

            return message;
        }
Ejemplo n.º 6
0
        public static byte[] BuildResponseMessage(RemotingResponse response)
        {
            var sequenceBytes = BitConverter.GetBytes(response.Sequence);
            var requestCodeBytes = BitConverter.GetBytes(response.RequestCode);
            var responseCodeBytes = BitConverter.GetBytes(response.Code);
            var requestTypeBytes = BitConverter.GetBytes(response.Type);
            var message = new byte[14 + response.Body.Length];

            sequenceBytes.CopyTo(message, 0);
            requestCodeBytes.CopyTo(message, 8);
            responseCodeBytes.CopyTo(message, 10);
            requestTypeBytes.CopyTo(message, 12);
            response.Body.CopyTo(message, 14);

            return message;
        }
Ejemplo n.º 7
0
        public static byte[] BuildResponseMessage(RemotingResponse response)
        {
            var sequenceBytes     = BitConverter.GetBytes(response.Sequence);
            var requestCodeBytes  = BitConverter.GetBytes(response.RequestCode);
            var responseCodeBytes = BitConverter.GetBytes(response.Code);
            var requestTypeBytes  = BitConverter.GetBytes(response.Type);
            var message           = new byte[14 + response.Body.Length];

            sequenceBytes.CopyTo(message, 0);
            requestCodeBytes.CopyTo(message, 8);
            responseCodeBytes.CopyTo(message, 10);
            requestTypeBytes.CopyTo(message, 12);
            response.Body.CopyTo(message, 14);

            return(message);
        }
 private void ExecutePullRequest(PullRequest pullRequest)
 {
     var consumerGroup = _brokerController.ConsumerManager.GetConsumerGroup(pullRequest.PullMessageRequest.ConsumerGroup);
     if (consumerGroup != null && consumerGroup.IsConsumerChannelActive(pullRequest.RequestHandlerContext.Channel.RemotingAddress))
     {
         var pullMessageRequest = pullRequest.PullMessageRequest;
         var messages = _messageService.GetMessages(
             pullMessageRequest.MessageQueue.Topic,
             pullMessageRequest.MessageQueue.QueueId,
             pullMessageRequest.QueueOffset,
             pullMessageRequest.PullMessageBatchSize);
         var pullMessageResponse = new PullMessageResponse(messages);
         var responseData = _binarySerializer.Serialize(pullMessageResponse);
         var remotingResponse = new RemotingResponse(messages.Count() > 0 ? (int)PullStatus.Found : (int)PullStatus.NoNewMessage, pullRequest.RemotingRequestSequence, responseData);
         pullRequest.RequestHandlerContext.SendRemotingResponse(remotingResponse);
     }
 }
Ejemplo n.º 9
0
        public static byte[] BuildResponseMessage(RemotingResponse response)
        {
            var requestSequenceBytes = BitConverter.GetBytes(response.RequestSequence);
            var requestCodeBytes = BitConverter.GetBytes(response.RequestCode);
            var requestTypeBytes = BitConverter.GetBytes(response.RequestType);
            var requestTimeBytes = ByteUtil.EncodeDateTime(response.RequestTime);
            var requestHeaderBytes = HeaderUtil.EncodeHeader(response.RequestHeader);
            var requestHeaderLengthBytes = BitConverter.GetBytes(requestHeaderBytes.Length);

            var responseCodeBytes = BitConverter.GetBytes(response.ResponseCode);
            var responseTimeBytes = ByteUtil.EncodeDateTime(response.ResponseTime);
            var responseHeaderBytes = HeaderUtil.EncodeHeader(response.ResponseHeader);
            var responseHeaderLengthBytes = BitConverter.GetBytes(requestHeaderBytes.Length);

            return ByteUtil.Combine(
                requestSequenceBytes,
                requestCodeBytes,
                requestTypeBytes,
                requestTimeBytes,
                requestHeaderLengthBytes,
                requestHeaderBytes,
                responseCodeBytes,
                responseTimeBytes,
                responseHeaderLengthBytes,
                responseHeaderBytes,
                response.ResponseBody);
        }
Ejemplo n.º 10
0
        public static SendResult ParseSendResult(RemotingResponse remotingResponse)
        {
            Ensure.NotNull(remotingResponse, "remotingResponse");

            if (remotingResponse.Code == ResponseCode.Success)
            {
                var result = MessageUtils.DecodeMessageStoreResult(remotingResponse.Body);
                return new SendResult(SendStatus.Success, result, null);
            }
            else if (remotingResponse.Code == 0)
            {
                return new SendResult(SendStatus.Timeout, null, Encoding.UTF8.GetString(remotingResponse.Body));
            }
            else
            {
                return new SendResult(SendStatus.Failed, null, Encoding.UTF8.GetString(remotingResponse.Body));
            }
        }
Ejemplo n.º 11
0
 public bool SetResponse(RemotingResponse response)
 {
     return(_taskSource.TrySetResult(response));
 }
Ejemplo n.º 12
0
        private void ProcessPullResponse(PullRequest pullRequest, RemotingResponse remotingResponse, Action<IEnumerable<QueueMessage>> handlePulledMessageAction)
        {
            if (remotingResponse == null)
            {
                _logger.ErrorFormat("Pull message response is null, pullRequest:{0}", pullRequest);
                SchedulePullRequest(pullRequest);
                return;
            }

            if (remotingResponse.Code == -1)
            {
                _logger.ErrorFormat("Pull message failed, pullRequest:{0}, errorMsg:{1}", pullRequest, Encoding.UTF8.GetString(remotingResponse.Body));
                SchedulePullRequest(pullRequest);
                return;
            }

            if (remotingResponse.Code == (short)PullStatus.Found)
            {
                var messages = DecodeMessages(pullRequest, remotingResponse.Body);
                if (messages.Count() > 0)
                {
                    handlePulledMessageAction(messages);
                    pullRequest.NextConsumeOffset = messages.Last().QueueOffset + 1;
                }
            }
            else if (remotingResponse.Code == (short)PullStatus.NextOffsetReset)
            {
                var newOffset = BitConverter.ToInt64(remotingResponse.Body, 0);
                ResetNextConsumeOffset(pullRequest, newOffset);
            }
            else if (remotingResponse.Code == (short)PullStatus.NoNewMessage)
            {
                //No new message to consume.
            }
            else if (remotingResponse.Code == (short)PullStatus.Ignored)
            {
                _logger.InfoFormat("Pull request was ignored, pullRequest:{0}", pullRequest);
                return;
            }
            else if (remotingResponse.Code == (short)PullStatus.BrokerIsCleaning)
            {
                Thread.Sleep(5000);
            }

            //Schedule the next pull request.
            SchedulePullRequest(pullRequest);
        }
Ejemplo n.º 13
0
 public void SetResponse(RemotingResponse response)
 {
     _taskSource.TrySetResult(response);
 }
Ejemplo n.º 14
0
        private void ProcessPullResponse(PullRequest pullRequest, RemotingResponse remotingResponse)
        {
            if (remotingResponse == null)
            {
                _logger.ErrorFormat("Pull message response is null, pullRequest:{0}", pullRequest);
                SchedulePullRequest(pullRequest);
                return;
            }

            if (remotingResponse.Code == -1)
            {
                _logger.ErrorFormat("Pull message failed, pullRequest:{0}, errorMsg:{1}", pullRequest, Encoding.UTF8.GetString(remotingResponse.Body));
                SchedulePullRequest(pullRequest);
                return;
            }

            if (remotingResponse.Code == (int)PullStatus.Found)
            {
                var messages = _binarySerializer.Deserialize<IEnumerable<QueueMessage>>(remotingResponse.Body);
                if (messages.Count() > 0)
                {
                    pullRequest.ProcessQueue.AddMessages(messages);
                    foreach (var message in messages)
                    {
                        _consumingMessageQueue.Add(new ConsumingMessage(message, pullRequest.ProcessQueue));
                    }
                    pullRequest.NextConsumeOffset = messages.Last().QueueOffset + 1;
                }
            }
            else if (remotingResponse.Code == (int)PullStatus.NextOffsetReset)
            {
                var newOffset = BitConverter.ToInt64(remotingResponse.Body, 0);
                var oldOffset = pullRequest.NextConsumeOffset;
                pullRequest.NextConsumeOffset = newOffset;
                _logger.InfoFormat("Reset queue next consume offset. topic:{0}, queueId:{1}, old offset:{2}, new offset:{3}", pullRequest.MessageQueue.Topic, pullRequest.MessageQueue.QueueId, oldOffset, newOffset);
            }
            else if (remotingResponse.Code == (int)PullStatus.NoNewMessage)
            {
                _logger.DebugFormat("No new message found, pullRequest:{0}", pullRequest);
            }
            else if (remotingResponse.Code == (int)PullStatus.Ignored)
            {
                _logger.InfoFormat("Pull request was ignored, pullRequest:{0}", pullRequest);
                return;
            }

            //Schedule the next pull request.
            SchedulePullRequest(pullRequest);
        }
Ejemplo n.º 15
0
 public void HandleResponse(RemotingResponse remotingResponse)
 {
     if (remotingResponse.Code <= 0)
     {
         Console.WriteLine(Encoding.UTF8.GetString(remotingResponse.Body));
         return;
     }
     var current = Interlocked.Increment(ref _totalReceivedCount);
     if (current % 10000 == 0)
     {
         Console.WriteLine("Sent {0} messages, timeSpent: {1}ms", current, _watch.ElapsedMilliseconds);
     }
 }
Ejemplo n.º 16
0
 public void HandleResponse(RemotingResponse remotingResponse)
 {
     if (remotingResponse.ResponseCode <= 0)
     {
         _logger.Error(Encoding.UTF8.GetString(remotingResponse.ResponseBody));
         return;
     }
     _performanceService.IncrementKeyCount(_performanceKey, (DateTime.Now - remotingResponse.RequestTime).TotalMilliseconds);
 }
Ejemplo n.º 17
0
 public void SetResponse(RemotingResponse response)
 {
     _taskSource.TrySetResult(response);
 }
Ejemplo n.º 18
0
 private void SendRemotingResponse(PullRequest pullRequest, RemotingResponse remotingResponse)
 {
     pullRequest.RequestHandlerContext.SendRemotingResponse(remotingResponse);
 }
Ejemplo n.º 19
0
 public void CompleteRequestTask(RemotingResponse response)
 {
     _requestTaskCompletionSource.TrySetResult(response);
 }
Ejemplo n.º 20
0
 public bool SetResponse(RemotingResponse response)
 {
     return _taskSource.TrySetResult(response);
 }
Ejemplo n.º 21
0
 public void HandleResponse(RemotingResponse remotingResponse)
 {
     if (remotingResponse.Code <= 0)
     {
         _logger.Error(Encoding.UTF8.GetString(remotingResponse.Body));
         return;
     }
     Interlocked.Increment(ref _sentCount);
 }
Ejemplo n.º 22
0
 private void ExecuteReplacedPullRequest(PullRequest pullRequest)
 {
     var consumerGroup = _brokerController.ConsumerManager.GetConsumerGroup(pullRequest.PullMessageRequest.ConsumerGroup);
     if (consumerGroup != null && consumerGroup.IsConsumerChannelActive(pullRequest.RequestHandlerContext.Channel.RemotingAddress))
     {
         var responseData = _binarySerializer.Serialize(new PullMessageResponse(new QueueMessage[0]));
         var remotingResponse = new RemotingResponse((int)PullStatus.Ignored, pullRequest.RemotingRequestSequence, responseData);
         pullRequest.RequestHandlerContext.SendRemotingResponse(remotingResponse);
     }
 }
Ejemplo n.º 23
0
            public void HandleResponse(RemotingResponse remotingResponse)
            {
                var sendResult = Producer.ParseSendResult(remotingResponse);
                if (sendResult.SendStatus != SendStatus.Success)
                {
                    _logger.Error(sendResult.ErrorMessage);
                    return;
                }

                var current = Interlocked.Increment(ref _sentCount);
                if (current % 10000 == 0)
                {
                    _logger.InfoFormat("Sent {0} messages, timeSpent: {1}ms, throughput: {2}/s", current, _watch.ElapsedMilliseconds, current * 1000 / _watch.ElapsedMilliseconds);
                }
            }
Ejemplo n.º 24
0
        private void ProcessPullResponse(PullRequest pullRequest, RemotingResponse remotingResponse)
        {
            if (remotingResponse == null)
            {
                _logger.ErrorFormat("Pull message response is null, pullRequest:{0}", pullRequest);
                SchedulePullRequest(pullRequest);
                return;
            }

            if (remotingResponse.Code == -1)
            {
                _logger.ErrorFormat("Pull message failed, pullRequest:{0}, errorMsg:{1}", pullRequest, Encoding.UTF8.GetString(remotingResponse.Body));
                SchedulePullRequest(pullRequest);
                return;
            }

            if (remotingResponse.Code == (short)PullStatus.Found)
            {
                var messages = DecodeMessages(pullRequest, remotingResponse.Body);
                if (messages.Count() > 0)
                {
                    var filterMessages = messages.Where(x => IsQueueMessageMatchTag(x, pullRequest.Tags));
                    pullRequest.ProcessQueue.AddMessages(filterMessages);
                    foreach (var message in filterMessages)
                    {
                        var consumingMessage = new ConsumingMessage(message, pullRequest.ProcessQueue);
                        if (Setting.MessageHandleMode == MessageHandleMode.Sequential)
                        {
                            _consumingMessageQueue.Add(consumingMessage);
                        }
                        else
                        {
                            Task.Factory.StartNew(HandleMessage, consumingMessage);
                        }
                    }
                    pullRequest.NextConsumeOffset = messages.Last().QueueOffset + 1;
                }
            }
            else if (remotingResponse.Code == (short)PullStatus.NextOffsetReset)
            {
                var newOffset = BitConverter.ToInt64(remotingResponse.Body, 0);
                var oldOffset = pullRequest.NextConsumeOffset;
                pullRequest.NextConsumeOffset = newOffset;
                _logger.InfoFormat("Reset queue next consume offset. topic:{0}, queueId:{1}, old offset:{2}, new offset:{3}", pullRequest.MessageQueue.Topic, pullRequest.MessageQueue.QueueId, oldOffset, newOffset);
            }
            else if (remotingResponse.Code == (short)PullStatus.NoNewMessage)
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.DebugFormat("No new message found, pullRequest:{0}", pullRequest);
                }
            }
            else if (remotingResponse.Code == (short)PullStatus.Ignored)
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.DebugFormat("Pull request was ignored, pullRequest:{0}", pullRequest);
                }
                return;
            }
            else if (remotingResponse.Code == (short)PullStatus.BrokerIsCleaning)
            {
                Thread.Sleep(5000);
            }

            //Schedule the next pull request.
            SchedulePullRequest(pullRequest);
        }
Ejemplo n.º 25
0
            public void HandleResponse(RemotingResponse remotingResponse)
            {
                var sendResult = Producer.ParseSendResult(remotingResponse);
                if (sendResult.SendStatus != SendStatus.Success)
                {
                    _hasError = true;
                    _logger.Error(sendResult.ErrorMessage);
                    return;
                }

                Interlocked.Increment(ref _sentCount);
            }