use of herddb.client.HDBConnection in project herddb by diennea.
the class TableSpaceManager method downloadTableSpaceData.
private void downloadTableSpaceData() throws MetadataStorageManagerException, DataStorageManagerException, LogNotAvailableException {
TableSpace tableSpaceData = metadataStorageManager.describeTableSpace(tableSpaceName);
String leaderId = tableSpaceData.leaderId;
if (this.nodeId.equals(leaderId)) {
throw new DataStorageManagerException("cannot download data of tableSpace " + tableSpaceName + " from myself");
}
Optional<NodeMetadata> leaderAddress = metadataStorageManager.listNodes().stream().filter(n -> n.nodeId.equals(leaderId)).findAny();
if (!leaderAddress.isPresent()) {
throw new DataStorageManagerException("cannot download data of tableSpace " + tableSpaceName + " from leader " + leaderId + ", no metadata found");
}
// ensure we do not have any data on disk and in memory
actualLogSequenceNumber = LogSequenceNumber.START_OF_TIME;
newTransactionId.set(0);
LOGGER.log(Level.INFO, "tablespace " + tableSpaceName + " at downloadTableSpaceData " + tables + ", " + indexes + ", " + transactions);
for (AbstractTableManager manager : tables.values()) {
// and all indexes
if (!manager.isSystemTable()) {
manager.dropTableData();
}
manager.close();
}
tables.clear();
// this map should be empty
for (AbstractIndexManager manager : indexes.values()) {
manager.dropIndexData();
manager.close();
}
indexes.clear();
transactions.clear();
dataStorageManager.eraseTablespaceData(tableSpaceUUID);
NodeMetadata nodeData = leaderAddress.get();
ClientConfiguration clientConfiguration = new ClientConfiguration(dbmanager.getTmpDirectory());
clientConfiguration.set(ClientConfiguration.PROPERTY_CLIENT_USERNAME, dbmanager.getServerToServerUsername());
clientConfiguration.set(ClientConfiguration.PROPERTY_CLIENT_PASSWORD, dbmanager.getServerToServerPassword());
// always use network, we want to run tests with this case
clientConfiguration.set(ClientConfiguration.PROPERTY_CLIENT_CONNECT_LOCALVM_SERVER, false);
try (HDBClient client = new HDBClient(clientConfiguration)) {
client.setClientSideMetadataProvider(new ClientSideMetadataProvider() {
@Override
public String getTableSpaceLeader(String tableSpace) throws ClientSideMetadataProviderException {
return leaderId;
}
@Override
public ServerHostData getServerHostData(String nodeId) throws ClientSideMetadataProviderException {
return new ServerHostData(nodeData.host, nodeData.port, "?", nodeData.ssl, Collections.emptyMap());
}
});
try (HDBConnection con = client.openConnection()) {
ReplicaFullTableDataDumpReceiver receiver = new ReplicaFullTableDataDumpReceiver(this);
int fetchSize = 10000;
con.dumpTableSpace(tableSpaceName, receiver, fetchSize, false);
receiver.getLatch().get(1, TimeUnit.HOURS);
this.actualLogSequenceNumber = receiver.logSequenceNumber;
LOGGER.log(Level.INFO, tableSpaceName + " After download local actualLogSequenceNumber is " + actualLogSequenceNumber);
} catch (ClientSideMetadataProviderException | HDBException | InterruptedException | ExecutionException | TimeoutException internalError) {
LOGGER.log(Level.SEVERE, tableSpaceName + " error downloading snapshot", internalError);
throw new DataStorageManagerException(internalError);
}
}
}
use of herddb.client.HDBConnection in project herddb by diennea.
the class BaseBench method backupRestore.
public void backupRestore(int batchSize, ProgressListener pl) throws Exception {
if (pl == null) {
pl = new ProgressListener() {
};
}
System.out.println("BACKUP PHASE");
File backupFile = folder.newFile("test.backup");
try {
try (HDBConnection connection = client.openConnection();
FileOutputStream oo = new FileOutputStream(backupFile);
GZIPOutputStream zout = new GZIPOutputStream(oo)) {
BackupUtils.dumpTableSpace(TableSpace.DEFAULT, batchSize, connection, zout, pl);
}
System.out.println("RESTORE PHASE on a " + backupFile.length() + " bytes file");
try (HDBConnection connection = client.openConnection();
FileInputStream oo = new FileInputStream(backupFile);
GZIPInputStream zin = new GZIPInputStream(oo)) {
BackupUtils.restoreTableSpace("newschema", server.getNodeId(), connection, zin, pl);
}
} finally {
backupFile.delete();
}
}
use of herddb.client.HDBConnection in project herddb by diennea.
the class SimpleClientScanTest method test.
@Test
public void test() throws Exception {
try (Server server = new Server(newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
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);
for (int i = 0; i < 99; i++) {
Assert.assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", 0, false, true, Arrays.asList("test_" + i, 1, 2)).updateCount);
}
assertEquals(99, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
// maxRows
assertEquals(17, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable", true, Collections.emptyList(), 0, 17, 10, true).consume().size());
// empty result set
assertEquals(0, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='none'", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
// single fetch result test
assertEquals(1, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test_1'", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
// agregation in transaction, this is trickier than what you can think
long tx = connection.beginTransaction(TableSpace.DEFAULT);
assertEquals(1, connection.executeScan(TableSpace.DEFAULT, "SELECT count(*) FROM mytable WHERE id='test_1'", true, Collections.emptyList(), tx, 0, 10, true).consume().size());
connection.rollbackTransaction(TableSpace.DEFAULT, tx);
assertEquals(0, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id=?", true, Arrays.<Object>asList((Object) null), 0, 0, 10, true).consume().size());
}
}
}
use of herddb.client.HDBConnection in project herddb by diennea.
the class SimpleClientScanTest method scanMultiChunk.
@Test
public void scanMultiChunk() throws Exception {
try (Server server = new Server(newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
// more than one socket
clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 10);
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, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
for (int i = 0; i < 99; i++) {
Assert.assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", 0, false, true, Arrays.asList("test_" + i, 1, 2)).updateCount);
}
checkUseOnlyOneSocket(connection, server);
checkNoScannersOnTheServer(server);
checkCloseScannerOnConnectionClose(client, connection, server, true);
checkNoScannersOnTheServer(server);
checkCloseScannerOnConnectionClose(client, connection, server, false);
checkNoScannersOnTheServer(server);
checkCloseResultSetNotFullyScanned(connection, server, true);
checkNoScannersOnTheServer(server);
checkCloseResultSetNotFullyScanned(connection, server, false);
checkNoScannersOnTheServer(server);
checkCloseResultSetNotFullyScannedWithTransactionAndAggregation(connection, server, true);
checkNoScannersOnTheServer(server);
checkCloseResultSetNotFullyScannedWithTransactionAndAggregation(connection, server, false);
checkNoScannersOnTheServer(server);
}
checkNoScannersOnTheServer(server);
}
}
use of herddb.client.HDBConnection in project herddb by diennea.
the class JmxTest method test.
@Test
public void test() throws Exception {
try (Server server = new Server(newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
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);
{
final ObjectName statusBeanName = new ObjectName("herddb.server:type=Table,Name=" + TableSpace.DEFAULT + ".mytable");
Object attribute = ManagementFactory.getPlatformMBeanServer().getAttribute(statusBeanName, "Tablesize");
assertEquals(0L, attribute);
}
{
final ObjectName statusBeanName = new ObjectName("herddb.server:type=TableSpace,Name=" + TableSpace.DEFAULT);
Object attribute = ManagementFactory.getPlatformMBeanServer().getAttribute(statusBeanName, "Tablesize");
assertEquals(0L, attribute);
}
}
}
}
Aggregations