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  import java.io.File;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.io.PrintWriter;
24  import junit.framework.Assert;
25  import junit.framework.TestCase;
26  import org.apache.hadoop.chukwa.conf.ChukwaConfiguration;
27  import org.apache.hadoop.chukwa.datacollection.agent.ChukwaAgent;
28  import org.apache.hadoop.chukwa.datacollection.controller.ChukwaAgentController;
29  import org.apache.hadoop.conf.Configuration;
30  
31  public class TestFileExpirationPolicy extends TestCase {
32  
33    public void testExpiration() {
34      ChukwaAgent agent = null;
35  
36      try {
37        Configuration conf = new ChukwaConfiguration();
38        conf.set("chukwaAgent.control.port", "0");
39        agent = new ChukwaAgent(conf);
40  
41        FileTailingAdaptor.GRACEFUL_PERIOD = 30 * 1000;
42  
43        String adaptorId = agent
44            .processAddCommand("add org.apache.hadoop.chukwa.datacollection.adaptor.filetailer.CharFileTailingAdaptorUTF8NewLineEscaped MyType 0 /myWrongPath"
45                + System.currentTimeMillis() + " 0");
46  
47        assertTrue(adaptorId != null);
48  
49        assertNotNull(agent.getAdaptor(adaptorId));
50  
51        Thread.sleep(FileTailingAdaptor.GRACEFUL_PERIOD + 10000);
52        assertNull(agent.getAdaptor(adaptorId));
53  
54      } catch (Exception e) {
55        Assert.fail("Exception in TestFileExpirationPolicy");
56      } finally {
57        if (agent != null) {
58          agent.shutdown();
59          try {
60            Thread.sleep(2000);
61          } catch (Exception ex) {
62          }
63        }
64      }
65  
66    }
67  
68    public void testExpirationOnFileThatHasBennDeleted() {
69      ChukwaAgent agent = null;
70      File testFile = null;
71      try {
72  
73        File tempDir = new File(System.getProperty("test.build.data", "/tmp"));
74        if (!tempDir.exists()) {
75          tempDir.mkdirs();
76        }
77        String logFile = tempDir.getPath() + "/chukwatestExpiration.txt";
78        testFile = makeTestFile(logFile, 8000);
79  
80        Configuration conf = new ChukwaConfiguration();
81        conf.set("chukwaAgent.control.port", "0");
82        agent = new ChukwaAgent(conf);
83        // Remove any adaptor left over from previous run
84  
85        ChukwaAgentController cli = new ChukwaAgentController("localhost", agent.getControllerPort());
86        cli.removeAll();
87        // sleep for some time to make sure we don't get chunk from existing
88        // streams
89        Thread.sleep(5000);
90  
91        assertTrue(testFile.canRead() == true);
92  
93        FileTailingAdaptor.GRACEFUL_PERIOD = 30 * 1000;
94        String adaptorId = agent
95            .processAddCommand("add org.apache.hadoop.chukwa.datacollection.adaptor.filetailer.CharFileTailingAdaptorUTF8NewLineEscaped MyType 0 "
96                + logFile + " 0");
97  
98        assertTrue(adaptorId != null);
99  
100       assertNotNull(agent.getAdaptor(adaptorId));
101 
102       Thread.sleep(10000);
103       testFile.delete();
104 
105       Thread.sleep(FileTailingAdaptor.GRACEFUL_PERIOD + 10000);
106       assertNull(agent.getAdaptor(adaptorId));
107       agent.shutdown();
108       Thread.sleep(2000);
109     } catch (Exception e) {
110       Assert.fail("Exception in TestFileExpirationPolicy");
111     } finally {
112       if (agent != null) {
113         agent.shutdown();
114         try {
115           Thread.sleep(2000);
116         } catch (Exception ex) {
117         }
118       }
119     }
120   }
121 
122   private File makeTestFile(String name, int size) throws IOException {
123     File tmpOutput = new File(name);
124     FileOutputStream fos = new FileOutputStream(tmpOutput);
125 
126     PrintWriter pw = new PrintWriter(fos);
127     for (int i = 0; i < size; ++i) {
128       pw.print(i + " ");
129       pw.println("abcdefghijklmnopqrstuvwxyz");
130     }
131     pw.flush();
132     pw.close();
133     return tmpOutput;
134   }
135 }