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.tools.offlineImageViewer;
19 
20 import java.io.IOException;
21 
22 /**
23  * An implementation of ImageVisitor can traverse the structure of an
24  * Hadoop fsimage and respond to each of the structures within the file.
25  */
26 abstract class ImageVisitor {
27 
28   /**
29    * Structural elements of an FSImage that may be encountered within the
30    * file. ImageVisitors are able to handle processing any of these elements.
31    */
32   public enum ImageElement {
33     FS_IMAGE,
34     IMAGE_VERSION,
35     NAMESPACE_ID,
36     IS_COMPRESSED,
37     COMPRESS_CODEC,
38     LAYOUT_VERSION,
39     NUM_INODES,
40     GENERATION_STAMP,
41     GENERATION_STAMP_V2,
42     GENERATION_STAMP_V1_LIMIT,
43     LAST_ALLOCATED_BLOCK_ID,
44     INODES,
45     INODE,
46     INODE_PATH,
47     REPLICATION,
48     MODIFICATION_TIME,
49     ACCESS_TIME,
50     BLOCK_SIZE,
51     NUM_BLOCKS,
52     BLOCKS,
53     BLOCK,
54     BLOCK_ID,
55     NUM_BYTES,
56     NS_QUOTA,
57     DS_QUOTA,
58     PERMISSIONS,
59     SYMLINK,
60     NUM_INODES_UNDER_CONSTRUCTION,
61     INODES_UNDER_CONSTRUCTION,
62     INODE_UNDER_CONSTRUCTION,
63     PREFERRED_BLOCK_SIZE,
64     CLIENT_NAME,
65     CLIENT_MACHINE,
66     USER_NAME,
67     GROUP_NAME,
68     PERMISSION_STRING,
69     CURRENT_DELEGATION_KEY_ID,
70     NUM_DELEGATION_KEYS,
71     DELEGATION_KEYS,
72     DELEGATION_KEY,
73     DELEGATION_TOKEN_SEQUENCE_NUMBER,
74     NUM_DELEGATION_TOKENS,
75     DELEGATION_TOKENS,
76     DELEGATION_TOKEN_IDENTIFIER,
77     DELEGATION_TOKEN_IDENTIFIER_KIND,
78     DELEGATION_TOKEN_IDENTIFIER_SEQNO,
79     DELEGATION_TOKEN_IDENTIFIER_OWNER,
80     DELEGATION_TOKEN_IDENTIFIER_RENEWER,
81     DELEGATION_TOKEN_IDENTIFIER_REALUSER,
82     DELEGATION_TOKEN_IDENTIFIER_ISSUE_DATE,
83     DELEGATION_TOKEN_IDENTIFIER_MAX_DATE,
84     DELEGATION_TOKEN_IDENTIFIER_EXPIRY_TIME,
85     DELEGATION_TOKEN_IDENTIFIER_MASTER_KEY_ID,
86     TRANSACTION_ID,
87     LAST_INODE_ID,
88     INODE_ID,
89 
90     SNAPSHOT_COUNTER,
91     NUM_SNAPSHOTS_TOTAL,
92     NUM_SNAPSHOTS,
93     SNAPSHOTS,
94     SNAPSHOT,
95     SNAPSHOT_ID,
96     SNAPSHOT_ROOT,
97     SNAPSHOT_QUOTA,
98     NUM_SNAPSHOT_DIR_DIFF,
99     SNAPSHOT_DIR_DIFFS,
100     SNAPSHOT_DIR_DIFF,
101     SNAPSHOT_DIFF_SNAPSHOTID,
102     SNAPSHOT_DIR_DIFF_CHILDREN_SIZE,
103     SNAPSHOT_INODE_FILE_ATTRIBUTES,
104     SNAPSHOT_INODE_DIRECTORY_ATTRIBUTES,
105     SNAPSHOT_DIR_DIFF_CREATEDLIST,
106     SNAPSHOT_DIR_DIFF_CREATEDLIST_SIZE,
107     SNAPSHOT_DIR_DIFF_CREATED_INODE,
108     SNAPSHOT_DIR_DIFF_DELETEDLIST,
109     SNAPSHOT_DIR_DIFF_DELETEDLIST_SIZE,
110     SNAPSHOT_DIR_DIFF_DELETED_INODE,
111     IS_SNAPSHOTTABLE_DIR,
112     IS_WITHSNAPSHOT_DIR,
113     SNAPSHOT_FILE_DIFFS,
114     SNAPSHOT_FILE_DIFF,
115     NUM_SNAPSHOT_FILE_DIFF,
116     SNAPSHOT_FILE_SIZE,
117     SNAPSHOT_DST_SNAPSHOT_ID,
118     SNAPSHOT_LAST_SNAPSHOT_ID,
119     SNAPSHOT_REF_INODE_ID,
120     SNAPSHOT_REF_INODE,
121 
122     CACHE_NEXT_ENTRY_ID,
123     CACHE_NUM_POOLS,
124     CACHE_POOL_NAME,
125     CACHE_POOL_OWNER_NAME,
126     CACHE_POOL_GROUP_NAME,
127     CACHE_POOL_PERMISSION_STRING,
128     CACHE_POOL_WEIGHT,
129     CACHE_NUM_ENTRIES,
130     CACHE_ENTRY_PATH,
131     CACHE_ENTRY_REPLICATION,
132     CACHE_ENTRY_POOL_NAME
133   }
134 
135   /**
136    * Begin visiting the fsimage structure.  Opportunity to perform
137    * any initialization necessary for the implementing visitor.
138    */
start()139   abstract void start() throws IOException;
140 
141   /**
142    * Finish visiting the fsimage structure.  Opportunity to perform any
143    * clean up necessary for the implementing visitor.
144    */
finish()145   abstract void finish() throws IOException;
146 
147   /**
148    * Finish visiting the fsimage structure after an error has occurred
149    * during the processing.  Opportunity to perform any clean up necessary
150    * for the implementing visitor.
151    */
finishAbnormally()152   abstract void finishAbnormally() throws IOException;
153 
154   /**
155    * Visit non enclosing element of fsimage with specified value.
156    *
157    * @param element FSImage element
158    * @param value Element's value
159    */
visit(ImageElement element, String value)160   abstract void visit(ImageElement element, String value) throws IOException;
161 
162   // Convenience methods to automatically convert numeric value types to strings
visit(ImageElement element, int value)163   void visit(ImageElement element, int value) throws IOException {
164     visit(element, Integer.toString(value));
165   }
166 
visit(ImageElement element, long value)167   void visit(ImageElement element, long value) throws IOException {
168     visit(element, Long.toString(value));
169   }
170 
171   /**
172    * Begin visiting an element that encloses another element, such as
173    * the beginning of the list of blocks that comprise a file.
174    *
175    * @param element Element being visited
176    */
visitEnclosingElement(ImageElement element)177   abstract void visitEnclosingElement(ImageElement element)
178      throws IOException;
179 
180   /**
181    * Begin visiting an element that encloses another element, such as
182    * the beginning of the list of blocks that comprise a file.
183    *
184    * Also provide an additional key and value for the element, such as the
185    * number items within the element.
186    *
187    * @param element Element being visited
188    * @param key Key describing the element being visited
189    * @param value Value associated with element being visited
190    */
visitEnclosingElement(ImageElement element, ImageElement key, String value)191   abstract void visitEnclosingElement(ImageElement element,
192       ImageElement key, String value) throws IOException;
193 
194   // Convenience methods to automatically convert value types to strings
visitEnclosingElement(ImageElement element, ImageElement key, int value)195   void visitEnclosingElement(ImageElement element,
196       ImageElement key, int value)
197      throws IOException {
198     visitEnclosingElement(element, key, Integer.toString(value));
199   }
200 
visitEnclosingElement(ImageElement element, ImageElement key, long value)201   void visitEnclosingElement(ImageElement element,
202       ImageElement key, long value)
203      throws IOException {
204     visitEnclosingElement(element, key, Long.toString(value));
205   }
206 
207   /**
208    * Leave current enclosing element.  Called, for instance, at the end of
209    * processing the blocks that compromise a file.
210    */
leaveEnclosingElement()211   abstract void leaveEnclosingElement() throws IOException;
212 }
213