Search in sources :

Example 1 with UnreachableServerException

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();
    }
}
Also used : UnreachableServerException(herddb.client.impl.UnreachableServerException) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) Channel(herddb.network.Channel) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) LeaderChangedException(herddb.client.impl.LeaderChangedException) RetryRequestException(herddb.client.impl.RetryRequestException) TimeoutException(java.util.concurrent.TimeoutException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) UnreachableServerException(herddb.client.impl.UnreachableServerException) DataStorageManagerException(herddb.storage.DataStorageManagerException) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with UnreachableServerException

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();
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ReentrantLock(java.util.concurrent.locks.ReentrantLock) KeeperException(org.apache.zookeeper.KeeperException) Watcher(org.apache.zookeeper.Watcher) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) HashMap(java.util.HashMap) Stat(org.apache.zookeeper.data.Stat) Logger(java.util.logging.Logger) WatchedEvent(org.apache.zookeeper.WatchedEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) Level(java.util.logging.Level) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Map(java.util.Map) NodeMetadata(herddb.model.NodeMetadata) UnreachableServerException(herddb.client.impl.UnreachableServerException) TableSpace(herddb.model.TableSpace) ServerHostData(herddb.network.ServerHostData) UnreachableServerException(herddb.client.impl.UnreachableServerException)

Example 3 with UnreachableServerException

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();
            }
        }
    }
}
Also used : UnreachableServerException(herddb.client.impl.UnreachableServerException)

Example 4 with UnreachableServerException

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));
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) UnreachableServerException(herddb.client.impl.UnreachableServerException) ZookeeperMetadataStorageManager(herddb.cluster.ZookeeperMetadataStorageManager) IOException(java.io.IOException) ServerHostData(herddb.network.ServerHostData) IOException(java.io.IOException) UnreachableServerException(herddb.client.impl.UnreachableServerException) Test(org.junit.Test)

Aggregations

UnreachableServerException (herddb.client.impl.UnreachableServerException)4 ServerHostData (herddb.network.ServerHostData)2 IOException (java.io.IOException)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)2 HDBOperationTimeoutException (herddb.client.impl.HDBOperationTimeoutException)1 LeaderChangedException (herddb.client.impl.LeaderChangedException)1 RetryRequestException (herddb.client.impl.RetryRequestException)1 ZookeeperMetadataStorageManager (herddb.cluster.ZookeeperMetadataStorageManager)1 NodeMetadata (herddb.model.NodeMetadata)1 TableSpace (herddb.model.TableSpace)1 Channel (herddb.network.Channel)1 DataStorageManagerException (herddb.storage.DataStorageManagerException)1 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1