Search in sources :

Example 26 with HikariDataSource

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

the class PostgresTest method testCase4.

// @Test
public void testCase4() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(15);
    config.setConnectionTimeout(10000);
    config.setIdleTimeout(TimeUnit.MINUTES.toMillis(1));
    config.setMaxLifetime(TimeUnit.MINUTES.toMillis(2));
    config.setRegisterMbeans(true);
    config.setJdbcUrl("jdbc:postgresql://localhost:5432/netld");
    config.setUsername("brettw");
    try (final HikariDataSource ds = new HikariDataSource(config)) {
        countdown(20);
        List<Thread> threads = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            threads.add(new Thread() {

                public void run() {
                    UtilityElf.quietlySleep((long) (Math.random() * 2500L));
                    final long start = currentTime();
                    do {
                        try (Connection conn = ds.getConnection();
                            Statement stmt = conn.createStatement()) {
                            try (ResultSet rs = stmt.executeQuery("SELECT * FROM device WHERE device_id=0 ORDER BY device_id LIMIT 1 OFFSET 0")) {
                                rs.next();
                            }
                            //Math.max(50L, (long)(Math.random() * 250L)));
                            UtilityElf.quietlySleep(100L);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        // throw new RuntimeException(e);
                        }
                    // UtilityElf.quietlySleep(10L); //Math.max(50L, (long)(Math.random() * 250L)));
                    } while (elapsedMillis(start) < TimeUnit.MINUTES.toMillis(5));
                }
            });
        }
    //         threads.forEach(t -> t.start());
    //         threads.forEach(t -> { try { t.join(); } catch (InterruptedException e) {} });
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig)

Example 27 with HikariDataSource

use of com.zaxxer.hikari.HikariDataSource 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 28 with HikariDataSource

use of com.zaxxer.hikari.HikariDataSource 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 29 with HikariDataSource

use of com.zaxxer.hikari.HikariDataSource 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 30 with HikariDataSource

use of com.zaxxer.hikari.HikariDataSource 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)

Aggregations

HikariDataSource (com.zaxxer.hikari.HikariDataSource)114 Test (org.junit.Test)78 HikariConfig (com.zaxxer.hikari.HikariConfig)75 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)63 Connection (java.sql.Connection)63 TestElf.newHikariDataSource (com.zaxxer.hikari.pool.TestElf.newHikariDataSource)33 StubConnection (com.zaxxer.hikari.mocks.StubConnection)29 SQLException (java.sql.SQLException)27 TestElf.setConfigUnitTest (com.zaxxer.hikari.pool.TestElf.setConfigUnitTest)16 StubDataSource (com.zaxxer.hikari.mocks.StubDataSource)10 MetricRegistry (com.codahale.metrics.MetricRegistry)8 PreparedStatement (java.sql.PreparedStatement)6 Statement (java.sql.Statement)6 PoolInitializationException (com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)5 SQLTransientConnectionException (java.sql.SQLTransientConnectionException)5 Properties (java.util.Properties)5 ResultSet (java.sql.ResultSet)4 HealthCheckRegistry (com.codahale.metrics.health.HealthCheckRegistry)3 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3