1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.database;
19
20 import junit.framework.TestCase;
21 import java.util.Calendar;
22 import org.apache.hadoop.chukwa.database.Macro;
23 import org.apache.hadoop.chukwa.util.DatabaseWriter;
24 import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
25 import org.apache.hadoop.chukwa.util.ExceptionUtil;
26 import org.apache.hadoop.chukwa.database.Aggregator;
27 import org.apache.hadoop.chukwa.database.TableCreator;
28 import java.io.BufferedReader;
29 import java.io.File;
30 import java.io.FileReader;
31 import java.io.IOException;
32 import java.sql.ResultSet;
33 import java.sql.SQLException;
34 import java.util.ArrayList;
35
36 public class TestDatabaseTableCreator extends TestCase {
37
38 long[] timeWindow = {7, 30, 91, 365, 3650};
39 String[] tables = {"system_metrics","disk","cluster_system_metrics","cluster_disk","mr_job","mr_task","dfs_namenode","dfs_datanode","dfs_fsnamesystem","dfs_throughput","hadoop_jvm","hadoop_mapred","hdfs_usage"};
40 String cluster = "demo";
41 long current = Calendar.getInstance().getTimeInMillis();
42
43 public void setUp() {
44 System.setProperty("CLUSTER","demo");
45 DatabaseWriter db = new DatabaseWriter(cluster);
46 String buffer = "";
47 File aFile = new File(System.getenv("CHUKWA_CONF_DIR")
48 + File.separator + "database_create_tables.sql");
49 buffer = readFile(aFile);
50 String tables[] = buffer.split(";");
51 for(String table : tables) {
52 if(table.length()>5) {
53 try {
54 db.execute(table);
55 } catch (Exception e) {
56 fail("Fail to retrieve meta data for database table:"+table);
57 }
58 }
59 }
60 db.close();
61 for(int i=0;i<timeWindow.length;i++) {
62 TableCreator tc = new TableCreator();
63 long start = current;
64 long end = current + (timeWindow[i]*1440*60*1000);
65 try {
66 tc.createTables(start, end);
67 } catch (Exception e) {
68 e.printStackTrace();
69 fail("Fail to create database tables.");
70 }
71 }
72 }
73
74 public void tearDown() {
75 DatabaseWriter db = null;
76 try {
77 db = new DatabaseWriter(cluster);
78 ResultSet rs = db.query("show tables");
79 ArrayList<String> list = new ArrayList<String>();
80 while(rs.next()) {
81 String table = rs.getString(1);
82 list.add(table);
83 }
84 for(String table : list) {
85 db.execute("drop table "+table);
86 }
87 } catch(Throwable ex) {
88 } finally {
89 if(db!=null) {
90 db.close();
91 }
92 }
93 }
94
95 public void verifyTable(String table) {
96 ChukwaConfiguration cc = new ChukwaConfiguration();
97 String query = "select * from ["+table+"];";
98 Macro mp = new Macro(current,query);
99 query = mp.toString();
100 try {
101 DatabaseWriter db = new DatabaseWriter(cluster);
102 ResultSet rs = db.query(query);
103 while(rs.next()) {
104 int i = 1;
105 String value = rs.getString(i);
106 }
107 db.close();
108 } catch(SQLException ex) {
109 fail("SQL Exception: "+ExceptionUtil.getStackTrace(ex));
110 }
111 }
112
113 public String readFile(File aFile) {
114 StringBuffer contents = new StringBuffer();
115 try {
116 BufferedReader input = new BufferedReader(new FileReader(aFile));
117 try {
118 String line = null;
119 while ((line = input.readLine()) != null) {
120 contents.append(line);
121 contents.append(System.getProperty("line.separator"));
122 }
123 } finally {
124 input.close();
125 }
126 } catch (IOException ex) {
127 ex.printStackTrace();
128 }
129 return contents.toString();
130 }
131
132 public void testTableCreator() {
133 for(int i=0;i<timeWindow.length;i++) {
134 try {
135 DatabaseWriter db = new DatabaseWriter(cluster);
136 for(String table : tables) {
137 String query = "select * from ["+table+"];";
138 Macro mp = new Macro(current,query);
139 query = mp.toString();
140 ResultSet rs = db.query(query);
141 rs.last();
142 int count = rs.getRow();
143 assertTrue("Table should exist and return empty result.", count==0);
144 }
145 db.close();
146 } catch(SQLException ex) {
147 fail("SQL Exception: "+ExceptionUtil.getStackTrace(ex));
148 }
149 }
150 }
151
152 public void testTables() {
153 for(String table : tables) {
154 verifyTable(table);
155 assertTrue("Table verified: " + table, true);
156 }
157 }
158
159 }