Search in sources :

Example 66 with Keyspace

use of org.apache.cassandra.db.Keyspace in project cassandra by apache.

the class StandaloneScrubber method main.

public static void main(String[] args) {
    Options options = Options.parseArgs(args);
    Util.initDatabaseDescriptor();
    try {
        // load keyspace descriptions.
        Schema.instance.loadFromDisk(false);
        if (Schema.instance.getKeyspaceMetadata(options.keyspaceName) == null)
            throw new IllegalArgumentException(String.format("Unknown keyspace %s", options.keyspaceName));
        // Do not load sstables since they might be broken
        Keyspace keyspace = Keyspace.openWithoutSSTables(options.keyspaceName);
        ColumnFamilyStore cfs = null;
        for (ColumnFamilyStore c : keyspace.getValidColumnFamilies(true, false, options.cfName)) {
            if (c.name.equals(options.cfName)) {
                cfs = c;
                break;
            }
        }
        if (cfs == null)
            throw new IllegalArgumentException(String.format("Unknown table %s.%s", options.keyspaceName, options.cfName));
        String snapshotName = "pre-scrub-" + System.currentTimeMillis();
        OutputHandler handler = new OutputHandler.SystemOutput(options.verbose, options.debug);
        Directories.SSTableLister lister = cfs.getDirectories().sstableLister(Directories.OnTxnErr.THROW).skipTemporary(true);
        List<SSTableReader> sstables = new ArrayList<>();
        // Scrub sstables
        for (Map.Entry<Descriptor, Set<Component>> entry : lister.list().entrySet()) {
            Set<Component> components = entry.getValue();
            if (!components.contains(Component.DATA))
                continue;
            try {
                SSTableReader sstable = SSTableReader.openNoValidation(entry.getKey(), components, cfs);
                sstables.add(sstable);
                File snapshotDirectory = Directories.getSnapshotDirectory(sstable.descriptor, snapshotName);
                sstable.createLinks(snapshotDirectory.getPath());
            } catch (Exception e) {
                JVMStabilityInspector.inspectThrowable(e);
                System.err.println(String.format("Error Loading %s: %s", entry.getKey(), e.getMessage()));
                if (options.debug)
                    e.printStackTrace(System.err);
            }
        }
        System.out.println(String.format("Pre-scrub sstables snapshotted into snapshot %s", snapshotName));
        if (!options.manifestCheckOnly) {
            for (SSTableReader sstable : sstables) {
                try (LifecycleTransaction txn = LifecycleTransaction.offline(OperationType.SCRUB, sstable)) {
                    // make sure originals are deleted and avoid NPE if index is missing, CASSANDRA-9591
                    txn.obsoleteOriginals();
                    try (Scrubber scrubber = new Scrubber(cfs, txn, options.skipCorrupted, handler, !options.noValidate)) {
                        scrubber.scrub();
                    } catch (Throwable t) {
                        if (!cfs.rebuildOnFailedScrub(t)) {
                            System.out.println(t.getMessage());
                            throw t;
                        }
                    }
                } catch (Exception e) {
                    System.err.println(String.format("Error scrubbing %s: %s", sstable, e.getMessage()));
                    e.printStackTrace(System.err);
                }
            }
        }
        // Check (and repair) manifests
        checkManifest(cfs.getCompactionStrategyManager(), cfs, sstables);
        CompactionManager.instance.finishCompactionsAndShutdown(5, TimeUnit.MINUTES);
        LifecycleTransaction.waitForDeletions();
        // We need that to stop non daemonized threads
        System.exit(0);
    } catch (Exception e) {
        System.err.println(e.getMessage());
        if (options.debug)
            e.printStackTrace(System.err);
        System.exit(1);
    }
}
Also used : CmdLineOptions(org.apache.cassandra.tools.BulkLoader.CmdLineOptions) LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) Directories(org.apache.cassandra.db.Directories) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) OutputHandler(org.apache.cassandra.utils.OutputHandler) File(java.io.File)

Example 67 with Keyspace

use of org.apache.cassandra.db.Keyspace in project cassandra by apache.

the class StandaloneUpgrader method main.

public static void main(String[] args) {
    Options options = Options.parseArgs(args);
    Util.initDatabaseDescriptor();
    try {
        // load keyspace descriptions.
        Schema.instance.loadFromDisk(false);
        if (Schema.instance.getTableMetadataRef(options.keyspace, options.cf) == null)
            throw new IllegalArgumentException(String.format("Unknown keyspace/table %s.%s", options.keyspace, options.cf));
        Keyspace keyspace = Keyspace.openWithoutSSTables(options.keyspace);
        ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(options.cf);
        OutputHandler handler = new OutputHandler.SystemOutput(false, options.debug);
        Directories.SSTableLister lister = cfs.getDirectories().sstableLister(Directories.OnTxnErr.THROW);
        if (options.snapshot != null)
            lister.onlyBackups(true).snapshots(options.snapshot);
        else
            lister.includeBackups(false);
        Collection<SSTableReader> readers = new ArrayList<>();
        // Upgrade sstables
        for (Map.Entry<Descriptor, Set<Component>> entry : lister.list().entrySet()) {
            Set<Component> components = entry.getValue();
            if (!components.contains(Component.DATA) || !components.contains(Component.PRIMARY_INDEX))
                continue;
            try {
                SSTableReader sstable = SSTableReader.openNoValidation(entry.getKey(), components, cfs);
                if (sstable.descriptor.version.equals(SSTableFormat.Type.current().info.getLatestVersion())) {
                    sstable.selfRef().release();
                    continue;
                }
                readers.add(sstable);
            } catch (Exception e) {
                JVMStabilityInspector.inspectThrowable(e);
                System.err.println(String.format("Error Loading %s: %s", entry.getKey(), e.getMessage()));
                if (options.debug)
                    e.printStackTrace(System.err);
            }
        }
        int numSSTables = readers.size();
        handler.output("Found " + numSSTables + " sstables that need upgrading.");
        for (SSTableReader sstable : readers) {
            try (LifecycleTransaction txn = LifecycleTransaction.offline(OperationType.UPGRADE_SSTABLES, sstable)) {
                Upgrader upgrader = new Upgrader(cfs, txn, handler);
                upgrader.upgrade(options.keepSource);
            } catch (Exception e) {
                System.err.println(String.format("Error upgrading %s: %s", sstable, e.getMessage()));
                if (options.debug)
                    e.printStackTrace(System.err);
            } finally {
                // we should have released this through commit of the LifecycleTransaction,
                // but in case the upgrade failed (or something else went wrong) make sure we don't retain a reference
                sstable.selfRef().ensureReleased();
            }
        }
        CompactionManager.instance.finishCompactionsAndShutdown(5, TimeUnit.MINUTES);
        LifecycleTransaction.waitForDeletions();
        System.exit(0);
    } catch (Exception e) {
        System.err.println(e.getMessage());
        if (options.debug)
            e.printStackTrace(System.err);
        System.exit(1);
    }
}
Also used : CmdLineOptions(org.apache.cassandra.tools.BulkLoader.CmdLineOptions) Upgrader(org.apache.cassandra.db.compaction.Upgrader) LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) Directories(org.apache.cassandra.db.Directories) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) OutputHandler(org.apache.cassandra.utils.OutputHandler)

Example 68 with Keyspace

use of org.apache.cassandra.db.Keyspace in project cassandra by apache.

the class StandaloneVerifier method main.

public static void main(String[] args) {
    Options options = Options.parseArgs(args);
    Util.initDatabaseDescriptor();
    try {
        // load keyspace descriptions.
        Schema.instance.loadFromDisk(false);
        boolean hasFailed = false;
        if (Schema.instance.getTableMetadataRef(options.keyspaceName, options.cfName) == null)
            throw new IllegalArgumentException(String.format("Unknown keyspace/table %s.%s", options.keyspaceName, options.cfName));
        // Do not load sstables since they might be broken
        Keyspace keyspace = Keyspace.openWithoutSSTables(options.keyspaceName);
        ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(options.cfName);
        OutputHandler handler = new OutputHandler.SystemOutput(options.verbose, options.debug);
        Directories.SSTableLister lister = cfs.getDirectories().sstableLister(Directories.OnTxnErr.THROW).skipTemporary(true);
        boolean extended = options.extended;
        List<SSTableReader> sstables = new ArrayList<>();
        // Verify sstables
        for (Map.Entry<Descriptor, Set<Component>> entry : lister.list().entrySet()) {
            Set<Component> components = entry.getValue();
            if (!components.contains(Component.DATA) || !components.contains(Component.PRIMARY_INDEX))
                continue;
            try {
                SSTableReader sstable = SSTableReader.openNoValidation(entry.getKey(), components, cfs);
                sstables.add(sstable);
            } catch (Exception e) {
                JVMStabilityInspector.inspectThrowable(e);
                System.err.println(String.format("Error Loading %s: %s", entry.getKey(), e.getMessage()));
                if (options.debug)
                    e.printStackTrace(System.err);
            }
        }
        for (SSTableReader sstable : sstables) {
            try {
                try (Verifier verifier = new Verifier(cfs, sstable, handler, true)) {
                    verifier.verify(extended);
                } catch (CorruptSSTableException cs) {
                    System.err.println(String.format("Error verifying %s: %s", sstable, cs.getMessage()));
                    hasFailed = true;
                }
            } catch (Exception e) {
                System.err.println(String.format("Error verifying %s: %s", sstable, e.getMessage()));
                e.printStackTrace(System.err);
            }
        }
        CompactionManager.instance.finishCompactionsAndShutdown(5, TimeUnit.MINUTES);
        // We need that to stop non daemonized threads
        System.exit(hasFailed ? 1 : 0);
    } catch (Exception e) {
        System.err.println(e.getMessage());
        if (options.debug)
            e.printStackTrace(System.err);
        System.exit(1);
    }
}
Also used : CmdLineOptions(org.apache.cassandra.tools.BulkLoader.CmdLineOptions) CorruptSSTableException(org.apache.cassandra.io.sstable.CorruptSSTableException) CorruptSSTableException(org.apache.cassandra.io.sstable.CorruptSSTableException) Directories(org.apache.cassandra.db.Directories) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Descriptor(org.apache.cassandra.io.sstable.Descriptor) OutputHandler(org.apache.cassandra.utils.OutputHandler) Component(org.apache.cassandra.io.sstable.Component)

Example 69 with Keyspace

use of org.apache.cassandra.db.Keyspace in project cassandra by apache.

the class Schema method reload.

private void reload(KeyspaceMetadata previous, KeyspaceMetadata updated) {
    Keyspace keyspace = getKeyspaceInstance(updated.name);
    if (keyspace != null)
        keyspace.setMetadata(updated);
    MapDifference<TableId, TableMetadata> tablesDiff = previous.tables.diff(updated.tables);
    MapDifference<TableId, ViewMetadata> viewsDiff = previous.views.diff(updated.views);
    MapDifference<String, TableMetadata> indexesDiff = previous.tables.indexesDiff(updated.tables);
    // clean up after removed entries
    tablesDiff.entriesOnlyOnLeft().values().forEach(table -> metadataRefs.remove(table.id));
    viewsDiff.entriesOnlyOnLeft().values().forEach(view -> metadataRefs.remove(view.metadata.id));
    indexesDiff.entriesOnlyOnLeft().values().forEach(indexTable -> indexMetadataRefs.remove(Pair.create(indexTable.keyspace, indexTable.indexName().get())));
    // load up new entries
    tablesDiff.entriesOnlyOnRight().values().forEach(table -> metadataRefs.put(table.id, new TableMetadataRef(table)));
    viewsDiff.entriesOnlyOnRight().values().forEach(view -> metadataRefs.put(view.metadata.id, new TableMetadataRef(view.metadata)));
    indexesDiff.entriesOnlyOnRight().values().forEach(indexTable -> indexMetadataRefs.put(Pair.create(indexTable.keyspace, indexTable.indexName().get()), new TableMetadataRef(indexTable)));
    // refresh refs to updated ones
    tablesDiff.entriesDiffering().values().forEach(diff -> metadataRefs.get(diff.rightValue().id).set(diff.rightValue()));
    viewsDiff.entriesDiffering().values().forEach(diff -> metadataRefs.get(diff.rightValue().metadata.id).set(diff.rightValue().metadata));
    indexesDiff.entriesDiffering().values().stream().map(MapDifference.ValueDifference::rightValue).forEach(indexTable -> indexMetadataRefs.get(Pair.create(indexTable.keyspace, indexTable.indexName().get())).set(indexTable));
}
Also used : MapDifference(com.google.common.collect.MapDifference) SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) Keyspace(org.apache.cassandra.db.Keyspace)

Aggregations

Keyspace (org.apache.cassandra.db.Keyspace)69 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)63 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)48 Test (org.junit.Test)42 LifecycleTransaction (org.apache.cassandra.db.lifecycle.LifecycleTransaction)23 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)17 CompactionController (org.apache.cassandra.db.compaction.CompactionController)13 CompactionIterator (org.apache.cassandra.db.compaction.CompactionIterator)12 DecoratedKey (org.apache.cassandra.db.DecoratedKey)11 ByteBuffer (java.nio.ByteBuffer)10 File (java.io.File)5 Directories (org.apache.cassandra.db.Directories)5 SystemKeyspace (org.apache.cassandra.db.SystemKeyspace)5 RestorableMeter (org.apache.cassandra.metrics.RestorableMeter)5 ArrayList (java.util.ArrayList)4 AbstractCompactionStrategy (org.apache.cassandra.db.compaction.AbstractCompactionStrategy)4 Range (org.apache.cassandra.dht.Range)4 Token (org.apache.cassandra.dht.Token)4 Descriptor (org.apache.cassandra.io.sstable.Descriptor)4 PrintStream (java.io.PrintStream)3