Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.dapr.actors.ActorId;
import io.dapr.actors.ActorUtils;
import io.dapr.client.DaprApiProtocol;
import io.dapr.client.DaprHttpBuilder;
import io.dapr.config.Properties;
import io.dapr.serializer.DaprObjectSerializer;
Expand All @@ -26,7 +27,7 @@ public class ActorProxyBuilder<T> implements Closeable {
/**
* Determine if this builder will create GRPC clients instead of HTTP clients.
*/
private final boolean useGrpc;
private final DaprApiProtocol apiProtocol;

/**
* Actor's type.
Expand Down Expand Up @@ -73,19 +74,32 @@ public ActorProxyBuilder(Class<T> actorTypeClass) {
* @param actorTypeClass Actor's type class.
*/
public ActorProxyBuilder(String actorType, Class<T> actorTypeClass) {
this(actorType, actorTypeClass, Properties.API_PROTOCOL.get());
}

/**
* Instantiates a new builder for a given Actor type, using {@link DefaultObjectSerializer} by default.
*
* {@link DefaultObjectSerializer} is not recommended for production scenarios.
*
* @param actorType Actor's type.
* @param actorTypeClass Actor's type class.
* @param apiProtocol Dapr's API protocol.
*/
private ActorProxyBuilder(String actorType, Class<T> actorTypeClass, DaprApiProtocol apiProtocol) {
if ((actorType == null) || actorType.isEmpty()) {
throw new IllegalArgumentException("ActorType is required.");
}
if (actorTypeClass == null) {
throw new IllegalArgumentException("ActorTypeClass is required.");
}

this.useGrpc = Properties.USE_GRPC.get();
this.apiProtocol = apiProtocol;
this.actorType = actorType;
this.objectSerializer = new DefaultObjectSerializer();
this.clazz = actorTypeClass;
this.daprHttpBuilder = new DaprHttpBuilder();
this.channel = buildManagedChannel();
this.channel = buildManagedChannel(apiProtocol);
}

/**
Expand Down Expand Up @@ -138,11 +152,11 @@ public T build(ActorId actorId) {
* @throws java.lang.IllegalStateException if any required field is missing
*/
private DaprClient buildDaprClient() {
if (this.useGrpc) {
return new DaprGrpcClient(DaprGrpc.newFutureStub(this.channel));
switch (this.apiProtocol) {
case GRPC: return new DaprGrpcClient(DaprGrpc.newFutureStub(this.channel));
case HTTP: return new DaprHttpClient(daprHttpBuilder.build());
default: throw new IllegalStateException("Unsupported protocol: " + this.apiProtocol.name());
}

return new DaprHttpClient(daprHttpBuilder.build());
}

/**
Expand All @@ -158,10 +172,11 @@ public void close() {
/**
* Creates a GRPC managed channel (or null, if not applicable).
*
* @param apiProtocol Dapr's API protocol.
* @return GRPC managed channel or null.
*/
private static ManagedChannel buildManagedChannel() {
if (!Properties.USE_GRPC.get()) {
private static ManagedChannel buildManagedChannel(DaprApiProtocol apiProtocol) {
if (apiProtocol != DaprApiProtocol.GRPC) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.dapr.actors.ActorId;
import io.dapr.actors.ActorTrace;
import io.dapr.client.DaprApiProtocol;
import io.dapr.client.DaprHttpBuilder;
import io.dapr.config.Properties;
import io.dapr.serializer.DaprObjectSerializer;
Expand Down Expand Up @@ -307,7 +308,7 @@ private ActorManager getActorManager(String actorTypeName) {
* @throws java.lang.IllegalStateException if any required field is missing
*/
private static DaprClient buildDaprClient(ManagedChannel channel) {
if (Properties.USE_GRPC.get()) {
if (Properties.API_PROTOCOL.get() == DaprApiProtocol.GRPC) {
return new DaprGrpcClient(channel);
}

Expand All @@ -320,7 +321,7 @@ private static DaprClient buildDaprClient(ManagedChannel channel) {
* @return GRPC managed channel or null.
*/
private static ManagedChannel buildManagedChannel() {
if (!Properties.USE_GRPC.get()) {
Comment thread
artursouza marked this conversation as resolved.
if (Properties.API_PROTOCOL.get() != DaprApiProtocol.GRPC) {
return null;
}

Expand Down
12 changes: 7 additions & 5 deletions sdk-tests/src/test/java/io/dapr/it/AppRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.dapr.it;

import io.dapr.client.DaprApiProtocol;
import io.dapr.config.Properties;

import java.io.IOException;
Expand All @@ -19,7 +20,7 @@
public class AppRun implements Stoppable {

private static final String APP_COMMAND =
"mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=test -D exec.args=\"%s\" -D dapr.grpc.enabled=%b";
"mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=test -D exec.args=\"%s\" -D %s=%s -D %s=%s";

private final DaprPorts ports;

Expand All @@ -31,10 +32,10 @@ public class AppRun implements Stoppable {
String successMessage,
Class serviceClass,
int maxWaitMilliseconds,
boolean useGRPC) {
DaprApiProtocol protocol) {
this.command = new Command(
successMessage,
buildCommand(serviceClass, ports, useGRPC),
buildCommand(serviceClass, ports, protocol),
new HashMap<>() {{
put("DAPR_HTTP_PORT", ports.getHttpPort().toString());
put("DAPR_GRPC_PORT", ports.getGrpcPort().toString());
Expand Down Expand Up @@ -72,10 +73,11 @@ public void stop() throws InterruptedException {
}
}

private static String buildCommand(Class serviceClass, DaprPorts ports, boolean useGRPC) {
private static String buildCommand(Class serviceClass, DaprPorts ports, DaprApiProtocol protocol) {
return String.format(APP_COMMAND, serviceClass.getCanonicalName(),
ports.getAppPort() != null ? ports.getAppPort().toString() : "",
useGRPC);
Properties.API_PROTOCOL.getName(), protocol,
Properties.API_METHOD_INVOCATION_PROTOCOL.getName(), protocol);
}

private static void assertListeningOnPort(int port) {
Expand Down
21 changes: 12 additions & 9 deletions sdk-tests/src/test/java/io/dapr/it/BaseIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.dapr.it;

import io.dapr.client.DaprApiProtocol;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.junit.AfterClass;

Expand All @@ -30,7 +31,7 @@ protected static DaprRun startDaprApp(
Class serviceClass,
Boolean useAppPort,
int maxWaitMilliseconds) throws Exception {
return startDaprApp(testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, true);
return startDaprApp(testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, DaprApiProtocol.GRPC);
}

protected static DaprRun startDaprApp(
Expand All @@ -39,14 +40,15 @@ protected static DaprRun startDaprApp(
Class serviceClass,
Boolean useAppPort,
int maxWaitMilliseconds,
boolean useGRPC) throws Exception {
return startDaprApp(testName, successMessage, serviceClass, useAppPort, true, maxWaitMilliseconds, useGRPC);
DaprApiProtocol protocol) throws Exception {
return startDaprApp(testName, successMessage, serviceClass, useAppPort, true, maxWaitMilliseconds, protocol);
}

protected static DaprRun startDaprApp(
String testName,
int maxWaitMilliseconds) throws Exception {
return startDaprApp(testName, "You're up and running!", null, false, true, maxWaitMilliseconds, true);
return startDaprApp(
testName, "You're up and running!", null, false, true, maxWaitMilliseconds, DaprApiProtocol.GRPC);
}

protected static DaprRun startDaprApp(
Expand All @@ -56,13 +58,13 @@ protected static DaprRun startDaprApp(
Boolean useAppPort,
Boolean useDaprPorts,
int maxWaitMilliseconds,
boolean useGRPC) throws Exception {
DaprApiProtocol protocol) throws Exception {
DaprRun.Builder builder = new DaprRun.Builder(
testName,
() -> DaprPorts.build(useAppPort, useDaprPorts, useDaprPorts),
successMessage,
maxWaitMilliseconds,
useGRPC).withServiceClass(serviceClass);
protocol).withServiceClass(serviceClass);
DaprRun run = builder.build();
TO_BE_STOPPED.add(run);
DAPR_RUN_BUILDERS.put(run.getAppName(), builder);
Expand All @@ -77,7 +79,8 @@ protected static ImmutablePair<AppRun, DaprRun> startSplitDaprAndApp(
Class serviceClass,
Boolean useAppPort,
int maxWaitMilliseconds) throws Exception {
return startSplitDaprAndApp(testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, true);
return startSplitDaprAndApp(
testName, successMessage, serviceClass, useAppPort, maxWaitMilliseconds, DaprApiProtocol.GRPC);
}

protected static ImmutablePair<AppRun, DaprRun> startSplitDaprAndApp(
Expand All @@ -86,13 +89,13 @@ protected static ImmutablePair<AppRun, DaprRun> startSplitDaprAndApp(
Class serviceClass,
Boolean useAppPort,
int maxWaitMilliseconds,
boolean useGRPC) throws Exception {
DaprApiProtocol protocol) throws Exception {
DaprRun.Builder builder = new DaprRun.Builder(
testName,
() -> DaprPorts.build(useAppPort, true, true),
successMessage,
maxWaitMilliseconds,
useGRPC).withServiceClass(serviceClass);
protocol).withServiceClass(serviceClass);
ImmutablePair<AppRun, DaprRun> runs = builder.splitBuild();
TO_BE_STOPPED.add(runs.left);
TO_BE_STOPPED.add(runs.right);
Expand Down
40 changes: 24 additions & 16 deletions sdk-tests/src/test/java/io/dapr/it/DaprRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.dapr.it;

import io.dapr.client.DaprApiProtocol;
import io.dapr.config.Properties;
import org.apache.commons.lang3.tuple.ImmutablePair;

Expand All @@ -23,7 +24,7 @@ public class DaprRun implements Stoppable {

// the arg in -Dexec.args is the app's port
private static final String DAPR_COMMAND =
" -- mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=test -D exec.args=\"%s\" -D dapr.grpc.enabled=%s";
" -- mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=test -D exec.args=\"%s\" -D %s=%s -D %s=%s";

private final DaprPorts ports;

Expand All @@ -44,11 +45,11 @@ private DaprRun(String testName,
String successMessage,
Class serviceClass,
int maxWaitMilliseconds,
boolean useGRPC) {
DaprApiProtocol protocol) {
// The app name needs to be deterministic since we depend on it to kill previous runs.
this.appName = serviceClass == null ? testName : String.format("%s_%s", testName, serviceClass.getSimpleName());
this.startCommand =
new Command(successMessage, buildDaprCommand(this.appName, serviceClass, ports, useGRPC));
new Command(successMessage, buildDaprCommand(this.appName, serviceClass, ports, protocol));
this.listCommand = new Command(
this.appName,
"dapr list");
Expand Down Expand Up @@ -132,20 +133,24 @@ public void stop() throws InterruptedException, IOException {

public void use() {
if (this.ports.getHttpPort() != null) {
System.getProperties().setProperty("dapr.http.port", String.valueOf(this.ports.getHttpPort()));
System.getProperties().setProperty(Properties.HTTP_PORT.getName(), String.valueOf(this.ports.getHttpPort()));
}
if (this.ports.getGrpcPort() != null) {
System.getProperties().setProperty("dapr.grpc.port", String.valueOf(this.ports.getGrpcPort()));
System.getProperties().setProperty(Properties.GRPC_PORT.getName(), String.valueOf(this.ports.getGrpcPort()));
}
System.getProperties().setProperty("dapr.grpc.enabled", Boolean.TRUE.toString());
System.getProperties().setProperty(Properties.API_PROTOCOL.getName(), DaprApiProtocol.GRPC.name());
}

public void switchToGRPC() {
System.getProperties().setProperty("dapr.grpc.enabled", Boolean.TRUE.toString());
System.getProperties().setProperty(Properties.API_PROTOCOL.getName(), DaprApiProtocol.GRPC.name());
}

public void switchToHTTP() {
System.getProperties().setProperty("dapr.grpc.enabled", Boolean.FALSE.toString());
System.getProperties().setProperty(Properties.API_PROTOCOL.getName(), DaprApiProtocol.HTTP.name());
}

public void switchToProtocol(DaprApiProtocol protocol) {
System.getProperties().setProperty(Properties.API_PROTOCOL.getName(), protocol.name());
}

public int getGrpcPort() {
Expand All @@ -164,14 +169,17 @@ public String getAppName() {
return appName;
}

private static String buildDaprCommand(String appName, Class serviceClass, DaprPorts ports, boolean useGRPC) {
private static String buildDaprCommand(
String appName, Class serviceClass, DaprPorts ports, DaprApiProtocol protocol) {
StringBuilder stringBuilder = new StringBuilder(String.format(DAPR_RUN, appName))
.append(ports.getAppPort() != null ? " --app-port " + ports.getAppPort() : "")
.append(ports.getHttpPort() != null ? " --dapr-http-port " + ports.getHttpPort() : "")
.append(ports.getGrpcPort() != null ? " --dapr-grpc-port " + ports.getGrpcPort() : "")
.append(serviceClass == null ? "" :
String.format(DAPR_COMMAND, serviceClass.getCanonicalName(),
ports.getAppPort() != null ? ports.getAppPort().toString() : "", useGRPC));
ports.getAppPort() != null ? ports.getAppPort().toString() : "",
Properties.API_PROTOCOL.getName(), protocol,
Properties.API_METHOD_INVOCATION_PROTOCOL.getName(), protocol));
return stringBuilder.toString();
}

Expand Down Expand Up @@ -200,19 +208,19 @@ static class Builder {

private Class serviceClass;

private boolean useGRPC;
private DaprApiProtocol protocol;

Builder(
String testName,
Supplier<DaprPorts> portsSupplier,
String successMessage,
int maxWaitMilliseconds,
boolean useGRPC) {
DaprApiProtocol protocol) {
this.testName = testName;
this.portsSupplier = portsSupplier;
this.successMessage = successMessage;
this.maxWaitMilliseconds = maxWaitMilliseconds;
this.useGRPC = useGRPC;
this.protocol = protocol;
}

public Builder withServiceClass(Class serviceClass) {
Expand All @@ -227,7 +235,7 @@ DaprRun build() {
this.successMessage,
this.serviceClass,
this.maxWaitMilliseconds,
this.useGRPC);
this.protocol);
}

/**
Expand All @@ -241,15 +249,15 @@ ImmutablePair<AppRun, DaprRun> splitBuild() {
this.successMessage,
this.serviceClass,
this.maxWaitMilliseconds,
this.useGRPC);
this.protocol);

DaprRun daprRun = new DaprRun(
this.testName,
ports,
DAPR_SUCCESS_MESSAGE,
null,
this.maxWaitMilliseconds,
this.useGRPC);
this.protocol);

return new ImmutablePair<>(appRun, daprRun);
}
Expand Down
Loading