Skip to content

Commit

Permalink
Remove all global state from setProperty (dapr#1138)
Browse files Browse the repository at this point in the history
* Remove all global state in from setProperty

Signed-off-by: Artur Souza <asouza.pro@gmail.com>

* use Map.of

Signed-off-by: Artur Souza <asouza.pro@gmail.com>

* Remove dependency that is not needed.

Signed-off-by: Artur Souza <asouza.pro@gmail.com>

---------

Signed-off-by: Artur Souza <asouza.pro@gmail.com>
Signed-off-by: salaboy <Salaboy@gmail.com>
  • Loading branch information
artursouza authored and salaboy committed Oct 4, 2024
1 parent 846db0d commit 1e90dcf
Show file tree
Hide file tree
Showing 51 changed files with 285 additions and 296 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Map;

public abstract class AbstractDaprKeyValueAdapter implements KeyValueAdapter {
private static final Map<String, String> CONTENT_TYPE_META = Collections.singletonMap(
private static final Map<String, String> CONTENT_TYPE_META = Map.of(
"contentType", "application/json");

private final DaprClient daprClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ private String createSql(String sqlPattern, String keyspace, Object criteria) {
}

private void execUsingBinding(String sql) {
Map<String, String> meta = Collections.singletonMap("sql", sql);
Map<String, String> meta = Map.of("sql", sql);

daprClient.invokeBinding(bindingName, "exec", null, meta).block();
}

private <T> T queryUsingBinding(String sql, TypeRef<T> typeRef) {
Map<String, String> meta = Collections.singletonMap("sql", sql);
Map<String, String> meta = Map.of("sql", sql);

return daprClient.invokeBinding(bindingName, "query", null, meta, typeRef).block();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ private String createSql(String sqlPattern, String keyspace, Object criteria) {
}

private void execUsingBinding(String sql) {
Map<String, String> meta = Collections.singletonMap("sql", sql);
Map<String, String> meta = Map.of("sql", sql);

daprClient.invokeBinding(bindingName, "exec", null, meta).block();
}

private <T> T queryUsingBinding(String sql, TypeRef<T> typeRef) {
Map<String, String> meta = Collections.singletonMap("sql", sql);
Map<String, String> meta = Map.of("sql", sql);

return daprClient.invokeBinding(bindingName, "query", null, meta, typeRef).block();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
import org.springframework.lang.Nullable;
import reactor.core.publisher.Hooks;
import reactor.core.publisher.Mono;
<<<<<<< HEAD
import reactor.util.context.Context;
import java.util.Collections;
import java.util.HashMap;
=======

>>>>>>> 7490434d (Remove all global state from setProperty (#1138))
import java.util.Map;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.dapr.client.domain.Metadata;
import io.dapr.client.domain.PublishEventRequest;

import java.util.Map;
import java.util.UUID;

import static java.util.Collections.singletonMap;
Expand Down Expand Up @@ -66,7 +67,7 @@ public static void main(String[] args) throws Exception {
client.publishEvent(
new PublishEventRequest(PUBSUB_NAME, TOPIC_NAME, cloudEvent)
.setContentType(CloudEvent.CONTENT_TYPE)
.setMetadata(singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS))).block();
.setMetadata(Map.of(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS))).block();
System.out.println("Published cloud event with message: " + cloudEvent.getData());

try {
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
51 changes: 10 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,10 +144,14 @@ 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;
}

protected static <T extends AutoCloseable> T deferClose(T object) {
TO_BE_CLOSED.add(object);
return object;
}

@AfterAll
public static void cleanUp() throws Exception {
while (!TO_BE_CLOSED.isEmpty()) {
Expand All @@ -194,8 +163,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
Loading

0 comments on commit 1e90dcf

Please sign in to comment.