1996fanrui commented on code in PR #710:
URL: 
https://github.com/apache/flink-kubernetes-operator/pull/710#discussion_r1394045676


##########
flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/autoscaler/state/ConfigMapStore.java:
##########
@@ -99,49 +93,30 @@ public void removeInfoFromCache(ResourceID resourceID) {
         cache.remove(resourceID);
     }
 
-    private Optional<ConfigMap> getConfigMap(KubernetesJobAutoScalerContext 
jobContext) {
+    private ConfigMapView getConfigMap(KubernetesJobAutoScalerContext 
jobContext) {
         return cache.computeIfAbsent(
                 jobContext.getJobKey(), (id) -> 
getConfigMapFromKubernetes(jobContext));
     }
 
-    private Map<String, String> 
getOrCreateState(KubernetesJobAutoScalerContext jobContext) {
+    private ConfigMapView getOrCreateState(KubernetesJobAutoScalerContext 
jobContext) {
         return cache.compute(
-                        jobContext.getJobKey(),
-                        (id, configMapOpt) -> {
-                            // If in the cache and valid simply return
-                            if (configMapOpt != null && 
configMapOpt.isPresent()) {
-                                return configMapOpt;
-                            }
-                            // Otherwise get or create
-                            return 
Optional.of(getOrCreateConfigMapFromKubernetes(jobContext));
-                        })
-                .get()
-                .getData();
+                jobContext.getJobKey(),
+                (id, configMapView) -> {
+                    // If in the cache and valid simply return
+                    if (configMapView != null) {
+                        return configMapView;
+                    }
+                    // Otherwise retrieve if it exists
+                    return getConfigMapFromKubernetes(jobContext);
+                });
     }
 
     @VisibleForTesting
-    protected Optional<ConfigMap> getConfigMapFromKubernetes(
-            KubernetesJobAutoScalerContext jobContext) {
+    ConfigMapView getConfigMapFromKubernetes(KubernetesJobAutoScalerContext 
jobContext) {

Review Comment:
   ```suggestion
       ConfigMapView createConfigMapView(KubernetesJobAutoScalerContext 
jobContext) {
   ```
   
   nit



##########
flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/autoscaler/state/ConfigMapView.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.flink.kubernetes.operator.autoscaler.state;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.util.Preconditions;
+
+import io.fabric8.kubernetes.api.model.ConfigMap;
+import io.fabric8.kubernetes.client.dsl.Resource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.function.Function;
+
+class ConfigMapView {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(ConfigMapView.class);
+
+    enum State {
+        /** ConfigMap is only stored locally, not created in Kubernetes yet. */
+        NEEDS_CREATE,
+        /** ConfigMap exists in Kubernetes but there are newer local changes. 
*/
+        NEEDS_UPDATE,
+        /** ConfigMap view reflects the actual contents of Kubernetes 
ConfigMap. */
+        UP_TO_DATE
+    }
+
+    private State state;
+
+    private ConfigMap configMap;
+
+    private final Function<ConfigMap, Resource<ConfigMap>> resourceRetriever;
+
+    public ConfigMapView(
+            ConfigMap configMap, Function<ConfigMap, Resource<ConfigMap>> 
resourceRetriever) {

Review Comment:
   ```suggestion
               ConfigMap newConfigMap, Function<ConfigMap, Resource<ConfigMap>> 
resourceRetriever) {
   ```
   
   How about rename the configMap to `newConfigMap` or `emptyConfigMap`? It's 
clear to let us know it's only used when `resourceRetriever.get()` is null.



##########
flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/autoscaler/state/ConfigMapStore.java:
##########
@@ -99,49 +93,30 @@ public void removeInfoFromCache(ResourceID resourceID) {
         cache.remove(resourceID);
     }
 
-    private Optional<ConfigMap> getConfigMap(KubernetesJobAutoScalerContext 
jobContext) {
+    private ConfigMapView getConfigMap(KubernetesJobAutoScalerContext 
jobContext) {
         return cache.computeIfAbsent(
                 jobContext.getJobKey(), (id) -> 
getConfigMapFromKubernetes(jobContext));
     }
 
-    private Map<String, String> 
getOrCreateState(KubernetesJobAutoScalerContext jobContext) {
+    private ConfigMapView getOrCreateState(KubernetesJobAutoScalerContext 
jobContext) {
         return cache.compute(
-                        jobContext.getJobKey(),
-                        (id, configMapOpt) -> {
-                            // If in the cache and valid simply return
-                            if (configMapOpt != null && 
configMapOpt.isPresent()) {
-                                return configMapOpt;
-                            }
-                            // Otherwise get or create
-                            return 
Optional.of(getOrCreateConfigMapFromKubernetes(jobContext));
-                        })
-                .get()
-                .getData();
+                jobContext.getJobKey(),
+                (id, configMapView) -> {
+                    // If in the cache and valid simply return
+                    if (configMapView != null) {
+                        return configMapView;
+                    }

Review Comment:
   nit: How about using the `computeIfAbsent` instead of `compute`? If so, we 
don't need to care about `configMapView != null`



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