palashc commented on code in PR #2057:
URL: https://github.com/apache/phoenix/pull/2057#discussion_r1931621343


##########
phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/PhoenixMasterObserver.java:
##########
@@ -173,50 +219,113 @@ private List<String> getAncestorIds(Connection conn, 
String tableName, String st
                     Bytes.toStringBinary(regionInfoB.getStartKey()),
                     Bytes.toStringBinary(regionInfoB.getEndKey())));
         }
+        // if parent was a result of a merge, there will be multiple 
grandparents.
+        while (rs.next()) {
+            ancestorIDs.add(rs.getString(2));
+        }
         return ancestorIDs;
     }
 
     /**
-     * Insert partition metadata for a daughter region from the split.
+     * Lookup the parent of a merged region.
+     * If the merged region was an output of a merge in the past, it will have 
multiple parents.
      */
-    private void upsertDaughterPartition(Connection conn, String tableName,
-                                         String streamName, String 
parentPartitionID,
-                                         RegionInfo regionInfo)
-            throws SQLException {
-        String partitionId = regionInfo.getEncodedName();
-        long startTime = regionInfo.getRegionId();
-        byte[] startKey = regionInfo.getStartKey();
-        byte[] endKey = regionInfo.getEndKey();
-        PreparedStatement pstmt = conn.prepareStatement(PARTITION_UPSERT_SQL);
+    private List<String> getAncestorIdsForMerge(Connection conn, String 
tableName, String streamName,
+                                                RegionInfo parent) throws 
SQLException {
+        List<String> ancestorIDs = new ArrayList<>();
+        ancestorIDs.add(parent.getEncodedName());
+        PreparedStatement pstmt = 
conn.prepareStatement(PARENT_PARTITION_QUERY_FOR_MERGE);
         pstmt.setString(1, tableName);
         pstmt.setString(2, streamName);
-        pstmt.setString(3, partitionId);
-        pstmt.setString(4, parentPartitionID);
-        pstmt.setLong(5, startTime);
-        // endTime in not set when inserting a new partition
-        pstmt.setNull(6, Types.BIGINT);
-        pstmt.setBytes(7, startKey.length == 0 ? null : startKey);
-        pstmt.setBytes(8, endKey.length == 0 ? null : endKey);
-        pstmt.executeUpdate();
+        pstmt.setString(3, parent.getEncodedName());
+        ResultSet rs = pstmt.executeQuery();
+        if (rs.next()) {
+            ancestorIDs.add(rs.getString(1));
+        } else {
+            throw new SQLException(String.format(
+                    "Could not find parent of the provided merged region: {}", 
parent.getEncodedName()));
+        }
+        // if parent was a result of a merge, there will be multiple 
grandparents.
+        while (rs.next()) {
+            ancestorIDs.add(rs.getString(1));
+        }
+        return ancestorIDs;
+    }
+
+    /**
+     * Insert partition metadata for a daughter region from a split or a merge.
+     * split: 2 daughters, 1 parent
+     * merge: 1 daughter, N parents
+     */
+    private void upsertDaughterPartitions(Connection conn, String tableName,
+                                         String streamName, List<String> 
parentPartitionIDs,
+                                         List<RegionInfo> daughters)
+            throws SQLException {
+        PreparedStatement pstmt = conn.prepareStatement(PARTITION_UPSERT_SQL);
+        for (RegionInfo daughter : daughters) {
+            for (String parentPartitionID : parentPartitionIDs) {
+                String partitionId = daughter.getEncodedName();
+                long startTime = daughter.getRegionId();
+                byte[] startKey = daughter.getStartKey();
+                byte[] endKey = daughter.getEndKey();
+                pstmt.setString(1, tableName);
+                pstmt.setString(2, streamName);
+                pstmt.setString(3, partitionId);
+                pstmt.setString(4, parentPartitionID);
+                pstmt.setLong(5, startTime);
+                // endTime in not set when inserting a new partition
+                pstmt.setNull(6, Types.BIGINT);
+                pstmt.setBytes(7, startKey.length == 0 ? null : startKey);
+                pstmt.setBytes(8, endKey.length == 0 ? null : endKey);
+                pstmt.executeUpdate();
+            }
+        }
         conn.commit();

Review Comment:
   Agreed, done!



-- 
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...@phoenix.apache.org

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

Reply via email to