Пример #1
0
 internal void AddResponse(CommandResponse response)
 {
     lock (_mutex)
     {
         if (!_responses.ContainsKey(response.SequenceId))
             _responses.Add(response.SequenceId, response);
     }
 }
Пример #2
0
        public CommandResponse RecieveCommandResponse(Socket client)
        {
            //string result = null;
            byte[] value = null;
            CommandResponse cmdRespose = null;
            try
            {
                value = AssureRecieve(client);

                ///Deserialize the response
                Alachisoft.NCache.Common.Protobuf.Response response = null;
                using (MemoryStream stream = new MemoryStream(value))
                {
                    response = ProtoBuf.Serializer.Deserialize<Alachisoft.NCache.Common.Protobuf.Response>(stream);
                    stream.Close();
                }

                if (response != null && response.responseType == Alachisoft.NCache.Common.Protobuf.Response.Type.RESPONSE_FRAGMENT)
                {
                    response = _responseIntegrator.AddResponseFragment(this._serverAddress,response.getResponseFragment);
                }

                if (response != null)
                {
                    cmdRespose = new CommandResponse(false, new Address());
                    cmdRespose.Result = response;
                }
            }
            catch (SocketException e)
            {
                throw new ConnectionException(e.Message);
            }
            return cmdRespose;
        }
Пример #3
0
 /// <summary>event handler for the Receive CommandBase Event.
 /// gets the command from teh _requestTable and if this command was async
 /// then this thread goes on to notify the initiator of this request. if
 /// the command was synchronous then the thread which is waiting on this
 /// command object is notified and command object is Remove from the _requestTable.
 /// 
 /// </summary>
 /// <param name="result">CommandResponse indicating the response/command received from the client.
 /// </param>
 internal void CommandReceived(CommandResponse result, Address serverAddress)
 {
     ProcessResponse(result, serverAddress);
 }
Пример #4
0
        private void ProcessResponse(CommandResponse response, Address remoteServerAddress)
        {
            CommandBase command = null;
            Request request = null;

            lock (_requestTable.SyncRoot)
            {
                request = (Request) _requestTable[response.RequestId];
                if (request != null)
                {
                    //The async Add/Insert/Remove complete events need to verify the command type to raise events specific to a commmand
                    try
                    {
                        if (request.Commands.Count > 0)
                            command = (CommandBase) request.Commands[remoteServerAddress];
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }

            response.CacheId = _cacheId;

            switch (response.Type)
            {
                case Alachisoft.NCache.Common.Protobuf.Response.Type.INIT:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.ADD:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.REMOVE:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.GET:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.INSERT:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.CLEAR:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.COUNT:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.REGISTER_NOTIF:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.GET_ENUMERATOR:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.ADD_BULK:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.INSERT_BULK:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.GET_OPTIMAL_SERVER:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.GET_BULK:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.REMOVE_BULK:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.CONTAINS:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.GET_CACHE_ITEM:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.SEARCH:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.SEARCH_ENTRIES:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.REGISTER_KEY_NOTIF:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.REGISTER_BULK_KEY_NOTIF:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.UNREGISTER_KEY_NOTIF:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.UNREGISTER_BULK_KEY_NOTIF:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.GET_TYPEINFO_MAP:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.GET_HASHMAP:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.UNLOCK:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.LOCK:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.ISLOCKED:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.GET_LOGGING_INFO:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.DISPOSE:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.DELETE:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.DELETE_BULK:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.EXCEPTION:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.GET_NEXT_CHUNK:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.ADD_ATTRIBUTE:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.GET_SERVER_MAPPING:

                    if (request == null)
                        return;

                    lock (request)
                    {
                        request.AddResponse(remoteServerAddress, response);
                        Monitor.PulseAll(request);
                    }
                    if (_logger.IsDetailedLogsEnabled)
                        _logger.NCacheLog.Debug("Broker.ProcessResponse",
                            "RequestID : " + request.RequestId + " " + request.Name + " received response from server " +
                            remoteServerAddress + ". Seq # " + response.SequenceId);
                    break;

                case Alachisoft.NCache.Common.Protobuf.Response.Type.ITEM_REMOVED_CALLBACK:
                    if (_cache != null && _cache.AsyncEventHandler != null)
                    {
                        _cache.EventListener.OnCustomRemoveCallback(response.CallbackId, response.Key, response.Value,
                            response.Reason, response.FlagValueEntry.Flag, true, null, response.DataFilter);
                    }
                    break;

                case Alachisoft.NCache.Common.Protobuf.Response.Type.NODE_JOINED_EVENT:
                    ThreadPool.QueueUserWorkItem(new WaitCallback(StartBalancingClients), response);
                    break;

                case Alachisoft.NCache.Common.Protobuf.Response.Type.HASHMAP_CHANGED_EVENT:
                    ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateHashmapAsync), response.Value);
                    break;

                case Alachisoft.NCache.Common.Protobuf.Response.Type.NODE_LEFT_EVENT:
                    RemoteServer serverLeft = new RemoteServer();

                    if (response.ServerPort > 0)
                    {
                        //Get Mapped Server will return the same "IP and port" incase of non-existance of the map
                        RemoteServer rm = _clientConfig.GetMappedServer(response.ServerIPAddress.ToString(), response.ServerPort);
                        serverLeft = rm;
                        _clientConfig.RemoveServer(serverLeft);
                    }
                    break;

                case Alachisoft.NCache.Common.Protobuf.Response.Type.ITEM_UPDATED_CALLBACK:
                    if (_cache != null && _cache.AsyncEventHandler != null)
                    {
                        _cache.EventListener.OnCustomUpdateCallback(response.CallbackId, response.Key, true, null, null, null, response.DataFilter);
                    }
                    break;

                case Alachisoft.NCache.Common.Protobuf.Response.Type.BULK_EVENT:
                    if (response.EventList.Count > 0)
                    {
                        ThreadPool.QueueUserWorkItem(_bulkEventCallback, new object[] {remoteServerAddress, response.EventList});
                        break;
                    }
                    break;
            }

            if (request != null)
            {
                if (_perfStatsColl2 != null)
                    _perfStatsColl2.DecrementRequestQueueSizeStats();
            }
        }
Пример #5
0
        private void MergeFailedResponse(CommandResponse response)
        {
            CommandBase command;
            _commands.TryGetValue(response.ResetConnectionIP, out command);

            switch (_type)
            {
                case Alachisoft.NCache.Common.Protobuf.Response.Type.ADD_BULK:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.INSERT_BULK:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.GET_BULK:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.REMOVE_BULK:
                    string key;
                    for (int index = 0; index < command.BulkKeys.Length; index++)
                    {
                        key = command.BulkKeys[index];
                        _finalResponse.KeyValueDic[key] = new ConnectionException("Connection with server lost [" + response.ResetConnectionIP + "]");
                    }
                    _finalResponse.SetBroker = false;
                    break;

                case Alachisoft.NCache.Common.Protobuf.Response.Type.SEARCH:
                case Alachisoft.NCache.Common.Protobuf.Response.Type.SEARCH_ENTRIES:
                    _finalResponse.SetBroker = true;
                    _finalResponse.ResetConnectionIP = response.ResetConnectionIP;
                    break;
            }
        }
Пример #6
0
        internal void MergeResponse(Common.Net.Address address, CommandResponse response)
        {
            if (_finalResponse == null && response.Type != Alachisoft.NCache.Common.Protobuf.Response.Type.GET_NEXT_CHUNK)
            {
                _finalResponse = response;

                if (response.IsBrokerReset)
                {
                    MergeFailedResponse(response);
                }

            }
            else
            {
                if (response.IsBrokerReset)
                {
                    MergeFailedResponse(response);
                }
                else
                {
                    IDictionaryEnumerator ide = null;
                    switch (response.Type)
                    {
                        case Alachisoft.NCache.Common.Protobuf.Response.Type.ADD_BULK:
                        case Alachisoft.NCache.Common.Protobuf.Response.Type.INSERT_BULK:
                        case Alachisoft.NCache.Common.Protobuf.Response.Type.GET_BULK:
                        case Alachisoft.NCache.Common.Protobuf.Response.Type.REMOVE_BULK:
                            ide = response.KeyValueDic.GetEnumerator();
                            while (ide.MoveNext())
                            {
                                _finalResponse.KeyValueDic[ide.Key] = ide.Value;
                            }
                            break;
                        case Alachisoft.NCache.Common.Protobuf.Response.Type.SEARCH:

                            if ((_finalResponse.ExceptionType == Alachisoft.NCache.Common.Enum.ExceptionType.TYPE_INDEX_NOT_FOUND)
                                || (_finalResponse.ExceptionType == Alachisoft.NCache.Common.Enum.ExceptionType.ATTRIBUTE_INDEX_NOT_FOUND))
                            {
                                _finalResponse = response;
                                break;
                            }

                            switch (response.ResultSet.AggregateFunctionType)
                            {
                                case Alachisoft.NCache.Common.Enum.AggregateFunctionType.NOTAPPLICABLE:
                                    _finalResponse.KeyList.AddRange(response.KeyList);
                                    break;

                                default:
                                    if (!_finalResponse.ResultSet.IsInitialized)
                                    {
                                        SetAggregateFunctionResult();
                                        _finalResponse.ResultSet.Initialize(_finalResponse.ResultSet);
                                    }
                                    _finalResponse.ResultSet.Compile(response.ResultSet);
                                    break;
                            }
                            break;

                       case Alachisoft.NCache.Common.Protobuf.Response.Type.SEARCH_ENTRIES:

                            if ((_finalResponse.ExceptionType == Alachisoft.NCache.Common.Enum.ExceptionType.TYPE_INDEX_NOT_FOUND)
                                || (_finalResponse.ExceptionType == Alachisoft.NCache.Common.Enum.ExceptionType.ATTRIBUTE_INDEX_NOT_FOUND))
                            {
                                _finalResponse = response;
                                break;
                            }
                                    switch (response.ResultSet.AggregateFunctionType)
                                    {
                                        case Alachisoft.NCache.Common.Enum.AggregateFunctionType.NOTAPPLICABLE:
                                            ide = response.KeyValueDic.GetEnumerator();
                                            while (ide.MoveNext())
                                            {
                                                _finalResponse.KeyValueDic[ide.Key] = ide.Value;
                                            }
                                            break;

                                        default:
                                            if (!_finalResponse.ResultSet.IsInitialized)
                                            {
                                                SetAggregateFunctionResult();
                                                _finalResponse.ResultSet.Initialize(_finalResponse.ResultSet);
                                            }
                                            _finalResponse.ResultSet.Compile(response.ResultSet);
                                            break;
                                    }
                            break;

                        case Alachisoft.NCache.Common.Protobuf.Response.Type.GET_NEXT_CHUNK:
                            if (_finalResponse == null)
                                _finalResponse = response;

                            EnumerationDataChunk chunk = null;
                            if (_chunks.ContainsKey(address))
                            {
                                chunk = _chunks[address];
                            }
                            else
                            {
                                chunk = new EnumerationDataChunk();
                                chunk.Data = new List<string>();
                                _chunks.Add(address, chunk);
                            }

                            for (int i = 0; i < response.NextChunk.Count; i++)
                            {
                                chunk.Data.AddRange(response.NextChunk[i].Data);
                                chunk.Pointer = response.NextChunk[i].Pointer;
                                if (chunk.Pointer.NodeIpAddress==null)
                                    chunk.Pointer.NodeIpAddress = address;
                            }

                            _finalResponse.NextChunk = new List<EnumerationDataChunk>(_chunks.Values);

                            break;

                        case Alachisoft.NCache.Common.Protobuf.Response.Type.EXCEPTION:
                            if (response.ExceptionType == Common.Enum.ExceptionType.STATE_TRANSFER_EXCEPTION)
                            {
                                _finalResponse = response;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
Пример #7
0
        internal void AddResponse(Common.Net.Address address, CommandResponse response)
        {
            _type = response.Type;

            lock (_responseMutex)
            {
                if (_responses.ContainsKey(address))
                {
                    ResponseList responseList = _responses[address];
                    if (!responseList.IsComplete)
                    {
                        responseList.AddResponse(response);
                    }
                    else
                    {
                        if (_reRoutedAddress != null && !_reRoutedAddress.Equals(address))
                        {
                            if (!_responses.ContainsKey(_reRoutedAddress))
                            {
                                ResponseList rspList = new ResponseList();
                                if (!rspList.IsComplete)
                                {
                                    rspList.AddResponse(response);
                                }

                                _responses.Add(_reRoutedAddress, rspList);
                            }
                            else
                            {
                                responseList = _responses[_reRoutedAddress];
                                if (!responseList.IsComplete)
                                {
                                    responseList.AddResponse(response);
                                }
                            }
                        }
                    }
                }
            }
        }