Search in sources :

Example 6 with ServerSideConnectionPeer

use of herddb.server.ServerSideConnectionPeer in project herddb by diennea.

the class RetryOnLeaderChangedTest method testSwitchLeaderAndAuthTimeout.

@Test
public void testSwitchLeaderAndAuthTimeout() throws Exception {
    TestStatsProvider statsProvider = new TestStatsProvider();
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
    serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
    final AtomicBoolean suspendProcessing = new AtomicBoolean(false);
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        try (Server server_2 = new Server(serverconfig_2) {

            @Override
            protected ServerSideConnectionPeer buildPeer(Channel channel) {
                return new ServerSideConnectionPeer(channel, this) {

                    @Override
                    public void requestReceived(Pdu message, Channel channel) {
                        if (suspendProcessing.get()) {
                            LOG.log(Level.INFO, "dropping message type " + message.type + " id " + message.messageId);
                            message.close();
                            return;
                        }
                        super.requestReceived(message, channel);
                    }
                };
            }
        }) {
            server_2.start();
            TestUtils.execute(server_1.getManager(), "CREATE TABLESPACE 'ttt','leader:" + server_2.getNodeId() + "','expectedreplicacount:2'", Collections.emptyList());
            // wait for server_2 to wake up
            for (int i = 0; i < 40; i++) {
                TableSpaceManager tableSpaceManager2 = server_2.getManager().getTableSpaceManager("ttt");
                if (tableSpaceManager2 != null && tableSpaceManager2.isLeader()) {
                    break;
                }
                Thread.sleep(500);
            }
            assertTrue(server_2.getManager().getTableSpaceManager("ttt") != null && server_2.getManager().getTableSpaceManager("ttt").isLeader());
            // wait for server_1 to announce as follower
            waitClusterStatus(server_1.getManager(), server_1.getNodeId(), "follower");
            ClientConfiguration clientConfiguration = new ClientConfiguration();
            clientConfiguration.set(ClientConfiguration.PROPERTY_MODE, ClientConfiguration.PROPERTY_MODE_CLUSTER);
            clientConfiguration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
            clientConfiguration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
            clientConfiguration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
            clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 2);
            clientConfiguration.set(ClientConfiguration.PROPERTY_TIMEOUT, 2000);
            StatsLogger logger = statsProvider.getStatsLogger("ds");
            try (HDBClient client1 = new HDBClient(clientConfiguration, logger) {

                @Override
                public HDBConnection openConnection() {
                    HDBConnection con = new VisibleRouteHDBConnection(this);
                    registerConnection(con);
                    return con;
                }
            }) {
                try (VisibleRouteHDBConnection connection = (VisibleRouteHDBConnection) client1.openConnection()) {
                    // create table and insert data
                    connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE ttt.t1(k1 int primary key, n1 int)", TransactionContext.NOTRANSACTION_ID, false, false, Collections.emptyList());
                    connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO ttt.t1(k1,n1) values(1,1)", TransactionContext.NOTRANSACTION_ID, false, false, Collections.emptyList());
                    assertEquals("server2", connection.getRouteToTableSpace("ttt").getNodeId());
                    // change leader
                    switchLeader(server_1.getNodeId(), server_2.getNodeId(), server_1.getManager());
                    try (VisibleRouteHDBConnection connection2 = (VisibleRouteHDBConnection) client1.openConnection()) {
                        // connection routing still point to old leader (now follower)
                        assertEquals("server2", connection2.getRouteToTableSpace("ttt").getNodeId());
                        // suspend server_2 authentication
                        suspendProcessing.set(true);
                        // attempt an insert with old routing. Suspended autentication generates a timeout
                        // and routing will be reevaluated
                        assertEquals(1, connection2.executeUpdate(TableSpace.DEFAULT, "INSERT INTO ttt.t1(k1,n1) values(2,2)", TransactionContext.NOTRANSACTION_ID, false, false, Collections.emptyList()).updateCount);
                        // right routing to current master
                        assertEquals("server1", connection2.getRouteToTableSpace("ttt").getNodeId());
                        suspendProcessing.set(false);
                    }
                }
            }
        }
    }
}
Also used : TestStatsProvider(org.apache.bookkeeper.test.TestStatsProvider) Pdu(herddb.proto.Pdu) ServerSideConnectionPeer(herddb.server.ServerSideConnectionPeer) StatsLogger(org.apache.bookkeeper.stats.StatsLogger) Server(herddb.server.Server) ServerConfiguration(herddb.server.ServerConfiguration) Channel(herddb.network.Channel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) TableSpaceManager(herddb.core.TableSpaceManager) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 7 with ServerSideConnectionPeer

use of herddb.server.ServerSideConnectionPeer in project herddb by diennea.

the class SimpleClientServerTest method testTimeoutDuringAuth.

@Test
public void testTimeoutDuringAuth() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    ServerConfiguration config = newServerConfigurationWithAutoPort(baseDir);
    final AtomicBoolean suspendProcessing = new AtomicBoolean(false);
    try (Server server = new Server(config) {

        @Override
        protected ServerSideConnectionPeer buildPeer(Channel channel) {
            return new ServerSideConnectionPeer(channel, this) {

                @Override
                public void requestReceived(Pdu message, Channel channel) {
                    if (suspendProcessing.get()) {
                        LOG.log(Level.INFO, "dropping message type " + message.type + " id " + message.messageId);
                        message.close();
                        return;
                    }
                    super.requestReceived(message, channel);
                }
            };
        }
    }) {
        server.start();
        server.waitForStandaloneBoot();
        ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
        clientConfiguration.set(ClientConfiguration.PROPERTY_TIMEOUT, 2000);
        clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 1);
        try (HDBClient client = new HDBClient(clientConfiguration);
            HDBConnection connection = client.openConnection()) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
            long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id int primary key, s1 string)", 0, false, true, Collections.emptyList()).updateCount;
            Assert.assertEquals(1, resultCreateTable);
            suspendProcessing.set(true);
            try (HDBConnection connection2 = client.openConnection()) {
                // auth will timeout
                try {
                    connection2.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(1, "test1"));
                    fail("insertion should fail");
                } catch (Exception e) {
                    TestUtils.assertExceptionPresentInChain(e, HDBOperationTimeoutException.class);
                }
                suspendProcessing.set(false);
                // new connection
                assertEquals(1, connection2.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(1, "test1")).updateCount);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Pdu(herddb.proto.Pdu) ServerSideConnectionPeer(herddb.server.ServerSideConnectionPeer) Server(herddb.server.Server) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) ServerConfiguration(herddb.server.ServerConfiguration) Channel(herddb.network.Channel) NettyChannel(herddb.network.netty.NettyChannel) SocketChannel(io.netty.channel.socket.SocketChannel) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) IOException(java.io.IOException) MissingJDBCParameterException(herddb.model.MissingJDBCParameterException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Test(org.junit.Test)

Example 8 with ServerSideConnectionPeer

use of herddb.server.ServerSideConnectionPeer in project herddb by diennea.

the class NonMarshallingClientSideConnectionPeer method executeUpdate.

@Override
public DMLResult executeUpdate(String tableSpace, String query, long tx, boolean returnValues, boolean usePreparedStatement, List<Object> params) throws HDBException, ClientSideMetadataProviderException {
    LocalVMChannel channel = (LocalVMChannel) realConnection.ensureOpen();
    ServerSideConnectionPeer serverSidePeer = (ServerSideConnectionPeer) channel.getServerSideChannel().getMessagesReceiver();
    return serverSidePeer.executeUpdate(tableSpace, query, tx, returnValues, params);
}
Also used : ServerSideConnectionPeer(herddb.server.ServerSideConnectionPeer) LocalVMChannel(herddb.network.netty.LocalVMChannel)

Example 9 with ServerSideConnectionPeer

use of herddb.server.ServerSideConnectionPeer in project herddb by diennea.

the class NonMarshallingClientSideConnectionPeer method rollbackTransaction.

@Override
public void rollbackTransaction(String tableSpace, long tx) throws HDBException, ClientSideMetadataProviderException {
    LocalVMChannel channel = (LocalVMChannel) realConnection.ensureOpen();
    ServerSideConnectionPeer serverSidePeer = (ServerSideConnectionPeer) channel.getServerSideChannel().getMessagesReceiver();
    serverSidePeer.rollbackTransaction(tableSpace, tx);
}
Also used : ServerSideConnectionPeer(herddb.server.ServerSideConnectionPeer) LocalVMChannel(herddb.network.netty.LocalVMChannel)

Aggregations

ServerSideConnectionPeer (herddb.server.ServerSideConnectionPeer)9 LocalVMChannel (herddb.network.netty.LocalVMChannel)7 Channel (herddb.network.Channel)2 Pdu (herddb.proto.Pdu)2 Server (herddb.server.Server)2 ServerConfiguration (herddb.server.ServerConfiguration)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Test (org.junit.Test)2 ClientConfiguration (herddb.client.ClientConfiguration)1 HDBClient (herddb.client.HDBClient)1 HDBConnection (herddb.client.HDBConnection)1 HDBOperationTimeoutException (herddb.client.impl.HDBOperationTimeoutException)1 TableSpaceManager (herddb.core.TableSpaceManager)1 MissingJDBCParameterException (herddb.model.MissingJDBCParameterException)1 NettyChannel (herddb.network.netty.NettyChannel)1 StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 StatsLogger (org.apache.bookkeeper.stats.StatsLogger)1