Search in sources :

Example 6 with ScanResultSet

use of herddb.client.ScanResultSet in project herddb by diennea.

the class HistoryChangelogTest method test.

@Test
@Ignore
public void test() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    ServerConfiguration serverConfiguration = newServerConfigurationWithAutoPort(baseDir);
    serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 10 * 1024);
    serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_DATA_MEMORY, 1024 * 1024);
    serverConfiguration.set(ServerConfiguration.PROPERTY_MAX_PK_MEMORY, 1024 * 1024);
    serverConfiguration.set(ServerConfiguration.PROPERTY_CHECKPOINT_PERIOD, 0);
    serverConfiguration.set(ServerConfiguration.PROPERTY_DATADIR, folder.newFolder().getAbsolutePath());
    serverConfiguration.set(ServerConfiguration.PROPERTY_LOGDIR, folder.newFolder().getAbsolutePath());
    ConcurrentSkipListSet<Long> doneElements = new ConcurrentSkipListSet<>();
    ConcurrentHashMap<Long, Element> elements = new ConcurrentHashMap<>();
    try (Server server = new Server(serverConfiguration)) {
        server.start();
        server.waitForStandaloneBoot();
        ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
        try (HDBClient client = new HDBClient(clientConfiguration);
            HDBConnection connection = client.openConnection()) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id long primary key, hid long, status integer)", 0, false, true, Collections.emptyList()).updateCount;
            Assert.assertEquals(1, resultCreateTable);
            long resultCreateTableHistory = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE history (id long, hid long, status integer, primary key (id,hid) )", 0, false, true, Collections.emptyList()).updateCount;
            Assert.assertEquals(1, resultCreateTableHistory);
            long tx = connection.beginTransaction(TableSpace.DEFAULT);
            for (long i = 0; i < TABLESIZE; i++) {
                int status = 0;
                connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,hid,status) values(?,?,?)", tx, false, true, Arrays.asList(i, 0, status));
                elements.put(i, new Element(0, status));
            }
            connection.commitTransaction(TableSpace.DEFAULT, tx);
            ExecutorService threadPool = Executors.newFixedThreadPool(THREADPOLSIZE);
            try {
                List<Future> futures = new ArrayList<>();
                AtomicLong updates = new AtomicLong();
                for (int i = 0; i < TABLESIZE * MULTIPLIER; i++) {
                    futures.add(threadPool.submit(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                long id = ThreadLocalRandom.current().nextInt(TABLESIZE);
                                doneElements.add(id);
                                Element element = elements.remove(id);
                                if (element == null) {
                                    return;
                                }
                                int new_status = element.status + 1;
                                long next_hid = element.hid + 1;
                                long transactionId;
                                updates.incrementAndGet();
                                DMLResult updateResult = connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set hid=?,status=? WHERE id=?", TransactionContext.AUTOTRANSACTION_ID, false, true, Arrays.asList(next_hid, new_status, id));
                                transactionId = updateResult.transactionId;
                                if (updateResult.updateCount <= 0) {
                                    throw new RuntimeException("not updated ?");
                                }
                                DMLResult insertResult = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO history (id,hid,status) values (?,?,?)", transactionId, false, true, Arrays.asList(id, next_hid, new_status));
                                if (insertResult.updateCount <= 0) {
                                    throw new RuntimeException("not inserted ?");
                                }
                                connection.commitTransaction(TableSpace.DEFAULT, transactionId);
                                // make the element available again
                                elements.put(id, new Element(new_status, next_hid));
                            } catch (Exception err) {
                                err.printStackTrace();
                                throw new RuntimeException(err);
                            }
                        }
                    }));
                }
                for (Future f : futures) {
                    f.get();
                }
                System.out.println("stats::updates:" + updates);
                assertTrue(updates.get() > 0);
                TableManagerStats stats = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTableManager("mytable").getStats();
                System.out.println("stats::tablesize:" + stats.getTablesize());
                System.out.println("stats::dirty records:" + stats.getDirtyrecords());
                System.out.println("stats::unload count:" + stats.getUnloadedPagesCount());
                System.out.println("stats::load count:" + stats.getLoadedPagesCount());
                System.out.println("stats::buffers used mem:" + stats.getBuffersUsedMemory());
                assertEquals(TABLESIZE, stats.getTablesize());
                for (Map.Entry<Long, Element> entry : elements.entrySet()) {
                    {
                        GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT status,hid FROM mytable where id=?", TransactionContext.NOTRANSACTION_ID, true, Arrays.asList(entry.getKey()));
                        assertNotNull(res.data);
                        assertEquals(entry.getValue().status, res.data.get(RawString.of("status")));
                        assertEquals(entry.getValue().hid, res.data.get(RawString.of("hid")));
                    }
                    if (doneElements.contains(entry.getKey())) {
                        ScanResultSet res = connection.executeScan(TableSpace.DEFAULT, "SELECT id, status, hid, (SELECT MAX(hid) as mm  from history where history.id=mytable.id) as maxhid " + "FROM mytable where id=?", true, Arrays.asList(entry.getKey()), TransactionContext.NOTRANSACTION_ID, -1, 10000, true);
                        List<Map<String, Object>> consume = res.consume();
                        assertEquals(1, consume.size());
                        Map<String, Object> data = consume.get(0);
                        System.out.println("data:" + data);
                        assertEquals(entry.getValue().status, data.get("status"));
                        assertEquals(entry.getValue().hid, data.get("hid"));
                        assertEquals(entry.getValue().hid, data.get("maxhid"));
                        assertEquals(entry.getKey(), data.get("id"));
                    }
                }
            } finally {
                threadPool.shutdown();
                threadPool.awaitTermination(1, TimeUnit.MINUTES);
            }
        }
    }
    // restart and recovery
    try (Server server = new Server(serverConfiguration)) {
        server.start();
        server.waitForStandaloneBoot();
        ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
        try (HDBClient client = new HDBClient(clientConfiguration);
            HDBConnection connection = client.openConnection()) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            for (Map.Entry<Long, Element> entry : elements.entrySet()) {
                {
                    GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT status,hid FROM mytable where id=?", TransactionContext.NOTRANSACTION_ID, true, Arrays.asList(entry.getKey()));
                    assertNotNull(res.data);
                    assertEquals(entry.getValue().status, res.data.get(RawString.of("status")));
                    assertEquals(entry.getValue().hid, res.data.get(RawString.of("hid")));
                }
                if (doneElements.contains(entry.getKey())) {
                    ScanResultSet res = connection.executeScan(TableSpace.DEFAULT, "SELECT id, status, hid, (SELECT MAX(hid) as mm  from history where history.id=mytable.id) as maxhid " + "FROM mytable where id=?", true, Arrays.asList(entry.getKey()), TransactionContext.NOTRANSACTION_ID, -1, 10000, true);
                    List<Map<String, Object>> consume = res.consume();
                    assertEquals(1, consume.size());
                    Map<String, Object> data = consume.get(0);
                    System.out.println("data:" + data);
                    assertEquals(entry.getValue().status, data.get("status"));
                    assertEquals(entry.getValue().hid, data.get("hid"));
                    assertEquals(entry.getValue().hid, data.get("maxhid"));
                    assertEquals(entry.getKey(), data.get("id"));
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ScanResultSet(herddb.client.ScanResultSet) RawString(herddb.utils.RawString) HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) DMLResult(herddb.client.DMLResult) TableManagerStats(herddb.core.stats.TableManagerStats) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Path(java.nio.file.Path) GetResult(herddb.client.GetResult) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClientConfiguration(herddb.client.ClientConfiguration) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with ScanResultSet

use of herddb.client.ScanResultSet 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 8 with ScanResultSet

use of herddb.client.ScanResultSet in project herddb by diennea.

the class SimpleClientScanTest method checkCloseResultSetNotFullyScanned.

private void checkCloseResultSetNotFullyScanned(final HDBConnection connection, final Server server, boolean withTransaction) throws HDBException, ClientSideMetadataProviderException, InterruptedException {
    long tx = withTransaction ? connection.beginTransaction(TableSpace.DEFAULT) : 0;
    // scan with fetchSize = 1, we will have 100 chunks
    ServerSideConnectionPeer peerWithScanner = null;
    int chunks = 0;
    try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable", true, Collections.emptyList(), tx, 0, 1, true)) {
        while (scan.hasNext()) {
            System.out.println("next:" + scan.next());
            for (ServerSideConnectionPeer peer : server.getConnections().values()) {
                System.out.println("peer " + peer + " scanners: " + peer.getScanners());
                if (!peer.getScanners().isEmpty()) {
                    if (peerWithScanner != null && peerWithScanner != peer) {
                        fail("Found more then one peer with an open scanner");
                    }
                    peerWithScanner = peer;
                    assertEquals(1, peer.getScanners().size());
                }
            }
            assertNotNull(peerWithScanner);
            if (chunks++ >= 5) {
                break;
            }
        }
    }
    assertNotNull(peerWithScanner);
    assertEquals(6, chunks);
    if (withTransaction) {
        connection.rollbackTransaction(TableSpace.DEFAULT, tx);
    }
}
Also used : ScanResultSet(herddb.client.ScanResultSet)

Example 9 with ScanResultSet

use of herddb.client.ScanResultSet 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 10 with ScanResultSet

use of herddb.client.ScanResultSet in project herddb by diennea.

the class SimpleClusterTest method test.

@Test
public void test() throws Exception {
    try (TestingServer zookeeperServer = new TestingServer(-1, folder.newFolder("zk"))) {
        File tmpConfFile = folder.newFile("test.server_cluster.properties");
        try (InputStream in = SimpleClusterTest.class.getResourceAsStream("/conf/test.server_cluster.properties")) {
            Properties props = new Properties();
            props.load(in);
            props.put(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().getAbsolutePath());
            props.put(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, zookeeperServer.getConnectString());
            try (FileOutputStream oo = new FileOutputStream(tmpConfFile)) {
                props.store(oo, "");
            }
        }
        Thread runner = new Thread(() -> {
            ServerMain.main(tmpConfFile.getAbsolutePath());
        });
        runner.start();
        while (ServerMain.getRunningInstance() == null || !ServerMain.getRunningInstance().isStarted()) {
            Thread.sleep(1000);
            System.out.println("waiting for boot");
        }
        ServerMain.getRunningInstance().getServer().waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new ZookeeperClientSideMetadataProvider(zookeeperServer.getConnectString(), 40000, "/herddb"));
            try (HDBConnection con = client.openConnection()) {
                try (ScanResultSet scan = con.executeScan(TableSpace.DEFAULT, "SELECT * FROM SYSTABLES", false, Collections.emptyList(), 0, 10, 10, false)) {
                    scan.consume();
                }
            }
        }
        ServerMain.getRunningInstance().close();
    } finally {
        if (ServerMain.getRunningInstance() != null) {
            ServerMain.getRunningInstance().close();
        }
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZookeeperClientSideMetadataProvider(herddb.client.ZookeeperClientSideMetadataProvider) ScanResultSet(herddb.client.ScanResultSet) Properties(java.util.Properties) File(java.io.File) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Aggregations

ScanResultSet (herddb.client.ScanResultSet)18 ClientConfiguration (herddb.client.ClientConfiguration)13 HDBClient (herddb.client.HDBClient)13 HDBConnection (herddb.client.HDBConnection)13 Test (org.junit.Test)10 ClientSideMetadataProviderException (herddb.client.ClientSideMetadataProviderException)7 TableSpaceManager (herddb.core.TableSpaceManager)7 GetResult (herddb.model.GetResult)6 Table (herddb.model.Table)6 AlterTableSpaceStatement (herddb.model.commands.AlterTableSpaceStatement)6 CreateTableStatement (herddb.model.commands.CreateTableStatement)6 GetStatement (herddb.model.commands.GetStatement)6 InsertStatement (herddb.model.commands.InsertStatement)6 HashSet (java.util.HashSet)6 GetResult (herddb.client.GetResult)4 Map (java.util.Map)4 DMLResult (herddb.client.DMLResult)3 LeaderChangedException (herddb.client.impl.LeaderChangedException)3 Path (java.nio.file.Path)3 HDBException (herddb.client.HDBException)2