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