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  package org.apache.hadoop.chukwa.rest.resource;
19  
20  import java.nio.charset.Charset;
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  import java.util.NoSuchElementException;
25  import java.util.regex.Matcher;
26  import java.util.regex.Pattern;
27  
28  import javax.ws.rs.GET;
29  import javax.ws.rs.Path;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.hadoop.chukwa.Chunk;
34  import org.apache.hadoop.chukwa.dataloader.SocketDataLoader;
35  import org.apache.hadoop.chukwa.rest.bean.ClientTraceBean;
36  
37  /**
38   * Client Trace REST API for parsing client trace log file and convert
39   * data into consumable format for web browser and web services.
40   */
41  @Path("clienttrace")
42  public class ClientTrace {
43    protected static final Log log = LogFactory.getLog(ClientTrace.class);
44    private static SocketDataLoader sdl = null;
45    // Client trace log file pattern
46    private final Pattern pattern =
47      Pattern.compile("(.+?) (.+?),(.+?) (.+?) src\\: /?(.+?):(.+?), dest\\: /?(.+?):(.+?), bytes\\: (\\d+), op\\: (.+?), cli(.+?)");
48  
49    /**
50     * Get a list of the most recent client trace activities.
51     * The extracted elements are:
52     * 
53     * date   - Timestamp of the activity happened.
54     * action - Operation type: HDFS_READ, HDFS_WRITE, or MAPRED_SHUFFLE.
55     * src    - Source IP address
56     * dest   - Destination IP address
57     * size   - Size of the data payload.
58     * @return 
59     * 
60     */
61    @GET
62    public List<ClientTraceBean> getTrace() {
63      if(sdl==null) {
64        sdl = new SocketDataLoader("ClientTrace");
65      } else if(!sdl.running()) {
66        sdl.start();
67      }
68  
69      List<ClientTraceBean> list = new ArrayList<ClientTraceBean>();
70      try {
71        Collection<Chunk> clist = sdl.read();
72        for(Chunk c : clist) {
73          if(c!=null && c.getData()!=null) {
74            String action = "";
75            long size = 0;
76            String data = new String(c.getData(), Charset.forName("UTF-8"));
77            String[] entries = data.split("\n");
78            for(String entry : entries) {
79              Matcher m = pattern.matcher(entry);
80              if(m.matches()) {
81                ClientTraceBean ctb = new ClientTraceBean();
82                size = Long.parseLong(m.group(9));
83                action = m.group(10);
84                StringBuilder date = new StringBuilder();
85                date.append(m.group(1));
86                date.append(" ");
87                date.append(m.group(2));
88                ctb.setDate(date.toString());
89                ctb.setSrc(m.group(5));
90                ctb.setDest(m.group(7));
91                ctb.setAction(action);
92                ctb.setSize(size);          
93                list.add(ctb);            
94              } else {
95                log.error("Unparsable line: "+entry);
96              }
97            }
98          }
99        }
100     } catch(NoSuchElementException e) {
101       log.debug("No data available for client trace.");
102     }
103     
104     return list;
105   }
106 
107 }