Repository: incubator-reef Updated Branches: refs/heads/master 603c71491 -> 875aaa0bc
[REEF-674] Made IEvaluatorDescriptor and its implementation immutable. `EvaluatorRequestor` was changed to call the new constructor of `EvaluatorDescriptorImpl` This also removes the `FromString` method from both the interface and the implementation. Its code is folded into the constructor which was the only place it was called from. Lastly, standard ReSharper cleanup has been applied to `IEvaluatorDescriptor` and `EvaluatorDescriptorImpl`. JIRA: [REEF-674](https://issues.apache.org/jira/browse/REEF-674) Pull Request: This closes #469 Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/875aaa0b Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/875aaa0b Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/875aaa0b Branch: refs/heads/master Commit: 875aaa0bcbda6702e2f17410f0272e8bda8368c6 Parents: 603c714 Author: Markus Weimer <[email protected]> Authored: Tue Sep 8 12:52:12 2015 -0700 Committer: Andrew Chung <[email protected]> Committed: Tue Sep 8 16:47:24 2015 -0700 ---------------------------------------------------------------------- .../Bridge/Events/EvaluatorRequestor.cs | 3 +- .../Evaluator/EvaluatorDescriptorImpl.cs | 178 +++++++------------ .../Evaluator/IEvaluatorDescriptor.cs | 18 +- 3 files changed, 72 insertions(+), 127 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/875aaa0b/lang/cs/Org.Apache.REEF.Driver/Bridge/Events/EvaluatorRequestor.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Driver/Bridge/Events/EvaluatorRequestor.cs b/lang/cs/Org.Apache.REEF.Driver/Bridge/Events/EvaluatorRequestor.cs index 3fc4cb7..6c3cbb2 100644 --- a/lang/cs/Org.Apache.REEF.Driver/Bridge/Events/EvaluatorRequestor.cs +++ b/lang/cs/Org.Apache.REEF.Driver/Bridge/Events/EvaluatorRequestor.cs @@ -71,8 +71,7 @@ namespace Org.Apache.REEF.Driver.Bridge.Events { for (int i = 0; i < request.Number; i++) { - EvaluatorDescriptorImpl descriptor = new EvaluatorDescriptorImpl(new NodeDescriptorImpl(), EvaluatorType.CLR, request.MemoryMegaBytes, request.VirtualCore); - descriptor.Rack = request.Rack; + EvaluatorDescriptorImpl descriptor = new EvaluatorDescriptorImpl(new NodeDescriptorImpl(), EvaluatorType.CLR, request.MemoryMegaBytes, request.VirtualCore, request.Rack); string key = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", request.EvaluatorBatchId, i); try { http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/875aaa0b/lang/cs/Org.Apache.REEF.Driver/Evaluator/EvaluatorDescriptorImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Driver/Evaluator/EvaluatorDescriptorImpl.cs b/lang/cs/Org.Apache.REEF.Driver/Evaluator/EvaluatorDescriptorImpl.cs index e56f165..319ba08 100644 --- a/lang/cs/Org.Apache.REEF.Driver/Evaluator/EvaluatorDescriptorImpl.cs +++ b/lang/cs/Org.Apache.REEF.Driver/Evaluator/EvaluatorDescriptorImpl.cs @@ -29,100 +29,38 @@ using Org.Apache.REEF.Utilities.Logging; namespace Org.Apache.REEF.Driver.Evaluator { - public class EvaluatorDescriptorImpl : IEvaluatorDescriptor + // This class is `public` because it is called from C++ code. + public sealed class EvaluatorDescriptorImpl : IEvaluatorDescriptor { + private const string DefaultRackName = "default_rack"; private static readonly Logger LOGGER = Logger.GetLogger(typeof(EvaluatorDescriptorImpl)); - - private INodeDescriptor _nodeDescriptor; - - private EvaluatorType _type; - - private int _megaBytes; - - private int _virtualCore; - - private readonly string _rack = "default_rack"; - - public EvaluatorDescriptorImpl(string serializedString) - { - FromString(serializedString); - } - - public EvaluatorDescriptorImpl(INodeDescriptor nodeDescriptor, EvaluatorType type, int megaBytes, int core) + private readonly int _core; + private readonly EvaluatorType _evaluatorType; + private readonly int _megaBytes; + private readonly INodeDescriptor _nodeDescriptor; + private readonly string _rack; + + internal EvaluatorDescriptorImpl(INodeDescriptor nodeDescriptor, EvaluatorType type, int megaBytes, int core, + string rack = DefaultRackName) { _nodeDescriptor = nodeDescriptor; - _type = type; + _evaluatorType = type; _megaBytes = megaBytes; - _virtualCore = core; + _core = core; + _rack = rack; } - public INodeDescriptor NodeDescriptor + /// <summary> + /// Constructor only to be used by the bridge. + /// </summary> + /// <param name="str"></param> + public EvaluatorDescriptorImpl(string str) { - get + var settings = new Dictionary<string, string>(); + var components = str.Split(','); + foreach (var component in components) { - return _nodeDescriptor; - } - - set - { - } - } - - public EvaluatorType EvaluatorType - { - get - { - return _type; - } - - set - { - } - } - - public int Memory - { - get - { - return _megaBytes; - } - - set - { - } - } - - public int VirtualCore - { - get - { - return _virtualCore; - } - - set - { - } - } - - public string Rack - { - get - { - return _rack; - } - - set - { - } - } - - public void FromString(string str) - { - Dictionary<string, string> settings = new Dictionary<string, string>(); - string[] components = str.Split(','); - foreach (string component in components) - { - string[] pair = component.Trim().Split('='); + var pair = component.Trim().Split('='); if (pair == null || pair.Length != 2) { var e = new ArgumentException("invalid component to be used as key-value pair:", component); @@ -133,27 +71,27 @@ namespace Org.Apache.REEF.Driver.Evaluator string ipAddress; if (!settings.TryGetValue("IP", out ipAddress)) { - Exceptions.Throw(new ArgumentException("cannot find IP entry"), LOGGER); + Exceptions.Throw(new ArgumentException("cannot find IP entry"), LOGGER); } ipAddress = ipAddress.Split('/').Last(); string port; if (!settings.TryGetValue("Port", out port)) { - Exceptions.Throw(new ArgumentException("cannot find Port entry"), LOGGER); + Exceptions.Throw(new ArgumentException("cannot find Port entry"), LOGGER); } - int portNumber = 0; + var portNumber = 0; int.TryParse(port, out portNumber); string hostName; if (!settings.TryGetValue("HostName", out hostName)) { - Exceptions.Throw(new ArgumentException("cannot find HostName entry"), LOGGER); + Exceptions.Throw(new ArgumentException("cannot find HostName entry"), LOGGER); } string memory; if (!settings.TryGetValue("Memory", out memory)) { Exceptions.Throw(new ArgumentException("cannot find Memory entry"), LOGGER); } - int memoryInMegaBytes = 0; + var memoryInMegaBytes = 0; int.TryParse(memory, out memoryInMegaBytes); string core; @@ -161,35 +99,45 @@ namespace Org.Apache.REEF.Driver.Evaluator { Exceptions.Throw(new ArgumentException("cannot find Core entry"), LOGGER); } - int vCore = 0; + var vCore = 0; int.TryParse(core, out vCore); - IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(ipAddress), portNumber); + var ipEndPoint = new IPEndPoint(IPAddress.Parse(ipAddress), portNumber); - _nodeDescriptor = new NodeDescriptorImpl(); - _nodeDescriptor.InetSocketAddress = ipEndPoint; - _nodeDescriptor.HostName = hostName; - _type = EvaluatorType.CLR; + _nodeDescriptor = new NodeDescriptorImpl {InetSocketAddress = ipEndPoint, HostName = hostName}; + _evaluatorType = EvaluatorType.CLR; _megaBytes = memoryInMegaBytes; - _virtualCore = vCore; + _core = vCore; } - public void SetType(EvaluatorType type) + public INodeDescriptor NodeDescriptor { - lock (this) - { - if (_type != EvaluatorType.UNDECIDED) - { - var e = new InvalidOperationException("Cannot change a set evaluator type: " + _type); - Exceptions.Throw(e, LOGGER); - } - _type = type; - } + get { return _nodeDescriptor; } + } + + public EvaluatorType EvaluatorType + { + get { return _evaluatorType; } + } + + public int Memory + { + get { return _megaBytes; } + } + + public int VirtualCore + { + get { return _core; } + } + + public string Rack + { + get { return _rack; } } public override bool Equals(object obj) { - EvaluatorDescriptorImpl other = obj as EvaluatorDescriptorImpl; + var other = obj as EvaluatorDescriptorImpl; if (other == null) { return false; @@ -205,14 +153,14 @@ namespace Org.Apache.REEF.Driver.Evaluator return base.GetHashCode(); } - private bool EquivalentMemory(EvaluatorDescriptorImpl other) + private bool EquivalentMemory(IEvaluatorDescriptor other) { - int granularity = ClrHandlerHelper.MemoryGranularity == 0 - ? Constants.DefaultMemoryGranularity - : ClrHandlerHelper.MemoryGranularity; - int m1 = (Memory - 1) / granularity; - int m2 = (other.Memory - 1 ) / granularity; + var granularity = ClrHandlerHelper.MemoryGranularity == 0 + ? Constants.DefaultMemoryGranularity + : ClrHandlerHelper.MemoryGranularity; + var m1 = (Memory - 1)/granularity; + var m2 = (other.Memory - 1)/granularity; return (m1 == m2); } } -} +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/875aaa0b/lang/cs/Org.Apache.REEF.Driver/Evaluator/IEvaluatorDescriptor.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Driver/Evaluator/IEvaluatorDescriptor.cs b/lang/cs/Org.Apache.REEF.Driver/Evaluator/IEvaluatorDescriptor.cs index 9fadbd9..28fffe7 100644 --- a/lang/cs/Org.Apache.REEF.Driver/Evaluator/IEvaluatorDescriptor.cs +++ b/lang/cs/Org.Apache.REEF.Driver/Evaluator/IEvaluatorDescriptor.cs @@ -23,35 +23,33 @@ using Org.Apache.REEF.Common.Evaluator; namespace Org.Apache.REEF.Driver.Evaluator { /// <summary> - /// Metadata about an Evaluator. + /// Metadata about an Evaluator. /// </summary> public interface IEvaluatorDescriptor { /// <summary> - /// NodeDescriptor of the node where this Evaluator is running. + /// NodeDescriptor of the node where this Evaluator is running. /// </summary> - INodeDescriptor NodeDescriptor { get; set; } + INodeDescriptor NodeDescriptor { get; } /// <summary> /// type of Evaluator. /// </summary> - EvaluatorType EvaluatorType { get; set; } + EvaluatorType EvaluatorType { get; } /// <summary> /// the amount of memory allocated to this Evaluator. /// </summary> - int Memory { get; set; } + int Memory { get; } /// <summary> /// the virtual core allocated to this Evaluator. /// </summary> - int VirtualCore { get; set; } + int VirtualCore { get; } /// <summary> /// rack on which the evaluator was allocated /// </summary> - string Rack { get; set; } - - void FromString(string str); + string Rack { get; } } -} +} \ No newline at end of file
