forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZKMetadataStore.java
451 lines (409 loc) · 18.7 KB
/
ZKMetadataStore.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/**
* 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.
*/
package org.apache.pulsar.metadata.impl;
import com.google.common.annotations.VisibleForTesting;
import java.io.File;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy;
import org.apache.pulsar.common.util.FutureUtil;
import org.apache.pulsar.metadata.api.GetResult;
import org.apache.pulsar.metadata.api.MetadataStoreConfig;
import org.apache.pulsar.metadata.api.MetadataStoreException;
import org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException;
import org.apache.pulsar.metadata.api.MetadataStoreException.BadVersionException;
import org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException;
import org.apache.pulsar.metadata.api.MetadataStoreLifecycle;
import org.apache.pulsar.metadata.api.Notification;
import org.apache.pulsar.metadata.api.NotificationType;
import org.apache.pulsar.metadata.api.Stat;
import org.apache.pulsar.metadata.api.extended.CreateOption;
import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended;
import org.apache.pulsar.metadata.api.extended.SessionEvent;
import org.apache.zookeeper.AddWatchMode;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.KeeperException.Code;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
@Slf4j
public class ZKMetadataStore extends AbstractMetadataStore implements MetadataStoreExtended, MetadataStoreLifecycle {
private final String metadataURL;
private final MetadataStoreConfig metadataStoreConfig;
private final boolean isZkManaged;
private final ZooKeeper zkc;
private Optional<ZKSessionWatcher> sessionWatcher;
public ZKMetadataStore(String metadataURL, MetadataStoreConfig metadataStoreConfig, boolean enableSessionWatcher)
throws MetadataStoreException {
try {
this.metadataURL = metadataURL;
this.metadataStoreConfig = metadataStoreConfig;
isZkManaged = true;
zkc = PulsarZooKeeperClient.newBuilder().connectString(metadataURL)
.connectRetryPolicy(new BoundExponentialBackoffRetryPolicy(100, 60_000, Integer.MAX_VALUE))
.allowReadOnlyMode(metadataStoreConfig.isAllowReadOnlyOperations())
.sessionTimeoutMs(metadataStoreConfig.getSessionTimeoutMillis())
.watchers(Collections.singleton(event -> {
if (sessionWatcher != null) {
sessionWatcher.ifPresent(sw -> sw.process(event));
}
}))
.build();
zkc.addWatch("/", this::handleWatchEvent, AddWatchMode.PERSISTENT_RECURSIVE);
if (enableSessionWatcher) {
sessionWatcher = Optional.of(new ZKSessionWatcher(zkc, this::receivedSessionEvent));
} else {
sessionWatcher = Optional.empty();
}
} catch (Throwable t) {
throw new MetadataStoreException(t);
}
}
@VisibleForTesting
@SneakyThrows
public ZKMetadataStore(ZooKeeper zkc) {
this.metadataURL = null;
this.metadataStoreConfig = null;
this.isZkManaged = false;
this.zkc = zkc;
this.sessionWatcher = Optional.of(new ZKSessionWatcher(zkc, this::receivedSessionEvent));
zkc.addWatch("/", this::handleWatchEvent, AddWatchMode.PERSISTENT_RECURSIVE);
}
@Override
protected void receivedSessionEvent(SessionEvent event) {
if (event == SessionEvent.SessionReestablished) {
// Recreate the persistent watch on the new session
zkc.addWatch("/", this::handleWatchEvent, AddWatchMode.PERSISTENT_RECURSIVE,
(rc, path, ctx) -> {
if (rc == Code.OK.intValue()) {
super.receivedSessionEvent(event);
} else {
log.error("Failed to recreate persistent watch on ZooKeeper: {}", Code.get(rc));
sessionWatcher.ifPresent(ZKSessionWatcher::setSessionInvalid);
// On the reconnectable client, mark the session as expired to trigger a new reconnect and
// we will have the chance to set the watch again.
if (zkc instanceof PulsarZooKeeperClient) {
((PulsarZooKeeperClient) zkc).process(
new WatchedEvent(Watcher.Event.EventType.None,
Watcher.Event.KeeperState.Expired,
null));
}
}
}, null);
} else {
super.receivedSessionEvent(event);
}
}
@Override
public CompletableFuture<Optional<GetResult>> storeGet(String path) {
CompletableFuture<Optional<GetResult>> future = new CompletableFuture<>();
try {
zkc.getData(path, null, (rc, path1, ctx, data, stat) -> {
execute(() -> {
Code code = Code.get(rc);
if (code == Code.OK) {
future.complete(Optional.of(new GetResult(data, getStat(path1, stat))));
} else if (code == Code.NONODE) {
future.complete(Optional.empty());
} else {
future.completeExceptionally(getException(code, path));
}
}, future);
}, null);
} catch (Throwable t) {
future.completeExceptionally(new MetadataStoreException(t));
}
return future;
}
@Override
public CompletableFuture<List<String>> getChildrenFromStore(String path) {
CompletableFuture<List<String>> future = new CompletableFuture<>();
try {
zkc.getChildren(path, null, (rc, path1, ctx, children) -> {
execute(() -> {
Code code = Code.get(rc);
if (code == Code.OK) {
Collections.sort(children);
future.complete(children);
} else if (code == Code.NONODE) {
// Z-node does not exist
future.complete(Collections.emptyList());
} else {
future.completeExceptionally(getException(code, path));
}
}, future);
}, null);
} catch (Throwable t) {
future.completeExceptionally(new MetadataStoreException(t));
}
return future;
}
@Override
public CompletableFuture<Boolean> existsFromStore(String path) {
CompletableFuture<Boolean> future = new CompletableFuture<>();
try {
zkc.exists(path, null, (rc, path1, ctx, stat) -> {
execute(() -> {
Code code = Code.get(rc);
if (code == Code.OK) {
future.complete(true);
} else if (code == Code.NONODE) {
future.complete(false);
} else {
future.completeExceptionally(getException(code, path));
}
}, future);
}, future);
} catch (Throwable t) {
future.completeExceptionally(new MetadataStoreException(t));
}
return future;
}
@Override
protected CompletableFuture<Stat> storePut(String path, byte[] value, Optional<Long> optExpectedVersion,
EnumSet<CreateOption> options) {
boolean hasVersion = optExpectedVersion.isPresent();
int expectedVersion = optExpectedVersion.orElse(-1L).intValue();
CompletableFuture<Stat> future = new CompletableFuture<>();
try {
if (hasVersion && expectedVersion == -1) {
CreateMode createMode = getCreateMode(options);
asyncCreateFullPathOptimistic(zkc, path, value, createMode, (rc, path1, ctx, name) -> {
execute(() -> {
Code code = Code.get(rc);
if (code == Code.OK) {
future.complete(new Stat(name, 0, 0, 0, createMode.isEphemeral(), true));
} else if (code == Code.NODEEXISTS) {
// We're emulating a request to create node, so the version is invalid
future.completeExceptionally(getException(Code.BADVERSION, path));
} else {
future.completeExceptionally(getException(code, path));
}
}, future);
});
} else {
zkc.setData(path, value, expectedVersion, (rc, path1, ctx, stat) -> {
execute(() -> {
Code code = Code.get(rc);
if (code == Code.OK) {
future.complete(getStat(path1, stat));
} else if (code == Code.NONODE) {
if (hasVersion) {
// We're emulating here a request to update or create the znode, depending on
// the version
future.completeExceptionally(getException(Code.BADVERSION, path));
} else {
// The z-node does not exist, let's create it first
put(path, value, Optional.of(-1L)).thenAccept(s -> future.complete(s))
.exceptionally(ex -> {
future.completeExceptionally(ex.getCause());
return null;
});
}
} else {
future.completeExceptionally(getException(code, path));
}
}, future);
}, null);
}
} catch (Throwable t) {
future.completeExceptionally(new MetadataStoreException(t));
}
return future;
}
@Override
protected CompletableFuture<Void> storeDelete(String path, Optional<Long> optExpectedVersion) {
int expectedVersion = optExpectedVersion.orElse(-1L).intValue();
CompletableFuture<Void> future = new CompletableFuture<>();
try {
zkc.delete(path, expectedVersion, (rc, path1, ctx) -> {
execute(() -> {
Code code = Code.get(rc);
if (code == Code.OK) {
future.complete(null);
} else {
future.completeExceptionally(getException(code, path));
}
}, future);
}, null);
} catch (Throwable t) {
future.completeExceptionally(new MetadataStoreException(t));
}
return future;
}
@Override
public void close() throws Exception {
if (isZkManaged) {
zkc.close();
}
if (sessionWatcher.isPresent()) {
sessionWatcher.get().close();
}
super.close();
}
private Stat getStat(String path, org.apache.zookeeper.data.Stat zkStat) {
return new Stat(path, zkStat.getVersion(), zkStat.getCtime(), zkStat.getMtime(),
zkStat.getEphemeralOwner() != -1,
zkStat.getEphemeralOwner() == zkc.getSessionId());
}
private static MetadataStoreException getException(Code code, String path) {
KeeperException ex = KeeperException.create(code, path);
switch (code) {
case BADVERSION:
return new BadVersionException(ex);
case NONODE:
return new NotFoundException(ex);
case NODEEXISTS:
return new AlreadyExistsException(ex);
default:
return new MetadataStoreException(ex);
}
}
private void handleWatchEvent(WatchedEvent event) {
if (log.isDebugEnabled()) {
log.debug("Received ZK watch : {}", event);
}
String path = event.getPath();
if (path == null) {
// Ignore Session events
return;
}
String parent = parent(path);
Notification childrenChangedNotification = null;
NotificationType type;
switch (event.getType()) {
case NodeCreated:
type = NotificationType.Created;
if (parent != null) {
childrenChangedNotification = new Notification(NotificationType.ChildrenChanged, parent);
}
break;
case NodeDataChanged:
type = NotificationType.Modified;
break;
case NodeChildrenChanged:
type = NotificationType.ChildrenChanged;
break;
case NodeDeleted:
type = NotificationType.Deleted;
if (parent != null) {
childrenChangedNotification = new Notification(NotificationType.ChildrenChanged, parent);
}
break;
default:
return;
}
receivedNotification(new Notification(type, event.getPath()));
if (childrenChangedNotification != null) {
receivedNotification(childrenChangedNotification);
}
}
private static CreateMode getCreateMode(EnumSet<CreateOption> options) {
if (options.contains(CreateOption.Ephemeral)) {
if (options.contains(CreateOption.Sequential)) {
return CreateMode.EPHEMERAL_SEQUENTIAL;
} else {
return CreateMode.EPHEMERAL;
}
} else if (options.contains(CreateOption.Sequential)) {
return CreateMode.PERSISTENT_SEQUENTIAL;
} else {
return CreateMode.PERSISTENT;
}
}
public long getZkSessionId() {
return zkc.getSessionId();
}
public ZooKeeper getZkClient() {
return zkc;
}
@Override
public CompletableFuture<Void> initializeCluster() {
if (this.metadataURL == null) {
return FutureUtil.failedFuture(new MetadataStoreException("metadataURL is not set"));
}
if (this.metadataStoreConfig == null) {
return FutureUtil.failedFuture(new MetadataStoreException("metadataStoreConfig is not set"));
}
int chrootIndex = metadataURL.indexOf("/");
if (chrootIndex > 0) {
String chrootPath = metadataURL.substring(chrootIndex);
String zkConnectForChrootCreation = metadataURL.substring(0, chrootIndex);
try (ZooKeeper chrootZk = PulsarZooKeeperClient.newBuilder()
.connectString(zkConnectForChrootCreation)
.sessionTimeoutMs(metadataStoreConfig.getSessionTimeoutMillis())
.connectRetryPolicy(
new BoundExponentialBackoffRetryPolicy(metadataStoreConfig.getSessionTimeoutMillis(),
metadataStoreConfig.getSessionTimeoutMillis(), 0))
.build()) {
if (chrootZk.exists(chrootPath, false) == null) {
createFullPathOptimistic(chrootZk, chrootPath, new byte[0], CreateMode.PERSISTENT);
log.info("Created zookeeper chroot path {} successfully", chrootPath);
}
} catch (Exception e) {
return FutureUtil.failedFuture(e);
}
}
return CompletableFuture.completedFuture(null);
}
private static void asyncCreateFullPathOptimistic(final ZooKeeper zk, final String originalPath, final byte[] data,
final CreateMode createMode,
final AsyncCallback.StringCallback callback) {
zk.create(originalPath, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode, (rc, path, ctx, name) -> {
if (rc != Code.NONODE.intValue()) {
callback.processResult(rc, path, ctx, name);
} else {
String parent = (new File(originalPath)).getParent().replace("\\", "/");
// Create parent nodes as "CONTAINER" so that ZK will automatically delete them when they're empty
asyncCreateFullPathOptimistic(zk, parent, new byte[0], CreateMode.CONTAINER,
(rc1, path1, ctx1, name1) -> {
if (rc1 != Code.OK.intValue() && rc1 != Code.NODEEXISTS.intValue()) {
callback.processResult(rc1, path1, ctx1, name1);
} else {
asyncCreateFullPathOptimistic(zk, originalPath, data,
createMode, callback);
}
});
}
}, null);
}
private static void createFullPathOptimistic(ZooKeeper zkc, String path, byte[] data, CreateMode createMode)
throws KeeperException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger rc = new AtomicInteger(Code.OK.intValue());
asyncCreateFullPathOptimistic(zkc, path, data, createMode, (rc2, path1, ctx, name) -> {
rc.set(rc2);
latch.countDown();
});
latch.await();
if (rc.get() != Code.OK.intValue()) {
throw KeeperException.create(Code.get(rc.get()));
}
}
}