Skip to content

Commit 70377ef

Browse files
Server options: add new cluster configuration
1 parent 3da3a4b commit 70377ef

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

objectbox-java/src/main/java/io/objectbox/sync/server/PeerInfo.java renamed to objectbox-java/src/main/java/io/objectbox/sync/server/ClusterPeerInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
* Internal class to keep configuration for a cluster peer.
2424
*/
2525
@Internal
26-
class PeerInfo {
26+
class ClusterPeerInfo {
2727
String url;
2828
SyncCredentialsToken credentials;
2929

30-
PeerInfo(String url, SyncCredentialsToken credentials) {
30+
ClusterPeerInfo(String url, SyncCredentialsToken credentials) {
3131
this.url = url;
3232
this.credentials = credentials;
3333
}

objectbox-java/src/main/java/io/objectbox/sync/server/SyncServerBuilder.java

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ public class SyncServerBuilder {
3939
final BoxStore boxStore;
4040
final String url;
4141
private final List<SyncCredentialsToken> credentials = new ArrayList<>();
42-
final List<PeerInfo> peers = new ArrayList<>();
4342

4443
private @Nullable String certificatePath;
4544
SyncChangeListener changeListener;
45+
private @Nullable String clusterId;
46+
private final List<ClusterPeerInfo> clusterPeers = new ArrayList<>();
47+
private int clusterFlags;
4648

4749
public SyncServerBuilder(BoxStore boxStore, String url, SyncCredentials authenticatorCredentials) {
4850
checkNotNull(boxStore, "BoxStore is required.");
@@ -98,21 +100,55 @@ public SyncServerBuilder changeListener(SyncChangeListener changeListener) {
98100
}
99101

100102
/**
101-
* Adds a server peer, to which this server should connect to as a client using {@link SyncCredentials#none()}.
103+
* Enables cluster mode (requires the Cluster feature) and associates this cluster peer with the given ID.
104+
* <p>
105+
* Cluster peers need to share the same ID to be in the same cluster.
106+
*/
107+
public SyncServerBuilder clusterId(String id) {
108+
checkNotNull(id, "Cluster ID must not be null");
109+
this.clusterId = id;
110+
return this;
111+
}
112+
113+
/**
114+
* @deprecated Use {@link #clusterPeer(String, SyncCredentials) clusterPeer(url, SyncCredentials.none())} instead.
102115
*/
116+
@Deprecated
103117
public SyncServerBuilder peer(String url) {
104-
return peer(url, SyncCredentials.none());
118+
return clusterPeer(url, SyncCredentials.none());
105119
}
106120

107121
/**
108-
* Adds a server peer, to which this server should connect to as a client using the given credentials.
122+
* @deprecated Use {@link #clusterPeer(String,SyncCredentials)} instead.
109123
*/
124+
@Deprecated
110125
public SyncServerBuilder peer(String url, SyncCredentials credentials) {
126+
return clusterPeer(url, credentials);
127+
}
128+
129+
/**
130+
* Adds a (remote) cluster peer, to which this server should connect to as a client using the given credentials.
131+
* <p>
132+
* To use this, must set a {@link #clusterId(String)}.
133+
*/
134+
public SyncServerBuilder clusterPeer(String url, SyncCredentials credentials) {
111135
if (!(credentials instanceof SyncCredentialsToken)) {
112136
throw new IllegalArgumentException("Sync credentials of type " + credentials.getType()
113137
+ " are not supported");
114138
}
115-
peers.add(new PeerInfo(url, (SyncCredentialsToken) credentials));
139+
clusterPeers.add(new ClusterPeerInfo(url, (SyncCredentialsToken) credentials));
140+
return this;
141+
}
142+
143+
/**
144+
* Sets bit flags to configure the cluster behavior of the Sync server (aka cluster peer).
145+
* <p>
146+
* To use this, must set a {@link #clusterId(String)}.
147+
*
148+
* @param flags One or more of {@link ClusterFlags}.
149+
*/
150+
public SyncServerBuilder clusterFlags(int flags) {
151+
this.clusterFlags = flags;
116152
return this;
117153
}
118154

@@ -125,6 +161,9 @@ public SyncServer build() {
125161
if (credentials.isEmpty()) {
126162
throw new IllegalStateException("At least one authenticator is required.");
127163
}
164+
if (!clusterPeers.isEmpty() || clusterFlags != 0) {
165+
checkNotNull(clusterId, "Cluster ID must be set to use cluster features.");
166+
}
128167
return new SyncServerImpl(this);
129168
}
130169

@@ -159,6 +198,10 @@ byte[] buildSyncServerOptions() {
159198
if (certificatePath != null) {
160199
certificatePathOffset = fbb.createString(certificatePath);
161200
}
201+
int clusterIdOffset = 0;
202+
if (clusterId != null) {
203+
clusterIdOffset = fbb.createString(clusterId);
204+
}
162205
int authenticationMethodsOffset = buildAuthenticationMethods(fbb);
163206
int clusterPeersVectorOffset = buildClusterPeers(fbb);
164207

@@ -177,11 +220,15 @@ byte[] buildSyncServerOptions() {
177220
// SyncServerOptions.addHistorySizeTargetKb();
178221
// SyncServerOptions.addAdminUrl();
179222
// SyncServerOptions.addAdminThreads();
180-
// SyncServerOptions.addClusterId();
223+
if (clusterIdOffset > 0) {
224+
SyncServerOptions.addClusterId(fbb, clusterIdOffset);
225+
}
181226
if (clusterPeersVectorOffset > 0) {
182227
SyncServerOptions.addClusterPeers(fbb, clusterPeersVectorOffset);
183228
}
184-
// SyncServerOptions.addClusterFlags();
229+
if (clusterFlags > 0) {
230+
SyncServerOptions.addClusterFlags(fbb, clusterFlags);
231+
}
185232
int offset = SyncServerOptions.endSyncServerOptions(fbb);
186233
fbb.finish(offset);
187234

@@ -216,13 +263,13 @@ private int buildCredentials(FlatBufferBuilder fbb, SyncCredentialsToken tokenCr
216263
}
217264

218265
private int buildClusterPeers(FlatBufferBuilder fbb) {
219-
if (peers.isEmpty()) {
266+
if (clusterPeers.isEmpty()) {
220267
return 0;
221268
}
222269

223-
int[] peersOffsets = new int[peers.size()];
224-
for (int i = 0; i < peers.size(); i++) {
225-
PeerInfo peer = peers.get(i);
270+
int[] peersOffsets = new int[clusterPeers.size()];
271+
for (int i = 0; i < clusterPeers.size(); i++) {
272+
ClusterPeerInfo peer = clusterPeers.get(i);
226273

227274
int urlOffset = fbb.createString(peer.url);
228275
int credentialsOffset = buildCredentials(fbb, peer.credentials);

0 commit comments

Comments
 (0)