Skip to content

Commit d00e92b

Browse files
committed
Java FIT performer: support two ClusterEnvironment modes
3.2.6 added an easy way for SDK users to configure the SDK without having to take ownership of ClusterEnvironment management. It also allows passing parameters in the connection string, which is not allowed with those externally owned ClusterEnvironments. The performer will now use the "new" mode where possible. The features required by the new mode are likely not to be required in the situations where we are using the old mode. Change-Id: I58755c369856bd977779fee4174ca54814189dea Reviewed-on: https://review.couchbase.org/c/couchbase-jvm-clients/+/229357 Reviewed-by: David Nault <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent 7c61097 commit d00e92b

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

java-fit-performer/src/main/java/com/couchbase/JavaPerformer.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,19 +231,30 @@ public void clusterConnectionCreate(ClusterConnectionCreateRequest request,
231231
});
232232
});
233233

234-
var clusterEnvironment = OptionsUtil.convertClusterConfig(request, getCluster, onClusterConnectionClose);
235-
236-
// [if:3.7.5] first version that allows specifying custom publishOn scheduler
237-
var userExecutorAndScheduler = UserSchedulerUtil.userExecutorAndScheduler();
238-
onClusterConnectionClose.add(userExecutorAndScheduler::dispose);
239-
clusterEnvironment.publishOnScheduler(userExecutorAndScheduler::scheduler);
240-
// [end]
234+
// [if:3.2.6]
235+
// 3.2.6 added an easy way for SDK users to configure the SDK without having to take ownership of
236+
// ClusterEnvironment management. It also allows passing parameters in the connection string, which
237+
// is not allowed with those externally owned ClusterEnvironments.
238+
var clusterEnvironment = OptionsUtil.convertClusterConfigToConsumer(request, getCluster, onClusterConnectionClose);
241239

242240
var connection = new ClusterConnection(request.getClusterHostname(),
243241
request.getClusterUsername(),
244242
request.getClusterPassword(),
245243
clusterEnvironment,
246244
onClusterConnectionClose);
245+
// [end]
246+
247+
// [if:<3.2.6]
248+
// Support falling back to the original way of creating
249+
//? var clusterEnvironment = OptionsUtil.convertClusterConfig(request, getCluster, onClusterConnectionClose);
250+
251+
//? var connection = new ClusterConnection(request.getClusterHostname(),
252+
//? request.getClusterUsername(),
253+
//? request.getClusterPassword(),
254+
//? clusterEnvironment,
255+
//? onClusterConnectionClose);
256+
// [end]
257+
247258
clusterConnections.put(clusterConnectionId, connection);
248259
logger.info("Created cluster connection {} for user {}, now have {}",
249260
clusterConnectionId, request.getClusterUsername(), clusterConnections.size());

java-fit-performer/src/main/java/com/couchbase/utils/ClusterConnection.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
import java.time.Duration;
3030
import java.util.ArrayList;
3131
import java.util.List;
32+
import java.util.function.Consumer;
3233

3334

3435
public class ClusterConnection {
3536
private final Cluster cluster;
36-
@Nullable private final ClusterEnvironment config;
37+
@Nullable private final ClusterEnvironment environmentOwnedByCaller;
3738
public final String username;
3839
// Commands to run when this ClusterConnection is being closed. Allows closing other related resources that have
3940
// the same lifetime.
@@ -49,16 +50,38 @@ public ClusterConnection(String hostname,
4950

5051
var co = ClusterOptions.clusterOptions(username, password);
5152
if (config != null) {
52-
this.config = config.build();
53-
co.environment(this.config);
53+
this.environmentOwnedByCaller = config.build();
54+
co.environment(this.environmentOwnedByCaller);
5455
}
5556
else {
56-
this.config = null;
57+
this.environmentOwnedByCaller = null;
5758
}
5859

5960
this.cluster = Cluster.connect(hostname, co);
6061
}
6162

63+
// [if:3.2.6]
64+
public ClusterConnection(String hostname,
65+
String username,
66+
String password,
67+
@Nullable Consumer<ClusterEnvironment.Builder> config,
68+
ArrayList<Runnable> onClusterConnectionClose) {
69+
this.username = username;
70+
this.onClusterConnectionClose = onClusterConnectionClose;
71+
72+
var co = ClusterOptions.clusterOptions(username, password)
73+
.environment(env -> {
74+
if (config != null) {
75+
config.accept(env);
76+
}
77+
});
78+
// The SDK will manage things instead in this mode
79+
this.environmentOwnedByCaller = null;
80+
81+
this.cluster = Cluster.connect(hostname, co);
82+
}
83+
// [end]
84+
6285
public Cluster cluster(){
6386
return cluster;
6487
}
@@ -104,8 +127,8 @@ public Collection collection(com.couchbase.client.protocol.shared.Collection col
104127

105128
public void close() {
106129
cluster.disconnect();
107-
if (config != null) {
108-
config.shutdown();
130+
if (environmentOwnedByCaller != null) {
131+
environmentOwnedByCaller.shutdown();
109132
}
110133
onClusterConnectionClose.forEach(Runnable::run);
111134
}

java-fit-performer/src/main/java/com/couchbase/utils/OptionsUtil.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.couchbase.client.core.env.SecurityConfig;
3131
import com.couchbase.client.core.env.TimeoutConfig;
3232
import com.couchbase.client.core.msg.kv.DurabilityLevel;
33-
import com.couchbase.client.java.ClusterOptions;
3433
import com.couchbase.client.java.env.ClusterEnvironment;
3534
import com.couchbase.client.java.json.JsonArray;
3635
import com.couchbase.client.java.json.JsonObject;
@@ -72,7 +71,6 @@
7271
import org.slf4j.LoggerFactory;
7372

7473
import javax.annotation.Nullable;
75-
import javax.net.ssl.TrustManagerFactory;
7674
import java.io.ByteArrayInputStream;
7775
import java.nio.charset.StandardCharsets;
7876
import java.nio.file.Path;
@@ -85,6 +83,7 @@
8583
import java.util.Map;
8684
import java.util.concurrent.ConcurrentHashMap;
8785
import java.util.concurrent.TimeUnit;
86+
import java.util.function.Consumer;
8887
import java.util.function.Supplier;
8988
import java.util.stream.Collectors;
9089

@@ -93,6 +92,23 @@ public class OptionsUtil {
9392

9493
private OptionsUtil() {}
9594

95+
public static Consumer<ClusterEnvironment.Builder> convertClusterConfigToConsumer(ClusterConnectionCreateRequest request,
96+
Supplier<ClusterConnection> getCluster,
97+
ArrayList<Runnable> onClusterConnectionClose) {
98+
return (builder) -> {
99+
if (request.hasClusterConfig()) {
100+
var cc = request.getClusterConfig();
101+
applyClusterConfig(builder, cc, onClusterConnectionClose);
102+
103+
// [if:3.3.0]
104+
if (request.getClusterConfig().hasTransactionsConfig()) {
105+
applyTransactionsConfig(request, getCluster, builder);
106+
}
107+
// [end]
108+
}
109+
};
110+
}
111+
96112
public static
97113
ClusterEnvironment.Builder convertClusterConfig(ClusterConnectionCreateRequest request,
98114
Supplier<ClusterConnection> getCluster,
@@ -102,14 +118,10 @@ ClusterEnvironment.Builder convertClusterConfig(ClusterConnectionCreateRequest r
102118
if (request.hasClusterConfig()) {
103119
var cc = request.getClusterConfig();
104120

105-
// [if:3.3.0]
106-
if (request.getClusterConfig().hasTransactionsConfig()) {
107-
applyTransactionsConfig(request, getCluster, clusterEnvironment);
108-
}
109-
// [end]
110-
111121
applyClusterConfig(clusterEnvironment, cc, onClusterConnectionClose);
112122

123+
// No need to support transactions here, as this code is now only executed in <3.2.6 mode (transactions
124+
// was introduced in 3.3.0)
113125
}
114126

115127
return clusterEnvironment;
@@ -295,6 +307,12 @@ private static void applyClusterConfig(ClusterEnvironment.Builder clusterEnviron
295307
clusterEnvironment.disableAppTelemetry(!cc.getEnableAppTelemetry());
296308
// [end]
297309
}
310+
311+
// [if:3.7.5] first version that allows specifying custom publishOn scheduler
312+
var userExecutorAndScheduler = UserSchedulerUtil.userExecutorAndScheduler();
313+
onClusterConnectionClose.add(userExecutorAndScheduler::dispose);
314+
clusterEnvironment.publishOnScheduler(userExecutorAndScheduler::scheduler);
315+
// [end]
298316
}
299317

300318
// [if:3.5.1]

0 commit comments

Comments
 (0)