You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2018/02/28 02:23:07 UTC
[GitHub] merlimat closed pull request #1292: Switched to BookKeeper-4.7.0-SNAPSHOT + Adjustements
merlimat closed pull request #1292: Switched to BookKeeper-4.7.0-SNAPSHOT + Adjustements
URL: https://github.com/apache/incubator-pulsar/pull/1292
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/all/pom.xml b/all/pom.xml
index 56d4a68ee..0c02d51ef 100644
--- a/all/pom.xml
+++ b/all/pom.xml
@@ -94,16 +94,6 @@
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
- <dependency>
- <groupId>org.apache.bookkeeper.stats</groupId>
- <artifactId>datasketches-metrics-provider</artifactId>
- <exclusions>
- <exclusion>
- <groupId>io.netty</groupId>
- <artifactId>netty-common</artifactId></exclusion>
- </exclusions>
- </dependency>
-
<dependency>
<groupId>org.apache.bookkeeper.stats</groupId>
<artifactId>prometheus-metrics-provider</artifactId>
diff --git a/buildtools/src/main/resources/log4j2.yml b/buildtools/src/main/resources/log4j2.yml
index 4295d723c..94ad627c6 100644
--- a/buildtools/src/main/resources/log4j2.yml
+++ b/buildtools/src/main/resources/log4j2.yml
@@ -32,11 +32,10 @@ Configuration:
Loggers:
Root:
- level: info
+ level: warn
AppenderRef:
- ref: Console
Logger:
- name: org.apache.bookkeeper.mledger
+ name: org.apache.pulsar
level: info
- additivity: false
diff --git a/managed-ledger/pom.xml b/managed-ledger/pom.xml
index d3570c6db..e8f4daecb 100644
--- a/managed-ledger/pom.xml
+++ b/managed-ledger/pom.xml
@@ -35,7 +35,7 @@
<dependencies>
<dependency>
<groupId>org.apache.bookkeeper</groupId>
- <artifactId>bookkeeper-server</artifactId>
+ <artifactId>bookkeeper-server-shaded</artifactId>
<version>${bookkeeper.version}</version>
<exclusions>
<exclusion>
@@ -53,6 +53,20 @@
</exclusions>
</dependency>
+ <dependency>
+ <groupId>org.apache.bookkeeper</groupId>
+ <artifactId>bookkeeper-server</artifactId>
+ <version>${bookkeeper.version}</version>
+ <classifier>tests</classifier>
+ <scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-common</artifactId>
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java
index 39ac5c599..45fcf9036 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java
@@ -269,7 +269,8 @@ protected void recoverFromLedger(final ManagedCursorInfo info, final VoidCallbac
}
// Read the last entry in the ledger
- lh.asyncReadLastEntry((rc1, lh1, seq, ctx1) -> {
+ long lastEntryInLedger = lh.getLastAddConfirmed();
+ lh.asyncReadEntries(lastEntryInLedger, lastEntryInLedger, (rc1, lh1, seq, ctx1) -> {
if (log.isDebugEnabled()) {
log.debug("[{}} readComplete rc={} entryId={}", ledger.getName(), rc1, lh1.getLastAddConfirmed());
}
@@ -1913,6 +1914,7 @@ void internalFlushPendingMarkDeletes() {
void createNewMetadataLedger(final VoidCallback callback) {
ledger.mbean.startCursorLedgerCreateOp();
+
bookkeeper.asyncCreateLedger(config.getMetadataEnsemblesize(), config.getMetadataWriteQuorumSize(),
config.getMetadataAckQuorumSize(), config.getDigestType(), config.getPassword(), (rc, lh, ctx) -> {
ledger.getExecutor().submit(safeRun(() -> {
@@ -1974,7 +1976,7 @@ public void deleteComplete(int rc, Object ctx) {
}
});
}));
- }, null);
+ }, null, Collections.emptyMap());
}
private List<LongProperty> buildPropertiesMap(Map<String, Long> properties) {
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerFactoryImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerFactoryImpl.java
index cf9058eeb..4c0d3d703 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerFactoryImpl.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerFactoryImpl.java
@@ -37,6 +37,7 @@
import java.util.concurrent.TimeUnit;
import org.apache.bookkeeper.client.BKException;
import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.mledger.AsyncCallbacks;
import org.apache.bookkeeper.mledger.AsyncCallbacks.ManagedLedgerInfoCallback;
@@ -78,7 +79,8 @@
private final ManagedLedgerFactoryConfig config;
protected final ScheduledExecutorService executor = Executors.newScheduledThreadPool(16,
new DefaultThreadFactory("bookkeeper-ml"));
- private final OrderedSafeExecutor orderedExecutor = new OrderedSafeExecutor(16, "bookkeeper-ml-workers");
+ private final OrderedScheduler orderedExecutor = OrderedScheduler.newSchedulerBuilder().numThreads(16)
+ .name("bookkeeper-ml-workers").build();
protected final ManagedLedgerFactoryMBeanImpl mbean;
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java
index 89d3476ea..ac030e52b 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java
@@ -20,17 +20,9 @@
import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.Math.min;
-import static org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException;
import static org.apache.bookkeeper.mledger.util.SafeRun.safeRun;
-import com.google.common.collect.BoundType;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Queues;
-import com.google.common.collect.Range;
-import com.google.common.util.concurrent.RateLimiter;
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -47,11 +39,13 @@
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+
import org.apache.bookkeeper.client.AsyncCallback.CreateCallback;
import org.apache.bookkeeper.client.AsyncCallback.OpenCallback;
import org.apache.bookkeeper.client.BKException;
import org.apache.bookkeeper.client.BookKeeper;
import org.apache.bookkeeper.client.LedgerHandle;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback;
import org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback;
import org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback;
@@ -82,14 +76,23 @@
import org.apache.bookkeeper.mledger.util.CallbackMutex;
import org.apache.bookkeeper.mledger.util.Futures;
import org.apache.bookkeeper.mledger.util.Pair;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
-import org.apache.bookkeeper.util.UnboundArrayBlockingQueue;
import org.apache.pulsar.common.api.Commands;
import org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata;
import org.apache.pulsar.common.util.collections.ConcurrentLongHashMap;
+import org.apache.pulsar.common.util.collections.GrowableArrayBlockingQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.collect.BoundType;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Queues;
+import com.google.common.collect.Range;
+import com.google.common.util.concurrent.RateLimiter;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+
public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
private final static long MegaByte = 1024 * 1024;
@@ -187,7 +190,7 @@
private volatile State state = null;
private final ScheduledExecutorService scheduledExecutor;
- private final OrderedSafeExecutor executor;
+ private final OrderedScheduler executor;
final ManagedLedgerFactoryImpl factory;
protected final ManagedLedgerMBeanImpl mbean;
@@ -195,12 +198,12 @@
* Queue of pending entries to be added to the managed ledger. Typically entries are queued when a new ledger is
* created asynchronously and hence there is no ready ledger to write into.
*/
- final Queue<OpAddEntry> pendingAddEntries = new UnboundArrayBlockingQueue<>();
+ final GrowableArrayBlockingQueue<OpAddEntry> pendingAddEntries = new GrowableArrayBlockingQueue<>();
// //////////////////////////////////////////////////////////////////////
public ManagedLedgerImpl(ManagedLedgerFactoryImpl factory, BookKeeper bookKeeper, MetaStore store,
- ManagedLedgerConfig config, ScheduledExecutorService scheduledExecutor, OrderedSafeExecutor orderedExecutor,
+ ManagedLedgerConfig config, ScheduledExecutorService scheduledExecutor, OrderedScheduler orderedExecutor,
final String name) {
this.factory = factory;
this.bookKeeper = bookKeeper;
@@ -352,7 +355,7 @@ public void operationFailed(MetaStoreException e) {
// Save it back to ensure all nodes exist
store.asyncUpdateLedgerIds(name, getManagedLedgerInfo(), ledgersStat, storeLedgersCb);
}));
- }, null);
+ }, null, Collections.emptyMap());
}
private void initializeCursors(final ManagedLedgerInitializeLedgerCallback callback) {
@@ -512,7 +515,8 @@ public synchronized void asyncAddEntry(ByteBuf buffer, AddEntryCallback callback
this.lastLedgerCreationInitiationTimestamp = System.nanoTime();
mbean.startDataLedgerCreateOp();
bookKeeper.asyncCreateLedger(config.getEnsembleSize(), config.getWriteQuorumSize(),
- config.getAckQuorumSize(), config.getDigestType(), config.getPassword(), this, ctx);
+ config.getAckQuorumSize(), config.getDigestType(), config.getPassword(), this, ctx,
+ Collections.emptyMap());
}
} else {
checkArgument(state == State.LedgerOpened, "ledger=%s is not opened", state);
@@ -1168,7 +1172,7 @@ public synchronized void updateLedgersIdsComplete(Stat stat) {
}
// Process all the pending addEntry requests
- for (OpAddEntry op : pendingAddEntries) {
+ for (OpAddEntry op : pendingAddEntries.toList()) {
op.setLedger(currentLedger);
++currentLedgerEntries;
currentLedgerSize += op.data.readableBytes();
@@ -1238,7 +1242,8 @@ synchronized void ledgerClosed(final LedgerHandle lh) {
this.lastLedgerCreationInitiationTimestamp = System.nanoTime();
mbean.startDataLedgerCreateOp();
bookKeeper.asyncCreateLedger(config.getEnsembleSize(), config.getWriteQuorumSize(),
- config.getAckQuorumSize(), config.getDigestType(), config.getPassword(), this, null);
+ config.getAckQuorumSize(), config.getDigestType(), config.getPassword(), this, null,
+ Collections.emptyMap());
}
}
@@ -2087,7 +2092,7 @@ ScheduledExecutorService getScheduledExecutor() {
return scheduledExecutor;
}
- OrderedSafeExecutor getExecutor() {
+ OrderedScheduler getExecutor() {
return executor;
}
diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/MetaStoreImplZookeeper.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/MetaStoreImplZookeeper.java
index ae3e12827..7a7697531 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/MetaStoreImplZookeeper.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/MetaStoreImplZookeeper.java
@@ -20,18 +20,15 @@
import static org.apache.bookkeeper.mledger.util.SafeRun.safeRun;
-import com.google.common.base.Charsets;
-import com.google.protobuf.InvalidProtocolBufferException;
-import com.google.protobuf.TextFormat;
-import com.google.protobuf.TextFormat.ParseException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
+
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.bookkeeper.mledger.ManagedLedgerException.BadVersionException;
import org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException;
import org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedCursorInfo;
import org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
import org.apache.bookkeeper.util.ZkUtils;
import org.apache.zookeeper.AsyncCallback.StringCallback;
import org.apache.zookeeper.CreateMode;
@@ -43,6 +40,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.base.Charsets;
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.TextFormat;
+import com.google.protobuf.TextFormat.ParseException;
+
@SuppressWarnings("checkstyle:javadoctype")
public class MetaStoreImplZookeeper implements MetaStore {
@@ -53,7 +55,7 @@
private static final String prefix = prefixName + "/";
private final ZooKeeper zk;
- private final OrderedSafeExecutor executor;
+ private final OrderedScheduler executor;
private static class ZKStat implements Stat {
private final int version;
@@ -88,7 +90,7 @@ public long getModificationTimestamp() {
}
}
- public MetaStoreImplZookeeper(ZooKeeper zk, OrderedSafeExecutor executor)
+ public MetaStoreImplZookeeper(ZooKeeper zk, OrderedScheduler executor)
throws Exception {
this.zk = zk;
this.executor = executor;
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/client/BookKeeperTestClient.java b/managed-ledger/src/test/java/org/apache/bookkeeper/client/BookKeeperTestClient.java
index b14d984bd..184fc7f4c 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/client/BookKeeperTestClient.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/client/BookKeeperTestClient.java
@@ -27,7 +27,7 @@
* Test BookKeeperClient which allows access to members we don't wish to expose in the public API.
*/
public class BookKeeperTestClient extends BookKeeper {
- public BookKeeperTestClient(ClientConfiguration conf) throws IOException, InterruptedException, KeeperException {
+ public BookKeeperTestClient(ClientConfiguration conf) throws IOException, InterruptedException, BKException {
super(conf);
}
@@ -45,7 +45,7 @@ public ClientConfiguration getConf() {
* @throws InterruptedException
* @throws KeeperException
*/
- public void readBookiesBlocking() throws InterruptedException, KeeperException {
- bookieWatcher.readBookiesBlocking();
+ public void readBookiesBlocking() throws InterruptedException, BKException {
+ bookieWatcher.initialBlockingBookieRead();
}
}
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/client/MockBookKeeper.java b/managed-ledger/src/test/java/org/apache/bookkeeper/client/MockBookKeeper.java
deleted file mode 100644
index 0da3369c0..000000000
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/client/MockBookKeeper.java
+++ /dev/null
@@ -1,270 +0,0 @@
-/**
- * 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.bookkeeper.client;
-
-import io.netty.channel.oio.OioEventLoopGroup;
-import io.netty.util.concurrent.DefaultThreadFactory;
-import java.util.Arrays;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.atomic.AtomicLong;
-import org.apache.bookkeeper.client.AsyncCallback.CreateCallback;
-import org.apache.bookkeeper.client.AsyncCallback.DeleteCallback;
-import org.apache.bookkeeper.client.AsyncCallback.OpenCallback;
-import org.apache.bookkeeper.conf.ClientConfiguration;
-import org.apache.zookeeper.ZooKeeper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Test BookKeeperClient which allows access to members we don't wish to expose in the public API.
- */
-public class MockBookKeeper extends BookKeeper {
-
- final ExecutorService executor = Executors.newFixedThreadPool(1, new DefaultThreadFactory("mock-bookkeeper"));
-
- public ZooKeeper getZkHandle() {
- return super.getZkHandle();
- }
-
- public ClientConfiguration getConf() {
- return super.getConf();
- }
-
- Map<Long, MockLedgerHandle> ledgers = new ConcurrentHashMap<Long, MockLedgerHandle>();
- AtomicLong sequence = new AtomicLong(3);
- AtomicBoolean stopped = new AtomicBoolean(false);
- AtomicInteger stepsToFail = new AtomicInteger(-1);
- int failReturnCode = BKException.Code.OK;
- int nextFailReturnCode = BKException.Code.OK;
-
- public MockBookKeeper(ClientConfiguration conf, ZooKeeper zk) throws Exception {
- super(conf, zk, new OioEventLoopGroup());
- }
-
- public LedgerHandle createLedger(DigestType digestType, byte passwd[]) throws BKException {
- return createLedger(3, 2, digestType, passwd);
- }
-
- public LedgerHandle createLedger(int ensSize, int qSize, DigestType digestType, byte passwd[]) throws BKException {
- return createLedger(ensSize, qSize, qSize, digestType, passwd);
- }
-
- @Override
- public void asyncCreateLedger(int ensSize, int writeQuorumSize, int ackQuorumSize, final DigestType digestType,
- final byte[] passwd, final CreateCallback cb, final Object ctx) {
- if (stopped.get()) {
- cb.createComplete(BKException.Code.WriteException, null, ctx);
- return;
- }
-
- executor.execute(new Runnable() {
- public void run() {
- if (getProgrammedFailStatus()) {
- if (failReturnCode != BkTimeoutOperation) {
- cb.createComplete(failReturnCode, null, ctx);
- }
- return;
- }
-
- if (stopped.get()) {
- cb.createComplete(BKException.Code.WriteException, null, ctx);
- return;
- }
-
- try {
- long id = sequence.getAndIncrement();
- log.info("Creating ledger {}", id);
- MockLedgerHandle lh = new MockLedgerHandle(MockBookKeeper.this, id, digestType, passwd);
- ledgers.put(id, lh);
- cb.createComplete(0, lh, ctx);
- } catch (Throwable t) {
- }
- }
- });
- }
-
- @Override
- public LedgerHandle createLedger(int ensSize, int writeQuorumSize, int ackQuorumSize, DigestType digestType,
- byte[] passwd) throws BKException {
- checkProgrammedFail();
-
- if (stopped.get()) {
- throw BKException.create(BKException.Code.WriteException);
- }
-
- try {
- long id = sequence.getAndIncrement();
- log.info("Creating ledger {}", id);
- MockLedgerHandle lh = new MockLedgerHandle(this, id, digestType, passwd);
- ledgers.put(id, lh);
- return lh;
- } catch (Throwable t) {
- log.error("Exception:", t);
- return null;
- }
- }
-
- @Override
- public void asyncCreateLedger(int ensSize, int qSize, DigestType digestType, byte[] passwd, CreateCallback cb,
- Object ctx) {
- asyncCreateLedger(ensSize, qSize, qSize, digestType, passwd, cb, ctx);
- }
-
- @Override
- public void asyncOpenLedger(long lId, DigestType digestType, byte[] passwd, OpenCallback cb, Object ctx) {
- if (getProgrammedFailStatus()) {
- if (failReturnCode != BkTimeoutOperation) {
- cb.openComplete(failReturnCode, null, ctx);
- }
- return;
- }
-
- if (stopped.get()) {
- cb.openComplete(BKException.Code.WriteException, null, ctx);
- return;
- }
-
- MockLedgerHandle lh = ledgers.get(lId);
- if (lh == null) {
- cb.openComplete(BKException.Code.NoSuchLedgerExistsException, null, ctx);
- } else if (lh.digest != digestType) {
- cb.openComplete(BKException.Code.DigestMatchException, null, ctx);
- } else if (!Arrays.equals(lh.passwd, passwd)) {
- cb.openComplete(BKException.Code.UnauthorizedAccessException, null, ctx);
- } else {
- cb.openComplete(0, lh, ctx);
- }
- }
-
- @Override
- public void asyncOpenLedgerNoRecovery(long lId, DigestType digestType, byte[] passwd, OpenCallback cb, Object ctx) {
- asyncOpenLedger(lId, digestType, passwd, cb, ctx);
- }
-
- @Override
- public void asyncDeleteLedger(long lId, DeleteCallback cb, Object ctx) {
- if (getProgrammedFailStatus()) {
- if (failReturnCode != BkTimeoutOperation) {
- cb.deleteComplete(failReturnCode, ctx);
- }
- } else if (stopped.get()) {
- cb.deleteComplete(BKException.Code.WriteException, ctx);
- } else if (ledgers.containsKey(lId)) {
- ledgers.remove(lId);
- cb.deleteComplete(0, ctx);
- } else {
- cb.deleteComplete(BKException.Code.NoSuchLedgerExistsException, ctx);
- }
- }
-
- @Override
- public void deleteLedger(long lId) throws InterruptedException, BKException {
- checkProgrammedFail();
-
- if (stopped.get()) {
- throw BKException.create(BKException.Code.WriteException);
- }
-
- if (!ledgers.containsKey(lId)) {
- throw BKException.create(BKException.Code.NoSuchLedgerExistsException);
- }
-
- ledgers.remove(lId);
- }
-
- @Override
- public void close() throws InterruptedException, BKException {
- checkProgrammedFail();
- super.close();
- }
-
- public void shutdown() {
- try {
- super.close();
- } catch (Exception e) {
- }
- stopped.set(true);
- for (MockLedgerHandle ledger : ledgers.values()) {
- ledger.entries.clear();
- }
-
- ledgers.clear();
- executor.shutdownNow();
- }
-
- public boolean isStopped() {
- return stopped.get();
- }
-
- public Set<Long> getLedgers() {
- return ledgers.keySet();
- }
-
- void checkProgrammedFail() throws BKException {
- int steps = stepsToFail.getAndDecrement();
- log.debug("Steps to fail: {}", steps);
- if (steps <= 0) {
- if (failReturnCode != BKException.Code.OK) {
- int rc = failReturnCode;
- failReturnCode = nextFailReturnCode;
- nextFailReturnCode = BKException.Code.OK;
- throw BKException.create(rc);
- }
- }
- }
-
- boolean getProgrammedFailStatus() {
- int steps = stepsToFail.getAndDecrement();
- log.debug("Steps to fail: {}", steps);
- return steps == 0;
- }
-
- public void failNow(int rc) {
- failNow(rc, BKException.Code.OK);
- }
-
- public void failNow(int rc, int nextErrorCode) {
- failAfter(0, rc);
- }
-
- public void failAfter(int steps, int rc) {
- failAfter(steps, rc, BKException.Code.OK);
- }
-
- public void failAfter(int steps, int rc, int nextErrorCode) {
- stepsToFail.set(steps);
- failReturnCode = rc;
- this.nextFailReturnCode = nextErrorCode;
- }
-
- public void timeoutAfter(int steps) {
- stepsToFail.set(steps);
- failReturnCode = BkTimeoutOperation;
- }
-
- private static final int BkTimeoutOperation = 1000;
-
- private static final Logger log = LoggerFactory.getLogger(MockBookKeeper.class);
-}
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/client/MockLedgerEntry.java b/managed-ledger/src/test/java/org/apache/bookkeeper/client/MockLedgerEntry.java
deleted file mode 100644
index 4b39cfd44..000000000
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/client/MockLedgerEntry.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/**
- * 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.bookkeeper.client;
-
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
-import java.io.InputStream;
-
-public class MockLedgerEntry extends LedgerEntry {
-
- final long ledgerId;
- final long entryId;
- final byte[] data;
-
- public MockLedgerEntry(long ledgerId, long entryId, byte[] data) {
- super(ledgerId, entryId);
- this.ledgerId = ledgerId;
- this.entryId = entryId;
- this.data = data;
- }
-
- @Override
- public long getLedgerId() {
- return ledgerId;
- }
-
- @Override
- public long getEntryId() {
- return entryId;
- }
-
- @Override
- public long getLength() {
- return data.length;
- }
-
- @Override
- public byte[] getEntry() {
- return data;
- }
-
- @Override
- public ByteBuf getEntryBuffer() {
- return Unpooled.wrappedBuffer(data);
- }
-
- @Override
- public InputStream getEntryInputStream() {
- return null;
- }
-
-}
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/client/MockLedgerHandle.java b/managed-ledger/src/test/java/org/apache/bookkeeper/client/MockLedgerHandle.java
deleted file mode 100644
index 7aa3a63a7..000000000
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/client/MockLedgerHandle.java
+++ /dev/null
@@ -1,223 +0,0 @@
-/**
- * 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.bookkeeper.client;
-
-import com.google.common.collect.Lists;
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
-import java.security.GeneralSecurityException;
-import java.util.ArrayDeque;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Enumeration;
-import java.util.Queue;
-import java.util.concurrent.RejectedExecutionException;
-import org.apache.bookkeeper.client.AsyncCallback.AddCallback;
-import org.apache.bookkeeper.client.AsyncCallback.CloseCallback;
-import org.apache.bookkeeper.client.AsyncCallback.ReadCallback;
-import org.apache.bookkeeper.client.BookKeeper.DigestType;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class MockLedgerHandle extends LedgerHandle {
-
- final ArrayList<LedgerEntry> entries = Lists.newArrayList();
- final MockBookKeeper bk;
- final long id;
- final DigestType digest;
- final byte[] passwd;
- long lastEntry = -1;
- boolean fenced = false;
-
- MockLedgerHandle(MockBookKeeper bk, long id, DigestType digest, byte[] passwd) throws GeneralSecurityException {
- super(bk, id, new LedgerMetadata(3, 3, 2, DigestType.MAC, "".getBytes()), DigestType.MAC, "".getBytes());
- this.bk = bk;
- this.id = id;
- this.digest = digest;
- this.passwd = Arrays.copyOf(passwd, passwd.length);
- }
-
- @Override
- public void asyncClose(CloseCallback cb, Object ctx) {
- if (bk.getProgrammedFailStatus()) {
- cb.closeComplete(bk.failReturnCode, this, ctx);
- return;
- }
-
- fenced = true;
- try {
- bk.executor.execute(() -> cb.closeComplete(0, this, ctx));
- } catch (RejectedExecutionException e) {
- cb.closeComplete(0, this, ctx);
- }
-
- }
-
- @Override
- public void asyncReadEntries(final long firstEntry, final long lastEntry, final ReadCallback cb, final Object ctx) {
- if (bk.isStopped()) {
- cb.readComplete(-1, MockLedgerHandle.this, null, ctx);
- return;
- }
-
- bk.executor.execute(new Runnable() {
- public void run() {
- if (bk.getProgrammedFailStatus()) {
- cb.readComplete(bk.failReturnCode, MockLedgerHandle.this, null, ctx);
- return;
- } else if (bk.isStopped()) {
- log.debug("Bookkeeper is closed!");
- cb.readComplete(-1, MockLedgerHandle.this, null, ctx);
- return;
- }
-
- log.debug("readEntries: first={} last={} total={}", firstEntry, lastEntry, entries.size());
- final Queue<LedgerEntry> seq = new ArrayDeque<LedgerEntry>();
- long entryId = firstEntry;
- while (entryId <= lastEntry && entryId < entries.size()) {
- seq.add(entries.get((int) entryId++));
- }
-
- log.debug("Entries read: {}", seq);
-
- try {
- Thread.sleep(1);
- } catch (InterruptedException e) {
- }
-
- cb.readComplete(0, MockLedgerHandle.this, new Enumeration<LedgerEntry>() {
- public boolean hasMoreElements() {
- return !seq.isEmpty();
- }
-
- public LedgerEntry nextElement() {
- return seq.remove();
- }
-
- }, ctx);
- }
- });
- }
-
- @Override
- public void asyncReadLastEntry(final ReadCallback cb, final Object ctx) {
- long lastEntryId = getLastAddConfirmed();
- asyncReadEntries(lastEntryId, lastEntryId, cb, ctx);
- }
-
- @Override
- public long addEntry(byte[] data) throws InterruptedException, BKException {
- try {
- bk.checkProgrammedFail();
- } catch (BKException e) {
- fenced = true;
- throw e;
- }
-
- if (fenced) {
- throw BKException.create(BKException.Code.LedgerFencedException);
- }
-
- if (bk.isStopped()) {
- throw BKException.create(BKException.Code.NoBookieAvailableException);
- }
-
- lastEntry = entries.size();
- entries.add(new MockLedgerEntry(ledgerId, lastEntry, data));
- return lastEntry;
- }
-
- @Override
- public void asyncAddEntry(final byte[] data, final AddCallback cb, final Object ctx) {
- asyncAddEntry(data, 0, data.length, cb, ctx);
- }
-
- @Override
- public void asyncAddEntry(final byte[] data, final int offset, final int length, final AddCallback cb,
- final Object ctx) {
- asyncAddEntry(Unpooled.wrappedBuffer(data, offset, length), cb, ctx);
- }
-
- @Override
- public void asyncAddEntry(final ByteBuf data, final AddCallback cb, final Object ctx) {
- if (bk.isStopped()) {
- cb.addComplete(-1, MockLedgerHandle.this, INVALID_ENTRY_ID, ctx);
- return;
- }
-
- data.retain();
- bk.executor.execute(new Runnable() {
- public void run() {
- if (bk.getProgrammedFailStatus()) {
- fenced = true;
- data.release();
- cb.addComplete(bk.failReturnCode, MockLedgerHandle.this, INVALID_ENTRY_ID, ctx);
- return;
- }
- if (bk.isStopped()) {
- data.release();
- cb.addComplete(-1, MockLedgerHandle.this, INVALID_ENTRY_ID, ctx);
- return;
- }
-
- try {
- Thread.sleep(1);
- } catch (InterruptedException e) {
- }
-
- if (fenced) {
- data.release();
- cb.addComplete(BKException.Code.LedgerFencedException, MockLedgerHandle.this,
- LedgerHandle.INVALID_ENTRY_ID, ctx);
- } else {
- lastEntry = entries.size();
- byte[] storedData = new byte[data.readableBytes()];
- data.readBytes(storedData);
- LedgerEntry entry = new MockLedgerEntry(ledgerId, lastEntry, storedData);
- entries.add(entry);
- data.release();
- cb.addComplete(0, MockLedgerHandle.this, lastEntry, ctx);
- }
- }
- });
- }
-
- @Override
- public long getId() {
- return ledgerId;
- }
-
- @Override
- public long getLastAddConfirmed() {
- return lastEntry;
- }
-
- @Override
- public long getLength() {
- long length = 0;
- for (LedgerEntry entry : entries) {
- length += entry.getLength();
- }
-
- return length;
- }
-
- private static final Logger log = LoggerFactory.getLogger(MockLedgerHandle.class);
-
-}
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
index d3ed63695..ee49810d5 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
@@ -2463,7 +2463,7 @@ public void operationFailed(ManagedLedgerException exception) {
/**
* Verifies cursor persists individually unack range into cursor-ledger if range count is higher than
* MaxUnackedRangesToPersistInZk
- *
+ *
* @throws Exception
*/
@Test(timeOut = 20000)
@@ -2517,7 +2517,8 @@ public void operationFailed(MetaStoreException e) {
final AtomicInteger individualDeletedMessagesCount = new AtomicInteger(0);
bkc.asyncOpenLedger(c1.getCursorLedger(), DigestType.MAC, "".getBytes(), (rc, lh, ctx) -> {
if (rc == BKException.Code.OK) {
- lh.asyncReadLastEntry((rc1, lh1, seq, ctx1) -> {
+ long lastEntry = lh.getLastAddConfirmed();
+ lh.asyncReadEntries(lastEntry, lastEntry, (rc1, lh1, seq, ctx1) -> {
try {
LedgerEntry entry = seq.nextElement();
PositionInfo positionInfo;
@@ -2549,7 +2550,7 @@ public void operationFailed(MetaStoreException e) {
/**
* Close Cursor without MaxUnackedRangesToPersistInZK: It should store individually unack range into Zk
- *
+ *
* @throws Exception
*/
@Test(timeOut = 20000)
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/test/MockedBookKeeperTestCase.java b/managed-ledger/src/test/java/org/apache/bookkeeper/test/MockedBookKeeperTestCase.java
index 7bdf2a1d5..e3c18a07d 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/test/MockedBookKeeperTestCase.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/test/MockedBookKeeperTestCase.java
@@ -21,16 +21,19 @@
import java.lang.reflect.Method;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+
import org.apache.bookkeeper.client.MockBookKeeper;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig;
import org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
import org.apache.bookkeeper.util.ZkUtils;
import org.apache.zookeeper.MockZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
/**
@@ -51,7 +54,7 @@
protected ClientConfiguration baseClientConf = new ClientConfiguration();
- protected OrderedSafeExecutor executor;
+ protected OrderedScheduler executor;
protected ExecutorService cachedExecutor;
public MockedBookKeeperTestCase() {
@@ -74,8 +77,6 @@ public void setUp(Method method) throws Exception {
throw e;
}
- executor = new OrderedSafeExecutor(2, "test");
- cachedExecutor = Executors.newCachedThreadPool();
ManagedLedgerFactoryConfig conf = new ManagedLedgerFactoryConfig();
factory = new ManagedLedgerFactoryImpl(bkc, zkc, conf);
}
@@ -87,9 +88,19 @@ public void tearDown(Method method) throws Exception {
factory = null;
stopBookKeeper();
stopZooKeeper();
+ LOG.info("--------- stopped {}", method);
+ }
+
+ @BeforeClass
+ public void setUpClass() throws Exception {
+ executor = OrderedScheduler.newSchedulerBuilder().numThreads(2).name("test").build();
+ cachedExecutor = Executors.newCachedThreadPool();
+ }
+
+ @AfterClass
+ public void tearDownClass() throws Exception {
executor.shutdown();
cachedExecutor.shutdown();
- LOG.info("--------- stopped {}", method);
}
/**
@@ -106,7 +117,7 @@ protected void startBookKeeper() throws Exception {
zkc.create("/ledgers/LAYOUT", "1\nflat:1".getBytes(), null, null);
- bkc = new MockBookKeeper(baseClientConf, zkc);
+ bkc = new MockBookKeeper(zkc);
}
protected void stopBookKeeper() throws Exception {
diff --git a/managed-ledger/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java b/managed-ledger/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java
index 044e22dc9..8b44ebb74 100644
--- a/managed-ledger/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java
+++ b/managed-ledger/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java
@@ -27,8 +27,8 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.CountDownLatch;
-import org.apache.bookkeeper.util.ZkUtils;
-import org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase;
+
+import org.apache.bookkeeper.zookeeper.ZooKeeperClient;
import org.apache.commons.io.FileUtils;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
@@ -85,8 +85,7 @@ public void startServer() throws Exception {
// create a zookeeper client
LOG.debug("Instantiate ZK Client");
- ZooKeeperWatcherBase w = new ZooKeeperWatcherBase(10000);
- zkc = ZkUtils.createConnectedZookeeperClient(getZooKeeperConnectString(), w);
+ zkc = ZooKeeperClient.newBuilder().connectString(getZooKeeperConnectString()).build();
// initialize the zk client with values
zkc.create("/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
diff --git a/pom.xml b/pom.xml
index 6b2dd0847..23104a96c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -109,7 +109,7 @@ flexible messaging model and an intuitive client API.</description>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <bookkeeper.version>4.3.1.91-yahoo</bookkeeper.version>
+ <bookkeeper.version>4.7.0-SNAPSHOT</bookkeeper.version>
<zookeeper.version>3.4.10</zookeeper.version>
<netty.version>4.1.21.Final</netty.version>
<storm.version>1.0.5</storm.version>
@@ -540,6 +540,12 @@ flexible messaging model and an intuitive client API.</description>
<version>${prometheus.version}</version>
</dependency>
+ <dependency>
+ <groupId>com.carrotsearch</groupId>
+ <artifactId>hppc</artifactId>
+ <version>0.7.3</version>
+ </dependency>
+
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.10</artifactId>
@@ -984,10 +990,6 @@ flexible messaging model and an intuitive client API.</description>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
</repository>
- <repository>
- <id>bookkeeper-yahoo-mvn-repo</id>
- <url>https://raw.githubusercontent.com/yahoo/bookkeeper/mvn-repo</url>
- </repository>
<repository>
<snapshots>
<enabled>false</enabled>
diff --git a/pulsar-broker/pom.xml b/pulsar-broker/pom.xml
index 6cd1f8957..b5c60564b 100644
--- a/pulsar-broker/pom.xml
+++ b/pulsar-broker/pom.xml
@@ -225,12 +225,32 @@
<artifactId>ea-agent-loader</artifactId>
</dependency>
+ <dependency>
+ <groupId>com.carrotsearch</groupId>
+ <artifactId>hppc</artifactId>
+ </dependency>
+
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>buildtools</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.bookkeeper</groupId>
+ <artifactId>bookkeeper-server</artifactId>
+ <version>${bookkeeper.version}</version>
+ <classifier>tests</classifier>
+ <scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+
</dependencies>
<build>
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/PulsarClusterMetadataSetup.java b/pulsar-broker/src/main/java/org/apache/pulsar/PulsarClusterMetadataSetup.java
index 0e9539f70..17349aa23 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/PulsarClusterMetadataSetup.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/PulsarClusterMetadataSetup.java
@@ -18,18 +18,19 @@
*/
package org.apache.pulsar;
+import static org.apache.pulsar.broker.cache.ConfigurationCacheService.POLICIES_ROOT;
+
import java.io.IOException;
import org.apache.bookkeeper.client.BookKeeperAdmin;
-import org.apache.bookkeeper.conf.ClientConfiguration;
+import org.apache.bookkeeper.conf.ServerConfiguration;
import org.apache.bookkeeper.meta.HierarchicalLedgerManagerFactory;
import org.apache.bookkeeper.util.ZkUtils;
-import static org.apache.pulsar.broker.cache.ConfigurationCacheService.POLICIES_ROOT;
import org.apache.pulsar.common.policies.data.ClusterData;
import org.apache.pulsar.common.util.ObjectMapperFactory;
import org.apache.pulsar.zookeeper.ZooKeeperClientFactory;
-import org.apache.pulsar.zookeeper.ZookeeperClientFactoryImpl;
import org.apache.pulsar.zookeeper.ZooKeeperClientFactory.SessionType;
+import org.apache.pulsar.zookeeper.ZookeeperClientFactoryImpl;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException.NodeExistsException;
import org.apache.zookeeper.ZooDefs;
@@ -96,7 +97,7 @@ public static void main(String[] args) throws Exception {
arguments.globalZookeeper);
// Format BookKeeper metadata
- ClientConfiguration bkConf = new ClientConfiguration();
+ ServerConfiguration bkConf = new ServerConfiguration();
bkConf.setLedgerManagerFactoryClass(HierarchicalLedgerManagerFactory.class);
bkConf.setZkServers(arguments.zookeeper);
if (!BookKeeperAdmin.format(bkConf, false /* interactive */, false /* force */)) {
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/BookKeeperClientFactoryImpl.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/BookKeeperClientFactoryImpl.java
index 8ad09bcd2..954d4ef89 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/BookKeeperClientFactoryImpl.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/BookKeeperClientFactoryImpl.java
@@ -21,22 +21,21 @@
import java.io.IOException;
import java.util.concurrent.TimeUnit;
+import org.apache.bookkeeper.client.BKException;
import org.apache.bookkeeper.client.BookKeeper;
import org.apache.bookkeeper.client.RackawareEnsemblePlacementPolicy;
import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.meta.HierarchicalLedgerManagerFactory;
-import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.zookeeper.ZkBookieRackAffinityMapping;
import org.apache.pulsar.zookeeper.ZkIsolatedBookieEnsemblePlacementPolicy;
import org.apache.pulsar.zookeeper.ZooKeeperCache;
-import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
public class BookKeeperClientFactoryImpl implements BookKeeperClientFactory {
private ZooKeeperCache rackawarePolicyZkCache;
private ZooKeeperCache clientIsolationZkCache;
-
+
@Override
public BookKeeper create(ServiceConfiguration conf, ZooKeeper zkClient) throws IOException {
ClientConfiguration bkConf = new ClientConfiguration();
@@ -85,11 +84,11 @@ public BookKeeper create(ServiceConfiguration conf, ZooKeeper zkClient) throws I
try {
return new BookKeeper(bkConf, zkClient);
- } catch (InterruptedException | KeeperException e) {
+ } catch (InterruptedException | BKException e) {
throw new IOException(e);
}
}
-
+
public void close() {
if (this.rackawarePolicyZkCache != null) {
this.rackawarePolicyZkCache.stop();
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java
index 5ee1532f7..9b2343337 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java
@@ -35,8 +35,8 @@
import java.util.function.Supplier;
import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.bookkeeper.mledger.ManagedLedgerFactory;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.pulsar.broker.admin.AdminResource;
import org.apache.pulsar.broker.cache.ConfigurationCacheService;
@@ -105,7 +105,8 @@
new DefaultThreadFactory("pulsar"));
private final ScheduledExecutorService cacheExecutor = Executors.newScheduledThreadPool(10,
new DefaultThreadFactory("zk-cache-callback"));
- private final OrderedSafeExecutor orderedExecutor = new OrderedSafeExecutor(8, "pulsar-ordered");
+ private final OrderedScheduler orderedExecutor = OrderedScheduler.newSchedulerBuilder().numThreads(8)
+ .name("pulsar-ordered").build();
private final ScheduledExecutorService loadManagerExecutor;
private ScheduledFuture<?> loadReportTask = null;
private ScheduledFuture<?> loadSheddingTask = null;
@@ -580,7 +581,7 @@ public ScheduledExecutorService getLoadManagerExecutor() {
return loadManagerExecutor;
}
- public OrderedSafeExecutor getOrderedExecutor() {
+ public OrderedScheduler getOrderedExecutor() {
return orderedExecutor;
}
@@ -590,7 +591,7 @@ public LocalZooKeeperCacheService getLocalZkCacheService() {
public ZooKeeperClientFactory getZooKeeperClientFactory() {
if (zkClientFactory == null) {
- zkClientFactory = new ZookeeperBkClientFactoryImpl();
+ zkClientFactory = new ZookeeperBkClientFactoryImpl(orderedExecutor);
}
// Return default factory
return zkClientFactory;
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
index 20b1ce4ee..575f1ec3d 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
@@ -50,12 +50,12 @@
import java.util.function.Predicate;
import org.apache.bookkeeper.client.BookKeeper.DigestType;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback;
import org.apache.bookkeeper.mledger.ManagedLedger;
import org.apache.bookkeeper.mledger.ManagedLedgerConfig;
import org.apache.bookkeeper.mledger.ManagedLedgerException;
import org.apache.bookkeeper.mledger.ManagedLedgerFactory;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
import org.apache.bookkeeper.util.ZkUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
@@ -143,7 +143,7 @@
private final EventLoopGroup acceptorGroup;
private final EventLoopGroup workerGroup;
- private final OrderedSafeExecutor topicOrderedExecutor;
+ private final OrderedScheduler topicOrderedExecutor;
// offline topic backlog cache
private final ConcurrentOpenHashMap<TopicName, PersistentOfflineTopicStats> offlineTopicStatCache;
private static final ConcurrentOpenHashMap<String, ConfigField> dynamicConfigurationMap = prepareDynamicConfigurationMap();
@@ -197,7 +197,9 @@ public BrokerService(PulsarService pulsar) throws Exception {
this.pulsarStats = new PulsarStats(pulsar);
this.offlineTopicStatCache = new ConcurrentOpenHashMap<>();
- this.topicOrderedExecutor = new OrderedSafeExecutor(pulsar.getConfiguration().getNumWorkerThreadsForNonPersistentTopic(), "broker-np-topic-workers");
+ this.topicOrderedExecutor = OrderedScheduler.newSchedulerBuilder()
+ .numThreads(pulsar.getConfiguration().getNumWorkerThreadsForNonPersistentTopic())
+ .name("broker-np-topic-workers").build();
final DefaultThreadFactory acceptorThreadFactory = new DefaultThreadFactory("pulsar-acceptor");
final DefaultThreadFactory workersThreadFactory = new DefaultThreadFactory("pulsar-io");
final int numThreads = Runtime.getRuntime().availableProcessors() * 2;
@@ -1296,7 +1298,7 @@ private void createPendingLoadTopic() {
}
- public OrderedSafeExecutor getTopicOrderedExecutor() {
+ public OrderedScheduler getTopicOrderedExecutor() {
return topicOrderedExecutor;
}
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/nonpersistent/NonPersistentTopic.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/nonpersistent/NonPersistentTopic.java
index 2ec804228..ef355f0ea 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/nonpersistent/NonPersistentTopic.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/nonpersistent/NonPersistentTopic.java
@@ -33,10 +33,10 @@
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.bookkeeper.mledger.Entry;
import org.apache.bookkeeper.mledger.Position;
import org.apache.bookkeeper.mledger.util.SafeRun;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
import org.apache.pulsar.broker.admin.AdminResource;
import org.apache.pulsar.broker.service.BrokerService;
import org.apache.pulsar.broker.service.BrokerServiceException;
@@ -110,7 +110,7 @@
.newUpdater(NonPersistentTopic.class, "usageCount");
private volatile long usageCount = 0;
- private final OrderedSafeExecutor executor;
+ private final OrderedScheduler executor;
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorTool.java b/pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorTool.java
index ac18d1f6e..073614623 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorTool.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorTool.java
@@ -20,38 +20,31 @@
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
-import java.io.FileInputStream;
-import org.apache.pulsar.common.configuration.PulsarConfigurationLoader;
-import com.beust.jcommander.JCommander;
-import com.beust.jcommander.Parameter;
-import com.beust.jcommander.ParameterException;
-import com.beust.jcommander.Parameters;
-import org.apache.pulsar.broker.ServiceConfiguration;
-import org.apache.pulsar.broker.PulsarService;
-import com.google.common.util.concurrent.ThreadFactoryBuilder;
-import java.util.Properties;
-
-import org.apache.pulsar.client.api.ClientConfiguration;
import java.nio.file.Paths;
-
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
+import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.pulsar.broker.BookKeeperClientFactory;
import org.apache.pulsar.broker.BookKeeperClientFactoryImpl;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.client.api.ClientConfiguration;
import org.apache.pulsar.client.api.PulsarClient;
-
+import org.apache.pulsar.common.configuration.PulsarConfigurationLoader;
import org.apache.pulsar.zookeeper.ZooKeeperClientFactory;
import org.apache.pulsar.zookeeper.ZookeeperBkClientFactoryImpl;
import org.apache.zookeeper.ZooKeeper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
-import org.apache.bookkeeper.client.BookKeeper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import lombok.Cleanup;
public class CompactorTool {
@@ -102,7 +95,8 @@ public static void main(String[] args) throws Exception {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder().setNameFormat("compaction-%d").setDaemon(true).build());
- ZooKeeperClientFactory zkClientFactory = new ZookeeperBkClientFactoryImpl();
+ OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
+ ZooKeeperClientFactory zkClientFactory = new ZookeeperBkClientFactoryImpl(executor);
ZooKeeper zk = zkClientFactory.create(brokerConfig.getZookeeperServers(),
ZooKeeperClientFactory.SessionType.ReadWrite,
@@ -118,6 +112,7 @@ public static void main(String[] args) throws Exception {
bkClientFactory.close();
zk.close();
scheduler.shutdownNow();
+ executor.shutdown();
}
}
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/compaction/TwoPhaseCompactor.java b/pulsar-broker/src/main/java/org/apache/pulsar/compaction/TwoPhaseCompactor.java
index 10dc8b129..7ae788c70 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/compaction/TwoPhaseCompactor.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/compaction/TwoPhaseCompactor.java
@@ -21,6 +21,7 @@
import com.google.common.collect.ImmutableMap;
import io.netty.buffer.ByteBuf;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@@ -224,7 +225,7 @@ private void phaseTwoLoop(RawReader reader, MessageId to, Map<String, MessageId>
} else {
bkf.complete(ledger);
}
- }, null);
+ }, null, Collections.emptyMap());
return bkf;
}
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/PulsarBrokerStarterTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/PulsarBrokerStarterTest.java
index 0cd9e879c..f9373c0b3 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/PulsarBrokerStarterTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/PulsarBrokerStarterTest.java
@@ -327,23 +327,6 @@ public void testMainRunBookieNoConfig() throws Exception {
}
}
- /**
- * Verifies that the main throws {@link IllegalArgumentException} when malformed config file for bookie is given.
- */
- @Test
- public void testMainRunBookieEmptyConfig() throws Exception {
- try {
- File testConfigFile = createValidBrokerConfigFile();
- String[] args = {"-c", testConfigFile.getAbsolutePath(),
- "-ra", "-rb", "-bc", testConfigFile.getAbsolutePath()};
- PulsarBrokerStarter.main(args);
- Assert.fail("Effectively empty config file for bookie should've raised NoWritableLedgerDirException!");
- } catch (LedgerDirsManager.NoWritableLedgerDirException e) {
- // code should reach here
- // Since empty config file will have empty ledgerDirs, it should raise exception when bookie init.
- }
- }
-
/**
* Verifies that the main throws {@link IllegalArgumentException} when no config file for bookie .
*/
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/MockedBookKeeperClientFactory.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/MockedBookKeeperClientFactory.java
new file mode 100644
index 000000000..1922da75c
--- /dev/null
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/MockedBookKeeperClientFactory.java
@@ -0,0 +1,52 @@
+/**
+ * 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.broker;
+
+import java.io.IOException;
+
+import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.client.MockBookKeeper;
+import org.apache.zookeeper.ZooKeeper;
+
+public class MockedBookKeeperClientFactory implements BookKeeperClientFactory {
+
+ private final BookKeeper mockedBk;
+
+ public MockedBookKeeperClientFactory() {
+ try {
+ mockedBk = new MockBookKeeper(null);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public BookKeeper create(ServiceConfiguration conf, ZooKeeper zkClient) throws IOException {
+ return mockedBk;
+ }
+
+ @Override
+ public void close() {
+ try {
+ mockedBk.close();
+ } catch (BKException | InterruptedException e) {
+ }
+ }
+}
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest.java
index 488f23775..7e098cf09 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest.java
@@ -497,10 +497,11 @@ public void testUpdateDynamicLocalConfiguration() throws Exception {
admin.brokers().updateDynamicConfiguration("brokerShutdownTimeoutMs", Long.toString(shutdownTime));
// sleep incrementally as zk-watch notification is async and may take some time
for (int i = 0; i < 5; i++) {
- if (pulsar.getConfiguration().getBrokerShutdownTimeoutMs() != initValue) {
+ if (pulsar.getConfiguration().getBrokerShutdownTimeoutMs() == initValue) {
Thread.sleep(50 + (i * 10));
}
}
+
// verify value is updated
assertEquals(pulsar.getConfiguration().getBrokerShutdownTimeoutMs(), shutdownTime);
}
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/MockedPulsarServiceBaseTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/MockedPulsarServiceBaseTest.java
index af92e9969..d1895599a 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/MockedPulsarServiceBaseTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/MockedPulsarServiceBaseTest.java
@@ -216,7 +216,7 @@ public static NonClosableMockBookKeeper createMockBookKeeper(ZooKeeper zookeeper
private static class NonClosableMockBookKeeper extends MockBookKeeper {
public NonClosableMockBookKeeper(ClientConfiguration conf, ZooKeeper zk) throws Exception {
- super(conf, zk);
+ super(zk);
}
@Override
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/SameThreadOrderedSafeExecutor.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/SameThreadOrderedSafeExecutor.java
index b4b50b1e5..98cafc547 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/SameThreadOrderedSafeExecutor.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/SameThreadOrderedSafeExecutor.java
@@ -18,13 +18,16 @@
*/
package org.apache.pulsar.broker.auth;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
-import org.apache.bookkeeper.util.SafeRunnable;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
+import org.apache.bookkeeper.common.util.SafeRunnable;
+import org.apache.bookkeeper.stats.NullStatsLogger;
-public class SameThreadOrderedSafeExecutor extends OrderedSafeExecutor {
+import io.netty.util.concurrent.DefaultThreadFactory;
+
+public class SameThreadOrderedSafeExecutor extends OrderedScheduler {
public SameThreadOrderedSafeExecutor() {
- super(1, "ordered-executor");
+ super("same-thread-executor", 1, new DefaultThreadFactory("test"), NullStatsLogger.INSTANCE, false, 100000, 10);
}
@Override
@@ -42,8 +45,4 @@ public void submitOrdered(long orderingKey, SafeRunnable r) {
r.run();
}
- @Override
- public void submitOrdered(Object orderingKey, SafeRunnable r) {
- r.run();
- }
}
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/ResourceQuotaCacheTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/ResourceQuotaCacheTest.java
index 8a620a46d..9fd2fb7ac 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/ResourceQuotaCacheTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/cache/ResourceQuotaCacheTest.java
@@ -28,10 +28,8 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.pulsar.broker.PulsarService;
-import org.apache.pulsar.broker.cache.LocalZooKeeperCacheService;
-import org.apache.pulsar.broker.cache.ResourceQuotaCache;
import org.apache.pulsar.common.naming.NamespaceBundle;
import org.apache.pulsar.common.naming.NamespaceBundleFactory;
import org.apache.pulsar.common.naming.NamespaceName;
@@ -53,13 +51,13 @@
private ZooKeeperCache zkCache;
private LocalZooKeeperCacheService localCache;
private NamespaceBundleFactory bundleFactory;
- private OrderedSafeExecutor executor;
+ private OrderedScheduler executor;
private ScheduledExecutorService scheduledExecutor;
@BeforeMethod
public void setup() throws Exception {
pulsar = mock(PulsarService.class);
- executor = new OrderedSafeExecutor(1, "test");
+ executor = OrderedScheduler.newSchedulerBuilder().numThreads(1).name("test").build();
scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
zkCache = new LocalZooKeeperCache(MockZooKeeper.newInstance(), executor, scheduledExecutor);
localCache = new LocalZooKeeperCacheService(zkCache, null);
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/namespace/OwnershipCacheTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/namespace/OwnershipCacheTest.java
index 4d20ff158..e8d4bb909 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/namespace/OwnershipCacheTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/namespace/OwnershipCacheTest.java
@@ -39,15 +39,10 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.cache.LocalZooKeeperCacheService;
-import org.apache.pulsar.broker.namespace.NamespaceEphemeralData;
-import org.apache.pulsar.broker.namespace.NamespaceService;
-import org.apache.pulsar.broker.namespace.OwnedBundle;
-import org.apache.pulsar.broker.namespace.OwnershipCache;
-import org.apache.pulsar.broker.namespace.ServiceUnitZkUtils;
import org.apache.pulsar.broker.service.BrokerService;
import org.apache.pulsar.common.naming.NamespaceBundle;
import org.apache.pulsar.common.naming.NamespaceBundleFactory;
@@ -75,7 +70,7 @@
private NamespaceBundleFactory bundleFactory;
private NamespaceService nsService;
private BrokerService brokerService;
- private OrderedSafeExecutor executor;
+ private OrderedScheduler executor;
private ScheduledExecutorService scheduledExecutor;
@BeforeMethod
@@ -84,7 +79,7 @@ public void setup() throws Exception {
selfBrokerUrl = "tcp://localhost:" + port;
pulsar = mock(PulsarService.class);
config = mock(ServiceConfiguration.class);
- executor = new OrderedSafeExecutor(1, "test");
+ executor = OrderedScheduler.newSchedulerBuilder().numThreads(1).name("test").build();
scheduledExecutor = Executors.newScheduledThreadPool(2);
zkCache = new LocalZooKeeperCache(MockZooKeeper.newInstance(), executor, scheduledExecutor);
localCache = spy(new LocalZooKeeperCacheService(zkCache, null));
@@ -92,7 +87,7 @@ public void setup() throws Exception {
when(pulsar.getLocalZkCacheService()).thenReturn(localCache);
when(localCache.policiesCache()).thenReturn(poilciesCache);
doNothing().when(poilciesCache).registerListener(any());
-
+
bundleFactory = new NamespaceBundleFactory(pulsar, Hashing.crc32());
nsService = mock(NamespaceService.class);
brokerService = mock(BrokerService.class);
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/web/WebServiceTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/web/WebServiceTest.java
index 41ab8bb97..c7092de71 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/web/WebServiceTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/web/WebServiceTest.java
@@ -40,6 +40,7 @@
import javax.net.ssl.TrustManager;
import org.apache.bookkeeper.test.PortManager;
+import org.apache.pulsar.broker.MockedBookKeeperClientFactory;
import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.client.admin.PulsarAdmin;
@@ -259,6 +260,7 @@ private void setupEnv(boolean enableFilter, String minApiVersion, boolean allowU
config.setZookeeperServers("localhost:2181");
pulsar = spy(new PulsarService(config));
doReturn(new MockedZooKeeperClientFactoryImpl()).when(pulsar).getZooKeeperClientFactory();
+ doReturn(new MockedBookKeeperClientFactory()).when(pulsar).getBookKeeperClientFactory();
pulsar.start();
try {
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/zookeeper/ZooKeeperClientAspectJTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/zookeeper/ZooKeeperClientAspectJTest.java
index 52886401b..5454c8bc1 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/zookeeper/ZooKeeperClientAspectJTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/zookeeper/ZooKeeperClientAspectJTest.java
@@ -32,6 +32,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.bookkeeper.test.PortManager;
import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.broker.service.BrokerService;
@@ -69,7 +70,7 @@
private final int LOCAL_ZOOKEEPER_PORT = PortManager.nextFreePort();
private final long ZOOKEEPER_SESSION_TIMEOUT_MILLIS = 2000;
private final List<ACL> Acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
-
+
static {
// load agent with aspectjweaver-Agent for testing
// maven-test waves advice on build-goal so, maven doesn't need explicit loading
@@ -79,8 +80,9 @@
@Test
public void testZkConnected() throws Exception {
+ OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
try {
- ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl();
+ ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl(executor);
CompletableFuture<ZooKeeper> zkFuture = zkf.create("127.0.0.1:" + LOCAL_ZOOKEEPER_PORT, SessionType.ReadWrite,
(int) ZOOKEEPER_SESSION_TIMEOUT_MILLIS);
localZkc = zkFuture.get(ZOOKEEPER_SESSION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
@@ -90,6 +92,8 @@ public void testZkConnected() throws Exception {
if (localZkc != null) {
localZkc.close();
}
+
+ executor.shutdown();
}
}
@@ -106,12 +110,13 @@ void teardown() throws Exception {
/**
* Verifies that aspect-advice calculates the latency of of zk-operation
- *
+ *
* @throws Exception
*/
@Test(enabled=false, timeOut = 7000)
void testZkClientAspectJTrigger() throws Exception {
- ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl();
+ OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
+ ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl(executor);
CompletableFuture<ZooKeeper> zkFuture = zkf.create("127.0.0.1:" + LOCAL_ZOOKEEPER_PORT, SessionType.ReadWrite,
(int) ZOOKEEPER_SESSION_TIMEOUT_MILLIS);
localZkc = zkFuture.get(ZOOKEEPER_SESSION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
@@ -160,18 +165,20 @@ public void recordLatency(EventType eventType, long latencyMiliSecond) {
if (localZkc != null) {
localZkc.close();
}
+
+ executor.shutdown();
}
}
/**
* Verifies that aspect-advice calculates the latency of of zk-operation and updates PulsarStats
- *
+ *
* @throws Exception
*/
@Test(enabled=false, timeOut = 7000)
public void testZkOpStatsMetrics() throws Exception {
-
- ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl();
+ OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
+ ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl(executor);
CompletableFuture<ZooKeeper> zkFuture = zkf.create("127.0.0.1:" + LOCAL_ZOOKEEPER_PORT, SessionType.ReadWrite,
(int) ZOOKEEPER_SESSION_TIMEOUT_MILLIS);
localZkc = zkFuture.get(ZOOKEEPER_SESSION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
@@ -244,6 +251,8 @@ public void testZkOpStatsMetrics() throws Exception {
if (localZkc != null) {
localZkc.close();
}
+
+ executor.shutdown();
}
}
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/MessageDispatchThrottlingTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/MessageDispatchThrottlingTest.java
index 8947eb815..9163e0dfd 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/MessageDispatchThrottlingTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/MessageDispatchThrottlingTest.java
@@ -94,7 +94,7 @@ protected void cleanup() throws Exception {
/**
* verifies: message-rate change gets reflected immediately into topic at runtime
- *
+ *
* @throws Exception
*/
@Test
@@ -154,7 +154,7 @@ public void testMessageRateDynamicallyChange() throws Exception {
/**
* verify: consumer should not receive all messages due to message-rate throttling
- *
+ *
* @param subscription
* @throws Exception
*/
@@ -226,7 +226,7 @@ public void testMessageRateLimitingNotReceiveAllMessages(SubscriptionType subscr
/**
* It verifies that dispatch-rate throttling with cluster-configuration
- *
+ *
* @param subscription
* @param dispatchRateType
* @throws Exception
@@ -246,7 +246,7 @@ public void testClusterMsgByteRateLimitingClusterConfig() throws Exception {
admin.brokers().updateDynamicConfiguration("dispatchThrottlingRatePerTopicInByte", Long.toString(byteRate));
// sleep incrementally as zk-watch notification is async and may take some time
for (int i = 0; i < 5; i++) {
- if (pulsar.getConfiguration().getDispatchThrottlingRatePerTopicInMsg() != initValue) {
+ if (pulsar.getConfiguration().getDispatchThrottlingRatePerTopicInMsg() == initValue) {
Thread.sleep(50 + (i * 10));
}
}
@@ -292,13 +292,13 @@ public void testClusterMsgByteRateLimitingClusterConfig() throws Exception {
/**
* verify rate-limiting should throttle message-dispatching based on message-rate
- *
+ *
* <pre>
* 1. dispatch-msg-rate = 10 msg/sec
- * 2. send 20 msgs
+ * 2. send 20 msgs
* 3. it should take up to 2 second to receive all messages
* </pre>
- *
+ *
* @param subscription
* @throws Exception
*/
@@ -366,13 +366,13 @@ public void testMessageRateLimitingReceiveAllMessagesAfterThrottling(Subscriptio
/**
* verify rate-limiting should throttle message-dispatching based on byte-rate
- *
+ *
* <pre>
* 1. dispatch-byte-rate = 100 bytes/sec
* 2. send 20 msgs : each with 10 byte
* 3. it should take up to 2 second to receive all messages
* </pre>
- *
+ *
* @param subscription
* @throws Exception
*/
@@ -438,7 +438,7 @@ public void testBytesRateLimitingReceiveAllMessagesAfterThrottling(SubscriptionT
/**
* verify message-rate on multiple consumers with shared-subscription
- *
+ *
* @throws Exception
*/
@Test(timeOut = 5000)
@@ -524,7 +524,7 @@ public void testClusterRateLimitingConfiguration(SubscriptionType subscription)
admin.brokers().updateDynamicConfiguration("dispatchThrottlingRatePerTopicInMsg", Integer.toString(messageRate));
// sleep incrementally as zk-watch notification is async and may take some time
for (int i = 0; i < 5; i++) {
- if (pulsar.getConfiguration().getDispatchThrottlingRatePerTopicInMsg() != initValue) {
+ if (pulsar.getConfiguration().getDispatchThrottlingRatePerTopicInMsg() == initValue) {
Thread.sleep(50 + (i * 10));
}
}
@@ -570,7 +570,7 @@ public void testClusterRateLimitingConfiguration(SubscriptionType subscription)
/**
* It verifies that that dispatch-throttling considers both msg/byte rate if both of them are configured together
- *
+ *
* @param subscription
* @throws Exception
*/
@@ -644,7 +644,7 @@ public void testMessageByteRateThrottlingCombined(SubscriptionType subscription)
* 1. It sets dispatch-rate for a local cluster into global-zk.policies
* 2. Topic fetches dispatch-rate for the local cluster from policies
* 3. applies dispatch rate
- *
+ *
* </pre>
* @throws Exception
*/
@@ -654,7 +654,7 @@ public void testGlobalNamespaceThrottling() throws Exception {
final String namespace = "my-property/global/throttling_ns";
final String topicName = "persistent://" + namespace + "/throttlingBlock";
-
+
final int messageRate = 5;
DispatchRate dispatchRate = new DispatchRate(messageRate, -1, 360);
@@ -663,7 +663,7 @@ public void testGlobalNamespaceThrottling() throws Exception {
admin.namespaces().createNamespace(namespace);
admin.namespaces().setNamespaceReplicationClusters(namespace, Lists.newArrayList("use"));
admin.namespaces().setDispatchRate(namespace, dispatchRate);
-
+
// create producer and topic
Producer producer = pulsarClient.createProducer(topicName);
PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService().getTopic(topicName).get();
@@ -717,7 +717,7 @@ public void testGlobalNamespaceThrottling() throws Exception {
/**
* It verifies that broker throttles already caught-up consumer which doesn't have backlog if the flag is enabled
- *
+ *
* @param subscription
* @throws Exception
*/
@@ -785,17 +785,17 @@ public void testNonBacklogConsumerWithThrottlingEnabled(SubscriptionType subscri
log.info("-- Exiting {} test --", methodName);
}
- /**
+ /**
* <pre>
* It verifies that cluster-throttling value gets considered when namespace-policy throttling is disabled.
- *
+ *
* 1. Update cluster-throttling-config: topic rate-limiter has cluster-config
* 2. Update namespace-throttling-config: topic rate-limiter has namespace-config
* 3. Disable namespace-throttling-config: topic rate-limiter has cluster-config
* 4. Create new topic with disable namespace-config and enabled cluster-config: it takes cluster-config
- *
+ *
* </pre>
- *
+ *
* @throws Exception
*/
@Test
@@ -813,7 +813,7 @@ public void testClusterPolicyOverrideConfiguration() throws Exception {
Integer.toString(clusterMessageRate));
// sleep incrementally as zk-watch notification is async and may take some time
for (int i = 0; i < 5; i++) {
- if (pulsar.getConfiguration().getDispatchThrottlingRatePerTopicInMsg() != initValue) {
+ if (pulsar.getConfiguration().getDispatchThrottlingRatePerTopicInMsg() == initValue) {
Thread.sleep(50 + (i * 10));
}
}
@@ -858,7 +858,7 @@ public void testClusterPolicyOverrideConfiguration() throws Exception {
log.info("-- Exiting {} test --", methodName);
}
-
+
private void deactiveCursors(ManagedLedgerImpl ledger) throws Exception {
Field statsUpdaterField = BrokerService.class.getDeclaredField("statsUpdater");
statsUpdaterField.setAccessible(true);
diff --git a/pulsar-client-tools/pom.xml b/pulsar-client-tools/pom.xml
index 1d2454db7..7c0fb1d05 100644
--- a/pulsar-client-tools/pom.xml
+++ b/pulsar-client-tools/pom.xml
@@ -102,6 +102,20 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.bookkeeper</groupId>
+ <artifactId>bookkeeper-server</artifactId>
+ <version>${bookkeeper.version}</version>
+ <classifier>tests</classifier>
+ <scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
</dependencies>
</project>
diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/GrowableArrayBlockingQueue.java b/pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/GrowableArrayBlockingQueue.java
index 14583655b..ae6a2727a 100644
--- a/pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/GrowableArrayBlockingQueue.java
+++ b/pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/GrowableArrayBlockingQueue.java
@@ -19,15 +19,18 @@
package org.apache.pulsar.common.util.collections;
import java.util.AbstractQueue;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
+import java.util.List;
import java.util.NoSuchElementException;
+import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.Consumer;
import io.netty.util.internal.MathUtil;
@@ -46,8 +49,10 @@
private final Condition isNotEmpty = headLock.newCondition();
private T[] data;
- private static final AtomicIntegerFieldUpdater<GrowableArrayBlockingQueue> SIZE_UPDATER =
- AtomicIntegerFieldUpdater.newUpdater(GrowableArrayBlockingQueue.class, "size");
+
+ @SuppressWarnings("rawtypes")
+ private static final AtomicIntegerFieldUpdater<GrowableArrayBlockingQueue> SIZE_UPDATER = AtomicIntegerFieldUpdater
+ .newUpdater(GrowableArrayBlockingQueue.class, "size");
private volatile int size = 0;
public GrowableArrayBlockingQueue() {
@@ -79,6 +84,7 @@ public T poll() {
try {
if (SIZE_UPDATER.get(this) > 0) {
T item = data[headIndex.value];
+ data[headIndex.value] = null;
headIndex.value = (headIndex.value + 1) & (data.length - 1);
SIZE_UPDATER.decrementAndGet(this);
return item;
@@ -273,6 +279,59 @@ public void clear() {
}
}
+ @Override
+ public boolean remove(Object o) {
+ tailLock.lock();
+ headLock.lock();
+
+ try {
+ int index = this.headIndex.value;
+ int size = this.size;
+
+ for (int i = 0; i < size; i++) {
+ T item = data[index];
+
+ if (Objects.equals(item, o)) {
+ remove(index);
+ return true;
+ }
+
+ index = (index + 1) & (data.length - 1);
+ }
+ } finally {
+ headLock.unlock();
+ tailLock.unlock();
+ }
+
+ return false;
+ }
+
+ private void remove(int index) {
+ int tailIndex = this.tailIndex.value;
+
+ if (index < tailIndex) {
+ System.arraycopy(data, index + 1, data, index, tailIndex - index - 1);
+ this.tailIndex.value--;
+ } else {
+ System.arraycopy(data, index + 1, data, index, data.length - index - 1);
+ data[data.length - 1] = data[0];
+ if (tailIndex > 0) {
+ System.arraycopy(data, 1, data, 0, tailIndex);
+ this.tailIndex.value--;
+ } else {
+ this.tailIndex.value = data.length - 1;
+ }
+ }
+
+ if (tailIndex > 0) {
+ data[tailIndex - 1] = null;
+ } else {
+ data[data.length - 1] = null;
+ }
+
+ SIZE_UPDATER.decrementAndGet(this);
+ }
+
@Override
public int size() {
return SIZE_UPDATER.get(this);
@@ -283,6 +342,35 @@ public int size() {
throw new UnsupportedOperationException();
}
+ public List<T> toList() {
+ List<T> list = new ArrayList<>(size());
+ forEach(list::add);
+ return list;
+ }
+
+ @Override
+ public void forEach(Consumer<? super T> action) {
+ tailLock.lock();
+ headLock.lock();
+
+ try {
+ int headIndex = this.headIndex.value;
+ int size = this.size;
+
+ for (int i = 0; i < size; i++) {
+ T item = data[headIndex];
+
+ action.accept(item);
+
+ headIndex = (headIndex + 1) & (data.length - 1);
+ }
+
+ } finally {
+ headLock.unlock();
+ tailLock.unlock();
+ }
+ }
+
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
diff --git a/pulsar-common/src/test/java/org/apache/pulsar/common/util/collections/GrowableArrayBlockingQueueTest.java b/pulsar-common/src/test/java/org/apache/pulsar/common/util/collections/GrowableArrayBlockingQueueTest.java
index 7c54bcf0f..25aec3afc 100644
--- a/pulsar-common/src/test/java/org/apache/pulsar/common/util/collections/GrowableArrayBlockingQueueTest.java
+++ b/pulsar-common/src/test/java/org/apache/pulsar/common/util/collections/GrowableArrayBlockingQueueTest.java
@@ -19,6 +19,7 @@
package org.apache.pulsar.common.util.collections;
import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
@@ -28,6 +29,7 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
import org.apache.pulsar.common.util.collections.GrowableArrayBlockingQueue;
import org.testng.annotations.Test;
@@ -75,6 +77,12 @@ public void simple() throws Exception {
queue.offer(4);
assertEquals(queue.toString(), "[1, 2, 3, 4]");
+ AtomicInteger value = new AtomicInteger(1);
+ queue.forEach(v -> {
+ assertEquals(v.intValue(), value.get());
+ value.incrementAndGet();
+ });
+
assertEquals(queue.size(), 4);
List<Integer> list = new ArrayList<>();
@@ -202,4 +210,74 @@ public void pollTimeout2() throws Exception {
latch.await();
}
+
+ @Test
+ public void removeTest() throws Exception {
+ BlockingQueue<Integer> queue = new GrowableArrayBlockingQueue<>(4);
+
+ assertEquals(queue.poll(), null);
+
+ assertTrue(queue.offer(1));
+ assertTrue(queue.offer(2));
+ assertTrue(queue.offer(3));
+
+ assertEquals(queue.size(), 3);
+
+ assertFalse(queue.remove(4));
+ assertEquals(queue.size(), 3);
+ assertEquals(queue.toString(), "[1, 2, 3]");
+
+ assertTrue(queue.remove(2));
+ assertEquals(queue.size(), 2);
+ assertEquals(queue.toString(), "[1, 3]");
+
+ assertTrue(queue.remove(3));
+ assertEquals(queue.size(), 1);
+ assertEquals(queue.toString(), "[1]");
+
+ assertTrue(queue.remove(1));
+ assertEquals(queue.size(), 0);
+ assertEquals(queue.toString(), "[]");
+
+ // Test queue rollover
+ queue.offer(1);
+ queue.offer(2);
+ queue.offer(3);
+
+ assertEquals(queue.poll().intValue(), 1);
+ assertEquals(queue.poll().intValue(), 2);
+
+ assertTrue(queue.offer(4));
+ assertTrue(queue.offer(5));
+
+ assertTrue(queue.remove(5));
+ assertEquals(queue.size(), 2);
+ assertEquals(queue.toString(), "[3, 4]");
+
+ queue.offer(6);
+ queue.offer(7);
+ assertTrue(queue.remove(6));
+ assertEquals(queue.size(), 3);
+ assertEquals(queue.toString(), "[3, 4, 7]");
+
+ queue.offer(8);
+ assertTrue(queue.remove(8));
+ assertEquals(queue.size(), 3);
+ assertEquals(queue.toString(), "[3, 4, 7]");
+
+ queue.offer(8);
+ assertEquals(queue.toString(), "[3, 4, 7, 8]");
+
+ assertTrue(queue.remove(4));
+ assertEquals(queue.size(), 3);
+ assertEquals(queue.toString(), "[3, 7, 8]");
+
+ assertTrue(queue.remove(8));
+ assertEquals(queue.size(), 2);
+ assertEquals(queue.toString(), "[3, 7]");
+
+ assertTrue(queue.remove(7));
+ assertEquals(queue.size(), 1);
+ assertEquals(queue.toString(), "[3]");
+ }
}
diff --git a/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/BrokerDiscoveryProvider.java b/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/BrokerDiscoveryProvider.java
index e8516ab74..a6e3cbce2 100644
--- a/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/BrokerDiscoveryProvider.java
+++ b/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/BrokerDiscoveryProvider.java
@@ -30,6 +30,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.bookkeeper.util.OrderedSafeExecutor;
import org.apache.pulsar.broker.authentication.AuthenticationDataSource;
import org.apache.pulsar.broker.PulsarServerException;
@@ -59,7 +60,8 @@
final GlobalZooKeeperCache globalZkCache;
private final AtomicInteger counter = new AtomicInteger();
- private final OrderedSafeExecutor orderedExecutor = new OrderedSafeExecutor(4, "pulsar-discovery-ordered");
+ private final OrderedScheduler orderedExecutor = OrderedScheduler.newSchedulerBuilder().numThreads(4)
+ .name("pulsar-discovery-ordered").build();
private final ScheduledExecutorService scheduledExecutorScheduler = Executors.newScheduledThreadPool(4,
new DefaultThreadFactory("pulsar-discovery"));
diff --git a/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/web/ZookeeperCacheLoader.java b/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/web/ZookeeperCacheLoader.java
index b56b2e58a..30f75be6b 100644
--- a/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/web/ZookeeperCacheLoader.java
+++ b/pulsar-discovery-service/src/main/java/org/apache/pulsar/discovery/service/web/ZookeeperCacheLoader.java
@@ -25,7 +25,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.pulsar.common.util.ObjectMapperFactory;
import org.apache.pulsar.policies.data.loadbalancer.LoadManagerReport;
import org.apache.pulsar.zookeeper.LocalZooKeeperCache;
@@ -53,7 +53,8 @@
private volatile List<LoadManagerReport> availableBrokers;
- private final OrderedSafeExecutor orderedExecutor = new OrderedSafeExecutor(8, "pulsar-discovery-ordered-cache");
+ private final OrderedScheduler orderedExecutor = OrderedScheduler.newSchedulerBuilder().numThreads(8)
+ .name("pulsar-discovery-ordered-cache").build();
private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(8,
new DefaultThreadFactory("pulsar-discovery-cache"));
diff --git a/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/BrokerDiscoveryProvider.java b/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/BrokerDiscoveryProvider.java
index d61a84aed..06e85800c 100644
--- a/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/BrokerDiscoveryProvider.java
+++ b/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/BrokerDiscoveryProvider.java
@@ -18,7 +18,7 @@
*/
package org.apache.pulsar.proxy.server;
-import static org.apache.bookkeeper.util.MathUtils.signSafeMod;
+import static org.apache.bookkeeper.common.util.MathUtils.signSafeMod;
import static org.apache.pulsar.broker.cache.ConfigurationCacheService.POLICIES;
import static org.apache.pulsar.common.util.ObjectMapperFactory.getThreadLocal;
@@ -30,7 +30,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.pulsar.broker.PulsarServerException;
import org.apache.pulsar.broker.authentication.AuthenticationDataSource;
import org.apache.pulsar.common.naming.TopicName;
@@ -58,7 +58,8 @@
final GlobalZooKeeperCache globalZkCache;
private final AtomicInteger counter = new AtomicInteger();
- private final OrderedSafeExecutor orderedExecutor = new OrderedSafeExecutor(4, "pulsar-proxy-ordered");
+ private final OrderedScheduler orderedExecutor = OrderedScheduler.newSchedulerBuilder().numThreads(4)
+ .name("pulsar-proxy-ordered").build();
private final ScheduledExecutorService scheduledExecutorScheduler = Executors.newScheduledThreadPool(4,
new DefaultThreadFactory("pulsar-proxy-scheduled-executor"));
diff --git a/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/util/ZookeeperCacheLoader.java b/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/util/ZookeeperCacheLoader.java
index 9df7e8d89..0ba1ea63c 100644
--- a/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/util/ZookeeperCacheLoader.java
+++ b/pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/util/ZookeeperCacheLoader.java
@@ -25,7 +25,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.pulsar.common.util.ObjectMapperFactory;
import org.apache.pulsar.policies.data.loadbalancer.LoadManagerReport;
import org.apache.pulsar.zookeeper.LocalZooKeeperCache;
@@ -54,7 +54,8 @@
private volatile Set<String> availableBrokersSet;
private volatile List<LoadManagerReport> availableBrokers;
- private final OrderedSafeExecutor orderedExecutor = new OrderedSafeExecutor(8, "pulsar-discovery-ordered-cache");
+ private final OrderedScheduler orderedExecutor = OrderedScheduler.newSchedulerBuilder().numThreads(8)
+ .name("pulsar-discovery-ordered-cache").build();
private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(8,
new DefaultThreadFactory("pulsar-discovery-cache"));
diff --git a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/WebSocketService.java b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/WebSocketService.java
index d7a349d7a..6573477d4 100644
--- a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/WebSocketService.java
+++ b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/WebSocketService.java
@@ -30,7 +30,7 @@
import javax.servlet.ServletException;
import javax.websocket.DeploymentException;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.pulsar.broker.PulsarServerException;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.authentication.AuthenticationService;
@@ -70,8 +70,8 @@
private final ScheduledExecutorService executor = Executors
.newScheduledThreadPool(WebSocketProxyConfiguration.WEBSOCKET_SERVICE_THREADS,
new DefaultThreadFactory("pulsar-websocket"));
- private final OrderedSafeExecutor orderedExecutor = new OrderedSafeExecutor(
- WebSocketProxyConfiguration.GLOBAL_ZK_THREADS, "pulsar-websocket-ordered");
+ private final OrderedScheduler orderedExecutor = OrderedScheduler.newSchedulerBuilder()
+ .numThreads(WebSocketProxyConfiguration.GLOBAL_ZK_THREADS).name("pulsar-websocket-ordered").build();
private GlobalZooKeeperCache globalZkCache;
private ZooKeeperClientFactory zkClientFactory;
private ServiceConfiguration config;
diff --git a/pulsar-zookeeper-utils/pom.xml b/pulsar-zookeeper-utils/pom.xml
index 47d42e411..0a621f03e 100644
--- a/pulsar-zookeeper-utils/pom.xml
+++ b/pulsar-zookeeper-utils/pom.xml
@@ -41,7 +41,7 @@
<dependency>
<groupId>org.apache.bookkeeper</groupId>
- <artifactId>bookkeeper-server</artifactId>
+ <artifactId>bookkeeper-server-shaded</artifactId>
<version>${bookkeeper.version}</version>
<exclusions>
<exclusion>
diff --git a/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/GlobalZooKeeperCache.java b/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/GlobalZooKeeperCache.java
index 572720c93..bd75e820e 100644
--- a/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/GlobalZooKeeperCache.java
+++ b/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/GlobalZooKeeperCache.java
@@ -28,7 +28,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.pulsar.zookeeper.ZooKeeperClientFactory.SessionType;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.ZooKeeper;
@@ -51,7 +51,7 @@
private final ScheduledExecutorService scheduledExecutor;
public GlobalZooKeeperCache(ZooKeeperClientFactory zkClientFactory, int zkSessionTimeoutMillis,
- String globalZkConnect, OrderedSafeExecutor orderedExecutor, ScheduledExecutorService scheduledExecutor) {
+ String globalZkConnect, OrderedScheduler orderedExecutor, ScheduledExecutorService scheduledExecutor) {
super(null, orderedExecutor, scheduledExecutor);
this.zlClientFactory = zkClientFactory;
this.zkSessionTimeoutMillis = zkSessionTimeoutMillis;
diff --git a/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/LocalZooKeeperCache.java b/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/LocalZooKeeperCache.java
index 69593cddf..23547dd24 100644
--- a/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/LocalZooKeeperCache.java
+++ b/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/LocalZooKeeperCache.java
@@ -20,7 +20,7 @@
import java.util.concurrent.ScheduledExecutorService;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
@@ -37,7 +37,7 @@
private static final Logger LOG = LoggerFactory.getLogger(LocalZooKeeperCache.class);
- public LocalZooKeeperCache(final ZooKeeper zk, final OrderedSafeExecutor executor,
+ public LocalZooKeeperCache(final ZooKeeper zk, final OrderedScheduler executor,
ScheduledExecutorService scheduledExecutor) {
super(zk, executor, scheduledExecutor);
}
diff --git a/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZkBookieRackAffinityMapping.java b/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZkBookieRackAffinityMapping.java
index 282d81b23..28b38e7f2 100644
--- a/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZkBookieRackAffinityMapping.java
+++ b/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZkBookieRackAffinityMapping.java
@@ -18,18 +18,18 @@
*/
package org.apache.pulsar.zookeeper;
+import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
-import org.apache.bookkeeper.client.RackawareEnsemblePlacementPolicy;
-import org.apache.bookkeeper.client.RackawareEnsemblePlacementPolicy.RackChangeNotifier;
+import org.apache.bookkeeper.client.RackChangeNotifier;
+import org.apache.bookkeeper.client.RackawareEnsemblePlacementPolicyImpl;
import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.net.AbstractDNSToSwitchMapping;
import org.apache.bookkeeper.net.BookieSocketAddress;
import org.apache.bookkeeper.net.NetworkTopology;
-import org.apache.bookkeeper.util.ZkUtils;
-import org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase;
+import org.apache.bookkeeper.zookeeper.ZooKeeperClient;
import org.apache.commons.configuration.Configuration;
import org.apache.pulsar.common.util.ObjectMapperFactory;
import org.apache.zookeeper.KeeperException;
@@ -45,16 +45,16 @@
* It provides the mapping of bookies to its rack from zookeeper.
*/
public class ZkBookieRackAffinityMapping extends AbstractDNSToSwitchMapping
- implements ZooKeeperCacheListener<Map<String, Map<BookieSocketAddress, BookieInfo>>>, RackChangeNotifier {
+ implements ZooKeeperCacheListener<Map<String, Map<String, BookieInfo>>>, RackChangeNotifier {
private static final Logger LOG = LoggerFactory.getLogger(ZkBookieRackAffinityMapping.class);
public static final String BOOKIE_INFO_ROOT_PATH = "/bookies";
- private ZooKeeperDataCache<Map<String, Map<BookieSocketAddress, BookieInfo>>> bookieMappingCache = null;
- private RackawareEnsemblePlacementPolicy rackawarePolicy = null;
+ private ZooKeeperDataCache<Map<String, Map<String, BookieInfo>>> bookieMappingCache = null;
+ private RackawareEnsemblePlacementPolicyImpl rackawarePolicy = null;
public static final ObjectMapper jsonMapper = ObjectMapperFactory.create();
- public static final TypeReference<Map<String, Map<BookieSocketAddress, BookieInfo>>> typeRef = new TypeReference<Map<String, Map<BookieSocketAddress, BookieInfo>>>() {
+ public static final TypeReference<Map<String, Map<String, BookieInfo>>> typeRef = new TypeReference<Map<String, Map<String, BookieInfo>>>() {
};
@Override
@@ -63,7 +63,7 @@ public void setConf(Configuration conf) {
bookieMappingCache = getAndSetZkCache(conf);
}
- private ZooKeeperDataCache<Map<String, Map<BookieSocketAddress, BookieInfo>>> getAndSetZkCache(Configuration conf) {
+ private ZooKeeperDataCache<Map<String, Map<String, BookieInfo>>> getAndSetZkCache(Configuration conf) {
ZooKeeperCache zkCache = null;
if (conf.getProperty(ZooKeeperCache.ZK_CACHE_INSTANCE) != null) {
zkCache = (ZooKeeperCache) conf.getProperty(ZooKeeperCache.ZK_CACHE_INSTANCE);
@@ -73,10 +73,9 @@ public void setConf(Configuration conf) {
if (conf instanceof ClientConfiguration) {
zkTimeout = ((ClientConfiguration) conf).getZkTimeout();
zkServers = ((ClientConfiguration) conf).getZkServers();
- ZooKeeperWatcherBase w = new ZooKeeperWatcherBase(zkTimeout) {
- };
try {
- ZooKeeper zkClient = ZkUtils.createConnectedZookeeperClient(zkServers, w);
+ ZooKeeper zkClient = ZooKeeperClient.newBuilder().connectString(zkServers)
+ .sessionTimeoutMs(zkTimeout).build();
zkCache = new ZooKeeperCache(zkClient) {
};
conf.addProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, zkCache);
@@ -87,7 +86,7 @@ public void setConf(Configuration conf) {
LOG.error("No zk configurations available");
}
}
- ZooKeeperDataCache<Map<String, Map<BookieSocketAddress, BookieInfo>>> zkDataCache = getZkBookieRackMappingCache(
+ ZooKeeperDataCache<Map<String, Map<String, BookieInfo>>> zkDataCache = getZkBookieRackMappingCache(
zkCache);
if (zkDataCache != null) {
zkDataCache.registerListener(this);
@@ -95,13 +94,13 @@ public void setConf(Configuration conf) {
return zkDataCache;
}
- public static ZooKeeperDataCache<Map<String, Map<BookieSocketAddress, BookieInfo>>> getZkBookieRackMappingCache(
+ public static ZooKeeperDataCache<Map<String, Map<String, BookieInfo>>> getZkBookieRackMappingCache(
ZooKeeperCache zkCache) {
- ZooKeeperDataCache<Map<String, Map<BookieSocketAddress, BookieInfo>>> zkDataCache = new ZooKeeperDataCache<Map<String, Map<BookieSocketAddress, BookieInfo>>>(
+ ZooKeeperDataCache<Map<String, Map<String, BookieInfo>>> zkDataCache = new ZooKeeperDataCache<Map<String, Map<String, BookieInfo>>>(
zkCache) {
@Override
- public Map<String, Map<BookieSocketAddress, BookieInfo>> deserialize(String key, byte[] content)
+ public Map<String, Map<String, BookieInfo>> deserialize(String key, byte[] content)
throws Exception {
LOG.info("Reloading the bookie rack affinity mapping cache.");
if (LOG.isDebugEnabled()) {
@@ -115,22 +114,22 @@ public void setConf(Configuration conf) {
}
@Override
- public List<String> resolve(List<BookieSocketAddress> bookieAddressList) {
+ public List<String> resolve(List<String> bookieAddressList) {
List<String> racks = new ArrayList<String>(bookieAddressList.size());
- for (BookieSocketAddress bookieAddress : bookieAddressList) {
+ for (String bookieAddress : bookieAddressList) {
racks.add(getRack(bookieAddress));
}
return racks;
}
- private String getRack(BookieSocketAddress bookieAddress) {
+ private String getRack(String bookieAddress) {
String rack = NetworkTopology.DEFAULT_RACK;
try {
if (bookieMappingCache != null) {
- Map<String, Map<BookieSocketAddress, BookieInfo>> allGroupsBookieMapping = bookieMappingCache
+ Map<String, Map<String, BookieInfo>> allGroupsBookieMapping = bookieMappingCache
.get(BOOKIE_INFO_ROOT_PATH)
.orElseThrow(() -> new KeeperException.NoNodeException(BOOKIE_INFO_ROOT_PATH));
- for (Map<BookieSocketAddress, BookieInfo> bookieMapping : allGroupsBookieMapping.values()) {
+ for (Map<String, BookieInfo> bookieMapping : allGroupsBookieMapping.values()) {
BookieInfo bookieInfo = bookieMapping.get(bookieAddress);
if (bookieInfo != null) {
rack = bookieInfo.getRack();
@@ -158,19 +157,25 @@ public void reloadCachedMappings() {
}
@Override
- public void onUpdate(String path, Map<String, Map<BookieSocketAddress, BookieInfo>> data, Stat stat) {
+ public void onUpdate(String path, Map<String, Map<String, BookieInfo>> data, Stat stat) {
if (rackawarePolicy != null) {
LOG.info("Bookie rack info updated to {}. Notifying rackaware policy.", data.toString());
List<BookieSocketAddress> bookieAddressList = new ArrayList<>();
- for (Map<BookieSocketAddress, BookieInfo> bookieMapping : data.values()) {
- bookieAddressList.addAll(bookieMapping.keySet());
+ for (Map<String, BookieInfo> bookieMapping : data.values()) {
+ for (String addr : bookieMapping.keySet()) {
+ try {
+ bookieAddressList.add(new BookieSocketAddress(addr));
+ } catch (UnknownHostException e) {
+ throw new RuntimeException(e);
+ }
+ }
}
rackawarePolicy.onBookieRackChange(bookieAddressList);
}
}
@Override
- public void registerRackChangeListener(RackawareEnsemblePlacementPolicy rackawarePolicy) {
+ public void registerRackChangeListener(RackawareEnsemblePlacementPolicyImpl rackawarePolicy) {
this.rackawarePolicy = rackawarePolicy;
}
diff --git a/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZkIsolatedBookieEnsemblePlacementPolicy.java b/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZkIsolatedBookieEnsemblePlacementPolicy.java
index edb4bdcd9..10ba67f5d 100644
--- a/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZkIsolatedBookieEnsemblePlacementPolicy.java
+++ b/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZkIsolatedBookieEnsemblePlacementPolicy.java
@@ -22,15 +22,18 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException;
-import org.apache.bookkeeper.client.EnsemblePlacementPolicy;
import org.apache.bookkeeper.client.RackawareEnsemblePlacementPolicy;
+import org.apache.bookkeeper.client.RackawareEnsemblePlacementPolicyImpl;
import org.apache.bookkeeper.conf.ClientConfiguration;
+import org.apache.bookkeeper.feature.FeatureProvider;
import org.apache.bookkeeper.net.BookieSocketAddress;
-import org.apache.bookkeeper.util.ZkUtils;
-import org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase;
+import org.apache.bookkeeper.net.DNSToSwitchMapping;
+import org.apache.bookkeeper.stats.StatsLogger;
+import org.apache.bookkeeper.zookeeper.ZooKeeperClient;
import org.apache.commons.configuration.Configuration;
import org.apache.pulsar.common.util.ObjectMapperFactory;
import org.apache.pulsar.zookeeper.ZooKeeperCache.Deserializer;
@@ -42,6 +45,8 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
+import io.netty.util.HashedWheelTimer;
+
public class ZkIsolatedBookieEnsemblePlacementPolicy extends RackawareEnsemblePlacementPolicy
implements Deserializer<Map<String, Map<BookieSocketAddress, BookieInfo>>> {
private static final Logger LOG = LoggerFactory.getLogger(ZkIsolatedBookieEnsemblePlacementPolicy.class);
@@ -60,7 +65,9 @@ public ZkIsolatedBookieEnsemblePlacementPolicy() {
}
@Override
- public EnsemblePlacementPolicy initialize(Configuration conf) {
+ public RackawareEnsemblePlacementPolicyImpl initialize(ClientConfiguration conf,
+ Optional<DNSToSwitchMapping> optionalDnsResolver, HashedWheelTimer timer, FeatureProvider featureProvider,
+ StatsLogger statsLogger) {
if (conf.getProperty(ISOLATION_BOOKIE_GROUPS) != null) {
String isolationGroupsString = (String) conf.getProperty(ISOLATION_BOOKIE_GROUPS);
if (!isolationGroupsString.isEmpty()) {
@@ -70,8 +77,8 @@ public EnsemblePlacementPolicy initialize(Configuration conf) {
bookieMappingCache = getAndSetZkCache(conf);
}
}
- super.initialize(conf);
- return this;
+
+ return super.initialize(conf, optionalDnsResolver, timer, featureProvider, statsLogger);
}
private ZooKeeperCache getAndSetZkCache(Configuration conf) {
@@ -84,10 +91,9 @@ private ZooKeeperCache getAndSetZkCache(Configuration conf) {
if (conf instanceof ClientConfiguration) {
zkTimeout = ((ClientConfiguration) conf).getZkTimeout();
zkServers = ((ClientConfiguration) conf).getZkServers();
- ZooKeeperWatcherBase w = new ZooKeeperWatcherBase(zkTimeout) {
- };
try {
- ZooKeeper zkClient = ZkUtils.createConnectedZookeeperClient(zkServers, w);
+ ZooKeeper zkClient = ZooKeeperClient.newBuilder().connectString(zkServers)
+ .sessionTimeoutMs(zkTimeout).build();
zkCache = new ZooKeeperCache(zkClient) {
};
conf.addProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, zkCache);
@@ -102,26 +108,29 @@ private ZooKeeperCache getAndSetZkCache(Configuration conf) {
}
@Override
- public ArrayList<BookieSocketAddress> newEnsemble(int ensembleSize, int writeQuorumSize,
- Set<BookieSocketAddress> excludeBookies) throws BKNotEnoughBookiesException {
+ public ArrayList<BookieSocketAddress> newEnsemble(int ensembleSize, int writeQuorumSize, int ackQuorumSize,
+ Map<String, byte[]> customMetadata, Set<BookieSocketAddress> excludeBookies)
+ throws BKNotEnoughBookiesException {
Set<BookieSocketAddress> blacklistedBookies = getBlacklistedBookies();
if (excludeBookies == null) {
excludeBookies = new HashSet<BookieSocketAddress>();
}
excludeBookies.addAll(blacklistedBookies);
- return super.newEnsemble(ensembleSize, writeQuorumSize, excludeBookies);
+ return super.newEnsemble(ensembleSize, writeQuorumSize, ackQuorumSize, customMetadata, excludeBookies);
}
@Override
- public BookieSocketAddress replaceBookie(BookieSocketAddress bookieToReplace,
- Set<BookieSocketAddress> currentEnsemble, Set<BookieSocketAddress> excludeBookies)
+ public BookieSocketAddress replaceBookie(int ensembleSize, int writeQuorumSize, int ackQuorumSize,
+ Map<String, byte[]> customMetadata, Set<BookieSocketAddress> currentEnsemble,
+ BookieSocketAddress bookieToReplace, Set<BookieSocketAddress> excludeBookies)
throws BKNotEnoughBookiesException {
Set<BookieSocketAddress> blacklistedBookies = getBlacklistedBookies();
if (excludeBookies == null) {
excludeBookies = new HashSet<BookieSocketAddress>();
}
excludeBookies.addAll(blacklistedBookies);
- return super.replaceBookie(bookieToReplace, currentEnsemble, excludeBookies);
+ return super.replaceBookie(ensembleSize, writeQuorumSize, ackQuorumSize, customMetadata, currentEnsemble,
+ bookieToReplace, excludeBookies);
}
private Set<BookieSocketAddress> getBlacklistedBookies() {
diff --git a/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZooKeeperCache.java b/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZooKeeperCache.java
index 8dcd31bad..d992c082f 100644
--- a/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZooKeeperCache.java
+++ b/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZooKeeperCache.java
@@ -36,7 +36,7 @@
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.bookkeeper.util.SafeRunnable;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.KeeperException.Code;
@@ -84,14 +84,14 @@
protected final AsyncLoadingCache<String, Entry<Object, Stat>> dataCache;
protected final Cache<String, Set<String>> childrenCache;
protected final Cache<String, Boolean> existsCache;
- private final OrderedSafeExecutor executor;
+ private final OrderedScheduler executor;
private final ScheduledExecutorService scheduledExecutor;
private boolean shouldShutdownExecutor = false;
public static final int cacheTimeOutInSec = 30;
protected AtomicReference<ZooKeeper> zkSession = new AtomicReference<ZooKeeper>(null);
- public ZooKeeperCache(ZooKeeper zkSession, OrderedSafeExecutor executor, ScheduledExecutorService scheduledExecutor) {
+ public ZooKeeperCache(ZooKeeper zkSession, OrderedScheduler executor, ScheduledExecutorService scheduledExecutor) {
checkNotNull(executor);
checkNotNull(scheduledExecutor);
this.executor = executor;
@@ -106,7 +106,7 @@ public ZooKeeperCache(ZooKeeper zkSession, OrderedSafeExecutor executor, Schedul
}
public ZooKeeperCache(ZooKeeper zkSession) {
- this(zkSession, new OrderedSafeExecutor(1, "zk-cache-executor"),
+ this(zkSession, OrderedScheduler.newSchedulerBuilder().numThreads(1).name("zk-cache-executor").build(),
Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("zk-cache-callback-executor")));
this.shouldShutdownExecutor = true;
}
@@ -316,12 +316,12 @@ public Boolean call() throws Exception {
} else {
exec.execute(() -> zkFuture.completeExceptionally(KeeperException.create(rc)));
}
- }, null);
+ }, null);
} catch (Exception e) {
LOG.warn("Failed to access zkSession for {} {}", path, e.getMessage(), e);
zkFuture.completeExceptionally(e);
}
-
+
return zkFuture;
}).thenAccept(result -> {
if (result != null) {
@@ -405,7 +405,7 @@ public void invalidateRoot(String root) {
}
}
}
-
+
public void stop() {
if (shouldShutdownExecutor) {
this.executor.shutdown();
diff --git a/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZookeeperBkClientFactoryImpl.java b/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZookeeperBkClientFactoryImpl.java
index f854df87c..c0f529999 100644
--- a/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZookeeperBkClientFactoryImpl.java
+++ b/pulsar-zookeeper-utils/src/main/java/org/apache/pulsar/zookeeper/ZookeeperBkClientFactoryImpl.java
@@ -18,105 +18,55 @@
*/
package org.apache.pulsar.zookeeper;
-import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.bookkeeper.util.SafeRunnable.safeRun;
-import java.lang.reflect.Constructor;
+import java.io.IOException;
import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy;
-import org.apache.bookkeeper.zookeeper.RetryPolicy;
import org.apache.bookkeeper.zookeeper.ZooKeeperClient;
-import org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase;
import org.apache.zookeeper.KeeperException;
-import org.apache.zookeeper.Watcher.Event;
import org.apache.zookeeper.ZooKeeper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.zookeeper.ZooKeeper.States;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
public class ZookeeperBkClientFactoryImpl implements ZooKeeperClientFactory {
+ private final OrderedScheduler executor;
+
+ public ZookeeperBkClientFactoryImpl(OrderedScheduler executor) {
+ this.executor = executor;
+ }
+
@Override
public CompletableFuture<ZooKeeper> create(String serverList, SessionType sessionType, int zkSessionTimeoutMillis) {
- boolean canBeReadOnly = sessionType == SessionType.AllowReadOnly;
-
CompletableFuture<ZooKeeper> future = new CompletableFuture<>();
- try {
- CompletableFuture<Void> internalFuture = new CompletableFuture<>();
+ executor.submit(safeRun(() -> {
try {
- Constructor<ZooKeeperClient> cn = (Constructor<ZooKeeperClient>) ZooKeeperClient.class
- .getDeclaredConstructor(String.class, int.class, ZooKeeperWatcherBase.class, RetryPolicy.class);
- cn.setAccessible(true);
- ZooKeeperWatcherBase zkWatch = new ZooKeeperWatcherBase(zkSessionTimeoutMillis);
- zkWatch.addChildWatcher((event) -> {
-
- if (event.getType() == Event.EventType.None) {
- switch (event.getState()) {
-
- case ConnectedReadOnly:
- checkArgument(canBeReadOnly);
- // Fall through
- case SyncConnected:
- // ZK session is ready to use
- internalFuture.complete(null);
- break;
-
- case Expired:
- internalFuture
- .completeExceptionally(KeeperException.create(KeeperException.Code.SESSIONEXPIRED));
- break;
-
- default:
- log.warn("Unexpected ZK event received: {}", event);
- break;
- }
- }
- });
- final ZooKeeperClient zk = cn.newInstance(serverList, zkSessionTimeoutMillis, zkWatch,
- new BoundExponentialBackoffRetryPolicy(zkSessionTimeoutMillis, zkSessionTimeoutMillis, 0));
-
- internalFuture.thenRun(() -> {
- log.info("ZooKeeper session established: {}", zk);
- // Sometimes: ZooKeeperWatcherBase triggers childwatch before setting zk.getState() = CONNECTED and
- // state is still in CONNECTING state which takes few more msec to update state CONNECTED
- completeFutureOnConnect(future, zk, System.currentTimeMillis() + zkSessionTimeoutMillis, Executors.newSingleThreadScheduledExecutor());
- }).exceptionally((exception) -> {
- log.error("Failed to establish ZooKeeper session: {}", exception.getMessage());
- future.completeExceptionally(exception);
- return null;
- });
-
- internalFuture.complete(null);
- } catch (Exception ex) {
- future.completeExceptionally(ex);
+ ZooKeeper zk = ZooKeeperClient.newBuilder().connectString(serverList)
+ .sessionTimeoutMs(zkSessionTimeoutMillis)
+ .connectRetryPolicy(new BoundExponentialBackoffRetryPolicy(zkSessionTimeoutMillis,
+ zkSessionTimeoutMillis, 0))
+ .build();
+
+ if (zk.getState() == States.CONNECTEDREADONLY && sessionType != SessionType.AllowReadOnly) {
+ future.completeExceptionally(new IllegalStateException("Cannot use a read-only session"));
+ }
+
+ log.info("ZooKeeper session established: {}", zk);
+ future.complete(zk);
+ } catch (IOException | KeeperException | InterruptedException exception) {
+ log.error("Failed to establish ZooKeeper session: {}", exception.getMessage());
+ future.completeExceptionally(exception);
}
-
- } catch (Exception e) {
- future.completeExceptionally(e);
- }
+ }, throwable -> {
+ future.completeExceptionally(throwable);
+ }));
return future;
}
-
- private void completeFutureOnConnect(CompletableFuture<ZooKeeper> future, ZooKeeperClient zk, long timeOutTime,
- ScheduledExecutorService executor) {
- if (zk.getState().isConnected()) {
- future.complete(zk);
- executor.shutdown();
- } else {
- if (System.currentTimeMillis() - timeOutTime < 0) {
- executor.schedule(() -> completeFutureOnConnect(future, zk, timeOutTime, executor), 100,
- TimeUnit.MILLISECONDS);
- } else {
- future.completeExceptionally(
- new IllegalStateException("zookeeper couldn't connect with in given zkSessionTimeout"));
- executor.shutdown();
- }
- }
- }
-
- private static final Logger log = LoggerFactory.getLogger(ZookeeperBkClientFactoryImpl.class);
}
diff --git a/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZkBookieRackAffinityMappingTest.java b/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZkBookieRackAffinityMappingTest.java
index 13739d306..78fed6250 100644
--- a/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZkBookieRackAffinityMappingTest.java
+++ b/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZkBookieRackAffinityMappingTest.java
@@ -24,17 +24,13 @@
import java.util.List;
import java.util.Map;
-import org.apache.bookkeeper.client.RackawareEnsemblePlacementPolicy;
import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.net.BookieSocketAddress;
import org.apache.bookkeeper.net.NetworkTopology;
import org.apache.bookkeeper.test.PortManager;
import org.apache.bookkeeper.util.ZkUtils;
-import org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase;
+import org.apache.bookkeeper.zookeeper.ZooKeeperClient;
import org.apache.pulsar.common.util.ObjectMapperFactory;
-import org.apache.pulsar.zookeeper.BookieInfo;
-import org.apache.pulsar.zookeeper.ZkBookieRackAffinityMapping;
-import org.apache.pulsar.zookeeper.ZooKeeperCache;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
@@ -60,8 +56,7 @@
public void setUp() throws Exception {
localZkS = new ZookeeperServerTest(LOCAL_ZOOKEEPER_PORT);
localZkS.start();
- ZooKeeperWatcherBase zkWatcherBase = new ZooKeeperWatcherBase(10000);
- localZkc = ZkUtils.createConnectedZookeeperClient("127.0.0.1" + ":" + LOCAL_ZOOKEEPER_PORT, zkWatcherBase);
+ localZkc = ZooKeeperClient.newBuilder().connectString("127.0.0.1:" + LOCAL_ZOOKEEPER_PORT).build();
BOOKIE1 = new BookieSocketAddress("127.0.0.1:3181");
BOOKIE2 = new BookieSocketAddress("127.0.0.2:3181");
BOOKIE3 = new BookieSocketAddress("127.0.0.3:3181");
@@ -86,7 +81,8 @@ public void testBasic() throws Exception {
bkClientConf1.setProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, new ZooKeeperCache(localZkc) {
});
mapping1.setConf(bkClientConf1);
- List<String> racks1 = mapping1.resolve(Lists.newArrayList(BOOKIE1, BOOKIE2, BOOKIE3));
+ List<String> racks1 = mapping1
+ .resolve(Lists.newArrayList(BOOKIE1.toString(), BOOKIE2.toString(), BOOKIE3.toString()));
assertEquals(racks1.get(0), "/rack0");
assertEquals(racks1.get(1), "/rack1");
assertEquals(racks1.get(2), NetworkTopology.DEFAULT_RACK);
@@ -98,7 +94,8 @@ public void testBasic() throws Exception {
bkClientConf2.setZkServers("127.0.0.1" + ":" + LOCAL_ZOOKEEPER_PORT);
bkClientConf2.setZkTimeout(1000);
mapping2.setConf(bkClientConf2);
- List<String> racks2 = mapping2.resolve(Lists.newArrayList(BOOKIE1, BOOKIE2, BOOKIE3));
+ List<String> racks2 = mapping2
+ .resolve(Lists.newArrayList(BOOKIE1.toString(), BOOKIE2.toString(), BOOKIE3.toString()));
assertEquals(racks2.get(0), "/rack0");
assertEquals(racks2.get(1), "/rack1");
assertEquals(racks2.get(2), NetworkTopology.DEFAULT_RACK);
@@ -113,7 +110,8 @@ public void testNoBookieInfo() throws Exception {
bkClientConf.setProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, new ZooKeeperCache(localZkc) {
});
mapping.setConf(bkClientConf);
- List<String> racks = mapping.resolve(Lists.newArrayList(BOOKIE1, BOOKIE2, BOOKIE3));
+ List<String> racks = mapping
+ .resolve(Lists.newArrayList(BOOKIE1.toString(), BOOKIE2.toString(), BOOKIE3.toString()));
assertEquals(racks.get(0), NetworkTopology.DEFAULT_RACK);
assertEquals(racks.get(1), NetworkTopology.DEFAULT_RACK);
assertEquals(racks.get(2), NetworkTopology.DEFAULT_RACK);
@@ -136,7 +134,7 @@ public void testNoBookieInfo() throws Exception {
Thread.sleep(100);
- racks = mapping.resolve(Lists.newArrayList(BOOKIE1, BOOKIE2, BOOKIE3));
+ racks = mapping.resolve(Lists.newArrayList(BOOKIE1.toString(), BOOKIE2.toString(), BOOKIE3.toString()));
assertEquals(racks.get(0), "/rack0");
assertEquals(racks.get(1), "/rack1");
assertEquals(racks.get(2), NetworkTopology.DEFAULT_RACK);
@@ -167,7 +165,8 @@ public void testBookieInfoChange() throws Exception {
bkClientConf.setProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, new ZooKeeperCache(localZkc) {
});
mapping.setConf(bkClientConf);
- List<String> racks = mapping.resolve(Lists.newArrayList(BOOKIE1, BOOKIE2, BOOKIE3));
+ List<String> racks = mapping
+ .resolve(Lists.newArrayList(BOOKIE1.toString(), BOOKIE2.toString(), BOOKIE3.toString()));
assertEquals(racks.get(0), "/rack0");
assertEquals(racks.get(1), "/rack1");
assertEquals(racks.get(2), NetworkTopology.DEFAULT_RACK);
@@ -185,7 +184,7 @@ public void testBookieInfoChange() throws Exception {
// wait for the zk to notify and update the mappings
Thread.sleep(100);
- racks = mapping.resolve(Lists.newArrayList(BOOKIE1, BOOKIE2, BOOKIE3));
+ racks = mapping.resolve(Lists.newArrayList(BOOKIE1.toString(), BOOKIE2.toString(), BOOKIE3.toString()));
assertEquals(racks.get(0), "/rack0");
assertEquals(racks.get(1), "/rack1");
assertEquals(racks.get(2), "/rack0");
@@ -194,7 +193,7 @@ public void testBookieInfoChange() throws Exception {
Thread.sleep(100);
- racks = mapping.resolve(Lists.newArrayList(BOOKIE1, BOOKIE2, BOOKIE3));
+ racks = mapping.resolve(Lists.newArrayList(BOOKIE1.toString(), BOOKIE2.toString(), BOOKIE3.toString()));
assertEquals(racks.get(0), NetworkTopology.DEFAULT_RACK);
assertEquals(racks.get(1), NetworkTopology.DEFAULT_RACK);
assertEquals(racks.get(2), NetworkTopology.DEFAULT_RACK);
diff --git a/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZkIsolatedBookieEnsemblePlacementPolicyTest.java b/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZkIsolatedBookieEnsemblePlacementPolicyTest.java
index e3b598f0b..4a437055c 100644
--- a/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZkIsolatedBookieEnsemblePlacementPolicyTest.java
+++ b/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZkIsolatedBookieEnsemblePlacementPolicyTest.java
@@ -23,23 +23,23 @@
import static org.testng.Assert.fail;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException;
import org.apache.bookkeeper.conf.ClientConfiguration;
+import org.apache.bookkeeper.feature.SettableFeatureProvider;
import org.apache.bookkeeper.net.BookieSocketAddress;
+import org.apache.bookkeeper.stats.NullStatsLogger;
import org.apache.bookkeeper.test.PortManager;
import org.apache.bookkeeper.util.ZkUtils;
-import org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase;
+import org.apache.bookkeeper.zookeeper.ZooKeeperClient;
import org.apache.pulsar.common.util.ObjectMapperFactory;
-import org.apache.pulsar.zookeeper.BookieInfo;
-import org.apache.pulsar.zookeeper.ZkBookieRackAffinityMapping;
-import org.apache.pulsar.zookeeper.ZkIsolatedBookieEnsemblePlacementPolicy;
-import org.apache.pulsar.zookeeper.ZooKeeperCache;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
@@ -49,6 +49,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
+import io.netty.util.HashedWheelTimer;
+
public class ZkIsolatedBookieEnsemblePlacementPolicyTest {
private static final String BOOKIE1 = "127.0.0.1:3181";
@@ -65,12 +67,15 @@
Set<BookieSocketAddress> readOnlyBookies = new HashSet<>();
List<String> isolationGroups = new ArrayList<>();
+ HashedWheelTimer timer;
+
@BeforeMethod
public void setUp() throws Exception {
+ timer = new HashedWheelTimer();
localZkS = new ZookeeperServerTest(LOCAL_ZOOKEEPER_PORT);
localZkS.start();
- ZooKeeperWatcherBase zkWatcherBase = new ZooKeeperWatcherBase(10000);
- localZkc = ZkUtils.createConnectedZookeeperClient("127.0.0.1" + ":" + LOCAL_ZOOKEEPER_PORT, zkWatcherBase);
+
+ localZkc = ZooKeeperClient.newBuilder().connectString("127.0.0.1" + ":" + LOCAL_ZOOKEEPER_PORT).build();
writableBookies.add(new BookieSocketAddress(BOOKIE1));
writableBookies.add(new BookieSocketAddress(BOOKIE2));
writableBookies.add(new BookieSocketAddress(BOOKIE3));
@@ -83,6 +88,7 @@ void teardown() throws Exception {
writableBookies.clear();
isolationGroups.clear();
localZkS.close();
+ timer.stop();
}
@Test
@@ -116,19 +122,19 @@ public void testBasic() throws Exception {
bkClientConf.setProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, new ZooKeeperCache(localZkc) {
});
bkClientConf.setProperty(ZkIsolatedBookieEnsemblePlacementPolicy.ISOLATION_BOOKIE_GROUPS, isolationGroups);
- isolationPolicy.initialize(bkClientConf);
+ isolationPolicy.initialize(bkClientConf, Optional.empty(), timer, SettableFeatureProvider.DISABLE_ALL, NullStatsLogger.INSTANCE);
isolationPolicy.onClusterChanged(writableBookies, readOnlyBookies);
- ArrayList<BookieSocketAddress> ensemble = isolationPolicy.newEnsemble(3, 3, new HashSet<>());
+ ArrayList<BookieSocketAddress> ensemble = isolationPolicy.newEnsemble(3, 3, 2, Collections.emptyMap(), new HashSet<>());
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE1)));
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE2)));
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE4)));
- ensemble = isolationPolicy.newEnsemble(1, 1, new HashSet<>());
+ ensemble = isolationPolicy.newEnsemble(1, 1, 1, Collections.emptyMap(), new HashSet<>());
assertFalse(ensemble.contains(new BookieSocketAddress(BOOKIE3)));
try {
- isolationPolicy.newEnsemble(4, 4, new HashSet<>());
+ isolationPolicy.newEnsemble(4, 4, 4, Collections.emptyMap(), new HashSet<>());
fail("should not pass");
} catch (BKNotEnoughBookiesException e) {
// ok
@@ -136,7 +142,7 @@ public void testBasic() throws Exception {
Set<BookieSocketAddress> bookieToExclude = new HashSet<>();
bookieToExclude.add(new BookieSocketAddress(BOOKIE1));
- ensemble = isolationPolicy.newEnsemble(2, 2, bookieToExclude);
+ ensemble = isolationPolicy.newEnsemble(2, 2, 2, Collections.emptyMap(), bookieToExclude);
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE4)));
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE2)));
@@ -150,20 +156,21 @@ public void testBasic() throws Exception {
Thread.sleep(100);
- ensemble = isolationPolicy.newEnsemble(2, 2, null);
+ ensemble = isolationPolicy.newEnsemble(2, 2, 2, Collections.emptyMap(), null);
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE1)));
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE2)));
try {
- isolationPolicy.newEnsemble(3, 3, new HashSet<>());
+ isolationPolicy.newEnsemble(3, 3, 3, Collections.emptyMap(), new HashSet<>());
fail("should not pass");
} catch (BKNotEnoughBookiesException e) {
// ok
}
try {
- isolationPolicy.replaceBookie(new BookieSocketAddress(BOOKIE5), new HashSet<>(ensemble), new HashSet<>());
+ isolationPolicy.replaceBookie(3, 3, 3, Collections.emptyMap(), new HashSet<>(ensemble),
+ new BookieSocketAddress(BOOKIE5), new HashSet<>());
fail("should not pass");
} catch (BKNotEnoughBookiesException e) {
// ok
@@ -171,9 +178,9 @@ public void testBasic() throws Exception {
bookieToExclude = new HashSet<>();
bookieToExclude.add(new BookieSocketAddress(BOOKIE1));
- ensemble = isolationPolicy.newEnsemble(1, 1, bookieToExclude);
- BookieSocketAddress chosenBookie = isolationPolicy.replaceBookie(new BookieSocketAddress(BOOKIE5),
- new HashSet<>(ensemble), null);
+ ensemble = isolationPolicy.newEnsemble(1, 1, 1, Collections.emptyMap(), bookieToExclude);
+ BookieSocketAddress chosenBookie = isolationPolicy.replaceBookie(1, 1, 1, Collections.emptyMap(),
+ new HashSet<>(ensemble), new BookieSocketAddress(BOOKIE5), new HashSet<>());
assertTrue(chosenBookie.equals(new BookieSocketAddress(BOOKIE1)));
localZkc.delete(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, -1);
@@ -186,10 +193,10 @@ public void testNoBookieInfo() throws Exception {
bkClientConf.setProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, new ZooKeeperCache(localZkc) {
});
bkClientConf.setProperty(ZkIsolatedBookieEnsemblePlacementPolicy.ISOLATION_BOOKIE_GROUPS, isolationGroups);
- isolationPolicy.initialize(bkClientConf);
+ isolationPolicy.initialize(bkClientConf, Optional.empty(), timer, SettableFeatureProvider.DISABLE_ALL, NullStatsLogger.INSTANCE);
isolationPolicy.onClusterChanged(writableBookies, readOnlyBookies);
- isolationPolicy.newEnsemble(4, 4, new HashSet<>());
+ isolationPolicy.newEnsemble(4, 4, 4, Collections.emptyMap(), new HashSet<>());
String data = "{\"group1\": {\"" + BOOKIE1
+ "\": {\"rack\": \"rack0\", \"hostname\": \"bookie1.example.com\"}, \"" + BOOKIE2
@@ -202,12 +209,12 @@ public void testNoBookieInfo() throws Exception {
Thread.sleep(100);
- ArrayList<BookieSocketAddress> ensemble = isolationPolicy.newEnsemble(2, 2, new HashSet<>());
+ ArrayList<BookieSocketAddress> ensemble = isolationPolicy.newEnsemble(2, 2, 2, Collections.emptyMap(), new HashSet<>());
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE1)));
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE2)));
try {
- isolationPolicy.newEnsemble(3, 3, new HashSet<>());
+ isolationPolicy.newEnsemble(3, 3, 3, Collections.emptyMap(), new HashSet<>());
fail("should not pass");
} catch (BKNotEnoughBookiesException e) {
// ok
@@ -251,15 +258,15 @@ public void testBookieInfoChange() throws Exception {
bkClientConf.setZkServers("127.0.0.1" + ":" + LOCAL_ZOOKEEPER_PORT);
bkClientConf.setZkTimeout(1000);
bkClientConf.setProperty(ZkIsolatedBookieEnsemblePlacementPolicy.ISOLATION_BOOKIE_GROUPS, isolationGroups);
- isolationPolicy.initialize(bkClientConf);
+ isolationPolicy.initialize(bkClientConf, Optional.empty(), timer, SettableFeatureProvider.DISABLE_ALL, NullStatsLogger.INSTANCE);
isolationPolicy.onClusterChanged(writableBookies, readOnlyBookies);
- ArrayList<BookieSocketAddress> ensemble = isolationPolicy.newEnsemble(2, 2, new HashSet<>());
+ ArrayList<BookieSocketAddress> ensemble = isolationPolicy.newEnsemble(2, 2, 2, Collections.emptyMap(), new HashSet<>());
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE1)));
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE2)));
try {
- isolationPolicy.newEnsemble(3, 3, new HashSet<>());
+ isolationPolicy.newEnsemble(3, 3, 3, Collections.emptyMap(), new HashSet<>());
fail("should not pass");
} catch (BKNotEnoughBookiesException e) {
// ok
@@ -276,7 +283,7 @@ public void testBookieInfoChange() throws Exception {
// wait for the zk to notify and update the mappings
Thread.sleep(100);
- ensemble = isolationPolicy.newEnsemble(3, 3, new HashSet<>());
+ ensemble = isolationPolicy.newEnsemble(3, 3, 3, Collections.emptyMap(), new HashSet<>());
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE1)));
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE2)));
assertTrue(ensemble.contains(new BookieSocketAddress(BOOKIE3)));
@@ -285,7 +292,7 @@ public void testBookieInfoChange() throws Exception {
Thread.sleep(100);
- isolationPolicy.newEnsemble(1, 1, new HashSet<>());
+ isolationPolicy.newEnsemble(1, 1, 1, Collections.emptyMap(), new HashSet<>());
}
@Test
@@ -305,9 +312,10 @@ public void testNoIsolationGroup() throws Exception {
ClientConfiguration bkClientConf = new ClientConfiguration();
bkClientConf.setProperty(ZooKeeperCache.ZK_CACHE_INSTANCE, new ZooKeeperCache(localZkc) {
});
- isolationPolicy.initialize(bkClientConf);
+ isolationPolicy.initialize(bkClientConf, Optional.empty(), timer, SettableFeatureProvider.DISABLE_ALL,
+ NullStatsLogger.INSTANCE);
isolationPolicy.onClusterChanged(writableBookies, readOnlyBookies);
- isolationPolicy.newEnsemble(4, 4, new HashSet<>());
+ isolationPolicy.newEnsemble(4, 4, 4, Collections.emptyMap(), new HashSet<>());
}
}
diff --git a/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZookeeperBkClientFactoryImplTest.java b/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZookeeperBkClientFactoryImplTest.java
index 18f95d9c0..bef6a3185 100644
--- a/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZookeeperBkClientFactoryImplTest.java
+++ b/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZookeeperBkClientFactoryImplTest.java
@@ -20,10 +20,13 @@
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.bookkeeper.test.PortManager;
import org.apache.pulsar.zookeeper.ZookeeperBkClientFactoryImpl;
import org.apache.zookeeper.ZooKeeper;
@@ -41,9 +44,11 @@
private ZooKeeper localZkc;
private final int LOCAL_ZOOKEEPER_PORT = PortManager.nextFreePort();
private final long ZOOKEEPER_SESSION_TIMEOUT_MILLIS = 1000;
+ private OrderedScheduler executor;
@BeforeMethod
void setup() throws Exception {
+ executor = OrderedScheduler.newSchedulerBuilder().build();
localZkS = new ZookeeperServerTest(LOCAL_ZOOKEEPER_PORT);
localZkS.start();
}
@@ -51,11 +56,12 @@ void setup() throws Exception {
@AfterMethod
void teardown() throws Exception {
localZkS.close();
+ executor.shutdown();
}
@Test
void testZKCreationRW() throws Exception {
- ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl();
+ ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl(executor);
CompletableFuture<ZooKeeper> zkFuture = zkf.create("127.0.0.1:" + LOCAL_ZOOKEEPER_PORT, SessionType.ReadWrite,
(int) ZOOKEEPER_SESSION_TIMEOUT_MILLIS);
localZkc = zkFuture.get(ZOOKEEPER_SESSION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
@@ -66,7 +72,7 @@ void testZKCreationRW() throws Exception {
@Test
void testZKCreationRO() throws Exception {
- ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl();
+ ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl(executor);
CompletableFuture<ZooKeeper> zkFuture = zkf.create("127.0.0.1:" + LOCAL_ZOOKEEPER_PORT,
SessionType.AllowReadOnly, (int) ZOOKEEPER_SESSION_TIMEOUT_MILLIS);
localZkc = zkFuture.get(ZOOKEEPER_SESSION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
@@ -76,9 +82,15 @@ void testZKCreationRO() throws Exception {
@Test
void testZKCreationFailure() throws Exception {
- ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl();
+ ZooKeeperClientFactory zkf = new ZookeeperBkClientFactoryImpl(executor);
CompletableFuture<ZooKeeper> zkFuture = zkf.create("invalid", SessionType.ReadWrite,
(int) ZOOKEEPER_SESSION_TIMEOUT_MILLIS);
- assertTrue(zkFuture.isCompletedExceptionally());
+
+ try {
+ zkFuture.get();
+ fail("Creation should have failed");
+ } catch (ExecutionException e) {
+ // Expected
+ }
}
}
diff --git a/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZookeeperCacheTest.java b/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZookeeperCacheTest.java
index 754b65dec..44aa486fc 100644
--- a/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZookeeperCacheTest.java
+++ b/pulsar-zookeeper-utils/src/test/java/org/apache/pulsar/zookeeper/ZookeeperCacheTest.java
@@ -33,10 +33,9 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
-import org.apache.bookkeeper.util.OrderedSafeExecutor;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
import org.apache.zookeeper.KeeperException.Code;
import org.apache.zookeeper.MockZooKeeper;
import org.apache.zookeeper.WatchedEvent;
@@ -44,7 +43,9 @@
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.testng.Assert;
+import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@@ -57,6 +58,8 @@
@Test
public class ZookeeperCacheTest {
private MockZooKeeper zkClient;
+ private OrderedScheduler executor;
+ private ScheduledExecutorService scheduledExecutor;
@BeforeMethod
void setup() throws Exception {
@@ -68,10 +71,22 @@ void teardown() throws Exception {
zkClient.shutdown();
}
+ @BeforeClass
+ void classSetup() throws Exception {
+ executor = OrderedScheduler.newSchedulerBuilder().maxTasksInQueue(1).name("ZookeeperCacheTest").build();
+ scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
+ }
+
+ @AfterClass
+ void classTeardown() throws Exception {
+ executor.shutdown();
+ scheduledExecutor.shutdown();
+ }
+
+
@Test
void testSimpleCache() throws Exception {
- OrderedSafeExecutor executor = new OrderedSafeExecutor(1, "test");
- ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
+
ZooKeeperCache zkCacheService = new LocalZooKeeperCache(zkClient, executor, scheduledExecutor);
ZooKeeperDataCache<String> zkCache = new ZooKeeperDataCache<String>(zkCacheService) {
@Override
@@ -106,15 +121,10 @@ public String deserialize(String key, byte[] content) throws Exception {
} catch (Exception e) {
// Ok
}
- executor.shutdown();
- scheduledExecutor.shutdown();
}
@Test
void testChildrenCache() throws Exception {
- OrderedSafeExecutor executor = new OrderedSafeExecutor(1, "test");
- ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
-
zkClient.create("/test", new byte[0], null, null);
ZooKeeperCache zkCacheService = new LocalZooKeeperCache(zkClient, executor, scheduledExecutor);
@@ -163,15 +173,10 @@ void testChildrenCache() throws Exception {
}
assertEquals(notificationCount.get(), 3);
- executor.shutdown();
- scheduledExecutor.shutdown();
}
@Test
void testExistsCache() throws Exception {
- OrderedSafeExecutor executor = new OrderedSafeExecutor(1, "test");
- ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
-
// Check existence after creation of the node
zkClient.create("/test", new byte[0], null, null);
Thread.sleep(20);
@@ -184,15 +189,10 @@ void testExistsCache() throws Exception {
Thread.sleep(20);
boolean shouldNotExist = zkCacheService.exists("/test");
Assert.assertFalse(shouldNotExist, "/test should not exist in the cache");
- executor.shutdown();
- scheduledExecutor.shutdown();
}
@Test
void testInvalidateCache() throws Exception {
- OrderedSafeExecutor executor = new OrderedSafeExecutor(1, "test");
- ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
-
zkClient.create("/test", new byte[0], null, null);
zkClient.create("/test/c1", new byte[0], null, null);
zkClient.create("/test/c2", new byte[0], null, null);
@@ -222,14 +222,10 @@ void testInvalidateCache() throws Exception {
assertNotNull(zkCacheService.getChildren("/test"));
zkCacheService.invalidateRoot("/test");
assertNull(zkCacheService.getChildrenIfPresent("/test"));
- executor.shutdown();
- scheduledExecutor.shutdown();
}
@Test
void testGlobalZooKeeperCache() throws Exception {
- OrderedSafeExecutor executor = new OrderedSafeExecutor(1, "test");
- ScheduledExecutorService scheduledExecutor = new ScheduledThreadPoolExecutor(1);
MockZooKeeper zkc = MockZooKeeper.newInstance();
ZooKeeperClientFactory zkClientfactory = new ZooKeeperClientFactory() {
@Override
@@ -324,8 +320,6 @@ public String deserialize(String key, byte[] content) throws Exception {
assertEquals(zkCache.get("/other2").get(), newValue);
zkCacheService.close();
- executor.shutdown();
- scheduledExecutor.shutdown();
// Update shouldn't happen after the last check
assertEquals(notificationCount.get(), 1);
@@ -339,7 +333,7 @@ public String deserialize(String key, byte[] content) throws Exception {
*/
@Test(timeOut = 2000)
void testZkCallbackThreadStuck() throws Exception {
- OrderedSafeExecutor executor = new OrderedSafeExecutor(1, "test");
+ OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2);
ExecutorService zkExecutor = Executors.newSingleThreadExecutor(new DefaultThreadFactory("mockZk"));
// add readOpDelayMs so, main thread will not serve zkCacahe-returned future and let zkExecutor-thread handle
@@ -390,8 +384,6 @@ public String deserialize(String key, byte[] content) throws Exception {
@Test
public void testInvalidateCacheOnFailure() throws Exception {
ExecutorService zkExecutor = Executors.newSingleThreadExecutor(new DefaultThreadFactory("mockZk"));
- OrderedSafeExecutor executor = new OrderedSafeExecutor(1, "test");
- ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
// add readOpDelayMs so, main thread will not serve zkCacahe-returned future and let zkExecutor-thread handle
// callback-result process
MockZooKeeper zkClient = MockZooKeeper.newInstance(zkExecutor, 100);
@@ -446,8 +438,5 @@ public String deserialize(String key, byte[] content) throws Exception {
// (6) now, cache should be invalidate failed-future and should refetch the data
assertEquals(zkCache.getAsync(key1).get().get(), value);
zkExecutor.shutdown();
- executor.shutdown();
- scheduledExecutor.shutdown();
-
}
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services