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