Exemple #1
0
        private static async Task GetChildCount(RingMasterClient ringMaster, string path, bool showPath, int maxToShow = 100)
        {
            var subtree = await ringMaster.GetFullSubtree(path);

            log($"Total number of children: {GetTotalNumberofChildren(subtree)}, direct children: {(subtree.Children == null ? 0 : subtree.Children.Count)}");

            if (showPath && subtree.Children != null)
            {
                if (subtree.Children.Count > maxToShow)
                {
                    log($"showing the first {maxToShow} direct children:");
                }

                for (var i = 0; i < subtree.Children.Count; i++)
                {
                    if (i == maxToShow)
                    {
                        break;
                    }

                    log($"{path}/{subtree.Children[i].Name}");
                }
            }
        }
Exemple #2
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);
            }
        }