1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.chukwa.validationframework;
20
21
22 import java.io.File;
23 import java.net.URI;
24 import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
25 import org.apache.hadoop.chukwa.validationframework.util.DataOperations;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.fs.FileStatus;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30
31 public class DemuxDirectoryValidator {
32
33 static Configuration conf = null;
34 static FileSystem fs = null;
35
36 public static void usage() {
37 System.out.println("Usage ...");
38 System.exit(-1);
39 }
40
41 public static void validate(boolean isLocal, FileSystem fs,
42 Configuration conf, String[] directories) {
43 DemuxDirectoryValidator.fs = fs;
44 DemuxDirectoryValidator.conf = conf;
45 try {
46 if (isLocal) {
47 compareLocalDirectory(directories[0], directories[1]);
48 } else {
49 DemuxDirectoryValidator.fs = fs;
50 compareHDFSDirectory(directories[0], directories[1]);
51 }
52 } catch (Exception e) {
53 e.printStackTrace();
54 throw new RuntimeException("Validation failed! [" + directories[0] + "]["
55 + directories[1] + "]", e);
56 }
57 }
58
59
60
61
62 public static void main(String[] args) {
63
64 if (args.length != 3) {
65 usage();
66 }
67
68 String demuxGoldDirectory = args[1];
69 String demuxTestDirectory = args[2];
70 boolean isLocal = true;
71
72 if ("-local".equalsIgnoreCase(args[0])) {
73 compareLocalDirectory(demuxGoldDirectory, demuxTestDirectory);
74 } else if ("-hdfs".equalsIgnoreCase(args[0])) {
75 isLocal = false;
76 conf = new ChukwaConfiguration();
77 String fsName = conf.get("writer.hdfs.filesystem");
78 try {
79 fs = FileSystem.get(new URI(fsName), conf);
80 } catch (Exception e) {
81 e.printStackTrace();
82 throw new RuntimeException(e);
83 }
84 } else {
85 System.out.println("Wrong first argument");
86 usage();
87 }
88
89 String[] dirs = { demuxGoldDirectory, demuxTestDirectory };
90 validate(isLocal, fs, conf, dirs);
91
92 System.out.println("Gold and test directories are equivalent");
93 System.exit(10);
94 }
95
96 public static void compareHDFSDirectory(String gold, String test) {
97 try {
98 Path goldDirectory = new Path(gold);
99 FileStatus[] goldFiles = fs.listStatus(goldDirectory);
100
101
102
103
104
105 for (int i = 0; i < goldFiles.length; i++) {
106
107
108 if (goldFiles[i].getPath().getName().endsWith(".crc")) {
109 continue;
110 }
111
112 System.out.println("Testing ["
113 + goldFiles[i].getPath().getName().intern() + "]");
114
115
116
117
118
119
120
121
122 if (goldFiles[i].isDir()) {
123
124 if (goldFiles[i].getPath().getName().equalsIgnoreCase("_logs")) {
125 continue;
126 }
127
128 compareHDFSDirectory(gold + "/" + goldFiles[i].getPath().getName(),
129 test + "/" + goldFiles[i].getPath().getName());
130 } else {
131 boolean isTheSme = DataOperations.validateChukwaRecords(fs, conf,
132 goldFiles[i].getPath(), new Path(test + "/"
133 + goldFiles[i].getPath().getName()));
134 if (!isTheSme) {
135
136
137 throw new RuntimeException(
138 "ChukwaRecords validation error: for Gold & test [" + gold
139 + "/" + goldFiles[i].getPath().getName() + "] [" + test
140 + "/" + goldFiles[i].getPath().getName()
141 + "] are not the same");
142 }
143 }
144 }
145 } catch (Exception e) {
146 e.printStackTrace();
147 throw new RuntimeException(e);
148 }
149
150 }
151
152 public static void compareLocalDirectory(String gold, String test) {
153 File goldDirectory = new File(gold);
154 String[] goldFiles = goldDirectory.list();
155 File testDirectory = new File(test);
156 String[] testFiles = testDirectory.list();
157
158 for (int i = 0; i < goldFiles.length; i++) {
159 if (goldFiles[i].intern() != testFiles[i].intern()) {
160 throw new RuntimeException("Gold & test dirrectories [" + gold + "/"
161 + goldFiles[i] + "] are not the same");
162 }
163 File g = new File(gold + "/" + goldFiles[i]);
164 if (g.isDirectory()) {
165
166 if (goldFiles[i].equalsIgnoreCase("_logs")) {
167 continue;
168 }
169
170 compareLocalDirectory(gold + "/" + goldFiles[i], test + "/"
171 + goldFiles[i]);
172 } else {
173 boolean md5 = DataOperations.validateMD5(gold + "/" + goldFiles[i],
174 test + "/" + goldFiles[i]);
175 if (!md5) {
176
177
178 throw new RuntimeException("MD5 for Gold & test [" + gold + "/"
179 + goldFiles[i] + "] are not the same");
180 }
181 }
182 }
183
184 }
185 }