|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.ozone.repair.om; |
| 20 | + |
| 21 | +import org.apache.hadoop.hdds.cli.SubcommandWithParent; |
| 22 | +import org.apache.hadoop.hdds.utils.IOUtils; |
| 23 | +import org.apache.hadoop.hdds.utils.db.StringCodec; |
| 24 | +import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksDB; |
| 25 | +import org.apache.hadoop.hdds.utils.db.managed.ManagedRocksIterator; |
| 26 | +import org.apache.hadoop.ozone.debug.RocksDBUtils; |
| 27 | +import org.apache.hadoop.ozone.om.helpers.SnapshotInfo; |
| 28 | +import org.apache.hadoop.ozone.repair.RDBRepair; |
| 29 | +import org.apache.hadoop.ozone.shell.bucket.BucketUri; |
| 30 | +import org.kohsuke.MetaInfServices; |
| 31 | +import org.rocksdb.ColumnFamilyDescriptor; |
| 32 | +import org.rocksdb.ColumnFamilyHandle; |
| 33 | +import org.rocksdb.RocksDBException; |
| 34 | +import picocli.CommandLine; |
| 35 | +import picocli.CommandLine.Model.CommandSpec; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.nio.charset.StandardCharsets; |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.Arrays; |
| 41 | +import java.util.HashSet; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Objects; |
| 44 | +import java.util.Set; |
| 45 | +import java.util.UUID; |
| 46 | +import java.util.concurrent.Callable; |
| 47 | + |
| 48 | +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; |
| 49 | +import static org.apache.hadoop.ozone.OzoneConsts.SNAPSHOT_INFO_TABLE; |
| 50 | + |
| 51 | +/** |
| 52 | + * Tool to repair snapshotInfoTable in case it has corrupted entries. |
| 53 | + */ |
| 54 | +@CommandLine.Command( |
| 55 | + name = "snapshot", |
| 56 | + description = "CLI to update global and path previous snapshot for a snapshot in case snapshot chain is corrupted." |
| 57 | +) |
| 58 | +@MetaInfServices(SubcommandWithParent.class) |
| 59 | +public class SnapshotRepair implements Callable<Void>, SubcommandWithParent { |
| 60 | + |
| 61 | + @CommandLine.Spec |
| 62 | + private static CommandSpec spec; |
| 63 | + |
| 64 | + @CommandLine.ParentCommand |
| 65 | + private RDBRepair parent; |
| 66 | + |
| 67 | + @CommandLine.Mixin |
| 68 | + private BucketUri bucketUri; |
| 69 | + |
| 70 | + @CommandLine.Parameters(description = "Snapshot name to update", index = "1") |
| 71 | + private String snapshotName; |
| 72 | + |
| 73 | + @CommandLine.Option(names = {"--global-previous", "--gp"}, |
| 74 | + required = true, |
| 75 | + description = "Global previous snapshotId to set for the given snapshot") |
| 76 | + private UUID globalPreviousSnapshotId; |
| 77 | + |
| 78 | + @CommandLine.Option(names = {"--path-previous", "--pp"}, |
| 79 | + required = true, |
| 80 | + description = "Path previous snapshotId to set for the given snapshot") |
| 81 | + private UUID pathPreviousSnapshotId; |
| 82 | + |
| 83 | + @CommandLine.Option(names = {"--dry-run"}, |
| 84 | + required = true, |
| 85 | + description = "To dry-run the command.", defaultValue = "true") |
| 86 | + private boolean dryRun; |
| 87 | + |
| 88 | + @Override |
| 89 | + public Void call() throws Exception { |
| 90 | + List<ColumnFamilyHandle> cfHandleList = new ArrayList<>(); |
| 91 | + List<ColumnFamilyDescriptor> cfDescList = RocksDBUtils.getColumnFamilyDescriptors(parent.getDbPath()); |
| 92 | + |
| 93 | + try (ManagedRocksDB db = ManagedRocksDB.open(parent.getDbPath(), cfDescList, cfHandleList)) { |
| 94 | + ColumnFamilyHandle snapshotInfoCfh = getSnapshotInfoCfh(cfHandleList); |
| 95 | + if (snapshotInfoCfh == null) { |
| 96 | + System.err.println(SNAPSHOT_INFO_TABLE + " is not in a column family in DB for the given path."); |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + String snapshotInfoTableKey = SnapshotInfo.getTableKey(bucketUri.getValue().getVolumeName(), |
| 101 | + bucketUri.getValue().getBucketName(), snapshotName); |
| 102 | + |
| 103 | + SnapshotInfo snapshotInfo = getSnapshotInfo(db, snapshotInfoCfh, snapshotInfoTableKey); |
| 104 | + if (snapshotInfo == null) { |
| 105 | + System.err.println(snapshotName + " does not exist for given bucketUri: " + OM_KEY_PREFIX + |
| 106 | + bucketUri.getValue().getVolumeName() + OM_KEY_PREFIX + bucketUri.getValue().getBucketName()); |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + // snapshotIdSet is the set of the all existed snapshots ID to make that the provided global previous and path |
| 111 | + // previous exist and after the update snapshot does not point to ghost snapshot. |
| 112 | + Set<UUID> snapshotIdSet = getSnapshotIdSet(db, snapshotInfoCfh); |
| 113 | + |
| 114 | + if (Objects.equals(snapshotInfo.getSnapshotId(), globalPreviousSnapshotId)) { |
| 115 | + System.err.println("globalPreviousSnapshotId: '" + globalPreviousSnapshotId + |
| 116 | + "' is equal to given snapshot's ID: '" + snapshotInfo.getSnapshotId() + "'."); |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + if (Objects.equals(snapshotInfo.getSnapshotId(), pathPreviousSnapshotId)) { |
| 121 | + System.err.println("pathPreviousSnapshotId: '" + pathPreviousSnapshotId + |
| 122 | + "' is equal to given snapshot's ID: '" + snapshotInfo.getSnapshotId() + "'."); |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + if (!snapshotIdSet.contains(globalPreviousSnapshotId)) { |
| 127 | + System.err.println("globalPreviousSnapshotId: '" + globalPreviousSnapshotId + |
| 128 | + "' does not exist in snapshotInfoTable."); |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + if (!snapshotIdSet.contains(pathPreviousSnapshotId)) { |
| 133 | + System.err.println("pathPreviousSnapshotId: '" + pathPreviousSnapshotId + |
| 134 | + "' does not exist in snapshotInfoTable."); |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + snapshotInfo.setGlobalPreviousSnapshotId(globalPreviousSnapshotId); |
| 139 | + snapshotInfo.setPathPreviousSnapshotId(pathPreviousSnapshotId); |
| 140 | + |
| 141 | + if (dryRun) { |
| 142 | + System.out.println("SnapshotInfo would be updated to : " + snapshotInfo); |
| 143 | + } else { |
| 144 | + byte[] snapshotInfoBytes = SnapshotInfo.getCodec().toPersistedFormat(snapshotInfo); |
| 145 | + db.get() |
| 146 | + .put(snapshotInfoCfh, StringCodec.get().toPersistedFormat(snapshotInfoTableKey), snapshotInfoBytes); |
| 147 | + |
| 148 | + System.out.println("Snapshot Info is updated to : " + |
| 149 | + getSnapshotInfo(db, snapshotInfoCfh, snapshotInfoTableKey)); |
| 150 | + } |
| 151 | + } catch (RocksDBException exception) { |
| 152 | + System.err.println("Failed to update the RocksDB for the given path: " + parent.getDbPath()); |
| 153 | + System.err.println( |
| 154 | + "Make sure that Ozone entity (OM, SCM or DN) is not running for the give dbPath and current host."); |
| 155 | + System.err.println(exception); |
| 156 | + } finally { |
| 157 | + IOUtils.closeQuietly(cfHandleList); |
| 158 | + } |
| 159 | + |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + private Set<UUID> getSnapshotIdSet(ManagedRocksDB db, ColumnFamilyHandle snapshotInfoCfh) |
| 164 | + throws IOException { |
| 165 | + Set<UUID> snapshotIdSet = new HashSet<>(); |
| 166 | + try (ManagedRocksIterator iterator = new ManagedRocksIterator(db.get().newIterator(snapshotInfoCfh))) { |
| 167 | + iterator.get().seekToFirst(); |
| 168 | + |
| 169 | + while (iterator.get().isValid()) { |
| 170 | + SnapshotInfo snapshotInfo = SnapshotInfo.getCodec().fromPersistedFormat(iterator.get().value()); |
| 171 | + snapshotIdSet.add(snapshotInfo.getSnapshotId()); |
| 172 | + iterator.get().next(); |
| 173 | + } |
| 174 | + } |
| 175 | + return snapshotIdSet; |
| 176 | + } |
| 177 | + |
| 178 | + private ColumnFamilyHandle getSnapshotInfoCfh(List<ColumnFamilyHandle> cfHandleList) throws RocksDBException { |
| 179 | + byte[] nameBytes = SNAPSHOT_INFO_TABLE.getBytes(StandardCharsets.UTF_8); |
| 180 | + |
| 181 | + for (ColumnFamilyHandle cf : cfHandleList) { |
| 182 | + if (Arrays.equals(cf.getName(), nameBytes)) { |
| 183 | + return cf; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return null; |
| 188 | + } |
| 189 | + |
| 190 | + private SnapshotInfo getSnapshotInfo(ManagedRocksDB db, ColumnFamilyHandle snapshotInfoCfh, String snapshotInfoLKey) |
| 191 | + throws IOException, RocksDBException { |
| 192 | + byte[] bytes = db.get().get(snapshotInfoCfh, StringCodec.get().toPersistedFormat(snapshotInfoLKey)); |
| 193 | + return bytes != null ? SnapshotInfo.getCodec().fromPersistedFormat(bytes) : null; |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public Class<?> getParentType() { |
| 198 | + return RDBRepair.class; |
| 199 | + } |
| 200 | +} |
0 commit comments