Skip to content

Commit

Permalink
Don't set exists watches if it is not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
gladysheva committed Feb 2, 2024
1 parent c0b01c7 commit 7afb5a3
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
4 changes: 3 additions & 1 deletion Vostok.ServiceDiscovery/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Vostok.ServiceDiscovery.ServiceLocatorSettings.ZooKeeperNodesPrefix.set -> void
Vostok.ServiceDiscovery.ServiceLocatorSettings.ObserveNonExistentApplications.get -> bool
Vostok.ServiceDiscovery.ServiceLocatorSettings.ObserveNonExistentApplications.set -> void
5 changes: 2 additions & 3 deletions Vostok.ServiceDiscovery/ServiceLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <inheritdoc />
Expand Down
2 changes: 2 additions & 0 deletions Vostok.ServiceDiscovery/ServiceLocatorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,6 +41,7 @@ public ApplicationWithReplicas(
IZooKeeperClient zooKeeperClient,
ServiceDiscoveryPathHelper pathHelper,
ActionsQueue eventsQueue,
bool observeNonExistentApplication,
ILog log)
{
this.environmentName = environmentName;
Expand All @@ -47,28 +50,33 @@ 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<ApplicationInfo>();
replicasContainer = new VersionedContainer<Uri[]>();
}

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;
}

if (applicationExists.Stat == null)
{
appExists = false;
Clear();
return;
}
Expand Down Expand Up @@ -115,7 +123,7 @@ private void OnNodeEvent(NodeChangedEventType type, string path)
if (isDisposed)
return;

eventsQueue.Enqueue(Update);
eventsQueue.Enqueue(() => Update(out _));
}

private void Clear()
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}

Expand All @@ -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 _);
}
}

Expand All @@ -64,9 +74,9 @@ private ApplicationWithReplicas CreateAndGet(string environment, string applicat
lazy = new Lazy<ApplicationWithReplicas>(
() =>
{
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,17 +21,26 @@ private readonly ConcurrentDictionary<string, Lazy<VersionedContainer<Environmen
private readonly IZooKeeperClient zooKeeperClient;
private readonly ServiceDiscoveryPathHelper pathHelper;
private readonly ActionsQueue eventsHandler;
private readonly bool observeNonExistentApplication;
private readonly ILog log;
private readonly AdHocNodeWatcher nodeWatcher;
private readonly AdHocNodeWatcher existsWatcher;
private readonly AtomicBoolean isDisposed = new AtomicBoolean(false);

public EnvironmentsStorage(IZooKeeperClient zooKeeperClient, ServiceDiscoveryPathHelper pathHelper, ActionsQueue eventsHandler, ILog log)
public EnvironmentsStorage(
IZooKeeperClient zooKeeperClient,
ServiceDiscoveryPathHelper pathHelper,
ActionsQueue eventsHandler,
bool observeNonExistentApplication,
ILog log)
{
this.zooKeeperClient = zooKeeperClient;
this.pathHelper = pathHelper;
this.eventsHandler = eventsHandler;
this.observeNonExistentApplication = observeNonExistentApplication;
this.log = log;
nodeWatcher = new AdHocNodeWatcher(OnNodeEvent);
existsWatcher = this.observeNonExistentApplication ? nodeWatcher : null;
}

public EnvironmentInfo Get(string name)
Expand Down Expand Up @@ -92,13 +102,18 @@ private void Update(string name, VersionedContainer<EnvironmentInfo> 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
Expand Down

0 comments on commit 7afb5a3

Please sign in to comment.