use of herddb.client.HDBConnection in project herddb by diennea.
the class JAASMD5Test method test.
@Test
public void test() throws Exception {
System.setProperty("java.security.auth.login.config", new File("src/test/resources/test_jaas_md5.conf").getAbsolutePath());
try (Server server = new Server(newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
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, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
Assert.assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", 0, false, true, Arrays.asList("test_0", 1, 2)).updateCount);
Map<RawString, Object> newValue = connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set n1= n1+1 where id=?", 0, true, true, Arrays.asList("test_0")).newvalue;
assertEquals(Long.valueOf(2), newValue.get(RawString.of("n1")));
}
} finally {
System.clearProperty("java.security.auth.login.config");
}
}
use of herddb.client.HDBConnection 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());
}
}
}
}
}
use of herddb.client.HDBConnection in project herddb by diennea.
the class DeleteCheckpointFilesTest method test.
@Test
public void test() throws Exception {
ServerConfiguration serverConfiguration = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
try (Server server = new Server(serverConfiguration)) {
server.start();
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()));
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
server.waitForStandaloneBoot();
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
long resultCreateIndex = connection.executeUpdate(TableSpace.DEFAULT, "CREATE HASH INDEX myhashindex on mytable (n2)", 0, false, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateIndex);
long resultCreateIndex2 = connection.executeUpdate(TableSpace.DEFAULT, "CREATE BRIN INDEX mybrinindex on mytable (n1)", 0, false, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateIndex2);
Assert.assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", 0, false, true, Arrays.asList("test_0", 1, 2)).updateCount);
Map<RawString, Object> newValue = connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set n1= n1+1 where id=?", 0, true, true, Arrays.asList("test_0")).newvalue;
assertEquals(Long.valueOf(2), newValue.get(RawString.of("n1")));
server.getManager().checkpoint();
server.getManager().checkpoint();
Assert.assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", 0, false, true, Arrays.asList("test_1", 2, 2)).updateCount);
server.getManager().checkpoint();
try (DataScanner scan = TestUtils.scan(server.getManager(), "SELECT * FROM mytable WHERE n2 = ?", Arrays.asList(2))) {
assertEquals(2, scan.consume().size());
}
try (DataScanner scan = TestUtils.scan(server.getManager(), "SELECT * FROM mytable WHERE n1 = ?", Arrays.asList(2))) {
assertEquals(2, scan.consume().size());
}
}
}
try (Server server = new Server(serverConfiguration)) {
server.start();
server.waitForStandaloneBoot();
try (DataScanner scan = TestUtils.scan(server.getManager(), "SELECT * FROM mytable WHERE n2 = ?", Arrays.asList(2))) {
assertEquals(2, scan.consume().size());
}
try (DataScanner scan = TestUtils.scan(server.getManager(), "SELECT * FROM mytable WHERE n1 = ?", Arrays.asList(2))) {
assertEquals(2, scan.consume().size());
}
}
}
use of herddb.client.HDBConnection 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();
}
}
}
use of herddb.client.HDBConnection in project herddb by diennea.
the class BackupRestoreTest method test_backup_restore_with_updates.
/**
* Check that restore a dirty update restores the latest record version
*/
@Test
public void test_backup_restore_with_updates() throws Exception {
ServerConfiguration serverconfig_1 = new ServerConfiguration(folder.newFolder().toPath());
serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
serverconfig_1.set(ServerConfiguration.PROPERTY_PORT, 7867);
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);
/* Disable page compaction (avoid compaction of dirty page) */
serverconfig_1.set(ServerConfiguration.PROPERTY_FILL_PAGE_THRESHOLD, 0.0D);
ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath()).set(ServerConfiguration.PROPERTY_PORT, 7868);
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 (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();
DBManager manager = server_1.getManager();
manager.executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeUpdate(new InsertStatement(TableSpace.DEFAULT, table.name, RecordSerializer.makeRecord(table, "c", 1, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeUpdate(new InsertStatement(TableSpace.DEFAULT, table.name, RecordSerializer.makeRecord(table, "c", 2, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeUpdate(new InsertStatement(TableSpace.DEFAULT, table.name, RecordSerializer.makeRecord(table, "c", 3, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeUpdate(new InsertStatement(TableSpace.DEFAULT, table.name, RecordSerializer.makeRecord(table, "c", 4, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.checkpoint();
/* Check that new data isn't in a dirty page */
assertEquals(0, manager.getTableSpaceManager(TableSpace.DEFAULT).getTableManager(table.name).getStats().getDirtypages());
final Record update = RecordSerializer.makeRecord(table, "c", 2, "d", 22);
manager.executeUpdate(new InsertStatement(TableSpace.DEFAULT, table.name, RecordSerializer.makeRecord(table, "c", 5, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.executeUpdate(new UpdateStatement(TableSpace.DEFAULT, table.name, new ConstValueRecordFunction(update.key.data), new ConstValueRecordFunction(update.value.data), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
/* Now we have a dirty page in the checkpoint */
manager.checkpoint();
/* Check that a dirty page exists */
assertEquals(1, manager.getTableSpaceManager(TableSpace.DEFAULT).getTableManager(table.name).getStats().getDirtypages());
try (Server server_2 = new Server(serverconfig_2)) {
server_2.start();
try (HDBClient client = new HDBClient(client_configuration);
HDBConnection connection = client.openConnection()) {
assertEquals(5, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1", Collections.emptyList(), 0, 0, 10).consume().size());
ByteArrayOutputStream oo = new ByteArrayOutputStream();
BackupUtils.dumpTableSpace(TableSpace.DEFAULT, 64 * 1024, connection, oo, new ProgressListener() {
});
byte[] backupData = oo.toByteArray();
connection.executeUpdate(TableSpace.DEFAULT, "DELETE FROM t1", 0, false, Collections.emptyList());
assertEquals(0, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1", Collections.emptyList(), 0, 0, 10).consume().size());
BackupUtils.restoreTableSpace("newts", server_2.getNodeId(), connection, new ByteArrayInputStream(backupData), new ProgressListener() {
});
/* No new insert AND no delete... if 5 it did see the new insert but missed the delete! */
assertEquals(5, connection.executeScan("newts", "SELECT * FROM newts.t1", Collections.emptyList(), 0, 0, 10).consume().size());
}
}
}
}
Aggregations