-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[improve][broker] If there is a deadlock in the service, the probe should return a failure because the service may be unavailable #23634
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
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
c18aa62
[fix][broker]If there is a deadlock in the service, the probe should …
yyj8 90ff720
[fix][broker]If there is a deadlock in the service, the probe should …
yyj8 2b18156
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 70325c0
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 c230aa5
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 2c8819b
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 f17da50
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 dc53040
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 532e69f
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 e1a1dd5
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 69aba3e
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 5970dcc
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 830c01a
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 52ab050
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 5b0c2ec
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 b1eaedd
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 1a2f6aa
Fix checkstyle
lhotari 03e6e02
[improvement][broker] If there is a deadlock in the service, the prob…
yyj8 2e2ce0b
Merge branch 'master' into check-status-add-deadlock-check
lhotari afa6980
Merge branch 'master' into check-status-add-deadlock-check
yyj8 b8da8ad
[improve][broker] If there is a deadlock in the service, the probe sh…
yyj8 4edff8e
Merge branch 'master' into check-status-add-deadlock-check
lhotari 65261a0
Improve log message
lhotari 71bb442
Fix test
lhotari 81b97cc
Ensure that test isn't flaky
lhotari 49af659
Fix warning message
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
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
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
161 changes: 161 additions & 0 deletions
161
pulsar-broker-common/src/test/java/org/apache/pulsar/common/configuration/VipStatusTest.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,161 @@ | ||
| /* | ||
| * 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.common.configuration; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import java.io.Closeable; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Phaser; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.locks.ReentrantLock; | ||
| import java.util.function.Supplier; | ||
| import javax.servlet.ServletContext; | ||
| import javax.ws.rs.WebApplicationException; | ||
| import javax.ws.rs.core.Response; | ||
| import lombok.SneakyThrows; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.assertj.core.util.Files; | ||
| import org.mockito.Mockito; | ||
| import org.testng.annotations.AfterMethod; | ||
| import org.testng.annotations.BeforeMethod; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| @Slf4j | ||
| public class VipStatusTest { | ||
|
|
||
| public static final String ATTRIBUTE_STATUS_FILE_PATH = "statusFilePath"; | ||
| public static final String ATTRIBUTE_IS_READY_PROBE = "isReadyProbe"; | ||
| private static final long LOG_THREADDUMP_INTERVAL_WHEN_DEADLOCK_DETECTED = 10000L; | ||
| // Rate limit status checks to every 500ms to prevent DoS | ||
| private static final long CHECK_STATUS_INTERVAL = 500L; | ||
|
|
||
| private ServletContext mockServletContext; | ||
| private VipStatus vipStatus; | ||
| private File file; | ||
|
|
||
| @BeforeMethod | ||
| public void setup() throws IOException { | ||
| file = Files.newTemporaryFile(); | ||
| Supplier<Boolean> isReadyProbe = () -> true; | ||
| mockServletContext = Mockito.mock(ServletContext.class); | ||
| Mockito.when(mockServletContext.getAttribute(ATTRIBUTE_STATUS_FILE_PATH)).thenReturn(file.getAbsolutePath()); | ||
| Mockito.when(mockServletContext.getAttribute(ATTRIBUTE_IS_READY_PROBE)).thenReturn(isReadyProbe); | ||
| vipStatus = new VipStatus(mockServletContext, LOG_THREADDUMP_INTERVAL_WHEN_DEADLOCK_DETECTED); | ||
| VipStatus.reset(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testVipStatusCheckStatus() { | ||
| // No deadlocks | ||
| testVipStatusCheckStatusWithoutDeadlock(); | ||
| // There is a deadlock | ||
| testVipStatusCheckStatusWithDeadlock(); | ||
| } | ||
|
|
||
| @AfterMethod(alwaysRun = true) | ||
| public void release() throws IOException { | ||
| if (file != null) { | ||
| file.delete(); | ||
| file = null; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testVipStatusCheckStatusWithoutDeadlock() { | ||
| assertEquals(vipStatus.checkStatus(), "OK"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testVipStatusCheckStatusWithDeadlock() { | ||
| MockDeadlock mockDeadlock = new MockDeadlock(); | ||
| boolean asExpected = true; | ||
| try { | ||
| mockDeadlock.startDeadlock(); | ||
| vipStatus.checkStatus(); | ||
| asExpected = false; | ||
| System.out.println("Simulated deadlock, no deadlock detected, not as expected."); | ||
| } catch (Exception wae) { | ||
| System.out.println("Simulated deadlock and detected it, as expected."); | ||
| } finally { | ||
| mockDeadlock.close(); | ||
| } | ||
|
|
||
| if (!asExpected) { | ||
| throw new WebApplicationException(Response.Status.SERVICE_UNAVAILABLE); | ||
| } | ||
| } | ||
|
|
||
| static class MockDeadlock implements Closeable { | ||
| private ExecutorService executorService = Executors.newCachedThreadPool(); | ||
| private ReentrantLock lockA = new ReentrantLock(); | ||
| private ReentrantLock lockB = new ReentrantLock(); | ||
| private Phaser phaser = new Phaser(2); | ||
|
|
||
| @SneakyThrows | ||
| public void startDeadlock() { | ||
| executorService.execute(new TaskOne()); | ||
| executorService.execute(new TaskTwo()); | ||
| Thread.sleep(CHECK_STATUS_INTERVAL); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| executorService.shutdownNow(); | ||
| } | ||
|
|
||
| private class TaskOne implements Runnable { | ||
| @Override | ||
| public void run() { | ||
| try { | ||
| lockA.lock(); | ||
| System.out.println("ThreadOne acquired lockA"); | ||
| phaser.arriveAndAwaitAdvance(); | ||
| while (!lockB.tryLock(1, TimeUnit.SECONDS)) { | ||
| System.out.println("ThreadOne acquired lockB"); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| //e.printStackTrace(); | ||
| } finally { | ||
| lockA.unlock(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private class TaskTwo implements Runnable { | ||
| @Override | ||
| public void run() { | ||
| try { | ||
| lockB.lock(); | ||
| System.out.println("ThreadOne acquired lockB"); | ||
| phaser.arriveAndAwaitAdvance(); | ||
| while (!lockA.tryLock(1, TimeUnit.SECONDS)) { | ||
| System.out.println("ThreadOne acquired lockA"); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| //e.printStackTrace(); | ||
| } finally { | ||
| lockB.unlock(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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.