Search in sources :

Example 1 with ConnectionStateManager

use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager in project legend-engine by finos.

the class TestLocalH2ConnectionState method stateAccumulatedForLocalH2.

@Test
public void stateAccumulatedForLocalH2() throws Exception {
    ConnectionStateManager stateManager = ConnectionStateManager.getInstance();
    assertEquals("mismatch in state count", 0, stateManager.size());
    Identity identity1 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("identity1");
    RelationalDatabaseConnection db1 = this.buildLocalH2DatasourceSpec();
    List<Connection> connections = IntStream.range(0, 10).mapToObj(i -> this.connectionManagerSelector.getDatabaseConnection(identity1, db1)).collect(Collectors.toList());
    Set<String> connectionNames = connections.stream().map(connection -> H2TestUtils.unwrapWrappedH2Connection(connection)).map(connection -> connection.getTraceObjectName()).collect(Collectors.toSet());
    assertEquals("did not create distinct connections", 10, connectionNames.size());
    assertEquals("mismatch in state count", 1, stateManager.size());
}
Also used : IntStream(java.util.stream.IntStream) Identity(org.finos.legend.engine.shared.core.identity.Identity) ConcurrentHashMap(org.eclipse.collections.impl.map.mutable.ConcurrentHashMap) DatabaseType(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.DatabaseType) TestDatabaseAuthenticationStrategy(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.authentication.TestDatabaseAuthenticationStrategy) Connection(java.sql.Connection) H2TestUtils(org.finos.legend.engine.plan.execution.stores.relational.connection.test.utils.H2TestUtils) TemporaryTestDbConfiguration(org.finos.legend.engine.plan.execution.stores.relational.config.TemporaryTestDbConfiguration) ConnectionStateManager(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager) Lists(org.eclipse.collections.api.factory.Lists) ReflectionUtils(org.finos.legend.engine.plan.execution.stores.relational.connection.test.utils.ReflectionUtils) Set(java.util.Set) Test(org.junit.Test) MutableList(org.eclipse.collections.api.list.MutableList) IdentityFactoryProvider(org.finos.legend.engine.shared.core.identity.factory.IdentityFactoryProvider) Collectors(java.util.stream.Collectors) Subject(javax.security.auth.Subject) ConnectionManagerSelector(org.finos.legend.engine.plan.execution.stores.relational.connection.manager.ConnectionManagerSelector) RelationalDatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection) List(java.util.List) LocalH2DatasourceSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.specification.LocalH2DatasourceSpecification) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) RelationalDatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection) Connection(java.sql.Connection) RelationalDatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection) Identity(org.finos.legend.engine.shared.core.identity.Identity) ConnectionStateManager(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager) Test(org.junit.Test)

Example 2 with ConnectionStateManager

use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager in project legend-engine by finos.

the class TestRelationalExecutionStatistics method canGetConnectionStatistics.

@Test
public void canGetConnectionStatistics() {
    ConnectionStateManager connectionStateManager = ConnectionStateManager.getInstance();
    SingleExecutionPlan executionPlan = buildPlan(TEST_EXECUTION_PLAN);
    Assert.assertNotNull(executionPlan);
    Assert.assertFalse(connectionStateManager.findByPoolName(getPoolName()).isPresent());
    Assert.assertNotNull(executePlan(executionPlan, Maps.mutable.empty()));
    Optional<ConnectionStateManagerPOJO.ConnectionPool> pool = connectionStateManager.findByPoolName(getPoolName());
    Assert.assertTrue(pool.isPresent());
    Assert.assertEquals(1, pool.get().dynamic.totalConnections);
    Assert.assertEquals(1, pool.get().dynamic.idleConnections);
    Assert.assertEquals(0, pool.get().dynamic.activeConnections);
    Assert.assertEquals(0, pool.get().dynamic.threadsAwaitingConnection);
    Assert.assertEquals(1, pool.get().statistics.getRequestedConnections());
    Assert.assertNotNull(executePlan(executionPlan, Maps.mutable.empty()));
    Optional<ConnectionStateManagerPOJO.ConnectionPool> poolAfter = connectionStateManager.findByPoolName(getPoolName());
    Assert.assertTrue(poolAfter.isPresent());
    Assert.assertEquals(1, poolAfter.get().dynamic.totalConnections);
    Assert.assertEquals(1, poolAfter.get().dynamic.idleConnections);
    Assert.assertEquals(0, poolAfter.get().dynamic.activeConnections);
    Assert.assertEquals(0, poolAfter.get().dynamic.threadsAwaitingConnection);
    Assert.assertEquals(2, poolAfter.get().statistics.getRequestedConnections());
}
Also used : SingleExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan) ConnectionStateManager(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager) Test(org.junit.Test)

Example 3 with ConnectionStateManager

use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager in project legend-engine by finos.

the class ConnectionPoolTestUtils method getAllConnectionPoolsForUser.

private static List<DataSourceWithStatistics> getAllConnectionPoolsForUser(String identityName, Set<ConnectionKey> connectionKeys) {
    ConnectionStateManager connectionStateManager = ConnectionStateManager.getInstance();
    Identity identity = IdentityFactoryProvider.getInstance().makeIdentityForTesting(identityName);
    Stream<String> poolNames = connectionKeys.stream().map(key -> connectionStateManager.poolNameFor(identity, key));
    List<DataSourceWithStatistics> connectionPoolsForUser = poolNames.map(poolName -> connectionStateManager.get(poolName)).filter(Objects::nonNull).collect(Collectors.toList());
    return connectionPoolsForUser;
}
Also used : DataSourceWithStatistics(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics) Identity(org.finos.legend.engine.shared.core.identity.Identity) ConnectionStateManager(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager)

Example 4 with ConnectionStateManager

use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager in project legend-engine by finos.

the class TestConnectionStateManagerRelationalExecutorFlow method canGetConnectionStatisticsWithConcurrency.

@Test
@Ignore
public void canGetConnectionStatisticsWithConcurrency() throws Exception {
    int numberOfThreads = 5000;
    AtomicInteger counter = new AtomicInteger(0);
    ThreadFactory threadFactory = (Runnable r) -> new Thread(r, "executer-" + counter.getAndIncrement());
    // start a bunch of threads where each thread acquires a LocalH2 connection
    List<Future<ExecutorState>> futures = new ArrayList<>();
    ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads, threadFactory);
    SingleExecutionPlan singleExecutionPlan = buildPlan(TEST_EXECUTION_PLAN, null);
    for (int i = 0; i < numberOfThreads; i++) {
        futures.add(executorService.submit(new TestRelationalExecutor(singleExecutionPlan)));
    }
    List<ExecutorState> workerStates = new ArrayList<>();
    for (Future<ExecutorState> future : futures) {
        workerStates.add(future.get());
    }
    executorService.shutdown();
    executorService.awaitTermination(100000, TimeUnit.MINUTES);
    ConnectionStateManager connectionStateManager = ConnectionStateManager.getInstance();
    System.out.println(new ObjectMapper().writeValueAsString(connectionStateManager));
    Assert.assertEquals(numberOfThreads, workerStates.size());
    workerStates.stream().forEach(thread -> Assert.assertTrue(thread.name, thread.ok));
    Optional<ConnectionStateManagerPOJO.ConnectionPool> pool = connectionStateManager.findByPoolName(getPoolName());
    Assert.assertTrue(getPoolName(), pool.isPresent());
    Assert.assertEquals(numberOfThreads, pool.get().statistics.getRequestedConnections());
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) SingleExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan) ConnectionStateManager(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with ConnectionStateManager

use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager in project legend-engine by finos.

the class TestConnectionStateManagerRelationalExecutorFlow method connectionPoolStateIsCorrect.

@Test
public void connectionPoolStateIsCorrect() {
    SingleExecutionPlan executionPlan = buildPlan(TEST_EXECUTION_PLAN);
    Assert.assertNotNull(executionPlan);
    executePlan(executionPlan);
    ConnectionStateManager connectionStateManager = ConnectionStateManager.getInstance();
    Assert.assertNotNull(connectionStateManager);
}
Also used : SingleExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan) ConnectionStateManager(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager) Test(org.junit.Test)

Aggregations

ConnectionStateManager (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager)5 Test (org.junit.Test)4 SingleExecutionPlan (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan)3 Identity (org.finos.legend.engine.shared.core.identity.Identity)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Set (java.util.Set)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Subject (javax.security.auth.Subject)1 Lists (org.eclipse.collections.api.factory.Lists)1 MutableList (org.eclipse.collections.api.list.MutableList)1 ConcurrentHashMap (org.eclipse.collections.impl.map.mutable.ConcurrentHashMap)1