zhipeng93 commented on a change in pull request #18:
URL: https://github.com/apache/flink-ml/pull/18#discussion_r728936588



##########
File path: 
flink-ml-lib/src/main/java/org/apache/flink/ml/common/broadcast/BroadcastContext.java
##########
@@ -0,0 +1,123 @@
+/*
+ * 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.ml.common.broadcast;
+
+import org.apache.flink.api.java.tuple.Tuple2;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class BroadcastContext {
+    /**
+     * Store broadcast DataStreams in a Map. The key is (broadcastName, 
partitionId) and the value
+     * is (isBroaddcastVariableReady, cacheList).
+     */
+    private static Map<Tuple2<String, Integer>, Tuple2<Boolean, List<?>>> 
broadcastVariables =
+            new HashMap<>();
+    /**
+     * We use lock because we want to enable `getBroadcastVariable(String)` in 
a TM with multiple
+     * slots here. Note that using ConcurrentHashMap is not enough since we 
need "contains and get
+     * in an atomic operation".
+     */
+    private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+
+    public static void putBroadcastVariable(
+            Tuple2<String, Integer> key, Tuple2<Boolean, List<?>> variable) {
+        lock.writeLock().lock();
+        try {
+            broadcastVariables.put(key, variable);
+        } finally {
+            lock.writeLock().unlock();
+        }
+    }
+
+    /**
+     * get the cached list with the given key.
+     *
+     * @param key
+     * @param <T>
+     * @return the cache list.
+     */
+    public static <T> List<T> getBroadcastVariable(Tuple2<String, Integer> 
key) {
+        lock.readLock().lock();
+        List<?> result = null;
+        try {
+            result = broadcastVariables.get(key).f1;
+        } finally {
+            lock.readLock().unlock();
+        }
+        return (List<T>) result;
+    }
+
+    /**
+     * get broadcast variables by name
+     *
+     * @param name
+     * @param <T>
+     * @return
+     */
+    public static <T> List<T> getBroadcastVariable(String name) {
+        lock.readLock().lock();
+        List<?> result = null;
+        try {
+            for (Tuple2<String, Integer> nameAndPartitionId : 
broadcastVariables.keySet()) {
+                if (name.equals(nameAndPartitionId.f0) && 
isCacheFinished(nameAndPartitionId)) {
+                    result = broadcastVariables.get(nameAndPartitionId).f1;
+                    break;

Review comment:
       Hi Yunfeng, it is a great observation. I aggree that we can optimize 
here later and let slots in one TM share one copy of the broadcast variables.
   
   For now, partitionId is employed here because we need to clean up the cached 
variables. If we do not know partitionId, we can not decide when to decide the 
clean up for one variable. (Please checkout for CacheStreamOperator#endInput().




-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to