Beispiel #1
0
        public async Task <bool> TryCreateEnvironmentAsync(IEnvironmentInfo environmentInfo)
        {
            var createRequest = new CreateRequest(pathHelper.BuildEnvironmentPath(environmentInfo.Environment), CreateMode.Persistent)
            {
                Data = EnvironmentNodeDataSerializer.Serialize(environmentInfo)
            };

            return((await zooKeeperClient.CreateAsync(createRequest).ConfigureAwait(false)).IsSuccessful);
        }
Beispiel #2
0
        public async Task <BizResult <bool> > CreateNode([FromBody] CreateNode node)
        {
            if (await _zookeeperClient.ExistsAsync(node.Path))
            {
                return(new BizResult <bool>(false, -1, $"Node '{node.Path}' already exists!"));
            }
            await _zookeeperClient.CreateAsync(node.Path, node.Value, node.CreateMode ?? CreateMode.PERSISTENT);

            return(new BizResult <bool>(true));
        }
        private async Task EnsureNodeExistsAsync()
        {
            if (!await EnvironmentExistsAsync().ConfigureAwait(false))
            {
                return;
            }

            var existsNode = await zooKeeperClient.ExistsAsync(new ExistsRequest(replicaNodePath) { Watcher = nodeWatcher }).ConfigureAwait(false);

            if (!existsNode.IsSuccessful)
            {
                return;
            }

            if (registrationAllowed.TrySet(settings.RegistrationAllowedProvider?.Invoke() ?? true))
            {
                log.Info(registrationAllowed ? "Registration has been allowed." : "Registration has been denied.");
            }

            if (!registrationAllowed)
            {
                if (existsNode.Stat != null)
                {
                    await DeleteNodeAsync().ConfigureAwait(false);
                }
                return;
            }

            if (existsNode.Stat != null)
            {
                if (existsNode.Stat.EphemeralOwner == zooKeeperClient.SessionId)
                {
                    nodeCreatedOnceSignal.Set();
                    return;
                }

                var lastConnected    = new DateTime(Interlocked.Read(ref lastConnectedTimestamp), DateTimeKind.Utc);
                var nodeCreationTime = existsNode.Stat.CreatedTime;
                if (nodeCreationTime > lastConnected)
                {
                    log.Warn(
                        "Node with path '{ReplicaNodePath}' already exists " +
                        "and has owner session id = {NodeEphemeralOwner:x16}, " +
                        "which differs from our id = {ClientSessionId:x16}. " +
                        "But it was created recently (at {NodeCreationTime}) so we won't touch it. " +
                        "This may indicate several beacons with same environment, application and replica exist.",
                        replicaNodePath,
                        existsNode.Stat.EphemeralOwner,
                        zooKeeperClient.SessionId,
                        nodeCreationTime);

                    return;
                }

                log.Warn(
                    "Node with path '{ReplicaNodePath}' already exists, " +
                    "but looks like a stale one from ourselves. " +
                    "It has owner session id = {NodeEphemeralOwner:x16}, " +
                    "which differs from our id = {ClientSessionId:x16}. " +
                    "It was created at {NodeCreationTime}. " +
                    "Will delete it and create a new one.",
                    replicaNodePath,
                    existsNode.Stat.EphemeralOwner,
                    zooKeeperClient.SessionId,
                    nodeCreationTime);

                if (!await DeleteNodeAsync().ConfigureAwait(false))
                {
                    return;
                }
            }

            var createRequest = new CreateRequest(replicaNodePath, CreateMode.Ephemeral)
            {
                Data = replicaNodeData
            };

            var create = await zooKeeperClient.CreateAsync(createRequest).ConfigureAwait(false);

            if (create.IsSuccessful)
            {
                nodeCreatedOnceSignal.Set();
            }

            if (!create.IsSuccessful && create.Status != ZooKeeperStatus.NodeAlreadyExists)
            {
                log.Error("Node creation has failed.");
                return;
            }

            await zooKeeperClient.ExistsAsync(new ExistsRequest(replicaNodePath) { Watcher = nodeWatcher }).ConfigureAwait(false);
        }
 /// <inheritdoc cref="IZooKeeperClient.CreateAsync"/>
 public static async Task <CreateResult> CreateAsync(this IZooKeeperClient client, [NotNull] string path, CreateMode createMode, [CanBeNull] byte[] data = null) =>
 await client.CreateAsync(new CreateRequest(path, createMode) { Data = data }).ConfigureAwait(false);
 /// <inheritdoc cref="IZooKeeperClient.CreateAsync"/>
 public static CreateResult Create(this IZooKeeperClient client, CreateRequest request) =>
 client.CreateAsync(request).GetAwaiter().GetResult();