示例#1
0
 private void UnregisterTypes(IRedisPipeline p)
 {
     foreach (var typeName in TypeNames)
     {
         p.QueueCommand(q => q.RemoveByPattern(UnregisterTypesFormatString.Fmt(RedisPrefix, NodeId.ToString())));
     }
     p.QueueCommand(q => q.Remove(RedisNodeRefreshKeySet));
 }
示例#2
0
 private void RegisterTypes(IRedisPipeline p)
 {
     foreach (var typeName in TypeNames)
     {
         p.QueueCommand(q => q.Set("{0}:req:{1}:{2}".Fmt(RedisPrefix, typeName, NodeId.ToString()), HostContext.AppHost.Config.WebHostUrl, NodeTimeoutPeriod));
     }
 }
示例#3
0
        public static void Run(object obj)
        {
            Tuple <int, int> tuple = (Tuple <int, int>)obj;
            int          partition = tuple.Item1, clientIndex = tuple.Item2;
            IRedisClient redisClient = clients[clientIndex];

            // PinThreadOnCores(clientIndex);

            // debug
            Console.WriteLine("{0}:{1}", redisClient.Host, redisClient.Port);

            SLIM.Wait();
            long beginTicks = DateTime.Now.Ticks;

            for (int i = 0; i < BATCHES; i++)
            {
                using (IRedisPipeline pipe = redisClient.CreatePipeline())
                {
                    string hashId = StaticRandom.RandIdentity().ToString();
                    byte[] key    = BitConverter.GetBytes(StaticRandom.RandIdentity());
                    for (int j = 0; j < 100; j++)
                    {
                        pipe.QueueCommand(r => ((RedisNativeClient)r).HGet(hashId, key));
                    }
                    pipe.Flush();
                }
            }
            long endTicks = DateTime.Now.Ticks;

            int throughput = (int)(BATCHES * 100 * 1.0 / ((endTicks - beginTicks) * 1.0 / 10000000));
            // Console.WriteLine("Single Thread Throughput: {0}", throughput);
        }
示例#4
0
 private void RegisterTypes(IRedisPipeline p)
 {
     if (FirstNodeRegistration)
     {
         foreach (var typeName in TypeNames)
         {
             string key = TypeKeyFormatString.Fmt(RedisPrefix, typeName, NodeId.ToString());
             p.QueueCommand(q => q.Set(key, HostContext.AppHost.Config.WebHostUrl));
             p.QueueCommand(q => q.AddItemToSet(RedisNodeRefreshKeySet, key));
         }
         FirstNodeRegistration = false;
     }
     else
     {
         p.QueueCommand(q => q.ExecLuaSha(RefreshScriptSHA1, NodeTimeoutPeriod.TotalSeconds.ToString()));
     }
 }
示例#5
0
        internal override void Clear()
        {
            if (this.redisVersionDbMode == RedisVersionDbMode.Cluster)
            {
                // IMPORTMENT: Since the Redis Cluster doesn't allow multi-key commands across multiple hash slots
                // So we couldn't clear keys in batch
                //using (RedisClient redisClient = this.singletonConnPool.GetRedisClient())
                //{
                //    byte[][] keysAndArgs =
                //    {
                //        Encoding.ASCII.GetBytes(RedisVersionDb.VER_KEY_PREFIX),
                //    };
                //    string sha1 = this.LuaManager.GetLuaScriptSha1(LuaScriptName.REMOVE_KEYS_WITH_PREFIX);
                //    redisClient.EvalSha(sha1, 0, keysAndArgs);
                //}

                int batchSize = 100;
                using (RedisClient redisClient = this.singletonConnPool.GetRedisClient())
                {
                    byte[][] keys = redisClient.Keys(RedisVersionDb.VER_KEY_PREFIX + "*");
                    if (keys != null)
                    {
                        for (int i = 0; i < keys.Length; i += batchSize)
                        {
                            int upperBound = Math.Min(keys.Length, i + batchSize);
                            using (IRedisPipeline pipe = redisClient.CreatePipeline())
                            {
                                for (int j = i; j < upperBound; j++)
                                {
                                    string keyStr = Encoding.ASCII.GetString(keys[j]);
                                    pipe.QueueCommand(r => ((RedisNativeClient)r).Del(keyStr));
                                }
                                pipe.Flush();
                            }
                        }
                    }
                }
            }
            else
            {
                for (int pid = 0; pid < this.PartitionCount; pid++)
                {
                    using (RedisClient redisClient = this.RedisManager.GetClient(
                               this.redisDbIndex, RedisVersionDb.GetRedisInstanceIndex(pid)))
                    {
                        redisClient.FlushDb();
                    }
                }
            }
        }
示例#6
0
        static void LocalRedisBenchmarkTest()
        {
            int                count = 100000, pipelineSize = 100;
            Random             rand         = new Random();
            const string       chars        = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            Func <int, string> RandomString = (int length) =>
            {
                return(new string(Enumerable.Repeat(chars, length)
                                  .Select(s => s[rand.Next(s.Length)]).ToArray()));
            };

            Func <int, byte[]> RandomBytes = (int length) =>
            {
                byte[] value = new byte[length];
                rand.NextBytes(value);
                return(value);
            };

            RedisVersionDb redisVersionDb = RedisVersionDb.Instance();

            // Non-Pipeline Mode
            using (RedisClient client = redisVersionDb.RedisManager.GetClient(3, 0))
            {
                long now = DateTime.Now.Ticks;
                for (int i = 0; i < count; i++)
                {
                    string hashId = RandomString(3);
                    byte[] key    = BitConverter.GetBytes(3);
                    byte[] value  = RandomBytes(50);
                    int    type   = rand.Next(0, 1);
                    if (type == 0)
                    {
                        client.HSet(hashId, key, value);
                    }
                    else
                    {
                        client.HGet(hashId, key);
                    }
                }

                long time       = DateTime.Now.Ticks - now;
                int  throughput = (int)((count * 1.0) / (time * 1.0 / 10000000));

                Console.WriteLine("Redis Local Non Pipeline Throughput: {0} ops/s", throughput);
            }

            // Pipeline Mode

            using (RedisClient client = redisVersionDb.RedisManager.GetClient(3, 0))
            {
                long now = DateTime.Now.Ticks;

                int i = 0;
                while (i < count)
                {
                    using (IRedisPipeline pipeline = client.CreatePipeline())
                    {
                        for (int j = 0; j < pipelineSize; j++)
                        {
                            string hashId = RandomString(3);
                            byte[] key    = BitConverter.GetBytes(3);
                            byte[] value  = RandomBytes(50);
                            int    type   = rand.Next(0, 1);
                            if (type == 0)
                            {
                                pipeline.QueueCommand(
                                    r => ((RedisNativeClient)r).HSet(hashId, key, value));
                            }
                            else
                            {
                                pipeline.QueueCommand(
                                    r => ((RedisNativeClient)r).HGet(hashId, key));
                            }
                        }
                        pipeline.Flush();
                    }

                    i += pipelineSize;
                }
                long time       = DateTime.Now.Ticks - now;
                int  throughput = (int)((count * 1.0) / (time * 1.0 / 10000000));

                Console.WriteLine("Redis Local Pipeline({0}) Throughput: {1} ops/s", pipelineSize, throughput);
            }
        }
 public void SetAsync(string key, string value)
 {
     EnterPipeline();
     _pipeline.QueueCommand(c => c.Set(key, value));
 }
示例#8
0
 private void RegisterNode(IRedisPipeline p)
 {
     p.QueueCommand(q => q.Set(RedisNodeKey, Config, NodeTimeoutPeriod));
 }
示例#9
0
        internal void Flush(IEnumerable <RedisRequest> requests, RedisClient redisClient = null, int maxRequests = -1)
        {
            int reqCount = 0;

            using (IRedisPipeline pipe = redisClient.CreatePipeline())
            {
                foreach (RedisRequest req in requests)
                {
                    reqCount++;
                    if (req == null)
                    {
                        continue;
                    }

                    switch (req.Type)
                    {
                    case RedisRequestType.HGet:
                        pipe.QueueCommand(
                            r => ((RedisNativeClient)r).HGet(req.HashId, req.Key),
                            req.SetValue, req.SetError);
                        break;

                    case RedisRequestType.HMGet:
                        pipe.QueueCommand(
                            r => ((RedisNativeClient)r).HMGet(req.HashId, req.Keys),
                            req.SetValues, req.SetError);
                        break;

                    case RedisRequestType.HGetAll:
                        pipe.QueueCommand(
                            r => ((RedisNativeClient)r).HGetAll(req.HashId),
                            req.SetValues, req.SetError);
                        break;

                    case RedisRequestType.HSetNX:
                        pipe.QueueCommand(
                            r => ((RedisNativeClient)r).HSetNX(req.HashId, req.Key, req.Value),
                            req.SetLong, req.SetError);
                        break;

                    case RedisRequestType.HSet:
                        pipe.QueueCommand(
                            r => ((RedisNativeClient)r).HSet(req.HashId, req.Key, req.Value),
                            req.SetLong, req.SetError);
                        break;

                    case RedisRequestType.HMSet:
                        pipe.QueueCommand(
                            r => ((RedisNativeClient)r).HMSet(req.HashId, req.Keys, req.Values),
                            req.SetVoid, req.SetError);
                        break;

                    case RedisRequestType.HDel:
                        // delete a single field
                        if (req.Key != null)
                        {
                            pipe.QueueCommand(
                                r => ((RedisNativeClient)r).HDel(req.HashId, req.Key),
                                req.SetLong, req.SetError);
                        }
                        // delete multiple fields
                        else
                        {
                            pipe.QueueCommand(
                                r => ((RedisNativeClient)r).HDel(req.HashId, req.Keys),
                                req.SetLong, req.SetError);
                        }
                        break;

                    case RedisRequestType.EvalSha:
                        pipe.QueueCommand(
                            r => ((RedisNativeClient)r).EvalSha(req.Sha1, req.NumberKeysInArgs, req.Keys),
                            req.SetValues, req.SetError);
                        break;

                    default:
                        break;
                    }

                    if (maxRequests != -1 && reqCount >= maxRequests)
                    {
                        break;
                    }
                }
                pipe.Flush();
            }
        }
示例#10
0
        internal override void MockLoadData(int recordCount)
        {
            int pk = 0;
            RedisConnectionPool connPool = null;

            if (this.redisVersionDbMode == RedisVersionDbMode.Cluster)
            {
                connPool = this.singletonConnPool;
            }

            int loaded = 0;

            while (pk < this.PartitionCount)
            {
                Console.WriteLine("Loading Partition {0}", pk);
                if (connPool == null)
                {
                    connPool = this.RedisManager.GetClientPool(this.redisDbIndex, RedisVersionDb.GetRedisInstanceIndex(pk));
                }

                using (RedisClient redisClient = connPool.GetRedisClient())
                {
                    int batchSize  = 100;
                    int partitions = this.PartitionCount;

                    for (int i = pk; i < recordCount; i += partitions * batchSize)
                    {
                        int upperBound = Math.Min(recordCount, i + partitions * batchSize);
                        using (IRedisPipeline pipe = redisClient.CreatePipeline())
                        {
                            for (int j = i; j < upperBound; j += partitions)
                            {
                                object recordKey = j;
                                string hashId    = recordKey.ToString();
                                if (this.redisVersionDbMode == RedisVersionDbMode.Cluster)
                                {
                                    hashId = RedisVersionDb.PACK_KEY(RedisVersionDb.VER_KEY_PREFIX, hashId);
                                }

                                VersionEntry versionEntry = new VersionEntry();
                                VersionEntry.InitFirstVersionEntry(new String('a', 100), versionEntry);

                                byte[] key   = BitConverter.GetBytes(VersionEntry.VERSION_KEY_START_INDEX + 1);
                                byte[] value = VersionEntry.Serialize(versionEntry);

                                pipe.QueueCommand(r => ((RedisNativeClient)r).HSet(hashId, key, value));
                                pipe.QueueCommand(r => ((RedisNativeClient)r).HSet(hashId, RedisVersionDb.LATEST_VERSION_PTR_FIELD, key));

                                loaded++;
                            }
                            pipe.Flush();
                        }
                    }
                }
                pk++;

                if (this.redisVersionDbMode != RedisVersionDbMode.Cluster)
                {
                    connPool = null;
                }
            }

            Console.WriteLine("Loaded {0} records Successfully", loaded);
        }
示例#11
0
 private void UnregisterNode(IRedisPipeline p)
 {
     p.QueueCommand(q => q.Remove(RedisNodeKey));
 }