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; 020 021import org.apache.reef.driver.parameters.DriverRestartHandler; 022import org.apache.reef.driver.parameters.ServiceDriverRestartedHandlers; 023import org.apache.reef.driver.restart.DriverRestartManager; 024import org.apache.reef.driver.restart.DriverRestarted; 025import org.apache.reef.exception.DriverFatalRuntimeException; 026import org.apache.reef.tang.annotations.Parameter; 027import org.apache.reef.wake.EventHandler; 028import org.apache.reef.wake.time.event.StartTime; 029 030import javax.inject.Inject; 031import java.util.ArrayList; 032import java.util.List; 033import java.util.Set; 034import java.util.logging.Level; 035import java.util.logging.Logger; 036 037/** 038 * This is bound to the start event of the clock and dispatches it to the appropriate application code. 039 */ 040public final class DriverStartHandler implements EventHandler<StartTime> { 041 042 private static final Logger LOG = Logger.getLogger(DriverStartHandler.class.getName()); 043 044 private final Set<EventHandler<StartTime>> startHandlers; 045 private final Set<EventHandler<DriverRestarted>> restartHandlers; 046 private final Set<EventHandler<DriverRestarted>> serviceRestartHandlers; 047 048 private final DriverRestartManager driverRestartManager; 049 050 @Inject 051 private DriverStartHandler( 052 @Parameter(org.apache.reef.driver.parameters.DriverStartHandler.class) 053 final Set<EventHandler<StartTime>> startHandlers, 054 @Parameter(DriverRestartHandler.class) 055 final Set<EventHandler<DriverRestarted>> restartHandlers, 056 @Parameter(ServiceDriverRestartedHandlers.class) 057 final Set<EventHandler<DriverRestarted>> serviceRestartHandlers, 058 final DriverRestartManager driverRestartManager) { 059 060 this.startHandlers = startHandlers; 061 this.restartHandlers = restartHandlers; 062 this.serviceRestartHandlers = serviceRestartHandlers; 063 this.driverRestartManager = driverRestartManager; 064 065 LOG.log(Level.FINE, 066 "Instantiated DriverStartHandler: StartHandlers:{0} RestartHandlers:{1} ServiceRestartHandlers:{2}", 067 new Object[] {this.startHandlers, this.restartHandlers, this.serviceRestartHandlers}); 068 } 069 070 @Override 071 public void onNext(final StartTime startTime) { 072 if (this.driverRestartManager.detectRestart()) { 073 this.onRestart(startTime); 074 } else { 075 this.onStart(startTime); 076 } 077 } 078 079 private void onRestart(final StartTime startTime) { 080 081 if (this.restartHandlers.isEmpty()) { 082 throw new DriverFatalRuntimeException("Driver restarted, but no ON_DRIVER_RESTART handler is bound."); 083 } 084 085 final List<EventHandler<DriverRestarted>> orderedRestartHandlers = 086 new ArrayList<>(this.serviceRestartHandlers.size() + this.restartHandlers.size()); 087 088 orderedRestartHandlers.addAll(this.serviceRestartHandlers); 089 orderedRestartHandlers.addAll(this.restartHandlers); 090 091 // This can only be called after calling client restart handlers because REEF.NET 092 // JobDriver requires making this call to set up the InterOp handlers. 093 this.driverRestartManager.onRestart(startTime, orderedRestartHandlers); 094 } 095 096 private void onStart(final StartTime startTime) { 097 for (final EventHandler<StartTime> startHandler : this.startHandlers) { 098 startHandler.onNext(startTime); 099 } 100 } 101}