Esempio n. 1
0
        internal virtual void Complete(RedisResult result, bool includeDetail)
        {
            RedisConnectionBase.Trace("completed", "~ {0}", command);
            var snapshot = Interlocked.Exchange(ref messageResult, null); // only run once

            ChangeState(MessageState.Sent, MessageState.Complete);
            if (snapshot != null)
            {
                snapshot.Complete(result, this, includeDetail);
            }
        }
Esempio n. 2
0
        void IMessageResult.Complete(RedisResult result, RedisMessage message, bool includeDetail)
        {
            if (result.IsCancellation)
            {
                RedisConnectionBase.Trace("transaction", "cancelled");
                SetInnerReplies(result);
                completion.TrySetCanceled();
            }
            else if (result.IsError)
            {
                RedisConnectionBase.Trace("transaction", "error");
                SetInnerReplies(result);
                completion.SafeSetException(result.Error());
            }
            else
            {
                try
                {
                    if (result.IsNil)
                    {   // aborted
                        RedisConnectionBase.Trace("transaction", "aborted");
                        SetInnerReplies(RedisResult.Cancelled);
                        completion.TrySetResult(false);
                    }
                    else
                    {
                        var items = result.ValueItems;
                        if (items.Length != (queued == null ? 0 : queued.Length))
                        {
                            throw new InvalidOperationException(string.Format("{0} results expected, {1} received", queued.Length, items.Length));
                        }

                        RedisConnectionBase.Trace("transaction", "success");
                        for (int i = 0; i < items.Length; i++)
                        {
                            RedisResult reply = items[i];
                            RedisConnectionBase.CallbackMode callbackMode;
                            var ctx = parent.ProcessReply(ref reply, queued[i].InnerMessage, out callbackMode);
                            RedisConnectionBase.Trace("transaction", "{0} = {1}", ctx, reply);
                            parent.ProcessCallbacks(ctx, reply, callbackMode);
                        }
                        completion.TrySetResult(true);
                    }
                }
                catch (Exception ex)
                {
                    completion.SafeSetException(ex);
                    throw;
                }
            }
        }
Esempio n. 3
0
 void SetInnerReplies(RedisResult result)
 {
     if (queued != null)
     {
         for (int i = 0; i < queued.Length; i++)
         {
             var reply = result; // need to be willing for this to be mutated
             RedisConnectionBase.CallbackMode callbackMode;
             var ctx = parent.ProcessReply(ref reply, queued[i].InnerMessage, out callbackMode);
             RedisConnectionBase.Trace("transaction", "{0} = {1}", ctx, reply);
             parent.ProcessCallbacks(ctx, reply, callbackMode);
         }
     }
 }
Esempio n. 4
0
 public void Abort(RedisMessage[] messages)
 {
     if (messages != null)
     {
         for (int i = 0; i < messages.Length; i++)
         {
             var reply = RedisResult.Cancelled;
             RedisConnectionBase.CallbackMode callbackMode;
             var ctx = parent.ProcessReply(ref reply, messages[i], out callbackMode);
             RedisConnectionBase.Trace("transaction", "{0} = {1}", ctx, reply);
             parent.ProcessCallbacks(ctx, reply, callbackMode);
         }
     }
 }
Esempio n. 5
0
 protected void WriteCommand(Stream stream, int argCount)
 {
     try
     {
         RedisConnectionBase.Trace("send", "write @{1}: {0}", this, stream.Position);
         stream.WriteByte((byte)'*');
         WriteRaw(stream, argCount + 1);
         WriteUnified(stream, command);
     }
     catch (Exception ex)
     {
         RedisConnectionBase.Trace("send", ex.Message);
         throw;
     }
 }
Esempio n. 6
0
        private static void ConnectToNodes(TextWriter log, string tieBreakerKey, int syncTimeout, int keepAlive, bool allowAdmin, string clientName, string[] arr, List <RedisConnection> connections, out Task <string>[] infos, out Task <string>[] aux, AuxMode mode)
        {
            TraceWriteTime("Infos");
            infos = new Task <string> [arr.Length];
            aux   = new Task <string> [arr.Length];
            var opens = new Task[arr.Length];

            for (int i = 0; i < arr.Length; i++)
            {
                var option = arr[i];
                if (string.IsNullOrWhiteSpace(option))
                {
                    continue;
                }

                RedisConnection conn = null;
                try
                {
                    var parts = option.Split(':');
                    if (parts.Length == 0)
                    {
                        continue;
                    }

                    string host = parts[0].Trim();
                    int    port = 6379, tmp;
                    if (parts.Length > 1 && int.TryParse(parts[1].Trim(), out tmp))
                    {
                        port = tmp;
                    }
                    conn      = new RedisConnection(host, port, syncTimeout: syncTimeout, allowAdmin: allowAdmin);
                    conn.Name = clientName;
                    log.WriteLine("Opening connection to {0}:{1}...", host, port);
                    if (keepAlive >= 0)
                    {
                        conn.SetKeepAlive(keepAlive);
                    }
                    opens[i] = conn.Open();
                    var info = conn.GetInfoImpl(null, false, false);
                    connections.Add(conn);
                    infos[i] = info;
                    switch (mode)
                    {
                    case AuxMode.TieBreakers:
                        if (tieBreakerKey != null)
                        {
                            aux[i] = conn.Strings.GetString(0, tieBreakerKey);
                        }
                        break;

                    case AuxMode.ClusterNodes:
                        aux[i] = conn.Cluster.GetNodes();
                        break;
                    }
                }
                catch (Exception ex)
                {
                    if (conn == null)
                    {
                        log.WriteLine("Error parsing option \"{0}\": {1}", option, ex.Message);
                    }
                    else
                    {
                        log.WriteLine("Error connecting: {0}", ex.Message);
                    }
                }
            }

            TraceWriteTime("Wait for infos");
            RedisConnectionBase.Trace("select-create", "wait...");
            var watch = new Stopwatch();

            foreach (Task task in infos.Concat(aux).Concat(opens))
            {
                if (task != null)
                {
                    try
                    {
                        int remaining = unchecked ((int)(syncTimeout - watch.ElapsedMilliseconds));
                        if (remaining > 0)
                        {
                            task.Wait(remaining);
                        }
                    }
                    catch { }
                }
            }
            watch.Stop();
            RedisConnectionBase.Trace("select-create", "complete");
        }