|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.common.configuration; |
| 20 | + |
| 21 | +import static org.testng.Assert.assertEquals; |
| 22 | +import java.io.Closeable; |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.concurrent.ExecutorService; |
| 26 | +import java.util.concurrent.Executors; |
| 27 | +import java.util.concurrent.Phaser; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | +import java.util.concurrent.locks.ReentrantLock; |
| 30 | +import java.util.function.Supplier; |
| 31 | +import javax.servlet.ServletContext; |
| 32 | +import javax.ws.rs.WebApplicationException; |
| 33 | +import javax.ws.rs.core.Response; |
| 34 | +import lombok.SneakyThrows; |
| 35 | +import lombok.extern.slf4j.Slf4j; |
| 36 | +import org.assertj.core.util.Files; |
| 37 | +import org.mockito.Mockito; |
| 38 | +import org.testng.annotations.AfterMethod; |
| 39 | +import org.testng.annotations.BeforeMethod; |
| 40 | +import org.testng.annotations.Test; |
| 41 | + |
| 42 | +@Slf4j |
| 43 | +public class VipStatusTest { |
| 44 | + |
| 45 | + public static final String ATTRIBUTE_STATUS_FILE_PATH = "statusFilePath"; |
| 46 | + public static final String ATTRIBUTE_IS_READY_PROBE = "isReadyProbe"; |
| 47 | + private static final long LOG_THREADDUMP_INTERVAL_WHEN_DEADLOCK_DETECTED = 10000L; |
| 48 | + // Rate limit status checks to every 500ms to prevent DoS |
| 49 | + private static final long CHECK_STATUS_INTERVAL = 500L; |
| 50 | + |
| 51 | + private ServletContext mockServletContext; |
| 52 | + private VipStatus vipStatus; |
| 53 | + private File file; |
| 54 | + |
| 55 | + @BeforeMethod |
| 56 | + public void setup() throws IOException { |
| 57 | + file = Files.newTemporaryFile(); |
| 58 | + Supplier<Boolean> isReadyProbe = () -> true; |
| 59 | + mockServletContext = Mockito.mock(ServletContext.class); |
| 60 | + Mockito.when(mockServletContext.getAttribute(ATTRIBUTE_STATUS_FILE_PATH)).thenReturn(file.getAbsolutePath()); |
| 61 | + Mockito.when(mockServletContext.getAttribute(ATTRIBUTE_IS_READY_PROBE)).thenReturn(isReadyProbe); |
| 62 | + vipStatus = new VipStatus(mockServletContext, LOG_THREADDUMP_INTERVAL_WHEN_DEADLOCK_DETECTED); |
| 63 | + VipStatus.reset(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testVipStatusCheckStatus() { |
| 68 | + // No deadlocks |
| 69 | + testVipStatusCheckStatusWithoutDeadlock(); |
| 70 | + // There is a deadlock |
| 71 | + testVipStatusCheckStatusWithDeadlock(); |
| 72 | + } |
| 73 | + |
| 74 | + @AfterMethod(alwaysRun = true) |
| 75 | + public void release() throws IOException { |
| 76 | + if (file != null) { |
| 77 | + file.delete(); |
| 78 | + file = null; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testVipStatusCheckStatusWithoutDeadlock() { |
| 84 | + assertEquals(vipStatus.checkStatus(), "OK"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testVipStatusCheckStatusWithDeadlock() { |
| 89 | + MockDeadlock mockDeadlock = new MockDeadlock(); |
| 90 | + boolean asExpected = true; |
| 91 | + try { |
| 92 | + mockDeadlock.startDeadlock(); |
| 93 | + vipStatus.checkStatus(); |
| 94 | + asExpected = false; |
| 95 | + System.out.println("Simulated deadlock, no deadlock detected, not as expected."); |
| 96 | + } catch (Exception wae) { |
| 97 | + System.out.println("Simulated deadlock and detected it, as expected."); |
| 98 | + } finally { |
| 99 | + mockDeadlock.close(); |
| 100 | + } |
| 101 | + |
| 102 | + if (!asExpected) { |
| 103 | + throw new WebApplicationException(Response.Status.SERVICE_UNAVAILABLE); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + static class MockDeadlock implements Closeable { |
| 108 | + private ExecutorService executorService = Executors.newCachedThreadPool(); |
| 109 | + private ReentrantLock lockA = new ReentrantLock(); |
| 110 | + private ReentrantLock lockB = new ReentrantLock(); |
| 111 | + private Phaser phaser = new Phaser(2); |
| 112 | + |
| 113 | + @SneakyThrows |
| 114 | + public void startDeadlock() { |
| 115 | + executorService.execute(new TaskOne()); |
| 116 | + executorService.execute(new TaskTwo()); |
| 117 | + Thread.sleep(CHECK_STATUS_INTERVAL); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void close() { |
| 122 | + executorService.shutdownNow(); |
| 123 | + } |
| 124 | + |
| 125 | + private class TaskOne implements Runnable { |
| 126 | + @Override |
| 127 | + public void run() { |
| 128 | + try { |
| 129 | + lockA.lock(); |
| 130 | + System.out.println("ThreadOne acquired lockA"); |
| 131 | + phaser.arriveAndAwaitAdvance(); |
| 132 | + while (!lockB.tryLock(1, TimeUnit.SECONDS)) { |
| 133 | + System.out.println("ThreadOne acquired lockB"); |
| 134 | + } |
| 135 | + } catch (InterruptedException e) { |
| 136 | + //e.printStackTrace(); |
| 137 | + } finally { |
| 138 | + lockA.unlock(); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private class TaskTwo implements Runnable { |
| 144 | + @Override |
| 145 | + public void run() { |
| 146 | + try { |
| 147 | + lockB.lock(); |
| 148 | + System.out.println("ThreadOne acquired lockB"); |
| 149 | + phaser.arriveAndAwaitAdvance(); |
| 150 | + while (!lockA.tryLock(1, TimeUnit.SECONDS)) { |
| 151 | + System.out.println("ThreadOne acquired lockA"); |
| 152 | + } |
| 153 | + } catch (InterruptedException e) { |
| 154 | + //e.printStackTrace(); |
| 155 | + } finally { |
| 156 | + lockB.unlock(); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments