Skip to content

Commit

Permalink
Remove all global state in from setProperty
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Souza <asouza.pro@gmail.com>
  • Loading branch information
artursouza committed Oct 1, 2024
1 parent cedaebc commit 1d0cd03
Show file tree
Hide file tree
Showing 36 changed files with 229 additions and 225 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ jobs:
GOARCH: amd64
GOPROXY: https://proxy.golang.org
JDK_VER: ${{ matrix.java }}
DAPR_CLI_VER: 1.14.0-rc.6
DAPR_RUNTIME_VER: 1.14.0-rc.6
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.14.0-rc.4/install/install.sh
DAPR_CLI_VER: 1.14.0
DAPR_RUNTIME_VER: 1.14.4
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.14.0/install/install.sh
DAPR_CLI_REF:
DAPR_REF:
TOXIPROXY_URL: https://github.com/Shopify/toxiproxy/releases/download/v2.5.0/toxiproxy-server-linux-amd64
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ jobs:
GOARCH: amd64
GOPROXY: https://proxy.golang.org
JDK_VER: ${{ matrix.java }}
DAPR_CLI_VER: 1.14.0-rc.6
DAPR_RUNTIME_VER: 1.14.0-rc.6
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.14.0-rc.3/install/install.sh
DAPR_CLI_VER: 1.14.0
DAPR_RUNTIME_VER: 1.14.4
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.14.0/install/install.sh
DAPR_CLI_REF:
DAPR_REF:
steps:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@ DaprConnectionDetails daprConnectionDetails(DaprClientProperties properties) {
@ConditionalOnMissingBean
DaprClientBuilder daprClientBuilder(DaprConnectionDetails daprConnectionDetails) {
DaprClientBuilder builder = new DaprClientBuilder();
builder.withPropertyOverride(Properties.HTTP_ENDPOINT, daprConnectionDetails.httpEndpoint());
builder.withPropertyOverride(Properties.GRPC_ENDPOINT, daprConnectionDetails.grpcEndpoint());
builder.withPropertyOverride(Properties.HTTP_PORT, String.valueOf(daprConnectionDetails.httpPort()));
builder.withPropertyOverride(Properties.GRPC_PORT, String.valueOf(daprConnectionDetails.grpcPort()));
if (daprConnectionDetails.httpEndpoint() != null) {
builder.withPropertyOverride(Properties.HTTP_ENDPOINT, daprConnectionDetails.httpEndpoint());
}
if (daprConnectionDetails.grpcEndpoint() != null) {
builder.withPropertyOverride(Properties.GRPC_ENDPOINT, daprConnectionDetails.grpcEndpoint());
}
if (daprConnectionDetails.httpPort() != null) {
builder.withPropertyOverride(Properties.HTTP_PORT, String.valueOf(daprConnectionDetails.httpPort()));
}
if (daprConnectionDetails.grpcPort() != null) {
builder.withPropertyOverride(Properties.GRPC_PORT, String.valueOf(daprConnectionDetails.grpcPort()));
}
return builder;
}

Expand Down
38 changes: 28 additions & 10 deletions sdk-actors/src/main/java/io/dapr/actors/client/ActorClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@
import io.grpc.Channel;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;

/**
* Holds a client for Dapr sidecar communication. ActorClient should be reused.
*/
public class ActorClient implements AutoCloseable {

private static final Logger LOGGER = LoggerFactory.getLogger(ActorClient.class);

/**
* gRPC channel for communication with Dapr sidecar.
*/
Expand All @@ -45,16 +41,35 @@ public class ActorClient implements AutoCloseable {
* Instantiates a new channel for Dapr sidecar communication.
*/
public ActorClient() {
this(null);
this(new Properties(), null);
}

/**
* Instantiates a new channel for Dapr sidecar communication.
*
* @param resiliencyOptions Client resiliency options.
*/
private ActorClient(ResiliencyOptions resiliencyOptions) {
this(buildManagedChannel(), resiliencyOptions);
public ActorClient(ResiliencyOptions resiliencyOptions) {
this(new Properties(), resiliencyOptions);
}

/**
* Instantiates a new channel for Dapr sidecar communication.
*
* @param overrideProperties Override properties.
*/
public ActorClient(Properties overrideProperties) {
this(buildManagedChannel(overrideProperties), null);
}

/**
* Instantiates a new channel for Dapr sidecar communication.
*
* @param overrideProperties Override properties.
* @param resiliencyOptions Client resiliency options.
*/
public ActorClient(Properties overrideProperties, ResiliencyOptions resiliencyOptions) {
this(buildManagedChannel(overrideProperties), resiliencyOptions);
}

/**
Expand Down Expand Up @@ -96,15 +111,18 @@ public void close() {
/**
* Creates a GRPC managed channel (or null, if not applicable).
*
* @param overrideProperties Overrides
* @return GRPC managed channel or null.
*/
private static ManagedChannel buildManagedChannel() {
int port = Properties.GRPC_PORT.get();
private static ManagedChannel buildManagedChannel(Properties overrideProperties) {
int port = overrideProperties.getValue(Properties.GRPC_PORT);
if (port <= 0) {
throw new IllegalArgumentException("Invalid port.");
}

return ManagedChannelBuilder.forAddress(Properties.SIDECAR_IP.get(), port)
var sidecarHost = overrideProperties.getValue(Properties.SIDECAR_IP);

return ManagedChannelBuilder.forAddress(sidecarHost, port)
.usePlaintext()
.userAgent(Version.getSdkVersion())
.build();
Expand Down
46 changes: 5 additions & 41 deletions sdk-tests/src/test/java/io/dapr/it/BaseIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import io.dapr.actors.client.ActorClient;
import io.dapr.client.resiliency.ResiliencyOptions;
import io.dapr.config.Properties;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.junit.jupiter.api.AfterAll;

Expand Down Expand Up @@ -46,7 +47,7 @@ protected static DaprRun startDaprApp(
Class serviceClass,
Boolean useAppPort,
int maxWaitMilliseconds) throws Exception {
return startDaprApp(testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, GRPC);
return startDaprApp(testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, HTTP);
}

protected static DaprRun startDaprApp(
Expand All @@ -55,7 +56,7 @@ protected static DaprRun startDaprApp(
Class serviceClass,
AppRun.AppProtocol appProtocol,
int maxWaitMilliseconds) throws Exception {
return startDaprApp(testName, successMessage, serviceClass, true, maxWaitMilliseconds, GRPC, appProtocol);
return startDaprApp(testName, successMessage, serviceClass, true, maxWaitMilliseconds, appProtocol);
}

protected static DaprRun startDaprApp(
Expand All @@ -64,25 +65,6 @@ protected static DaprRun startDaprApp(
Class serviceClass,
Boolean useAppPort,
int maxWaitMilliseconds,
AppRun.AppProtocol protocol) throws Exception {
return startDaprApp(
testName,
successMessage,
serviceClass,
useAppPort,
true,
maxWaitMilliseconds,
protocol,
HTTP);
}

protected static DaprRun startDaprApp(
String testName,
String successMessage,
Class serviceClass,
Boolean useAppPort,
int maxWaitMilliseconds,
AppRun.AppProtocol protocol,
AppRun.AppProtocol appProtocol) throws Exception {
return startDaprApp(
testName,
Expand All @@ -91,7 +73,6 @@ protected static DaprRun startDaprApp(
useAppPort,
true,
maxWaitMilliseconds,
protocol,
appProtocol);
}

Expand All @@ -105,7 +86,6 @@ protected static DaprRun startDaprApp(
false,
true,
maxWaitMilliseconds,
GRPC,
HTTP);
}

Expand All @@ -116,7 +96,6 @@ protected static DaprRun startDaprApp(
Boolean useAppPort,
Boolean useDaprPorts,
int maxWaitMilliseconds,
AppRun.AppProtocol protocol,
AppRun.AppProtocol appProtocol) throws Exception {
DaprRun.Builder builder = new DaprRun.Builder(
testName,
Expand All @@ -128,7 +107,6 @@ protected static DaprRun startDaprApp(
TO_BE_STOPPED.add(run);
DAPR_RUN_BUILDERS.put(run.getAppName(), builder);
run.start();
run.use();
return run;
}

Expand All @@ -138,24 +116,12 @@ protected static ImmutablePair<AppRun, DaprRun> startSplitDaprAndApp(
Class serviceClass,
Boolean useAppPort,
int maxWaitMilliseconds) throws Exception {
return startSplitDaprAndApp(
testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, AppRun.AppProtocol.GRPC);
}

protected static ImmutablePair<AppRun, DaprRun> startSplitDaprAndApp(
String testName,
String successMessage,
Class serviceClass,
Boolean useAppPort,
int maxWaitMilliseconds,
AppRun.AppProtocol protocol) throws Exception {
return startSplitDaprAndApp(
testName,
successMessage,
serviceClass,
useAppPort,
maxWaitMilliseconds,
protocol,
HTTP);
}

Expand All @@ -165,7 +131,6 @@ protected static ImmutablePair<AppRun, DaprRun> startSplitDaprAndApp(
Class serviceClass,
Boolean useAppPort,
int maxWaitMilliseconds,
AppRun.AppProtocol protocol,
AppRun.AppProtocol appProtocol) throws Exception {
DaprRun.Builder builder = new DaprRun.Builder(
testName,
Expand All @@ -179,7 +144,6 @@ protected static ImmutablePair<AppRun, DaprRun> startSplitDaprAndApp(
DAPR_RUN_BUILDERS.put(runs.right.getAppName(), builder);
runs.left.start();
runs.right.start();
runs.right.use();
return runs;
}

Expand All @@ -194,8 +158,8 @@ public static void cleanUp() throws Exception {
}
}

protected static ActorClient newActorClient() {
return newActorClient(null);
protected static ActorClient newActorClient(Properties properties) {
return new ActorClient(properties, null);
}

protected static ActorClient newActorClient(ResiliencyOptions resiliencyOptions) throws RuntimeException {
Expand Down
30 changes: 16 additions & 14 deletions sdk-tests/src/test/java/io/dapr/it/DaprPorts.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
package io.dapr.it;

import io.dapr.config.Properties;
import io.dapr.config.Property;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DaprPorts {
Expand All @@ -30,10 +34,18 @@ public class DaprPorts {

private final Integer appPort;

private final Map<Property<?>, String> overrides;

private DaprPorts(Integer appPort, Integer httpPort, Integer grpcPort) {
this.grpcPort = grpcPort;
this.httpPort = httpPort;
this.appPort = appPort;
this.overrides = Collections.unmodifiableMap(new HashMap<>(){{
put(Properties.GRPC_PORT, grpcPort.toString());
put(Properties.HTTP_PORT, httpPort.toString());
put(Properties.HTTP_ENDPOINT, "http://127.0.0.1:" + httpPort);
put(Properties.GRPC_ENDPOINT, "127.0.0.1:" + grpcPort);
}});
}

public static DaprPorts build(boolean appPort, boolean httpPort, boolean grpcPort) {
Expand All @@ -48,20 +60,6 @@ public static DaprPorts build(boolean appPort, boolean httpPort, boolean grpcPor
}
}

public void use() {
if (this.httpPort != null) {
System.getProperties().setProperty(Properties.HTTP_PORT.getName(), String.valueOf(this.httpPort));
System.getProperties().setProperty(
Properties.HTTP_ENDPOINT.getName(), "http://127.0.0.1:" + this.httpPort);
}

if (this.grpcPort != null) {
System.getProperties().setProperty(Properties.GRPC_PORT.getName(), String.valueOf(this.grpcPort));
System.getProperties().setProperty(
Properties.GRPC_ENDPOINT.getName(), "127.0.0.1:" + this.grpcPort);
}
}

public Integer getGrpcPort() {
return grpcPort;
}
Expand All @@ -74,6 +72,10 @@ public Integer getAppPort() {
return appPort;
}

public Map<Property<?>, String> getPropertyOverrides() {
return this.overrides;
}

private static Set<Integer> findFreePorts(int n) throws IOException {
Set<Integer> output = new HashSet<>();
for (int i = 0; i < n;) {
Expand Down
21 changes: 19 additions & 2 deletions sdk-tests/src/test/java/io/dapr/it/DaprRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
package io.dapr.it;

import com.google.protobuf.Empty;
import io.dapr.actors.client.ActorClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.resiliency.ResiliencyOptions;
import io.dapr.config.Properties;
import io.dapr.config.Property;
import io.dapr.v1.AppCallbackHealthCheckGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
Expand All @@ -24,6 +28,7 @@
import org.apache.commons.lang3.tuple.ImmutablePair;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
Expand Down Expand Up @@ -141,8 +146,20 @@ public void stop() throws InterruptedException, IOException {
}
}

public void use() {
this.ports.use();
public Map<Property<?>, String> getPropertyOverrides() {
return this.ports.getPropertyOverrides();
}

public DaprClientBuilder newDaprClientBuilder() {
return new DaprClientBuilder().withPropertyOverrides(this.getPropertyOverrides());
}

public ActorClient newActorClient() {
return this.newActorClient(null);
}

public ActorClient newActorClient(ResiliencyOptions resiliencyOptions) {
return new ActorClient(new Properties(this.getPropertyOverrides()), resiliencyOptions);
}

public void waitForAppHealth(int maxWaitMilliseconds) throws InterruptedException {
Expand Down
Loading

0 comments on commit 1d0cd03

Please sign in to comment.