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