From 7afb5a322a0066f4bdac13a7b720a2fecb9d1f82 Mon Sep 17 00:00:00 2001 From: gladysheva Date: Fri, 2 Feb 2024 15:34:30 +0500 Subject: [PATCH] Don't set exists watches if it is not necessary --- .../ApplicationsStorage_Tests.cs | 2 +- .../EnvironmentsStorage_Tests.cs | 2 +- Vostok.ServiceDiscovery/PublicAPI.Shipped.txt | 4 +++- Vostok.ServiceDiscovery/ServiceLocator.cs | 5 ++--- .../ServiceLocatorSettings.cs | 2 ++ .../ApplicationWithReplicas.cs | 14 ++++++++++--- .../ApplicationsStorage.cs | 18 ++++++++++++---- .../EnvironmentsStorage.cs | 21 ++++++++++++++++--- 8 files changed, 52 insertions(+), 16 deletions(-) diff --git a/Vostok.ServiceDiscovery.Tests/ServiceLocatorStorage/ApplicationsStorage_Tests.cs b/Vostok.ServiceDiscovery.Tests/ServiceLocatorStorage/ApplicationsStorage_Tests.cs index 1796312..305dbee 100644 --- a/Vostok.ServiceDiscovery.Tests/ServiceLocatorStorage/ApplicationsStorage_Tests.cs +++ b/Vostok.ServiceDiscovery.Tests/ServiceLocatorStorage/ApplicationsStorage_Tests.cs @@ -237,7 +237,7 @@ private static void ShouldReturnImmediately(ApplicationsStorage storage, string private ApplicationsStorage GetApplicationsStorage() { - return new ApplicationsStorage(ZooKeeperClient, PathHelper, EventsQueue, Log); + return new ApplicationsStorage(ZooKeeperClient, PathHelper, EventsQueue, true, Log); } } } \ No newline at end of file diff --git a/Vostok.ServiceDiscovery.Tests/ServiceLocatorStorage/EnvironmentsStorage_Tests.cs b/Vostok.ServiceDiscovery.Tests/ServiceLocatorStorage/EnvironmentsStorage_Tests.cs index c80d34d..8325f82 100644 --- a/Vostok.ServiceDiscovery.Tests/ServiceLocatorStorage/EnvironmentsStorage_Tests.cs +++ b/Vostok.ServiceDiscovery.Tests/ServiceLocatorStorage/EnvironmentsStorage_Tests.cs @@ -168,7 +168,7 @@ private static void ShouldReturnImmediately(EnvironmentsStorage storage, string private EnvironmentsStorage GetEnvironmentsStorage() { - return new EnvironmentsStorage(ZooKeeperClient, PathHelper, EventsQueue, Log); + return new EnvironmentsStorage(ZooKeeperClient, PathHelper, EventsQueue, true, Log); } } } \ No newline at end of file diff --git a/Vostok.ServiceDiscovery/PublicAPI.Shipped.txt b/Vostok.ServiceDiscovery/PublicAPI.Shipped.txt index 0de8f3f..fb38622 100644 --- a/Vostok.ServiceDiscovery/PublicAPI.Shipped.txt +++ b/Vostok.ServiceDiscovery/PublicAPI.Shipped.txt @@ -99,4 +99,6 @@ Vostok.ServiceDiscovery.ServiceLocatorSettings.ServiceLocatorSettings() -> void Vostok.ServiceDiscovery.ServiceLocatorSettings.ZooKeeperNodesPathEscaper.get -> Vostok.ServiceDiscovery.Helpers.IZooKeeperPathEscaper Vostok.ServiceDiscovery.ServiceLocatorSettings.ZooKeeperNodesPathEscaper.set -> void Vostok.ServiceDiscovery.ServiceLocatorSettings.ZooKeeperNodesPrefix.get -> string -Vostok.ServiceDiscovery.ServiceLocatorSettings.ZooKeeperNodesPrefix.set -> void \ No newline at end of file +Vostok.ServiceDiscovery.ServiceLocatorSettings.ZooKeeperNodesPrefix.set -> void +Vostok.ServiceDiscovery.ServiceLocatorSettings.ObserveNonExistentApplications.get -> bool +Vostok.ServiceDiscovery.ServiceLocatorSettings.ObserveNonExistentApplications.set -> void \ No newline at end of file diff --git a/Vostok.ServiceDiscovery/ServiceLocator.cs b/Vostok.ServiceDiscovery/ServiceLocator.cs index daf58cb..f58436b 100644 --- a/Vostok.ServiceDiscovery/ServiceLocator.cs +++ b/Vostok.ServiceDiscovery/ServiceLocator.cs @@ -44,9 +44,8 @@ public ServiceLocator( pathHelper = new ServiceDiscoveryPathHelper(this.settings.ZooKeeperNodesPrefix, this.settings.ZooKeeperNodesPathEscaper); eventsQueue = new ActionsQueue(this.log); - environmentsStorage = new EnvironmentsStorage(zooKeeperClient, pathHelper, eventsQueue, log); - applicationsStorage = new ApplicationsStorage(zooKeeperClient, pathHelper, eventsQueue, log); - + environmentsStorage = new EnvironmentsStorage(zooKeeperClient, pathHelper, eventsQueue, this.settings.ObserveNonExistentApplications, log); + applicationsStorage = new ApplicationsStorage(zooKeeperClient, pathHelper, eventsQueue, this.settings.ObserveNonExistentApplications, log); } /// diff --git a/Vostok.ServiceDiscovery/ServiceLocatorSettings.cs b/Vostok.ServiceDiscovery/ServiceLocatorSettings.cs index b36936b..566ddd4 100644 --- a/Vostok.ServiceDiscovery/ServiceLocatorSettings.cs +++ b/Vostok.ServiceDiscovery/ServiceLocatorSettings.cs @@ -18,5 +18,7 @@ public class ServiceLocatorSettings public IZooKeeperPathEscaper ZooKeeperNodesPathEscaper { get; set; } = ZooKeeperPathEscaper.Instance; public TimeSpan IterationPeriod { get; set; } = 5.Seconds(); + + public bool ObserveNonExistentApplications { get; set; } = true; } } \ No newline at end of file diff --git a/Vostok.ServiceDiscovery/ServiceLocatorStorage/ApplicationWithReplicas.cs b/Vostok.ServiceDiscovery/ServiceLocatorStorage/ApplicationWithReplicas.cs index 407d03b..2d10db1 100644 --- a/Vostok.ServiceDiscovery/ServiceLocatorStorage/ApplicationWithReplicas.cs +++ b/Vostok.ServiceDiscovery/ServiceLocatorStorage/ApplicationWithReplicas.cs @@ -28,7 +28,9 @@ internal class ApplicationWithReplicas : IDisposable private readonly IZooKeeperClient zooKeeperClient; private readonly ServiceDiscoveryPathHelper pathHelper; private readonly ActionsQueue eventsQueue; + private readonly bool observeNonExistentApplication; private readonly AdHocNodeWatcher nodeWatcher; + private readonly AdHocNodeWatcher existsWatcher; private readonly ILog log; private readonly AtomicBoolean isDisposed = false; @@ -39,6 +41,7 @@ public ApplicationWithReplicas( IZooKeeperClient zooKeeperClient, ServiceDiscoveryPathHelper pathHelper, ActionsQueue eventsQueue, + bool observeNonExistentApplication, ILog log) { this.environmentName = environmentName; @@ -47,21 +50,25 @@ public ApplicationWithReplicas( this.zooKeeperClient = zooKeeperClient; this.pathHelper = pathHelper; this.eventsQueue = eventsQueue; + this.observeNonExistentApplication = observeNonExistentApplication; this.log = log; nodeWatcher = new AdHocNodeWatcher(OnNodeEvent); + existsWatcher = this.observeNonExistentApplication ? nodeWatcher : null; applicationContainer = new VersionedContainer(); replicasContainer = new VersionedContainer(); } - public void Update() + public void Update(out bool appExists) { + appExists = true; + if (isDisposed) return; try { - var applicationExists = zooKeeperClient.Exists(new ExistsRequest(applicationNodePath) {Watcher = nodeWatcher}); + var applicationExists = zooKeeperClient.Exists(new ExistsRequest(applicationNodePath) {Watcher = existsWatcher}); if (!applicationExists.IsSuccessful) { return; @@ -69,6 +76,7 @@ public void Update() if (applicationExists.Stat == null) { + appExists = false; Clear(); return; } @@ -115,7 +123,7 @@ private void OnNodeEvent(NodeChangedEventType type, string path) if (isDisposed) return; - eventsQueue.Enqueue(Update); + eventsQueue.Enqueue(() => Update(out _)); } private void Clear() diff --git a/Vostok.ServiceDiscovery/ServiceLocatorStorage/ApplicationsStorage.cs b/Vostok.ServiceDiscovery/ServiceLocatorStorage/ApplicationsStorage.cs index 633945c..dad2be7 100644 --- a/Vostok.ServiceDiscovery/ServiceLocatorStorage/ApplicationsStorage.cs +++ b/Vostok.ServiceDiscovery/ServiceLocatorStorage/ApplicationsStorage.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Threading; using Vostok.Commons.Threading; @@ -16,14 +17,21 @@ internal class ApplicationsStorage : IDisposable private readonly IZooKeeperClient zooKeeperClient; private readonly ServiceDiscoveryPathHelper pathHelper; private readonly ActionsQueue eventsQueue; + private readonly bool observeNonExistentApplications; private readonly ILog log; private readonly AtomicBoolean isDisposed = false; - public ApplicationsStorage(IZooKeeperClient zooKeeperClient, ServiceDiscoveryPathHelper pathHelper, ActionsQueue eventsQueue, ILog log) + public ApplicationsStorage( + IZooKeeperClient zooKeeperClient, + ServiceDiscoveryPathHelper pathHelper, + ActionsQueue eventsQueue, + bool observeNonExistentApplications, + ILog log) { this.zooKeeperClient = zooKeeperClient; this.pathHelper = pathHelper; this.eventsQueue = eventsQueue; + this.observeNonExistentApplications = observeNonExistentApplications; this.log = log; } @@ -42,7 +50,9 @@ public void UpdateAll() if (isDisposed) return; - kvp.Value.Value.Update(); + kvp.Value.Value.Update(out var appExists); + if (!appExists && !observeNonExistentApplications) + applications.TryRemove(kvp.Key, out _); } } @@ -64,9 +74,9 @@ private ApplicationWithReplicas CreateAndGet(string environment, string applicat lazy = new Lazy( () => { - var container = new ApplicationWithReplicas(environment, application, pathHelper.BuildApplicationPath(environment, application), zooKeeperClient, pathHelper, eventsQueue, log); + var container = new ApplicationWithReplicas(environment, application, pathHelper.BuildApplicationPath(environment, application), zooKeeperClient, pathHelper, eventsQueue, observeNonExistentApplications, log); if (!isDisposed) - container.Update(); + container.Update(out _); return container; }, LazyThreadSafetyMode.ExecutionAndPublication); diff --git a/Vostok.ServiceDiscovery/ServiceLocatorStorage/EnvironmentsStorage.cs b/Vostok.ServiceDiscovery/ServiceLocatorStorage/EnvironmentsStorage.cs index 7d6c2fd..c68e113 100644 --- a/Vostok.ServiceDiscovery/ServiceLocatorStorage/EnvironmentsStorage.cs +++ b/Vostok.ServiceDiscovery/ServiceLocatorStorage/EnvironmentsStorage.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Threading; using Vostok.Commons.Threading; @@ -20,17 +21,26 @@ private readonly ConcurrentDictionary container) try { var environmentPath = pathHelper.BuildEnvironmentPath(name); - - var environmentExists = zooKeeperClient.Exists(new ExistsRequest(environmentPath) {Watcher = nodeWatcher}); + var environmentExists = zooKeeperClient.Exists(new ExistsRequest(environmentPath) {Watcher = existsWatcher}); if (!environmentExists.IsSuccessful) return; if (environmentExists.Stat == null) { + if (!observeNonExistentApplication) + { + environments.TryRemove(name, out _); + return; + } + container.Clear(); } else