Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes 3175 - adds discovery status messaging to ps #3194

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion cli/cli/Commands/Project/CheckStatusCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class ServiceDiscoveryEvent
public bool isContainer;
public int healthPort;
public string containerId;
public string status;
}

public class CheckStatusCommand : StreamCommand<CheckStatusCommandArgs, ServiceDiscoveryEvent>
Expand Down Expand Up @@ -59,7 +60,26 @@ await foreach (var evt in discovery.StartDiscovery(timeout))
}
else
{
Log.Information($"{evt.service} is available prefix=[{evt.prefix}] docker=[{evt.isContainer}]");
if (DiscoveryStatusUtil.TryGetStatusFromString(evt.status, out var status))
{
switch (status)
{
case DiscoveryStatus.Starting:
Log.Information($"{evt.service} is starting at prefix=[{evt.prefix}] docker=[{evt.isContainer}]");
break;
case DiscoveryStatus.Stopping:
Log.Information($"{evt.service} is stopping at prefix=[{evt.prefix}] docker=[{evt.isContainer}]");
break;
case DiscoveryStatus.AcceptingTraffic:
Log.Information($"{evt.service} is accepting traffic at prefix=[{evt.prefix}] docker=[{evt.isContainer}]");
break;
}
}
else
{
Log.Information($"{evt.service} is broadcasing at prefix=[{evt.prefix}] docker=[{evt.isContainer}]");
}

}
SendResults(evt);
}
Expand Down
11 changes: 9 additions & 2 deletions cli/cli/Services/DiscoveryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,14 @@ public async IAsyncEnumerable<ServiceDiscoveryEvent> StartDiscovery(TimeSpan tim
continue;
}

if (!nameToEntryWithTimestamp.ContainsKey(service.serviceName))
if (!nameToEntryWithTimestamp.TryGetValue(service.serviceName, out var existing))
{
// if it doesn't exist, enter it.
evtQueue.Enqueue(CreateEvent(service, true));

} else if (existing.Item2.status != service.status)
{
// or if the status has changed.
evtQueue.Enqueue(CreateEvent(service, true));
}

Expand Down Expand Up @@ -152,7 +158,8 @@ public static ServiceDiscoveryEvent CreateEvent(ServiceDiscoveryEntry entry, boo
isRunning = isRunning,
isContainer = entry.isContainer,
containerId = entry.containerId,
healthPort = entry.healthPort
healthPort = entry.healthPort,
status = entry.status.GetDisplayString()
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,45 @@ public class ServiceDiscoveryEntry
public int healthPort;
public bool isContainer;
public string containerId;
public DiscoveryStatus status;
}

public enum DiscoveryStatus
{
Starting,
AcceptingTraffic,
Stopping
}

public static class DiscoveryStatusUtil
{
public static string GetDisplayString(this DiscoveryStatus status)
{
switch (status)
{
case DiscoveryStatus.Starting: return "starting";
case DiscoveryStatus.Stopping: return "stopping";
case DiscoveryStatus.AcceptingTraffic: return "accepting-traffic";
default: throw new ArgumentException();
}
}
public static bool TryGetStatusFromString(string statusString, out DiscoveryStatus status)
{
switch (statusString)
{
case "starting":
status = DiscoveryStatus.Starting;
return true;
case "stopping":
status = DiscoveryStatus.Stopping;
return true;
case "accepting-traffic":
status = DiscoveryStatus.AcceptingTraffic;
return true;
default:
status = DiscoveryStatus.Starting;
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Beamable.Common.Reflection;
using Beamable.Server.Api.Usage;
using microservice.Common;
using microservice.dbmicroservice;
using Newtonsoft.Json;
using Serilog;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -195,6 +196,8 @@ public async Task Start<TMicroService>(IMicroserviceArgs args)
}

await SetupWebsocket(socket, _serviceAttribute.EnableEagerContentLoading);

Provider.GetService<IDiscoveryService>().SetStatus(DiscoveryStatus.AcceptingTraffic);
if (!_serviceAttribute.EnableEagerContentLoading)
{
var _ = contentService.Init();
Expand Down Expand Up @@ -228,6 +231,7 @@ public async Task OnShutdown(object sender, EventArgs args)

// need to wait for all tasks to complete...
Log.Debug("Shutdown started... {runningTaskCount} tasks running.", _runningTaskTable.Count);
Provider.GetService<IDiscoveryService>().SetStatus(DiscoveryStatus.Stopping);

var sw = new System.Diagnostics.Stopwatch();
sw.Start();
Expand Down
68 changes: 68 additions & 0 deletions microservice/microservice/dbmicroservice/DiscoveryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Beamable.Common;
using Beamable.Common.Dependencies;
using Beamable.Server;
using Beamable.Server.Common;
using NetMQ;
using Newtonsoft.Json;
using Serilog;
using System;

namespace microservice.dbmicroservice;

public interface IDiscoveryService
{
void SetStatus(DiscoveryStatus status);
}

public class NoDiscoveryService : IDiscoveryService
{
public void SetStatus(DiscoveryStatus _)
{
// no-op
}
}

public static class DiscoveryExtensions
{
public static IDiscoveryService GetDiscoveryService(this IDependencyProvider provider)
{
return provider.GetService<IDiscoveryService>();
}
}


public class DiscoveryService : IDiscoveryService
{
private readonly NetMQBeacon _beacon;
private readonly int _port;
private readonly ServiceDiscoveryEntry _heartBeatMsg;

public DiscoveryService(IMicroserviceArgs args, MicroserviceAttribute attribute)
{
_beacon = new NetMQBeacon();
_port = Constants.Features.Services.DISCOVERY_PORT;
_beacon.Configure(_port);
_heartBeatMsg = new ServiceDiscoveryEntry
{
cid = args.CustomerID,
pid = args.ProjectName,
prefix = args.NamePrefix,
serviceName = attribute.MicroserviceName,
healthPort = args.HealthPort,
status = DiscoveryStatus.Starting
};
}


public void SetStatus(DiscoveryStatus status)
{
_heartBeatMsg.status = status;
_beacon.Silence();

var json = HeartBeatMessageJson;
Log.Verbose("setting discovery json : " + json);
_beacon.Publish(json, TimeSpan.FromMilliseconds(Constants.Features.Services.DISCOVERY_BROADCAST_PERIOD_MS));
}

string HeartBeatMessageJson => JsonConvert.SerializeObject(_heartBeatMsg, UnitySerializationSettings.Instance);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static class MicroserviceBootstrapper
public static List<BeamableMicroService> Instances = new List<BeamableMicroService>();

public static IUsageApi EcsService;
private static IDiscoveryService _discovery;

private static DebugLogSink ConfigureLogging(IMicroserviceArgs args, MicroserviceAttribute attr)
{
Expand Down Expand Up @@ -220,6 +221,7 @@ public static ReflectionCache ConfigureReflectionCache()
.AddSingleton(attribute)
.AddSingleton<IBeamSchedulerContext, SchedulerContext>()
.AddSingleton<BeamScheduler>()
.AddSingleton<IDiscoveryService>(_discovery)
.AddSingleton<IUsageApi>(EcsService)
.AddScoped<IDependencyProvider>(provider => new MicrosoftServiceProviderWrapper(provider))
.AddScoped<IRealmInfo>(provider => provider.GetService<IMicroserviceArgs>())
Expand Down Expand Up @@ -390,28 +392,18 @@ public static void InitializeServices(IServiceProvider provider)
mongo.Init();
}

public static void ConfigureDiscovery(IMicroserviceArgs args, MicroserviceAttribute attribute)
public static IDiscoveryService ConfigureDiscovery(IMicroserviceArgs args, MicroserviceAttribute attribute)
{
var inDocker = Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true";

if (inDocker)
{
return;
return new NoDiscoveryService();
}
var beacon = new NetMQBeacon();
var port = Constants.Features.Services.DISCOVERY_PORT;
beacon.Configure(port);

var msg = new ServiceDiscoveryEntry
{
cid = args.CustomerID,
pid = args.ProjectName,
prefix = args.NamePrefix,
serviceName = attribute.MicroserviceName,
healthPort = args.HealthPort,
};
var msgJson = JsonConvert.SerializeObject(msg, UnitySerializationSettings.Instance);
beacon.Publish(msgJson, TimeSpan.FromMilliseconds(Constants.Features.Services.DISCOVERY_BROADCAST_PERIOD_MS));

var service = new DiscoveryService(args, attribute);
service.SetStatus(DiscoveryStatus.Starting);
return service;
}

public static async Task<string> ConfigureCid(IMicroserviceArgs args)
Expand Down Expand Up @@ -560,7 +552,7 @@ public static bool TryFindBeamableFolder(out string beamableFolderPath)
var pipeSink = ConfigureLogging(envArgs, attribute);
ConfigureUncaughtExceptions();
ConfigureUnhandledError();
ConfigureDiscovery(envArgs, attribute);
_discovery = ConfigureDiscovery(envArgs, attribute);
await ConfigureUsageService(envArgs);
ReflectionCache = ConfigureReflectionCache();

Expand Down