{"action":"created","comment":{"url":"https://api.github.com/repos/caskdata/cdap/pulls/comments/25202975","id":25202975,"diff_hunk":"@@ -122,4 +253,372 @@ private String getProgramScheduleId(Id.Program program, SchedulableProgramType p\n     return String.format(\"%s:%s:%s:%s\", program.getNamespaceId(), program.getApplicationId(),\n                          programType.name(), program.getId());\n   }\n+\n+  /**\n+   * One instance of this class contains a list of {@link StreamSizeSchedule}s, which are all interested\n+   * in the same stream. This instance subscribes to the size notification of the stream, and polls the\n+   * stream for its size whenever the schedules it references need the information.\n+   * The {@link StreamSizeScheduler} communicates with this class, which in turn communicates to the schedules\n+   * it contains to perform operations on the schedules - suspend, resume, etc.\n+   */\n+  private final class StreamSubscriber implements NotificationHandler<StreamSizeNotification>, Cancellable {\n+    // Key is the schedule ID\n+    private final ConcurrentMap<String, StreamSizeScheduleTask> scheduleTasks;\n+    private final Object lastNotificationLock;\n+    private final Id.Stream streamId;\n+    private final AtomicInteger activeTasks;\n+\n+    private Cancellable notificationSubscription;\n+    private ScheduledFuture<?> scheduledPolling;\n+    private StreamSizeNotification lastNotification;\n+\n+    private StreamSubscriber(Id.Stream streamId) {\n+      this.streamId = streamId;\n+      this.scheduleTasks = Maps.newConcurrentMap();\n+      this.lastNotificationLock = new Object();\n+      this.activeTasks = new AtomicInteger(0);\n+    }\n+\n+    public void start() throws NotificationFeedException, NotificationFeedNotFoundException {\n+      notificationSubscription = notificationService.subscribe(getFeed(), this, notificationExecutor);\n+    }\n+\n+    @Override\n+    public void cancel() {\n+      if (scheduledPolling != null) {\n+        scheduledPolling.cancel(true);\n+      }\n+      if (notificationSubscription != null) {\n+        notificationSubscription.cancel();\n+      }\n+    }\n+\n+    /**\n+     * Add a new scheduling task based on the data received by the stream referenced by {@code this} object.\n+     */\n+    public void createScheduleTask(Id.Program programId, SchedulableProgramType programType,\n+                                   StreamSizeSchedule streamSizeSchedule, boolean active,\n+                                   long baseRunSize, long baseRunTs, boolean persist) {\n+      // TODO add a createScheduleTasks, so that if we create multiple schedules for the same stream at the same\n+      // time, we don't have to poll the stream many times\n+\n+      StreamSizeScheduleTask newTask = new StreamSizeScheduleTask(programId, programType, streamSizeSchedule);\n+      StreamSizeScheduleTask previous =\n+        scheduleTasks.putIfAbsent(getScheduleId(programId, programType, streamSizeSchedule.getName()),\n+                                  newTask);\n+      if (previous != null) {\n+        // We cannot replace an existing schedule - that functionality is not wanted - yet\n+        return;\n+      }\n+\n+      // Initialize the schedule task\n+      if (baseRunSize == -1 && baseRunTs == -1) {\n+        // This is likely to be the first time that we schedule this task - ie it was not in the schedule store\n+        // before. Hence we set the base metrics properly\n+        long baseTs = System.currentTimeMillis();\n+        long baseSize = pollStream();\n+        newTask.startSchedule(baseSize, baseTs, active, persist);\n+        synchronized (lastNotificationLock) {\n+          lastNotification = new StreamSizeNotification(baseTs, baseSize);\n+        }\n+      } else {\n+        newTask.startSchedule(baseRunSize, baseRunTs, active, persist);\n+      }\n+\n+      if (active) {\n+        activeTasks.incrementAndGet();\n+      }\n+\n+      if (lastNotification != null) {\n+        // In all cases, when creating a schedule - either if it comes from the store or not,\n+        // we want to pass it the last seen notification\n+        // This call will send the notification to all the active tasks. The ones which are active before\n+        // that method is called will therefore see the notification twice. It is fine though.\n+        received(lastNotification, null);\n+      }\n+    }\n+\n+    /**\n+     * Suspend a scheduling task that is based on the data received by the stream referenced by {@code this} object.\n+     */\n+    public void suspendScheduleTask(Id.Program programId, SchedulableProgramType programType, String scheduleName) {\n+      StreamSizeScheduleTask task = scheduleTasks.get(getScheduleId(programId, programType, scheduleName));\n+      if (task == null) {\n+        return;\n+      }\n+      synchronized (this) {\n+        if (task.suspend()) {","path":"cdap-app-fabric/src/main/java/co/cask/cdap/internal/app/runtime/schedule/StreamSizeScheduler.java","position":337,"original_position":337,"commit_id":"930eb265c7f99a0c9bda9e05595f7cd42ed65bc5","original_commit_id":"930eb265c7f99a0c9bda9e05595f7cd42ed65bc5","user":{"login":"jguery","id":3600810,"avatar_url":"https://avatars.githubusercontent.com/u/3600810?v=3","gravatar_id":"","url":"https://api.github.com/users/jguery","html_url":"https://github.com/jguery","followers_url":"https://api.github.com/users/jguery/followers","following_url":"https://api.github.com/users/jguery/following{/other_user}","gists_url":"https://api.github.com/users/jguery/gists{/gist_id}","starred_url":"https://api.github.com/users/jguery/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jguery/subscriptions","organizations_url":"https://api.github.com/users/jguery/orgs","repos_url":"https://api.github.com/users/jguery/repos","events_url":"https://api.github.com/users/jguery/events{/privacy}","received_events_url":"https://api.github.com/users/jguery/received_events","type":"User","site_admin":false},"body":"Right, if ``task.suspend()`` is thread safe, then we don't need the synchronized block here.","created_at":"2015-02-23T21:16:39Z","updated_at":"2015-02-23T21:16:39Z","html_url":"https://github.com/caskdata/cdap/pull/1358#discussion_r25202975","pull_request_url":"https://api.github.com/repos/caskdata/cdap/pulls/1358","_links":{"self":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/comments/25202975"},"html":{"href":"https://github.com/caskdata/cdap/pull/1358#discussion_r25202975"},"pull_request":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/1358"}}},"pull_request":{"url":"https://api.github.com/repos/caskdata/cdap/pulls/1358","id":29641240,"html_url":"https://github.com/caskdata/cdap/pull/1358","diff_url":"https://github.com/caskdata/cdap/pull/1358.diff","patch_url":"https://github.com/caskdata/cdap/pull/1358.patch","issue_url":"https://api.github.com/repos/caskdata/cdap/issues/1358","number":1358,"state":"open","locked":false,"title":"Implementation of StreamSizeScheduler and Tests","user":{"login":"jguery","id":3600810,"avatar_url":"https://avatars.githubusercontent.com/u/3600810?v=3","gravatar_id":"","url":"https://api.github.com/users/jguery","html_url":"https://github.com/jguery","followers_url":"https://api.github.com/users/jguery/followers","following_url":"https://api.github.com/users/jguery/following{/other_user}","gists_url":"https://api.github.com/users/jguery/gists{/gist_id}","starred_url":"https://api.github.com/users/jguery/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jguery/subscriptions","organizations_url":"https://api.github.com/users/jguery/orgs","repos_url":"https://api.github.com/users/jguery/repos","events_url":"https://api.github.com/users/jguery/events{/privacy}","received_events_url":"https://api.github.com/users/jguery/received_events","type":"User","site_admin":false},"body":"This does not yet contain a persistent store for the stream size schedules.\r\n\r\nBuild: http://builds.cask.co/browse/CDAP-DUT833-1","created_at":"2015-02-19T19:31:52Z","updated_at":"2015-02-23T21:16:39Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6498aa85d6ff8629a7a7eff2431d340342a5549f","assignee":{"login":"anew","id":483004,"avatar_url":"https://avatars.githubusercontent.com/u/483004?v=3","gravatar_id":"","url":"https://api.github.com/users/anew","html_url":"https://github.com/anew","followers_url":"https://api.github.com/users/anew/followers","following_url":"https://api.github.com/users/anew/following{/other_user}","gists_url":"https://api.github.com/users/anew/gists{/gist_id}","starred_url":"https://api.github.com/users/anew/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/anew/subscriptions","organizations_url":"https://api.github.com/users/anew/orgs","repos_url":"https://api.github.com/users/anew/repos","events_url":"https://api.github.com/users/anew/events{/privacy}","received_events_url":"https://api.github.com/users/anew/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/caskdata/cdap/milestones/3","labels_url":"https://api.github.com/repos/caskdata/cdap/milestones/3/labels","id":944584,"number":3,"title":"2.8.0","description":null,"creator":{"login":"jwang47","id":1084704,"avatar_url":"https://avatars.githubusercontent.com/u/1084704?v=3","gravatar_id":"","url":"https://api.github.com/users/jwang47","html_url":"https://github.com/jwang47","followers_url":"https://api.github.com/users/jwang47/followers","following_url":"https://api.github.com/users/jwang47/following{/other_user}","gists_url":"https://api.github.com/users/jwang47/gists{/gist_id}","starred_url":"https://api.github.com/users/jwang47/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jwang47/subscriptions","organizations_url":"https://api.github.com/users/jwang47/orgs","repos_url":"https://api.github.com/users/jwang47/repos","events_url":"https://api.github.com/users/jwang47/events{/privacy}","received_events_url":"https://api.github.com/users/jwang47/received_events","type":"User","site_admin":false},"open_issues":13,"closed_issues":71,"state":"open","created_at":"2015-01-21T01:15:14Z","updated_at":"2015-02-23T21:05:10Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/caskdata/cdap/pulls/1358/commits","review_comments_url":"https://api.github.com/repos/caskdata/cdap/pulls/1358/comments","review_comment_url":"https://api.github.com/repos/caskdata/cdap/pulls/comments{/number}","comments_url":"https://api.github.com/repos/caskdata/cdap/issues/1358/comments","statuses_url":"https://api.github.com/repos/caskdata/cdap/statuses/930eb265c7f99a0c9bda9e05595f7cd42ed65bc5","head":{"label":"caskdata:feature/stream-size-scheduler","ref":"feature/stream-size-scheduler","sha":"930eb265c7f99a0c9bda9e05595f7cd42ed65bc5","user":{"login":"caskdata","id":8123023,"avatar_url":"https://avatars.githubusercontent.com/u/8123023?v=3","gravatar_id":"","url":"https://api.github.com/users/caskdata","html_url":"https://github.com/caskdata","followers_url":"https://api.github.com/users/caskdata/followers","following_url":"https://api.github.com/users/caskdata/following{/other_user}","gists_url":"https://api.github.com/users/caskdata/gists{/gist_id}","starred_url":"https://api.github.com/users/caskdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caskdata/subscriptions","organizations_url":"https://api.github.com/users/caskdata/orgs","repos_url":"https://api.github.com/users/caskdata/repos","events_url":"https://api.github.com/users/caskdata/events{/privacy}","received_events_url":"https://api.github.com/users/caskdata/received_events","type":"Organization","site_admin":false},"repo":{"id":22542759,"name":"cdap","full_name":"caskdata/cdap","owner":{"login":"caskdata","id":8123023,"avatar_url":"https://avatars.githubusercontent.com/u/8123023?v=3","gravatar_id":"","url":"https://api.github.com/users/caskdata","html_url":"https://github.com/caskdata","followers_url":"https://api.github.com/users/caskdata/followers","following_url":"https://api.github.com/users/caskdata/following{/other_user}","gists_url":"https://api.github.com/users/caskdata/gists{/gist_id}","starred_url":"https://api.github.com/users/caskdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caskdata/subscriptions","organizations_url":"https://api.github.com/users/caskdata/orgs","repos_url":"https://api.github.com/users/caskdata/repos","events_url":"https://api.github.com/users/caskdata/events{/privacy}","received_events_url":"https://api.github.com/users/caskdata/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/caskdata/cdap","description":"Cask Data Application Platform (CDAP)","fork":false,"url":"https://api.github.com/repos/caskdata/cdap","forks_url":"https://api.github.com/repos/caskdata/cdap/forks","keys_url":"https://api.github.com/repos/caskdata/cdap/keys{/key_id}","collaborators_url":"https://api.github.com/repos/caskdata/cdap/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/caskdata/cdap/teams","hooks_url":"https://api.github.com/repos/caskdata/cdap/hooks","issue_events_url":"https://api.github.com/repos/caskdata/cdap/issues/events{/number}","events_url":"https://api.github.com/repos/caskdata/cdap/events","assignees_url":"https://api.github.com/repos/caskdata/cdap/assignees{/user}","branches_url":"https://api.github.com/repos/caskdata/cdap/branches{/branch}","tags_url":"https://api.github.com/repos/caskdata/cdap/tags","blobs_url":"https://api.github.com/repos/caskdata/cdap/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/caskdata/cdap/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/caskdata/cdap/git/refs{/sha}","trees_url":"https://api.github.com/repos/caskdata/cdap/git/trees{/sha}","statuses_url":"https://api.github.com/repos/caskdata/cdap/statuses/{sha}","languages_url":"https://api.github.com/repos/caskdata/cdap/languages","stargazers_url":"https://api.github.com/repos/caskdata/cdap/stargazers","contributors_url":"https://api.github.com/repos/caskdata/cdap/contributors","subscribers_url":"https://api.github.com/repos/caskdata/cdap/subscribers","subscription_url":"https://api.github.com/repos/caskdata/cdap/subscription","commits_url":"https://api.github.com/repos/caskdata/cdap/commits{/sha}","git_commits_url":"https://api.github.com/repos/caskdata/cdap/git/commits{/sha}","comments_url":"https://api.github.com/repos/caskdata/cdap/comments{/number}","issue_comment_url":"https://api.github.com/repos/caskdata/cdap/issues/comments{/number}","contents_url":"https://api.github.com/repos/caskdata/cdap/contents/{+path}","compare_url":"https://api.github.com/repos/caskdata/cdap/compare/{base}...{head}","merges_url":"https://api.github.com/repos/caskdata/cdap/merges","archive_url":"https://api.github.com/repos/caskdata/cdap/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/caskdata/cdap/downloads","issues_url":"https://api.github.com/repos/caskdata/cdap/issues{/number}","pulls_url":"https://api.github.com/repos/caskdata/cdap/pulls{/number}","milestones_url":"https://api.github.com/repos/caskdata/cdap/milestones{/number}","notifications_url":"https://api.github.com/repos/caskdata/cdap/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/caskdata/cdap/labels{/name}","releases_url":"https://api.github.com/repos/caskdata/cdap/releases{/id}","created_at":"2014-08-02T09:00:10Z","updated_at":"2015-02-23T19:55:37Z","pushed_at":"2015-02-23T21:13:54Z","git_url":"git://github.com/caskdata/cdap.git","ssh_url":"git@github.com:caskdata/cdap.git","clone_url":"https://github.com/caskdata/cdap.git","svn_url":"https://github.com/caskdata/cdap","homepage":"http://cdap.io","size":784048,"stargazers_count":27,"watchers_count":27,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":9,"mirror_url":null,"open_issues_count":22,"forks":9,"open_issues":22,"watchers":27,"default_branch":"develop"}},"base":{"label":"caskdata:develop","ref":"develop","sha":"a0087f1c8ce68e1fd807f9664451aeb9db5906ed","user":{"login":"caskdata","id":8123023,"avatar_url":"https://avatars.githubusercontent.com/u/8123023?v=3","gravatar_id":"","url":"https://api.github.com/users/caskdata","html_url":"https://github.com/caskdata","followers_url":"https://api.github.com/users/caskdata/followers","following_url":"https://api.github.com/users/caskdata/following{/other_user}","gists_url":"https://api.github.com/users/caskdata/gists{/gist_id}","starred_url":"https://api.github.com/users/caskdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caskdata/subscriptions","organizations_url":"https://api.github.com/users/caskdata/orgs","repos_url":"https://api.github.com/users/caskdata/repos","events_url":"https://api.github.com/users/caskdata/events{/privacy}","received_events_url":"https://api.github.com/users/caskdata/received_events","type":"Organization","site_admin":false},"repo":{"id":22542759,"name":"cdap","full_name":"caskdata/cdap","owner":{"login":"caskdata","id":8123023,"avatar_url":"https://avatars.githubusercontent.com/u/8123023?v=3","gravatar_id":"","url":"https://api.github.com/users/caskdata","html_url":"https://github.com/caskdata","followers_url":"https://api.github.com/users/caskdata/followers","following_url":"https://api.github.com/users/caskdata/following{/other_user}","gists_url":"https://api.github.com/users/caskdata/gists{/gist_id}","starred_url":"https://api.github.com/users/caskdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caskdata/subscriptions","organizations_url":"https://api.github.com/users/caskdata/orgs","repos_url":"https://api.github.com/users/caskdata/repos","events_url":"https://api.github.com/users/caskdata/events{/privacy}","received_events_url":"https://api.github.com/users/caskdata/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/caskdata/cdap","description":"Cask Data Application Platform (CDAP)","fork":false,"url":"https://api.github.com/repos/caskdata/cdap","forks_url":"https://api.github.com/repos/caskdata/cdap/forks","keys_url":"https://api.github.com/repos/caskdata/cdap/keys{/key_id}","collaborators_url":"https://api.github.com/repos/caskdata/cdap/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/caskdata/cdap/teams","hooks_url":"https://api.github.com/repos/caskdata/cdap/hooks","issue_events_url":"https://api.github.com/repos/caskdata/cdap/issues/events{/number}","events_url":"https://api.github.com/repos/caskdata/cdap/events","assignees_url":"https://api.github.com/repos/caskdata/cdap/assignees{/user}","branches_url":"https://api.github.com/repos/caskdata/cdap/branches{/branch}","tags_url":"https://api.github.com/repos/caskdata/cdap/tags","blobs_url":"https://api.github.com/repos/caskdata/cdap/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/caskdata/cdap/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/caskdata/cdap/git/refs{/sha}","trees_url":"https://api.github.com/repos/caskdata/cdap/git/trees{/sha}","statuses_url":"https://api.github.com/repos/caskdata/cdap/statuses/{sha}","languages_url":"https://api.github.com/repos/caskdata/cdap/languages","stargazers_url":"https://api.github.com/repos/caskdata/cdap/stargazers","contributors_url":"https://api.github.com/repos/caskdata/cdap/contributors","subscribers_url":"https://api.github.com/repos/caskdata/cdap/subscribers","subscription_url":"https://api.github.com/repos/caskdata/cdap/subscription","commits_url":"https://api.github.com/repos/caskdata/cdap/commits{/sha}","git_commits_url":"https://api.github.com/repos/caskdata/cdap/git/commits{/sha}","comments_url":"https://api.github.com/repos/caskdata/cdap/comments{/number}","issue_comment_url":"https://api.github.com/repos/caskdata/cdap/issues/comments{/number}","contents_url":"https://api.github.com/repos/caskdata/cdap/contents/{+path}","compare_url":"https://api.github.com/repos/caskdata/cdap/compare/{base}...{head}","merges_url":"https://api.github.com/repos/caskdata/cdap/merges","archive_url":"https://api.github.com/repos/caskdata/cdap/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/caskdata/cdap/downloads","issues_url":"https://api.github.com/repos/caskdata/cdap/issues{/number}","pulls_url":"https://api.github.com/repos/caskdata/cdap/pulls{/number}","milestones_url":"https://api.github.com/repos/caskdata/cdap/milestones{/number}","notifications_url":"https://api.github.com/repos/caskdata/cdap/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/caskdata/cdap/labels{/name}","releases_url":"https://api.github.com/repos/caskdata/cdap/releases{/id}","created_at":"2014-08-02T09:00:10Z","updated_at":"2015-02-23T19:55:37Z","pushed_at":"2015-02-23T21:13:54Z","git_url":"git://github.com/caskdata/cdap.git","ssh_url":"git@github.com:caskdata/cdap.git","clone_url":"https://github.com/caskdata/cdap.git","svn_url":"https://github.com/caskdata/cdap","homepage":"http://cdap.io","size":784048,"stargazers_count":27,"watchers_count":27,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":9,"mirror_url":null,"open_issues_count":22,"forks":9,"open_issues":22,"watchers":27,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/1358"},"html":{"href":"https://github.com/caskdata/cdap/pull/1358"},"issue":{"href":"https://api.github.com/repos/caskdata/cdap/issues/1358"},"comments":{"href":"https://api.github.com/repos/caskdata/cdap/issues/1358/comments"},"review_comments":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/1358/comments"},"review_comment":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/caskdata/cdap/pulls/1358/commits"},"statuses":{"href":"https://api.github.com/repos/caskdata/cdap/statuses/930eb265c7f99a0c9bda9e05595f7cd42ed65bc5"}}}}