Exemplo n.º 1
0
        private static async Task CreateFlatTree(RingMasterClient ringMaster, string path, int maxNodes, int[] numCreated)
        {
            await ringMaster.Create(path, null, null, CreateMode.PersistentAllowPathCreation, throwIfNodeExists : false);

            while (numCreated[0] < maxNodes)
            {
                await ringMaster.Create($"{path}/{Guid.NewGuid()}", null, null, CreateMode.PersistentAllowPathCreation, throwIfNodeExists : false);

                numCreated[0]++;
            }
        }
Exemplo n.º 2
0
        public void TestCreateDontThrowIfNodeExists()
        {
            using (var requestHandler = new TestRequestHandler())
                using (var client = new RingMasterClient(requestHandler))
                {
                    string path = this.GetRandomString();
                    byte[] data = this.GetRandomData();
                    var    acl  = new List <Acl>()
                    {
                        new Acl((int)Acl.Perm.ALL, new Id(AuthSchemes.Digest, this.GetRandomString()))
                    };
                    var createMode     = CreateMode.Persistent;
                    var expectedResult = this.GetRandomString();

                    requestHandler.Implementation = request =>
                    {
                        Assert.IsTrue(request is RequestCreate);
                        var createRequest = (RequestCreate)request;
                        Assert.AreEqual(path, createRequest.Path);
                        Assert.AreEqual(data, createRequest.Data);
                        Assert.AreEqual(acl.Count, createRequest.Acl.Count);
                        Assert.IsTrue(Acl.AreEqual(acl[0], createRequest.Acl[0]));
                        Assert.AreEqual(createMode, createRequest.CreateMode);

                        return(new RequestResponse()
                        {
                            ResultCode = (int)RingMasterException.Code.Nodeexists,
                            Content = expectedResult
                        });
                    };

                    client.Create(path, data, acl, createMode, throwIfNodeExists: false).Wait();
                }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Send a poison pill to the given ringmaster affecting the given path.
        /// </summary>
        /// <param name="ringMaster">Ring master to which the poison pill must be sent</param>
        /// <param name="path">Path affected by the poison pill</param>
        /// <returns>A Task that tracks execution of this method</returns>
        private static async Task SendPoisonPillCommand(RingMasterClient ringMaster, string path)
        {
            await ringMaster.SetAuth(new Id(AuthSchemes.Digest, "commander"));

            string poisonPillCommand = string.Format("break:{0}", path);

            byte[] data = Encoding.UTF8.GetBytes(poisonPillCommand);
            await ringMaster.Create("$/poisonpill", data, null, CreateMode.Persistent);
        }
Exemplo n.º 4
0
        public void TestRequestInMultiReturnCorrectly()
        {
            TestRequestInMultiReturnCorrectlyAsync().GetAwaiter().GetResult();

            async Task TestRequestInMultiReturnCorrectlyAsync()
            {
                const string RootName = nameof(this.TestRequestInMultiReturnCorrectly);

                using (var rm = new RingMasterClient(serverAddress, null, null, 10000))
                {
                    await rm.Create($"/{RootName}/child1", null, null, CreateMode.PersistentAllowPathCreation);

                    await rm.Create($"/{RootName}/child2", null, null, CreateMode.PersistentAllowPathCreation);

                    await rm.Create($"/{RootName}/child3", null, null, CreateMode.PersistentAllowPathCreation);

                    var ops = new List <Op>
                    {
                        Op.Check($"/{RootName}", -1),
                        Op.GetChildren($"/{RootName}"),
                    };

                    var multiResult = await rm.Multi(ops);

                    Assert.AreEqual(multiResult.Count, ops.Count);

                    var checkResult       = multiResult[0] as OpResult.CheckResult;
                    var getChildrenResult = multiResult[1] as OpResult.GetChildrenResult;

                    Assert.AreEqual(3, checkResult.Stat.NumChildren);
                    Assert.AreEqual(RingMasterException.Code.Ok, getChildrenResult.ErrCode);
                    Assert.AreEqual(3, getChildrenResult.Children.Count);
                    Assert.AreEqual(3, getChildrenResult.Stat.NumChildren);
                }
            }
        }
Exemplo n.º 5
0
        public void TestCreateSingleThread()
        {
            using (var cancellationSource = new CancellationTokenSource())
            {
                var cancel = cancellationSource.Token;

                using (var client = new RingMasterClient(serverAddress, null, null, 10000))
                {
                    var rnd         = new Random();
                    var createCount = 0;
                    var dataSize    = 0;

                    Task.Run(async() =>
                    {
                        while (!cancel.IsCancellationRequested)
                        {
                            var data = new byte[rnd.Next(minPayloadSize, maxPayloadSize)];
                            var path = await client.Create($"/Perf/{createCount}", data, null, CreateMode.PersistentAllowPathCreation);
                            Assert.AreNotEqual(null, path);

                            Interlocked.Increment(ref createCount);
                            Interlocked.Add(ref dataSize, data.Length);
                        }
                    });

                    Task.Run(async() =>
                    {
                        var lastCount = createCount;

                        while (!cancel.IsCancellationRequested)
                        {
                            await Task.Delay(1000);
                            var delta = createCount - lastCount;
                            lastCount = createCount;

                            log($"{DateTime.Now} createCount={createCount} +{delta} dataSize={dataSize}");
                        }
                    });

                    Thread.Sleep(10 * 1000);
                    cancellationSource.Cancel();
                    log($"CreateCount = {createCount}");
                }
            }
        }
Exemplo n.º 6
0
        private static async Task CreateRandomStructuredTree(RingMasterClient ringMaster, string path, int maxNodes, int[] numCreated)
        {
            var random = new Random();
            var paths  = new Queue <string>();

            paths.Enqueue(path);

            while ((paths.Count > 0) && (numCreated[0] < maxNodes))
            {
                var currentPath = paths.Dequeue();
                await ringMaster.Create(currentPath, null, null, CreateMode.PersistentAllowPathCreation, throwIfNodeExists : false);

                numCreated[0]++;

                for (int i = 0; i < random.Next(1, 10); i++)
                {
                    string childPath = string.Format("{0}/{1}", currentPath, Guid.NewGuid());
                    paths.Enqueue(childPath);
                }
            }
        }
Exemplo n.º 7
0
        public void TestConcurrentDelete()
        {
            TestAsync().GetAwaiter().GetResult();

            async Task TestAsync()
            {
                const int    ChildrenCount = 1000;
                const string RootName      = nameof(this.TestConcurrentDelete);
                const int    threadCount   = 64;

                using (var client = new RingMasterClient(serverAddress, null, null, 10000))
                {
                    await client.Create($"/{RootName}", null, null, CreateMode.PersistentAllowPathCreation).ConfigureAwait(false);

                    var ops = new List <Op>(ChildrenCount);
                    for (int count = 0; count < ChildrenCount; count++)
                    {
                        ops.Add(Op.Create($"/{RootName}/{count}", null, null, CreateMode.PersistentAllowPathCreation));
                    }

                    await client.Batch(ops).ConfigureAwait(false);
                }

                for (int i = 0; i < threadCount; i++)
                {
                    var deleteChildTask = this.DeleteChild(RootName, ChildrenCount);
                    var deleteParent    = Task.Run(async() =>
                    {
                        using (var client = new RingMasterClient(serverAddress, null, null, 10000))
                        {
                            await client.Delete($"/{RootName}", -1, DeleteMode.None);
                        }
                    });
                }
            }
        }
Exemplo n.º 8
0
        public void TestConflictingCreateDeleteExists()
        {
            TestConflictingCreateDeleteExistsAsync().GetAwaiter().GetResult();

            async Task TestConflictingCreateDeleteExistsAsync()
            {
                const string path  = "/$rmbvt/test";
                var          stop  = false;
                var          taskW = Task.Run(async() =>
                {
                    while (!stop)
                    {
                        try
                        {
                            using (var rm = new RingMasterClient(serverAddress, null, null, 10000))
                            {
                                while (!stop)
                                {
                                    await rm.Create(path, null, null, CreateMode.PersistentAllowPathCreation | CreateMode.SuccessEvenIfNodeExistsFlag);
                                    await rm.Delete(path, -1, DeleteMode.SuccessEvenIfNodeDoesntExist);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            var rmException = ex as RingMasterException;
                            if (rmException != null && rmException.ErrorCode == RingMasterException.Code.Operationtimeout)
                            {
                                // Do thing. Continue test.
                            }
                            else
                            {
                                stop = true;
                                throw;
                            }
                        }
                    }
                });

                var taskR = Task.Run(async() =>
                {
                    while (!stop)
                    {
                        try
                        {
                            using (var rm = new RingMasterClient(serverAddress, null, null, 10000))
                            {
                                while (!stop)
                                {
                                    await Task.WhenAll(
                                        rm.Exists(path, null, true));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            var rmException = ex as RingMasterException;
                            if (rmException != null && rmException.ErrorCode == RingMasterException.Code.Operationtimeout)
                            {
                                // Do thing. Continue test.
                            }
                            else
                            {
                                stop = true;
                                throw;
                            }
                        }
                    }
                });

                var clock = Stopwatch.StartNew();

                SpinWait.SpinUntil(() => clock.Elapsed.TotalSeconds >= 3600 || stop);

                stop = true;
                await Task.WhenAll(taskW, taskR).ContinueWith(t => log(t.Exception?.ToString()));
            }
        }
Exemplo n.º 9
0
        public void TestGetFullSubtreeWhileUpdating()
        {
            TestAsync().GetAwaiter().GetResult();

            async Task TestAsync()
            {
                const int    InitialNodeData = 1;
                const int    NewNodeData     = 2;
                const int    ChildrenCount   = 50000;
                const string RootName        = nameof(this.TestGetFullSubtreeWhileUpdating);

                using (var client = new RingMasterClient(serverAddress, null, null, 100000))
                {
                    byte[] data = BitConverter.GetBytes(InitialNodeData);
                    await client.Create($"/{RootName}/node1", data, null, CreateMode.PersistentAllowPathCreation).ConfigureAwait(false);

                    await client.Create($"/{RootName}/node2", data, null, CreateMode.PersistentAllowPathCreation).ConfigureAwait(false);

                    await client.Create($"/{RootName}/node3", data, null, CreateMode.PersistentAllowPathCreation).ConfigureAwait(false);

                    var ops = new List <Op>(ChildrenCount);
                    for (int count = 0; count < ChildrenCount; count++)
                    {
                        ops.Add(Op.Create($"/{RootName}/node2/{count}", data, null, CreateMode.PersistentAllowPathCreation));
                    }

                    await client.Batch(ops).ConfigureAwait(false);
                }

                ManualResetEvent manualResetEvent = new ManualResetEvent(false);
                Task <TreeNode>  getSubtreeTask   = new Task <TreeNode>(() =>
                {
                    using (var client = new RingMasterClient(serverAddress, null, null, 10000))
                    {
                        return(client.GetFullSubtree($"/{RootName}").Result);
                    }
                });

                Task updateDataTask = Task.Run(async() =>
                {
                    using (var client = new RingMasterClient(serverAddress, null, null, 10000))
                    {
                        var ops        = new List <Op>(2);
                        byte[] newData = BitConverter.GetBytes(NewNodeData);

                        ops.Add(Op.SetData($"/{RootName}/node1", newData, -1));
                        ops.Add(Op.SetData($"/{RootName}/node3", newData, -1));

                        manualResetEvent.WaitOne();

                        // this is to make sure the set data occurs after get full substree started.
                        Thread.Sleep(20);
                        await client.Batch(ops).ConfigureAwait(false);
                    }
                });

                getSubtreeTask.Start();
                manualResetEvent.Set();

                await Task.WhenAll(getSubtreeTask, updateDataTask);

                var tree      = getSubtreeTask.Result;
                int node1Data = BitConverter.ToInt32(tree.Children[0].Data, 0);
                int node3Data = BitConverter.ToInt32(tree.Children[2].Data, 0);

                Assert.IsTrue(node1Data >= node3Data);
            }
        }