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 | package org.apache.hadoop.hdfs.server.namenode; |
---|
19 | |
---|
20 | import java.io.File; |
---|
21 | import java.io.IOException; |
---|
22 | |
---|
23 | import org.apache.hadoop.fs.permission.FsPermission; |
---|
24 | import org.apache.hadoop.fs.permission.PermissionStatus; |
---|
25 | import org.apache.hadoop.hdfs.protocol.Block; |
---|
26 | import org.apache.hadoop.hdfs.server.common.GenerationStamp; |
---|
27 | import org.apache.hadoop.hdfs.server.common.Storage; |
---|
28 | import org.apache.hadoop.hdfs.server.namenode.BlocksMap.BlockInfo; |
---|
29 | |
---|
30 | /** |
---|
31 | * |
---|
32 | * CreateEditsLog |
---|
33 | * Synopsis: CreateEditsLog -f numFiles StartingBlockId numBlocksPerFile |
---|
34 | * [-r replicafactor] [-d editsLogDirectory] |
---|
35 | * Default replication factor is 1 |
---|
36 | * Default edits log directory is /tmp/EditsLogOut |
---|
37 | * |
---|
38 | * Create a name node's edits log in /tmp/EditsLogOut. |
---|
39 | * The file /tmp/EditsLogOut/current/edits can be copied to a name node's |
---|
40 | * dfs.name.dir/current direcotry and the name node can be started as usual. |
---|
41 | * |
---|
42 | * The files are created in /createdViaInjectingInEditsLog |
---|
43 | * The file names contain the starting and ending blockIds; hence once can |
---|
44 | * create multiple edits logs using this command using non overlapping |
---|
45 | * block ids and feed the files to a single name node. |
---|
46 | * |
---|
47 | * See Also @link #DataNodeCluster for injecting a set of matching |
---|
48 | * blocks created with this command into a set of simulated data nodes. |
---|
49 | * |
---|
50 | */ |
---|
51 | |
---|
52 | public class CreateEditsLog { |
---|
53 | static final String BASE_PATH = "/createdViaInjectingInEditsLog"; |
---|
54 | static final String EDITS_DIR = "/tmp/EditsLogOut"; |
---|
55 | static String edits_dir = EDITS_DIR; |
---|
56 | static final public long BLOCK_GENERATION_STAMP = |
---|
57 | GenerationStamp.FIRST_VALID_STAMP; |
---|
58 | |
---|
59 | static void addFiles(FSEditLog editLog, int numFiles, short replication, |
---|
60 | int blocksPerFile, long startingBlockId, |
---|
61 | FileNameGenerator nameGenerator) { |
---|
62 | |
---|
63 | PermissionStatus p = new PermissionStatus("joeDoe", "people", |
---|
64 | new FsPermission((short)0777)); |
---|
65 | INodeDirectory dirInode = new INodeDirectory(p, 0L); |
---|
66 | editLog.logMkDir(BASE_PATH, dirInode); |
---|
67 | long blockSize = 10; |
---|
68 | BlockInfo[] blocks = new BlockInfo[blocksPerFile]; |
---|
69 | for (int iB = 0; iB < blocksPerFile; ++iB) { |
---|
70 | blocks[iB] = |
---|
71 | new BlockInfo(new Block(0, blockSize, BLOCK_GENERATION_STAMP), |
---|
72 | replication); |
---|
73 | } |
---|
74 | |
---|
75 | long currentBlockId = startingBlockId; |
---|
76 | long bidAtSync = startingBlockId; |
---|
77 | |
---|
78 | for (int iF = 0; iF < numFiles; iF++) { |
---|
79 | for (int iB = 0; iB < blocksPerFile; ++iB) { |
---|
80 | blocks[iB].setBlockId(currentBlockId++); |
---|
81 | } |
---|
82 | |
---|
83 | try { |
---|
84 | |
---|
85 | INodeFileUnderConstruction inode = new INodeFileUnderConstruction( |
---|
86 | null, replication, 0, blockSize, blocks, p, "", "", null); |
---|
87 | // Append path to filename with information about blockIDs |
---|
88 | String path = "_" + iF + "_B" + blocks[0].getBlockId() + |
---|
89 | "_to_B" + blocks[blocksPerFile-1].getBlockId() + "_"; |
---|
90 | String filePath = nameGenerator.getNextFileName(""); |
---|
91 | filePath = filePath + path; |
---|
92 | // Log the new sub directory in edits |
---|
93 | if ((iF % nameGenerator.getFilesPerDirectory()) == 0) { |
---|
94 | String currentDir = nameGenerator.getCurrentDir(); |
---|
95 | dirInode = new INodeDirectory(p, 0L); |
---|
96 | editLog.logMkDir(currentDir, dirInode); |
---|
97 | } |
---|
98 | editLog.logOpenFile(filePath, inode); |
---|
99 | editLog.logCloseFile(filePath, inode); |
---|
100 | |
---|
101 | if (currentBlockId - bidAtSync >= 2000) { // sync every 2K blocks |
---|
102 | editLog.logSync(); |
---|
103 | bidAtSync = currentBlockId; |
---|
104 | } |
---|
105 | } catch (IOException e) { |
---|
106 | System.out.println("Creating trascation for file " + iF + |
---|
107 | " encountered exception " + e); |
---|
108 | } |
---|
109 | } |
---|
110 | System.out.println("Created edits log in directory " + edits_dir); |
---|
111 | System.out.println(" containing " + |
---|
112 | numFiles + " File-Creates, each file with " + blocksPerFile + " blocks"); |
---|
113 | System.out.println(" blocks range: " + |
---|
114 | startingBlockId + " to " + (currentBlockId-1)); |
---|
115 | } |
---|
116 | |
---|
117 | static String usage = "Usage: createditlogs " + |
---|
118 | " -f numFiles startingBlockIds NumBlocksPerFile [-r replicafactor] " + |
---|
119 | "[-d editsLogDirectory]\n" + |
---|
120 | " Default replication factor is 1\n" + |
---|
121 | " Default edits log direcory is " + EDITS_DIR + "\n"; |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | static void printUsageExit() { |
---|
126 | System.out.println(usage); |
---|
127 | System.exit(-1); |
---|
128 | } |
---|
129 | static void printUsageExit(String err) { |
---|
130 | System.out.println(err); |
---|
131 | printUsageExit(); |
---|
132 | } |
---|
133 | /** |
---|
134 | * @param args |
---|
135 | * @throws IOException |
---|
136 | */ |
---|
137 | public static void main(String[] args) throws IOException { |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | long startingBlockId = 1; |
---|
142 | int numFiles = 0; |
---|
143 | short replication = 1; |
---|
144 | int numBlocksPerFile = 0; |
---|
145 | |
---|
146 | if (args.length == 0) { |
---|
147 | printUsageExit(); |
---|
148 | } |
---|
149 | |
---|
150 | for (int i = 0; i < args.length; i++) { // parse command line |
---|
151 | if (args[i].equals("-h")) |
---|
152 | printUsageExit(); |
---|
153 | if (args[i].equals("-f")) { |
---|
154 | if (i + 3 >= args.length || args[i+1].startsWith("-") || |
---|
155 | args[i+2].startsWith("-") || args[i+3].startsWith("-")) { |
---|
156 | printUsageExit( |
---|
157 | "Missing num files, starting block and/or number of blocks"); |
---|
158 | } |
---|
159 | numFiles = Integer.parseInt(args[++i]); |
---|
160 | startingBlockId = Integer.parseInt(args[++i]); |
---|
161 | numBlocksPerFile = Integer.parseInt(args[++i]); |
---|
162 | if (numFiles <=0 || numBlocksPerFile <= 0) { |
---|
163 | printUsageExit("numFiles and numBlocksPerFile most be greater than 0"); |
---|
164 | } |
---|
165 | } else if (args[i].equals("-r") || args[i+1].startsWith("-")) { |
---|
166 | if (i + 1 >= args.length) { |
---|
167 | printUsageExit( |
---|
168 | "Missing num files, starting block and/or number of blocks"); |
---|
169 | } |
---|
170 | replication = Short.parseShort(args[++i]); |
---|
171 | } else if (args[i].equals("-d")) { |
---|
172 | if (i + 1 >= args.length || args[i+1].startsWith("-")) { |
---|
173 | printUsageExit("Missing edits logs directory"); |
---|
174 | } |
---|
175 | edits_dir = args[++i]; |
---|
176 | } else { |
---|
177 | printUsageExit(); |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | File editsLogDir = new File(edits_dir); |
---|
183 | File subStructureDir = new File(edits_dir + "/" + |
---|
184 | Storage.STORAGE_DIR_CURRENT); |
---|
185 | if ( !editsLogDir.exists() ) { |
---|
186 | if ( !editsLogDir.mkdir()) { |
---|
187 | System.out.println("cannot create " + edits_dir); |
---|
188 | System.exit(-1); |
---|
189 | } |
---|
190 | } |
---|
191 | if ( !subStructureDir.exists() ) { |
---|
192 | if ( !subStructureDir.mkdir()) { |
---|
193 | System.out.println("cannot create subdirs of " + edits_dir); |
---|
194 | System.exit(-1); |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | FSImage fsImage = new FSImage(new File(edits_dir)); |
---|
199 | FileNameGenerator nameGenerator = new FileNameGenerator(BASE_PATH, 100); |
---|
200 | |
---|
201 | |
---|
202 | FSEditLog editLog = fsImage.getEditLog(); |
---|
203 | editLog.createEditLogFile(fsImage.getFsEditName()); |
---|
204 | editLog.open(); |
---|
205 | addFiles(editLog, numFiles, replication, numBlocksPerFile, startingBlockId, |
---|
206 | nameGenerator); |
---|
207 | editLog.logSync(); |
---|
208 | editLog.close(); |
---|
209 | } |
---|
210 | } |
---|