Search in sources :

Example 16 with HikariConfig

use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.

the class RampUpDown method rampUpDownTest.

@Test
public void rampUpDownTest() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(5);
    config.setMaximumPoolSize(60);
    config.setInitializationFailTimeout(0);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "250");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        ds.setIdleTimeout(1000);
        HikariPool pool = getPool(ds);
        // wait two housekeeping periods so we don't fail if this part of test runs too quickly
        quietlySleep(500);
        Assert.assertSame("Total connections not as expected", 5, pool.getTotalConnections());
        Connection[] connections = new Connection[ds.getMaximumPoolSize()];
        for (int i = 0; i < connections.length; i++) {
            connections[i] = ds.getConnection();
        }
        assertSame("Total connections not as expected", 60, pool.getTotalConnections());
        for (Connection connection : connections) {
            connection.close();
        }
        quietlySleep(500);
        assertSame("Total connections not as expected", 5, pool.getTotalConnections());
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 17 with HikariConfig

use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.

the class SaturatedPoolTest830 method saturatedPoolTest.

@Test
public void saturatedPoolTest() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(5);
    config.setMaximumPoolSize(MAX_POOL_SIZE);
    config.setInitializationFailTimeout(Long.MAX_VALUE);
    config.setConnectionTimeout(1000);
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    StubConnection.slowCreate = true;
    StubStatement.setSimulatedQueryTime(1000);
    setSlf4jLogLevel(HikariPool.class, Level.DEBUG);
    System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "5000");
    final long start = currentTime();
    try (final HikariDataSource ds = new HikariDataSource(config)) {
        LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(50, /*core*/
        50, /*max*/
        2, /*keepalive*/
        SECONDS, queue, new ThreadPoolExecutor.CallerRunsPolicy());
        threadPool.allowCoreThreadTimeOut(true);
        AtomicInteger windowIndex = new AtomicInteger();
        boolean[] failureWindow = new boolean[100];
        Arrays.fill(failureWindow, true);
        // Initial saturation
        for (int i = 0; i < 50; i++) {
            threadPool.execute(() -> {
                try (Connection conn = ds.getConnection();
                    Statement stmt = conn.createStatement()) {
                    stmt.execute("SELECT bogus FROM imaginary");
                } catch (SQLException e) {
                    LOGGER.info(e.getMessage());
                }
            });
        }
        long sleep = 80;
        outer: while (true) {
            quietlySleep(sleep);
            if (elapsedMillis(start) > SECONDS.toMillis(12) && sleep < 100) {
                sleep = 100;
                LOGGER.warn("Switching to 100ms sleep");
            } else if (elapsedMillis(start) > SECONDS.toMillis(6) && sleep < 90) {
                sleep = 90;
                LOGGER.warn("Switching to 90ms sleep");
            }
            threadPool.execute(() -> {
                int ndx = windowIndex.incrementAndGet() % failureWindow.length;
                try (Connection conn = ds.getConnection();
                    Statement stmt = conn.createStatement()) {
                    stmt.execute("SELECT bogus FROM imaginary");
                    failureWindow[ndx] = false;
                } catch (SQLException e) {
                    LOGGER.info(e.getMessage());
                    failureWindow[ndx] = true;
                }
            });
            for (int i = 0; i < failureWindow.length; i++) {
                if (failureWindow[i]) {
                    if (elapsedMillis(start) % (SECONDS.toMillis(1) - sleep) < sleep) {
                        LOGGER.info("Active threads {}, submissions per second {}, waiting threads {}", threadPool.getActiveCount(), SECONDS.toMillis(1) / sleep, getPool(ds).getThreadsAwaitingConnection());
                    }
                    continue outer;
                }
            }
            LOGGER.info("Timeouts have subsided.");
            LOGGER.info("Active threads {}, submissions per second {}, waiting threads {}", threadPool.getActiveCount(), SECONDS.toMillis(1) / sleep, getPool(ds).getThreadsAwaitingConnection());
            break;
        }
        LOGGER.info("Waiting for completion of {} active tasks.", threadPool.getActiveCount());
        while (getPool(ds).getActiveConnections() > 0) {
            quietlySleep(50);
        }
        assertEquals("Rate not in balance at 10req/s", SECONDS.toMillis(1) / sleep, 10L);
    } finally {
        StubStatement.setSimulatedQueryTime(0);
        StubConnection.slowCreate = false;
        System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs");
        setSlf4jLogLevel(HikariPool.class, Level.INFO);
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Statement(java.sql.Statement) StubStatement(com.zaxxer.hikari.mocks.StubStatement) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test)

Example 18 with HikariConfig

use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.

the class ShutdownTest method testShutdown1.

@Test
public void testShutdown1() throws SQLException {
    Assert.assertSame("StubConnection count not as expected", 0, StubConnection.count.get());
    StubConnection.slowCreate = true;
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(10);
    config.setInitializationFailTimeout(Long.MAX_VALUE);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        HikariPool pool = getPool(ds);
        Thread[] threads = new Thread[10];
        for (int i = 0; i < 10; i++) {
            threads[i] = new Thread() {

                @Override
                public void run() {
                    try {
                        if (ds.getConnection() != null) {
                            quietlySleep(SECONDS.toMillis(1));
                        }
                    } catch (SQLException e) {
                    }
                }
            };
            threads[i].setDaemon(true);
        }
        for (int i = 0; i < 10; i++) {
            threads[i].start();
        }
        quietlySleep(1800L);
        assertTrue("Total connection count not as expected, ", pool.getTotalConnections() > 0);
        ds.close();
        assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
        assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
        assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
        assertTrue(ds.isClosed());
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 19 with HikariConfig

use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.

the class ShutdownTest method testShutdown3.

@Test
public void testShutdown3() throws SQLException {
    assertSame("StubConnection count not as expected", 0, StubConnection.count.get());
    StubConnection.slowCreate = false;
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(5);
    config.setMaximumPoolSize(5);
    config.setInitializationFailTimeout(0);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        HikariPool pool = getPool(ds);
        quietlySleep(1200L);
        assertTrue("Total connection count not as expected, ", pool.getTotalConnections() == 5);
        ds.close();
        assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
        assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
        assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 20 with HikariConfig

use of com.zaxxer.hikari.HikariConfig in project HikariCP by brettwooldridge.

the class ShutdownTest method testShutdown4.

@Test
public void testShutdown4() throws SQLException {
    StubConnection.slowCreate = true;
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(10);
    config.setMaximumPoolSize(10);
    config.setInitializationFailTimeout(Long.MAX_VALUE);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        quietlySleep(500L);
        ds.close();
        long startTime = currentTime();
        while (elapsedMillis(startTime) < SECONDS.toMillis(5) && threadCount() > 0) {
            quietlySleep(250);
        }
        assertSame("Unreleased connections after shutdown", 0, getPool(ds).getTotalConnections());
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Aggregations

HikariConfig (com.zaxxer.hikari.HikariConfig)101 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)85 HikariDataSource (com.zaxxer.hikari.HikariDataSource)75 Test (org.junit.Test)75 Connection (java.sql.Connection)48 StubConnection (com.zaxxer.hikari.mocks.StubConnection)28 SQLException (java.sql.SQLException)23 TestElf.newHikariDataSource (com.zaxxer.hikari.pool.TestElf.newHikariDataSource)17 TestElf.setConfigUnitTest (com.zaxxer.hikari.pool.TestElf.setConfigUnitTest)15 Properties (java.util.Properties)10 StubDataSource (com.zaxxer.hikari.mocks.StubDataSource)8 PreparedStatement (java.sql.PreparedStatement)6 DataSource (javax.sql.DataSource)6 PoolInitializationException (com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 SQLTransientConnectionException (java.sql.SQLTransientConnectionException)5 Statement (java.sql.Statement)5 PrintStream (java.io.PrintStream)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 ResultSet (java.sql.ResultSet)3