use of herddb.network.ServerHostData in project herddb by diennea.
the class ZookeeperClientSideMetadataProvider method getServerHostData.
@Override
public ServerHostData getServerHostData(String nodeId) throws ClientSideMetadataProviderException {
ServerHostData cached = servers.get(nodeId);
if (cached != null) {
return cached;
}
ZooKeeper zooKeeper = getZooKeeper();
try {
for (int i = 0; i < MAX_TRIALS; i++) {
try {
Stat stat = new Stat();
byte[] node = zooKeeper.getData(basePath + "/nodes/" + nodeId, null, stat);
NodeMetadata nodeMetadata = NodeMetadata.deserialize(node, stat.getVersion());
ServerHostData result = new ServerHostData(nodeMetadata.host, nodeMetadata.port, "?", nodeMetadata.ssl, new HashMap<>());
servers.put(nodeId, result);
return result;
} catch (KeeperException.NoNodeException ex) {
return null;
} catch (KeeperException.ConnectionLossException ex) {
LOG.log(Level.SEVERE, "tmp error getServerHostData for " + nodeId + ": " + ex);
try {
Thread.sleep(i * 500 + 1000);
} catch (InterruptedException exit) {
throw new ClientSideMetadataProviderException(exit);
}
} catch (KeeperException | InterruptedException | IOException ex) {
throw new ClientSideMetadataProviderException(ex);
} finally {
if (ownZooKeeper) {
try {
zooKeeper.close();
} catch (InterruptedException ex) {
throw new ClientSideMetadataProviderException(ex);
}
}
}
}
} finally {
if (ownZooKeeper) {
try {
zooKeeper.close();
} catch (InterruptedException ex) {
throw new ClientSideMetadataProviderException(ex);
}
}
}
throw new ClientSideMetadataProviderException("Could not find a server info for node " + nodeId + " in time");
}
use of herddb.network.ServerHostData 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");
}
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());
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);
long _start = System.currentTimeMillis();
boolean ok = receiver.join(1000 * 60 * 60);
if (!ok) {
throw new DataStorageManagerException("Cannot receive dump within " + (System.currentTimeMillis() - _start) + " ms");
}
if (receiver.getError() != null) {
throw new DataStorageManagerException("Error while receiving dump: " + receiver.getError(), receiver.getError());
}
this.actualLogSequenceNumber = receiver.logSequenceNumber;
LOGGER.log(Level.SEVERE, "After download local actualLogSequenceNumber is " + actualLogSequenceNumber);
} catch (ClientSideMetadataProviderException | HDBException | InterruptedException networkError) {
throw new DataStorageManagerException(networkError);
}
}
}
Aggregations