-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[Broker] Fix LeaderElectionService.getCurrentLeader and add support for empheralOwner in MockZooKeeper #13066
Merged
lhotari
merged 14 commits into
apache:master
from
lhotari:lh-fix-LeaderElectionService.getCurrentLeader
Dec 2, 2021
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9a838b7
Add leader election unit test that uses multiple brokers
lhotari f5a081c
Support empheralOwner in MockZooKeeper
lhotari c1478c8
Use unique sessions for all brokers
lhotari 0cb7148
Add failing test case for leader broker information not available on …
lhotari ed055f6
Add test for reading the current leader
lhotari ebeb584
Fix issue in leader election
lhotari 0d8dd59
Address review feedback: make methods static
lhotari 4ee02d7
Use unique-session only in MultiBrokerBaseTests
lhotari 1191f15
Move tenant and namespace creation to it's own method
lhotari 1834141
Improve cleanup
lhotari 5a3cfa4
Add alwaysRun to BeforeClass
lhotari b8d81ad
Fix leaks in locking in MockZooKeeper
lhotari f68da4c
Reduce code duplication
lhotari 331329e
Fix NPE when CreateMode is null
lhotari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
pulsar-broker/src/test/java/org/apache/pulsar/broker/MultiBrokerBaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/** | ||
* 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.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest; | ||
import org.apache.pulsar.client.admin.PulsarAdmin; | ||
import org.apache.pulsar.client.admin.PulsarAdminBuilder; | ||
import org.apache.pulsar.client.admin.PulsarAdminException; | ||
import org.apache.pulsar.client.api.PulsarClient; | ||
import org.apache.pulsar.client.api.PulsarClientException; | ||
import org.apache.pulsar.metadata.impl.ZKMetadataStore; | ||
import org.apache.zookeeper.MockZooKeeperSession; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
|
||
public abstract class MultiBrokerBaseTest extends MockedPulsarServiceBaseTest { | ||
protected List<PulsarService> additionalBrokers; | ||
protected List<PulsarAdmin> additionalBrokerAdmins; | ||
protected List<PulsarClient> additionalBrokerClients; | ||
|
||
protected int numberOfAdditionalBrokers() { | ||
return 2; | ||
} | ||
|
||
@BeforeClass(alwaysRun = true) | ||
@Override | ||
public final void setup() throws Exception { | ||
super.internalSetup(); | ||
additionalBrokersSetup(); | ||
pulsarResourcesSetup(); | ||
} | ||
|
||
protected void pulsarResourcesSetup() throws PulsarAdminException { | ||
admin.tenants().createTenant("public", createDefaultTenantInfo()); | ||
admin.namespaces() | ||
.createNamespace("public/default", getPulsar().getConfiguration().getDefaultNumberOfNamespaceBundles()); | ||
} | ||
|
||
protected void additionalBrokersSetup() throws Exception { | ||
int numberOfAdditionalBrokers = numberOfAdditionalBrokers(); | ||
additionalBrokers = new ArrayList<>(numberOfAdditionalBrokers); | ||
additionalBrokerAdmins = new ArrayList<>(numberOfAdditionalBrokers); | ||
additionalBrokerClients = new ArrayList<>(numberOfAdditionalBrokers); | ||
for (int i = 0; i < numberOfAdditionalBrokers; i++) { | ||
PulsarService pulsarService = createAdditionalBroker(i); | ||
additionalBrokers.add(i, pulsarService); | ||
PulsarAdminBuilder pulsarAdminBuilder = | ||
PulsarAdmin.builder().serviceHttpUrl(pulsarService.getWebServiceAddress() != null | ||
? pulsarService.getWebServiceAddress() | ||
: pulsarService.getWebServiceAddressTls()); | ||
customizeNewPulsarAdminBuilder(pulsarAdminBuilder); | ||
additionalBrokerAdmins.add(i, pulsarAdminBuilder.build()); | ||
additionalBrokerClients.add(i, newPulsarClient(pulsarService.getBrokerServiceUrl(), 0)); | ||
} | ||
} | ||
|
||
protected ServiceConfiguration createConfForAdditionalBroker(int additionalBrokerIndex) { | ||
return getDefaultConf(); | ||
} | ||
|
||
protected PulsarService createAdditionalBroker(int additionalBrokerIndex) throws Exception { | ||
return startBroker(createConfForAdditionalBroker(additionalBrokerIndex)); | ||
} | ||
|
||
@Override | ||
protected ZKMetadataStore createLocalMetadataStore() { | ||
// use MockZooKeeperSession to provide a unique session id for each instance | ||
return new ZKMetadataStore(MockZooKeeperSession.newInstance(mockZooKeeper)); | ||
} | ||
|
||
@Override | ||
protected ZKMetadataStore createConfigurationMetadataStore() { | ||
// use MockZooKeeperSession to provide a unique session id for each instance | ||
return new ZKMetadataStore(MockZooKeeperSession.newInstance(mockZooKeeperGlobal)); | ||
} | ||
|
||
@AfterClass(alwaysRun = true) | ||
@Override | ||
public final void cleanup() throws Exception { | ||
additionalBrokersCleanup(); | ||
super.internalCleanup(); | ||
} | ||
|
||
protected void additionalBrokersCleanup() { | ||
if (additionalBrokerAdmins != null) { | ||
for (PulsarAdmin additionalBrokerAdmin : additionalBrokerAdmins) { | ||
additionalBrokerAdmin.close(); | ||
} | ||
additionalBrokerAdmins = null; | ||
} | ||
if (additionalBrokerClients != null) { | ||
for (PulsarClient additionalBrokerClient : additionalBrokerClients) { | ||
try { | ||
additionalBrokerClient.shutdown(); | ||
} catch (PulsarClientException e) { | ||
// ignore | ||
} | ||
} | ||
additionalBrokerClients = null; | ||
} | ||
if (additionalBrokers != null) { | ||
for (PulsarService pulsarService : additionalBrokers) { | ||
try { | ||
pulsarService.getConfiguration().setBrokerShutdownTimeoutMs(0L); | ||
pulsarService.close(); | ||
} catch (PulsarServerException e) { | ||
// ignore | ||
} | ||
} | ||
additionalBrokers = null; | ||
} | ||
} | ||
|
||
public final List<PulsarService> getAllBrokers() { | ||
List<PulsarService> brokers = new ArrayList<>(numberOfAdditionalBrokers() + 1); | ||
brokers.add(getPulsar()); | ||
brokers.addAll(additionalBrokers); | ||
return Collections.unmodifiableList(brokers); | ||
} | ||
|
||
public final List<PulsarAdmin> getAllAdmins() { | ||
List<PulsarAdmin> admins = new ArrayList<>(numberOfAdditionalBrokers() + 1); | ||
admins.add(admin); | ||
admins.addAll(additionalBrokerAdmins); | ||
return Collections.unmodifiableList(admins); | ||
} | ||
|
||
public final List<PulsarClient> getAllClients() { | ||
List<PulsarClient> clients = new ArrayList<>(numberOfAdditionalBrokers() + 1); | ||
clients.add(pulsarClient); | ||
clients.addAll(additionalBrokerClients); | ||
return Collections.unmodifiableList(clients); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...ker/src/test/java/org/apache/pulsar/broker/loadbalance/MultiBrokerLeaderElectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* 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.loadbalance; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertTrue; | ||
import java.util.Optional; | ||
import java.util.concurrent.TimeUnit; | ||
import org.apache.pulsar.broker.MultiBrokerBaseTest; | ||
import org.apache.pulsar.broker.PulsarService; | ||
import org.awaitility.Awaitility; | ||
import org.testng.annotations.Test; | ||
|
||
@Test(groups = "broker") | ||
public class MultiBrokerLeaderElectionTest extends MultiBrokerBaseTest { | ||
|
||
@Test | ||
public void shouldElectOneLeader() { | ||
int leaders = 0; | ||
for (PulsarService broker : getAllBrokers()) { | ||
if (broker.getLeaderElectionService().isLeader()) { | ||
leaders++; | ||
} | ||
} | ||
assertEquals(leaders, 1); | ||
} | ||
|
||
@Test | ||
public void shouldAllBrokersKnowTheLeader() { | ||
Awaitility.await().untilAsserted(() -> { | ||
for (PulsarService broker : getAllBrokers()) { | ||
Optional<LeaderBroker> currentLeader = broker.getLeaderElectionService().getCurrentLeader(); | ||
assertTrue(currentLeader.isPresent(), "Leader wasn't known on broker " + broker.getBrokerServiceUrl()); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void shouldAllBrokersBeAbleToGetTheLeader() { | ||
Awaitility.await().untilAsserted(() -> { | ||
LeaderBroker leader = null; | ||
for (PulsarService broker : getAllBrokers()) { | ||
Optional<LeaderBroker> currentLeader = | ||
broker.getLeaderElectionService().readCurrentLeader().get(1, TimeUnit.SECONDS); | ||
assertTrue(currentLeader.isPresent(), "Leader wasn't known on broker " + broker.getBrokerServiceUrl()); | ||
if (leader != null) { | ||
assertEquals(currentLeader.get(), leader, | ||
"Different leader on broker " + broker.getBrokerServiceUrl()); | ||
} else { | ||
leader = currentLeader.get(); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
this is actually not a "Test", we should not name it "**Test"
btw this is the current code style in Pulsar, so no big deal, just sharing this thought