Search in sources :

Example 1 with DataSourceWithStatistics

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

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);
}
Also used : RelationalDatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection) DataSourceWithStatistics(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics) ConcurrentMutableMap(org.eclipse.collections.api.map.ConcurrentMutableMap) ConnectionKey(org.finos.legend.engine.plan.execution.stores.relational.connection.ConnectionKey) Identity(org.finos.legend.engine.shared.core.identity.Identity) Test(org.junit.Test)

Example 3 with DataSourceWithStatistics

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);
}
Also used : RelationalDatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection) DataSourceWithStatistics(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics) ConcurrentMutableMap(org.eclipse.collections.api.map.ConcurrentMutableMap) ConnectionKey(org.finos.legend.engine.plan.execution.stores.relational.connection.ConnectionKey) Identity(org.finos.legend.engine.shared.core.identity.Identity) LocalH2DataSourceSpecification(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.specifications.LocalH2DataSourceSpecification) DataSourceSpecification(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceSpecification) StaticDataSourceSpecification(org.finos.legend.engine.plan.execution.stores.relational.connection.ds.specifications.StaticDataSourceSpecification) Test(org.junit.Test)

Example 4 with DataSourceWithStatistics

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

Example 5 with DataSourceWithStatistics

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

Aggregations

DataSourceWithStatistics (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceWithStatistics)11 Identity (org.finos.legend.engine.shared.core.identity.Identity)7 Test (org.junit.Test)5 ConnectionKey (org.finos.legend.engine.plan.execution.stores.relational.connection.ConnectionKey)4 RelationalDatabaseConnection (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection)4 ConcurrentMutableMap (org.eclipse.collections.api.map.ConcurrentMutableMap)3 DataSourceSpecification (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.DataSourceSpecification)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 Properties (java.util.Properties)1 DataSource (javax.sql.DataSource)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 ConnectionStateManager (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.ConnectionStateManager)1 IdentityState (org.finos.legend.engine.plan.execution.stores.relational.connection.ds.state.IdentityState)1 Credential (org.finos.legend.engine.shared.core.identity.Credential)1