use of herddb.client.impl.UnreachableServerException in project herddb by diennea.
the class RoutedClientSideConnection method ensureOpen.
@Override
public Channel ensureOpen() throws HDBException {
connectionLock.readLock().lock();
try {
Channel channel = this.channel;
if (channel != null && channel.isValid()) {
return channel;
}
connectionLock.readLock().unlock();
connectionLock.writeLock().lock();
try {
channel = this.channel;
if (this.channel != null) {
if (channel.isValid()) {
return channel;
}
// channel is not valid, force close
channel.close();
}
// clean up local cache, if the server restarted we would use old ids
preparedStatements.clear();
LOGGER.log(Level.FINE, "{0} - connect to {1}:{2} ssh:{3}", new Object[] { this, server.getHost(), server.getPort(), server.isSsl() });
channel = this.connection.getClient().createChannelTo(server, this);
try {
performAuthentication(channel, server.getHost());
this.channel = channel;
return channel;
} catch (TimeoutException err) {
LOGGER.log(Level.SEVERE, "Error", err);
if (channel != null) {
channel.close();
}
throw new HDBOperationTimeoutException(err);
} catch (Exception err) {
LOGGER.log(Level.SEVERE, "Error", err);
if (channel != null) {
channel.close();
}
throw err;
}
} finally {
connectionLock.writeLock().unlock();
connectionLock.readLock().lock();
}
} catch (java.net.ConnectException err) {
// this error will be retryed by the client
throw new UnreachableServerException("Cannot connect to " + nodeId, err, nodeId);
} catch (HDBException err) {
throw err;
} catch (Exception err) {
throw new HDBException(err);
} finally {
connectionLock.readLock().unlock();
}
}
use of herddb.client.impl.UnreachableServerException in project herddb by diennea.
the class ZookeeperClientSideMetadataProvider method requestMetadataRefresh.
@Override
public void requestMetadataRefresh(Exception error) {
if (error instanceof UnreachableServerException) {
UnreachableServerException u = (UnreachableServerException) error;
servers.remove(u.getNodeId());
List<String> tablespaces = tableSpaceLeaders.entrySet().stream().filter(entry -> entry.getValue().equalsIgnoreCase(u.getNodeId())).map(entry -> entry.getKey()).collect(Collectors.toList());
tablespaces.forEach(tableSpaceLeaders::remove);
} else {
tableSpaceLeaders.clear();
servers.clear();
}
}
use of herddb.client.impl.UnreachableServerException in project herddb by diennea.
the class HDBConnection method requestMetadataRefresh.
void requestMetadataRefresh(Exception err) throws ClientSideMetadataProviderException {
client.getClientSideMetadataProvider().requestMetadataRefresh(err);
if (err instanceof UnreachableServerException) {
UnreachableServerException u = (UnreachableServerException) err;
String nodeId = u.getNodeId();
ClientSideConnectionPeer[] all = routes.remove(nodeId);
if (all != null) {
for (ClientSideConnectionPeer con : all) {
con.close();
}
}
}
}
use of herddb.client.impl.UnreachableServerException in project herddb by diennea.
the class ZookeeperClientSideMetadataProviderTest method test.
@Test
public void test() throws Exception {
try (ZookeeperMetadataStorageManager server = new ZookeeperMetadataStorageManager(testEnv.getAddress(), testEnv.getTimeout(), testEnv.getPath());
ZookeeperClientSideMetadataProvider prov = new ZookeeperClientSideMetadataProvider(testEnv.getAddress(), testEnv.getTimeout(), testEnv.getPath())) {
server.start();
assertTrue(server.ensureDefaultTableSpace("node1", "node1", 5000, 1));
server.registerNode(NodeMetadata.builder().host("test-node").port(1234).ssl(true).nodeId("node1").build());
ServerHostData currentData = prov.getServerHostData("node1");
assertEquals("test-node", currentData.getHost());
assertEquals(1234, currentData.getPort());
assertTrue(currentData.isSsl());
assertEquals("node1", prov.getTableSpaceLeader(TableSpace.DEFAULT));
server.registerNode(NodeMetadata.builder().host("test-node-newhost").port(1234).ssl(true).nodeId("node1").build());
prov.requestMetadataRefresh(new UnreachableServerException("error", new IOException(), "node1"));
assertEquals("node1", prov.getTableSpaceLeader(TableSpace.DEFAULT));
currentData = prov.getServerHostData("node1");
assertEquals("test-node-newhost", currentData.getHost());
assertEquals(1234, currentData.getPort());
assertTrue(currentData.isSsl());
prov.requestMetadataRefresh(new Exception());
currentData = prov.getServerHostData("node1");
assertEquals("test-node-newhost", currentData.getHost());
assertEquals(1234, currentData.getPort());
assertTrue(currentData.isSsl());
assertEquals("node1", prov.getTableSpaceLeader(TableSpace.DEFAULT));
final ZooKeeper currentZooKeeper = prov.getZooKeeper();
// expire session
currentZooKeeper.getTestable().injectSessionExpiration();
// wait for a new handle to be created
TestUtils.waitForCondition(() -> {
ZooKeeper zk = prov.getZooKeeper();
return zk != currentZooKeeper;
}, TestUtils.NOOP, 100);
// clean cache
prov.requestMetadataRefresh(new UnreachableServerException("error", new IOException(), "node1"));
assertEquals("node1", prov.getTableSpaceLeader(TableSpace.DEFAULT));
prov.getServerHostData("node1");
assertEquals("test-node-newhost", currentData.getHost());
assertEquals(1234, currentData.getPort());
assertTrue(currentData.isSsl());
assertEquals("node1", prov.getTableSpaceLeader(TableSpace.DEFAULT));
}
}
Aggregations