blob: c4a101d6bec0ed9e6f11d2d28ad4d827efbf86c3 [file] [log] [blame]
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
using System;
using Org.Apache.REEF.Common.Tasks;
using Org.Apache.REEF.IMRU.API;
using Org.Apache.REEF.IMRU.OnREEF.Driver;
using Org.Apache.REEF.IMRU.OnREEF.MapInputWithControlMessage;
using Org.Apache.REEF.IMRU.OnREEF.Parameters;
using Org.Apache.REEF.Network.Group.Operators;
using Org.Apache.REEF.Network.Group.Task;
using Org.Apache.REEF.Tang.Annotations;
using Org.Apache.REEF.Utilities.Logging;
namespace Org.Apache.REEF.IMRU.OnREEF.IMRUTasks
{
/// <summary>
/// Hosts the IMRU MapTask in a REEF Task.
/// </summary>
/// <typeparam name="TMapInput">Map input</typeparam>
/// <typeparam name="TMapOutput">Map output</typeparam>
internal sealed class MapTaskHost<TMapInput, TMapOutput> : ITask
{
private static readonly Logger Logger = Logger.GetLogger(typeof(MapTaskHost<TMapInput, TMapOutput>));
private readonly IBroadcastReceiver<MapInputWithControlMessage<TMapInput>> _dataAndMessageReceiver;
private readonly IReduceSender<TMapOutput> _dataReducer;
private readonly IMapFunction<TMapInput, TMapOutput> _mapTask;
private readonly bool _invokeGC;
/// <summary>
/// </summary>
/// <param name="mapTask">The MapTask hosted in this REEF Task.</param>
/// <param name="groupCommunicationsClient">Used to setup the communications.</param>
/// <param name="invokeGC">Whether to call Garbage Collector after each iteration or not</param>
[Inject]
private MapTaskHost(
IMapFunction<TMapInput, TMapOutput> mapTask,
IGroupCommClient groupCommunicationsClient,
[Parameter(typeof(InvokeGC))] bool invokeGC)
{
_mapTask = mapTask;
var cg = groupCommunicationsClient.GetCommunicationGroup(IMRUConstants.CommunicationGroupName);
_dataAndMessageReceiver =
cg.GetBroadcastReceiver<MapInputWithControlMessage<TMapInput>>(IMRUConstants.BroadcastOperatorName);
_dataReducer = cg.GetReduceSender<TMapOutput>(IMRUConstants.ReduceOperatorName);
_invokeGC = invokeGC;
}
/// <summary>
/// Performs IMRU iterations on map side
/// </summary>
/// <param name="memento"></param>
/// <returns></returns>
public byte[] Call(byte[] memento)
{
while (true)
{
if (_invokeGC)
{
Logger.Log(Level.Verbose, "Calling Garbage Collector");
GC.Collect();
GC.WaitForPendingFinalizers();
}
TMapOutput result;
using (
MapInputWithControlMessage<TMapInput> mapInput = _dataAndMessageReceiver.Receive())
{
if (mapInput.ControlMessage == MapControlMessage.Stop)
{
break;
}
result = _mapTask.Map(mapInput.Message);
}
_dataReducer.Send(result);
}
return null;
}
/// <summary>
/// Dispose function
/// </summary>
public void Dispose()
{
}
}
}