-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[fix][broker] Fix issue where leader broker information isn't available after 10 minutes #17401
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
Merged
lhotari
merged 7 commits into
apache:master
from
lhotari:lh-leaderelection-should-not-expire
Sep 8, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
74901f7
[fix][broker] Leader election cache should not invalidate entries
lhotari 8e1d0c9
Merge branch 'master' into lh-leaderelection-should-not-expire
eolivelli 02df95b
Add test case
lhotari ccd732a
Prevent stale values in leader election cache
lhotari 0954bbe
Merge branch 'master' into lh-leaderelection-should-not-expire
lhotari 88a1c85
Merge branch 'master' into lh-leaderelection-should-not-expire
eolivelli 3dee347
Merge branch 'master' into lh-leaderelection-should-not-expire
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
61 changes: 61 additions & 0 deletions
61
pulsar-broker/src/test/java/org/apache/pulsar/broker/MultiBrokerTestZKBaseTest.java
This file contains hidden or 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,61 @@ | ||
| /** | ||
| * 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 lombok.extern.slf4j.Slf4j; | ||
| import org.apache.pulsar.metadata.TestZKServer; | ||
| import org.apache.pulsar.metadata.api.MetadataStoreConfig; | ||
| import org.apache.pulsar.metadata.api.MetadataStoreException; | ||
| import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended; | ||
|
|
||
| /** | ||
| * Multiple brokers with a real test Zookeeper server (instead of the mock server) | ||
| */ | ||
| @Slf4j | ||
| public abstract class MultiBrokerTestZKBaseTest extends MultiBrokerBaseTest { | ||
| TestZKServer testZKServer; | ||
|
|
||
| @Override | ||
| protected void doInitConf() throws Exception { | ||
| super.doInitConf(); | ||
| testZKServer = new TestZKServer(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void onCleanup() { | ||
| super.onCleanup(); | ||
| if (testZKServer != null) { | ||
| try { | ||
| testZKServer.close(); | ||
| } catch (Exception e) { | ||
| log.error("Error in stopping ZK server", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected MetadataStoreExtended createLocalMetadataStore() throws MetadataStoreException { | ||
| return MetadataStoreExtended.create(testZKServer.getConnectionString(), MetadataStoreConfig.builder().build()); | ||
| } | ||
|
|
||
| @Override | ||
| protected MetadataStoreExtended createConfigurationMetadataStore() throws MetadataStoreException { | ||
| return MetadataStoreExtended.create(testZKServer.getConnectionString(), MetadataStoreConfig.builder().build()); | ||
| } | ||
| } |
114 changes: 114 additions & 0 deletions
114
...st/java/org/apache/pulsar/broker/loadbalance/MultiBrokerLeaderElectionExpirationTest.java
This file contains hidden or 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,114 @@ | ||
| /** | ||
| * 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.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertTrue; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.pulsar.broker.MultiBrokerTestZKBaseTest; | ||
| import org.apache.pulsar.broker.PulsarService; | ||
| import org.apache.pulsar.metadata.api.MetadataCacheConfig; | ||
| import org.apache.pulsar.metadata.api.MetadataStoreException; | ||
| import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended; | ||
| import org.awaitility.Awaitility; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| @Slf4j | ||
| @Test(groups = "broker") | ||
| public class MultiBrokerLeaderElectionExpirationTest extends MultiBrokerTestZKBaseTest { | ||
| private static final long EXPIRE_AFTER_WRITE_MILLIS_IN_TEST = 2000L; | ||
| private static final long REFRESH_AFTER_WRITE_MILLIS_IN_TEST = 1000L; | ||
|
|
||
| @Override | ||
| protected int numberOfAdditionalBrokers() { | ||
| return 9; | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldElectOneLeader() { | ||
| int leaders = 0; | ||
| for (PulsarService broker : getAllBrokers()) { | ||
| if (broker.getLeaderElectionService().isLeader()) { | ||
| leaders++; | ||
| } | ||
| } | ||
| assertEquals(leaders, 1); | ||
| } | ||
|
|
||
| @Override | ||
| protected MetadataStoreExtended createLocalMetadataStore() throws MetadataStoreException { | ||
| return changeDefaultMetadataCacheConfig(super.createLocalMetadataStore()); | ||
| } | ||
|
|
||
| @Override | ||
| protected MetadataStoreExtended createConfigurationMetadataStore() throws MetadataStoreException { | ||
| return changeDefaultMetadataCacheConfig(super.createConfigurationMetadataStore()); | ||
| } | ||
|
|
||
| MetadataStoreExtended changeDefaultMetadataCacheConfig(MetadataStoreExtended metadataStore) { | ||
| MetadataStoreExtended spy = spy(metadataStore); | ||
| when(spy.getDefaultMetadataCacheConfig()).thenReturn(MetadataCacheConfig | ||
| .builder() | ||
| .refreshAfterWriteMillis(REFRESH_AFTER_WRITE_MILLIS_IN_TEST) | ||
| .expireAfterWriteMillis(EXPIRE_AFTER_WRITE_MILLIS_IN_TEST) | ||
| .build()); | ||
| return spy; | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldAllBrokersBeAbleToGetTheLeaderAfterExpiration() | ||
| throws ExecutionException, InterruptedException, TimeoutException { | ||
|
|
||
| // if you want to see this test fail, modify the line in LeaderElectionImpl constructor for creating | ||
| // the metadata cache to not skip expirations: | ||
| // this.cache = store.getMetadataCache(clazz); | ||
|
|
||
| // Given that all brokers have the leader elected | ||
| Awaitility.await().untilAsserted(() -> { | ||
| for (PulsarService broker : getAllBrokers()) { | ||
| Optional<LeaderBroker> currentLeader = broker.getLeaderElectionService().getCurrentLeader(); | ||
| assertTrue(currentLeader.isPresent(), "Leader wasn't known on broker " + broker.getBrokerServiceUrl()); | ||
| } | ||
| }); | ||
|
|
||
| // Wait for metadata cache entries to expire | ||
| Thread.sleep(EXPIRE_AFTER_WRITE_MILLIS_IN_TEST); | ||
|
|
||
| // then leader should be known on all brokers and it should be the same leader | ||
| 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 hidden or 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
50 changes: 50 additions & 0 deletions
50
pulsar-metadata/src/main/java/org/apache/pulsar/metadata/api/MetadataCacheConfig.java
This file contains hidden or 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,50 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.pulsar.metadata.api; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.ToString; | ||
|
|
||
| /** | ||
| * The configuration builder for a {@link MetadataCache} config. | ||
| */ | ||
| @Builder | ||
| @Getter | ||
| @ToString | ||
| public class MetadataCacheConfig { | ||
| private static final long DEFAULT_CACHE_REFRESH_TIME_MILLIS = TimeUnit.MINUTES.toMillis(5); | ||
|
|
||
| /** | ||
| * Specifies that active entries are eligible for automatic refresh once a fixed duration has | ||
| * elapsed after the entry's creation, or the most recent replacement of its value. | ||
| * A negative or zero value disables automatic refresh. | ||
| */ | ||
| @Builder.Default | ||
| private final long refreshAfterWriteMillis = DEFAULT_CACHE_REFRESH_TIME_MILLIS; | ||
|
|
||
| /** | ||
| * Specifies that each entry should be automatically removed from the cache once a fixed duration | ||
| * has elapsed after the entry's creation, or the most recent replacement of its value. | ||
| * A negative or zero value disables automatic expiration. | ||
| */ | ||
| @Builder.Default | ||
| private final long expireAfterWriteMillis = 2 * DEFAULT_CACHE_REFRESH_TIME_MILLIS; | ||
| } |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.