|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.bigtable.beam.hbasesnapshots; |
| 17 | + |
| 18 | +import com.google.api.core.InternalExtensionOnly; |
| 19 | +import com.google.cloud.bigtable.beam.hbasesnapshots.conf.ImportConfig; |
| 20 | +import com.google.cloud.bigtable.beam.hbasesnapshots.conf.SnapshotConfig; |
| 21 | +import com.google.common.annotations.VisibleForTesting; |
| 22 | +import com.google.common.base.Preconditions; |
| 23 | +import com.google.gson.Gson; |
| 24 | +import com.google.gson.GsonBuilder; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import org.apache.beam.sdk.extensions.gcp.options.GcsOptions; |
| 29 | +import org.apache.beam.sdk.options.PipelineOptionsFactory; |
| 30 | +import org.apache.commons.logging.Log; |
| 31 | +import org.apache.commons.logging.LogFactory; |
| 32 | +import org.apache.hadoop.conf.Configuration; |
| 33 | +import org.apache.hadoop.fs.FileSystem; |
| 34 | +import org.apache.hadoop.fs.Path; |
| 35 | +import org.apache.hadoop.hbase.snapshot.RestoreSnapshotHelper; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tool to restore HBase snapshots in GCS for scanning. This tool runs locally (without Dataflow) |
| 39 | + * and copies snapshot files to a restore path, resolving HLinks and References so that they can be |
| 40 | + * read by a scanner. |
| 41 | + * |
| 42 | + * <p>Execute the following command to run the tool directly using system properties: |
| 43 | + * |
| 44 | + * <pre> |
| 45 | + * {@code mvn compile exec:java \ |
| 46 | + * -Dexec.mainClass=com.google.cloud.bigtable.beam.hbasesnapshots.HBaseSnapshotRestoreTool \ |
| 47 | + * -Dproject=[PROJECT_ID] \ |
| 48 | + * -DhbaseSnapshotSourceDir=gs://[BUCKET]/[HBASE_EXPORT_ROOT_PATH]/data \ |
| 49 | + * -Dsnapshots=[SNAPSHOT_NAMES] \ |
| 50 | + * -DrestorePath=gs://[BUCKET]/[HBASE_EXPORT_ROOT_PATH]/restore |
| 51 | + * } |
| 52 | + * </pre> |
| 53 | + * |
| 54 | + * <p>Alternatively, you can provide a path to a JSON configuration file: |
| 55 | + * |
| 56 | + * <pre> |
| 57 | + * {@code mvn compile exec:java \ |
| 58 | + * -Dexec.mainClass=com.google.cloud.bigtable.beam.hbasesnapshots.HBaseSnapshotRestoreTool \ |
| 59 | + * -Dproject=[PROJECT_ID] \ |
| 60 | + * -DimportConfigFilePath=[PATH_TO_JSON_CONFIG] |
| 61 | + * } |
| 62 | + * </pre> |
| 63 | + * |
| 64 | + * <p>The JSON configuration file should have the following format: |
| 65 | + * |
| 66 | + * <pre> |
| 67 | + * { |
| 68 | + * "sourcepath": "gs://[BUCKET]/[HBASE_EXPORT_ROOT_PATH]/data", |
| 69 | + * "restorepath": "gs://[BUCKET]/[HBASE_EXPORT_ROOT_PATH]/restore", |
| 70 | + * "snapshots": { |
| 71 | + * "snapshot1": "table1", |
| 72 | + * "snapshot2": "table2" |
| 73 | + * } |
| 74 | + * } |
| 75 | + * </pre> |
| 76 | + */ |
| 77 | +@InternalExtensionOnly |
| 78 | +public class HBaseSnapshotRestoreTool { |
| 79 | + private static final Log LOG = LogFactory.getLog(HBaseSnapshotRestoreTool.class); |
| 80 | + |
| 81 | + private static final String PROJECT_PROPERTY = "project"; |
| 82 | + private static final String IMPORT_CONFIG_FILE_PATH_PROPERTY = "importConfigFilePath"; |
| 83 | + private static final String HBASE_SNAPSHOT_SOURCE_DIR_PROPERTY = "hbaseSnapshotSourceDir"; |
| 84 | + private static final String SNAPSHOTS_PROPERTY = "snapshots"; |
| 85 | + private static final String RESTORE_PATH_PROPERTY = "restorePath"; |
| 86 | + |
| 87 | + public static void main(String[] args) throws Exception { |
| 88 | + GcsOptions options = PipelineOptionsFactory.create().as(GcsOptions.class); |
| 89 | + String project = System.getProperty(PROJECT_PROPERTY); |
| 90 | + if (project != null) { |
| 91 | + options.setProject(project); |
| 92 | + } |
| 93 | + |
| 94 | + ImportConfig importConfig = |
| 95 | + System.getProperty(IMPORT_CONFIG_FILE_PATH_PROPERTY) != null |
| 96 | + ? buildImportConfigFromConfigFile(System.getProperty(IMPORT_CONFIG_FILE_PATH_PROPERTY)) |
| 97 | + : buildImportConfigFromArgs(options); |
| 98 | + |
| 99 | + LOG.info( |
| 100 | + String.format( |
| 101 | + "SourcePath:%s, RestorePath:%s", |
| 102 | + importConfig.getSourcepath(), importConfig.getRestorepath())); |
| 103 | + |
| 104 | + Map<String, String> configurations = |
| 105 | + SnapshotUtils.getConfiguration( |
| 106 | + null, // invoke from a DirectRunner without using dataflow |
| 107 | + options.getProject(), |
| 108 | + importConfig.getSourcepath(), |
| 109 | + importConfig.getHbaseConfiguration()); |
| 110 | + |
| 111 | + List<SnapshotConfig> snapshotConfigs = |
| 112 | + SnapshotUtils.buildSnapshotConfigs( |
| 113 | + importConfig.getSnapshots(), |
| 114 | + configurations, |
| 115 | + options.getProject(), |
| 116 | + importConfig.getSourcepath(), |
| 117 | + importConfig.getRestorepath()); |
| 118 | + |
| 119 | + for (SnapshotConfig config : snapshotConfigs) { |
| 120 | + restoreSnapshot(config); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @VisibleForTesting |
| 125 | + static ImportConfig buildImportConfigFromArgs(GcsOptions gcsOptions) throws IOException { |
| 126 | + String sourceDir = System.getProperty(HBASE_SNAPSHOT_SOURCE_DIR_PROPERTY); |
| 127 | + String snapshotsProperty = System.getProperty(SNAPSHOTS_PROPERTY); |
| 128 | + Map<String, String> snapshots = null; |
| 129 | + if (snapshotsProperty != null) { |
| 130 | + snapshots = |
| 131 | + (sourceDir != null && SnapshotUtils.isRegex(snapshotsProperty)) |
| 132 | + ? SnapshotUtils.getSnapshotsFromSnapshotPath( |
| 133 | + sourceDir, gcsOptions.getGcsUtil(), snapshotsProperty) |
| 134 | + : SnapshotUtils.getSnapshotsFromString(snapshotsProperty); |
| 135 | + } |
| 136 | + |
| 137 | + ImportConfig importConfig = new ImportConfig(); |
| 138 | + importConfig.setSourcepath(sourceDir); |
| 139 | + if (snapshots != null) { |
| 140 | + importConfig.setSnapshotsFromMap(snapshots); |
| 141 | + } |
| 142 | + importConfig.validate(); |
| 143 | + SnapshotUtils.setRestorePath(System.getProperty(RESTORE_PATH_PROPERTY), importConfig); |
| 144 | + |
| 145 | + return importConfig; |
| 146 | + } |
| 147 | + |
| 148 | + @VisibleForTesting |
| 149 | + static ImportConfig buildImportConfigFromConfigFile(String configFilePath) throws Exception { |
| 150 | + Gson gson = new GsonBuilder().create(); |
| 151 | + ImportConfig importConfig = |
| 152 | + gson.fromJson(SnapshotUtils.readFileContents(configFilePath), ImportConfig.class); |
| 153 | + Preconditions.checkNotNull(importConfig, "ImportConfig parsed from file cannot be null."); |
| 154 | + importConfig.validate(); |
| 155 | + SnapshotUtils.setRestorePath(importConfig.getRestorepath(), importConfig); |
| 156 | + return importConfig; |
| 157 | + } |
| 158 | + |
| 159 | + @VisibleForTesting |
| 160 | + /** |
| 161 | + * Creates a copy of Snasphsot from the source path into restore path. |
| 162 | + * |
| 163 | + * @param snapshotConfig - Snapshot Configuration |
| 164 | + * @throws IOException |
| 165 | + */ |
| 166 | + static void restoreSnapshot(SnapshotConfig snapshotConfig) throws IOException { |
| 167 | + Path sourcePath = snapshotConfig.getSourcePath(); |
| 168 | + Path restorePath = snapshotConfig.getRestorePath(); |
| 169 | + Configuration configuration = snapshotConfig.getConfiguration(); |
| 170 | + LOG.info( |
| 171 | + String.format("RestoreSnapshot - sourcePath:%s restorePath: %s", sourcePath, restorePath)); |
| 172 | + FileSystem fileSystem = sourcePath.getFileSystem(configuration); |
| 173 | + if (fileSystem.exists(restorePath)) { |
| 174 | + LOG.info( |
| 175 | + String.format( |
| 176 | + "Restore path %s already exists, deleting it for idempotency", restorePath)); |
| 177 | + fileSystem.delete(restorePath, true); |
| 178 | + } |
| 179 | + RestoreSnapshotHelper.copySnapshotForScanner( |
| 180 | + configuration, fileSystem, sourcePath, restorePath, snapshotConfig.getSnapshotName()); |
| 181 | + } |
| 182 | +} |
0 commit comments