Search in sources :

Example 1 with ServiceDescriptor

use of io.airlift.discovery.client.ServiceDescriptor in project airlift by airlift.

the class PostgreSqlDataSource method createConnectionInternal.

@Override
protected PooledConnection createConnectionInternal() throws SQLException {
    // attempt to get a connection from the current datasource if we have one
    SQLException lastException = null;
    if (dataSource != null) {
        try {
            return dataSource.getPooledConnection();
        } catch (SQLException e) {
            lastException = e;
        }
    }
    // drop reference to current datasource
    dataSource = null;
    // attempt to create a connection to each PostgreSQL server (except for the one that we know is bad)
    for (ServiceDescriptor serviceDescriptor : serviceSelector.selectAllServices()) {
        // skip the current server since it is having problems
        if (serviceDescriptor.getId().equals(currentServer)) {
            continue;
        }
        // skip bogus announcements
        String jdbcUrl = serviceDescriptor.getProperties().get("jdbc");
        if (jdbcUrl == null) {
            continue;
        }
        try {
            PGConnectionPoolDataSource dataSource = new PGConnectionPoolDataSource();
            dataSource.setUrl(jdbcUrl);
            dataSource.setDefaultRowFetchSize(defaultFetchSize);
            int timeout = Ints.saturatedCast(MILLISECONDS.toSeconds(getMaxConnectionWaitMillis()));
            dataSource.setConnectTimeout(timeout);
            dataSource.setLoginTimeout(timeout);
            dataSource.setSocketTimeout(timeout);
            PooledConnection connection = dataSource.getPooledConnection();
            // that worked so save the datasource and server id
            this.dataSource = dataSource;
            this.currentServer = serviceDescriptor.getId();
            return connection;
        } catch (SQLException e) {
            lastException = e;
        }
    }
    // no servers found, clear the current server id since we no longer have a server at all
    currentServer = null;
    // throw the last exception we got
    if (lastException != null) {
        throw lastException;
    }
    throw new SQLException(format("No PostgreSQL servers of type '%s' available in pool '%s'", serviceSelector.getType(), serviceSelector.getPool()));
}
Also used : PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) ServiceDescriptor(io.airlift.discovery.client.ServiceDescriptor) PGConnectionPoolDataSource(org.postgresql.ds.PGConnectionPoolDataSource)

Example 2 with ServiceDescriptor

use of io.airlift.discovery.client.ServiceDescriptor in project airlift by airlift.

the class InMemoryDiscoveryClient method getServices.

@Override
public CheckedFuture<ServiceDescriptors, DiscoveryException> getServices(String type, String pool) {
    requireNonNull(type, "type is null");
    requireNonNull(pool, "pool is null");
    ImmutableList.Builder<ServiceDescriptor> builder = ImmutableList.builder();
    for (ServiceDescriptor serviceDescriptor : this.announcements.get()) {
        if (serviceDescriptor.getType().equals(type) && serviceDescriptor.getPool().equals(pool)) {
            builder.add(serviceDescriptor);
        }
    }
    for (ServiceDescriptor serviceDescriptor : this.discovered.values()) {
        if (serviceDescriptor.getType().equals(type) && serviceDescriptor.getPool().equals(pool)) {
            builder.add(serviceDescriptor);
        }
    }
    return Futures.immediateCheckedFuture(new ServiceDescriptors(type, pool, builder.build(), maxAge, UUID.randomUUID().toString()));
}
Also used : ServiceDescriptor(io.airlift.discovery.client.ServiceDescriptor) ImmutableList(com.google.common.collect.ImmutableList) ServiceDescriptors(io.airlift.discovery.client.ServiceDescriptors)

Example 3 with ServiceDescriptor

use of io.airlift.discovery.client.ServiceDescriptor in project airlift by airlift.

the class InMemoryDiscoveryClient method announce.

@Override
public CheckedFuture<Duration, DiscoveryException> announce(Set<ServiceAnnouncement> services) {
    requireNonNull(services, "services is null");
    ImmutableSet.Builder<ServiceDescriptor> builder = ImmutableSet.builder();
    for (ServiceAnnouncement service : services) {
        builder.add(service.toServiceDescriptor(nodeInfo));
    }
    announcements.set(builder.build());
    return Futures.immediateCheckedFuture(maxAge);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) ServiceDescriptor(io.airlift.discovery.client.ServiceDescriptor) ServiceAnnouncement(io.airlift.discovery.client.ServiceAnnouncement)

Example 4 with ServiceDescriptor

use of io.airlift.discovery.client.ServiceDescriptor in project airlift by airlift.

the class MySqlDataSource method createConnectionInternal.

protected PooledConnection createConnectionInternal() throws SQLException {
    // attempt to get a connection from the current datasource if we have one
    SQLException lastException = null;
    if (dataSource != null) {
        try {
            return dataSource.getPooledConnection();
        } catch (SQLException e) {
            lastException = e;
        }
    }
    // drop reference to current datasource
    dataSource = null;
    // attempt to create a connection to each mysql server (except for the one that we know is bad)
    for (ServiceDescriptor serviceDescriptor : serviceSelector.selectAllServices()) {
        // skip the current server since it is having problems
        if (serviceDescriptor.getId().equals(currentServer)) {
            continue;
        }
        // skip bogus announcements
        String jdbcUrl = serviceDescriptor.getProperties().get("jdbc");
        if (jdbcUrl == null) {
            continue;
        }
        try {
            MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
            dataSource.setUrl(jdbcUrl);
            // these are in MS *not* seconds like everything else in JDBC
            dataSource.setConnectTimeout(getMaxConnectionWaitMillis());
            dataSource.setInitialTimeout(getMaxConnectionWaitMillis());
            dataSource.setDefaultFetchSize(defaultFetchSize);
            PooledConnection connection = dataSource.getPooledConnection();
            // that worked so save the datasource and server id
            this.dataSource = dataSource;
            this.currentServer = serviceDescriptor.getId();
            return connection;
        } catch (SQLException e) {
            lastException = e;
        }
    }
    // no servers found, clear the current server id since we no longer have a server at all
    currentServer = null;
    // throw the last exception we got
    if (lastException != null) {
        throw lastException;
    }
    throw new SQLException(String.format("No mysql servers of type '%s' available in pool '%s'", serviceSelector.getType(), serviceSelector.getPool()));
}
Also used : PooledConnection(javax.sql.PooledConnection) SQLException(java.sql.SQLException) ServiceDescriptor(io.airlift.discovery.client.ServiceDescriptor) MysqlConnectionPoolDataSource(com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource)

Aggregations

ServiceDescriptor (io.airlift.discovery.client.ServiceDescriptor)4 SQLException (java.sql.SQLException)2 PooledConnection (javax.sql.PooledConnection)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 MysqlConnectionPoolDataSource (com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource)1 ServiceAnnouncement (io.airlift.discovery.client.ServiceAnnouncement)1 ServiceDescriptors (io.airlift.discovery.client.ServiceDescriptors)1 PGConnectionPoolDataSource (org.postgresql.ds.PGConnectionPoolDataSource)1