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.datacollection.adaptor.filetailer;
19  
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.PrintWriter;
25  import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
26  import org.apache.hadoop.chukwa.Chunk;
27  import org.apache.hadoop.chukwa.datacollection.adaptor.*;
28  import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent;
29  import org.apache.hadoop.chukwa.datacollection.controller.ChukwaAgentController;
30  import org.apache.hadoop.chukwa.datacollection.connector.ChunkCatcherConnector;
31  import org.apache.hadoop.conf.Configuration;
32  import junit.framework.TestCase;
33  
34  public class TestStartAtOffset extends TestCase {
35  
36    ChunkCatcherConnector chunks;
37  
38    public TestStartAtOffset() {
39      chunks = new ChunkCatcherConnector();
40      chunks.start();
41    }
42    
43  
44    public void startAtOffset() throws IOException, InterruptedException,
45        ChukwaAgent.AlreadyRunningException {
46      Configuration conf = new Configuration();
47      conf.set("chukwaAgent.control.port", "0");
48      conf.setInt("chukwaAgent.adaptor.context.switch.time", 100);
49      ChukwaAgent agent = new ChukwaAgent(conf);
50      File testFile = makeTestFile();
51      int startOffset = 0; // skip first line
52      String adaptorId = agent
53         .processAddCommand("add org.apache.hadoop.chukwa.datacollection.adaptor"+
54              "filetailer.CharFileTailingAdaptorUTF8 "
55              + "lines " + startOffset + " " + testFile + " " + startOffset);
56      assertTrue(adaptorId != null);
57      System.out.println("getting a chunk...");
58      Chunk c = chunks.waitForAChunk();
59      System.out.println("got chunk");
60      while (!c.getDataType().equals("lines")) {
61        c = chunks.waitForAChunk();
62      }
63      assertTrue(c.getSeqID() == testFile.length() + startOffset);
64      System.out.println("RecordOffsets length:" + c.getRecordOffsets().length);
65      assertTrue(c.getRecordOffsets().length == 80); // 80 lines in the file.
66      int recStart = 0;
67      for (int rec = 0; rec < c.getRecordOffsets().length; ++rec) {
68        String record = new String(c.getData(), recStart,
69            c.getRecordOffsets()[rec] - recStart + 1);
70        System.out.println("record " + rec + " was: " + record);
71        assertTrue(record.equals(rec + " abcdefghijklmnopqrstuvwxyz\n"));
72        recStart = c.getRecordOffsets()[rec] + 1;
73      }
74      assertTrue(c.getDataType().equals("lines"));
75      agent.stopAdaptor(adaptorId, false);
76      agent.shutdown();
77    }
78  
79    public void testStartAfterOffset() throws IOException, InterruptedException,
80        ChukwaAgent.AlreadyRunningException {
81      Configuration conf = new Configuration();
82      conf.set("chukwaAgent.control.port", "0");
83      conf.setInt("chukwaAgent.adaptor.context.switch.time", 100);
84      ChukwaAgent agent = new ChukwaAgent(conf);
85      File testFile = makeTestFile();
86      int startOffset = 0;
87      String adaptorId = agent
88          .processAddCommand("add org.apache.hadoop.chukwa.datacollection.adaptor.filetailer.CharFileTailingAdaptorUTF8 "
89              + "lines "
90              + startOffset
91              + " "
92              + testFile
93              + " "
94              + (startOffset + 29));
95      assertTrue(adaptorId != null);
96      System.out.println("getting a chunk...");
97      Chunk c = chunks.waitForAChunk();
98      System.out.println("got chunk");
99      while (!c.getDataType().equals("lines")) {
100       c = chunks.waitForAChunk();
101     }
102     assertTrue(c.getSeqID() == testFile.length() + startOffset);
103 
104     assertTrue(c.getRecordOffsets().length == 79);// 80 lines in test file,
105                                                   // minus the one we skipped
106     int recStart = 0;
107     for (int rec = 0; rec < c.getRecordOffsets().length; ++rec) {
108       String record = new String(c.getData(), recStart,
109           c.getRecordOffsets()[rec] - recStart + 1);
110       System.out.println("record " + rec + " was: " + record);
111       assertTrue(record.equals((rec + 1) + " abcdefghijklmnopqrstuvwxyz\n"));
112       recStart = c.getRecordOffsets()[rec] + 1;
113     }
114     assertTrue(c.getDataType().equals("lines"));
115     agent.stopAdaptor(adaptorId, false);
116     agent.shutdown();
117   }
118 
119   private File makeTestFile() throws IOException {
120     return org.apache.hadoop.chukwa.util.TempFileUtil.makeTestFile();
121   }
122 
123 }