1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.giraph.ooc;
2021import org.apache.giraph.utils.CallableFactory;
22import org.apache.giraph.utils.ThreadUtils;
23import org.apache.log4j.Logger;
2425import java.util.ArrayList;
26import java.util.List;
27import java.util.concurrent.Callable;
28import java.util.concurrent.ExecutionException;
29import java.util.concurrent.ExecutorService;
30import java.util.concurrent.Future;
31import java.util.concurrent.LinkedBlockingQueue;
32import java.util.concurrent.ThreadPoolExecutor;
33import java.util.concurrent.TimeUnit;
3435/**36 * Factory class to create IO threads for out-of-core engine.37 */38publicclassOutOfCoreIOCallableFactory {
39/** Class logger. */40privatestaticfinal Logger LOG =
41 Logger.getLogger(OutOfCoreIOCallableFactory.class);
42/** Out-of-core engine */43privatefinalOutOfCoreEngine oocEngine;
44/** Result of IO threads at the end of the computation */45privatefinal List<Future> results;
46/** Number of threads used for IO operations */47privatefinalint numIOThreads;
48/** Thread UncaughtExceptionHandler to use */49privatefinal Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
50/** Executor service for IO threads */51private ExecutorService outOfCoreIOExecutor;
5253/**54 * Constructor55 * @param oocEngine Out-of-core engine56 * @param numIOThreads Number of IO threads used57 * @param uncaughtExceptionHandler Thread UncaughtExceptionHandler to use58 */59publicOutOfCoreIOCallableFactory(OutOfCoreEngine oocEngine,
60int numIOThreads,
61 Thread.UncaughtExceptionHandler uncaughtExceptionHandler) {
62this.oocEngine = oocEngine;
63this.numIOThreads = numIOThreads;
64this.results = new ArrayList<>(numIOThreads);
65this.uncaughtExceptionHandler = uncaughtExceptionHandler;
66 }
6768/**69 * Creates/Launches IO threads70 */71publicvoid createCallable() {
72 CallableFactory<Void> outOfCoreIOCallableFactory =
73new CallableFactory<Void>() {
74 @Override
75public Callable<Void> newCallable(int callableId) {
76returnnewOutOfCoreIOCallable(oocEngine, callableId);
77 }
78 };
79 outOfCoreIOExecutor = new ThreadPoolExecutor(numIOThreads, numIOThreads, 0L,
80 TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
81 ThreadUtils.createThreadFactory("ooc-io-%d"));
8283for (int i = 0; i < numIOThreads; ++i) {
84 Future<Void> future = ThreadUtils.submitToExecutor(outOfCoreIOExecutor,
85 outOfCoreIOCallableFactory.newCallable(i), uncaughtExceptionHandler);
86 results.add(future);
87 }
88// Notify executor to not accept any more tasks89 outOfCoreIOExecutor.shutdown();
90 }
9192/**93 * Check whether all IO threads terminated gracefully.94 */95publicvoid shutdown() {
96boolean threadsTerminated = false;
97while (!threadsTerminated) {
98if (LOG.isInfoEnabled()) {
99 LOG.info("shutdown: waiting for IO threads to finish!");
100 }
101try {
102 threadsTerminated =
103 outOfCoreIOExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS);
104 } catch (InterruptedException e) {
105thrownew IllegalStateException("shutdown: caught " +
106"InterruptedException while waiting for IO threads to finish");
107 }
108 }
109for (int i = 0; i < numIOThreads; ++i) {
110try {
111// Check whether the tread terminated gracefully112 results.get(i).get();
113 } catch (InterruptedException e) {
114 LOG.error("shutdown: IO thread " + i + " was interrupted during its " +
115"execution");
116thrownew IllegalStateException(e);
117 } catch (ExecutionException e) {
118 LOG.error("shutdown: IO thread " + i + " threw an exception during " +
119"its execution");
120thrownew IllegalStateException(e);
121 }
122 }
123 }
124 }