private async Task <DeleteResult> DeleteWithChildren(DeleteRequest request)
        {
            while (true)
            {
                var children = await GetChildrenAsync(new GetChildrenRequest(request.Path)).ConfigureAwait(false);

                if (!children.IsSuccessful)
                {
                    return(DeleteResult.Unsuccessful(children.Status, request.Path, children.Exception));
                }

                foreach (var name in children.ChildrenNames)
                {
                    var deleted = await DeleteWithChildren(new DeleteRequest(ZooKeeperPath.Combine(request.Path, name))).ConfigureAwait(false);

                    if (!deleted.IsSuccessful)
                    {
                        return(DeleteResult.Unsuccessful(deleted.Status, request.Path, deleted.Exception));
                    }
                }

                var result = await ExecuteOperation(new DeleteOperation(request)).ConfigureAwait(false);

                if (result.Status != ZooKeeperStatus.NodeHasChildren)
                {
                    return(result);
                }

                // Note(kungurtsev): someone has created a new child since we checked, delete it again.
            }
        }
        private async Task <CreateResult> CreateWithParents(CreateRequest request)
        {
            while (true)
            {
                var parentPath = ZooKeeperPath.GetParentPath(request.Path);
                if (parentPath == null)
                {
                    return(CreateResult.Unsuccessful(
                               ZooKeeperStatus.BadArguments,
                               request.Path,
                               new ArgumentException($"Can't get parent path for `{request.Path}`")));
                }

                var result = await CreateAsync(new CreateRequest(parentPath, CreateMode.Persistent)).ConfigureAwait(false);

                if (!result.IsSuccessful && result.Status != ZooKeeperStatus.NodeAlreadyExists)
                {
                    return(CreateResult.Unsuccessful(result.Status, request.Path, result.Exception));
                }

                result = await ExecuteOperation(new CreateOperation(request)).ConfigureAwait(false);

                if (result.Status != ZooKeeperStatus.NodeNotFound)
                {
                    return(result);
                }

                // Note(kungurtsev): someone has deleted our parent since we checked, create it again.
            }
        }
        public void Split_followed_by_Combine_should_not_change_path(string path)
        {
            var expected = path;

            for (var i = 0; i < 10; i++)
            {
                path = ZooKeeperPath.Combine(ZooKeeperPath.Split(path));
            }

            path.Should().Be(expected);
        }
Exemple #4
0
        public async Task TestDoesNotAttemptToCreateOrDeleteExistingNode()
        {
            // This doesn't work because creating the lock attempts to acquire which will then fail initially. We could work around this by testing
            // for a different set of conditions in the multi-ticket case, but the extra coverage doesn't seem valuable (we still have coverage of single-ticket)
            if (IsMultiTicketSemaphoreProvider)
            {
                Assert.Pass("not supported");
            }

            var path = new ZooKeeperPath($"/{this.GetType()}.{nameof(this.TestDoesNotAttemptToCreateOrDeleteExistingNode)} ({TargetFramework.Current})");

            using var connection = await ZooKeeperConnection.DefaultPool.ConnectAsync(
                      new ZooKeeperConnectionInfo (ZooKeeperPorts.DefaultConnectionString, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30), new EquatableReadOnlyList <ZooKeeperAuthInfo>(Array.Empty <ZooKeeperAuthInfo>())),
                      CancellationToken.None
                      );

            // pre-clean up just in case
            try { await connection.ZooKeeper.deleteAsync(path.ToString()); }
            catch (KeeperException.NoNodeException) { }

            this._provider.Strategy.AssumeNodeExists = true;
            var @lock = this._provider.CreateLockWithExactName(path.ToString());

            Assert.That(
                Assert.ThrowsAsync <InvalidOperationException>(() => @lock.TryAcquireAsync().AsTask()).Message,
                Does.Contain("does not exist")
                );

            await connection.ZooKeeper.createAsync(path.ToString(), Array.Empty <byte>(), new List <ACL> {
                ZooKeeperNodeCreator.PublicAcl
            }, CreateMode.PERSISTENT);

            try
            {
                await using (var handle = await @lock.TryAcquireAsync())
                {
                    Assert.IsNotNull(handle);
                }

                Assert.IsNotNull(await connection.ZooKeeper.existsAsync(path.ToString()));
            }
            finally
            {
                await connection.ZooKeeper.deleteAsync(path.ToString());
            }
        }
 public void GetSequentialNodeIndex_should_provide_correct_node_index(string path, long?expectedIndex)
 {
     ZooKeeperPath.GetSequentialNodeIndex(path).Should().Be(expectedIndex);
 }
 public void GetNodeName_should_provide_correct_node_name(string path, string expectedName)
 {
     ZooKeeperPath.GetNodeName(path).Should().Be(expectedName);
 }
 public void GetParentPath_should_provide_correct_parent_paths(string path, string expectedParent)
 {
     ZooKeeperPath.GetParentPath(path).Should().Be(expectedParent);
 }
 public void Combine_should_work_correctly_for_two_segments(string basePath, string relativePath, string expected)
 {
     ZooKeeperPath.Combine(basePath, relativePath).Should().Be(expected);
 }
 public void Combine_should_work_correctly_for_multiple_segments(string[] segments, string expected)
 {
     ZooKeeperPath.Combine(segments).Should().Be(expected);
 }
 public void Split_should_split_by_slashes(string path, string[] expected)
 {
     ZooKeeperPath.Split(path).Should().BeEquivalentTo(expected, options => options.WithStrictOrdering());
 }
 public string BuildReplicaPath([NotNull] string environment, [NotNull] string application, [NotNull] string replica) =>
 ZooKeeperPath.Combine(BuildApplicationPath(environment, application), Escape(replica));
 public string BuildApplicationPath([NotNull] string environment, [NotNull] string application) =>
 ZooKeeperPath.Combine(BuildEnvironmentPath(environment), Escape(application));