[jira] [Commented] (TWILL-244) Provide access to ResourceReport to TwillRunnable

2017-09-05 Thread ASF GitHub Bot (JIRA)

[ 
https://issues.apache.org/jira/browse/TWILL-244?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16154483#comment-16154483
 ] 

ASF GitHub Bot commented on TWILL-244:
--

Github user chtyim commented on a diff in the pull request:

https://github.com/apache/twill/pull/61#discussion_r137130751
  
--- Diff: 
twill-core/src/main/java/org/apache/twill/internal/ResourceReportClient.java ---
@@ -0,0 +1,116 @@
+/*
+ * 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.
+ */
+package org.apache.twill.internal;
+
+import com.google.common.base.Charsets;
+import org.apache.twill.api.ResourceReport;
+import org.apache.twill.api.ResourceReporter;
+import org.apache.twill.api.TwillRunResources;
+import org.apache.twill.internal.json.ResourceReportAdapter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.lang.reflect.Type;
+import java.net.URL;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import javax.annotation.Nullable;
+
+/**
+ * A {@link ResourceReporter} that fetches reports from the given set of 
URLs.
+ */
+public final class ResourceReportClient implements ResourceReporter {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(ResourceReportClient.class);
+
+  private final List resourceUrls;
+
+  public ResourceReportClient(List resourceUrls) {
+this.resourceUrls = resourceUrls;
+  }
+
+  @Nullable
+  @Override
+  public ResourceReport getResourceReport() {
+for (URL url : resourceUrls) {
+  try {
+return fetchURL(url, "", ResourceReport.class);
+  } catch (IOException e) {
+// Just log a trace as it's ok to not able to fetch resource report
+LOG.trace("Exception raised when getting resource report from 
{}.", url, e);
+  }
+}
--- End diff --

That's the contract of the `ResourceReporter`. If the report is not 
available for whatever reason, it'll just return `null` / empty collection.


> Provide access to ResourceReport to TwillRunnable
> -
>
> Key: TWILL-244
> URL: https://issues.apache.org/jira/browse/TWILL-244
> Project: Apache Twill
>  Issue Type: Improvement
>  Components: api
>Reporter: Terence Yim
>
> One of the use case for this is to let TwillRunnable know about what 
> containers are running at the moment, which could be useful when building 
> fencing mechanism for single-master + HA type of applications.
> This information can be available via the TwillContext object.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)


[GitHub] twill pull request #61: (TWILL-244) Make resource report available to TwillR...

2017-09-05 Thread chtyim
Github user chtyim commented on a diff in the pull request:

https://github.com/apache/twill/pull/61#discussion_r137130751
  
--- Diff: 
twill-core/src/main/java/org/apache/twill/internal/ResourceReportClient.java ---
@@ -0,0 +1,116 @@
+/*
+ * 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.
+ */
+package org.apache.twill.internal;
+
+import com.google.common.base.Charsets;
+import org.apache.twill.api.ResourceReport;
+import org.apache.twill.api.ResourceReporter;
+import org.apache.twill.api.TwillRunResources;
+import org.apache.twill.internal.json.ResourceReportAdapter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.lang.reflect.Type;
+import java.net.URL;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import javax.annotation.Nullable;
+
+/**
+ * A {@link ResourceReporter} that fetches reports from the given set of 
URLs.
+ */
+public final class ResourceReportClient implements ResourceReporter {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(ResourceReportClient.class);
+
+  private final List resourceUrls;
+
+  public ResourceReportClient(List resourceUrls) {
+this.resourceUrls = resourceUrls;
+  }
+
+  @Nullable
+  @Override
+  public ResourceReport getResourceReport() {
+for (URL url : resourceUrls) {
+  try {
+return fetchURL(url, "", ResourceReport.class);
+  } catch (IOException e) {
+// Just log a trace as it's ok to not able to fetch resource report
+LOG.trace("Exception raised when getting resource report from 
{}.", url, e);
+  }
+}
--- End diff --

That's the contract of the `ResourceReporter`. If the report is not 
available for whatever reason, it'll just return `null` / empty collection.


---