001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hdfs.server.namenode; 019 020import java.io.File; 021import java.io.IOException; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.apache.hadoop.hdfs.server.common.Storage; 026import org.apache.hadoop.hdfs.server.common.Storage.StorageDirectory; 027import org.apache.hadoop.hdfs.server.common.StorageInfo; 028 029import com.google.common.base.Preconditions; 030 031public abstract class NNUpgradeUtil { 032 033 private static final Log LOG = LogFactory.getLog(NNUpgradeUtil.class); 034 035 /** 036 * Return true if this storage dir can roll back to the previous storage 037 * state, false otherwise. The NN will refuse to run the rollback operation 038 * unless at least one JM or fsimage storage directory can roll back. 039 * 040 * @param storage the storage info for the current state 041 * @param prevStorage the storage info for the previous (unupgraded) state 042 * @param targetLayoutVersion the layout version we intend to roll back to 043 * @return true if this JM can roll back, false otherwise. 044 * @throws IOException in the event of error 045 */ 046 static boolean canRollBack(StorageDirectory sd, StorageInfo storage, 047 StorageInfo prevStorage, int targetLayoutVersion) throws IOException { 048 File prevDir = sd.getPreviousDir(); 049 if (!prevDir.exists()) { // use current directory then 050 LOG.info("Storage directory " + sd.getRoot() 051 + " does not contain previous fs state."); 052 // read and verify consistency with other directories 053 storage.readProperties(sd); 054 return false; 055 } 056 057 // read and verify consistency of the prev dir 058 prevStorage.readPreviousVersionProperties(sd); 059 060 if (prevStorage.getLayoutVersion() != targetLayoutVersion) { 061 throw new IOException( 062 "Cannot rollback to storage version " + 063 prevStorage.getLayoutVersion() + 064 " using this version of the NameNode, which uses storage version " + 065 targetLayoutVersion + ". " + 066 "Please use the previous version of HDFS to perform the rollback."); 067 } 068 069 return true; 070 } 071 072 /** 073 * Finalize the upgrade. The previous dir, if any, will be renamed and 074 * removed. After this is completed, rollback is no longer allowed. 075 * 076 * @param sd the storage directory to finalize 077 * @throws IOException in the event of error 078 */ 079 static void doFinalize(StorageDirectory sd) throws IOException { 080 File prevDir = sd.getPreviousDir(); 081 if (!prevDir.exists()) { // already discarded 082 LOG.info("Directory " + prevDir + " does not exist."); 083 LOG.info("Finalize upgrade for " + sd.getRoot()+ " is not required."); 084 return; 085 } 086 LOG.info("Finalizing upgrade of storage directory " + sd.getRoot()); 087 Preconditions.checkState(sd.getCurrentDir().exists(), 088 "Current directory must exist."); 089 final File tmpDir = sd.getFinalizedTmp(); 090 // rename previous to tmp and remove 091 NNStorage.rename(prevDir, tmpDir); 092 NNStorage.deleteDir(tmpDir); 093 LOG.info("Finalize upgrade for " + sd.getRoot()+ " is complete."); 094 } 095 096 /** 097 * Perform any steps that must succeed across all storage dirs/JournalManagers 098 * involved in an upgrade before proceeding onto the actual upgrade stage. If 099 * a call to any JM's or local storage dir's doPreUpgrade method fails, then 100 * doUpgrade will not be called for any JM. The existing current dir is 101 * renamed to previous.tmp, and then a new, empty current dir is created. 102 * 103 * @param sd the storage directory to perform the pre-upgrade procedure. 104 * @throws IOException in the event of error 105 */ 106 static void doPreUpgrade(StorageDirectory sd) throws IOException { 107 LOG.info("Starting upgrade of storage directory " + sd.getRoot()); 108 109 // rename current to tmp 110 renameCurToTmp(sd); 111 } 112 113 /** 114 * Rename the existing current dir to previous.tmp, and create a new empty 115 * current dir. 116 */ 117 public static void renameCurToTmp(StorageDirectory sd) throws IOException { 118 File curDir = sd.getCurrentDir(); 119 File prevDir = sd.getPreviousDir(); 120 final File tmpDir = sd.getPreviousTmp(); 121 122 Preconditions.checkState(curDir.exists(), 123 "Current directory must exist for preupgrade."); 124 Preconditions.checkState(!prevDir.exists(), 125 "Previous directory must not exist for preupgrade."); 126 Preconditions.checkState(!tmpDir.exists(), 127 "Previous.tmp directory must not exist for preupgrade." 128 + "Consider restarting for recovery."); 129 130 // rename current to tmp 131 NNStorage.rename(curDir, tmpDir); 132 133 if (!curDir.mkdir()) { 134 throw new IOException("Cannot create directory " + curDir); 135 } 136 } 137 138 /** 139 * Perform the upgrade of the storage dir to the given storage info. The new 140 * storage info is written into the current directory, and the previous.tmp 141 * directory is renamed to previous. 142 * 143 * @param sd the storage directory to upgrade 144 * @param storage info about the new upgraded versions. 145 * @throws IOException in the event of error 146 */ 147 public static void doUpgrade(StorageDirectory sd, Storage storage) 148 throws IOException { 149 LOG.info("Performing upgrade of storage directory " + sd.getRoot()); 150 try { 151 // Write the version file, since saveFsImage only makes the 152 // fsimage_<txid>, and the directory is otherwise empty. 153 storage.writeProperties(sd); 154 155 File prevDir = sd.getPreviousDir(); 156 File tmpDir = sd.getPreviousTmp(); 157 Preconditions.checkState(!prevDir.exists(), 158 "previous directory must not exist for upgrade."); 159 Preconditions.checkState(tmpDir.exists(), 160 "previous.tmp directory must exist for upgrade."); 161 162 // rename tmp to previous 163 NNStorage.rename(tmpDir, prevDir); 164 } catch (IOException ioe) { 165 LOG.error("Unable to rename temp to previous for " + sd.getRoot(), ioe); 166 throw ioe; 167 } 168 } 169 170 /** 171 * Perform rollback of the storage dir to the previous state. The existing 172 * current dir is removed, and the previous dir is renamed to current. 173 * 174 * @param sd the storage directory to roll back. 175 * @throws IOException in the event of error 176 */ 177 static void doRollBack(StorageDirectory sd) 178 throws IOException { 179 File prevDir = sd.getPreviousDir(); 180 if (!prevDir.exists()) { 181 return; 182 } 183 184 File tmpDir = sd.getRemovedTmp(); 185 Preconditions.checkState(!tmpDir.exists(), 186 "removed.tmp directory must not exist for rollback." 187 + "Consider restarting for recovery."); 188 // rename current to tmp 189 File curDir = sd.getCurrentDir(); 190 Preconditions.checkState(curDir.exists(), 191 "Current directory must exist for rollback."); 192 193 NNStorage.rename(curDir, tmpDir); 194 // rename previous to current 195 NNStorage.rename(prevDir, curDir); 196 197 // delete tmp dir 198 NNStorage.deleteDir(tmpDir); 199 LOG.info("Rollback of " + sd.getRoot() + " is complete."); 200 } 201 202}