/
LocalBookkeeperEnsemble.java
330 lines (288 loc) · 12.9 KB
/
LocalBookkeeperEnsemble.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/**
* This file is derived from LocalBookkeeperEnsemble from Apache BookKeeper
* http://bookkeeper.apache.org
*/
package org.apache.pulsar.zookeeper;
import static org.apache.commons.io.FileUtils.cleanDirectory;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.apache.bookkeeper.bookie.BookieException.InvalidCookieException;
import org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorage;
import org.apache.bookkeeper.conf.ServerConfiguration;
import org.apache.bookkeeper.proto.BookieServer;
import org.apache.bookkeeper.stats.NullStatsLogger;
import org.apache.bookkeeper.util.MathUtils;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.server.NIOServerCnxnFactory;
import org.apache.zookeeper.server.ZooKeeperServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LocalBookkeeperEnsemble {
protected static final Logger LOG = LoggerFactory.getLogger(LocalBookkeeperEnsemble.class);
public static final int CONNECTION_TIMEOUT = 30000;
int numberOfBookies;
private boolean clearOldData = false;
public LocalBookkeeperEnsemble(int numberOfBookies, int zkPort, int bkBasePort) {
this(numberOfBookies, zkPort, bkBasePort, null, null, true);
}
public LocalBookkeeperEnsemble(int numberOfBookies, int zkPort, int bkBasePort, String zkDataDirName,
String bkDataDirName, boolean clearOldData) {
this(numberOfBookies, zkPort, bkBasePort, zkDataDirName, bkDataDirName, clearOldData, null);
}
public LocalBookkeeperEnsemble(int numberOfBookies, int zkPort, int bkBasePort, String zkDataDirName,
String bkDataDirName, boolean clearOldData, String advertisedAddress) {
this.numberOfBookies = numberOfBookies;
this.HOSTPORT = "127.0.0.1:" + zkPort;
this.ZooKeeperDefaultPort = zkPort;
this.initialPort = bkBasePort;
this.zkDataDirName = zkDataDirName;
this.bkDataDirName = bkDataDirName;
this.clearOldData = clearOldData;
this.advertisedAddress = null == advertisedAddress ? "127.0.0.1" : advertisedAddress;
LOG.info("Running {} bookie(s) and advertised them at {}.", this.numberOfBookies, advertisedAddress);
}
private final String HOSTPORT;
private final String advertisedAddress;
NIOServerCnxnFactory serverFactory;
ZooKeeperServer zks;
ZooKeeper zkc;
final int ZooKeeperDefaultPort;
static int zkSessionTimeOut = 5000;
String zkDataDirName;
// BookKeeper variables
String bkDataDirName;
BookieServer bs[];
ServerConfiguration bsConfs[];
Integer initialPort = 5000;
/**
* @param args
*/
private void runZookeeper(int maxCC) throws IOException {
// create a ZooKeeper server(dataDir, dataLogDir, port)
LOG.info("Starting ZK server");
// ServerStats.registerAsConcrete();
// ClientBase.setupTestEnv();
File zkDataDir = isNotBlank(zkDataDirName) ? Files.createDirectories(Paths.get(zkDataDirName)).toFile()
: Files.createTempDirectory("zktest").toFile();
if (this.clearOldData) {
cleanDirectory(zkDataDir);
}
try {
zks = new ZooKeeperServer(zkDataDir, zkDataDir, ZooKeeperServer.DEFAULT_TICK_TIME);
serverFactory = new NIOServerCnxnFactory();
serverFactory.configure(new InetSocketAddress(ZooKeeperDefaultPort), maxCC);
serverFactory.startup(zks);
} catch (Exception e) {
// TODO Auto-generated catch block
LOG.error("Exception while instantiating ZooKeeper", e);
}
boolean b = waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT);
LOG.info("ZooKeeper server up: {}", b);
LOG.debug("Local ZK started (port: {}, data_directory: {})", ZooKeeperDefaultPort, zkDataDir.getAbsolutePath());
}
private void initializeZookeper() throws IOException {
LOG.info("Instantiate ZK Client");
// initialize the zk client with values
try {
ZKConnectionWatcher zkConnectionWatcher = new ZKConnectionWatcher();
zkc = new ZooKeeper(HOSTPORT, zkSessionTimeOut, zkConnectionWatcher);
zkConnectionWatcher.waitForConnection();
if (zkc.exists("/ledgers", false) == null) {
zkc.create("/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
if (zkc.exists("/ledgers/available", false) == null) {
zkc.create("/ledgers/available", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
// No need to create an entry for each requested bookie anymore as the
// BookieServers will register themselves with ZooKeeper on startup.
} catch (KeeperException e) {
// TODO Auto-generated catch block
LOG.error("Exception while creating znodes", e);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
LOG.error("Interrupted while creating znodes", e);
}
}
private void runBookies(ServerConfiguration baseConf) throws Exception {
LOG.info("Starting Bookie(s)");
// Create Bookie Servers (B1, B2, B3)
bs = new BookieServer[numberOfBookies];
bsConfs = new ServerConfiguration[numberOfBookies];
for (int i = 0; i < numberOfBookies; i++) {
File bkDataDir = isNotBlank(bkDataDirName)
? Files.createDirectories(Paths.get(bkDataDirName + Integer.toString(i))).toFile()
: Files.createTempDirectory("bk" + Integer.toString(i) + "test").toFile();
if (this.clearOldData) {
cleanDirectory(bkDataDir);
}
bsConfs[i] = new ServerConfiguration(baseConf);
// override settings
bsConfs[i].setBookiePort(initialPort + i);
bsConfs[i].setZkServers("127.0.0.1:" + ZooKeeperDefaultPort);
bsConfs[i].setJournalDirName(bkDataDir.getPath());
bsConfs[i].setLedgerDirNames(new String[] { bkDataDir.getPath() });
bsConfs[i].setAllowLoopback(true);
bsConfs[i].setGcWaitTime(60000);
try {
bs[i] = new BookieServer(bsConfs[i], NullStatsLogger.INSTANCE);
} catch (InvalidCookieException e) {
// InvalidCookieException can happen if the machine IP has changed
// Since we are running here a local bookie that is always accessed
// from localhost, we can ignore the error
for (String path : zkc.getChildren("/ledgers/cookies", false)) {
zkc.delete("/ledgers/cookies/" + path, -1);
}
// Also clean the on-disk cookie
new File(new File(bkDataDir, "current"), "VERSION").delete();
// Retry to start the bookie after cleaning the old left cookie
bs[i] = new BookieServer(bsConfs[i], NullStatsLogger.INSTANCE);
}
bs[i].start();
LOG.debug("Local BK[{}] started (port: {}, data_directory: {})", i, initialPort + i,
bkDataDir.getAbsolutePath());
}
}
public void start() throws Exception {
LOG.debug("Local ZK/BK starting ...");
ServerConfiguration conf = new ServerConfiguration();
conf.setLedgerManagerFactoryClassName("org.apache.bookkeeper.meta.HierarchicalLedgerManagerFactory");
// Use minimal configuration requiring less memory for unit tests
conf.setLedgerStorageClass(DbLedgerStorage.class.getName());
conf.setProperty("dbStorage_writeCacheMaxSizeMb", 2);
conf.setProperty("dbStorage_readAheadCacheMaxSizeMb", 1);
conf.setProperty("dbStorage_rocksDB_writeBufferSizeMB", 1);
conf.setProperty("dbStorage_rocksDB_blockCacheSize", 1024 * 1024);
conf.setFlushInterval(60000);
conf.setProperty("journalMaxGroupWaitMSec", 0L);
runZookeeper(1000);
initializeZookeper();
runBookies(conf);
}
public void startStandalone() throws Exception {
LOG.debug("Local ZK/BK starting ...");
ServerConfiguration conf = new ServerConfiguration();
conf.setLedgerManagerFactoryClassName("org.apache.bookkeeper.meta.HierarchicalLedgerManagerFactory");
conf.setLedgerStorageClass(DbLedgerStorage.class.getName());
conf.setProperty("dbStorage_writeCacheMaxSizeMb", 256);
conf.setProperty("dbStorage_readAheadCacheMaxSizeMb", 64);
conf.setFlushInterval(60000);
conf.setProperty("journalMaxGroupWaitMSec", 1L);
conf.setAdvertisedAddress(advertisedAddress);
runZookeeper(1000);
initializeZookeper();
runBookies(conf);
}
public void stop() throws Exception {
LOG.debug("Local ZK/BK stopping ...");
for (BookieServer bookie : bs) {
bookie.shutdown();
}
zkc.close();
zks.shutdown();
serverFactory.shutdown();
LOG.debug("Local ZK/BK stopped");
}
/* Watching SyncConnected event from ZooKeeper */
public static class ZKConnectionWatcher implements Watcher {
private CountDownLatch clientConnectLatch = new CountDownLatch(1);
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
clientConnectLatch.countDown();
}
}
// Waiting for the SyncConnected event from the ZooKeeper server
public void waitForConnection() throws IOException {
try {
if (!clientConnectLatch.await(zkSessionTimeOut, TimeUnit.MILLISECONDS)) {
throw new IOException("Couldn't connect to zookeeper server");
}
} catch (InterruptedException e) {
throw new IOException("Interrupted when connecting to zookeeper server", e);
}
}
}
public static boolean waitForServerUp(String hp, long timeout) {
long start = MathUtils.now();
String split[] = hp.split(":");
String host = split[0];
int port = Integer.parseInt(split[1]);
while (true) {
try {
Socket sock = new Socket(host, port);
BufferedReader reader = null;
try {
OutputStream outstream = sock.getOutputStream();
outstream.write("stat".getBytes());
outstream.flush();
reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String line = reader.readLine();
if (line != null && line.startsWith("Zookeeper version:")) {
LOG.info("Server UP");
return true;
}
} finally {
sock.close();
if (reader != null) {
reader.close();
}
}
} catch (IOException e) {
// ignore as this is expected
LOG.info("server " + hp + " not up " + e);
}
if (MathUtils.now() > start + timeout) {
break;
}
try {
Thread.sleep(250);
} catch (InterruptedException e) {
// ignore
}
}
return false;
}
public ZooKeeper getZkClient() {
return zkc;
}
public ZooKeeperServer getZkServer() {
return zks;
}
public BookieServer[] getBookies() {
return bs;
}
}