use of herddb.server.ServerConfiguration in project herddb by diennea.
the class SimpleCDCTest method testBasicCaptureDataChangeWithTransactions.
@Test
public void testBasicCaptureDataChangeWithTransactions() 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());
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();
// create table in transaction
long tx = TestUtils.beginTransaction(server_1.getManager(), TableSpace.DEFAULT);
server_1.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
// work on the table in transaction
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
// commit
TestUtils.commitTransaction(server_1.getManager(), TableSpace.DEFAULT, tx);
// work on the table outside of the transaction
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
// close the server and the ledger
server_1.close();
List<ChangeDataCapture.Mutation> mutations = new ArrayList<>();
try (final ChangeDataCapture cdc = new ChangeDataCapture(server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTableSpaceUUID(), client_configuration, new ChangeDataCapture.MutationListener() {
@Override
public void accept(ChangeDataCapture.Mutation mutation) {
LOG.log(Level.INFO, "mutation " + mutation);
assertTrue(mutation.getTimestamp() > 0);
assertNotNull(mutation.getLogSequenceNumber());
assertNotNull(mutation.getTable());
mutations.add(mutation);
}
}, LogSequenceNumber.START_OF_TIME, new InMemoryTableHistoryStorage())) {
cdc.start();
cdc.run();
assertEquals(3, mutations.size());
int i = 0;
ChangeDataCapture.Mutation m1 = mutations.get(i++);
assertEquals(ChangeDataCapture.MutationType.CREATE_TABLE, m1.getMutationType());
Table tableFromM1 = m1.getTable();
assertNotNull(tableFromM1);
assertEquals(table, tableFromM1);
ChangeDataCapture.Mutation m2 = mutations.get(i++);
assertEquals(ChangeDataCapture.MutationType.INSERT, m2.getMutationType());
assertEquals(m2.getTable(), tableFromM1);
assertEquals(1, m2.getRecord().get("c"));
assertEquals(2, m2.getRecord().get("d"));
ChangeDataCapture.Mutation m3 = mutations.get(i++);
assertEquals(ChangeDataCapture.MutationType.INSERT, m3.getMutationType());
assertEquals(m3.getTable(), tableFromM1);
assertEquals(2, m3.getRecord().get("c"));
assertEquals(2, m3.getRecord().get("d"));
}
}
}
use of herddb.server.ServerConfiguration in project herddb by diennea.
the class AuthTest method test.
@Test
public void test() throws Exception {
ServerConfiguration serverConfiguration = TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath());
serverConfiguration.set(SimpleSingleUserManager.PROPERTY_ADMIN_USERNAME, "myuser");
serverConfiguration.set(SimpleSingleUserManager.PROPERTY_ADMIN_PASSWORD, "mypassword");
try (Server server = new Server(serverConfiguration)) {
server.start();
server.waitForStandaloneBoot();
try (Connection connection = DriverManager.getConnection(server.getJdbcUrl());
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM SYSTABLES")) {
fail();
} catch (SQLException authFailedExpected) {
authFailedExpected.printStackTrace();
}
try (Connection connection = DriverManager.getConnection(server.getJdbcUrl(), "myuser", "mypassword");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM SYSTABLES")) {
assertTrue(rs.next());
}
}
}
use of herddb.server.ServerConfiguration in project herddb by diennea.
the class JdbcDriverDiskLessClusterTest method testDisklessCluster.
@Test
public void testDisklessCluster() throws Exception {
ServerConfiguration serverconfig_1 = TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath());
serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
serverconfig_1.set(ServerConfiguration.PROPERTY_PORT, 7869);
serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_DISKLESSCLUSTER);
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());
try (Server server = new Server(serverconfig_1)) {
server.start();
server.waitForStandaloneBoot();
try (Connection connection = DriverManager.getConnection("jdbc:herddb:zookeeper:" + testEnv.getAddress() + "" + testEnv.getPath());
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM SYSTABLES")) {
int count = 0;
while (rs.next()) {
System.out.println("table: " + rs.getString(1));
count++;
}
assertTrue(count > 0);
}
}
}
use of herddb.server.ServerConfiguration in project herddb by diennea.
the class RetryOnLeaderChangedTest method testSwitchLeaderAndAuthTimeout.
@Test
public void testSwitchLeaderAndAuthTimeout() 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());
final AtomicBoolean suspendProcessing = new AtomicBoolean(false);
try (Server server_1 = new Server(serverconfig_1)) {
server_1.start();
server_1.waitForStandaloneBoot();
try (Server server_2 = new Server(serverconfig_2) {
@Override
protected ServerSideConnectionPeer buildPeer(Channel channel) {
return new ServerSideConnectionPeer(channel, this) {
@Override
public void requestReceived(Pdu message, Channel channel) {
if (suspendProcessing.get()) {
LOG.log(Level.INFO, "dropping message type " + message.type + " id " + message.messageId);
message.close();
return;
}
super.requestReceived(message, channel);
}
};
}
}) {
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());
clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 2);
clientConfiguration.set(ClientConfiguration.PROPERTY_TIMEOUT, 2000);
StatsLogger logger = statsProvider.getStatsLogger("ds");
try (HDBClient client1 = new HDBClient(clientConfiguration, logger) {
@Override
public HDBConnection openConnection() {
HDBConnection con = new VisibleRouteHDBConnection(this);
registerConnection(con);
return con;
}
}) {
try (VisibleRouteHDBConnection connection = (VisibleRouteHDBConnection) 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());
assertEquals("server2", connection.getRouteToTableSpace("ttt").getNodeId());
// change leader
switchLeader(server_1.getNodeId(), server_2.getNodeId(), server_1.getManager());
try (VisibleRouteHDBConnection connection2 = (VisibleRouteHDBConnection) client1.openConnection()) {
// connection routing still point to old leader (now follower)
assertEquals("server2", connection2.getRouteToTableSpace("ttt").getNodeId());
// suspend server_2 authentication
suspendProcessing.set(true);
// attempt an insert with old routing. Suspended autentication generates a timeout
// and routing will be reevaluated
assertEquals(1, connection2.executeUpdate(TableSpace.DEFAULT, "INSERT INTO ttt.t1(k1,n1) values(2,2)", TransactionContext.NOTRANSACTION_ID, false, false, Collections.emptyList()).updateCount);
// right routing to current master
assertEquals("server1", connection2.getRouteToTableSpace("ttt").getNodeId());
suspendProcessing.set(false);
}
}
}
}
}
}
use of herddb.server.ServerConfiguration in project herddb by diennea.
the class TablespaceReplicasStateTest method testDisklessCluster.
@Test
public void testDisklessCluster() 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_DISKLESSCLUSTER);
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);
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));
try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM systablespacereplicastate", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(2, results.size());
}
// and it writing its state to metadata
try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM systablespacereplicastate " + "where nodeId='" + server_2.getNodeId() + "' and mode='follower'", Collections.emptyList())) {
assertEquals(1, scan.consume().size());
}
try (DataScanner scan = scan(server_2.getManager(), "SELECT * FROM systablespacereplicastate " + "where nodeId='" + server_2.getNodeId() + "' and mode='follower'", Collections.emptyList())) {
assertEquals(1, scan.consume().size());
}
try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM systablespacereplicastate " + "where nodeId='" + server_1.getNodeId() + "' and mode='leader'", Collections.emptyList())) {
assertEquals(1, scan.consume().size());
}
server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1")), "server1", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
for (int i = 0; i < 100; i++) {
try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM systablespacereplicastate where nodeId='" + server_2.getNodeId() + "' and mode='stopped'", Collections.emptyList())) {
if (scan.consume().size() > 0) {
break;
}
}
}
try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM systablespacereplicastate where nodeId='" + server_2.getNodeId() + "' and mode='stopped'", Collections.emptyList())) {
assertEquals(1, scan.consume().size());
}
// add again server2 as follower
server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server1", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
for (int i = 0; i < 100; i++) {
try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM systablespacereplicastate where nodeId='" + server_2.getNodeId() + "' and mode='follower'", Collections.emptyList())) {
if (scan.consume().size() > 0) {
break;
}
}
}
// make server2 leader
server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server2", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_2.waitForTableSpaceBoot(TableSpace.DEFAULT, true);
server_1.waitForTableSpaceBoot(TableSpace.DEFAULT, false);
for (int i = 0; i < 100; i++) {
try (DataScanner scan = scan(server_2.getManager(), "SELECT * FROM systablespacereplicastate where nodeId='" + server_2.getNodeId() + "' and mode='leader'", Collections.emptyList())) {
if (scan.consume().size() > 0) {
break;
}
}
}
for (int i = 0; i < 100; i++) {
try (DataScanner scan = scan(server_2.getManager(), "SELECT * FROM systablespacereplicastate where nodeId='" + server_1.getNodeId() + "' and mode='follower'", Collections.emptyList())) {
if (scan.consume().size() > 0) {
break;
}
}
}
// the TableSpaceManager for a follower must not be able to perform a checkpoint
DataStorageManagerException err = TestUtils.expectThrows(DataStorageManagerException.class, () -> {
server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT).checkpoint(true, false, false);
});
// ZooKeeper BadVersionException is expected because
// server1 is not holding the expected version of znode metdata
assertThat(err.getCause(), instanceOf(KeeperException.BadVersionException.class));
}
}
}
Aggregations