Beispiel #1
0
 public Task SendClusterConfigurationAsync(ClusterConfiguration configuration)
 {
     try
     {
         var command = ClusterConfigurationUpdateCommand.Create(configuration);
         raftEngine.AppendCommand(command);
         return(command.Completion.Task);
     }
     catch (NotLeadingException)
     {
         return(SendClusterConfigurationInternalAsync(raftEngine.GetLeaderNode(WaitForLeaderTimeoutInSeconds), configuration));
     }
 }
Beispiel #2
0
        public async Task <HttpResponseMessage> Read([FromUri] string key, [FromUri] string mode = null)
        {
            switch (mode)
            {
            case "quorum":
                var taskCompletionSource = new TaskCompletionSource <object>();
                try
                {
                    RaftEngine.AppendCommand(new GetCommand
                    {
                        Key        = key,
                        Completion = taskCompletionSource
                    });
                }
                catch (NotLeadingException e)
                {
                    return(RedirectToLeader(e.CurrentLeader, Request.RequestUri));
                }
                var consistentRead = await taskCompletionSource.Task;
                return(Request.CreateResponse(HttpStatusCode.OK, new
                {
                    RaftEngine.State,
                    Key = key,
                    Value = consistentRead,
                    Missing = consistentRead == null
                }));

            case "leader":
                if (RaftEngine.State != RaftEngineState.Leader)
                {
                    return(RedirectToLeader(RaftEngine.CurrentLeader, Request.RequestUri));
                }
                goto case null;

            case "any":
            case null:
                var read = StateMachine.Read(key);
                return(Request.CreateResponse(HttpStatusCode.OK, new
                {
                    RaftEngine.State,
                    Key = key,
                    Value = read,
                    Missing = read == null
                }));

            default:
                return(Request.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    Error = "Unknown read mode"
                }));
            }
        }
Beispiel #3
0
        private async Task <HttpResponseMessage> Batch(KeyValueOperation[] operations)
        {
            var taskCompletionSource = new TaskCompletionSource <object>();

            try
            {
                RaftEngine.AppendCommand(new OperationBatchCommand
                {
                    Batch      = operations,
                    Completion = taskCompletionSource
                });
            }
            catch (NotLeadingException e)
            {
                return(RedirectToLeader(e.CurrentLeader, Request.RequestUri));
            }
            await taskCompletionSource.Task;

            return(Request.CreateResponse(HttpStatusCode.Accepted));
        }