abstractdog commented on code in PR #427:
URL: https://github.com/apache/tez/pull/427#discussion_r2563630356


##########
tez-api/src/main/java/org/apache/tez/frameworkplugins/FrameworkUtils.java:
##########
@@ -0,0 +1,96 @@
+/**
+ * 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.tez.frameworkplugins;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ServiceLoader;
+
+import javax.annotation.Nullable;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.tez.common.ReflectionUtils;
+import org.apache.tez.dag.api.TezConfiguration;
+import org.apache.tez.dag.api.TezConstants;
+import org.apache.tez.dag.api.TezReflectionException;
+
+public final class FrameworkUtils {
+
+  private FrameworkUtils() {}
+
+  /*
+    Searches for a FrameworkService provider which implements a target 
interface.
+    The interface should be either ClientFrameworkService or 
ServerFrameworkService.
+    Depending on which interface is used, either the client or server class of 
a
+    matching FrameworkMode will be used as the implementation.
+
+    NOTE: Layering of FrameworkServices in a decorator-style is currently not 
supported
+
+    An implementation is searched in the following order:
+     1. If conf is not null and the parameter TEZ_FRAMEWORK_MODE is set:
+       the value of TEZ_FRAMEWORK_MODE from the conf will be used
+     2. If conf is null or the parameter TEZ_FRAMEWORK_MODE is not set
+        and the environment var TEZ_FRAMEWORK_MODE is not empty:
+            the value of the environment var will be used
+     3. Otherwise:
+       the default java.util.ServiceLoader behavior will be used,
+       i.e. the implementation classname should appear in a file on the 
classpath at the location
+            
META-INF/services/org.apache.tez.frameworkplugins.ClientFrameworkService
+            or 
META-INF/services/org.apache.tez.frameworkplugins.ServerFrameworkService
+   */
+  public static <T extends FrameworkService> T get(Class<T> interfaze, 
@Nullable Configuration conf) {
+    try {
+      if ((conf != null) && (conf.get(TezConfiguration.TEZ_FRAMEWORK_MODE) != 
null)) {
+        return getByMode(interfaze, 
conf.get(TezConfiguration.TEZ_FRAMEWORK_MODE));
+      } else if (System.getenv(TezConstants.TEZ_FRAMEWORK_MODE) != null) {
+        return getByMode(interfaze, 
System.getenv(TezConstants.TEZ_FRAMEWORK_MODE));
+      } else {
+        return getByServiceLoader(interfaze);
+      }
+    } catch (TezReflectionException e) {
+      throw new RuntimeException("Failed to load framework service for 
interface: " + interfaze.getName(), e);
+    }
+  }
+
+  private static <T extends FrameworkService> T getByServiceLoader(Class<T> 
interfaze) {
+    List<T> services = new ArrayList<>();
+    ServiceLoader<T> frameworkService = ServiceLoader.load(interfaze);
+    for (T service : frameworkService) {
+      services.add(service);
+    }
+    if (services.isEmpty()) {
+      return null;

Review Comment:
   yeah, I was thinking about the same, which might look hacky, but maybe it's 
worth it
   the problem is that this method is in `tez-api`, whereas the server-side 
stuff is in `tez-dag`, and `tez-api` by design cannot depend on `tez-dag`, so 
instead of the null-checks, this might need at least two runtime workarounds:
   
   1. check if `interfaze` is `ServerFrameworkService` or 
`ClientFrameworkService`, and return Yarn one accordingly
   2. instantiate the `YarnServerFrameworkService` by reflection (reflection is 
needed here, because even if moving the `ServerFrameworkService` to `tez-api` 
fine, doing the same with `YarnServerFrameworkService` looks strange, as it's 
Tez AM specific implementation



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to