use of herddb.client.ScanResultSet in project herddb by diennea.
the class SimpleClientScanTest method checkCloseResultSetNotFullyScannedWithTransactionAndAggregation.
private void checkCloseResultSetNotFullyScannedWithTransactionAndAggregation(final HDBConnection connection, final Server server, boolean withTransaction) throws HDBException, ClientSideMetadataProviderException, InterruptedException {
long tx = withTransaction ? connection.beginTransaction(TableSpace.DEFAULT) : 0;
ServerSideConnectionPeer peerWithScanner = null;
try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT count(*) FROM mytable " + "UNION ALL " + "SELECT count(*) FROM mytable", true, Collections.emptyList(), tx, 0, 1, true)) {
assertTrue(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);
}
assertNotNull(peerWithScanner);
if (withTransaction) {
connection.rollbackTransaction(TableSpace.DEFAULT, tx);
}
}
use of herddb.client.ScanResultSet in project herddb by diennea.
the class SimpleClientScanTest method checkUseOnlyOneSocket.
private void checkUseOnlyOneSocket(final HDBConnection connection, final Server server) throws HDBException, ClientSideMetadataProviderException, InterruptedException {
// scan with fetchSize = 1, we will have 100 chunks
ServerSideConnectionPeer peerWithScanner = null;
try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable", true, Collections.emptyList(), 0, 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);
}
}
assertNotNull(peerWithScanner);
}
use of herddb.client.ScanResultSet in project herddb by diennea.
the class ClientMultiServerTest method testLeaderDetached.
@Test
public void testLeaderDetached() 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());
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).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());
}
// switch leader to server2
server_2.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "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 (tManager != null && !tManager.isLeader()) {
break;
}
Thread.sleep(100);
}
TableSpaceManager tManager = server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT);
assertTrue(tManager != null && !tManager.isLeader());
// 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);
}
});
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);
}
}
// 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());
}
}
}
}
}
Aggregations