Debian Codegraph @

src/test/java/com/zaxxer/hikari/pool/SaturatedPoolTest830.java

raw
1 /*
2 * Copyright (C) 2017 Brett Wooldridge
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.zaxxer.hikari.pool;
18
19 import static com.zaxxer.hikari.pool.TestElf.getConcurrentBag;
20 import static com.zaxxer.hikari.pool.TestElf.getPool;
21 import static com.zaxxer.hikari.pool.TestElf.newHikariConfig;
22 import static com.zaxxer.hikari.pool.TestElf.setSlf4jLogLevel;
23 import static com.zaxxer.hikari.util.ClockSource.currentTime;
24 import static com.zaxxer.hikari.util.ClockSource.elapsedMillis;
25 import static com.zaxxer.hikari.util.UtilityElf.quietlySleep;
26 import static java.lang.Math.round;
27 import static java.util.concurrent.TimeUnit.SECONDS;
28 import static org.junit.Assert.assertEquals;
29
30 import java.sql.Connection;
31 import java.sql.SQLException;
32 import java.sql.Statement;
33 import java.util.Arrays;
34 import java.util.concurrent.LinkedBlockingQueue;
35 import java.util.concurrent.ThreadPoolExecutor;
36 import java.util.concurrent.TimeUnit;
37 import java.util.concurrent.atomic.AtomicInteger;
38
39 import org.apache.logging.log4j.Level;
40 import org.junit.Test;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import com.zaxxer.hikari.HikariConfig;
45 import com.zaxxer.hikari.HikariDataSource;
46 import com.zaxxer.hikari.mocks.StubConnection;
47 import com.zaxxer.hikari.mocks.StubStatement;
48
49 /**
50 * @author Brett Wooldridge
51 */
52 public class SaturatedPoolTest830
53 {
54 private static final Logger LOGGER = LoggerFactory.getLogger(SaturatedPoolTest830.class);
55 private static final int MAX_POOL_SIZE = 10;
56
57 @Test
58 public void saturatedPoolTest() throws Exception {
59 HikariConfig config = newHikariConfig();
60 config.setMinimumIdle(5);
61 config.setMaximumPoolSize(MAX_POOL_SIZE);
62 config.setInitializationFailTimeout(Long.MAX_VALUE);
63 config.setConnectionTimeout(1000);
64 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
65
66 StubConnection.slowCreate = true;
67 StubStatement.setSimulatedQueryTime(1000);
68 setSlf4jLogLevel(HikariPool.class, Level.DEBUG);
69 System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "5000");
70
71 final long start = currentTime();
72
73 try (final HikariDataSource ds = new HikariDataSource(config)) {
74 LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
75 ThreadPoolExecutor threadPool = new ThreadPoolExecutor( 50 /*core*/, 50 /*max*/, 2 /*keepalive*/, SECONDS, queue, new ThreadPoolExecutor.CallerRunsPolicy());
76 threadPool.allowCoreThreadTimeOut(true);
77
78 AtomicInteger windowIndex = new AtomicInteger();
79 boolean[] failureWindow = new boolean[100];
80 Arrays.fill(failureWindow, true);
81
82 // Initial saturation
83 for (int i = 0; i < 50; i++) {
84 threadPool.execute(() -> {
85 try (Connection conn = ds.getConnection();
86 Statement stmt = conn.createStatement()) {
87 stmt.execute("SELECT bogus FROM imaginary");
88 }
89 catch (SQLException e) {
90 LOGGER.info(e.getMessage());
91 }
92 });
93 }
94
95 long sleep = 80;
96 outer: while (true) {
97 quietlySleep(sleep);
98
99 if (elapsedMillis(start) > SECONDS.toMillis(12) && sleep < 100) {
100 sleep = 100;
101 LOGGER.warn("Switching to 100ms sleep");
102 }
103 else if (elapsedMillis(start) > SECONDS.toMillis(6) && sleep < 90) {
104 sleep = 90;
105 LOGGER.warn("Switching to 90ms sleep");
106 }
107
108 threadPool.execute(() -> {
109 int ndx = windowIndex.incrementAndGet() % failureWindow.length;
110
111 try (Connection conn = ds.getConnection();
112 Statement stmt = conn.createStatement()) {
113 stmt.execute("SELECT bogus FROM imaginary");
114 failureWindow[ndx] = false;
115 }
116 catch (SQLException e) {
117 LOGGER.info(e.getMessage());
118 failureWindow[ndx] = true;
119 }
120 });
121
122 for (int i = 0; i < failureWindow.length; i++) {
123 if (failureWindow[i]) {
124 if (elapsedMillis(start) % (SECONDS.toMillis(1) - sleep) < sleep) {
125 LOGGER.info("Active threads {}, submissions per second {}, waiting threads {}",
126 threadPool.getActiveCount(),
127 SECONDS.toMillis(1) / sleep,
128 getPool(ds).getThreadsAwaitingConnection());
129 }
130 continue outer;
131 }
132 }
133
134 LOGGER.info("Timeouts have subsided.");
135 LOGGER.info("Active threads {}, submissions per second {}, waiting threads {}",
136 threadPool.getActiveCount(),
137 SECONDS.toMillis(1) / sleep,
138 getPool(ds).getThreadsAwaitingConnection());
139 break;
140 }
141
142 LOGGER.info("Waiting for completion of {} active tasks.", threadPool.getActiveCount());
143 while (getPool(ds).getActiveConnections() > 0) {
144 quietlySleep(50);
145 }
146
147 assertEquals("Rate not in balance at 10req/s", SECONDS.toMillis(1) / sleep, 10L);
148 }
149 finally {
150 StubStatement.setSimulatedQueryTime(0);
151 StubConnection.slowCreate = false;
152 System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs");
153 setSlf4jLogLevel(HikariPool.class, Level.INFO);
154 }
155 }
156 }