node启动时, 会创建ResourceWatcherService
final ResourceWatcherService resourceWatcherService = new ResourceWatcherService(settings, threadPool);
ResourceWatcherService包含三个资源监视器ResourceMonitor,分别为低频、中频和高频,分别由属性resource.reload.interval.low, resource.reload.interval.medium, resource.reload.interval.high配置。资源监视器的总开头由属性resource.reload.enabled配置。
通过add(W watcher)方法向资源监视器添加ResourceWatcher得到WatcherHandler。
频率枚举为
public enum Frequency {
/**
* Defaults to 5 seconds
*/
HIGH(TimeValue.timeValueSeconds(5)),
/**
* Defaults to 30 seconds
*/
MEDIUM(TimeValue.timeValueSeconds(30)),
/**
* Defaults to 60 seconds
*/
LOW(TimeValue.timeValueSeconds(60));
final TimeValue interval;
Frequency(TimeValue interval) {
this.interval = interval;
}
}
添加ResourceWatcher
public <W extends ResourceWatcher> WatcherHandle<W> add(W watcher) throws IOException {
return add(watcher, Frequency.MEDIUM);
}
public <W extends ResourceWatcher> WatcherHandle<W> add(W watcher, Frequency frequency) throws IOException {
watcher.init();
switch (frequency) {
case LOW:
return lowMonitor.add(watcher);
case MEDIUM:
return mediumMonitor.add(watcher);
case HIGH:
return highMonitor.add(watcher);
default:
throw new IllegalArgumentException("Unknown frequency [" + frequency + "]");
}
}
在调度时,会调用ResourceMonitor的run方法,遍历ResourceWatcher,执行checkAndNotify方法
public synchronized void run() {
for (ResourceWatcher watcher : watchers) {
try {
watcher.checkAndNotify();
} catch (IOException e) {
logger.trace("failed to check resource watcher", e);
}
}
}
其类结构图为