Search in sources :

Example 11 with TableSpace

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");
    }
}
Also used : TableSpace(herddb.model.TableSpace)

Example 12 with 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");
    }
}
Also used : TableSpace(herddb.model.TableSpace) HashSet(java.util.HashSet)

Example 13 with 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();
    }
}
Also used : MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) TableSpace(herddb.model.TableSpace) DDLException(herddb.model.DDLException)

Example 14 with TableSpace

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);
    }
}
Also used : Path(java.nio.file.Path) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) TableSpace(herddb.model.TableSpace) IOException(java.io.IOException)

Example 15 with TableSpace

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;
}
Also used : MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) TableSpace(herddb.model.TableSpace) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) IOException(java.io.IOException)

Aggregations

TableSpace (herddb.model.TableSpace)37 MetadataStorageManagerException (herddb.metadata.MetadataStorageManagerException)22 StatementExecutionException (herddb.model.StatementExecutionException)15 DDLException (herddb.model.DDLException)13 IOException (java.io.IOException)13 DataScanner (herddb.model.DataScanner)11 DataScannerException (herddb.model.DataScannerException)11 ArrayList (java.util.ArrayList)11 TableSpaceDoesNotExistException (herddb.model.TableSpaceDoesNotExistException)10 LogNotAvailableException (herddb.log.LogNotAvailableException)9 DataStorageManagerException (herddb.storage.DataStorageManagerException)9 ExecutionException (java.util.concurrent.ExecutionException)9 DDLStatementExecutionResult (herddb.model.DDLStatementExecutionResult)8 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)7 Test (org.junit.Test)6 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)5 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)5 DataAccessor (herddb.utils.DataAccessor)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)4