use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceSpecification 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.DataSourceSpecification in project legend-engine by finos.
the class TestConnectionStateManager method testDataSourceEvictionWithUnclosedConnection.
@Test
public void testDataSourceEvictionWithUnclosedConnection() throws SQLException {
Identity user1 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("user1");
Identity user2 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("user2");
Identity user3 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("user3");
DataSourceSpecification ds1 = buildLocalDataSourceSpecification(Arrays.asList("DROP TABLE IF EXISTS T1"));
DataSourceSpecification ds2 = buildLocalDataSourceSpecification(Collections.emptyList());
String pool1 = connectionStateManager.poolNameFor(user1, ds1.getConnectionKey());
String pool2 = connectionStateManager.poolNameFor(user2, ds2.getConnectionKey());
Assert.assertEquals(0, connectionStateManager.size());
assertPoolExists(false, user1.getName(), ds1.getConnectionKey());
assertPoolExists(false, user2.getName(), ds2.getConnectionKey());
ConnectionStateManager.ConnectionStateHousekeepingTask houseKeeper = new ConnectionStateManager.ConnectionStateHousekeepingTask(Duration.ofMinutes(5).getSeconds());
Connection connection1 = requestConnection(user1, ds1);
Connection connection2 = requestConnection(user2, ds2);
connection2.close();
// advance clock by 4 minutes and run housekeeper
clock.advance(Duration.ofMinutes(4));
houseKeeper.run();
Assert.assertEquals(2, connectionStateManager.size());
assertPoolExists(true, user1.getName(), ds1.getConnectionKey());
assertPoolExists(true, user2.getName(), ds2.getConnectionKey());
assertPoolExists(false, user3.getName(), ds1.getConnectionKey());
assertPoolStateExists(pool1, pool2);
Assert.assertEquals(2, connectionStateManager.size());
clock.advance(Duration.ofMinutes(5));
// new user makes a connection
requestConnection(user3, ds1);
String pool3 = connectionStateManager.poolNameFor(user3, ds1.getConnectionKey());
Assert.assertNotNull(pool3);
Assert.assertEquals(3, connectionStateManager.size());
assertPoolExists(true, user1.getName(), ds1.getConnectionKey());
assertPoolExists(true, user2.getName(), ds2.getConnectionKey());
assertPoolExists(true, user3.getName(), ds1.getConnectionKey());
// default time is 10 mins , no new connection for user2 , should be removed
clock.advance(Duration.ofMinutes(2));
houseKeeper.run();
Assert.assertEquals(2, connectionStateManager.size());
assertPoolExists(true, user1.getName(), ds1.getConnectionKey());
assertPoolExists(false, user2.getName(), ds2.getConnectionKey());
assertPoolExists(false, user3.getName(), ds1.getConnectionKey());
assertPoolStateExists(pool1);
assertPoolStateExists(pool3);
connection1.close();
houseKeeper.run();
Assert.assertEquals(1, connectionStateManager.size());
assertPoolExists(false, user1.getName(), ds1.getConnectionKey());
assertPoolExists(false, user2.getName(), ds2.getConnectionKey());
assertPoolExists(true, user3.getName(), ds1.getConnectionKey());
assertPoolStateExists(pool3);
}
use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceSpecification in project legend-engine by finos.
the class TestConnectionStateManager method testDataSourceConnectionsRequest.
@Test
public void testDataSourceConnectionsRequest() {
DataSourceSpecification ds1 = buildLocalDataSourceSpecification(Arrays.asList("DROP TABLE IF EXISTS T1"));
DataSourceSpecification ds2 = buildLocalDataSourceSpecification(Arrays.asList("DROP TABLE IF EXISTS T2"));
DataSourceSpecification ds3 = buildLocalDataSourceSpecification(Collections.emptyList());
assertPoolExists(false, "user1", ds1.getConnectionKey());
assertPoolExists(false, "user2", ds2.getConnectionKey());
assertPoolExists(false, "user3", ds3.getConnectionKey());
requestConnection("user1", ds1);
requestConnection("user2", ds2);
requestConnection("user3", ds3);
requestConnection("user1", ds1);
Assert.assertEquals(3, connectionStateManager.size());
assertPoolExists(true, "user1", ds1.getConnectionKey());
assertPoolExists(true, "user2", ds2.getConnectionKey());
assertPoolExists(true, "user3", ds3.getConnectionKey());
}
use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceSpecification in project legend-engine by finos.
the class TestConnectionStateManager method testShutdown.
@Test
public void testShutdown() throws IOException {
Identity user1 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("user1");
Identity user2 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("user2");
Identity user3 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("user3");
DataSourceSpecification ds1 = buildLocalDataSourceSpecification(Collections.emptyList());
requestConnection(user1, ds1);
requestConnection(user2, ds1);
requestConnection(user3, ds1);
connectionStateManager.close();
Assert.assertEquals(0, connectionStateManager.size());
assertPoolExists(false, user1.getName(), ds1.getConnectionKey());
assertPoolExists(false, user2.getName(), ds1.getConnectionKey());
assertPoolExists(false, user3.getName(), ds1.getConnectionKey());
}
use of org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceSpecification in project legend-engine by finos.
the class TestConnectionStateManager method testDataSourceEviction.
@Test
public void testDataSourceEviction() throws SQLException {
Identity user1 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("user1");
Identity user2 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("user2");
Identity user3 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("user3");
DataSourceSpecification ds1 = buildLocalDataSourceSpecification(Arrays.asList("DROP TABLE IF EXISTS T1"));
DataSourceSpecification ds2 = buildLocalDataSourceSpecification(Collections.emptyList());
String pool1 = connectionStateManager.poolNameFor(user1, ds1.getConnectionKey());
String pool2 = connectionStateManager.poolNameFor(user2, ds2.getConnectionKey());
Assert.assertEquals(0, connectionStateManager.size());
assertPoolExists(false, user1.getName(), ds1.getConnectionKey());
assertPoolExists(false, user2.getName(), ds2.getConnectionKey());
ConnectionStateManager.ConnectionStateHousekeepingTask houseKeeper = new ConnectionStateManager.ConnectionStateHousekeepingTask(Duration.ofMinutes(5).getSeconds());
Connection connection1 = requestConnection(user1, ds1);
Connection connection2 = requestConnection(user2, ds2);
connection1.close();
connection2.close();
// advance clock by 4 minutes and run housekeeper
clock.advance(Duration.ofMinutes(4));
houseKeeper.run();
Assert.assertEquals(2, connectionStateManager.size());
assertPoolExists(true, user1.getName(), ds1.getConnectionKey());
assertPoolExists(true, user2.getName(), ds2.getConnectionKey());
assertPoolExists(false, user3.getName(), ds1.getConnectionKey());
assertPoolStateExists(pool1, pool2);
Assert.assertEquals(2, connectionStateManager.size());
clock.advance(Duration.ofMinutes(5));
// new user makes a connection
requestConnection(user3, ds1);
String pool3 = connectionStateManager.poolNameFor(user3, ds1.getConnectionKey());
Assert.assertNotNull(pool3);
assertPoolExists(true, user1.getName(), ds1.getConnectionKey());
assertPoolExists(true, user2.getName(), ds2.getConnectionKey());
assertPoolExists(true, user3.getName(), ds1.getConnectionKey());
Assert.assertEquals(3, connectionStateManager.size());
// default time is 10 mins , no new connection for user1 and 2 , should be removed
clock.advance(Duration.ofMinutes(2));
houseKeeper.run();
assertPoolExists(false, user1.getName(), ds1.getConnectionKey());
assertPoolExists(false, user2.getName(), ds2.getConnectionKey());
assertPoolExists(true, user3.getName(), ds1.getConnectionKey());
Assert.assertEquals(1, connectionStateManager.size());
assertPoolStateExists(pool3);
}
Aggregations