kadirozde commented on code in PR #5545:
URL: https://github.com/apache/hbase/pull/5545#discussion_r1443673538


##########
hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/DualFileWriter.java:
##########
@@ -0,0 +1,253 @@
+/*
+ * 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.hadoop.hbase.regionserver;
+
+import static 
org.apache.hadoop.hbase.regionserver.HStoreFile.HAS_LIVE_VERSIONS_KEY;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.hadoop.hbase.Cell;
+import org.apache.hadoop.hbase.CellComparator;
+import org.apache.hadoop.hbase.CellUtil;
+import org.apache.hadoop.hbase.io.hfile.HFile;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.yetus.audience.InterfaceAudience;
+
+/**
+ * Separates the provided cells into two files, one file for the live cells 
and the other for the
+ * rest of the cells (historical cells). The live cells includes the live put 
cells, delete all and
+ * version delete markers that are not masked by other delete all markers.
+ */
[email protected]
+public class DualFileWriter extends AbstractMultiFileWriter {
+
+  private final CellComparator comparator;
+  private StoreFileWriter liveVersionWriter;
+  private StoreFileWriter historicalVersionWriter;
+
+  private final List<StoreFileWriter> writers;
+  // The last cell of the current row
+  private Cell lastCell;
+  // The first (latest) delete family marker of the current row
+  private Cell deleteFamily;
+  // The list of delete family version markers of the current row
+  private List<Cell> deleteFamilyVersionList = new ArrayList<>();
+  // The first (latest) delete column marker of the current column
+  private Cell deleteColumn;
+  // The list of delete column version markers of the current column
+  private List<Cell> deleteColumnVersionList = new ArrayList<>();
+  // The live put cell count for the current column
+  private int livePutCellCount;
+  private final boolean dualWriterEnabled;
+  private final int maxVersions;
+  private final boolean newVersionBehavior;
+
+  public DualFileWriter(CellComparator comparator, int maxVersions, boolean 
dualWriterEnabled,
+    boolean newVersionBehavior) {
+    this.comparator = comparator;
+    this.maxVersions = maxVersions;
+    this.dualWriterEnabled = dualWriterEnabled;
+    this.newVersionBehavior = newVersionBehavior;
+    writers = new ArrayList<>(2);
+    initRowState();
+  }
+
+  private void initRowState() {
+    deleteFamily = null;
+    deleteFamilyVersionList.clear();
+    lastCell = null;
+  }
+
+  private void initColumnState() {
+    livePutCellCount = 0;
+    deleteColumn = null;
+    deleteColumnVersionList.clear();
+
+  }
+
+  private void addLiveVersion(Cell cell) throws IOException {
+    if (liveVersionWriter == null) {
+      liveVersionWriter = writerFactory.createWriter();
+      writers.add(liveVersionWriter);
+    }
+    liveVersionWriter.append(cell);
+  }
+
+  private void addHistoricalVersion(Cell cell) throws IOException {
+    if (historicalVersionWriter == null) {
+      historicalVersionWriter = writerFactory.createWriter();
+      writers.add(historicalVersionWriter);
+    }
+    historicalVersionWriter.append(cell);
+  }
+
+  private boolean isDeletedByDeleteFamily(Cell cell) {
+    return deleteFamily != null && (deleteFamily.getTimestamp() > 
cell.getTimestamp()
+      || (deleteFamily.getTimestamp() == cell.getTimestamp()
+        && (!newVersionBehavior || cell.getSequenceId() < 
deleteFamily.getSequenceId())));
+  }
+
+  private boolean isDeletedByDeleteFamilyVersion(Cell cell) {
+    for (Cell deleteFamilyVersion : deleteFamilyVersionList) {
+      if (
+        deleteFamilyVersion.getTimestamp() == cell.getTimestamp()
+          && (!newVersionBehavior || cell.getSequenceId() < 
deleteFamilyVersion.getSequenceId())
+      ) return true;
+    }
+    return false;
+  }
+
+  private boolean isDeletedByDeleteColumn(Cell cell) {
+    return deleteColumn != null && (deleteColumn.getTimestamp() > 
cell.getTimestamp()
+      || (deleteColumn.getTimestamp() == cell.getTimestamp()
+        && (!newVersionBehavior || cell.getSequenceId() < 
deleteColumn.getSequenceId())));
+  }
+
+  private boolean isDeletedByDeleteColumnVersion(Cell cell) {
+    for (Cell deleteColumnVersion : deleteColumnVersionList) {
+      if (
+        deleteColumnVersion.getTimestamp() == cell.getTimestamp()
+          && (!newVersionBehavior || cell.getSequenceId() < 
deleteColumnVersion.getSequenceId())
+      ) return true;
+    }
+    return false;
+  }
+
+  private boolean isDeleted(Cell cell) {
+    return isDeletedByDeleteFamily(cell) || isDeletedByDeleteColumn(cell)
+      || isDeletedByDeleteFamilyVersion(cell) || 
isDeletedByDeleteColumnVersion(cell);
+  }
+
+  private void appendCell(Cell cell) throws IOException {
+    if ((lastCell == null || !CellUtil.matchingColumn(lastCell, cell))) {
+      initColumnState();
+    }
+    if (cell.getType() == Cell.Type.DeleteFamily) {
+      if (deleteFamily == null) {
+        deleteFamily = cell;
+        addLiveVersion(cell);
+      } else {
+        addHistoricalVersion(cell);
+      }
+    } else if (cell.getType() == Cell.Type.DeleteFamilyVersion) {
+      if (!isDeletedByDeleteFamily(cell)) {

Review Comment:
   Dual file compaction is an optimization and does not guarantee that live 
version files will include only live cell versions. Since minor compaction does 
not see all the cell versions, dual file compaction may include cells that are 
not actually live since the decision is done based on a subset of files in the 
case of minor compaction.  I just wanted to make sure we have the same 
understanding on this.
   
   If a DeleteFamily and DeleteFamilyVersion marker both have same timestamp 
and the DeleteFamilyVersion marker is not deleted/masked by the DeleteFamily 
marker then, the sequence id of the DeleteFamilyVersion marker must be higher 
than that of the DeleteFamily marker. In this case, we still need to track the 
DeleteFamilyVersion marker (i.e., need to add to the delete family version 
list). This tracking is necessary to make sure that the put cell with the same 
timestamp will be deleted by the DeleteFamilyVersion marker. And then we can 
append the DeleteFamilyVersion marker to the historical version file as you 
suggested. The same is also applicable to the Delete marker. I will make the 
changes for this in the next commit.
   



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