Skip to content
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 @@ -19,6 +19,7 @@
package org.apache.pulsar.metadata.coordination.impl;

import com.fasterxml.jackson.databind.type.TypeFactory;
import com.google.common.annotations.VisibleForTesting;
import java.util.EnumSet;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -111,13 +112,13 @@ private synchronized CompletableFuture<LeaderElectionState> elect() {
} else {
return tryToBecomeLeader();
}
}).thenComposeAsync(leaderElectionState -> {
}).thenCompose(leaderElectionState -> {
Comment thread
mattisonchao marked this conversation as resolved.
// make sure that the cache contains the current leader
// so that getLeaderValueIfPresent works on all brokers
cache.refresh(path);

@mattisonchao mattisonchao Mar 8, 2023

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the logic. This method looks changed and no blocking call anymore. /cc @lhotari

return cache.get(path)
.thenApply(__ -> leaderElectionState);
}, executor);

@mattisonchao mattisonchao Mar 8, 2023

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use this single thread executor here because session notification method ( handleSessionNotification )also uses this executor to do blocking get. It will cause a deadlock.

});
}

private synchronized CompletableFuture<LeaderElectionState> handleExistingLeaderValue(GetResult res) {
Expand Down Expand Up @@ -336,4 +337,9 @@ private void handlePathNotification(Notification notification) {
}
}
}

@VisibleForTesting
protected ScheduledExecutorService getSchedulerExecutor() {
return executor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.coordination.impl;

import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import lombok.Cleanup;
import org.apache.pulsar.metadata.BaseMetadataStoreTest;
import org.apache.pulsar.metadata.api.MetadataStoreConfig;
import org.apache.pulsar.metadata.api.coordination.CoordinationService;
import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended;
import org.testng.annotations.Test;

public class LeaderElectionImplTest extends BaseMetadataStoreTest {
Comment thread
mattisonchao marked this conversation as resolved.

@Test(dataProvider = "impl", timeOut = 10000)
public void validateDeadLock(String provider, Supplier<String> urlSupplier)
throws Exception {
if (provider.equals("Memory") || provider.equals("RocksDB")) {
// There are no multiple sessions for the local memory provider
return;
}

@Cleanup
MetadataStoreExtended store = MetadataStoreExtended.create(urlSupplier.get(),
MetadataStoreConfig.builder().build());

String path = newKey();

@Cleanup
CoordinationService cs = new CoordinationServiceImpl(store);

@Cleanup
LeaderElectionImpl<String> le = (LeaderElectionImpl<String>) cs.getLeaderElection(String.class,
path, __ -> {
});
final CompletableFuture<Void> blockFuture = new CompletableFuture<>();
// simulate handleSessionNotification method logic
le.getSchedulerExecutor().execute(() -> {
try {
le.elect("test-2").join();
blockFuture.complete(null);
} catch (Throwable ex) {
blockFuture.completeExceptionally(ex);
}
});
blockFuture.join();
}
}