use of herddb.model.NodeMetadata in project herddb by diennea.
the class ZookeeperClientSideMetadataProvider method readAsNode.
private String readAsNode(ZooKeeper zooKeeper, String tableSpace) throws IOException, InterruptedException, KeeperException {
tableSpace = tableSpace.toLowerCase();
Stat stat = new Stat();
byte[] result = zooKeeper.getData(basePath + "/nodes/" + tableSpace, false, stat);
NodeMetadata md = NodeMetadata.deserialize(result, stat.getVersion());
String leader = md.nodeId;
tableSpaceLeaders.put(tableSpace, leader);
return leader;
}
use of herddb.model.NodeMetadata 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;
}
for (int i = 0; i < MAX_TRIALS; i++) {
ZooKeeper zooKeeper = getZooKeeper();
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);
}
}
throw new ClientSideMetadataProviderException("Could not find a server info for node " + nodeId + " in time");
}
use of herddb.model.NodeMetadata in project herddb by diennea.
the class RetryOnLeaderChangedTest method testExpectedReplicaCount.
@Test
public void testExpectedReplicaCount() 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());
ServerConfiguration serverconfig_3 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server3").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
try (Server server_1 = new Server(serverconfig_1);
Server server_2 = new Server(serverconfig_2);
Server server_3 = new Server(serverconfig_3)) {
server_1.start();
server_1.waitForStandaloneBoot();
server_2.start();
server_3.start();
// wait for all of the three nodes to announce
herddb.utils.TestUtils.waitForCondition(() -> {
List<NodeMetadata> listNodes = server_3.getMetadataStorageManager().listNodes();
System.out.println("NODES: " + listNodes);
return listNodes.size() == 3;
}, herddb.utils.TestUtils.NOOP, 100);
// create the tablespace
TestUtils.execute(server_1.getManager(), "CREATE TABLESPACE 'ttt','leader:" + server_1.getNodeId() + "','expectedreplicacount:2'", Collections.emptyList());
server_2.getManager().triggerActivator(ActivatorRunRequest.FULL);
// wait for the cluster to settle to 2 replicas
herddb.utils.TestUtils.waitForCondition(() -> {
TableSpace ts = server_3.getMetadataStorageManager().describeTableSpace("ttt");
System.out.println("TS: " + ts);
assertTrue(ts.replicas.size() <= 2);
return ts.replicas.size() == 2;
}, herddb.utils.TestUtils.NOOP, 100);
}
}
use of herddb.model.NodeMetadata in project herddb by diennea.
the class SysnodesTableManager method buildVirtualRecordList.
@Override
protected Iterable<Record> buildVirtualRecordList() throws StatementExecutionException {
try {
Collection<NodeMetadata> nodes = tableSpaceManager.getMetadataStorageManager().listNodes();
List<Record> result = new ArrayList<>();
for (NodeMetadata t : nodes) {
result.add(RecordSerializer.makeRecord(table, "nodeid", t.nodeId, "address", t.host + ":" + t.port, "ssl", t.ssl ? 1 : 0));
}
return result;
} catch (MetadataStorageManagerException error) {
throw new StatementExecutionException(error);
}
}
use of herddb.model.NodeMetadata 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);
}
}
}
Aggregations