Coverage Report - org.apache.giraph.ooc.OutOfCoreIOCallableFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
OutOfCoreIOCallableFactory
0%
0/37
0%
0/8
0
OutOfCoreIOCallableFactory$1
0%
0/2
N/A
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * 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 and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 package org.apache.giraph.ooc;
 20  
 
 21  
 import org.apache.giraph.utils.CallableFactory;
 22  
 import org.apache.giraph.utils.ThreadUtils;
 23  
 import org.apache.log4j.Logger;
 24  
 
 25  
 import java.util.ArrayList;
 26  
 import java.util.List;
 27  
 import java.util.concurrent.Callable;
 28  
 import java.util.concurrent.ExecutionException;
 29  
 import java.util.concurrent.ExecutorService;
 30  
 import java.util.concurrent.Future;
 31  
 import java.util.concurrent.LinkedBlockingQueue;
 32  
 import java.util.concurrent.ThreadPoolExecutor;
 33  
 import java.util.concurrent.TimeUnit;
 34  
 
 35  
 /**
 36  
  * Factory class to create IO threads for out-of-core engine.
 37  
  */
 38  0
 public class OutOfCoreIOCallableFactory {
 39  
   /** Class logger. */
 40  0
   private static final Logger LOG =
 41  0
       Logger.getLogger(OutOfCoreIOCallableFactory.class);
 42  
   /** Out-of-core engine */
 43  
   private final OutOfCoreEngine oocEngine;
 44  
   /** Result of IO threads at the end of the computation */
 45  
   private final List<Future> results;
 46  
   /** Number of threads used for IO operations */
 47  
   private final int numIOThreads;
 48  
   /** Thread UncaughtExceptionHandler to use */
 49  
   private final Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
 50  
   /** Executor service for IO threads */
 51  
   private ExecutorService outOfCoreIOExecutor;
 52  
 
 53  
   /**
 54  
    * Constructor
 55  
    * @param oocEngine Out-of-core engine
 56  
    * @param numIOThreads Number of IO threads used
 57  
    * @param uncaughtExceptionHandler Thread UncaughtExceptionHandler to use
 58  
    */
 59  
   public OutOfCoreIOCallableFactory(OutOfCoreEngine oocEngine,
 60  
       int numIOThreads,
 61  0
       Thread.UncaughtExceptionHandler uncaughtExceptionHandler) {
 62  0
     this.oocEngine = oocEngine;
 63  0
     this.numIOThreads = numIOThreads;
 64  0
     this.results = new ArrayList<>(numIOThreads);
 65  0
     this.uncaughtExceptionHandler = uncaughtExceptionHandler;
 66  0
   }
 67  
 
 68  
   /**
 69  
    * Creates/Launches IO threads
 70  
    */
 71  
   public void createCallable() {
 72  0
     CallableFactory<Void> outOfCoreIOCallableFactory =
 73  0
       new CallableFactory<Void>() {
 74  
         @Override
 75  
         public Callable<Void> newCallable(int callableId) {
 76  0
           return new OutOfCoreIOCallable(oocEngine, callableId);
 77  
         }
 78  
       };
 79  0
     outOfCoreIOExecutor = new ThreadPoolExecutor(numIOThreads, numIOThreads, 0L,
 80  
         TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
 81  0
         ThreadUtils.createThreadFactory("ooc-io-%d"));
 82  
 
 83  0
     for (int i = 0; i < numIOThreads; ++i) {
 84  0
       Future<Void> future = ThreadUtils.submitToExecutor(outOfCoreIOExecutor,
 85  0
           outOfCoreIOCallableFactory.newCallable(i), uncaughtExceptionHandler);
 86  0
       results.add(future);
 87  
     }
 88  
     // Notify executor to not accept any more tasks
 89  0
     outOfCoreIOExecutor.shutdown();
 90  0
   }
 91  
 
 92  
   /**
 93  
    * Check whether all IO threads terminated gracefully.
 94  
    */
 95  
   public void shutdown() {
 96  0
     boolean threadsTerminated = false;
 97  0
     while (!threadsTerminated) {
 98  0
       if (LOG.isInfoEnabled()) {
 99  0
         LOG.info("shutdown: waiting for IO threads to finish!");
 100  
       }
 101  
       try {
 102  0
         threadsTerminated =
 103  0
             outOfCoreIOExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS);
 104  0
       } catch (InterruptedException e) {
 105  0
         throw new IllegalStateException("shutdown: caught " +
 106  
             "InterruptedException while waiting for IO threads to finish");
 107  0
       }
 108  
     }
 109  0
     for (int i = 0; i < numIOThreads; ++i) {
 110  
       try {
 111  
         // Check whether the tread terminated gracefully
 112  0
         results.get(i).get();
 113  0
       } catch (InterruptedException e) {
 114  0
         LOG.error("shutdown: IO thread " + i + " was interrupted during its " +
 115  
             "execution");
 116  0
         throw new IllegalStateException(e);
 117  0
       } catch (ExecutionException e) {
 118  0
         LOG.error("shutdown: IO thread " + i + " threw an exception during " +
 119  
             "its execution");
 120  0
         throw new IllegalStateException(e);
 121  0
       }
 122  
     }
 123  0
   }
 124  
 }