Search in sources :

Example 1 with Identity

use of org.finos.legend.engine.shared.core.identity.Identity 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 Identity

use of org.finos.legend.engine.shared.core.identity.Identity 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 Identity

use of org.finos.legend.engine.shared.core.identity.Identity 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 4 with Identity

use of org.finos.legend.engine.shared.core.identity.Identity 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)

Example 5 with Identity

use of org.finos.legend.engine.shared.core.identity.Identity in project legend-engine by finos.

the class TestConnectionStateManagerRace method testEvictionRace.

@Test
public void testEvictionRace() throws InterruptedException, SQLException {
    // cache has 2 state objects
    Identity user1 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("pool1");
    Identity user2 = IdentityFactoryProvider.getInstance().makeIdentityForTesting("pool2");
    DataSourceSpecification ds1 = buildLocalDataSourceSpecification(Arrays.asList("DROP TABLE IF EXISTS T1;"));
    String pool1 = connectionStateManager.poolNameFor(user1, ds1.getConnectionKey());
    String pool2 = connectionStateManager.poolNameFor(user2, ds1.getConnectionKey());
    Connection c = requestConnection(user1, ds1);
    c.close();
    Assert.assertEquals(1, connectionStateManager.get(pool1).getStatistics().getRequestedConnections());
    Connection c1 = requestConnection(user1, ds1);
    Connection c2 = requestConnection(user2, ds1);
    DataSourceStatistics pool1Version1 = DataSourceStatistics.clone(connectionStateManager.get(pool1).getStatistics());
    assertEquals(2, connectionStateManager.size());
    assertPoolStateExists(pool1, pool2);
    Assert.assertEquals(2, connectionStateManager.get(pool1).getStatistics().getRequestedConnections());
    Assert.assertEquals(1, connectionStateManager.get(pool2).getStatistics().getRequestedConnections());
    c1.close();
    c2.close();
    // advance clock by 4 minutes and invoke eviction (to simulate eviction task)
    clock.advance(Duration.ofMinutes(4));
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    Future<?> evictionTask = executorService.submit(() -> connectionStateManager.evictUnusedPoolsOlderThan(Duration.ofMinutes(3)));
    // the instrumented state manager is given a countdown latch to artificially introduce a delay after it has found entries to evict
    Thread.sleep(Duration.ofSeconds(2).toMillis());
    // update the state for pool1
    Connection c3 = requestConnection(user1, ds1);
    c3.close();
    Assert.assertEquals(3, connectionStateManager.get(pool1).getStatistics().getRequestedConnections());
    // now let the state manager continue with the eviction
    countDownLatch.countDown();
    Thread.sleep(Duration.ofSeconds(2).toMillis());
    assertTrue("Background eviction task has not completed", evictionTask.isDone());
    Assert.assertNotSame("mismatch in cached state", pool1Version1.getLastConnectionRequestAge(), connectionStateManager.get(pool1).getStatistics().getLastConnectionRequestAge());
    assertEquals(1, connectionStateManager.size());
    assertPoolStateExists(pool1);
}
Also used : Connection(java.sql.Connection) ExecutorService(java.util.concurrent.ExecutorService) DataSourceStatistics(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceStatistics) 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

Identity (org.finos.legend.engine.shared.core.identity.Identity)36 Test (org.junit.Test)25 RelationalDatabaseConnection (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection)20 Connection (java.sql.Connection)17 DataSourceSpecification (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceSpecification)11 ConnectionKey (org.finos.legend.engine.plan.execution.stores.relational.connection.ConnectionKey)8 DataSourceWithStatistics (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics)7 IdentityState (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.IdentityState)4 MutableList (org.eclipse.collections.api.list.MutableList)3 ConcurrentMutableMap (org.eclipse.collections.api.map.ConcurrentMutableMap)3 CredentialSupplier (org.finos.legend.engine.authentication.credential.CredentialSupplier)3 LocalH2DataSourceSpecification (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.specifications.LocalH2DataSourceSpecification)3 ConnectionStateManager (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager)3 IdentityFactoryProvider (org.finos.legend.engine.shared.core.identity.factory.IdentityFactoryProvider)3 Collections (java.util.Collections)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Subject (javax.security.auth.Subject)2 Lists (org.eclipse.collections.api.factory.Lists)2 JdbcConnection (org.h2.jdbc.JdbcConnection)2