unique_ptr BuildLocalReplicaMode()

in src/kudu/tools/tool_action_local_replica.cc [1359:1526]


unique_ptr<Mode> BuildLocalReplicaMode() {
  unique_ptr<Action> print_replica_uuids =
      ActionBuilder("print_replica_uuids", &PrintReplicaUuids)
      .Description("Print all tablet replica peer UUIDs found in a "
                   "tablet's Raft configuration")
      .AddRequiredParameter({ kTabletIdsCsvArg, kTabletIdsCsvArgDesc })
      .AddOptionalParameter("fs_data_dirs")
      .AddOptionalParameter("fs_metadata_dir")
      .AddOptionalParameter("fs_wal_dir")
      .Build();

  unique_ptr<Action> rewrite_raft_config =
      ActionBuilder("rewrite_raft_config", &RewriteRaftConfig)
      .Description("Rewrite a tablet replica's Raft configuration")
      .AddRequiredParameter({ kTabletIdsCsvArg, kTabletIdsCsvArgDesc })
      .AddRequiredVariadicParameter({ kRaftPeersArg, kRaftPeersArgDesc })
      .AddOptionalParameter("fs_data_dirs")
      .AddOptionalParameter("fs_metadata_dir")
      .AddOptionalParameter("fs_wal_dir")
      .Build();

  unique_ptr<Action> set_term =
      ActionBuilder("set_term", &SetRaftTerm)
      .Description("Bump the current term stored in consensus metadata")
      .AddRequiredParameter({ kTabletIdArg, kTabletIdArgDesc })
      .AddRequiredParameter({ kTermArg,
                              "the new raft term (must be greater "
                              "than the current term)" })
      .AddOptionalParameter("fs_data_dirs")
      .AddOptionalParameter("fs_metadata_dir")
      .AddOptionalParameter("fs_wal_dir")
      .Build();

  unique_ptr<Action> delete_rowsets =
      ActionBuilder("delete_rowsets", &DeleteRowsets)
          .Description("Delete rowsets from a local replica.")
          .ExtraDescription("The common usage pattern of this tool is described below.\n"
              "That involves checking the result by a dry run of the tablet server with the "
              "modified tablet's data after running the tool. It's crucial to customize tablet "
              "server's --enable_tablet_orphaned_block_deletion flag for the dry run to avoid "
              "deleting orphaned blocks, so it's possible to roll back to the original state of "
              "the tablet's data if something goes wrong. First, run the tool with default "
              "settings for  --backup_metadata and --enable_tablet_orphaned_block_deletion to (a) "
              "create a backup of the original metadata file and (b) keep the orphaned blocks on "
              "the file system. Second, start the tablet server with "
              "--enable_tablet_orphaned_block_deletion=false to check whether the change worked as "
              "expected and the tablet server works fine with the new state of the tablet's data. "
              "If it doesn't work as expected, stop the tablet server (if still running), rollback "
              "the change by replacing the updated metadata file with the backup created earlier, "
              "and retry the  procedure again, specifying proper rowset identifiers to the tool. "
              "If the change works as expected and the tablet server runs fine after with the "
              "updated tablet's data, remove the customization for the "
              "--enable_tablet_orphaned_block_deletion flag and restart the tablet server.")
          .AddRequiredParameter({ kTabletIdArg, kTabletIdArgDesc })
          .AddRequiredParameter({ kRowsetIdsCsvArg, kRowsetIdsCsvArgDesc })
          .AddOptionalParameter("backup_metadata")
          // Set --enable_tablet_orphaned_block_deletion to false to promote a safer usage
          // of this tools.
          .AddOptionalParameter("enable_tablet_orphaned_block_deletion", string("false"))
          .AddOptionalParameter("fs_data_dirs")
          .AddOptionalParameter("fs_metadata_dir")
          .AddOptionalParameter("fs_wal_dir")
          .Build();

  unique_ptr<Action> unsafe_recreate =
      ActionBuilder("unsafe_recreate", &UnsafeRecreateCmeta)
      .Description("Rewrite the consensus metadata based on the provided arguments")
      .AddRequiredParameter({kTabletIdArg, kTabletIdArgDesc})
      .AddRequiredParameter({"term", "Raft term"})
      .AddRequiredParameter({"index", "OpId index"})
      .AddRequiredVariadicParameter({kRaftPeersArg, kRaftPeersArgDesc})
      .AddOptionalParameter("fs_data_dirs")
      .AddOptionalParameter("fs_metadata_dir")
      .AddOptionalParameter("fs_wal_dir")
      .AddOptionalParameter("force")
      .Build();

  unique_ptr<Mode> cmeta =
      ModeBuilder("cmeta")
      .Description("Operate on a local tablet replica's consensus "
                   "metadata file")
      .AddAction(std::move(print_replica_uuids))
      .AddAction(std::move(rewrite_raft_config))
      .AddAction(std::move(set_term))
      .AddAction(std::move(unsafe_recreate))
      .Build();

  unique_ptr<Mode> tmeta =
      ModeBuilder("tmeta")
      .Description("Edit a local tablet metadata")
      .AddAction(std::move(delete_rowsets))
      .Build();

  unique_ptr<Action> copy_from_remote =
      ActionBuilder("copy_from_remote", &CopyFromRemote)
      .Description("Copy tablet replicas from a remote server")
      .AddRequiredParameter({ kTabletIdsCsvArg, kTabletIdsCsvArgDesc })
      .AddRequiredParameter({ "source", "Source RPC address of "
                              "form hostname:port" })
      .AddOptionalParameter("fs_data_dirs")
      .AddOptionalParameter("fs_metadata_dir")
      .AddOptionalParameter("fs_wal_dir")
      .AddOptionalParameter("tablet_copy_download_threads_nums_per_session")
      .AddOptionalParameter("num_threads")
      .AddOptionalParameter("tablet_copy_support_download_superblock_in_batch")
      .AddOptionalParameter("tablet_copy_throttler_bytes_per_sec")
      .AddOptionalParameter("tablet_copy_throttler_burst_factor")
      .Build();

  unique_ptr<Action> copy_from_local =
      ActionBuilder("copy_from_local", &CopyFromLocal)
      .Description("Copy tablet replicas from local filesystem. Before using this tool, you "
          "MUST stop the master/tserver you want to copy from, and make sure --src_*_dir(s) and "
          "--dst_*_dir(s) are exactly what whey should be.")
      .AddRequiredParameter({ kTabletIdsCsvArg, kTabletIdsCsvArgDesc })
      .AddOptionalParameter("src_fs_wal_dir")
      .AddOptionalParameter("src_fs_metadata_dir")
      .AddOptionalParameter("src_fs_data_dirs")
      .AddOptionalParameter("dst_fs_wal_dir")
      .AddOptionalParameter("dst_fs_metadata_dir")
      .AddOptionalParameter("dst_fs_data_dirs")
      .AddOptionalParameter("num_threads")
      .Build();

  unique_ptr<Action> list =
      ActionBuilder("list", &ListLocalReplicas)
      .Description("Show list of tablet replicas in the local filesystem")
      .AddOptionalParameter("fs_data_dirs")
      .AddOptionalParameter("fs_metadata_dir")
      .AddOptionalParameter("fs_wal_dir")
      .AddOptionalParameter("list_detail")
      .Build();

  unique_ptr<Action> delete_local_replica =
      ActionBuilder("delete", &DeleteLocalReplicas)
      .Description("Delete tablet replicas from the local filesystem. "
          "By default, leaves a tombstone record upon replica removal.")
      .AddRequiredParameter({ kTabletIdsGlobArg, kTabletIdsGlobArgDesc })
      .AddOptionalParameter("fs_data_dirs")
      .AddOptionalParameter("fs_metadata_dir")
      .AddOptionalParameter("fs_wal_dir")
      .AddOptionalParameter("clean_unsafe")
      .AddOptionalParameter("ignore_nonexistent")
      .AddOptionalParameter("tables")
      .Build();

  unique_ptr<Action> data_size =
      ActionBuilder("data_size", &SummarizeDataSize)
      .Description("Summarize the data size/space usage of the given local replica(s).")
      .AddRequiredParameter({ kTabletIdGlobArg, kTabletIdGlobArgDesc })
      .AddOptionalParameter("fs_data_dirs")
      .AddOptionalParameter("fs_metadata_dir")
      .AddOptionalParameter("fs_wal_dir")
      .AddOptionalParameter("format")
      .Build();

  return ModeBuilder("local_replica")
      .Description("Operate on local tablet replicas via the local filesystem")
      .AddMode(std::move(cmeta))
      .AddMode(std::move(tmeta))
      .AddAction(std::move(copy_from_local))
      .AddAction(std::move(copy_from_remote))
      .AddAction(std::move(data_size))
      .AddAction(std::move(delete_local_replica))
      .AddAction(std::move(list))
      .AddMode(BuildDumpMode())
      .Build();
}