Search in sources :

Example 51 with GetStatement

use of herddb.model.commands.GetStatement 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 52 with GetStatement

use of herddb.model.commands.GetStatement 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 53 with GetStatement

use of herddb.model.commands.GetStatement 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 54 with GetStatement

use of herddb.model.commands.GetStatement 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)

Example 55 with GetStatement

use of herddb.model.commands.GetStatement in project herddb by diennea.

the class RawSQLTest method basicTest.

@Test
public void basicTest() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
        {
            GetResult result = manager.get(new GetStatement("tblspace1", "tsql", Bytes.from_string("mykey"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(result.found());
            assertEquals(result.getRecord().key, Bytes.from_string("mykey"));
            Map<String, Object> finalRecord = result.getRecord().toBean(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable());
            assertEquals(RawString.of("mykey"), finalRecord.get("k1"));
            assertEquals(Integer.valueOf(1234), finalRecord.get("n1"));
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=? where k1 = ?", Arrays.asList(Integer.valueOf(999), "mykey")).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=? where k1 = ? and n1 = ?", Arrays.asList(Integer.valueOf(100), "mykey", Integer.valueOf(999))).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=? where k1 = ? and (n1 = ? or n1 <> ?)", Arrays.asList(Integer.valueOf(999), "mykey", Integer.valueOf(100), Integer.valueOf(1000))).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=? where k1 = ? and (n1 <> ?)", Arrays.asList(Integer.valueOf(34), "mykey", Integer.valueOf(15))).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=? where k1 = ? and not (n1 <> ?)", Arrays.asList(Integer.valueOf(999), "mykey", Integer.valueOf(34))).getUpdateCount());
        }
        {
            GetResult result = manager.get(new GetStatement("tblspace1", "tsql", Bytes.from_string("mykey"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(result.found());
            assertEquals(result.getRecord().key, Bytes.from_string("mykey"));
            Map<String, Object> finalRecord = result.getRecord().toBean(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable());
            assertEquals(RawString.of("mykey"), finalRecord.get("k1"));
            assertEquals(Integer.valueOf(999), finalRecord.get("n1"));
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.tsql where k1 = ?", Arrays.asList("mykey"), false, true, false, -1);
            GetStatement st_get = (GetStatement) translate1.plan.mainStatement;
            GetResult result = manager.get(st_get, translate1.context, TransactionContext.NO_TRANSACTION);
            assertTrue(result.found());
            assertEquals(result.getRecord().key, Bytes.from_string("mykey"));
            Map<String, Object> finalRecord = result.getRecord().toBean(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable());
            assertEquals(RawString.of("mykey"), finalRecord.get("k1"));
            assertEquals(Integer.valueOf(999), finalRecord.get("n1"));
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.tsql where k1 = ? and n1=?", Arrays.asList("mykey", 999), false, true, false, -1);
            GetStatement st_get_with_condition = (GetStatement) translate1.plan.mainStatement;
            GetResult result = manager.get(st_get_with_condition, translate1.context, TransactionContext.NO_TRANSACTION);
            assertTrue(result.found());
            assertEquals(result.getRecord().key, Bytes.from_string("mykey"));
            Map<String, Object> finalRecord = result.getRecord().toBean(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable());
            assertEquals(RawString.of("mykey"), finalRecord.get("k1"));
            assertEquals(Integer.valueOf(999), finalRecord.get("n1"));
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.tsql where k1 = ? and n1=?", Arrays.asList("mykey", 9992), false, true, false, -1);
            GetStatement st_get_with_wrong_condition = (GetStatement) translate1.plan.mainStatement;
            GetResult result = manager.get(st_get_with_wrong_condition, translate1.context, TransactionContext.NO_TRANSACTION);
            assertFalse(result.found());
        }
        {
            assertEquals(0, executeUpdate(manager, "DELETE FROM tblspace1.tsql where k1 = ? and n1 = ?", Arrays.asList("mykey", 123)).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "DELETE FROM tblspace1.tsql where k1 = ?", Arrays.asList("mykey")).getUpdateCount());
        }
        {
            GetResult result = manager.get(new GetStatement("tblspace1", "tsql", Bytes.from_string("mykey"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertFalse(result.found());
        }
        {
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
        }
        {
            GetResult result = manager.get(new GetStatement("tblspace1", "tsql", Bytes.from_string("mykey"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(result.found());
        }
        {
            assertEquals(1, executeUpdate(manager, "DELETE FROM tblspace1.tsql where k1 = ? and n1=?", Arrays.asList("mykey", 1234)).getUpdateCount());
        }
        {
            GetResult result = manager.get(new GetStatement("tblspace1", "tsql", Bytes.from_string("mykey"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertFalse(result.found());
        }
        {
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values('mykey2',1234)", Collections.emptyList()).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=2135 where k1 = 'mykey2'", Collections.emptyList()).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=2138,s1='foo' where k1 = 'mykey2' and s1 is null", Collections.emptyList()).getUpdateCount());
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.tsql where k1 ='mykey2'", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
            try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
                assertEquals(1, scan1.consume().size());
            }
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 FROM tblspace1.tsql where k1 ='mykey2'", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
            try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
                List<DataAccessor> records = scan1.consume();
                assertEquals(1, records.size());
                System.out.println("records:" + records);
                assertEquals(1, records.get(0).getFieldNames().length);
                assertEquals(1, records.get(0).toMap().size());
                assertEquals("k1", records.get(0).getFieldNames()[0]);
                assertEquals(RawString.of("mykey2"), records.get(0).get(0));
            }
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 theKey FROM tblspace1.tsql where k1 ='mykey2'", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
            PlannerOp plannerOp = translate1.plan.mainStatement.unwrap(PlannerOp.class);
            System.out.println("plannerOp:" + plannerOp);
            try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
                List<DataAccessor> records = scan1.consume();
                assertEquals(1, records.size());
                assertEquals(1, records.get(0).getFieldNames().length);
                assertEquals(1, records.get(0).toMap().size());
                assertEquals("thekey", records.get(0).getFieldNames()[0].toLowerCase());
                System.out.println("type: " + records.get(0).getClass());
                assertEquals(RawString.of("mykey2"), records.get(0).get(0));
            }
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 as theKey,'one' as theStringConstant,3  LongConstant FROM tblspace1.tsql where k1 ='mykey2'", Collections.emptyList(), true, true, false, -1);
            try (DataScanner scan1 = ((ScanResult) manager.executePlan(translate1.plan, translate1.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
                List<DataAccessor> records = scan1.consume();
                assertEquals(1, records.size());
                assertEquals(3, records.get(0).getFieldNames().length);
                assertEquals(3, records.get(0).toMap().size());
                assertEquals("thekey", records.get(0).getFieldNames()[0].toLowerCase());
                assertEquals(RawString.of("mykey2"), records.get(0).get(0));
                assertEquals("thestringconstant", records.get(0).getFieldNames()[1].toLowerCase());
                assertEquals(RawString.of("one"), records.get(0).get(1));
                assertEquals("longconstant", records.get(0).getFieldNames()[2].toLowerCase());
                assertEquals(3, ((Number) records.get(0).get(2)).longValue());
            }
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.tsql where k1 ='mykey2' and s1 is not null", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
            try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
                assertEquals(1, scan1.consume().size());
            }
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=2138,s1='bar' where k1 = 'mykey2' and s1 is not null", Collections.emptyList()).getUpdateCount());
        }
        {
            assertEquals(0, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=2138,s1='bar' where k1 = 'mykey2' and s1 is null", Collections.emptyList()).getUpdateCount());
        }
        {
            assertEquals(0, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=2138,s1='bar' where k1 = 'mykey2' and not (s1 is not null)", Collections.emptyList()).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=2138,s1='bar' where k1 = 'mykey2' and not (s1 is null)", Collections.emptyList()).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "DELETE FROM  tblspace1.tsql where k1 = 'mykey2' and s1 is not null", Collections.emptyList()).getUpdateCount());
            assertEquals(0, executeUpdate(manager, "DELETE FROM  tblspace1.tsql where k1 = 'mykey2' and s1 is not null", Collections.emptyList()).getUpdateCount());
        }
        {
            GetResult result = manager.get(new GetStatement("tblspace1", "tsql", Bytes.from_string("mykey2"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertFalse(result.found());
        }
        {
            TransactionResult result = (TransactionResult) execute(manager, "EXECUTE BEGINTRANSACTION 'tblspace1'", Collections.emptyList());
            long tx = result.getTransactionId();
            execute(manager, "EXECUTE COMMITTRANSACTION 'tblspace1'," + tx, Collections.emptyList());
            ;
        }
        {
            TransactionResult result = (TransactionResult) execute(manager, "EXECUTE BEGINTRANSACTION 'tblspace1'", Collections.emptyList());
            long tx = result.getTransactionId();
            execute(manager, "EXECUTE ROLLBACKTRANSACTION 'tblspace1'," + tx, Collections.emptyList());
        }
    }
}
Also used : TransactionResult(herddb.model.TransactionResult) TranslatedQuery(herddb.sql.TranslatedQuery) PlannerOp(herddb.model.planner.PlannerOp) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) GetResult(herddb.model.GetResult) RawString(herddb.utils.RawString) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) GetStatement(herddb.model.commands.GetStatement) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) List(java.util.List) Map(java.util.Map) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Aggregations

GetStatement (herddb.model.commands.GetStatement)100 GetResult (herddb.model.GetResult)92 Test (org.junit.Test)90 InsertStatement (herddb.model.commands.InsertStatement)87 Table (herddb.model.Table)75 CreateTableStatement (herddb.model.commands.CreateTableStatement)72 Record (herddb.model.Record)56 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)50 Bytes (herddb.utils.Bytes)48 Path (java.nio.file.Path)45 TransactionContext (herddb.model.TransactionContext)39 FileCommitLogManager (herddb.file.FileCommitLogManager)30 FileDataStorageManager (herddb.file.FileDataStorageManager)30 FileMetadataStorageManager (herddb.file.FileMetadataStorageManager)30 TransactionResult (herddb.model.TransactionResult)28 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)27 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)26 UpdateStatement (herddb.model.commands.UpdateStatement)25 HashSet (java.util.HashSet)24 ScanStatement (herddb.model.commands.ScanStatement)23