Skip to content

Commit ba05e59

Browse files
authored
Add standalone mode in routeclient (#39)
* add ceresdb standalone route mode * standalone mode * format * test case
1 parent 609c22b commit ba05e59

File tree

7 files changed

+124
-7
lines changed

7 files changed

+124
-7
lines changed

ceresdb-protocol/src/main/java/io/ceresdb/CeresDBClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ private static RpcClient initRpcClient(final CeresDBOptions opts) {
293293
private static RouterClient initRouteClient(final CeresDBOptions opts, final RpcClient rpcClient) {
294294
final RouterOptions routerOpts = opts.getRouterOptions();
295295
routerOpts.setRpcClient(rpcClient);
296-
final RouterClient routerClient = new RouterClient();
296+
final RouterClient routerClient = routerOpts.getRouteMode().equals(RouteMode.CLUSTER) ? new RouterClient() :
297+
new StandaloneRouterClient();
297298
if (!routerClient.init(routerOpts)) {
298299
throw new IllegalStateException("Fail to start router client");
299300
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.ceresdb;
18+
19+
/**
20+
* route mode
21+
* @author lee
22+
* @version : RouteMode.java, v 0.1 2023.01.17 14:23 lee Exp $
23+
*/
24+
public enum RouteMode {
25+
26+
/**
27+
* In this mode, the client does not cache routing information and each request is proxied through the server to the correct server
28+
*/
29+
STANDALONE,
30+
31+
/**
32+
* In this mode, the client cache routing information. Client find the correct server firstly, and then request to the correct server directly.
33+
*/
34+
CLUSTER
35+
}

ceresdb-protocol/src/main/java/io/ceresdb/RouterClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ public class RouterClient implements Lifecycle<RouterOptions>, Display, Iterable
8080
private ScheduledExecutorService cleaner;
8181
private ScheduledExecutorService refresher;
8282

83-
private RouterOptions opts;
84-
private RpcClient rpcClient;
85-
private RouterByMetrics router;
86-
private InnerMetrics metrics;
83+
protected RouterOptions opts;
84+
protected RpcClient rpcClient;
85+
protected RouterByMetrics router;
86+
protected InnerMetrics metrics;
8787

8888
private final ConcurrentMap<String, Route> routeCache = new ConcurrentHashMap<>();
8989

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.ceresdb;
18+
19+
import java.util.Collection;
20+
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
import java.util.concurrent.CompletableFuture;
24+
25+
/**
26+
* A route rpc client which cached nothing about table information, and return
27+
* clusterAddress directly
28+
* @author lee
29+
*/
30+
public class StandaloneRouterClient extends RouterClient {
31+
32+
public CompletableFuture<Map<String, Route>> routeFor(final Collection<String> metrics) {
33+
if (metrics == null || metrics.isEmpty()) {
34+
return Utils.completedCf(Collections.emptyMap());
35+
}
36+
37+
final Map<String, Route> routeMap = new HashMap<>();
38+
39+
metrics.forEach(metric -> {
40+
Route route = new Route();
41+
route.setEndpoint(this.opts.getClusterAddress());
42+
route.setMetric(metric);
43+
routeMap.put(metric, route);
44+
});
45+
return Utils.completedCf(routeMap);
46+
47+
}
48+
49+
}

ceresdb-protocol/src/main/java/io/ceresdb/options/CeresDBOptions.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.concurrent.Executor;
2020

2121
import io.ceresdb.LimitedPolicy;
22+
import io.ceresdb.RouteMode;
2223
import io.ceresdb.common.Copiable;
2324
import io.ceresdb.common.Endpoint;
2425
import io.ceresdb.common.Tenant;
@@ -237,6 +238,11 @@ public static final class Builder {
237238
// all route tables are refreshed every 30 seconds.
238239
private long routeTableRefreshPeriodSeconds = 30;
239240

241+
/** Route mode for request
242+
@see RouteMode
243+
**/
244+
private RouteMode routeMode = RouteMode.CLUSTER;
245+
240246
public Builder(Endpoint clusterAddress) {
241247
this.clusterAddress = clusterAddress;
242248
}
@@ -432,6 +438,18 @@ public Builder routeTableRefreshPeriodSeconds(final long routeTableRefreshPeriod
432438
return this;
433439
}
434440

441+
/**
442+
* Route mode for request
443+
* @see RouteMode
444+
*
445+
* @param routeMode route mode for request
446+
* @return this builder
447+
*/
448+
public Builder routeMode(final RouteMode routeMode) {
449+
this.routeMode = routeMode;
450+
return this;
451+
}
452+
435453
/**
436454
* A good start, happy coding.
437455
*
@@ -449,6 +467,8 @@ public CeresDBOptions build() {
449467
opts.routerOptions.setMaxCachedSize(this.routeTableMaxCachedSize);
450468
opts.routerOptions.setGcPeriodSeconds(this.routeTableGcPeriodSeconds);
451469
opts.routerOptions.setRefreshPeriodSeconds(this.routeTableRefreshPeriodSeconds);
470+
opts.routerOptions.setRouteMode(this.routeMode);
471+
452472
opts.writeOptions = new WriteOptions();
453473
opts.writeOptions.setMaxWriteSize(this.maxWriteSize);
454474
opts.writeOptions.setMaxRetries(this.writeMaxRetries);

ceresdb-protocol/src/main/java/io/ceresdb/options/RouterOptions.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package io.ceresdb.options;
1818

19+
import io.ceresdb.RouteMode;
1920
import io.ceresdb.common.Copiable;
2021
import io.ceresdb.common.Endpoint;
2122
import io.ceresdb.rpc.RpcClient;
@@ -38,6 +39,8 @@ public class RouterOptions implements Copiable<RouterOptions> {
3839
// all route tables are refreshed every 30 seconds.
3940
private long refreshPeriodSeconds = 30;
4041

42+
private RouteMode routeMode = RouteMode.CLUSTER;
43+
4144
public RpcClient getRpcClient() {
4245
return rpcClient;
4346
}
@@ -78,6 +81,14 @@ public void setRefreshPeriodSeconds(long refreshPeriodSeconds) {
7881
this.refreshPeriodSeconds = refreshPeriodSeconds;
7982
}
8083

84+
public RouteMode getRouteMode() {
85+
return routeMode;
86+
}
87+
88+
public void setRouteMode(RouteMode routeMode) {
89+
this.routeMode = routeMode;
90+
}
91+
8192
@Override
8293
public RouterOptions copy() {
8394
final RouterOptions opts = new RouterOptions();
@@ -86,6 +97,7 @@ public RouterOptions copy() {
8697
opts.maxCachedSize = this.maxCachedSize;
8798
opts.gcPeriodSeconds = this.gcPeriodSeconds;
8899
opts.refreshPeriodSeconds = this.refreshPeriodSeconds;
100+
opts.routeMode = this.routeMode;
89101
return opts;
90102
}
91103

@@ -97,6 +109,8 @@ public String toString() {
97109
", maxCachedSize=" + maxCachedSize + //
98110
", gcPeriodSeconds=" + gcPeriodSeconds + //
99111
", refreshPeriodSeconds=" + refreshPeriodSeconds + //
112+
", routeMode=" + routeMode + //
100113
'}';
101114
}
115+
102116
}

ceresdb-protocol/src/test/java/io/ceresdb/CeresDBClientTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import java.util.concurrent.CompletableFuture;
2020
import java.util.concurrent.ExecutionException;
2121

22-
import io.ceresdb.CeresDBClient;
23-
2422
import org.junit.After;
2523
import org.junit.Assert;
2624
import org.junit.Before;

0 commit comments

Comments
 (0)