/
CopycatReplica.java
262 lines (230 loc) · 8.84 KB
/
CopycatReplica.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.atomix.copycat;
import io.atomix.catalogue.client.RaftClient;
import io.atomix.catalogue.server.RaftServer;
import io.atomix.catalogue.server.storage.Storage;
import io.atomix.catalyst.serializer.Serializer;
import io.atomix.catalyst.transport.*;
import io.atomix.catalyst.util.Assert;
import io.atomix.catalyst.util.ConfigurationException;
import io.atomix.copycat.manager.ResourceManager;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
/**
* Server-side {@link Copycat} implementation.
* <p>
* This is a {@link Copycat} implementation that manages state for resources and executes all
* {@link Resource} operations locally via a {@link RaftServer}.
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
public final class CopycatReplica extends Copycat {
/**
* Returns a new Copycat replica builder.
* <p>
* The provided set of members will be used to connect to the other members in the Raft cluster.
*
* @param address The local server member address.
* @param members The cluster members to which to connect.
* @return The replica builder.
*/
public static Builder builder(Address address, Address... members) {
return builder(address, Arrays.asList(Assert.notNull(members, "members")));
}
/**
* Returns a new Copycat replica builder.
* <p>
* The provided set of members will be used to connect to the other members in the Raft cluster.
*
* @param address The local server member address.
* @param members The cluster members to which to connect.
* @return The replica builder.
*/
public static Builder builder(Address address, Collection<Address> members) {
return new Builder(address, members);
}
private final RaftServer server;
/**
* @throws NullPointerException if {@code client} or {@code server} are null
*/
public CopycatReplica(RaftClient client, RaftServer server, Transport transport) {
super(client, transport);
this.server = server;
}
@Override
public CompletableFuture<Copycat> open() {
return server.open().thenCompose(v -> super.open());
}
@Override
public CompletableFuture<Void> close() {
return super.close().thenCompose(v -> server.close());
}
/**
* Combined transport that aids in the local client communicating directly with the local server.
*/
private static class CombinedTransport implements Transport {
private final Transport local;
private final Transport remote;
private final Map<UUID, Server> servers = new ConcurrentHashMap<>();
private CombinedTransport(Transport local, Transport remote) {
this.local = local;
this.remote = remote;
}
@Override
public Client client(UUID id) {
return remote.client(Assert.notNull(id, "id"));
}
@Override
public Server server(UUID id) {
return servers.computeIfAbsent(Assert.notNull(id, "id"), i -> new CombinedServer(local.server(i), remote.server(i)));
}
@Override
public CompletableFuture<Void> close() {
return local.close().thenCompose(v -> remote.close());
}
}
/**
* Combined server that access connections from the local client directly.
*/
private static class CombinedServer implements Server {
private final Server local;
private final Server remote;
private CombinedServer(Server local, Server remote) {
this.local = local;
this.remote = remote;
}
@Override
public UUID id() {
return local.id();
}
@Override
public CompletableFuture<Void> listen(Address address, Consumer<Connection> listener) {
Assert.notNull(address, "address");
Assert.notNull(listener, "listener");
return local.listen(address, listener).thenCompose(v -> remote.listen(address, listener));
}
@Override
public CompletableFuture<Void> close() {
return local.close().thenCompose(v -> remote.close());
}
}
/**
* Copycat builder.
*/
public static class Builder extends Copycat.Builder {
private RaftServer.Builder serverBuilder;
private Transport transport;
private LocalServerRegistry localRegistry = new LocalServerRegistry();
private Builder(Address address, Collection<Address> members) {
super(Collections.singleton(address));
this.serverBuilder = RaftServer.builder(address, members);
}
/**
* Sets the server transport.
*
* @param transport The client server.
* @return The client builder.
* @throws NullPointerException if {@code command} is null
*/
public Builder withTransport(Transport transport) {
this.transport = Assert.notNull(transport, "transport");
return this;
}
/**
* Sets the Raft serializer.
*
* @param serializer The Raft serializer.
* @return The Raft builder.
* @throws NullPointerException if {@code command} is null
*/
public Builder withSerializer(Serializer serializer) {
clientBuilder.withSerializer(serializer);
serverBuilder.withSerializer(serializer);
return this;
}
/**
* Sets the server storage module.
*
* @param storage The server storage module.
* @return The Copycat server builder.
* @throws NullPointerException if {@code command} is null
*/
public Builder withStorage(Storage storage) {
serverBuilder.withStorage(storage);
return this;
}
/**
* Sets the Raft election timeout, returning the Raft configuration for method chaining.
*
* @param electionTimeout The Raft election timeout in milliseconds.
* @return The Raft configuration.
* @throws IllegalArgumentException If the election timeout is not positive
* @throws NullPointerException if {@code command} is null
*/
public Builder withElectionTimeout(Duration electionTimeout) {
serverBuilder.withElectionTimeout(electionTimeout);
return this;
}
/**
* Sets the Raft heartbeat interval, returning the Raft configuration for method chaining.
*
* @param heartbeatInterval The Raft heartbeat interval in milliseconds.
* @return The Raft configuration.
* @throws IllegalArgumentException If the heartbeat interval is not positive
* @throws NullPointerException if {@code command} is null
*/
public Builder withHeartbeatInterval(Duration heartbeatInterval) {
serverBuilder.withHeartbeatInterval(heartbeatInterval);
return this;
}
/**
* Sets the Raft session timeout, returning the Raft configuration for method chaining.
*
* @param sessionTimeout The Raft session timeout in milliseconds.
* @return The Raft configuration.
* @throws IllegalArgumentException If the session timeout is not positive
* @throws NullPointerException if {@code command} is null
*/
public Builder withSessionTimeout(Duration sessionTimeout) {
serverBuilder.withSessionTimeout(sessionTimeout);
return this;
}
@Override
public CopycatReplica build() {
// If no transport was configured by the user, attempt to load the Netty transport.
if (transport == null) {
try {
transport = (Transport) Class.forName("io.atomix.catalyst.transport.NettyTransport").newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new ConfigurationException("transport not configured");
}
}
// Configure the client and server with a transport that routes all local client communication
// directly through the local server, ensuring we don't incur unnecessary network traffic by
// sending operations to a remote server when a local server is already available in the same JVM.
RaftClient client = clientBuilder.withTransport(new LocalTransport(localRegistry)).build();
// Construct the underlying RaftServer. The server should have been configured with a CombinedTransport
// that facilitates the local client connecting directly to the server.
RaftServer server = serverBuilder.withTransport(new CombinedTransport(new LocalTransport(localRegistry), transport))
.withStateMachine(new ResourceManager()).build();
return new CopycatReplica(client, server, transport);
}
}
}