{"action":"created","comment":{"url":"https://api.github.com/repos/apache/incubator-reef/pulls/comments/33895099","id":33895099,"diff_hunk":"@@ -0,0 +1,217 @@\n+/**\n+ * Licensed to the Apache Software Foundation (ASF) under one\n+ * or more contributor license agreements.  See the NOTICE file\n+ * distributed with this work for additional information\n+ * regarding copyright ownership.  The ASF licenses this file\n+ * to you under the Apache License, Version 2.0 (the\n+ * \"License\"); you may not use this file except in compliance\n+ * with the License.  You may obtain a copy of the License at\n+ *\n+ *   http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing,\n+ * software distributed under the License is distributed on an\n+ * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+ * KIND, either express or implied.  See the License for the\n+ * specific language governing permissions and limitations\n+ * under the License.\n+ */\n+package org.apache.reef.io.network.impl;\n+\n+import org.apache.reef.exception.evaluator.NetworkException;\n+import org.apache.reef.io.Tuple;\n+import org.apache.reef.io.network.ConnectionFactory;\n+import org.apache.reef.io.network.Message;\n+import org.apache.reef.io.network.NetworkServiceClient;\n+import org.apache.reef.io.network.naming.NameResolver;\n+import org.apache.reef.tang.annotations.Name;\n+import org.apache.reef.tang.annotations.Parameter;\n+import org.apache.reef.wake.EStage;\n+import org.apache.reef.wake.EventHandler;\n+import org.apache.reef.wake.Identifier;\n+import org.apache.reef.wake.IdentifierFactory;\n+import org.apache.reef.wake.impl.SingleThreadStage;\n+import org.apache.reef.wake.remote.Codec;\n+import org.apache.reef.wake.remote.impl.TransportEvent;\n+import org.apache.reef.wake.remote.transport.Link;\n+import org.apache.reef.wake.remote.transport.LinkListener;\n+import org.apache.reef.wake.remote.transport.Transport;\n+import org.apache.reef.wake.remote.transport.TransportFactory;\n+\n+import javax.inject.Inject;\n+import java.net.InetSocketAddress;\n+import java.net.SocketAddress;\n+import java.util.concurrent.ConcurrentHashMap;\n+import java.util.concurrent.ConcurrentMap;\n+import java.util.logging.Level;\n+import java.util.logging.Logger;\n+\n+/**\n+ * Default NetworkServiceClient implementation\n+ */\n+public final class DefaultNetworkServiceImpl implements NetworkServiceClient {\n+\n+  private static final Logger LOG = Logger.getLogger(DefaultNetworkServiceImpl.class.getName());\n+\n+  private final IdentifierFactory idFactory;\n+  private final NameResolver nameResolver;\n+  private final Transport transport;\n+  private final EventHandler<TransportEvent> recvHandler;\n+  private final ConcurrentMap<String, NSConnectionFactory> connectionFactoryMap;\n+  private final ConcurrentMap<String, Boolean> isStreamingCodecMap;\n+  private Identifier myId;\n+  private final Codec<DefaultNSMessage> nsCodec;\n+  private final LinkListener<DefaultNSMessage> nsLinkListener;\n+  private final EStage<Tuple<Identifier, InetSocketAddress>> nameServiceRegisteringStage;\n+  private final EStage<Identifier> nameServiceUnregisteringStage;\n+\n+  @Inject\n+  private DefaultNetworkServiceImpl(\n+      @Parameter(NetworkServiceParameters.NetworkServiceIdentifierFactory.class) final IdentifierFactory idFactory,\n+      @Parameter(NetworkServiceParameters.NetworkServicePort.class) final int nsPort,\n+      final TransportFactory transportFactory,\n+      final NameResolver nameResolver) {\n+\n+    this.idFactory = idFactory;\n+    this.connectionFactoryMap = new ConcurrentHashMap<>();\n+    this.isStreamingCodecMap = new ConcurrentHashMap<>();\n+    this.nsCodec = new DefaultNSMessageCodec(idFactory, connectionFactoryMap, isStreamingCodecMap);\n+    this.nsLinkListener = new NetworkServiceLinkListener(connectionFactoryMap);\n+    this.recvHandler = new NetworkServiceReceiveHandler(connectionFactoryMap, nsCodec);\n+    this.nameResolver = nameResolver;\n+    this.transport = transportFactory.newInstance(nsPort, recvHandler, recvHandler, new DefaultNSExceptionHandler());\n+\n+    this.nameServiceRegisteringStage = new SingleThreadStage<>(\n+        \"NameServiceRegisterer\", new EventHandler<Tuple<Identifier, InetSocketAddress>>() {\n+      @Override\n+      public void onNext(final Tuple<Identifier, InetSocketAddress> tuple) {\n+        try {\n+          nameResolver.register(tuple.getKey(), tuple.getValue());\n+          LOG.log(Level.FINEST, \"Registered {0} with nameservice\", tuple.getKey());\n+        } catch (final Exception ex) {\n+          final String msg = \"Unable to register \" + tuple.getKey() + \" with name service\";\n+          LOG.log(Level.WARNING, msg, ex);\n+          throw new RuntimeException(msg, ex);\n+        }\n+      }\n+    }, 5);\n+\n+    this.nameServiceUnregisteringStage = new SingleThreadStage<>(\n+        \"NameServiceRegisterer\", new EventHandler<Identifier>() {\n+      @Override\n+      public void onNext(final Identifier id) {\n+        try {\n+          nameResolver.unregister(id);\n+          LOG.log(Level.FINEST, \"Unregistered {0} with nameservice\", id);\n+        } catch (final Exception ex) {\n+          final String msg = \"Unable to unregister \" + id + \" with name service\";\n+          LOG.log(Level.WARNING, msg, ex);\n+          throw new RuntimeException(msg, ex);\n+        }\n+      }\n+    }, 5);\n+  }\n+\n+  @Override\n+  public synchronized <T> void registerConnectionFactory(final Class<? extends Name<String>> connectionFactoryId,","path":"lang/java/reef-io/src/main/java/org/apache/reef/io/network/impl/DefaultNetworkServiceImpl.java","position":null,"original_position":116,"commit_id":"baf310c2f6bff024b36b9d60c7cfb4c716b7cf2d","original_commit_id":"0b40f737b74dc3af1a17cf3ea21101c8769a376a","user":{"login":"bgchun","id":359137,"avatar_url":"https://avatars.githubusercontent.com/u/359137?v=3","gravatar_id":"","url":"https://api.github.com/users/bgchun","html_url":"https://github.com/bgchun","followers_url":"https://api.github.com/users/bgchun/followers","following_url":"https://api.github.com/users/bgchun/following{/other_user}","gists_url":"https://api.github.com/users/bgchun/gists{/gist_id}","starred_url":"https://api.github.com/users/bgchun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bgchun/subscriptions","organizations_url":"https://api.github.com/users/bgchun/orgs","repos_url":"https://api.github.com/users/bgchun/repos","events_url":"https://api.github.com/users/bgchun/events{/privacy}","received_events_url":"https://api.github.com/users/bgchun/received_events","type":"User","site_admin":false},"body":"If performance matters, use ConcurrentMap.\n\nOn Sun, Jul 5, 2015 at 11:03 PM, Tae-Geon Um <notifications@github.com>\nwrote:\n\n> In\n> lang/java/reef-io/src/main/java/org/apache/reef/io/network/impl/DefaultNetworkServiceImpl.java\n> <https://github.com/apache/incubator-reef/pull/255#discussion_r33895084>:\n>\n> > +      @Override\n> > +      public void onNext(final Identifier id) {\n> > +        try {\n> > +          nameResolver.unregister(id);\n> > +          LOG.log(Level.FINEST, \"Unregistered {0} with nameservice\", id);\n> > +        } catch (final Exception ex) {\n> > +          final String msg = \"Unable to unregister \" + id + \" with name service\";\n> > +          LOG.log(Level.WARNING, msg, ex);\n> > +          throw new RuntimeException(msg, ex);\n> > +        }\n> > +      }\n> > +    }, 5);\n> > +  }\n> > +\n> > +  @Override\n> > +  public synchronized <T> void registerConnectionFactory(final Class<? extends Name<String>> connectionFactoryId,\n>\n> getConnectionFactory is just to read map. I don't think we have to\n> synchronize this method.\n>\n> —\n> Reply to this email directly or view it on GitHub\n> <https://github.com/apache/incubator-reef/pull/255/files#r33895084>.\n>\n\n\n\n-- \nByung-Gon Chun\n","created_at":"2015-07-05T14:06:40Z","updated_at":"2015-07-05T14:06:40Z","html_url":"https://github.com/apache/incubator-reef/pull/255#discussion_r33895099","pull_request_url":"https://api.github.com/repos/apache/incubator-reef/pulls/255","_links":{"self":{"href":"https://api.github.com/repos/apache/incubator-reef/pulls/comments/33895099"},"html":{"href":"https://github.com/apache/incubator-reef/pull/255#discussion_r33895099"},"pull_request":{"href":"https://api.github.com/repos/apache/incubator-reef/pulls/255"}}},"pull_request":{"url":"https://api.github.com/repos/apache/incubator-reef/pulls/255","id":38720848,"html_url":"https://github.com/apache/incubator-reef/pull/255","diff_url":"https://github.com/apache/incubator-reef/pull/255.diff","patch_url":"https://github.com/apache/incubator-reef/pull/255.patch","issue_url":"https://api.github.com/repos/apache/incubator-reef/issues/255","number":255,"state":"open","locked":false,"title":"[REEF-184] Allow NetworkService to add more than one message handlers","user":{"login":"taegeonum","id":1371383,"avatar_url":"https://avatars.githubusercontent.com/u/1371383?v=3","gravatar_id":"","url":"https://api.github.com/users/taegeonum","html_url":"https://github.com/taegeonum","followers_url":"https://api.github.com/users/taegeonum/followers","following_url":"https://api.github.com/users/taegeonum/following{/other_user}","gists_url":"https://api.github.com/users/taegeonum/gists{/gist_id}","starred_url":"https://api.github.com/users/taegeonum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/taegeonum/subscriptions","organizations_url":"https://api.github.com/users/taegeonum/orgs","repos_url":"https://api.github.com/users/taegeonum/repos","events_url":"https://api.github.com/users/taegeonum/events{/privacy}","received_events_url":"https://api.github.com/users/taegeonum/received_events","type":"User","site_admin":false},"body":"This pull request addressed the issue by\r\n\r\n  * Introduce an interface of `NetworkServiceClient`\r\n  * Implement `DefaultNetworkServiceImpl` which is an implementation of `NetworkServiceClient`\r\n\r\nAcknowledgement: Collaborates with @gwsshs22 (GeonWoo Kim)\r\n\r\nJIRA: [REEF-184](http://issues.apache.org/jira/browse/REEF-184)","created_at":"2015-06-28T06:17:59Z","updated_at":"2015-07-05T14:06:40Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5f3a22755e06547ec5c0671861622a1381bf0e97","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/incubator-reef/pulls/255/commits","review_comments_url":"https://api.github.com/repos/apache/incubator-reef/pulls/255/comments","review_comment_url":"https://api.github.com/repos/apache/incubator-reef/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/incubator-reef/issues/255/comments","statuses_url":"https://api.github.com/repos/apache/incubator-reef/statuses/baf310c2f6bff024b36b9d60c7cfb4c716b7cf2d","head":{"label":"taegeonum:REEF-184","ref":"REEF-184","sha":"baf310c2f6bff024b36b9d60c7cfb4c716b7cf2d","user":{"login":"taegeonum","id":1371383,"avatar_url":"https://avatars.githubusercontent.com/u/1371383?v=3","gravatar_id":"","url":"https://api.github.com/users/taegeonum","html_url":"https://github.com/taegeonum","followers_url":"https://api.github.com/users/taegeonum/followers","following_url":"https://api.github.com/users/taegeonum/following{/other_user}","gists_url":"https://api.github.com/users/taegeonum/gists{/gist_id}","starred_url":"https://api.github.com/users/taegeonum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/taegeonum/subscriptions","organizations_url":"https://api.github.com/users/taegeonum/orgs","repos_url":"https://api.github.com/users/taegeonum/repos","events_url":"https://api.github.com/users/taegeonum/events{/privacy}","received_events_url":"https://api.github.com/users/taegeonum/received_events","type":"User","site_admin":false},"repo":{"id":27798307,"name":"incubator-reef","full_name":"taegeonum/incubator-reef","owner":{"login":"taegeonum","id":1371383,"avatar_url":"https://avatars.githubusercontent.com/u/1371383?v=3","gravatar_id":"","url":"https://api.github.com/users/taegeonum","html_url":"https://github.com/taegeonum","followers_url":"https://api.github.com/users/taegeonum/followers","following_url":"https://api.github.com/users/taegeonum/following{/other_user}","gists_url":"https://api.github.com/users/taegeonum/gists{/gist_id}","starred_url":"https://api.github.com/users/taegeonum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/taegeonum/subscriptions","organizations_url":"https://api.github.com/users/taegeonum/orgs","repos_url":"https://api.github.com/users/taegeonum/repos","events_url":"https://api.github.com/users/taegeonum/events{/privacy}","received_events_url":"https://api.github.com/users/taegeonum/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/taegeonum/incubator-reef","description":"Mirror of Apache REEF (Incubating)","fork":true,"url":"https://api.github.com/repos/taegeonum/incubator-reef","forks_url":"https://api.github.com/repos/taegeonum/incubator-reef/forks","keys_url":"https://api.github.com/repos/taegeonum/incubator-reef/keys{/key_id}","collaborators_url":"https://api.github.com/repos/taegeonum/incubator-reef/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/taegeonum/incubator-reef/teams","hooks_url":"https://api.github.com/repos/taegeonum/incubator-reef/hooks","issue_events_url":"https://api.github.com/repos/taegeonum/incubator-reef/issues/events{/number}","events_url":"https://api.github.com/repos/taegeonum/incubator-reef/events","assignees_url":"https://api.github.com/repos/taegeonum/incubator-reef/assignees{/user}","branches_url":"https://api.github.com/repos/taegeonum/incubator-reef/branches{/branch}","tags_url":"https://api.github.com/repos/taegeonum/incubator-reef/tags","blobs_url":"https://api.github.com/repos/taegeonum/incubator-reef/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/taegeonum/incubator-reef/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/taegeonum/incubator-reef/git/refs{/sha}","trees_url":"https://api.github.com/repos/taegeonum/incubator-reef/git/trees{/sha}","statuses_url":"https://api.github.com/repos/taegeonum/incubator-reef/statuses/{sha}","languages_url":"https://api.github.com/repos/taegeonum/incubator-reef/languages","stargazers_url":"https://api.github.com/repos/taegeonum/incubator-reef/stargazers","contributors_url":"https://api.github.com/repos/taegeonum/incubator-reef/contributors","subscribers_url":"https://api.github.com/repos/taegeonum/incubator-reef/subscribers","subscription_url":"https://api.github.com/repos/taegeonum/incubator-reef/subscription","commits_url":"https://api.github.com/repos/taegeonum/incubator-reef/commits{/sha}","git_commits_url":"https://api.github.com/repos/taegeonum/incubator-reef/git/commits{/sha}","comments_url":"https://api.github.com/repos/taegeonum/incubator-reef/comments{/number}","issue_comment_url":"https://api.github.com/repos/taegeonum/incubator-reef/issues/comments{/number}","contents_url":"https://api.github.com/repos/taegeonum/incubator-reef/contents/{+path}","compare_url":"https://api.github.com/repos/taegeonum/incubator-reef/compare/{base}...{head}","merges_url":"https://api.github.com/repos/taegeonum/incubator-reef/merges","archive_url":"https://api.github.com/repos/taegeonum/incubator-reef/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/taegeonum/incubator-reef/downloads","issues_url":"https://api.github.com/repos/taegeonum/incubator-reef/issues{/number}","pulls_url":"https://api.github.com/repos/taegeonum/incubator-reef/pulls{/number}","milestones_url":"https://api.github.com/repos/taegeonum/incubator-reef/milestones{/number}","notifications_url":"https://api.github.com/repos/taegeonum/incubator-reef/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/taegeonum/incubator-reef/labels{/name}","releases_url":"https://api.github.com/repos/taegeonum/incubator-reef/releases{/id}","created_at":"2014-12-10T02:24:04Z","updated_at":"2015-02-22T10:46:56Z","pushed_at":"2015-07-05T14:04:04Z","git_url":"git://github.com/taegeonum/incubator-reef.git","ssh_url":"git@github.com:taegeonum/incubator-reef.git","clone_url":"https://github.com/taegeonum/incubator-reef.git","svn_url":"https://github.com/taegeonum/incubator-reef","homepage":null,"size":30108,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"2ce6c81e21129e038c95fceac33f54f4194e9cb0","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":25623941,"name":"incubator-reef","full_name":"apache/incubator-reef","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/incubator-reef","description":"Mirror of Apache REEF (Incubating)","fork":false,"url":"https://api.github.com/repos/apache/incubator-reef","forks_url":"https://api.github.com/repos/apache/incubator-reef/forks","keys_url":"https://api.github.com/repos/apache/incubator-reef/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/incubator-reef/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/incubator-reef/teams","hooks_url":"https://api.github.com/repos/apache/incubator-reef/hooks","issue_events_url":"https://api.github.com/repos/apache/incubator-reef/issues/events{/number}","events_url":"https://api.github.com/repos/apache/incubator-reef/events","assignees_url":"https://api.github.com/repos/apache/incubator-reef/assignees{/user}","branches_url":"https://api.github.com/repos/apache/incubator-reef/branches{/branch}","tags_url":"https://api.github.com/repos/apache/incubator-reef/tags","blobs_url":"https://api.github.com/repos/apache/incubator-reef/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/incubator-reef/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/incubator-reef/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/incubator-reef/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/incubator-reef/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/incubator-reef/languages","stargazers_url":"https://api.github.com/repos/apache/incubator-reef/stargazers","contributors_url":"https://api.github.com/repos/apache/incubator-reef/contributors","subscribers_url":"https://api.github.com/repos/apache/incubator-reef/subscribers","subscription_url":"https://api.github.com/repos/apache/incubator-reef/subscription","commits_url":"https://api.github.com/repos/apache/incubator-reef/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/incubator-reef/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/incubator-reef/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/incubator-reef/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/incubator-reef/contents/{+path}","compare_url":"https://api.github.com/repos/apache/incubator-reef/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/incubator-reef/merges","archive_url":"https://api.github.com/repos/apache/incubator-reef/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/incubator-reef/downloads","issues_url":"https://api.github.com/repos/apache/incubator-reef/issues{/number}","pulls_url":"https://api.github.com/repos/apache/incubator-reef/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/incubator-reef/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/incubator-reef/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/incubator-reef/labels{/name}","releases_url":"https://api.github.com/repos/apache/incubator-reef/releases{/id}","created_at":"2014-10-23T07:00:10Z","updated_at":"2015-07-03T08:22:28Z","pushed_at":"2015-07-05T14:04:05Z","git_url":"git://github.com/apache/incubator-reef.git","ssh_url":"git@github.com:apache/incubator-reef.git","clone_url":"https://github.com/apache/incubator-reef.git","svn_url":"https://github.com/apache/incubator-reef","homepage":null,"size":41380,"stargazers_count":23,"watchers_count":23,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":27,"mirror_url":"git://git.apache.org/incubator-reef.git","open_issues_count":3,"forks":27,"open_issues":3,"watchers":23,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/incubator-reef/pulls/255"},"html":{"href":"https://github.com/apache/incubator-reef/pull/255"},"issue":{"href":"https://api.github.com/repos/apache/incubator-reef/issues/255"},"comments":{"href":"https://api.github.com/repos/apache/incubator-reef/issues/255/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/incubator-reef/pulls/255/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/incubator-reef/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/incubator-reef/pulls/255/commits"},"statuses":{"href":"https://api.github.com/repos/apache/incubator-reef/statuses/baf310c2f6bff024b36b9d60c7cfb4c716b7cf2d"}}}}