use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics in project legend-engine by finos.
the class TestConnectionStateManager method testPoolIdentityIsValid.
@Test
public void testPoolIdentityIsValid() {
Credential mockCredential = mock(Credential.class);
Identity identityOne = new Identity("mock", mockCredential);
when(mockCredential.isValid()).thenReturn(true);
DataSourceSpecification ds1 = buildLocalDataSourceSpecification(Collections.emptyList());
String poolName = connectionStateManager.poolNameFor(identityOne, ds1.getConnectionKey());
Assert.assertNotNull(poolName);
requestConnection(identityOne, ds1);
Assert.assertEquals(1, connectionStateManager.size());
DataSourceWithStatistics dataSourceWithStatistics = connectionStateManager.get(poolName);
requestConnection(identityOne, ds1);
Assert.assertEquals(1, connectionStateManager.size());
DataSourceWithStatistics dataSourceWithStatistics1 = connectionStateManager.get(poolName);
Assert.assertEquals(dataSourceWithStatistics.getStatistics().getFirstConnectionRequest(), dataSourceWithStatistics1.getStatistics().getFirstConnectionRequest());
// mock expiring of credentials
when(mockCredential.isValid()).thenReturn(false);
requestConnection(identityOne, ds1);
Assert.assertEquals(1, connectionStateManager.size());
DataSourceWithStatistics dataSourceWithStatisticsAfter = connectionStateManager.get(poolName);
Assert.assertNotEquals(dataSourceWithStatistics.getStatistics().getFirstConnectionRequest(), dataSourceWithStatisticsAfter.getStatistics().getFirstConnectionRequest());
}
use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics in project legend-engine by finos.
the class TestDatasourceCreation method userAcquiresConcurrentConnectionsToDifferentDbs.
@Test
public void userAcquiresConcurrentConnectionsToDifferentDbs() throws Exception {
ConcurrentMutableMap datasourceSpecifications = null;
Identity identity = IdentityFactoryProvider.getInstance().makeIdentityForTesting("testuser1");
// User gets connection to db1
RelationalDatabaseConnection database1 = buildStaticDatabaseSpec("127.0.0.1", server.getPort(), "db2");
ConnectionKey connectionKey = this.connectionManagerSelector.generateKeyFromDatabaseConnection(database1);
String pool1 = this.connectionStateManager.poolNameFor(identity, connectionKey);
this.connectionManagerSelector.getDatabaseConnection(identity, database1);
// We have a single data source
datasourceSpecifications = ConnectionPoolTestUtils.getDataSourceSpecifications();
assertEquals(1, datasourceSpecifications.size());
// We have a single data source for user1
DataSourceWithStatistics datasource1 = this.getDatasourceByPool(pool1);
// User gets another connection to db2
RelationalDatabaseConnection database2 = buildStaticDatabaseSpec("127.0.0.1", server.getPort(), "db3");
ConnectionKey key2 = this.connectionManagerSelector.generateKeyFromDatabaseConnection(database2);
String pool2 = this.connectionStateManager.poolNameFor(identity, key2);
this.connectionManagerSelector.getDatabaseConnection(identity, database2);
// We now have 2 data sources one per database
datasourceSpecifications = ConnectionPoolTestUtils.getDataSourceSpecifications();
assertEquals(2, datasourceSpecifications.size());
// We have a single data source for user2
DataSourceWithStatistics datasource2 = this.getDatasourceByPool(pool2);
assertNotSame("found same datasource when distinct datasources was expected", datasource1, datasource2);
}
use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics in project legend-engine by finos.
the class TestDatasourceCreation method multipleUsersAcquireConnectionsToDifferentDatabases.
@Test
public void multipleUsersAcquireConnectionsToDifferentDatabases() throws Exception {
ConcurrentMutableMap datasourceSpecifications = null;
Identity identity1 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("testuser1");
// User gets connection to db1
RelationalDatabaseConnection database1 = buildStaticDatabaseSpec("127.0.0.1", server.getPort(), "db4");
ConnectionKey connectionKey = this.connectionManagerSelector.generateKeyFromDatabaseConnection(database1);
String key1 = this.connectionStateManager.poolNameFor(identity1, connectionKey);
this.connectionManagerSelector.getDatabaseConnection(identity1, database1);
DataSourceSpecification ds = builStaticDataSourceSpecification("127.0.0.1", server.getPort(), "db4");
assertEquals(ds.getConnectionKey(), connectionKey);
// We have a single data source
datasourceSpecifications = ConnectionPoolTestUtils.getDataSourceSpecifications();
assertEquals(1, datasourceSpecifications.size());
// We have a single data source for user1
DataSourceWithStatistics datasource1 = this.getDatasourceByPool(key1);
Identity identity2 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("testuser2");
// User gets another connection to db2
RelationalDatabaseConnection database2 = buildStaticDatabaseSpec("127.0.0.1", server.getPort(), "db5");
ConnectionKey key2 = this.connectionManagerSelector.generateKeyFromDatabaseConnection(database2);
String pool2 = this.connectionStateManager.poolNameFor(identity2, key2);
this.connectionManagerSelector.getDatabaseConnection(identity2, database2);
// We now have 2 data sources one per database + user
datasourceSpecifications = ConnectionPoolTestUtils.getDataSourceSpecifications();
assertEquals(2, datasourceSpecifications.size());
// We have a single data source for user2
DataSourceWithStatistics datasource2 = this.getDatasourceByPool(pool2);
assertNotSame("found same datasource when distinct datasources was expected", datasource1, datasource2);
}
use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics in project legend-engine by finos.
the class ConnectionStateManager method closeAndRemoveConnectionPool.
public boolean closeAndRemoveConnectionPool(String poolKey) {
DataSourceWithStatistics ds = this.connectionPools.remove(poolKey);
if (ds != null) {
LOGGER.info("Closing {} has active connections ? {}", ds.getPoolName(), ds.hasActiveConnections());
ds.close();
}
return true;
}
use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics in project legend-engine by finos.
the class ConnectionStateManager method atomicallyRemovePool.
private void atomicallyRemovePool(String poolName, DataSourceStatistics expectedState) {
synchronized (poolLockManager.getLock(poolName)) {
DataSourceWithStatistics currentState = this.connectionPools.get(poolName);
if (currentState.getStatistics().equals(expectedState)) {
currentState.close();
this.connectionPools.remove(poolName);
LOGGER.info("Removed and closed pool {}", poolName);
}
}
}
Aggregations