Repository: incubator-reef Updated Branches: refs/heads/master f1628e899 -> 9769fcb5b
[REEF-448] Synchronization issue while reading message in WritableNsMessage When adding a node to class hierarchy, the entire process must be atomic in case multiple threads are accessing it at the same time. This PR is to add lock in ClassHierarchy methods to ensure it. JIRA: [REEF-448](https://issues.apache.org/jira/browse/REEF-448) This closes #275 Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/9769fcb5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/9769fcb5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/9769fcb5 Branch: refs/heads/master Commit: 9769fcb5ba0e5345e9d1908bdfda6c90cfb0a172 Parents: f1628e8 Author: Julia Wang <[email protected]> Authored: Mon Jul 6 13:43:57 2015 -0700 Committer: Markus Weimer <[email protected]> Committed: Mon Jul 6 14:08:24 2015 -0700 ---------------------------------------------------------------------- .../ClassHierarchy/ClassHierarchyImpl.cs | 54 ++++++++++++-------- 1 file changed, 33 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/9769fcb5/lang/cs/Org.Apache.REEF.Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs b/lang/cs/Org.Apache.REEF.Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs index 6820317..1f320dc 100644 --- a/lang/cs/Org.Apache.REEF.Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs +++ b/lang/cs/Org.Apache.REEF.Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs @@ -35,13 +35,16 @@ namespace Org.Apache.REEF.Tang.Implementations.ClassHierarchy { private static readonly Logger LOGGER = Logger.GetLogger(typeof (ClassHierarchyImpl)); private readonly INode rootNode; - private readonly MonotonicTreeMap<String, INamedParameterNode> shortNames = new MonotonicTreeMap<String, INamedParameterNode>(); + private readonly MonotonicTreeMap<string, INamedParameterNode> shortNames = new MonotonicTreeMap<string, INamedParameterNode>(); private readonly IList<string> assemblies; private readonly AssemblyLoader loader = null; + private object _nodeLock = new object(); + private object _mergeLock = new object(); + private object _implLock = new object(); public ParameterParser Parameterparser = new ParameterParser(); - public ClassHierarchyImpl(String file) : this(new string[] { file }, new Type[0]) + public ClassHierarchyImpl(string file) : this(new string[] { file }, new Type[0]) { } @@ -362,8 +365,11 @@ namespace Org.Apache.REEF.Tang.Implementations.ClassHierarchy public INode GetNode(Type type) { - this.RegisterType(type); - return GetAlreadyBoundNode(type); + lock (_nodeLock) + { + RegisterType(type); + return GetAlreadyBoundNode(type); + } } public INode GetNamespace() @@ -373,7 +379,10 @@ namespace Org.Apache.REEF.Tang.Implementations.ClassHierarchy public bool IsImplementation(IClassNode inter, IClassNode impl) { - return impl.IsImplementationOf(inter); + lock (_implLock) + { + return impl.IsImplementationOf(inter); + } } public IClassHierarchy Merge(IClassHierarchy ch) @@ -389,22 +398,25 @@ namespace Org.Apache.REEF.Tang.Implementations.ClassHierarchy { return ch; } - - ClassHierarchyImpl chi = (ClassHierarchyImpl)ch; - MonotonicHashSet<string> otherJars = new MonotonicHashSet<string>(); - otherJars.AddAll(chi.assemblies); - MonotonicHashSet<string> myJars = new MonotonicHashSet<string>(); - myJars.AddAll(this.assemblies); - if(myJars.ContainsAll(otherJars)) - { - return this; - } - if (otherJars.ContainsAll(myJars)) + + lock (_mergeLock) { - return ch; - } - myJars.AddAll(otherJars); - return new ClassHierarchyImpl(myJars.ToArray()); + ClassHierarchyImpl chi = (ClassHierarchyImpl) ch; + MonotonicHashSet<string> otherJars = new MonotonicHashSet<string>(); + otherJars.AddAll(chi.assemblies); + MonotonicHashSet<string> myJars = new MonotonicHashSet<string>(); + myJars.AddAll(this.assemblies); + if (myJars.ContainsAll(otherJars)) + { + return this; + } + if (otherJars.ContainsAll(myJars)) + { + return ch; + } + myJars.AddAll(otherJars); + return new ClassHierarchyImpl(myJars.ToArray()); + } } public object Parse(INamedParameterNode np, string value) @@ -421,7 +433,7 @@ namespace Org.Apache.REEF.Tang.Implementations.ClassHierarchy Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } Type clazz; - String fullName; + string fullName; try { clazz = (Type)ClassForName(iface.GetFullName());
