Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In unit tests, do ZK cache reloads in same thread, to avoid race conditions #31

Merged
merged 1 commit into from
Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}