use of herddb.model.TableSpace in project herddb by diennea.
the class HerdDBCLI method setLeader.
private static void setLeader(ZookeeperMetadataStorageManager clusterManager, String schema, String nodeId) throws SQLException, ScriptException, MetadataStorageManagerException {
if (clusterManager == null) {
println("You are not managing a cluster. This command cannot be used");
exitCode = 1;
System.exit(exitCode);
return;
}
if (!checkNodeExistenceUsingClusterMetadataStorageManager(clusterManager, nodeId, false)) {
println("Unknown node " + nodeId);
exitCode = 1;
System.exit(exitCode);
return;
}
TableSpace tableSpace = clusterManager.describeTableSpace(schema);
if (tableSpace == null) {
println("Cannot find tablespace " + schema);
exitCode = 1;
System.exit(exitCode);
return;
}
if (!tableSpace.replicas.contains(nodeId)) {
println("Node " + nodeId + " is not in replica list: " + tableSpace.replicas);
exitCode = 1;
System.exit(exitCode);
return;
}
TableSpace.Builder newMetadata = TableSpace.builder().cloning(tableSpace).leader(nodeId);
boolean ok = clusterManager.updateTableSpace(newMetadata.build(), tableSpace);
if (!ok) {
println("Failed to alter " + schema + " tablespace");
} else {
println("Successfully altered " + schema + " tablespace");
}
}
use of herddb.model.TableSpace in project herddb by diennea.
the class HerdDBCLI method changeReplica.
private static void changeReplica(ZookeeperMetadataStorageManager clusterManager, String schema, String nodeId, ChangeReplicaAction action) throws SQLException, ScriptException, MetadataStorageManagerException {
if (clusterManager == null) {
println("Not in cluster mode!");
exitCode = 1;
System.exit(exitCode);
}
if (!checkNodeExistenceUsingClusterMetadataStorageManager(clusterManager, nodeId, true)) {
println("Unknown node " + nodeId);
exitCode = 1;
System.exit(exitCode);
}
TableSpace tableSpace = clusterManager.describeTableSpace(schema);
if (tableSpace == null) {
println("Unknown tablespace " + schema);
exitCode = 1;
System.exit(exitCode);
}
TableSpace.Builder newMetadata = TableSpace.builder().cloning(tableSpace);
switch(action) {
case ADD:
if (tableSpace.replicas.contains(nodeId)) {
println("Node " + nodeId + " is already a replica for tablespace " + schema);
exitCode = 1;
System.exit(exitCode);
}
newMetadata.replica(nodeId);
break;
case REMOVE:
if (!tableSpace.replicas.contains(nodeId)) {
println("Node " + nodeId + " is not a replica for tablespace " + schema);
exitCode = 1;
System.exit(exitCode);
}
Set<String> copy = new HashSet<>(tableSpace.replicas);
copy.remove(nodeId);
newMetadata.replicas(copy);
break;
}
boolean ok = clusterManager.updateTableSpace(newMetadata.build(), tableSpace);
if (!ok) {
println("Failed to alter " + schema + " tablespace");
} else {
println("Successfully altered " + schema + " tablespace");
}
}
use of herddb.model.TableSpace in project herddb by diennea.
the class FileMetadataStorageManager method ensureDefaultTableSpace.
@Override
public boolean ensureDefaultTableSpace(String localNodeId, String initialReplicaList, long maxLeaderInactivityTime, int expectedReplicaCount) throws MetadataStorageManagerException {
lock.writeLock().lock();
try {
TableSpace exists = tableSpaces.get(TableSpace.DEFAULT);
if (exists == null) {
TableSpace defaultTableSpace = TableSpace.builder().leader(localNodeId).replica(localNodeId).name(TableSpace.DEFAULT).build();
registerTableSpace(defaultTableSpace);
return true;
} else {
return false;
}
} catch (DDLException err) {
throw new MetadataStorageManagerException(err);
} finally {
lock.writeLock().unlock();
}
}
use of herddb.model.TableSpace in project herddb by diennea.
the class FileMetadataStorageManager method reloadFromDisk.
private void reloadFromDisk() throws MetadataStorageManagerException {
tableSpaces.clear();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(baseDirectory)) {
for (Path p : stream) {
Path filename = p.getFileName();
if (filename == null) {
continue;
}
String _filename = filename.toString();
LOGGER.log(Level.SEVERE, "reading metadata file {0}", p.toAbsolutePath().toString());
if (_filename.endsWith(".metadata")) {
TableSpace ts = readTableSpaceMetadataFile(p);
if (_filename.equals(ts.name.toLowerCase() + ".metadata")) {
tableSpaces.put(ts.name.toLowerCase(), ts);
}
}
}
} catch (IOException err) {
throw new MetadataStorageManagerException(err);
}
}
use of herddb.model.TableSpace in project herddb by diennea.
the class FileMetadataStorageManager method readTableSpaceMetadataFile.
public static TableSpace readTableSpaceMetadataFile(Path p) throws IOException, MetadataStorageManagerException {
TableSpace ts;
byte[] pageData;
try {
pageData = FileUtils.fastReadFile(p);
} catch (IOException err) {
throw new MetadataStorageManagerException(err);
}
boolean okHash = XXHash64Utils.verifyBlockWithFooter(pageData, 0, pageData.length);
if (!okHash) {
throw new MetadataStorageManagerException("corrutped data file " + p.toAbsolutePath() + ", checksum failed");
}
try (InputStream in = new SimpleByteArrayInputStream(pageData);
ExtendedDataInputStream iin = new ExtendedDataInputStream(in)) {
// version
long version = iin.readVLong();
// flags for future implementations
long flags = iin.readVLong();
if (version != 1 || flags != 0) {
throw new IOException("corrupted data file " + p.toAbsolutePath());
}
ts = TableSpace.deserialize(iin, 0, 0);
}
return ts;
}
Aggregations