Search in sources :

Example 46 with GetResult

use of herddb.model.GetResult in project herddb by diennea.

the class EmbeddedBookieTest method testBootWithBookie.

private void testBootWithBookie(boolean useBookiedId, boolean automaticNodeId) throws Exception {
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder("server1").toPath());
    if (!automaticNodeId) {
        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());
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_START, true);
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_BOOKIE_PORT, 0);
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_BOOKIE_BOOKIEID_ENABLED, useBookiedId);
    serverconfig_1.set("bookie.allowLoopback", true);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder("server2").toPath().toAbsolutePath());
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        if (automaticNodeId) {
            assertNotEquals("server1", server_1.getNodeId());
        } else {
            assertEquals("server1", server_1.getNodeId());
        }
        server_1.waitForStandaloneBoot();
        if (useBookiedId) {
            assertEquals(server_1.getEmbeddedBookie().getBookieId(), server_1.getNodeId());
        }
        Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).primaryKey("c").build();
        server_1.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList(server_1.getNodeId(), server_2.getNodeId())), server_1.getNodeId(), 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
            server_2.getManager().setErrorIfNotLeader(false);
            // wait for data to arrive on server_2
            for (int i = 0; i < 100; i++) {
                GetResult found = server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(1), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
                if (found.found()) {
                    break;
                }
                Thread.sleep(100);
            }
            assertTrue(server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(1), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).found());
            server_2.getManager().setErrorIfNotLeader(true);
            ClientConfiguration client_configuration = new ClientConfiguration(folder.newFolder().toPath());
            client_configuration.set(ClientConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
            client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
            client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
            client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
            try (HDBClient client = new HDBClient(client_configuration);
                HDBConnection connection = client.openConnection()) {
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1 WHERE c=1", true, Collections.emptyList(), 0, 0, 10, true)) {
                    assertEquals(1, scan.consume().size());
                }
                // switch leader to server2
                server_2.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList(server_1.getNodeId(), server_2.getNodeId())), server_2.getNodeId(), 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
                // wait that server_1 leaves leadership
                for (int i = 0; i < 100; i++) {
                    TableSpaceManager tManager = server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT);
                    if (tManager != null && !tManager.isLeader()) {
                        break;
                    }
                    Thread.sleep(100);
                }
                TableSpaceManager tManager = server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT);
                assertTrue(tManager != null && !tManager.isLeader());
                // the client MUST automatically look for the new leader
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1 WHERE c=1", true, Collections.emptyList(), 0, 0, 10, true)) {
                    assertEquals(1, scan.consume().size());
                }
            }
            // assert  that server_1 is not accepting request any more
            try (HDBClient client_to_1 = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()));
                HDBConnection connection = client_to_1.openConnection()) {
                client_to_1.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server_1) {

                    @Override
                    public void requestMetadataRefresh(Exception error) throws ClientSideMetadataProviderException {
                        assertTrue(error instanceof LeaderChangedException);
                        throw new ClientSideMetadataProviderException(error);
                    }
                });
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1 WHERE c=1", true, Collections.emptyList(), 0, 0, 10, true)) {
                    fail("server_1 MUST not accept queries");
                } catch (ClientSideMetadataProviderException ok) {
                    assertTrue(ok.getCause() instanceof LeaderChangedException);
                }
            }
            // assert that server_2 is accepting requests
            try (HDBClient client_to_2 = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()));
                HDBConnection connection = client_to_2.openConnection()) {
                client_to_2.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server_2));
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1 WHERE c=1", true, Collections.emptyList(), 0, 0, 10, true)) {
                    assertEquals(1, scan.consume().size());
                }
            }
        }
    }
}
Also used : AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) Table(herddb.model.Table) GetResult(herddb.model.GetResult) CreateTableStatement(herddb.model.commands.CreateTableStatement) LeaderChangedException(herddb.client.impl.LeaderChangedException) ScanResultSet(herddb.client.ScanResultSet) ClientSideMetadataProviderException(herddb.client.ClientSideMetadataProviderException) InsertStatement(herddb.model.commands.InsertStatement) LeaderChangedException(herddb.client.impl.LeaderChangedException) ClientSideMetadataProviderException(herddb.client.ClientSideMetadataProviderException) HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) GetStatement(herddb.model.commands.GetStatement) TableSpaceManager(herddb.core.TableSpaceManager) ClientConfiguration(herddb.client.ClientConfiguration) HashSet(java.util.HashSet)

Example 47 with GetResult

use of herddb.model.GetResult in project herddb by diennea.

the class UseVirtualTableSpaceIdWithZookKeeperTest method test_leader_online_log_available.

@Test
public void test_leader_online_log_available() throws Exception {
    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());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).primaryKey("c").build();
        server_1.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 3)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server1", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
            // wait for data to arrive on server_2
            for (int i = 0; i < 100; i++) {
                GetResult found = server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(1), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
                if (found.found()) {
                    break;
                }
                Thread.sleep(100);
            }
            assertTrue(server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(1), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).found());
            ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
            try (HDBClient client = new HDBClient(clientConfiguration);
                HDBConnection connection = client.openConnection()) {
                client.setClientSideMetadataProvider(new ZookeeperClientSideMetadataProvider(testEnv.getAddress(), testEnv.getTimeout(), testEnv.getPath()));
                try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server_1.getManager().getVirtualTableSpaceId() + ".sysnodes", true, Collections.emptyList(), 0, 0, 10, true)) {
                    List<Map<String, Object>> all = scan.consume();
                    assertEquals(2, all.size());
                }
                try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server_2.getManager().getVirtualTableSpaceId() + ".sysnodes", true, Collections.emptyList(), 0, 0, 10, true)) {
                    List<Map<String, Object>> all = scan.consume();
                    assertEquals(2, all.size());
                }
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM sysnodes", true, Collections.emptyList(), 0, 0, 10, true)) {
                    List<Map<String, Object>> all = scan.consume();
                    assertEquals(2, all.size());
                }
            }
        }
    }
}
Also used : AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) Table(herddb.model.Table) GetResult(herddb.model.GetResult) CreateTableStatement(herddb.model.commands.CreateTableStatement) ScanResultSet(herddb.client.ScanResultSet) InsertStatement(herddb.model.commands.InsertStatement) HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) GetStatement(herddb.model.commands.GetStatement) ZookeeperClientSideMetadataProvider(herddb.client.ZookeeperClientSideMetadataProvider) Map(java.util.Map) ClientConfiguration(herddb.client.ClientConfiguration) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 48 with GetResult

use of herddb.model.GetResult in project herddb by diennea.

the class ClientMultiServerTest method testLeaderChanged.

private void testLeaderChanged(boolean becomesFollower) throws Exception {
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder("server1").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());
    serverconfig_1.set(ServerConfiguration.PROPERTY_PLANNER_WAITFORTABLESPACE_TIMEOUT, 1000);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder("server2").toPath().toAbsolutePath());
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).column("d", ColumnTypes.INTEGER).primaryKey("c").build();
        server_1.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server1", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
            server_2.getManager().setErrorIfNotLeader(false);
            // wait for data to arrive on server_2
            for (int i = 0; i < 100; i++) {
                GetResult found = server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(1), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
                if (found.found()) {
                    break;
                }
                Thread.sleep(100);
            }
            assertTrue(server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(1), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).found());
            server_2.getManager().setErrorIfNotLeader(true);
            ClientConfiguration client_configuration = new ClientConfiguration(folder.newFolder().toPath());
            client_configuration.set(ClientConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
            client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
            client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
            client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
            try (HDBClient client = new HDBClient(client_configuration);
                HDBConnection connection = client.openConnection()) {
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1 WHERE c=1", true, Collections.emptyList(), 0, 0, 10, true)) {
                    assertEquals(1, scan.consume().size());
                }
                Set<String> newReplicaList;
                if (becomesFollower) {
                    newReplicaList = new HashSet<>(Arrays.asList("server1", "server2"));
                } else {
                    newReplicaList = new HashSet<>(Arrays.asList("server2"));
                }
                // switch leader to server2
                server_2.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, newReplicaList, "server2", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
                // wait that server_1 leaves leadership
                for (int i = 0; i < 100; i++) {
                    TableSpaceManager tManager = server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT);
                    if (becomesFollower) {
                        // wait for server1 to become follower
                        if (tManager != null && !tManager.isLeader() && !tManager.isFailed()) {
                            break;
                        }
                    } else {
                        // wait for server1 to become shutdown and lose the tablespace
                        if (tManager == null) {
                            break;
                        }
                    }
                    Thread.sleep(100);
                }
                if (becomesFollower) {
                    TableSpaceManager tManager = server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT);
                    assertTrue(tManager != null && !tManager.isLeader());
                } else {
                    TableSpaceManager tManager = server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT);
                    assertNull(tManager);
                }
                // the client MUST automatically look for the new leader
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1 WHERE c=1", true, Collections.emptyList(), 0, 0, 10, true)) {
                    assertEquals(1, scan.consume().size());
                }
            }
            // assert  that server_1 is not accepting request any more
            try (HDBClient client_to_1 = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()));
                HDBConnection connection = client_to_1.openConnection()) {
                client_to_1.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server_1) {

                    @Override
                    public void requestMetadataRefresh(Exception error) throws ClientSideMetadataProviderException {
                        assertTrue(error instanceof LeaderChangedException);
                        throw new ClientSideMetadataProviderException(error);
                    }
                });
                // with prepare statement
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1 WHERE c=1", true, Collections.emptyList(), 0, 0, 10, true)) {
                    fail("server_1 MUST not accept queries");
                } catch (ClientSideMetadataProviderException ok) {
                    assertTrue(ok.getCause() instanceof LeaderChangedException);
                }
                // without prepare statement
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1 WHERE c=1", false, Collections.emptyList(), 0, 0, 10, true)) {
                    fail("server_1 MUST not accept queries");
                } catch (ClientSideMetadataProviderException ok) {
                    assertTrue(ok.getCause() instanceof LeaderChangedException);
                }
                // with prepare statement
                try {
                    connection.executeUpdate(TableSpace.DEFAULT, "UPDATE t1 set d=2 WHERE c=1", 0, false, true, Collections.emptyList());
                    fail("server_1 MUST not accept queries");
                } catch (ClientSideMetadataProviderException ok) {
                    assertTrue(ok.getCause() instanceof LeaderChangedException);
                }
                // ensure that we don't use internal plans cache
                server_1.getManager().getPlanner().clearCache();
                server_2.getManager().getPlanner().clearCache();
                // without prepare statement
                try {
                    connection.executeUpdate(TableSpace.DEFAULT, "UPDATE t1 set d=2 WHERE c=1", 0, false, false, Collections.emptyList());
                    fail("server_1 MUST not accept queries");
                } catch (ClientSideMetadataProviderException ok) {
                    assertTrue(ok.getCause() instanceof LeaderChangedException);
                }
            }
            // assert that server_2 is accepting requests
            try (HDBClient client_to_2 = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()));
                HDBConnection connection = client_to_2.openConnection()) {
                client_to_2.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server_2));
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1 WHERE c=1", true, Collections.emptyList(), 0, 0, 10, true)) {
                    assertEquals(1, scan.consume().size());
                }
            }
        }
    }
}
Also used : AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) Table(herddb.model.Table) GetResult(herddb.model.GetResult) CreateTableStatement(herddb.model.commands.CreateTableStatement) LeaderChangedException(herddb.client.impl.LeaderChangedException) ScanResultSet(herddb.client.ScanResultSet) ClientSideMetadataProviderException(herddb.client.ClientSideMetadataProviderException) InsertStatement(herddb.model.commands.InsertStatement) LeaderChangedException(herddb.client.impl.LeaderChangedException) ClientSideMetadataProviderException(herddb.client.ClientSideMetadataProviderException) HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) GetStatement(herddb.model.commands.GetStatement) TableSpaceManager(herddb.core.TableSpaceManager) ClientConfiguration(herddb.client.ClientConfiguration) HashSet(java.util.HashSet)

Example 49 with GetResult

use of herddb.model.GetResult in project herddb by diennea.

the class ServerSideConnectionPeer method handleExecuteStatement.

private void handleExecuteStatement(Message message, Channel _channel) {
    Long tx = (Long) message.parameters.get("tx");
    long txId = tx != null ? tx : TransactionContext.NOTRANSACTION_ID;
    String query = (String) message.parameters.get("query");
    String tableSpace = (String) message.parameters.get("tableSpace");
    Boolean returnValues = (Boolean) message.parameters.get("returnValues");
    if (returnValues == null) {
        returnValues = Boolean.FALSE;
    }
    List<Object> parameters = (List<Object>) message.parameters.get("params");
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.log(Level.FINEST, "query " + query + " with " + parameters);
    }
    try {
        TransactionContext transactionContext = new TransactionContext(txId);
        TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, false, true, returnValues, -1);
        Statement statement = translatedQuery.plan.mainStatement;
        // LOGGER.log(Level.SEVERE, "query " + query + ", " + parameters + ", plan: " + translatedQuery.plan);
        StatementExecutionResult result = server.getManager().executePlan(translatedQuery.plan, translatedQuery.context, transactionContext);
        // LOGGER.log(Level.SEVERE, "query " + query + ", " + parameters + ", result:" + result);
        if (result instanceof DMLStatementExecutionResult) {
            DMLStatementExecutionResult dml = (DMLStatementExecutionResult) result;
            Map<String, Object> otherData = null;
            if (returnValues && dml.getKey() != null) {
                TableAwareStatement tableStatement = statement.unwrap(TableAwareStatement.class);
                Table table = server.getManager().getTableSpaceManager(statement.getTableSpace()).getTableManager(tableStatement.getTable()).getTable();
                otherData = new HashMap<>();
                Object key = RecordSerializer.deserializePrimaryKey(dml.getKey().data, table);
                otherData.put("key", key);
                if (dml.getNewvalue() != null) {
                    Map<String, Object> newvalue = RecordSerializer.toBean(new Record(dml.getKey(), dml.getNewvalue()), table);
                    otherData.put("newvalue", newvalue);
                }
            }
            _channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(dml.getUpdateCount(), otherData, dml.transactionId));
        } else if (result instanceof GetResult) {
            GetResult get = (GetResult) result;
            if (!get.found()) {
                _channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(0, null, get.transactionId));
            } else {
                Map<String, Object> record = get.getRecord().toBean(get.getTable());
                _channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(1, record, get.transactionId));
            }
        } else if (result instanceof TransactionResult) {
            TransactionResult txresult = (TransactionResult) result;
            Map<String, Object> data = new HashMap<>();
            Set<Long> transactionsForTableSpace = openTransactions.computeIfAbsent(statement.getTableSpace(), k -> new ConcurrentSkipListSet<>());
            switch(txresult.getOutcome()) {
                case BEGIN:
                    {
                        transactionsForTableSpace.add(txresult.getTransactionId());
                        break;
                    }
                case COMMIT:
                case ROLLBACK:
                    transactionsForTableSpace.remove(txresult.getTransactionId());
                    break;
            }
            data.put("tx", txresult.getTransactionId());
            _channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(1, data, txresult.transactionId));
        } else if (result instanceof DDLStatementExecutionResult) {
            DDLStatementExecutionResult ddl = (DDLStatementExecutionResult) result;
            _channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(1, null, ddl.transactionId));
        } else {
            _channel.sendReplyMessage(message, Message.ERROR(null, new Exception("unknown result type " + result.getClass() + " (" + result + ")")));
        }
    } catch (DuplicatePrimaryKeyException err) {
        LOGGER.log(Level.SEVERE, "error on query " + query + ", parameters: " + parameters + ": err", err);
        Message error = Message.ERROR(null, err);
        _channel.sendReplyMessage(message, error);
    } catch (NotLeaderException err) {
        Message error = Message.ERROR(null, err);
        error.setParameter("notLeader", "true");
        _channel.sendReplyMessage(message, error);
    } catch (StatementExecutionException err) {
        Message error = Message.ERROR(null, err);
        _channel.sendReplyMessage(message, error);
    } catch (RuntimeException err) {
        LOGGER.log(Level.SEVERE, "unexpected error on query " + query + ", parameters: " + parameters + ": err", err);
        Message error = Message.ERROR(null, err);
        _channel.sendReplyMessage(message, error);
    }
}
Also used : NotLeaderException(herddb.model.NotLeaderException) Message(herddb.network.Message) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) StatementExecutionException(herddb.model.StatementExecutionException) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) List(java.util.List) TuplesList(herddb.utils.TuplesList) ArrayList(java.util.ArrayList) Record(herddb.model.Record) TransactionResult(herddb.model.TransactionResult) TranslatedQuery(herddb.sql.TranslatedQuery) Table(herddb.model.Table) GetResult(herddb.model.GetResult) RollbackTransactionStatement(herddb.model.commands.RollbackTransactionStatement) TableAwareStatement(herddb.model.TableAwareStatement) ScanStatement(herddb.model.commands.ScanStatement) Statement(herddb.model.Statement) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) TableAwareStatement(herddb.model.TableAwareStatement) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) HerdDBInternalException(herddb.core.HerdDBInternalException) StatementExecutionException(herddb.model.StatementExecutionException) NotLeaderException(herddb.model.NotLeaderException) DataScannerException(herddb.model.DataScannerException) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 50 with GetResult

use of herddb.model.GetResult in project herddb by diennea.

the class ColumnTimestampTest method test2.

@Test
public void test2() throws Exception {
    String tableName2 = "t2";
    Table table2 = Table.builder().tablespace("tblspace1").name(tableName2).tablespace(tableSpace).column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).column("ts1", ColumnTypes.TIMESTAMP).primaryKey("id").build();
    CreateTableStatement st2 = new CreateTableStatement(table2);
    manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    java.sql.Timestamp ts1 = new java.sql.Timestamp(System.currentTimeMillis());
    {
        Map<String, Object> bean = new HashMap<>();
        bean.put("id", "key1");
        bean.put("ts1", ts1);
        Record record = RecordSerializer.toRecord(bean, table2);
        InsertStatement st = new InsertStatement(tableSpace, "t2", record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    {
        GetResult result = manager.get(new GetStatement(tableSpace, tableName2, Bytes.from_string("key1"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(result.found());
        Map<String, Object> resultbean = result.getRecord().toBean(table2);
        assertEquals(Bytes.from_string("key1"), result.getRecord().key);
        assertEquals(2, resultbean.entrySet().size());
        assertEquals(RawString.of("key1"), resultbean.get("id"));
        assertEquals(ts1, resultbean.get("ts1"));
    }
    {
        Map<String, Object> bean = new HashMap<>();
        bean.put("id", "key1");
        bean.put("ts1", null);
        Record record = RecordSerializer.toRecord(bean, table2);
        UpdateStatement st = new UpdateStatement(tableSpace, tableName2, record, null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    {
        GetResult result = manager.get(new GetStatement(tableSpace, tableName2, Bytes.from_string("key1"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(result.found());
        Map<String, Object> resultbean = result.getRecord().toBean(table2);
        assertEquals(Bytes.from_string("key1"), result.getRecord().key);
        assertEquals(1, resultbean.entrySet().size());
        assertEquals(RawString.of("key1"), resultbean.get("id"));
        assertEquals(null, resultbean.get("ts1"));
    }
}
Also used : UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) GetResult(herddb.model.GetResult) CreateTableStatement(herddb.model.commands.CreateTableStatement) RawString(herddb.utils.RawString) InsertStatement(herddb.model.commands.InsertStatement) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

GetResult (herddb.model.GetResult)96 GetStatement (herddb.model.commands.GetStatement)91 Test (org.junit.Test)85 InsertStatement (herddb.model.commands.InsertStatement)84 Table (herddb.model.Table)70 CreateTableStatement (herddb.model.commands.CreateTableStatement)68 Record (herddb.model.Record)58 Bytes (herddb.utils.Bytes)49 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)46 Path (java.nio.file.Path)43 TransactionContext (herddb.model.TransactionContext)41 TransactionResult (herddb.model.TransactionResult)30 FileCommitLogManager (herddb.file.FileCommitLogManager)29 FileDataStorageManager (herddb.file.FileDataStorageManager)29 FileMetadataStorageManager (herddb.file.FileMetadataStorageManager)29 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)29 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)28 UpdateStatement (herddb.model.commands.UpdateStatement)24 DeleteStatement (herddb.model.commands.DeleteStatement)21 ScanStatement (herddb.model.commands.ScanStatement)21