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.*;
21 import java.util.*;
22 import java.text.*;
23 import java.io.*;
24 import java.net.URL;
25 import java.sql.*;
26 import org.apache.commons.httpclient.*;
27 import org.apache.commons.httpclient.methods.*;
28 import org.apache.commons.httpclient.params.HttpMethodParams;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.json.*;
32 import org.json.simple.JSONArray;
33 import org.json.simple.JSONObject;
34 import org.json.simple.JSONValue;
35 import org.mortbay.jetty.Server;
36 import org.mortbay.xml.XmlConfiguration;
37 import org.apache.hadoop.chukwa.util.*;
38
39
40
41
42
43 public class TestDatabaseWebJson extends TestCase {
44 protected HashMap testTables;
45 protected String data_url="http://localhost:8080/hicc/jsp/get_db_data.jsp";
46 private Server server = null;
47 private Log log = LogFactory.getLog(TestDatabaseWebJson.class);
48
49
50
51
52
53 protected void setUp() {
54 testTables = new HashMap();
55
56 ArrayList<String> keys = new ArrayList<String>();
57 keys.add("timestamp");
58 keys.add("mount");
59 testTables.put("cluster_disk", keys);
60
61 keys = new ArrayList<String>();
62 keys.add("timestamp");
63 testTables.put("cluster_system_metrics", keys);
64
65 keys = new ArrayList<String>();
66 keys.add("timestamp");
67 keys.add("host");
68 keys.add("mount");
69 testTables.put("disk", keys);
70
71 keys = new ArrayList<String>();
72 keys.add("job_id");
73 testTables.put("mr_job", keys);
74
75 keys = new ArrayList<String>();
76 keys.add("task_id");
77 testTables.put("mr_task", keys);
78
79 keys = new ArrayList<String>();
80 keys.add("timestamp");
81 testTables.put("system_metrics", keys);
82 URL serverConf = TestDatabaseWebJson.class
83 .getResource("/WEB-INF/jetty.xml");
84 server = new Server();
85 XmlConfiguration configuration;
86 try {
87 configuration = new XmlConfiguration(serverConf);
88 configuration.configure(server);
89 server.start();
90 server.setStopAtShutdown(true);
91 } catch (Exception e) {
92 log.error(ExceptionUtil.getStackTrace(e));
93 }
94 }
95
96 protected void tearDown() {
97 try {
98 server.stop();
99 Thread.sleep(2000);
100 } catch (Exception e) {
101 log.error(ExceptionUtil.getStackTrace(e));
102 }
103 }
104
105
106
107
108
109
110
111
112
113
114 protected static String join( String token, ArrayList<String> strings )
115 {
116 StringBuffer sb = new StringBuffer();
117
118 for( int x = 0; x < ( strings.size() - 1 ); x++ ) {
119 sb.append( strings.get(x) );
120 sb.append( token );
121 }
122 sb.append( strings.get( strings.size() - 1 ) );
123
124 return( sb.toString() );
125 }
126
127
128
129
130
131
132
133
134
135
136 protected String getDatabaseQuery(String tableName, JSONObject jo) {
137 ArrayList<String> keys = (ArrayList<String>)testTables.get(tableName);
138 ArrayList<String> criterias = new ArrayList<String>();
139 Iterator i = keys.iterator();
140 while (i.hasNext()) {
141 String key=(String)i.next();
142 try {
143 String value=(String)jo.get(key);
144 if (key.compareToIgnoreCase("timestamp")==0) {
145 value=DatabaseWriter.formatTimeStamp(Long.parseLong(value));
146 }
147 String c=key+"=\""+value+"\"";
148 criterias.add(c);
149 } catch (Exception e) {
150 System.out.println("Cannot get value for key: "+key);
151 }
152 }
153 String criteria=join(" and ", criterias);
154
155 String query="select * from ["+tableName+"] where "+criteria;
156 return query;
157 }
158
159
160
161
162
163
164
165
166 protected void verifyTableData(String table) {
167 Calendar startCalendar = new GregorianCalendar();
168
169 startCalendar.add(Calendar.MINUTE, -30);
170 long startTime=startCalendar.getTime().getTime();
171
172 Calendar endCalendar = new GregorianCalendar();
173
174 long endTime=endCalendar.getTime().getTime();
175
176 String url=data_url+"?table="+table+"&start="+startTime+"&end="+endTime;
177 System.out.println(url);
178
179 HttpClient client = new HttpClient();
180 GetMethod method = new GetMethod(url);
181
182
183 try {
184
185
186
187
188 int statusCode = client.executeMethod(method);
189 if (statusCode != HttpStatus.SC_OK) {
190 System.out.println("Http Error: "+method.getStatusLine());
191 }
192 BufferedReader reader = new BufferedReader(new InputStreamReader( method.getResponseBodyAsStream(),
193 method.getResponseCharSet()));
194 String json_str="";
195 String str;
196 while ((str = reader.readLine()) != null) {
197 json_str+=str;
198 }
199
200
201
202
203
204
205 String cluster = "demo";
206 DatabaseWriter db = new DatabaseWriter(cluster);
207
208 JSONArray json_array=(JSONArray)JSONValue.parse(json_str);
209 for (int i=0; i < json_array.size(); i++) {
210 JSONObject row_obj=(JSONObject) json_array.get(i);
211
212
213
214 String queryString=getDatabaseQuery(table, row_obj);
215 Macro m=new Macro(startTime, endTime, queryString);
216 ResultSet rs = db.query(m.toString());
217
218 rs.next();
219 ResultSetMetaData md=rs.getMetaData();
220 Iterator names=row_obj.keySet().iterator();
221 while (names.hasNext()) {
222 String name=(String)names.next();
223 String jsonValue=(String)row_obj.get(name);
224 String dbValue=rs.getString(name);
225 int dbCol=rs.findColumn(name);
226 int dbType=md.getColumnType(dbCol);
227 if (dbType==93) {
228
229 dbValue=Long.toString(rs.getTimestamp(name).getTime());
230 }
231
232 assertEquals(dbValue, jsonValue);
233 }
234 }
235
236 db.close();
237 } catch (SQLException e) {
238 System.out.println("Exception: "+e.toString()+":"+e.getMessage());
239 System.out.println("Exception: "+e.toString()+":"+e.getSQLState());
240 System.out.println("Exception: "+e.toString()+":"+e.getErrorCode());
241 fail("SQL Error:"+ExceptionUtil.getStackTrace(e));
242 } catch (Exception eOther) {
243 System.out.println("Other Exception: "+eOther.toString());
244 eOther.printStackTrace();
245 fail("Error:"+ExceptionUtil.getStackTrace(eOther));
246 } finally {
247 }
248 }
249
250
251
252
253
254 public void testJsonResult() {
255 Iterator i=testTables.keySet().iterator();
256 while (i.hasNext()) {
257 String tableName=(String)i.next();
258 verifyTableData(tableName);
259
260 }
261 }
262 }