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());
}
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());
}
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;
}
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());
}
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);
}
Aggregations