Пример #1
0
        /// <summary>
        /// LeaseGrant creates a lease in async which expires if the server does not receive a keepAlive
        /// within a given time to live period. All keys attached to the lease will be expired and
        /// deleted if the lease expires. Each expired key generates a delete event in the event history.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <LeaseGrantResponse> LeaseGrantAsync(LeaseGrantRequest request, Metadata headers = null)
        {
            LeaseGrantResponse response = new LeaseGrantResponse();
            bool success    = false;
            int  retryCount = 0;

            while (!success)
            {
                try
                {
                    response = await _balancer.GetConnection().leaseClient.LeaseGrantAsync(request, headers);

                    success = true;
                }
                catch (RpcException ex) when(ex.StatusCode == StatusCode.Unavailable)
                {
                    retryCount++;
                    if (retryCount >= _balancer._numNodes)
                    {
                        throw ex;
                    }
                }
            }
            return(response);
        }
Пример #2
0
        /// <summary>
        /// LeaseGrant creates a lease in async which expires if the server does not receive a keepAlive
        /// within a given time to live period. All keys attached to the lease will be expired and
        /// deleted if the lease expires. Each expired key generates a delete event in the event history.
        /// </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 response received from the server.</returns>
        public async Task <LeaseGrantResponse> LeaseGrantAsync(LeaseGrantRequest request,
                                                               Grpc.Core.Metadata headers          = null, DateTime?deadline = null,
                                                               CancellationToken cancellationToken = default)
        {
            LeaseGrantResponse response = new LeaseGrantResponse();
            bool success    = false;
            int  retryCount = 0;

            while (!success)
            {
                try
                {
                    response = await _balancer.GetConnection().leaseClient
                               .LeaseGrantAsync(request, headers, deadline, cancellationToken);

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

            return(response);
        }
Пример #3
0
 public static Entity.LeaseGrantResponse FromProto(this LeaseGrantResponse response)
 {
     return(new Entity.LeaseGrantResponse()
     {
         Error = response.Error,
         Header = response.Header.FromProto(),
         ID = response.ID,
         TTL = response.TTL
     });
 }
Пример #4
0
        public LeaseGrantResponse Grant(long ttl)
        {
            Etcdserverpb.LeaseGrantRequest leaseGrantRequest = new Etcdserverpb.LeaseGrantRequest
            {
                TTL = ttl
            };
            var rsp = leaseClient.LeaseGrant(leaseGrantRequest);
            LeaseGrantResponse response = new LeaseGrantResponse(rsp);

            return(response);
        }
Пример #5
0
        public LeaseGrantResponse Grant(long ttl)
        {
            Etcdserverpb.LeaseGrantRequest leaseGrantRequest = new Etcdserverpb.LeaseGrantRequest();
            leaseGrantRequest.TTL = ttl;
            var rsp = leaseClient.LeaseGrant(leaseGrantRequest);
            LeaseGrantResponse response = new LeaseGrantResponse(rsp);

            return(response);

            //  return Util.ToCompletableFutureWithRetry(
            //       this.stub.LeaseGrant(leaseGrantRequest),
            //      new FunctionResponse<Etcdserverpb.LeaseGrantRequest, LeaseGrantResponse>()
            //  );
        }
        public void LeaseGrant()
        {
            Mock <IEtcdClient> etcdClientMock = new Mock <IEtcdClient>();
            Mock <IDisposable> disposableMock = new Mock <IDisposable>();

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

            Fixture fixture = new Fixture();

            LeaseGrantRequest  request  = fixture.Create <LeaseGrantRequest>();
            LeaseGrantResponse response = fixture.Create <LeaseGrantResponse>();

            etcdClientMock.Setup(p => p.LeaseGrant(request, null, null, CancellationToken.None)).Returns(response);

            LeaseGrantResponse actualResponse = client.LeaseGrant(request);

            etcdClientMock.Verify(p => p.LeaseGrant(request, null, null, CancellationToken.None), Times.Once);
            Assert.AreSame(response, actualResponse);
        }
Пример #7
0
        private static async Task <bool> AddLeader(EtcdClient client, string key, string value, LeaseGrantResponse lease)
        {
            var protoKey         = Google.Protobuf.ByteString.CopyFromUtf8(key);
            var transactionAsync = await client.TransactionAsync(new TxnRequest
            {
                Compare =
                {
                    new Compare {
                        Key = protoKey, Version = 0
                    }
                },
                Success =
                {
                    new RequestOp
                    {
                        RequestPut = new PutRequest
                        {
                            Key   = protoKey,
                            Value = Google.Protobuf.ByteString.CopyFromUtf8(value),
                            Lease = lease.ID
                        }
                    }
                },
                Failure = { }
            });

            return(transactionAsync.Succeeded);
        }