Search in sources :

Example 1 with SSTableHeaderFix

use of org.apache.cassandra.io.sstable.SSTableHeaderFix in project cassandra by apache.

the class StandaloneScrubber method main.

public static void main(String[] args) {
    Options options = Options.parseArgs(args);
    if (Boolean.getBoolean(Util.ALLOW_TOOL_REINIT_FOR_TEST))
        // Necessary for testing
        DatabaseDescriptor.toolInitialization(false);
    else
        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-" + currentTimeMillis();
        OutputHandler handler = new OutputHandler.SystemOutput(options.verbose, options.debug);
        Directories.SSTableLister lister = cfs.getDirectories().sstableLister(Directories.OnTxnErr.THROW).skipTemporary(true);
        List<Pair<Descriptor, Set<Component>>> listResult = new ArrayList<>();
        // create snapshot
        for (Map.Entry<Descriptor, Set<Component>> entry : lister.list().entrySet()) {
            Descriptor descriptor = entry.getKey();
            Set<Component> components = entry.getValue();
            if (!components.contains(Component.DATA))
                continue;
            listResult.add(Pair.create(descriptor, components));
            File snapshotDirectory = Directories.getSnapshotDirectory(descriptor, snapshotName);
            SSTableReader.createLinks(descriptor, components, snapshotDirectory.path());
        }
        System.out.println(String.format("Pre-scrub sstables snapshotted into snapshot %s", snapshotName));
        if (options.headerFixMode != Options.HeaderFixMode.OFF) {
            // Run the frozen-UDT checks _before_ the sstables are opened
            List<String> logOutput = new ArrayList<>();
            SSTableHeaderFix.Builder headerFixBuilder = SSTableHeaderFix.builder().logToList(logOutput).schemaCallback(() -> Schema.instance::getTableMetadata);
            if (options.headerFixMode == Options.HeaderFixMode.VALIDATE)
                headerFixBuilder = headerFixBuilder.dryRun();
            for (Pair<Descriptor, Set<Component>> p : listResult) headerFixBuilder.withPath(Paths.get(p.left.filenameFor(Component.DATA)));
            SSTableHeaderFix headerFix = headerFixBuilder.build();
            try {
                headerFix.execute();
            } catch (Exception e) {
                JVMStabilityInspector.inspectThrowable(e);
                if (options.debug)
                    e.printStackTrace(System.err);
            }
            if (headerFix.hasChanges() || headerFix.hasError())
                logOutput.forEach(System.out::println);
            if (headerFix.hasError()) {
                System.err.println("Errors in serialization-header detected, aborting.");
                System.exit(1);
            }
            switch(options.headerFixMode) {
                case VALIDATE_ONLY:
                case FIX_ONLY:
                    System.out.printf("Not continuing with scrub, since '--%s %s' was specified.%n", HEADERFIX_OPTION, options.headerFixMode.asCommandLineOption());
                    System.exit(0);
                case VALIDATE:
                    if (headerFix.hasChanges()) {
                        System.err.printf("Unfixed, but fixable errors in serialization-header detected, aborting. " + "Use a non-validating mode ('-e %s' or '-e %s') for --%s%n", Options.HeaderFixMode.FIX.asCommandLineOption(), Options.HeaderFixMode.FIX_ONLY.asCommandLineOption(), HEADERFIX_OPTION);
                        System.exit(2);
                    }
                    break;
                case FIX:
                    break;
            }
        }
        List<SSTableReader> sstables = new ArrayList<>();
        // Open sstables
        for (Pair<Descriptor, Set<Component>> pair : listResult) {
            Descriptor descriptor = pair.left;
            Set<Component> components = pair.right;
            if (!components.contains(Component.DATA))
                continue;
            try {
                SSTableReader sstable = SSTableReader.openNoValidation(descriptor, components, cfs);
                sstables.add(sstable);
            } catch (Exception e) {
                JVMStabilityInspector.inspectThrowable(e);
                System.err.println(String.format("Error Loading %s: %s", descriptor, e.getMessage()));
                if (options.debug)
                    e.printStackTrace(System.err);
            }
        }
        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, options.reinserOverflowedTTL)) {
                        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) Set(java.util.Set) ArrayList(java.util.ArrayList) Directories(org.apache.cassandra.db.Directories) SSTableHeaderFix(org.apache.cassandra.io.sstable.SSTableHeaderFix) Scrubber(org.apache.cassandra.db.compaction.Scrubber) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) OutputHandler(org.apache.cassandra.utils.OutputHandler) Component(org.apache.cassandra.io.sstable.Component) Pair(org.apache.cassandra.utils.Pair) LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) ParseException(org.apache.commons.cli.ParseException) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Descriptor(org.apache.cassandra.io.sstable.Descriptor) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) Map(java.util.Map) File(org.apache.cassandra.io.util.File)

Aggregations

ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Set (java.util.Set)1 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)1 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)1 Directories (org.apache.cassandra.db.Directories)1 Keyspace (org.apache.cassandra.db.Keyspace)1 Scrubber (org.apache.cassandra.db.compaction.Scrubber)1 LifecycleTransaction (org.apache.cassandra.db.lifecycle.LifecycleTransaction)1 Component (org.apache.cassandra.io.sstable.Component)1 Descriptor (org.apache.cassandra.io.sstable.Descriptor)1 SSTableHeaderFix (org.apache.cassandra.io.sstable.SSTableHeaderFix)1 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)1 File (org.apache.cassandra.io.util.File)1 CmdLineOptions (org.apache.cassandra.tools.BulkLoader.CmdLineOptions)1 OutputHandler (org.apache.cassandra.utils.OutputHandler)1 Pair (org.apache.cassandra.utils.Pair)1 ParseException (org.apache.commons.cli.ParseException)1