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.vortex.protocol.mastertoworker; 020 021import org.apache.reef.annotations.Unstable; 022import org.apache.reef.annotations.audience.Private; 023import org.apache.reef.vortex.api.VortexFunction; 024 025/** 026 * Request to execute a tasklet. 027 */ 028@Unstable 029@Private 030public final class TaskletExecutionRequest<TInput, TOutput> implements MasterToWorkerRequest { 031 private int taskletId; 032 private VortexFunction<TInput, TOutput> userFunction; 033 private TInput input; 034 035 /** 036 * @return the type of this MasterToWorkerRequest. 037 */ 038 @Override 039 public Type getType() { 040 return Type.ExecuteTasklet; 041 } 042 043 /** 044 * No-arg constructor required for Kryo to serialize/deserialize. 045 */ 046 TaskletExecutionRequest() { 047 } 048 049 /** 050 * Request from Vortex Master to Vortex Worker to execute a tasklet. 051 */ 052 public TaskletExecutionRequest(final int taskletId, 053 final VortexFunction<TInput, TOutput> userFunction, 054 final TInput input) { 055 this.taskletId = taskletId; 056 this.userFunction = userFunction; 057 this.input = input; 058 } 059 060 /** 061 * Execute the function using the input. 062 * @return Output of the function. 063 */ 064 public TOutput execute() throws Exception { 065 return userFunction.call(input); 066 } 067 068 /** 069 * @return the ID of the VortexTasklet associated with this MasterToWorkerRequest. 070 */ 071 public int getTaskletId() { 072 return taskletId; 073 } 074 075 /** 076 * Get function of the tasklet. 077 */ 078 public VortexFunction getFunction() { 079 return userFunction; 080 } 081 082 /** 083 * Get input of the tasklet. 084 */ 085 public TInput getInput() { 086 return input; 087 } 088}