in src/java/org/apache/cassandra/tools/StandaloneScrubber.java [85:206]
public static void main(String args[])
{
Options options = Options.parseArgs(args);
if (TEST_UTIL_ALLOW_TOOL_REINIT_FOR_TEST.getBoolean())
DatabaseDescriptor.toolInitialization(false); //Necessary for testing
else
Util.initDatabaseDescriptor();
ClusterMetadataService.initializeForTools(false);
try
{
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(Components.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));
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(Components.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))
{
txn.obsoleteOriginals(); // make sure originals are deleted and avoid NPE if index is missing, CASSANDRA-9591
SSTableFormat format = sstable.descriptor.getFormat();
try (IScrubber scrubber = format.getScrubber(cfs, txn, handler, options.build()))
{
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();
System.exit(0); // We need that to stop non daemonized threads
}
catch (Exception e)
{
System.err.println(e.getMessage());
if (options.debug)
e.printStackTrace(System.err);
System.exit(1);
}
}