Beispiel #1
0
        protected void CreateEnvironmentNode(string environment, string parent = null, IReadOnlyDictionary <string, string> properties = null)
        {
            var info = new EnvironmentInfo(environment, parent, properties);
            var data = EnvironmentNodeDataSerializer.Serialize(info);

            var path = PathHelper.BuildEnvironmentPath(environment);

            CreateOrUpdate(path, data);
        }
Beispiel #2
0
        public void Build_should_combine_with_prefix(string prefix)
        {
            var environment = "default";
            var application = "App.1";
            var replica     = "http://some-infra-host123:13528/";

            var path = new ServiceDiscoveryPathHelper(prefix, ZooKeeperPathEscaper.Instance);

            path.BuildEnvironmentPath(environment).Should().Be("/prefix/nested/default");
            path.BuildApplicationPath(environment, application).Should().Be("/prefix/nested/default/App.1");
            path.BuildReplicaPath(environment, application, replica).Should().Be("/prefix/nested/default/App.1/http%3A%2F%2Fsome-infra-host123%3A13528%2F");
        }
Beispiel #3
0
        private void Update(string name, VersionedContainer <EnvironmentInfo> container)
        {
            if (isDisposed)
            {
                return;
            }

            try
            {
                var environmentPath = pathHelper.BuildEnvironmentPath(name);

                var environmentExists = zooKeeperClient.Exists(new ExistsRequest(environmentPath)
                {
                    Watcher = nodeWatcher
                });
                if (!environmentExists.IsSuccessful)
                {
                    return;
                }

                if (environmentExists.Stat == null)
                {
                    container.Clear();
                }
                else
                {
                    if (!container.NeedUpdate(environmentExists.Stat.ModifiedZxId))
                    {
                        return;
                    }

                    var environmentData = zooKeeperClient.GetData(new GetDataRequest(environmentPath)
                    {
                        Watcher = nodeWatcher
                    });
                    if (environmentData.Status == ZooKeeperStatus.NodeNotFound)
                    {
                        container.Clear();
                    }
                    if (!environmentData.IsSuccessful)
                    {
                        return;
                    }

                    var info = EnvironmentNodeDataSerializer.Deserialize(name, environmentData.Data);
                    container.Update(environmentData.Stat.ModifiedZxId, info);
                }
            }
            catch (Exception error)
            {
                log.Error(error, "Failed to update '{Environment}' environment.", name);
            }
        }
Beispiel #4
0
        public async Task <IReadOnlyList <string> > GetAllApplicationsAsync(string environment)
        {
            var data = await zooKeeperClient.GetChildrenAsync(new GetChildrenRequest(pathHelper.BuildEnvironmentPath(environment))).ConfigureAwait(false);

            if (data.Status == ZooKeeperStatus.NodeNotFound)
            {
                return(new string[0]);
            }

            data.EnsureSuccess();
            return(data.ChildrenNames.Select(n => pathHelper.Unescape(n)).ToList());
        }
        internal ServiceBeacon(
            [NotNull] IZooKeeperClient zooKeeperClient,
            [NotNull] ReplicaInfo replicaInfo,
            [CanBeNull] ServiceBeaconSettings settings,
            [CanBeNull] ILog log)
        {
            this.zooKeeperClient = zooKeeperClient ?? throw new ArgumentNullException(nameof(settings));
            this.replicaInfo     = replicaInfo = replicaInfo ?? throw new ArgumentNullException(nameof(settings));
            this.settings        = settings ?? new ServiceBeaconSettings();
            this.log             = (log ?? LogProvider.Get()).ForContext <ServiceBeacon>();

            var pathHelper = new ServiceDiscoveryPathHelper(this.settings.ZooKeeperNodesPrefix, this.settings.ZooKeeperNodesPathEscaper);

            environmentNodePath = pathHelper.BuildEnvironmentPath(replicaInfo.Environment);
            applicationNodePath = pathHelper.BuildApplicationPath(replicaInfo.Environment, replicaInfo.Application);
            replicaNodePath     = pathHelper.BuildReplicaPath(replicaInfo.Environment, replicaInfo.Application, replicaInfo.Replica);
            replicaNodeData     = ReplicaNodeDataSerializer.Serialize(replicaInfo);

            nodeWatcher = new AdHocNodeWatcher(OnNodeEvent);
        }
Beispiel #6
0
        public void TryParse_should_parse_replica_path(
            [Values(null, "prefix/node", "/prefix/node", "prefix/node/", "/prefix/node/")]
            string prefix,
            [Values("environment", "EEE/eee")] string environment,
            [Values("application", "AAA/aaa")] string application,
            [Values("replica", "RRR/rrr")] string replica)
        {
            var path = new ServiceDiscoveryPathHelper(prefix, ZooKeeperPathEscaper.Instance);

            path.TryParse(path.BuildEnvironmentPath(environment))
            .Should()
            .Be(((string environment, string application, string replica)?)(environment?.ToLowerInvariant(), null, null));

            path.TryParse(path.BuildApplicationPath(environment, application))
            .Should()
            .Be(((string environment, string application, string replica)?)(environment?.ToLowerInvariant(), application, null));

            path.TryParse(path.BuildReplicaPath(environment, application, replica))
            .Should()
            .Be(((string environment, string application, string replica)?)(environment?.ToLowerInvariant(), application, replica));
        }