Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
Removes Dapr placement server management (#1191)
Browse files Browse the repository at this point in the history
* Try inverting default placement service behavior.

* Eliminate placement service management.

* Remove references to placement service options.

* Try to determine Dapr status.

* Update formatting.
  • Loading branch information
philliphoff authored Oct 5, 2021
1 parent 0ce940d commit 565b7d3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 75 deletions.
19 changes: 6 additions & 13 deletions samples/dapr/pub-sub/tye.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ name: dapr
extensions:
- name: dapr

# If you want to use a different tag or container port
# placement-image: daprio/dapr
# placement-container-port: 50005

# log-level configures the log level of the dapr sidecar
log-level: debug

Expand All @@ -24,11 +20,8 @@ extensions:
# components-path configures the components path of the dapr sidecar
components-path: "./components/"

# You can instruct Tye to not create the Dapr placement container on your behalf. This is required if you have Dapr running and want to use that container.
# Doing a `docker ps` can show if its already running. If it's running then you can set 'exclude-placement-container: true' with `placement-port: xxxx` set to the host port of that container.
# (i.e. In Windows + WSL2, Dapr uses 6050 as the host port)

# exclude-placement-container: true
# If not using the default Dapr placement service or otherwise using a placement service on a nonstandard port,
# you can configure the Dapr sidecar to use an explicit port.
# placement-port: 6050
services:
- name: orders
Expand All @@ -42,7 +35,7 @@ services:
#
# Doing a `docker ps` can show if its already running. If that's the case
# then comment out out when running locally.
- name: redis
image: redis
bindings:
- port: 6379
# - name: redis
# image: redis
# bindings:
# - port: 6379
11 changes: 2 additions & 9 deletions samples/dapr/service-invocation/tye.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ name: distributedtyedemo
extensions:
- name: dapr

# If you want to use a different tag or container port
# placement-image: daprio/dapr
# placement-container-port: 50005

# log-level configures the log level of the dapr sidecar
log-level: debug

Expand All @@ -24,11 +20,8 @@ extensions:
# components-path configures the components path of the dapr sidecard
# components-path: "./components/"

# You can instruct Tye to not create the Dapr placement container on your behalf. This is required if you have Dapr running and want to use that container.
# Doing a `docker ps` can show if its already running. If it's running then you can set 'exclude-placement-container: true' with `placement-port: xxxx` set to the host port of that container.
# (i.e. In Windows + WSL2, Dapr uses 6050 as the host port)

# exclude-placement-container: true
# If not using the default Dapr placement service or otherwise using a placement service on a nonstandard port,
# you can configure the Dapr sidecar to use an explicit port.
# placement-port: 6050
services:
# uppercase service is a node app and is run via a dockerfile
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.Tye.Core/ProcessExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Microsoft.Tye
{
internal static class ProcessExtensions
public static class ProcessExtensions
{
private static readonly bool _isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
private static readonly TimeSpan _defaultTimeout = TimeSpan.FromSeconds(30);
Expand Down Expand Up @@ -97,7 +97,7 @@ private static void KillProcessUnix(int processId, TimeSpan timeout)
}
}

private static void RunProcessAndWaitForExit(string fileName, string arguments, TimeSpan timeout, out string? stdout)
public static void RunProcessAndWaitForExit(string fileName, string arguments, TimeSpan timeout, out string? stdout)
{
var startInfo = new ProcessStartInfo
{
Expand Down
98 changes: 47 additions & 51 deletions src/Microsoft.Tye.Extensions/Dapr/DaprExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,29 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Microsoft.Tye.Extensions.Dapr
{
internal sealed class DaprExtension : Extension
{
public override Task ProcessAsync(ExtensionContext context, ExtensionConfiguration config)
public override async Task ProcessAsync(ExtensionContext context, ExtensionConfiguration config)
{
// If we're getting called then the user configured dapr in their tye.yaml.
// We don't have any of our own config.

if (context.Operation == ExtensionContext.OperationKind.LocalRun)
{
// default placement port number
var daprPlacementImage = "daprio/dapr";
var daprPlacementContainerPort = 50005;
var daprPlacementPort = NextPortFinder.GetNextPort();
var isCustomPlacementPortDefined = false;
await VerifyDaprInitialized(context);

int? daprPlacementPort = null;

// see if a placement port number has been defined
if (config.Data.TryGetValue("placement-port", out var obj) && obj?.ToString() is string && int.TryParse(obj.ToString(), out var customPlacementPort))
{
context.Output.WriteDebugLine($"Using Dapr placement service host port {customPlacementPort} from 'placement-port'");
daprPlacementPort = customPlacementPort;
isCustomPlacementPortDefined = true;
}

// see if a placement image has been defined
if (config.Data.TryGetValue("placement-image", out obj) && obj?.ToString() is string customPlacementImage)
{
context.Output.WriteDebugLine($"Using Dapr placement service image {customPlacementImage} from 'placement-image'");
daprPlacementImage = customPlacementImage;
}

// see if a placement container port has been defined
if (config.Data.TryGetValue("placement-container-port", out obj) && obj?.ToString() is string && int.TryParse(obj.ToString(), out var customPlacementContainerPort))
{
context.Output.WriteDebugLine($"Using Dapr placement service container port {customPlacementContainerPort} from 'placement-container-port'");
daprPlacementContainerPort = customPlacementContainerPort;
}

// We can only skip injecting a Dapr placement container if a 'placement-port' has been defined and 'exclude-placement-container=true'
if (!(isCustomPlacementPortDefined && config.Data.TryGetValue("exclude-placement-container", out obj) &&
obj?.ToString() is string excludePlacementContainer && excludePlacementContainer == "true"))
{
if (!isCustomPlacementPortDefined)
{
context.Output.WriteDebugLine("A 'placement-port' has not been defined. So the 'exclude-placement-container' will default to 'false'.");
}

context.Output.WriteDebugLine("Injecting Dapr placement service...");
var daprPlacement = new ContainerServiceBuilder("placement", daprPlacementImage, ServiceSource.Extension)
{
Args = "./placement",
Bindings = {
new BindingBuilder() {
Port = daprPlacementPort,
ContainerPort = daprPlacementContainerPort,
Protocol = "http"
}
}
};
context.Application.Services.Add(daprPlacement);
}
else
{
context.Output.WriteDebugLine("Skipping injecting Dapr placement service because 'exclude-placement-container=true'.");
}

// For local run, enumerate all projects, and add services for each dapr proxy.
Expand Down Expand Up @@ -110,9 +66,14 @@ public override Task ProcessAsync(ExtensionContext context, ExtensionConfigurati

// These environment variables are replaced with environment variables
// defined for this service.
Args = $"run --app-id {project.Name} --app-port %APP_PORT% --dapr-grpc-port %DAPR_GRPC_PORT% --dapr-http-port %DAPR_HTTP_PORT% --metrics-port %METRICS_PORT% --placement-host-address localhost:{daprPlacementPort}",
Args = $"run --app-id {project.Name} --app-port %APP_PORT% --dapr-grpc-port %DAPR_GRPC_PORT% --dapr-http-port %DAPR_HTTP_PORT% --metrics-port %METRICS_PORT%",
};

if (daprPlacementPort.HasValue)
{
proxy.Args += $" --placement-host-address localhost:{daprPlacementPort.Value}";
}

// When running locally `-config` specifies a filename, not a configuration name. By convention
// we'll assume the filename and config name are the same.
if (config.Data.TryGetValue("config", out obj) && obj?.ToString() is string daprConfig)
Expand Down Expand Up @@ -265,8 +226,43 @@ public override Task ProcessAsync(ExtensionContext context, ExtensionConfigurati
}
}
}
}

private static Task VerifyDaprInitialized(ExtensionContext context)
{
return Task.Run(
() =>
{
string? stdout = null;
try
{
ProcessExtensions.RunProcessAndWaitForExit("dapr", "--version", TimeSpan.FromSeconds(10), out stdout);
}
catch
{
}
if (stdout != null)
{
var match = Regex.Match(stdout, "^Runtime version: (?<version>.+)$", RegexOptions.Multiline);
if (match.Success)
{
if (match.Groups["version"].Value == "n/a")
{
throw new CommandException("Dapr has not been initialized (e.g. via `dapr init`).");
}
else
{
// Some version of Dapr has been initialized...
return;
}
}
}
return Task.CompletedTask;
context.Output.WriteAlwaysLine($"Unable to determine whether Dapr has been installed and initialized (e.g. via `dapr init`).");
});
}

private string GetDaprExecutablePath()
Expand Down

0 comments on commit 565b7d3

Please sign in to comment.