Search in sources :

Example 46 with ClientConfiguration

use of herddb.client.ClientConfiguration 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 47 with ClientConfiguration

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

the class JAASKerberosTest method test.

@Test
public void test() throws Exception {
    ServerConfiguration serverConfig = new ServerConfiguration(folder.newFolder().toPath());
    serverConfig.set(ServerConfiguration.PROPERTY_HOST, "localhost.localdomain");
    try (Server server = new Server(serverConfig)) {
        server.start();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()));
            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);
            Assert.assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", 0, false, Arrays.asList("test_0", 1, 2)).updateCount);
            Map<String, Object> newValue = connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set n1= n1+1 where id=?", 0, true, Arrays.asList("test_0")).newvalue;
            assertEquals(Long.valueOf(2), newValue.get("n1"));
        }
    } finally {
        System.clearProperty("java.security.auth.login.config");
    }
}
Also used : HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 48 with ClientConfiguration

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

the class MultipleConcurrentUpdatesTest method performTest.

private void performTest(boolean useTransactions, long checkPointPeriod, boolean withIndexes) throws Exception {
    Path baseDir = folder.newFolder().toPath();
    ServerConfiguration serverConfiguration = new ServerConfiguration(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, checkPointPeriod);
    serverConfiguration.set(ServerConfiguration.PROPERTY_DATADIR, folder.newFolder().getAbsolutePath());
    serverConfiguration.set(ServerConfiguration.PROPERTY_LOGDIR, folder.newFolder().getAbsolutePath());
    ConcurrentHashMap<String, Long> expectedValue = 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 string primary key, n1 long, n2 integer)", 0, false, Collections.emptyList()).updateCount;
            Assert.assertEquals(1, resultCreateTable);
            if (withIndexes) {
                long resultCreateIndex = connection.executeUpdate(TableSpace.DEFAULT, "CREATE INDEX theindex ON mytable (n1 long)", 0, false, Collections.emptyList()).updateCount;
                Assert.assertEquals(1, resultCreateIndex);
            }
            long tx = connection.beginTransaction(TableSpace.DEFAULT);
            for (int i = 0; i < TABLESIZE; i++) {
                connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, Arrays.asList("test_" + i, 1, 2));
                expectedValue.put("test_" + i, 1L);
            }
            connection.commitTransaction(TableSpace.DEFAULT, tx);
            ExecutorService threadPool = Executors.newFixedThreadPool(THREADPOLSIZE);
            try {
                List<Future> futures = new ArrayList<>();
                AtomicLong updates = new AtomicLong();
                AtomicLong skipped = new AtomicLong();
                AtomicLong gets = new AtomicLong();
                for (int i = 0; i < TABLESIZE * MULTIPLIER; i++) {
                    futures.add(threadPool.submit(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                boolean update = ThreadLocalRandom.current().nextBoolean();
                                int k = ThreadLocalRandom.current().nextInt(TABLESIZE);
                                long value = ThreadLocalRandom.current().nextInt(TABLESIZE);
                                long transactionId;
                                String key = "test_" + k;
                                Long actual = expectedValue.remove(key);
                                if (actual == null) {
                                    // another thread working on this entry, skip
                                    skipped.incrementAndGet();
                                    return;
                                }
                                if (update) {
                                    updates.incrementAndGet();
                                    DMLResult updateResult = connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set n1=? WHERE id=?", useTransactions ? TransactionContext.AUTOTRANSACTION_ID : TransactionContext.NOTRANSACTION_ID, false, Arrays.asList(value, "test_" + k));
                                    long count = updateResult.updateCount;
                                    transactionId = updateResult.transactionId;
                                    if (count <= 0) {
                                        throw new RuntimeException("not updated ?");
                                    }
                                } else {
                                    gets.incrementAndGet();
                                    GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable where id=?", useTransactions ? TransactionContext.AUTOTRANSACTION_ID : TransactionContext.NOTRANSACTION_ID, Arrays.asList("test_" + k));
                                    if (res.data == null) {
                                        throw new RuntimeException("not found?");
                                    }
                                    if (!res.data.get("n1").equals(actual)) {
                                        throw new RuntimeException("unspected value " + res.data + ", expected: " + actual);
                                    }
                                    transactionId = res.transactionId;
                                    // value did not change actually
                                    value = actual;
                                }
                                if (useTransactions) {
                                    if (transactionId <= 0) {
                                        throw new RuntimeException("no transaction ?");
                                    }
                                    connection.commitTransaction(TableSpace.DEFAULT, transactionId);
                                }
                                expectedValue.put(key, value);
                            } catch (Exception err) {
                                throw new RuntimeException(err);
                            }
                        }
                    }));
                }
                for (Future f : futures) {
                    f.get();
                }
                System.out.println("stats::updates:" + updates);
                System.out.println("stats::get:" + gets);
                System.out.println("stats::skipped:" + skipped);
                assertTrue(updates.get() > 0);
                assertTrue(gets.get() > 0);
                List<String> erroredKeys = new ArrayList<>();
                for (Map.Entry<String, Long> entry : expectedValue.entrySet()) {
                    GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT n1 FROM mytable where id=?", TransactionContext.NOTRANSACTION_ID, Arrays.asList(entry.getKey()));
                    assertNotNull(res.data);
                    if (!entry.getValue().equals(res.data.get("n1"))) {
                        if (!entry.getValue().equals(res.data.get("n1"))) {
                            System.out.println("expected value " + res.data.get("n1") + ", but got " + Long.valueOf(entry.getValue()) + " for key " + entry.getKey());
                            erroredKeys.add(entry.getKey());
                        }
                    }
                }
                assertTrue(erroredKeys.isEmpty());
                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());
                assertTrue(stats.getUnloadedPagesCount() > 0);
                assertEquals(TABLESIZE, stats.getTablesize());
            } 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<String, Long> entry : expectedValue.entrySet()) {
                GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT n1 FROM mytable where id=?", TransactionContext.NOTRANSACTION_ID, Arrays.asList(entry.getKey()));
                assertNotNull(res.data);
                assertEquals(entry.getValue(), res.data.get("n1"));
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) 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) 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)

Example 49 with ClientConfiguration

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

the class RollbackOnCloseConnectionTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.getNetworkServer().setEnableJVMNetwork(false);
        server.start();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            long tx;
            try (HDBConnection connection = client.openConnection()) {
                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);
                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);
                assertTrue(server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getOpenTransactions().contains(tx));
            }
            for (int i = 0; i < 100; i++) {
                if (!server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getOpenTransactions().contains(tx)) {
                    break;
                }
                Thread.sleep(100);
            }
            assertFalse(server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getOpenTransactions().contains(tx));
        }
    }
}
Also used : HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 50 with ClientConfiguration

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

the class SelectColumnTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
                Connection con = dataSource.getConnection();
                Statement statement = con.createStatement()) {
                statement.execute("CREATE TABLE mytable (key " + data.keyType + " primary key, val " + data.valueType + ")");
                try (PreparedStatement insert = con.prepareStatement("INSERT INTO mytable (key,val) values(?,?)")) {
                    for (Object[] odata : data.data) {
                        insert.setObject(1, odata[0]);
                        setParameterAccordingToJavaType(insert, 2, odata[1]);
                        assertEquals(1, insert.executeUpdate());
                    }
                }
                if (data.checkKey()) {
                    try (PreparedStatement selectKey = con.prepareStatement("SELECT key FROM mytable WHERE key = ?")) {
                        for (Object[] odata : data.data) {
                            selectKey.setObject(1, odata[0]);
                            try (ResultSet rs = selectKey.executeQuery()) {
                                int count = 0;
                                while (rs.next()) {
                                    data.keyChecker.accept(rs, odata[0]);
                                    count++;
                                }
                                assertEquals(1, count);
                            }
                        }
                    }
                }
                try (PreparedStatement selectKey = con.prepareStatement("SELECT val FROM mytable WHERE key = ?")) {
                    for (Object[] odata : data.data) {
                        selectKey.setObject(1, odata[0]);
                        try (ResultSet rs = selectKey.executeQuery()) {
                            int count = 0;
                            while (rs.next()) {
                                data.valueChecker.accept(rs, odata[1]);
                                count++;
                            }
                            assertEquals(1, count);
                        }
                    }
                }
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Aggregations

ClientConfiguration (herddb.client.ClientConfiguration)89 HDBClient (herddb.client.HDBClient)83 Test (org.junit.Test)79 Server (herddb.server.Server)56 HDBConnection (herddb.client.HDBConnection)45 StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)41 Connection (java.sql.Connection)39 Statement (java.sql.Statement)34 PreparedStatement (java.sql.PreparedStatement)30 ResultSet (java.sql.ResultSet)29 Table (herddb.model.Table)23 CreateTableStatement (herddb.model.commands.CreateTableStatement)23 InsertStatement (herddb.model.commands.InsertStatement)22 ServerConfiguration (herddb.server.ServerConfiguration)17 ScanResultSet (herddb.client.ScanResultSet)13 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ProgressListener (herddb.backup.ProgressListener)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 HashSet (java.util.HashSet)12 Index (herddb.model.Index)9