Search in sources :

Example 11 with ScanResultSet

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

the class SimpleClientServerAutoTransactionTest method test.

@Test
public void test() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    ;
    String _baseDir = baseDir.toString();
    try (Server server = new Server(new ServerConfiguration(baseDir))) {
        server.start();
        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 string primary key, n1 long, n2 integer)", 0, false, Collections.emptyList()).updateCount;
            Assert.assertEquals(1, resultCreateTable);
            DMLResult executeUpdateResult = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", TransactionContext.AUTOTRANSACTION_ID, false, Arrays.asList("test", 1, 2));
            long tx = executeUpdateResult.transactionId;
            long countInsert = executeUpdateResult.updateCount;
            Assert.assertEquals(1, countInsert);
            GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", tx, Collections.emptyList());
            ;
            Map<String, Object> record = res.data;
            Assert.assertNotNull(record);
            assertEquals("test", record.get("id"));
            assertEquals(Long.valueOf(1), record.get("n1"));
            assertEquals(Integer.valueOf(2), record.get("n2"));
            connection.commitTransaction(TableSpace.DEFAULT, tx);
            try (ScanResultSet scan = connection.executeScan(server.getManager().getVirtualTableSpaceId(), "SELECT * FROM sysconfig", Collections.emptyList(), 0, 0, 10)) {
                List<Map<String, Object>> all = scan.consume();
                for (Map<String, Object> aa : all) {
                    String name = (String) aa.get("name");
                    assertEquals("server.base.dir", name);
                    String value = (String) aa.get("value");
                    assertEquals(_baseDir, value);
                }
            }
            try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server.getManager().getVirtualTableSpaceId() + ".sysclients", Collections.emptyList(), 0, 0, 10)) {
                List<Map<String, Object>> all = scan.consume();
                for (Map<String, Object> aa : all) {
                    assertEquals("jvm-local", aa.get("address"));
                    assertEquals("1", aa.get("id") + "");
                    assertEquals(ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT, aa.get("username"));
                    assertNotNull(aa.get("connectionts"));
                }
                assertEquals(1, all.size());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) GetResult(herddb.client.GetResult) ScanResultSet(herddb.client.ScanResultSet) HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) DMLResult(herddb.client.DMLResult) Map(java.util.Map) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 12 with ScanResultSet

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

the class SimpleClientServerTest method test.

@Test
public void test() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    ;
    String _baseDir = baseDir.toString();
    try (Server server = new Server(new ServerConfiguration(baseDir))) {
        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));
            assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, 100));
            assertFalse(connection.waitForTableSpace("bad-tablespace", 100));
            long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, Collections.emptyList()).updateCount;
            Assert.assertEquals(1, resultCreateTable);
            long tx = connection.beginTransaction(TableSpace.DEFAULT);
            long countInsert = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, Arrays.asList("test", 1, 2)).updateCount;
            Assert.assertEquals(1, countInsert);
            long countInsert2 = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, Arrays.asList("test2", 2, 3)).updateCount;
            Assert.assertEquals(1, countInsert2);
            GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", tx, Collections.emptyList());
            Map<String, Object> record = res.data;
            Assert.assertNotNull(record);
            assertEquals("test", record.get("id"));
            assertEquals(Long.valueOf(1), record.get("n1"));
            assertEquals(Integer.valueOf(2), record.get("n2"));
            List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", tx, false, Arrays.asList(Arrays.asList(1, "test"), Arrays.asList(2, "test2"), Arrays.asList(3, "test_not_exists")));
            assertEquals(3, executeUpdates.size());
            assertEquals(1, executeUpdates.get(0).updateCount);
            assertEquals(1, executeUpdates.get(1).updateCount);
            assertEquals(0, executeUpdates.get(2).updateCount);
            assertEquals(tx, executeUpdates.get(0).transactionId);
            assertEquals(tx, executeUpdates.get(1).transactionId);
            assertEquals(tx, executeUpdates.get(2).transactionId);
            connection.commitTransaction(TableSpace.DEFAULT, tx);
            try (ScanResultSet scan = connection.executeScan(server.getManager().getVirtualTableSpaceId(), "SELECT * FROM sysconfig", Collections.emptyList(), 0, 0, 10)) {
                List<Map<String, Object>> all = scan.consume();
                for (Map<String, Object> aa : all) {
                    String name = (String) aa.get("name");
                    assertEquals("server.base.dir", name);
                    String value = (String) aa.get("value");
                    assertEquals(_baseDir, value);
                }
            }
            try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server.getManager().getVirtualTableSpaceId() + ".sysclients", Collections.emptyList(), 0, 0, 10)) {
                List<Map<String, Object>> all = scan.consume();
                for (Map<String, Object> aa : all) {
                    assertEquals("jvm-local", aa.get("address"));
                    assertNotNull(aa.get("id"));
                    assertEquals(ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT, aa.get("username"));
                    assertNotNull(aa.get("connectionts"));
                }
                assertEquals(1, all.size());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) GetResult(herddb.client.GetResult) ScanResultSet(herddb.client.ScanResultSet) HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) DMLResult(herddb.client.DMLResult) Map(java.util.Map) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 13 with ScanResultSet

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

the class RetryOnLeaderChangedTest method test.

@Test
public void test() throws Exception {
    TestStatsProvider statsProvider = new TestStatsProvider();
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
    serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            TestUtils.execute(server_1.getManager(), "CREATE TABLESPACE 'ttt','leader:" + server_2.getNodeId() + "','expectedreplicacount:2'", Collections.emptyList());
            // wait for server_2 to wake up
            for (int i = 0; i < 40; i++) {
                TableSpaceManager tableSpaceManager2 = server_2.getManager().getTableSpaceManager("ttt");
                if (tableSpaceManager2 != null && tableSpaceManager2.isLeader()) {
                    break;
                }
                Thread.sleep(500);
            }
            assertTrue(server_2.getManager().getTableSpaceManager("ttt") != null && server_2.getManager().getTableSpaceManager("ttt").isLeader());
            // wait for server_1 to announce as follower
            waitClusterStatus(server_1.getManager(), server_1.getNodeId(), "follower");
            ClientConfiguration clientConfiguration = new ClientConfiguration();
            clientConfiguration.set(ClientConfiguration.PROPERTY_MODE, ClientConfiguration.PROPERTY_MODE_CLUSTER);
            clientConfiguration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
            clientConfiguration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
            clientConfiguration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
            StatsLogger logger = statsProvider.getStatsLogger("ds");
            try (HDBClient client1 = new HDBClient(clientConfiguration, logger)) {
                try (HDBConnection connection = client1.openConnection()) {
                    // create table and insert data
                    connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE ttt.t1(k1 int primary key, n1 int)", TransactionContext.NOTRANSACTION_ID, false, false, Collections.emptyList());
                    connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO ttt.t1(k1,n1) values(1,1)", TransactionContext.NOTRANSACTION_ID, false, false, Collections.emptyList());
                    connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO ttt.t1(k1,n1) values(2,1)", TransactionContext.NOTRANSACTION_ID, false, false, Collections.emptyList());
                    connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO ttt.t1(k1,n1) values(3,1)", TransactionContext.NOTRANSACTION_ID, false, false, Collections.emptyList());
                    // use client2, so that it opens a connection to current leader
                    try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM ttt.t1", false, Collections.emptyList(), TransactionContext.NOTRANSACTION_ID, 0, 0, true)) {
                        assertEquals(3, scan.consume().size());
                    }
                    // change leader
                    switchLeader(server_1.getNodeId(), server_2.getNodeId(), server_1.getManager());
                    // perform operation
                    try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM ttt.t1", false, Collections.emptyList(), TransactionContext.NOTRANSACTION_ID, 0, 0, true)) {
                        assertEquals(3, scan.consume().size());
                    }
                    // check the client handled "not leader error"
                    assertEquals(1, logger.scope("hdbclient").getCounter("leaderChangedErrors").get().intValue());
                    // change leader
                    switchLeader(server_2.getNodeId(), server_1.getNodeId(), server_1.getManager());
                    // perform operation
                    GetResult get = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM ttt.t1 where k1=1", TransactionContext.NOTRANSACTION_ID, false, Collections.emptyList());
                    assertTrue(get.isFound());
                    // check the client handled "not leader error"
                    assertEquals(2, logger.scope("hdbclient").getCounter("leaderChangedErrors").get().intValue());
                    // change leader
                    switchLeader(server_1.getNodeId(), server_2.getNodeId(), server_1.getManager());
                    // perform operation
                    assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "UPDATE ttt.t1 set n1=3 where k1=1", TransactionContext.NOTRANSACTION_ID, false, false, Collections.emptyList()).updateCount);
                    // check the client handled "not leader error"
                    assertEquals(3, logger.scope("hdbclient").getCounter("leaderChangedErrors").get().intValue());
                    // change leader
                    switchLeader(server_2.getNodeId(), server_1.getNodeId(), server_1.getManager());
                    // perform operation
                    assertEquals(1, connection.executeUpdateAsync(TableSpace.DEFAULT, "UPDATE ttt.t1 set n1=3 where k1=1", TransactionContext.NOTRANSACTION_ID, false, false, Collections.emptyList()).get().updateCount);
                    // check the client handled "not leader error"
                    assertEquals(4, logger.scope("hdbclient").getCounter("leaderChangedErrors").get().intValue());
                    // change leader
                    switchLeader(server_1.getNodeId(), server_2.getNodeId(), server_1.getManager());
                    // perform operation
                    assertEquals(1, connection.executeUpdates(TableSpace.DEFAULT, "UPDATE ttt.t1 set n1=3 where k1=1", TransactionContext.NOTRANSACTION_ID, false, false, Arrays.asList(Collections.emptyList())).get(0).updateCount);
                    // check the client handled "not leader error"
                    assertEquals(5, logger.scope("hdbclient").getCounter("leaderChangedErrors").get().intValue());
                    // change leader
                    switchLeader(server_2.getNodeId(), server_1.getNodeId(), server_1.getManager());
                    // perform operation
                    assertEquals(1, connection.executeUpdatesAsync(TableSpace.DEFAULT, "UPDATE ttt.t1 set n1=3 where k1=1", TransactionContext.NOTRANSACTION_ID, false, false, Arrays.asList(Collections.emptyList())).get().get(0).updateCount);
                    // check the client handled "not leader error"
                    assertEquals(6, logger.scope("hdbclient").getCounter("leaderChangedErrors").get().intValue());
                }
            }
        }
    }
}
Also used : TestStatsProvider(org.apache.bookkeeper.test.TestStatsProvider) StatsLogger(org.apache.bookkeeper.stats.StatsLogger) HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) GetResult(herddb.client.GetResult) ServerConfiguration(herddb.server.ServerConfiguration) TableSpaceManager(herddb.core.TableSpaceManager) ScanResultSet(herddb.client.ScanResultSet) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 14 with ScanResultSet

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

the class HerdDBPreparedStatement method executeQuery.

@Override
public ResultSet executeQuery() throws SQLException {
    try {
        parent.discoverTableSpace(sql);
        ScanResultSet scanResult = this.parent.getConnection().executeScan(parent.getTableSpace(), sql, true, parameters, parent.ensureTransaction(), maxRows, fetchSize, parent.isKeepReadLocks());
        this.parent.bindToTransaction(scanResult.transactionId);
        return lastResultSet = new HerdDBResultSet(scanResult, this);
    } catch (ClientSideMetadataProviderException | HDBException | InterruptedException ex) {
        throw SQLExceptionUtils.wrapException(ex);
    }
}
Also used : ScanResultSet(herddb.client.ScanResultSet) ClientSideMetadataProviderException(herddb.client.ClientSideMetadataProviderException) HDBException(herddb.client.HDBException)

Example 15 with ScanResultSet

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

the class HerdDBStatement method executeQuery.

@Override
public ResultSet executeQuery(String sql) throws SQLException {
    try {
        parent.discoverTableSpace(sql);
        ScanResultSet scanResult = this.parent.getConnection().executeScan(parent.getTableSpace(), sql, false, Collections.emptyList(), parent.ensureTransaction(), maxRows, fetchSize, parent.isKeepReadLocks());
        parent.bindToTransaction(scanResult.transactionId);
        return lastResultSet = new HerdDBResultSet(scanResult, this);
    } catch (ClientSideMetadataProviderException | HDBException | InterruptedException ex) {
        throw SQLExceptionUtils.wrapException(ex);
    }
}
Also used : EmptyScanResultSet(herddb.client.impl.EmptyScanResultSet) SingletonScanResultSet(herddb.client.impl.SingletonScanResultSet) ScanResultSet(herddb.client.ScanResultSet) ClientSideMetadataProviderException(herddb.client.ClientSideMetadataProviderException) HDBException(herddb.client.HDBException)

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