001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.runtime.common.driver.idle; 020 021import org.apache.reef.driver.parameters.DriverIdleSources; 022import org.apache.reef.runtime.common.driver.DriverStatusManager; 023import org.apache.reef.tang.InjectionFuture; 024import org.apache.reef.tang.annotations.Parameter; 025 026import javax.inject.Inject; 027import java.util.Set; 028import java.util.logging.Level; 029import java.util.logging.Logger; 030 031/** 032 * Handles the various sources for driver idleness and forwards decisions to DriverStatusManager. 033 */ 034public final class DriverIdleManager { 035 private static final Logger LOG = Logger.getLogger(DriverIdleManager.class.getName()); 036 private static final Level IDLE_REASONS_LEVEL = Level.FINEST; 037 private final Set<DriverIdlenessSource> idlenessSources; 038 private final InjectionFuture<DriverStatusManager> driverStatusManager; 039 040 @Inject 041 DriverIdleManager(@Parameter(DriverIdleSources.class) final Set<DriverIdlenessSource> idlenessSources, 042 final InjectionFuture<DriverStatusManager> driverStatusManager) { 043 this.idlenessSources = idlenessSources; 044 this.driverStatusManager = driverStatusManager; 045 } 046 047 public synchronized void onPotentiallyIdle(final IdleMessage reason) { 048 synchronized (driverStatusManager.get()) { 049 if (this.driverStatusManager.get().isShuttingDownOrFailing()) { 050 LOG.log(IDLE_REASONS_LEVEL, "Ignoring idle call from [{0}] for reason [{1}]", 051 new Object[]{reason.getComponentName(), reason.getReason()}); 052 } else { 053 boolean isIdle = true; 054 LOG.log(IDLE_REASONS_LEVEL, "Checking for idle because {0} reported idleness for reason [{1}]", 055 new Object[]{reason.getComponentName(), reason.getReason()}); 056 057 058 for (final DriverIdlenessSource idlenessSource : this.idlenessSources) { 059 final IdleMessage idleMessage = idlenessSource.getIdleStatus(); 060 LOG.log(IDLE_REASONS_LEVEL, "[{0}] is reporting {1} because [{2}].", 061 new Object[]{idleMessage.getComponentName(), idleMessage.isIdle() ? "idle" : "not idle", 062 idleMessage.getReason()} 063 ); 064 isIdle &= idleMessage.isIdle(); 065 } 066 067 if (isIdle) { 068 LOG.log(Level.INFO, "All components indicated idle. Initiating Driver shutdown."); 069 this.driverStatusManager.get().onComplete(); 070 } 071 } 072 } 073 } 074}