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.io.data.loading.api; 020 021import org.apache.reef.driver.evaluator.EvaluatorRequest; 022import org.apache.reef.driver.evaluator.EvaluatorRequestor; 023import org.apache.reef.wake.EventHandler; 024 025import java.util.concurrent.CountDownLatch; 026import java.util.logging.Level; 027import java.util.logging.Logger; 028 029public class ResourceRequestHandler implements EventHandler<EvaluatorRequest> { 030 031 private static final Logger LOG = Logger.getLogger(ResourceRequestHandler.class.getName()); 032 033 private final EvaluatorRequestor requestor; 034 035 private CountDownLatch resourceRequestGate = new CountDownLatch(1); 036 037 public ResourceRequestHandler(final EvaluatorRequestor requestor) { 038 this.requestor = requestor; 039 } 040 041 public void releaseResourceRequestGate() { 042 LOG.log(Level.FINE, "Releasing Gate"); 043 this.resourceRequestGate.countDown(); 044 } 045 046 @Override 047 public void onNext(final EvaluatorRequest request) { 048 try { 049 050 LOG.log(Level.FINE, 051 "Processing a request with count: {0} - Waiting for gate to be released", 052 request.getNumber()); 053 054 this.resourceRequestGate.await(); 055 056 LOG.log(Level.FINE, "Gate released. Submitting request: {0}", request); 057 this.resourceRequestGate = new CountDownLatch(1); 058 this.requestor.submit(request); 059 060 } catch (final InterruptedException ex) { 061 LOG.log(Level.FINEST, "Interrupted", ex); 062 } 063 } 064}