Exemple #1
0
        /// <summary>
        /// Get the etcd response for a specified key in async
        /// </summary>
        /// <param name="request">The request to send to the server.</param>
        /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param>
        /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param>
        /// <param name="cancellationToken">An optional token for canceling the call.</param>
        /// <returns>The etcd response for the specified request</returns>
        public async Task <RangeResponse> GetAsync(RangeRequest request, Grpc.Core.Metadata headers = null,
                                                   DateTime?deadline = null,
                                                   CancellationToken cancellationToken = default)
        {
            RangeResponse rangeResponse = new RangeResponse();
            bool          success       = false;
            int           retryCount    = 0;

            while (!success)
            {
                try
                {
                    rangeResponse = await _balancer.GetConnection().kvClient
                                    .RangeAsync(request, headers, deadline, cancellationToken);

                    success = true;
                }
                catch (RpcException ex) when(ex.StatusCode == StatusCode.Unavailable)
                {
                    retryCount++;
                    if (retryCount >= _balancer._numNodes)
                    {
                        throw;
                    }
                }
            }

            return(rangeResponse);
        }
Exemple #2
0
        /// <summary>
        /// Gets the range of keys with the specified prefix
        /// </summary>
        /// <returns>Dictionary containing range of key-values</returns>
        /// <param name="prefixKey">Prefix key</param>
        public IDictionary <string, string> GetRange(string prefixKey)
        {
            RangeResponse rangeResponse = new RangeResponse();

            try
            {
                var rangeEnd = GetRangeEnd(prefixKey);

                rangeResponse = _kvClient.Range(new RangeRequest
                {
                    Key      = ByteString.CopyFromUtf8(prefixKey),
                    RangeEnd = ByteString.CopyFromUtf8(rangeEnd)
                }, _headers);
            }
            catch (Grpc.Core.RpcException)
            {
                ResetConnection();
                throw;
            }
            catch
            {
                throw;
            }

            return(RangeRespondToDictionary(rangeResponse));
        }
Exemple #3
0
        /// <summary>
        /// Get the etcd response for a specified key in async
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The etcd response for the specified request</returns>
        public async Task <RangeResponse> GetAsync(RangeRequest request, Metadata headers = null)
        {
            RangeResponse rangeResponse = new RangeResponse();
            bool          success       = false;
            int           retryCount    = 0;

            while (!success)
            {
                try
                {
                    rangeResponse = await _balancer.GetConnection().kvClient.RangeAsync(request, headers);

                    success = true;
                }
                catch (RpcException ex) when(ex.StatusCode == StatusCode.Unavailable)
                {
                    retryCount++;
                    if (retryCount >= _balancer._numNodes)
                    {
                        throw ex;
                    }
                }
            }

            return(rangeResponse);
        }
        /// <summary>
        /// Gets the range of keys with the specified prefix
        /// </summary>
        /// <returns>RangeResponse containing range of key-values</returns>
        /// <param name="prefixKey">Prefix key</param>
        public RangeResponse GetRange(string prefixKey)
        {
            RangeResponse rangeResponse = new RangeResponse();

            try
            {
                string rangeEnd = GetRangeEnd(prefixKey);

                rangeResponse = _kvClient.Range(new RangeRequest
                {
                    Key      = ByteString.CopyFromUtf8(prefixKey),
                    RangeEnd = ByteString.CopyFromUtf8(rangeEnd)
                }, _headers);
            }
            catch (RpcException)
            {
                ResetConnection();
                throw;
            }
            catch
            {
                throw;
            }

            return(rangeResponse);
        }
Exemple #5
0
        /// <summary>
        /// Get the value for a specified key in async
        /// </summary>
        /// <param name="key">Key for which value need to be fetched</param>
        /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param>
        /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param>
        /// <param name="cancellationToken">An optional token for canceling the call.</param>
        /// <returns>The value for the specified key</returns>
        public async Task <string> GetValAsync(string key, Grpc.Core.Metadata headers = null, DateTime?deadline = null,
                                               CancellationToken cancellationToken    = default)
        {
            RangeResponse rangeResponse = await GetAsync(key, headers, deadline, cancellationToken);

            return(rangeResponse.Count != 0 ? rangeResponse.Kvs[0].Value.ToStringUtf8().Trim() : string.Empty);
        }
Exemple #6
0
        /// <summary>
        /// Gets the range of keys with the specified prefix in async
        /// </summary>
        /// <returns>Dictionary containing range of key-values</returns>
        /// <param name="prefixKey">Prefix key</param>
        public async Task <IDictionary <string, string> > GetRangeAsync(string prefixKey)
        {
            RangeResponse rangeResponse = new RangeResponse();

            try
            {
                string rangeEnd = GetRangeEnd(prefixKey);

                rangeResponse = await _kvClient.RangeAsync(new RangeRequest
                {
                    Key      = ByteString.CopyFromUtf8(prefixKey),
                    RangeEnd = ByteString.CopyFromUtf8(rangeEnd)
                }, _headers);
            }
            catch (RpcException)
            {
                ResetConnection();
                throw;
            }
            catch
            {
                throw;
            }

            return(RangeRespondToDictionary(rangeResponse));
        }
Exemple #7
0
 private void HandleTopicListGet(RangeResponse rangeResponse)
 {
     foreach (var keyValue in rangeResponse.Kvs)
     {
         Task.Run(async() => await HandleElectionForKeyValue(keyValue));
     }
 }
Exemple #8
0
        /// <summary>
        /// Get the etcd response for a specified key in async
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The etcd response for the specified request</returns>
        public async Task <RangeResponse> GetAsync(RangeRequest request, Metadata headers = null)
        {
            RangeResponse rangeResponse = new RangeResponse();

            rangeResponse = await _balancer.GetConnection().kvClient.RangeAsync(request, headers);


            return(rangeResponse);
        }
Exemple #9
0
        /// <summary>
        /// Get the value for a specified key
        /// </summary>
        /// <returns>The value for the specified key</returns>
        /// <param name="key">Key for which value need to be fetched</param>
        public string GetVal(string key)
        {
            RangeResponse rangeResponse = new RangeResponse();

            rangeResponse = Get(key);


            return(rangeResponse.Count != 0 ? rangeResponse.Kvs[0].Value.ToStringUtf8().Trim() : string.Empty);
        }
Exemple #10
0
        /// <summary>
        /// Get the value for a specified key in async
        /// </summary>
        /// <returns>The value for the specified key</returns>
        /// <param name="key">Key for which value need to be fetched</param>
        public async Task <string> GetValAsync(string key)
        {
            RangeResponse rangeResponse = new RangeResponse();

            rangeResponse = await GetAsync(key);


            return(rangeResponse.Count != 0 ? rangeResponse.Kvs[0].Value.ToStringUtf8().Trim() : string.Empty);
        }
Exemple #11
0
        /// <summary>
        /// Get the etcd response for a specified RangeRequest
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The etcd response for the specified request</returns>
        public RangeResponse Get(RangeRequest request, Metadata headers = null)
        {
            RangeResponse rangeResponse = new RangeResponse();

            rangeResponse = _balancer.GetConnection().kvClient.Range(request, headers);


            return(rangeResponse);
        }
Exemple #12
0
        /// <summary>
        /// Converts RangeResponse to Dictionary
        /// </summary>
        /// <returns>IDictionary corresponding the RangeResponse</returns>
        /// <param name="resp">RangeResponse received from etcd server</param>
        private static IDictionary <string, string> RangeRespondToDictionary(RangeResponse resp)
        {
            var resDictionary = new Dictionary <string, string>();

            foreach (var kv in resp.Kvs)
            {
                resDictionary.Add(kv.Key.ToStringUtf8(), kv.Value.ToStringUtf8());
            }
            return(resDictionary);
        }
Exemple #13
0
        /// <summary>
        /// Converts RangeResponse to Dictionary
        /// </summary>
        /// <returns>IDictionary corresponding the RangeResponse</returns>
        /// <param name="resp">RangeResponse received from etcd server</param>
        public static IDictionary <string, string> RangeRespondToDictionary(RangeResponse resp)
        {
            Dictionary <string, string> resDictionary = new Dictionary <string, string>();

            foreach (Mvccpb.KeyValue kv in resp.Kvs)
            {
                resDictionary.Add(kv.Key.ToStringUtf8(), kv.Value.ToStringUtf8());
            }
            return(resDictionary);
        }
Exemple #14
0
        private string GetKey(IEtcdCompoundClient client, string key)
        {
            RangeResponse response = client.Get(key);

            if (response.Count == 0 || response.Kvs.Count == 0)
            {
                return(null);
            }

            return(response.Kvs[0].Value.ToStringUtf8());
        }
Exemple #15
0
 public static Entity.RangeResponse FromProto(this  RangeResponse response)
 {
     return(new Entity.RangeResponse()
     {
         Count = response.Count,
         Header = response.Header.FromProto(),
         Kvs = response.Kvs.FromProto(),
         More = response.More,
         CalculateSize = response.CalculateSize()
     });
 }
            public void Next_page_should_be_null_when_end_is_reached()
            {
                // Arrange
                var range = new RangeResponse(1, 15, 15);

                // Act
                var next = range.NextPage;

                // Assert
                next.ShouldBe(null);
            }
Exemple #17
0
        /// <summary>
        /// Get the etcd response for a specified key in async
        /// </summary>
        /// <returns>The etcd response for the specified key</returns>
        /// <param name="key">Key for which value need to be fetched</param>
        public async Task <RangeResponse> GetAsync(string key)
        {
            RangeResponse rangeResponse = new RangeResponse();

            rangeResponse = await GetAsync(new RangeRequest
            {
                Key = ByteString.CopyFromUtf8(key)
            });


            return(rangeResponse);
        }
Exemple #18
0
        /// <summary>
        /// Get the etcd response for a specified key in async
        /// </summary>
        /// <returns>The etcd response for the specified key</returns>
        /// <param name="key">Key for which value need to be fetched</param>
        public async Task <RangeResponse> GetAsync(string key, Metadata headers = null)
        {
            RangeResponse rangeResponse = new RangeResponse();

            rangeResponse = await GetAsync(new RangeRequest
            {
                Key = ByteString.CopyFromUtf8(key)
            }, headers);


            return(rangeResponse);
        }
Exemple #19
0
        /// <summary>
        /// Get the etcd response for a specified key
        /// </summary>
        /// <returns>The etcd response for the specified key</returns>
        /// <param name="key">Key for which value need to be fetched</param>
        public RangeResponse Get(string key)
        {
            RangeResponse rangeResponse = new RangeResponse();

            rangeResponse = Get(new RangeRequest
            {
                Key = ByteString.CopyFromUtf8(key)
            });


            return(rangeResponse);
        }
Exemple #20
0
        /// <summary>
        /// Get the etcd response for a specified key
        /// </summary>
        /// <returns>The etcd response for the specified key</returns>
        /// <param name="key">Key for which value need to be fetched</param>
        public RangeResponse Get(string key, Metadata headers = null)
        {
            RangeResponse rangeResponse = new RangeResponse();

            rangeResponse = Get(new RangeRequest
            {
                Key = ByteString.CopyFromUtf8(key)
            }, headers);


            return(rangeResponse);
        }
            public void Get_next_page()
            {
                // Arrange
                var range = new RangeResponse(1, 15, 250);

                // Act
                var next = range.NextPage;

                // Assert
                next.From.ShouldBe(16);
                next.To.ShouldBe(30);
            }
Exemple #22
0
        /// <summary>
        /// Gets the range of keys with the specified prefix in async
        /// </summary>
        /// <returns>RangeResponse containing range of key-values</returns>
        /// <param name="prefixKey">Prefix key</param>
        public async Task <RangeResponse> GetRangeAsync(string prefixKey)
        {
            RangeResponse rangeResponse = new RangeResponse();

            string rangeEnd = GetRangeEnd(prefixKey);

            rangeResponse = await GetAsync(new RangeRequest
            {
                Key      = ByteString.CopyFromUtf8(prefixKey),
                RangeEnd = ByteString.CopyFromUtf8(rangeEnd)
            });

            return(rangeResponse);
        }
Exemple #23
0
        /// <summary>
        /// Gets the range of keys with the specified prefix in async
        /// </summary>
        /// <returns>Dictionary containing range of key-values</returns>
        /// <param name="prefixKey">Prefix key</param>
        public async Task <IDictionary <string, string> > GetRangeValAsync(string prefixKey)
        {
            RangeResponse rangeResponse = new RangeResponse();

            string rangeEnd = GetRangeEnd(prefixKey);

            rangeResponse = await GetAsync(new RangeRequest
            {
                Key      = ByteString.CopyFromUtf8(prefixKey),
                RangeEnd = ByteString.CopyFromUtf8(rangeEnd)
            });


            return(RangeRespondToDictionary(rangeResponse));
        }
Exemple #24
0
        /// <summary>
        /// Gets the range of keys with the specified prefix
        /// </summary>
        /// <returns>Dictionary containing range of key-values</returns>
        /// <param name="prefixKey">Prefix key</param>
        public IDictionary <string, string> GetRangeVal(string prefixKey)
        {
            RangeResponse rangeResponse = new RangeResponse();

            string rangeEnd = GetRangeEnd(prefixKey);

            rangeResponse = Get(new RangeRequest
            {
                Key      = ByteString.CopyFromUtf8(prefixKey),
                RangeEnd = ByteString.CopyFromUtf8(rangeEnd)
            });



            return(RangeRespondToDictionary(rangeResponse));
        }
Exemple #25
0
        /// <summary>
        /// Gets the range of keys with the specified prefix
        /// </summary>
        /// <returns>RangeResponse containing range of key-values</returns>
        /// <param name="prefixKey">Prefix key</param>
        public RangeResponse GetRange(string prefixKey, Metadata headers = null)
        {
            RangeResponse rangeResponse = new RangeResponse();

            string rangeEnd = GetRangeEnd(prefixKey);

            rangeResponse = Get(new RangeRequest
            {
                Key      = ByteString.CopyFromUtf8(prefixKey),
                RangeEnd = ByteString.CopyFromUtf8(rangeEnd)
            }, headers);



            return(rangeResponse);
        }
Exemple #26
0
        /// <summary>
        /// Gets the range of keys with the specified prefix
        /// </summary>
        /// <returns>RangeResponse containing range of key-values</returns>
        /// <param name="prefixKey">Prefix key</param>
        public RangeResponse GetRange(string prefixKey)
        {
            RangeResponse rangeResponse = new RangeResponse();

            string rangeEnd = GetRangeEnd(prefixKey);

            rangeResponse = Get(new RangeRequest
            {
                Key      = ByteString.CopyFromUtf8(prefixKey),
                RangeEnd = ByteString.CopyFromUtf8(rangeEnd)
            });



            return(rangeResponse);
        }
        public void Get()
        {
            Mock <IEtcdClient> etcdClientMock = new Mock <IEtcdClient>();
            Mock <IDisposable> disposableMock = new Mock <IDisposable>();

            EtcdCompoundClient client = new EtcdCompoundClient(etcdClientMock.Object, disposableMock.Object);

            Fixture fixture = new Fixture();

            string        key      = fixture.Create <string>();
            RangeResponse response = fixture.Create <RangeResponse>();

            etcdClientMock.Setup(p => p.Get(key, null, null, CancellationToken.None)).Returns(response);

            RangeResponse actualResponse = client.Get(key);

            etcdClientMock.Verify(p => p.Get(key, null, null, CancellationToken.None), Times.Once);
            Assert.AreSame(response, actualResponse);
        }
Exemple #28
0
        /// <summary>
        /// Get the value for a specified key
        /// </summary>
        /// <returns>The value for the specified key</returns>
        /// <param name="key">Key for which value need to be fetched</param>
        public string GetVal(string key)
        {
            RangeResponse rangeResponse = new RangeResponse();

            try
            {
                rangeResponse = Get(key);
            }
            catch (RpcException)
            {
                ResetConnection();
                throw;
            }
            catch
            {
                throw;
            }

            return(rangeResponse.Count != 0 ? rangeResponse.Kvs[0].Value.ToStringUtf8().Trim() : string.Empty);
        }
Exemple #29
0
        /// <summary>
        /// Get the value for a specified key in async
        /// </summary>
        /// <returns>The value for the specified key</returns>
        /// <param name="key">Key for which value need to be fetched</param>
        public async Task <string> GetValAsync(string key)
        {
            RangeResponse rangeResponse = new RangeResponse();

            try
            {
                rangeResponse = await GetAsync(key);
            }
            catch (RpcException)
            {
                ResetConnection();
                throw;
            }
            catch
            {
                throw;
            }

            return(rangeResponse.Count != 0 ? rangeResponse.Kvs[0].Value.ToStringUtf8().Trim() : string.Empty);
        }
Exemple #30
0
        /// <summary>
        /// Get the etcd response for a specified key in async
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The etcd response for the specified request</returns>
        public async Task <RangeResponse> GetAsync(RangeRequest request)
        {
            RangeResponse rangeResponse = new RangeResponse();

            try
            {
                rangeResponse = await _kvClient.RangeAsync(request, _headers);
            }
            catch (RpcException ex)
            {
                ResetConnection(ex);
                throw;
            }
            catch
            {
                throw;
            }

            return(rangeResponse);
        }
Exemple #31
0
        /// <summary>
        /// Get the etcd response for a specified RangeRequest
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The etcd response for the specified request</returns>
        public RangeResponse Get(RangeRequest request)
        {
            RangeResponse rangeResponse = new RangeResponse();

            try
            {
                rangeResponse = _kvClient.Range(request, _headers);
            }
            catch (RpcException ex)
            {
                ResetConnection(ex);
                throw;
            }
            catch
            {
                throw;
            }

            return(rangeResponse);
        }
Exemple #32
0
        /// <summary>
        /// Get the etcd response for a specified key
        /// </summary>
        /// <returns>The etcd response for the specified key</returns>
        /// <param name="key">Key for which value need to be fetched</param>
        public RangeResponse Get(string key)
        {
            RangeResponse rangeResponse = new RangeResponse();

            try
            {
                rangeResponse = _kvClient.Range(new RangeRequest
                {
                    Key = ByteString.CopyFromUtf8(key)
                }, _headers);
            }
            catch (RpcException)
            {
                ResetConnection();
                throw;
            }
            catch
            {
                throw;
            }

            return(rangeResponse);
        }