Search in sources :

Example 1 with DataSourceSpecification

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());
}
Also used : Credential(org.finos.legend.engine.shared.core.identity.Credential) DataSourceWithStatistics(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics) Identity(org.finos.legend.engine.shared.core.identity.Identity) DataSourceSpecification(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceSpecification) Test(org.junit.Test)

Example 2 with DataSourceSpecification

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);
}
Also used : Connection(java.sql.Connection) Identity(org.finos.legend.engine.shared.core.identity.Identity) DataSourceSpecification(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceSpecification) Test(org.junit.Test)

Example 3 with DataSourceSpecification

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());
}
Also used : DataSourceSpecification(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceSpecification) Test(org.junit.Test)

Example 4 with DataSourceSpecification

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());
}
Also used : Identity(org.finos.legend.engine.shared.core.identity.Identity) DataSourceSpecification(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceSpecification) Test(org.junit.Test)

Example 5 with DataSourceSpecification

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);
}
Also used : Connection(java.sql.Connection) Identity(org.finos.legend.engine.shared.core.identity.Identity) DataSourceSpecification(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceSpecification) Test(org.junit.Test)

Aggregations

DataSourceSpecification (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceSpecification)12 Identity (org.finos.legend.engine.shared.core.identity.Identity)11 Test (org.junit.Test)10 Connection (java.sql.Connection)3 DataSourceWithStatistics (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics)2 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 ConcurrentMutableMap (org.eclipse.collections.api.map.ConcurrentMutableMap)1 ConnectionKey (org.finos.legend.engine.plan.execution.stores.relational.connection.ConnectionKey)1 DataSourceStatistics (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceStatistics)1 LocalH2DataSourceSpecification (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.specifications.LocalH2DataSourceSpecification)1 StaticDataSourceSpecification (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.specifications.StaticDataSourceSpecification)1 RelationalDatabaseConnection (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection)1 Credential (org.finos.legend.engine.shared.core.identity.Credential)1