phoenix-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [phoenix] kadirozde commented on a change in pull request #469: PHOENIX-5156 Consistent Global Indexes for Non-Transactional Tables
Date Thu, 25 Apr 2019 19:59:54 GMT
kadirozde commented on a change in pull request #469: PHOENIX-5156 Consistent Global Indexes
for Non-Transactional Tables
URL: https://github.com/apache/phoenix/pull/469#discussion_r278708430
 
 

 ##########
 File path: phoenix-core/src/main/java/org/apache/phoenix/index/GlobalIndexChecker.java
 ##########
 @@ -0,0 +1,426 @@
+/*
+ * 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.phoenix.index;
+
+import static org.apache.phoenix.coprocessor.BaseScannerRegionObserver.CHECK_VERIFY_COLUMN;
+import static org.apache.phoenix.coprocessor.BaseScannerRegionObserver.EMPTY_COLUMN_FAMILY_NAME;
+import static org.apache.phoenix.coprocessor.BaseScannerRegionObserver.EMPTY_COLUMN_QUALIFIER_NAME;
+import static org.apache.phoenix.coprocessor.BaseScannerRegionObserver.PHYSICAL_DATA_TABLE_NAME;
+import static org.apache.phoenix.index.IndexMaintainer.getIndexMaintainer;
+import static org.apache.phoenix.schema.types.PDataType.FALSE_BYTES;
+import static org.apache.phoenix.schema.types.PDataType.TRUE_BYTES;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NavigableSet;
+import java.util.Optional;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.hbase.Cell;
+import org.apache.hadoop.hbase.DoNotRetryIOException;
+import org.apache.hadoop.hbase.client.Delete;
+import org.apache.hadoop.hbase.client.Get;
+import org.apache.hadoop.hbase.client.Mutation;
+import org.apache.hadoop.hbase.client.RegionInfo;
+import org.apache.hadoop.hbase.client.Result;
+import org.apache.hadoop.hbase.client.ResultScanner;
+import org.apache.hadoop.hbase.client.Scan;
+import org.apache.hadoop.hbase.client.Table;
+import org.apache.hadoop.hbase.coprocessor.ObserverContext;
+import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
+import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
+import org.apache.hadoop.hbase.coprocessor.RegionObserver;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.hbase.regionserver.Region;
+import org.apache.hadoop.hbase.regionserver.RegionScanner;
+import org.apache.hadoop.hbase.regionserver.ScannerContext;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.coprocessor.BaseScannerRegionObserver;
+import org.apache.phoenix.query.QueryServices;
+import org.apache.phoenix.query.QueryServicesOptions;
+import org.apache.phoenix.util.EnvironmentEdgeManager;
+import org.apache.phoenix.util.IndexUtil;
+import org.apache.phoenix.util.SchemaUtil;
+import org.apache.phoenix.util.ServerUtil;
+
+/**
+ * 
+ * Coprocessor that verifies the scanned rows of a non-transactional global index.
+ *
+ */
+public class GlobalIndexChecker implements RegionCoprocessor, RegionObserver {
+    private static final Log LOG = LogFactory.getLog(GlobalIndexChecker.class);
+    /**
+     * Class that verifies a given row of a non-transactional global index.
+     * An instance of this class is created for each scanner on an index
+     * and used to verify individual rows and rebuild them if they are not valid
+     */
+    private static class GlobalIndexScanner implements RegionScanner {
+        RegionScanner scanner;
+        private long ageThreshold;
+        private Scan scan;
+        private Scan indexScan;
+        private Scan buildIndexScan = null;
+        private Table dataHTable;
+        private byte[] emptyCF;
+        private byte[] emptyCQ;
+        private IndexMaintainer indexMaintainer = null;
+        private byte[][] viewConstants = null;
+        private RegionCoprocessorEnvironment env;
+        private Region region;
+        private long minTimestamp;
+        private long maxTimestamp;
+
+        public GlobalIndexScanner(RegionCoprocessorEnvironment env, Scan scan, RegionScanner
scanner) throws IOException {
+            this.env = env;
+            this.scan = scan;
+            region = env.getRegion();
+            this.scanner = scanner;
+            emptyCF = scan.getAttribute(EMPTY_COLUMN_FAMILY_NAME);
+            emptyCQ = scan.getAttribute(EMPTY_COLUMN_QUALIFIER_NAME);
+            ageThreshold = env.getConfiguration().getLong(
+                    QueryServices.GLOBAL_INDEX_ROW_AGE_THRESHOLD_TO_DELETE_MS_ATTRIB,
+                    QueryServicesOptions.DEFAULT_GLOBAL_INDEX_ROW_AGE_THRESHOLD_TO_DELETE_MS);
+            minTimestamp = scan.getTimeRange().getMin();
+            maxTimestamp = scan.getTimeRange().getMax();
+        }
+
+        @Override
+        public int getBatch() {
+            return scanner.getBatch();
+        }
+
+        @Override
+        public long getMaxResultSize() {
+            return scanner.getMaxResultSize();
+        }
+
+        @Override
+        public boolean next(List<Cell> result) throws IOException {
+            try {
+                boolean hasMore;
+                do {
+                    hasMore = scanner.next(result);
+                    if (result.isEmpty()) {
+                        break;
+                    }
+                    if (verifyRowAndReconstructIfNecessary(result)) {
+                        break;
+                    }
+                    // skip this row as it is invalid
+                    // if there is no more row, then result will be an empty list
+                } while (hasMore);
+                return hasMore;
+            } catch (Throwable t) {
+                ServerUtil.throwIOException(region.getRegionInfo().getRegionNameAsString(),
t);
+                return false; // impossible
+            }
+        }
+
+        @Override
+        public boolean next(List<Cell> result, ScannerContext scannerContext) throws
IOException {
+            throw new IOException("next with scannerContext should not be called in Phoenix
environment");
+        }
+
+        @Override
+        public boolean nextRaw(List<Cell> result, ScannerContext scannerContext) throws
IOException {
+            throw new IOException("NextRaw with scannerContext should not be called in Phoenix
environment");
+        }
+
+        @Override
+        public void close() throws IOException {
+            scanner.close();
+        }
+
+        @Override
+        public RegionInfo getRegionInfo() {
+            return scanner.getRegionInfo();
+        }
+
+        @Override
+        public boolean isFilterDone() throws IOException {
+            return scanner.isFilterDone();
+        }
+
+        @Override
+        public boolean reseek(byte[] row) throws IOException {
+            return scanner.reseek(row);
+        }
+
+        @Override
+        public long getMvccReadPoint() {
+            return scanner.getMvccReadPoint();
+        }
+
+
+        @Override
+        public boolean nextRaw(List<Cell> result) throws IOException {
+            try {
+                boolean hasMore;
+                do {
+                    hasMore = scanner.nextRaw(result);
+                    if (result.isEmpty()) {
+                        break;
+                    }
+                    if (verifyRowAndReconstructIfNecessary(result)) {
+                        break;
+                    }
+                    // skip this row as it is invalid
+                    // if there is no more row, then result will be an empty list
+                } while (hasMore);
+                return hasMore;
+            } catch (Throwable t) {
+                ServerUtil.throwIOException(region.getRegionInfo().getRegionNameAsString(),
t);
+                return false; // impossible
+            }
+        }
+
+        private void deleteRowIfAgedEnough(byte[] indexRowKey, long ts) throws IOException
{
+            if ((EnvironmentEdgeManager.currentTimeMillis() - ts) > ageThreshold) {
+                Delete del = new Delete(indexRowKey);
+                Mutation[] mutations = new Mutation[]{del};
+                region.batchMutate(mutations);
+            }
+        }
+
+        private List<Cell> rebuildSingleIndexRow(byte[] indexRowKey, long ts, List<Cell>
row) throws IOException {
+            // Build the data table row key from the index table row key
+            if (buildIndexScan == null) {
+                buildIndexScan = new Scan();
+                indexScan = new Scan(scan);
+                byte[] dataTableName = scan.getAttribute(PHYSICAL_DATA_TABLE_NAME);
+                byte[] indexTableName = region.getRegionInfo().getTable().getName();
+                dataHTable = ServerUtil.getHTableForCoprocessorScan(env,
+                        SchemaUtil.getPhysicalTableName(dataTableName, env.getConfiguration()));
+                if (indexMaintainer == null) {
+                    byte[] md = scan.getAttribute(PhoenixIndexCodec.INDEX_PROTO_MD);
+                    List<IndexMaintainer> maintainers = IndexMaintainer.deserialize(md,
true);
+                    indexMaintainer = getIndexMaintainer(maintainers, indexTableName);
+                }
+                if (indexMaintainer == null) {
+                    throw new DoNotRetryIOException(
+                            "rebuildSingleIndexRow: IndexMaintainer is not included in scan
attributes for " +
+                                    region.getRegionInfo().getTable().getNameAsString());
+                }
+                if (viewConstants == null) {
+                    viewConstants = IndexUtil.deserializeViewConstantsFromScan(scan);
+                }
+                buildIndexScan.setAttribute(BaseScannerRegionObserver.UNGROUPED_AGG, TRUE_BYTES);
+                buildIndexScan.setAttribute(PhoenixIndexCodec.INDEX_PROTO_MD, scan.getAttribute(PhoenixIndexCodec.INDEX_PROTO_MD));
+                buildIndexScan.setAttribute(BaseScannerRegionObserver.REBUILD_INDEXES, TRUE_BYTES);
+            }
+            // Rebuild the index row from the corresponding the row in the the data table
+            byte[] dataRowKey = indexMaintainer.buildDataRowKey(new ImmutableBytesWritable(indexRowKey),
viewConstants);
+            buildIndexScan.withStartRow(dataRowKey, true);
+            buildIndexScan.withStopRow(dataRowKey, true);
+            buildIndexScan.setRaw(true);
+            ResultScanner resultScanner = dataHTable.getScanner(buildIndexScan);
+            if (resultScanner.next() == null) {
+                // The corresponding row does not exist in the data table. Need to delete
the row from index if it is old enough
+                deleteRowIfAgedEnough(indexRowKey, ts);
+                row.clear();
+                return row;
+            }
+            // Scan the newly build row
+            indexScan.withStartRow(indexRowKey, true);
+            indexScan.withStopRow(indexRowKey, true);
+            RegionScanner regionScanner = region.getScanner(indexScan);
+            regionScanner.next(row);
+            regionScanner.close();
+            if (!row.isEmpty() && !verifyRowAndRemoveVerifiedCell(row)) {
+                deleteRowIfAgedEnough(indexRowKey, ts);
+                row.clear();
+            }
+            return row;
+        }
+
+        private boolean isVerifiedCell(Cell cell) {
+            return Bytes.compareTo(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength(),
+                    emptyCF, 0, emptyCF.length) == 0 &&
+                    Bytes.compareTo(cell.getQualifierArray(), cell.getQualifierOffset(),
cell.getQualifierLength(),
+                            emptyCQ, 0, emptyCQ.length) == 0;
+        }
+
+        private boolean verifyRow(byte[] rowKey, long ts) throws IOException {
+            Get get = new Get(rowKey);
+            get.setTimeRange(minTimestamp, maxTimestamp);
+            get.addColumn(emptyCF, emptyCQ);
+            Result result = region.get(get);
+            if (result.isEmpty()) {
+                return true;
+            }
+            if (Bytes.compareTo(result.getValue(emptyCF, emptyCQ), 0, FALSE_BYTES.length,
+                    FALSE_BYTES, 0, FALSE_BYTES.length) == 0) {
+                return false;
+            }
+            return true;
+        }
+
+        private boolean verifyRowAndRemoveVerifiedCell(List<Cell> cellList) throws
IOException {
+            boolean valid = true;
+            boolean verifiedColumnExist = false;
+            long verifiedTs = 0;
+            long deleteTs = 0;
+            long maxTs;
+            long ts = 0;
+            long cellListSize = cellList.size();
+            Cell cell = null;
+            maxTs = 0;
+            if (cellListSize == 0) {
+                return true;
+            }
+            Iterator<Cell> cellIterator = cellList.iterator();
+            while (cellIterator.hasNext()) {
+                cell = cellIterator.next();
+                ts = cell.getTimestamp();
+                if (ts > maxTs) {
+                    maxTs = ts;
+                }
+                if (cell.getType() != Cell.Type.Put) {
+                    if (Bytes.compareTo(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength(),
+                            emptyCF, 0, emptyCF.length) == 0) {
+                        if (ts > deleteTs) {
+                            deleteTs = ts;
+                        }
+                    }
+                    continue;
+                }
+                if (isVerifiedCell(cell)) {
+                    verifiedColumnExist = true;
+                    if (Bytes.compareTo(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(),
+                            FALSE_BYTES, 0, FALSE_BYTES.length) == 0) {
+                        if (verifiedTs < ts) {
+                            valid = false;
+                            verifiedTs = ts;
+                        }
+                    } else {
+                        if (verifiedTs < ts) {
+                            valid = true;
+                            verifiedTs = ts;
+                        }
+                    }
+                    if (cellListSize > 1) {
+                        cellIterator.remove();
+                    }
+                }
+            }
+            if (deleteTs >= maxTs) {
+                return true;
+            }
+            if (!verifiedColumnExist) {
+                byte[] rowKey = new byte[cell.getRowLength()];
+                System.arraycopy(cell.getRowArray(), cell.getRowOffset(), rowKey, 0, cell.getRowLength());
+                valid = verifyRow(rowKey, maxTs);
+            }
+            if (!valid || (verifiedColumnExist && verifiedTs != maxTs)) {
+                return false;
+            }
+            return true;
+        }
+
+        private long getMaxTimestamp(List<Cell> cellList) {
 
 Review comment:
   These functions are not the same. One takes a Mutation and the other List<Cell>.
Their implementations are also different.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message