1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util;
19
20 import java.io.IOException;
21 import java.util.List;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.fs.FileStatus;
26 import org.apache.hadoop.fs.FileSystem;
27 import org.apache.hadoop.fs.Path;
28 import org.apache.hadoop.hbase.TableName;
29 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
30
31
32
33
34
35
36
37
38 @Deprecated
39 public class FSTableDescriptorMigrationToSubdir {
40
41 private static final Log LOG = LogFactory.getLog(FSTableDescriptorMigrationToSubdir.class);
42
43 public static void migrateFSTableDescriptorsIfNecessary(FileSystem fs, Path rootDir)
44 throws IOException {
45 if (needsMigration(fs, rootDir)) {
46 migrateFsTableDescriptors(fs, rootDir);
47 LOG.info("Migration complete.");
48 }
49 }
50
51
52
53
54
55 private static boolean needsMigration(FileSystem fs, Path rootDir) throws IOException {
56 Path metaTableDir = FSUtils.getTableDir(rootDir,
57 TableName.META_TABLE_NAME);
58 FileStatus metaTableInfoStatus =
59 FSTableDescriptors.getTableInfoPath(fs, metaTableDir);
60 return metaTableInfoStatus == null;
61 }
62
63
64
65
66
67
68
69
70 private static void migrateFsTableDescriptors(FileSystem fs, Path rootDir) throws IOException {
71
72 Path snapshotsDir = SnapshotDescriptionUtils.getSnapshotsDir(rootDir);
73 if (fs.exists(snapshotsDir)) {
74 LOG.info("Migrating snapshots");
75 FileStatus[] snapshots = fs.listStatus(snapshotsDir,
76 new SnapshotDescriptionUtils.CompletedSnaphotDirectoriesFilter(fs));
77 for (FileStatus snapshot : snapshots) {
78 migrateTable(fs, snapshot.getPath());
79 }
80 }
81
82 LOG.info("Migrating user tables");
83 List<Path> userTableDirs = FSUtils.getTableDirs(fs, rootDir);
84 for (Path userTableDir : userTableDirs) {
85 migrateTable(fs, userTableDir);
86 }
87
88 LOG.info("Migrating system tables");
89
90 migrateTableIfExists(fs, rootDir, TableName.META_TABLE_NAME);
91 }
92
93 private static void migrateTableIfExists(FileSystem fs, Path rootDir, TableName tableName)
94 throws IOException {
95 Path tableDir = FSUtils.getTableDir(rootDir, tableName);
96 if (fs.exists(tableDir)) {
97 migrateTable(fs, tableDir);
98 }
99 }
100
101
102
103
104
105
106 private static void migrateTable(FileSystem fs, Path tableDir) throws IOException {
107 FileStatus oldTableStatus = FSTableDescriptors.getCurrentTableInfoStatus(fs, tableDir, true);
108 if (oldTableStatus == null) {
109 LOG.debug("No table info file to migrate for " + tableDir);
110 return;
111 }
112
113 Path tableInfoDir = new Path(tableDir, FSTableDescriptors.TABLEINFO_DIR);
114
115 boolean removedExistingSubdir = FSUtils.deleteDirectory(fs, tableInfoDir);
116 if (removedExistingSubdir) {
117 LOG.info("Removed existing subdir at: " + tableInfoDir);
118 }
119 boolean createdSubdir = fs.mkdirs(tableInfoDir);
120 if (!createdSubdir) {
121 throw new IOException("Unable to create new table info directory: " + tableInfoDir);
122 }
123
124 Path oldTableInfoPath = oldTableStatus.getPath();
125 Path newTableInfoPath = new Path(tableInfoDir, oldTableInfoPath.getName());
126 boolean renamedInfoFile = fs.rename(oldTableInfoPath, newTableInfoPath);
127 if (!renamedInfoFile) {
128 throw new IOException("Failed to move table info file from old location: "
129 + oldTableInfoPath + " to new location: " + newTableInfoPath);
130 }
131
132 LOG.info("Migrated table info from: " + oldTableInfoPath
133 + " to new location: " + newTableInfoPath);
134 }
135
136 }