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.List; 030import java.util.concurrent.ExecutorService; 031import java.util.concurrent.Executors; 032import java.util.concurrent.TimeUnit; 033import java.util.logging.Level; 034import java.util.logging.Logger; 035 036/** 037 * Runs threads in a thread pool to check the completion of Evaluators on the closing 038 * of an {@link EvaluatorManager} in order to trigger Evaluator idleness checks. 039 */ 040@Private 041public final class EvaluatorIdlenessThreadPool implements AutoCloseable { 042 043 private static final Logger LOG = Logger.getLogger(EvaluatorIdlenessThreadPool.class.getName()); 044 045 private final ExecutorService executor; 046 private final long waitInMillis; 047 048 @Inject 049 private EvaluatorIdlenessThreadPool( 050 @Parameter(EvaluatorIdlenessThreadPoolSize.class) final int numThreads, 051 @Parameter(EvaluatorIdlenessWaitInMilliseconds.class) final long waitInMillis) { 052 053 Validate.isTrue(waitInMillis >= 0, "EvaluatorIdlenessWaitInMilliseconds must be configured to be >= 0"); 054 Validate.isTrue(numThreads > 0, "EvaluatorIdlenessThreadPoolSize must be configured to be > 0"); 055 056 this.waitInMillis = waitInMillis; 057 058 this.executor = Executors.newFixedThreadPool( 059 numThreads, new DefaultThreadFactory(this.getClass().getSimpleName())); 060 } 061 062 /** 063 * Runs a check in the ThreadPool for the {@link EvaluatorManager} to wait for it to finish its 064 * Event Handling and check its idleness source. 065 * @param manager the {@link EvaluatorManager} 066 */ 067 void runCheckAsync(final EvaluatorManager manager) { 068 069 final String evaluatorId = manager.getId(); 070 LOG.log(Level.FINEST, "Idle check for Evaluator: {0}", manager); 071 072 this.executor.submit(new Runnable() { 073 074 @Override 075 public void run() { 076 077 LOG.log(Level.FINEST, "Idle check for Evaluator {0} - begin", evaluatorId); 078 079 while (!manager.isClosed()) { 080 try { 081 082 LOG.log(Level.FINEST, 083 "Waiting for Evaluator {0} to close: Sleep for {1} ms", 084 new Object[] {evaluatorId, waitInMillis}); 085 086 Thread.sleep(waitInMillis); 087 088 } catch (final InterruptedException e) { 089 LOG.log(Level.SEVERE, "Thread interrupted while waiting for Evaluator to finish."); 090 throw new RuntimeException(e); 091 } 092 } 093 094 manager.checkIdlenessSource(); 095 096 LOG.log(Level.FINEST, "Idle check for Evaluator {0} - end", evaluatorId); 097 } 098 099 @Override 100 public String toString() { 101 return "CheckIdle: " + evaluatorId; 102 } 103 }); 104 } 105 106 /** 107 * Shutdown the thread pool of idleness checkers. 108 */ 109 @Override 110 public void close() { 111 112 LOG.log(Level.FINE, "EvaluatorIdlenessThreadPool shutdown: begin"); 113 114 this.executor.shutdown(); 115 116 boolean isTerminated = false; 117 try { 118 isTerminated = this.executor.awaitTermination(this.waitInMillis, TimeUnit.MILLISECONDS); 119 } catch (final InterruptedException ex) { 120 LOG.log(Level.WARNING, "EvaluatorIdlenessThreadPool shutdown: Interrupted", ex); 121 } 122 123 if (isTerminated) { 124 LOG.log(Level.FINE, "EvaluatorIdlenessThreadPool shutdown: Terminated successfully"); 125 } else { 126 final List<Runnable> pendingJobs = this.executor.shutdownNow(); 127 LOG.log(Level.SEVERE, "EvaluatorIdlenessThreadPool shutdown: {0} jobs after timeout", pendingJobs.size()); 128 LOG.log(Level.FINE, "EvaluatorIdlenessThreadPool shutdown: pending jobs: {0}", pendingJobs); 129 } 130 } 131}