dracarys09 commented on code in PR #4581: URL: https://github.com/apache/cassandra/pull/4581#discussion_r2905062445
########## src/java/org/apache/cassandra/tools/OfflineClusterMetadataDump.java: ########## @@ -0,0 +1,584 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.tools; + +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableList; + +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.db.ColumnFamilyStore; +import org.apache.cassandra.db.ConsistencyLevel; +import org.apache.cassandra.db.Keyspace; +import org.apache.cassandra.db.SystemKeyspace; +import org.apache.cassandra.io.util.FileOutputStreamPlus; +import org.apache.cassandra.schema.DistributedMetadataLogKeyspace; +import org.apache.cassandra.schema.DistributedSchema; +import org.apache.cassandra.schema.Keyspaces; +import org.apache.cassandra.schema.Schema; +import org.apache.cassandra.schema.SchemaConstants; +import org.apache.cassandra.tcm.ClusterMetadata; +import org.apache.cassandra.tcm.ClusterMetadataService; +import org.apache.cassandra.tcm.Epoch; +import org.apache.cassandra.tcm.MetadataSnapshots; +import org.apache.cassandra.tcm.log.Entry; +import org.apache.cassandra.tcm.log.LogReader; +import org.apache.cassandra.tcm.log.LogState; +import org.apache.cassandra.tcm.log.SystemKeyspaceStorage; +import org.apache.cassandra.tcm.membership.NodeVersion; +import org.apache.cassandra.tcm.serialization.VerboseMetadataSerializer; +import org.apache.cassandra.tcm.serialization.Version; + +import picocli.CommandLine; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; + +import static com.google.common.base.Throwables.getStackTraceAsString; + +/** + * Offline tool to dump cluster metadata from local SSTables. + * <p> + * This is an emergency recovery tool for debugging when a Cassandra instance cannot + * start due to cluster metadata issues. It reads the local_metadata_log and metadata_snapshots + * tables from the system keyspace to reconstruct and display the cluster metadata state. + * <p> + * <b>NOTE: This tool is for offline use only. Do not run on a live cluster.</b> + * <p> + * Usage: + * <pre> + * # Dump cluster metadata as binary (default) + * offlineclustermetadatadump metadata --data-dir /path/to/data + * + * # Dump cluster metadata as toString output + * offlineclustermetadatadump metadata --data-dir /path/to/data --to-string + * + * # Dump local log entries + * offlineclustermetadatadump log --data-dir /path/to/data --from-epoch 1 --to-epoch 50 + * + * # Dump distributed log (CMS nodes) + * offlineclustermetadatadump distributed-log --data-dir /path/to/data + * </pre> + */ +@Command(name = "offlineclustermetadatadump", +mixinStandardHelpOptions = true, +description = "Offline tool to dump cluster metadata from local SSTables. NOTE: For offline use only.", +subcommands = { OfflineClusterMetadataDump.MetadataCommand.class, OfflineClusterMetadataDump.LogCommand.class, OfflineClusterMetadataDump.DistributedLogCommand.class }) +public class OfflineClusterMetadataDump implements Runnable +{ + private static final Output output = Output.CONSOLE; + + public static void main(String... args) + { + Util.initDatabaseDescriptor(); + + CommandLine cli = new CommandLine(OfflineClusterMetadataDump.class).setExecutionExceptionHandler((ex, cmd, parseResult) -> { + err(ex); + return 2; + }); + int status = cli.execute(args); + System.exit(status); + } + + protected static void err(Throwable e) + { + output.err.println("error: " + e.getMessage()); + output.err.println("-- StackTrace --"); + output.err.println(getStackTraceAsString(e)); + } + + @Override + public void run() + { + CommandLine.usage(this, output.out); + } + + /** + * Base class with common options and methods shared by all subcommands. + */ + @Command(mixinStandardHelpOptions = true) + static abstract class BaseCommand implements Runnable + { + @Option(names = { "-d", "--data-dir" }, description = "Data directory containing system keyspace") Review Comment: Fixed it. ``` tools/bin/offlineclustermetadatadump distributed-log \ -d data/data \ -d data/data1 \ --verbose WARN 17:42:54,420 Only 21.210GiB free across all data volumes. Consider adding more capacity to your cluster or removing obsolete snapshots Using temporary directory: /var/folders/tq/h12_qt9j6jb5xj_nydtvg5c40000gn/T/offlinedump16764704045214633091 Imported SSTables from: [data/data/system_cluster_metadata/distributed_metadata_log-d885727af67b3a32951b78efe9c75732, data/data1/system_cluster_metadata/distributed_metadata_log-d885727af67b3a32951b78efe9c75732] Entry{id=EntryId{entryId=-1}, epoch=Epoch{epoch=1}, transform=PreInitialize} Entry{id=EntryId{entryId=72058143701871096}, epoch=Epoch{epoch=2}, transform=Initialize{baseState = Epoch{epoch=1}}} ... ``` ``` tools/bin/offlineclustermetadatadump distributed-log \ -s data/data/system_cluster_metadata/distributed_metadata_log-d885727af67b3a32951b78efe9c75732/ \ -s data/data1/system_cluster_metadata/distributed_metadata_log-d885727af67b3a32951b78efe9c75732/ \ --verbose WARN 17:44:25,437 Only 21.205GiB free across all data volumes. Consider adding more capacity to your cluster or removing obsolete snapshots Using temporary directory: /var/folders/tq/h12_qt9j6jb5xj_nydtvg5c40000gn/T/offlinedump11831909208117860502 Imported SSTables from: [data/data/system_cluster_metadata/distributed_metadata_log-d885727af67b3a32951b78efe9c75732/, data/data1/system_cluster_metadata/distributed_metadata_log-d885727af67b3a32951b78efe9c75732/] Entry{id=EntryId{entryId=-1}, epoch=Epoch{epoch=1}, transform=PreInitialize} Entry{id=EntryId{entryId=72058143701871096}, epoch=Epoch{epoch=2}, transform=Initialize{baseState = Epoch{epoch=1}}} ... ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

