[GitHub] [incubator-gobblin] sv2000 commented on a change in pull request #2667: [GOBBLIN-800] Remove the metric context cache from GobblinMetricsRegistry

2019-06-07 Thread GitBox
sv2000 commented on a change in pull request #2667: [GOBBLIN-800] Remove the 
metric context cache from GobblinMetricsRegistry
URL: https://github.com/apache/incubator-gobblin/pull/2667#discussion_r291757745
 
 

 ##
 File path: 
gobblin-runtime/src/main/java/org/apache/gobblin/runtime/util/ForkMetrics.java
 ##
 @@ -0,0 +1,80 @@
+/*
+ * 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.gobblin.runtime.util;
+
+import java.util.List;
+import java.util.concurrent.Callable;
+
+import com.google.common.collect.ImmutableList;
+
+import org.apache.gobblin.configuration.ConfigurationKeys;
+import org.apache.gobblin.configuration.State;
+import org.apache.gobblin.metrics.GobblinMetrics;
+import org.apache.gobblin.metrics.MetricContext;
+import org.apache.gobblin.metrics.Tag;
+import org.apache.gobblin.runtime.TaskState;
+import org.apache.gobblin.runtime.fork.Fork;
+
+/**
+ * An extension to {@link GobblinMetrics} specifically for {@link Fork}.
+ */
+public class ForkMetrics extends GobblinMetrics {
+  private static final String FORK_METRICS_BRANCH_NAME_KEY = "forkBranchName";
+
+  protected ForkMetrics(TaskState taskState, int index) {
+super(name(taskState, index), parentContextForFork(taskState), 
getForkMetricsTags(taskState, index));
+  }
+
+  private static MetricContext parentContextForFork(TaskState taskState) {
+return TaskMetrics.get(METRICS_ID_PREFIX + taskState.getJobId() + "." + 
taskState.getTaskId()).getMetricContext();
+  }
+
+  public static ForkMetrics get(final TaskState taskState, int index) {
+return (ForkMetrics) GOBBLIN_METRICS_REGISTRY.getOrDefault(name(taskState, 
index), new Callable() {
+  @Override
+  public GobblinMetrics call() throws Exception {
+return new ForkMetrics(taskState, index);
+  }
+});
+  }
+
+  /**
+   * Creates a unique {@link String} representing this branch.
+   */
+  private static String getForkMetricsId(State state, int index) {
+return state.getProp(ConfigurationKeys.FORK_BRANCH_NAME_KEY + "." + index,
+ConfigurationKeys.DEFAULT_FORK_BRANCH_NAME + index);
+  }
+
+  /**
+   * Creates a {@link List} of {@link Tag}s for a {@link Fork} instance. The 
{@link Tag}s are purely based on the
+   * index and the branch name.
+   */
+  private static List> getForkMetricsTags(State state, int index) {
+return ImmutableList.>of(new Tag<>(FORK_METRICS_BRANCH_NAME_KEY, 
getForkMetricsId(state, index)));
 
 Review comment:
   Maybe add a null check here i.e. if (getForkMetricsId(state,index) != null)? 
Else, ImmutableList.of() may throw an exception. 


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-gobblin] sv2000 commented on a change in pull request #2667: [GOBBLIN-800] Remove the metric context cache from GobblinMetricsRegistry

2019-06-07 Thread GitBox
sv2000 commented on a change in pull request #2667: [GOBBLIN-800] Remove the 
metric context cache from GobblinMetricsRegistry
URL: https://github.com/apache/incubator-gobblin/pull/2667#discussion_r291756656
 
 

 ##
 File path: 
gobblin-runtime/src/main/java/org/apache/gobblin/runtime/util/TaskMetrics.java
 ##
 @@ -61,15 +62,35 @@ public GobblinMetrics call() throws Exception {
 
   /**
* Remove the {@link TaskMetrics} instance for the task with the given 
{@link TaskMetrics} instance.
+   * Please note this method is invoked by job driver so it won't delete any 
underlying {@link ForkMetrics}
+   * because the {@link org.apache.gobblin.runtime.fork.Fork} can be created 
on different nodes.
*
* @param taskState the given {@link TaskState} instance
*/
   public static void remove(TaskState taskState) {
 remove(name(taskState));
   }
 
+  /**
+   * Remove the {@link TaskMetrics} instance for the task with the given 
{@link TaskMetrics} instance.
+   * Please note that this will also delete the underlying {@link ForkMetrics} 
related to this specific task.
+   *
+   * @param task the given task instance
+   */
+  public static void remove(Task task) {
+task.getForks().forEach(forkOpt -> {
+  remove(ForkMetrics.name(task.getTaskState(), forkOpt.get().getIndex()));
+});
+
+remove(name(task));
+  }
+
   private static String name(TaskState taskState) {
-return "gobblin.metrics." + taskState.getJobId() + "." + 
taskState.getTaskId();
+return METRICS_ID_PREFIX + taskState.getJobId() + "." + 
taskState.getTaskId();
+  }
+
+  private static String name(Task task) {
+return METRICS_ID_PREFIX + task.getJobId() + "." + task.getTaskId();
 
 Review comment:
   return name(task.getTaskState())?


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services