Ejemplo n.º 1
0
        private void ConnectToNewServer(string partitionId)
        {
            //_clientLogic.ServerList[partitionId].Remove(_usedUrl);
            _client = null;
            var aux = _usedUrl.Split("//");
            var url = aux[1];

            foreach (var parId in _clientLogic.ServerList.Keys)
            {
                Console.WriteLine("Removing from partition:" + parId + " .Url: " + url);
                _clientLogic.ServerList[parId].Remove(url);
            }

            if (_clientLogic.ServerList[partitionId].Count == 0)
            {
                throw new Exception("should not happen");
            }

            var r = new Random();

            _usedUrl = "http://" + _clientLogic.ServerList[partitionId][
                r.Next(0, _clientLogic.ServerList[partitionId].Count)];

            Console.WriteLine(_usedUrl);
            _client = BuildClientFromServerUrl(_usedUrl);
        }
Ejemplo n.º 2
0
        public List <ListServerResult> ListServerUrl(string serverUrl)
        {
            var request = new ListServerRequest();

            try {
                _client = BuildClientFromServerUrl("http://" + serverUrl);
                var listServerResponse = _client.listServer(request);
                return(listServerResponse
                       .Objects
                       .Select(MapToListServerResult)
                       .ToList());
            }
            catch (RpcException e) {
                RemoveClientUrl(serverUrl);

                Console.WriteLine("Server is down");
                Console.WriteLine(e.Message);

                return(new List <ListServerResult>());
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);

                return(new List <ListServerResult>());
            }
        }
Ejemplo n.º 3
0
        private string ReadAdvanced(string partitionId, string objectId, string serverId)
        {
            var request = new ReadAdvancedRequest {
                PartitionId = partitionId, ObjectId = objectId
            };

            try {
                var readResponse = _client.readAdvanced(request);
                if (!readResponse.ObjectValue.Equals(ObjectNotPresent))
                {
                    return(_previousStorage.GetAndUpdate(partitionId, objectId, readResponse));
                }
                //return readResponse.ObjectValue;
                if (serverId.Equals("-1"))
                {
                    return(_previousStorage.GetAndUpdate(partitionId, objectId));
                }
                var serverUrl = MapServerIdToUrl(serverId);
                _client = BuildClientFromServerUrl(serverUrl);
                var secondReadResponse = _client.readAdvanced(request);
                if (secondReadResponse.ObjectValue.Equals(ObjectNotPresent))
                {
                    return(_previousStorage.GetAndUpdate(partitionId, objectId));
                }
                return(_previousStorage.GetAndUpdate(partitionId, objectId, readResponse));
            }
            catch (RpcException e) {
                ConnectToNewServer(partitionId);

                Console.WriteLine("Error reading");
                Console.WriteLine(e.Message);

                return(ReadAdvanced(partitionId, objectId, serverId));
            }
        }
Ejemplo n.º 4
0
 public GrpcService(string serverIp, int serverPort, ClientLogic clientLogic, bool baseVer)
 {
     AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
     _usedUrl         = $"http://{serverIp}:{serverPort}";
     _client          = BuildClientFromServerUrl(_usedUrl);
     _clientLogic     = clientLogic;
     UseBaseVersion   = baseVer;
     _previousStorage = new PreviousStorage(this);
 }
Ejemplo n.º 5
0
        private void WriteAdvanced(string partitionId, string objectId, string objectValue)
        {
            var request = new WriteAdvancedRequest
            {
                PartitionId = partitionId, ObjectId = objectId, ObjectValue = objectValue
            };

            try {
                var response = _client.writeAdvanced(request);
                //Console.WriteLine("Am i here?");
                switch (response.ResponseCase)
                {
                case WriteAdvancedResponse.ResponseOneofCase.Timestamp:
                    Console.WriteLine("Advanced Write Successful");

                    _previousStorage.WriteToCache(partitionId, objectId, objectValue, response.Timestamp);
                    break;

                case WriteAdvancedResponse.ResponseOneofCase.MasterServerUrl:
                    Console.WriteLine($"Advanced  Write - Changing to server {response.MasterServerUrl.ServerUrl}");

                    _usedUrl = response.MasterServerUrl.ServerUrl;
                    _client  = BuildClientFromServerUrl(response.MasterServerUrl.ServerUrl);

                    WriteAdvanced(partitionId, objectId, objectValue);
                    break;

                case WriteAdvancedResponse.ResponseOneofCase.None:
                    Console.WriteLine("Unexpected Error");
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (RpcException) {
                ConnectToNewServer(partitionId);
                Write(partitionId, objectId, objectValue);
                return;
            }
            catch (Exception e) {
                Console.WriteLine("Error Writing");

                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
Ejemplo n.º 6
0
        private string ReadBasic(string partitionId, string objectId, string serverId)
        {
            var request = new ReadRequest {
                PartitionId = partitionId, ObjectId = objectId
            };

            try {
                var readResponse = _client.read(request);

                if (!readResponse.ObjectValue.Equals(ObjectNotPresent))
                {
                    return(readResponse.ObjectValue);
                }

                if (serverId.Equals("-1"))
                {
                    return(ObjectNotPresent);
                }

                var serverUrl = MapServerIdToUrl(serverId);
                _client = BuildClientFromServerUrl(serverUrl);

                var secondReadResponse = _client.read(request);
                return(secondReadResponse.ObjectValue.Equals(ObjectNotPresent)
                    ? ObjectNotPresent
                    : secondReadResponse.ObjectValue);
            }
            catch (RpcException e) {
                ConnectToNewServer(partitionId);

                Console.WriteLine("Error reading");
                Console.WriteLine(e.Message);

                return(Read(partitionId, objectId, serverId));
            }
        }
Ejemplo n.º 7
0
 public ClientService(string serverUrl)
 {
     AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
     _client = BuildClientFromServerUrl(serverUrl);
 }