public void NeedUpdate_should_be_false_for_smaller_or_equal_versions()
        {
            var container = new VersionedContainer <string>();

            container.Update(2, "x").Should().BeTrue();

            container.NeedUpdate(1).Should().BeFalse();
            container.NeedUpdate(2).Should().BeFalse();
            container.NeedUpdate(3).Should().BeTrue();
        }
        public void Update_should_work_sequentially()
        {
            var container = new VersionedContainer <string>();

            for (var i = 0; i < 10; i++)
            {
                // ReSharper disable once AccessToModifiedClosure
                container.Update(i, i.ToString()).Should().BeTrue();
                container.Value.Should().Be(i.ToString());
            }
        }
Exemplo n.º 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);
            }
        }
        public void Clear_should_reset_value_and_version()
        {
            var container = new VersionedContainer <string>();

            container.Update(10, "x").Should().BeTrue();
            container.Value.Should().Be("x");

            container.Clear();
            container.Value.Should().BeNull();

            container.Update(1, "y").Should().BeTrue();
            container.Value.Should().Be("y");
        }
        public void Update_should_has_max_version_after_concurrent_updates()
        {
            var container = new VersionedContainer <string>();

            var random  = new Random(314);
            var updates = Enumerable.Range(0, 10000).Select(i => random.Next()).ToList();

            Parallel.ForEach(
                updates,
                new ParallelOptions {
                MaxDegreeOfParallelism = 10
            },
                u => { container.Update(u, u.ToString()); });

            container.Value.Should().Be(updates.Max().ToString());
        }
Exemplo n.º 6
0
        public EnvironmentInfo Get(string name)
        {
            if (environments.TryGetValue(name, out var lazy))
            {
                return(lazy.Value.Value);
            }

            lazy = new Lazy <VersionedContainer <EnvironmentInfo> >(
                () =>
            {
                var container = new VersionedContainer <EnvironmentInfo>();
                Update(name, container);
                return(container);
            },
                LazyThreadSafetyMode.ExecutionAndPublication);

            return(environments.GetOrAdd(name, _ => lazy).Value.Value);
        }
        public ApplicationWithReplicas(
            string environmentName,
            string applicationName,
            string applicationNodePath,
            IZooKeeperClient zooKeeperClient,
            ServiceDiscoveryPathHelper pathHelper,
            ActionsQueue eventsQueue,
            ILog log)
        {
            this.environmentName     = environmentName;
            this.applicationName     = applicationName;
            this.applicationNodePath = applicationNodePath;
            this.zooKeeperClient     = zooKeeperClient;
            this.pathHelper          = pathHelper;
            this.eventsQueue         = eventsQueue;
            this.log = log;

            nodeWatcher          = new AdHocNodeWatcher(OnNodeEvent);
            applicationContainer = new VersionedContainer <ApplicationInfo>();
            replicasContainer    = new VersionedContainer <Uri[]>();
        }