morningman commented on a change in pull request #246: Colocate Join (#245)
URL: https://github.com/apache/incubator-doris/pull/246#discussion_r239303221
 
 

 ##########
 File path: fe/src/main/java/org/apache/doris/catalog/ColocateTableIndex.java
 ##########
 @@ -0,0 +1,366 @@
+// 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.doris.catalog;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Multimap;
+import org.apache.doris.common.io.Writable;
+import org.apache.doris.persist.ColocatePersistInfo;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ * maintain the colocate table related indexes and meta
+ */
+public class ColocateTableIndex implements Writable {
+    private ReentrantReadWriteLock lock;
+
+    //group_id -> table_ids
+    private Multimap<Long, Long> group2Tables;
+    //table_id -> group_id
+    private Map<Long, Long> table2Groups;
+    //group_id -> db_id
+    private Map<Long, Long> group2DBs;
+    //group_id -> bucketSeq -> backends
+    private Map<Long, List<List<Long>>> group2BackendsPerBucketSeq;
+    //the colocate group is balancing
+    private Set<Long> balancingGroups;
+
+    public ColocateTableIndex() {
+        lock = new ReentrantReadWriteLock();
+
+        group2Tables = ArrayListMultimap.create();
+        table2Groups = Maps.newHashMap();
+        group2DBs = Maps.newHashMap();
+        group2BackendsPerBucketSeq = Maps.newHashMap();
+        balancingGroups = new CopyOnWriteArraySet<Long>();
+    }
+
+    private final void readLock() {
+        this.lock.readLock().lock();
+    }
+
+    private final void readUnlock() {
+        this.lock.readLock().unlock();
+    }
+
+    private final void writeLock() {
+        this.lock.writeLock().lock();
+    }
+
+    private final void writeUnlock() {
+        this.lock.writeLock().unlock();
+    }
+
+    public void addTableToGroup(long dbId, long tableId, long groupId) {
+        writeLock();
+        try {
+            group2Tables.put(groupId, tableId);
+            group2DBs.put(groupId, dbId);
+            table2Groups.put(tableId, groupId);
+        } finally {
+            writeUnlock();
+        }
+    }
+
+    public void addBackendsPerBucketSeq(long groupId, List<List<Long>> 
backendsPerBucketSeq) {
+        writeLock();
+        try {
+            group2BackendsPerBucketSeq.put(groupId, backendsPerBucketSeq);
+        } finally {
+            writeUnlock();
+        }
+    }
+
+    public void markGroupBalancing(long groupId) {
+        balancingGroups.add(groupId);
+    }
+
+    public void markGroupStable(long groupId) {
+        balancingGroups.remove(groupId);
+    }
+
+    public void removeTable(long tableId) {
+        long groupId;
+        readLock();
+        try {
+            if (!table2Groups.containsKey(tableId)) {
+                return;
+            }
+            groupId = table2Groups.get(tableId);
+        } finally {
+            readUnlock();
+        }
+
+        removeTableFromGroup(tableId, groupId);
+    }
+
+    private void removeTableFromGroup(long tableId, long groupId) {
+        writeLock();
+        try {
+
+            if (groupId == tableId) {
+                //for parent table
+                group2Tables.removeAll(groupId);
+                group2BackendsPerBucketSeq.remove(groupId);
+                group2DBs.remove(groupId);
+                balancingGroups.remove(groupId);
+            } else {
+                //for child table
+                group2Tables.remove(groupId, tableId);
+            }
+            table2Groups.remove(tableId);
+        } finally {
+            writeUnlock();
+        }
+    }
+
+    public void removeBackendsPerBucketSeq(long groupId) {
+        writeLock();
+        try {
+            group2BackendsPerBucketSeq.remove(groupId);
+        } finally {
+            writeUnlock();
+        }
+    }
+
+    public boolean isGroupBalancing(long groupId) {
+        return balancingGroups.contains(groupId);
+    }
+
+    public boolean isColocateParentTable(long tableId) {
+        readLock();
+        try {
+            return group2Tables.containsKey(tableId);
+        } finally {
+            readUnlock();
+        }
+
+    }
+
+    public boolean isColocateTable(long tableId) {
+        readLock();
+        try {
+            return table2Groups.containsKey(tableId);
+        } finally {
+            readUnlock();
+        }
+    }
+
+    public long getGroup(long tableId) {
+        readLock();
+        try {
+            Preconditions.checkState(table2Groups.containsKey(tableId));
+            return table2Groups.get(tableId);
+        } finally {
+            readUnlock();
+        }
+    }
+
+    public Set<Long> getBalancingGroupIds() {
+        return balancingGroups;
+    }
+
+    public Set<Long> getAllGroupIds() {
 
 Review comment:
   the returning keyset is not protected by lock

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@doris.apache.org
For additional commands, e-mail: dev-h...@doris.apache.org

Reply via email to