Skip to content

Commit

Permalink
In unit tests, do ZK cache reloads in same thread, to avoid race cond…
Browse files Browse the repository at this point in the history
…itions
  • Loading branch information
merlimat committed Sep 22, 2016
1 parent afc771b commit 7dc412f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void close() throws PulsarServerException {
adminClient.close();
adminClient = null;
}

nsservice = null;

// executor is not initialized in mocks even when real close method is called
Expand Down Expand Up @@ -357,10 +357,10 @@ private void startZkCacheService() throws PulsarServerException {

LOG.info("starting configuration cache service");

this.localZkCache = new LocalZooKeeperCache(getZkClient(), this.orderedExecutor);
this.localZkCache = new LocalZooKeeperCache(getZkClient(), getOrderedExecutor());
this.globalZkCache = new GlobalZooKeeperCache(getZooKeeperClientFactory(),
(int) config.getZooKeeperSessionTimeoutMillis(), config.getGlobalZookeeperServers(),
this.orderedExecutor, this.executor);
getOrderedExecutor(), this.executor);
try {
this.globalZkCache.start();
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public abstract class MockedPulsarServiceBaseTest {
protected MockZooKeeper mockZookKeeper;
protected NonClosableMockBookKeeper mockBookKeeper;

private SameThreadOrderedSafeExecutor sameThreadOrderedSafeExecutor;

public MockedPulsarServiceBaseTest() {
this.conf = new ServiceConfiguration();
this.conf.setBrokerServicePort(BROKER_PORT);
Expand Down Expand Up @@ -96,6 +98,8 @@ private final void init() throws Exception {
mockZookKeeper = createMockZooKeeper();
mockBookKeeper = new NonClosableMockBookKeeper(new ClientConfiguration(), mockZookKeeper);

sameThreadOrderedSafeExecutor = new SameThreadOrderedSafeExecutor();

startBroker();

brokerUrl = new URL("http://localhost:" + BROKER_WEBSERVICE_PORT);
Expand All @@ -110,6 +114,7 @@ protected final void internalCleanup() throws Exception {
pulsar.close();
mockBookKeeper.reallyShutdow();
mockZookKeeper.shutdown();
sameThreadOrderedSafeExecutor.shutdown();
}

protected abstract void setup() throws Exception;
Expand Down Expand Up @@ -146,6 +151,8 @@ protected void setupBrokerMocks(PulsarService pulsar) throws Exception {

Supplier<NamespaceService> namespaceServiceSupplier = () -> spy(new NamespaceService(pulsar));
doReturn(namespaceServiceSupplier).when(pulsar).getNamespaceServiceProvider();

doReturn(sameThreadOrderedSafeExecutor).when(pulsar).getOrderedExecutor();
}

private MockZooKeeper createMockZooKeeper() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright 2016 Yahoo Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yahoo.pulsar.broker.auth;

import org.apache.bookkeeper.util.OrderedSafeExecutor;
import org.apache.bookkeeper.util.SafeRunnable;

public class SameThreadOrderedSafeExecutor extends OrderedSafeExecutor {

public SameThreadOrderedSafeExecutor() {
super(1, "ordered-executor");
}

@Override
public void submit(SafeRunnable r) {
r.run();
}

@Override
public void submitOrdered(int orderingKey, SafeRunnable r) {
r.run();
}

@Override
public void submitOrdered(long orderingKey, SafeRunnable r) {
r.run();
}

@Override
public void submitOrdered(Object orderingKey, SafeRunnable r) {
r.run();
}
}

0 comments on commit 7dc412f

Please sign in to comment.