kadirozde commented on a change in pull request #915: URL: https://github.com/apache/phoenix/pull/915#discussion_r506685882
########## File path: phoenix-core/src/main/java/org/apache/phoenix/coprocessor/IndexRepairRegionScanner.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.coprocessor; + +import static org.apache.phoenix.coprocessor.BaseScannerRegionObserver.PHYSICAL_DATA_TABLE_NAME; +import static org.apache.phoenix.query.QueryConstants.AGG_TIMESTAMP; +import static org.apache.phoenix.query.QueryConstants.SINGLE_COLUMN; +import static org.apache.phoenix.query.QueryConstants.SINGLE_COLUMN_FAMILY; +import static org.apache.phoenix.query.QueryConstants.UNGROUPED_AGG_ROW_KEY; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +import org.apache.hadoop.hbase.Cell; +import org.apache.hadoop.hbase.CellUtil; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.KeyValue; +import org.apache.hadoop.hbase.client.Delete; +import org.apache.hadoop.hbase.client.Mutation; +import org.apache.hadoop.hbase.client.Put; +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.coprocessor.RegionCoprocessorEnvironment; +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.util.Bytes; +import org.apache.phoenix.query.HBaseFactoryProvider; +import org.apache.phoenix.util.ByteUtil; +import org.apache.phoenix.compile.ScanRanges; +import org.apache.phoenix.filter.SkipScanFilter; +import org.apache.phoenix.hbase.index.parallel.Task; +import org.apache.phoenix.hbase.index.parallel.TaskBatch; +import org.apache.phoenix.hbase.index.util.ImmutableBytesPtr; +import org.apache.phoenix.mapreduce.index.IndexTool; +import org.apache.phoenix.query.KeyRange; +import org.apache.phoenix.schema.types.PLong; +import org.apache.phoenix.schema.types.PVarbinary; +import org.apache.phoenix.util.KeyValueUtil; +import org.apache.phoenix.util.ServerUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Maps; + +public class IndexRepairRegionScanner extends GlobalIndexRegionScanner { + + private static final Logger LOGGER = LoggerFactory.getLogger(IndexRepairRegionScanner.class); + + public IndexRepairRegionScanner(final RegionScanner innerScanner, + final Region region, + final Scan scan, + final RegionCoprocessorEnvironment env, + final UngroupedAggregateRegionObserver ungroupedAggregateRegionObserver) + throws IOException { + super(innerScanner, region, scan, env, ungroupedAggregateRegionObserver); + + byte[] dataTableName = scan.getAttribute(PHYSICAL_DATA_TABLE_NAME); + dataHTable = hTableFactory.getTable(new ImmutableBytesPtr(dataTableName)); + indexTableTTL = region.getTableDesc().getColumnFamilies()[0].getTimeToLive(); + try (org.apache.hadoop.hbase.client.Connection connection = + HBaseFactoryProvider.getHConnectionFactory().createConnection(env.getConfiguration())) { + regionEndKeys = connection.getRegionLocator(dataHTable.getName()).getEndKeys(); + } + } + + public void prepareExpectedIndexMutations(Result dataRow, Map<byte[], List<Mutation>> expectedIndexMutationMap) throws IOException { + Put put = null; + Delete del = null; + for (Cell cell : dataRow.rawCells()) { + if (KeyValue.Type.codeToType(cell.getTypeByte()) == KeyValue.Type.Put) { + if (put == null) { + put = new Put(CellUtil.cloneRow(cell)); + } + put.add(cell); + } else { + if (del == null) { + del = new Delete(CellUtil.cloneRow(cell)); + } + del.addDeleteMarker(cell); + } + } + List<Mutation> indexMutations = prepareIndexMutationsForRebuild(indexMaintainer, put, del); + Collections.reverse(indexMutations); + for (Mutation mutation : indexMutations) { + byte[] indexRowKey = mutation.getRow(); + List<Mutation> mutationList = expectedIndexMutationMap.get(indexRowKey); + if (mutationList == null) { + mutationList = new ArrayList<>(); + mutationList.add(mutation); + expectedIndexMutationMap.put(indexRowKey, mutationList); + } else { + mutationList.add(mutation); + } + } + } + + private void repairIndexRows(Map<byte[], List<Mutation>> indexMutationMap, + List<Mutation> indexRowsToBeDeleted, + IndexToolVerificationResult verificationResult) throws IOException { + try { + int batchSize = 0; + List<Mutation> indexUpdates = new ArrayList<Mutation>(maxBatchSize); + for (List<Mutation> mutationList : indexMutationMap.values()) { + indexUpdates.addAll(mutationList); + batchSize += mutationList.size(); + if (batchSize >= maxBatchSize) { + ungroupedAggregateRegionObserver.checkForRegionClosing(); + region.batchMutate(indexUpdates.toArray(new Mutation[indexUpdates.size()]), + HConstants.NO_NONCE, HConstants.NO_NONCE); + batchSize = 0; + indexUpdates = new ArrayList<Mutation>(maxBatchSize); + } + } + if (batchSize > 0) { + ungroupedAggregateRegionObserver.checkForRegionClosing(); + region.batchMutate(indexUpdates.toArray(new Mutation[indexUpdates.size()]), + HConstants.NO_NONCE, HConstants.NO_NONCE); + } + batchSize = 0; + indexUpdates = new ArrayList<Mutation>(maxBatchSize); + for (Mutation mutation : indexRowsToBeDeleted) { + indexUpdates.add(mutation); + batchSize ++; + if (batchSize >= maxBatchSize) { + ungroupedAggregateRegionObserver.checkForRegionClosing(); + region.batchMutate(indexUpdates.toArray(new Mutation[indexUpdates.size()]), + HConstants.NO_NONCE, HConstants.NO_NONCE); + batchSize = 0; + indexUpdates = new ArrayList<Mutation>(maxBatchSize); + } + } + if (batchSize > 0) { + ungroupedAggregateRegionObserver.checkForRegionClosing(); + region.batchMutate(indexUpdates.toArray(new Mutation[indexUpdates.size()]), + HConstants.NO_NONCE, HConstants.NO_NONCE); Review comment: 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org