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.evaluator; 020 021import org.apache.commons.lang3.Validate; 022import org.apache.reef.annotations.audience.Private; 023import org.apache.reef.runtime.common.driver.parameters.EvaluatorIdlenessThreadPoolSize; 024import org.apache.reef.runtime.common.driver.parameters.EvaluatorIdlenessWaitInMilliseconds; 025import org.apache.reef.tang.annotations.Parameter; 026import org.apache.reef.wake.impl.DefaultThreadFactory; 027 028import javax.inject.Inject; 029import java.util.concurrent.ExecutorService; 030import java.util.concurrent.Executors; 031import java.util.logging.Level; 032import java.util.logging.Logger; 033 034/** 035 * Runs threads in a thread pool to check the completion of Evaluators on the closing 036 * of an {@link EvaluatorManager} in order to trigger Evaluator idleness checks. 037 */ 038@Private 039public final class EvaluatorIdlenessThreadPool { 040 private static final Logger LOG = Logger.getLogger(EvaluatorIdlenessThreadPool.class.getName()); 041 042 private final ExecutorService executor; 043 private final long waitInMillis; 044 045 @Inject 046 private EvaluatorIdlenessThreadPool(@Parameter(EvaluatorIdlenessThreadPoolSize.class) final int numThreads, 047 @Parameter(EvaluatorIdlenessWaitInMilliseconds.class) final long waitInMillis) { 048 049 Validate.isTrue(waitInMillis >= 0, "EvaluatorIdlenessWaitInMilliseconds must be configured to be >= 0"); 050 Validate.isTrue(numThreads > 0, "EvaluatorIdlenessThreadPoolSize must be configured to be > 0"); 051 052 this.waitInMillis = waitInMillis; 053 this.executor = Executors.newFixedThreadPool( 054 numThreads, new DefaultThreadFactory(EvaluatorIdlenessThreadPool.class.getName())); 055 } 056 057 /** 058 * Runs a check in the ThreadPool for the {@link EvaluatorManager} to wait for it to finish its 059 * Event Handling and check its idleness source. 060 * @param manager the {@link EvaluatorManager} 061 */ 062 void runCheckAsync(final EvaluatorManager manager) { 063 executor.submit(new Runnable() { 064 @Override 065 public void run() { 066 while (!manager.isClosed()) { 067 try { 068 Thread.sleep(waitInMillis); 069 } catch (final InterruptedException e) { 070 LOG.log(Level.SEVERE, "Thread interrupted while waiting for Evaluator to finish."); 071 throw new RuntimeException(e); 072 } 073 } 074 075 manager.checkIdlenessSource(); 076 LOG.log(Level.FINE, "Evaluator " + manager.getId() + " has finished."); 077 } 078 }); 079 } 080}