1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.hadoop.chukwa.datacollection.adaptor.filetailer;
202122import java.util.List;
23import java.util.concurrent.CopyOnWriteArrayList;
24import org.apache.hadoop.conf.Configuration;
25import org.apache.log4j.Logger;
2627/**28 * A shared thread used by all FileTailingAdaptors.29 * 30 * For now, it tries each file in succession. If it gets through every file31 * 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 */36classFileTailerextends Thread {
37static Logger log = Logger.getLogger(FileTailer.class);
3839private List<LWFTAdaptor> adaptors;
40privatevolatileboolean isRunning = true;
41// ChunkQueue eq; // not private -- useful for file tailing adaptor classes4243/**44 * How often to tail each file.45 */46int DEFAULT_SAMPLE_PERIOD_MS = 1000 * 2;
47int SAMPLE_PERIOD_MS = DEFAULT_SAMPLE_PERIOD_MS;
48// private Configuration conf = null;49publicstaticfinalint MAX_SAMPLE_PERIOD = 60 * 1000;
5051FileTailer(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();5758// iterations are much more common than adding a new adaptor59 adaptors = new CopyOnWriteArrayList<LWFTAdaptor>();
6061this.setDaemon(true);
62 start();// start the file-tailing thread63 }
6465// called by FileTailingAdaptor, only66void startWatchingFile(LWFTAdaptor f) {
67 adaptors.add(f);
68 }
6970// called by FileTailingAdaptor, only71void stopWatchingFile(LWFTAdaptor f) {
72 adaptors.remove(f);
73 }
7475publicvoid run() {
76while (isRunning) {
77try {
78boolean shouldISleep = true;
79long startTime = System.currentTimeMillis();
80for (LWFTAdaptor f : adaptors) {
81boolean hasMoreData = f.tailFile();
82 shouldISleep &= !hasMoreData;
83 }
84long timeToReadFiles = System.currentTimeMillis() - startTime;
85if(timeToReadFiles > MAX_SAMPLE_PERIOD)
86 log.warn("took " + timeToReadFiles + " ms to check all files being tailed");
87if (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 }
9697 }