View Javadoc

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  
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     * @param args
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       // Path testDirectory = new Path(test);
102       // FileStatus[] testFiles = fs.listStatus(testDirectory);
103       //      
104 
105       for (int i = 0; i < goldFiles.length; i++) {
106 
107         // Skip the crc files
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         // if (goldFiles[i].getPath().getName().intern() !=
116         // testFiles[i].getPath().getName().intern())
117         // {
118         // throw new RuntimeException("Gold & test dirrectories [" + gold +"/"
119         // +goldFiles[i].getPath().getName() +"] are not the same");
120         // }
121 
122         if (goldFiles[i].isDir()) {
123           // Skip the _logs directory
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             // System.out.println("MD5 failed on [" + gold +"/" +goldFiles[i]
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         // Skip the _logs directory
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           // System.out.println("MD5 failed on [" + gold +"/" +goldFiles[i]
177           // +"]");
178           throw new RuntimeException("MD5 for Gold & test [" + gold + "/"
179               + goldFiles[i] + "] are not the same");
180         }
181       }
182     }
183 
184   }
185 }