KarmaGYZ commented on a change in pull request #11854:
URL: https://github.com/apache/flink/pull/11854#discussion_r425550221



##########
File path: 
flink-yarn/src/main/java/org/apache/flink/yarn/ResourceInformationReflector.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.flink.yarn;
+
+import org.apache.flink.annotation.VisibleForTesting;
+
+import org.apache.hadoop.yarn.api.records.Resource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Looks up the methods related to 
org.apache.hadoop.yarn.api.records.Resource#setResourceInformation.
+ * Only supported in Hadoop 3.0+ or 2.10+.
+ */
+class ResourceInformationReflector {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(ResourceInformationReflector.class);
+
+       static final ResourceInformationReflector INSTANCE = new 
ResourceInformationReflector();
+
+       /** Class used to set the extended resource. */
+       private static final String RESOURCE_INFO_CLASS = 
"org.apache.hadoop.yarn.api.records.ResourceInformation";
+
+       /** Could be Null iff isYarnResourceTypesAvailable is false. */
+       @Nullable
+       private final Method resourceSetResourceInformationMethod;
+
+       /** Could be Null iff isYarnResourceTypesAvailable is false. */
+       @Nullable
+       private final Method resourceGetResourcesMethod;
+
+       /** Could be Null iff isYarnResourceTypesAvailable is false. */
+       @Nullable
+       private final Method resourceInformationGetNameMethod;
+
+       /** Could be Null iff isYarnResourceTypesAvailable is false. */
+       @Nullable
+       private final Method resourceInformationGetValueMethod;
+
+       /** Could be Null iff isYarnResourceTypesAvailable is false. */
+       @Nullable
+       private final Method resourceInformationNewInstanceMethod;
+
+       private final boolean isYarnResourceTypesAvailable;
+
+       private ResourceInformationReflector() {
+               this(Resource.class.getName(), RESOURCE_INFO_CLASS);
+       }
+
+       @VisibleForTesting
+       ResourceInformationReflector(String resourceClassName, String 
resourceInfoClassName) {
+               Method resourceSetResourceInformationMethod = null;
+               Method resourceGetResourcesMethod = null;
+               Method resourceInformationGetNameMethod = null;
+               Method resourceInformationGetValueMethod = null;
+               Method resourceInformationNewInstanceMethod = null;
+               boolean isYarnResourceTypesAvailable = false;
+               try {
+                       final Class<?> resourceClass = 
Class.forName(resourceClassName);
+                       final Class<?> resourceInfoClass = 
Class.forName(resourceInfoClassName);
+                       resourceSetResourceInformationMethod = 
resourceClass.getMethod("setResourceInformation", String.class, 
resourceInfoClass);
+                       resourceGetResourcesMethod = 
resourceClass.getMethod("getResources");
+                       resourceInformationGetNameMethod = 
resourceInfoClass.getMethod("getName");
+                       resourceInformationGetValueMethod = 
resourceInfoClass.getMethod("getValue");
+                       resourceInformationNewInstanceMethod = 
resourceInfoClass.getMethod("newInstance", String.class, long.class);
+                       isYarnResourceTypesAvailable = true;
+               } catch (Exception e) {
+                       LOG.debug("The underlying Yarn does not support 
external resource.", e);
+               } finally {
+                       this.resourceSetResourceInformationMethod = 
resourceSetResourceInformationMethod;
+                       this.resourceGetResourcesMethod = 
resourceGetResourcesMethod;
+                       this.resourceInformationGetNameMethod = 
resourceInformationGetNameMethod;
+                       this.resourceInformationGetValueMethod = 
resourceInformationGetValueMethod;
+                       this.resourceInformationNewInstanceMethod = 
resourceInformationNewInstanceMethod;
+                       this.isYarnResourceTypesAvailable = 
isYarnResourceTypesAvailable;

Review comment:
       Since all those fields are `final`, we would also need an `else` branch 
to assign those fields to `null`. I would keep the current 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.

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


Reply via email to