This project has retired. For details please refer to its Attic page.
FileTailer xref
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  
19  package org.apache.hadoop.chukwa.datacollection.adaptor.filetailer;
20  
21  
22  import java.util.List;
23  import java.util.concurrent.CopyOnWriteArrayList;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.log4j.Logger;
26  
27  /**
28   * A shared thread used by all FileTailingAdaptors.
29   * 
30   * For now, it tries each file in succession. If it gets through every file
31   * within two seconds, and no more data remains, it will sleep.
32   * 
33   * If there was still data available in any file, the adaptor will loop again.
34   * 
35   */
36  class FileTailer extends Thread {
37    static Logger log = Logger.getLogger(FileTailer.class);
38  
39    private List<LWFTAdaptor> adaptors;
40    private volatile boolean isRunning = true;
41  //  ChunkQueue eq; // not private -- useful for file tailing adaptor classes
42  
43    /**
44     * How often to tail each file.
45     */
46    int DEFAULT_SAMPLE_PERIOD_MS = 1000 * 2;
47    int SAMPLE_PERIOD_MS = DEFAULT_SAMPLE_PERIOD_MS;
48  //  private Configuration conf = null;
49    public static final int MAX_SAMPLE_PERIOD = 60 * 1000;
50  
51    FileTailer(Configuration conf) {
52   //   this.conf = conf;
53      SAMPLE_PERIOD_MS = conf.getInt(
54          "chukwaAgent.adaptor.context.switch.time",
55          DEFAULT_SAMPLE_PERIOD_MS);
56  //    eq = DataFactory.getInstance().getEventQueue();
57  
58      // iterations are much more common than adding a new adaptor
59      adaptors = new CopyOnWriteArrayList<LWFTAdaptor>();
60  
61      this.setDaemon(true);
62      start();// start the file-tailing thread
63    }
64  
65    // called by FileTailingAdaptor, only
66    void startWatchingFile(LWFTAdaptor f) {
67      adaptors.add(f);
68    }
69  
70    // called by FileTailingAdaptor, only
71    void stopWatchingFile(LWFTAdaptor f) {
72      adaptors.remove(f);
73    }
74  
75    public void run() {
76      while (isRunning) {
77        try {
78          boolean shouldISleep = true;
79          long startTime = System.currentTimeMillis();
80          for (LWFTAdaptor f : adaptors) {
81            boolean hasMoreData = f.tailFile();
82            shouldISleep &= !hasMoreData;
83          }
84          long timeToReadFiles = System.currentTimeMillis() - startTime;
85          if(timeToReadFiles > MAX_SAMPLE_PERIOD)
86            log.warn("took " + timeToReadFiles + " ms to check all files being tailed");
87          if (timeToReadFiles < SAMPLE_PERIOD_MS && shouldISleep) {
88            Thread.sleep(SAMPLE_PERIOD_MS);
89          }
90        } catch (Throwable e) {
91          log.warn("Exception in FileTailer, while loop", e);
92          e.printStackTrace();
93        }
94      }
95    }
96  
97  }