phoenix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ramkrish86 <...@git.apache.org>
Subject [GitHub] phoenix pull request: Phoenix-180
Date Fri, 12 Sep 2014 05:30:30 GMT
Github user ramkrish86 commented on a diff in the pull request:

    https://github.com/apache/phoenix/pull/12#discussion_r17463589
  
    --- Diff: phoenix-core/src/main/java/org/apache/phoenix/schema/stat/StatisticsCollector.java
---
    @@ -0,0 +1,344 @@
    +/*
    + * 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.schema.stat;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.TreeMap;
    +import java.util.TreeSet;
    +
    +import org.apache.commons.logging.Log;
    +import org.apache.commons.logging.LogFactory;
    +import org.apache.hadoop.hbase.Cell;
    +import org.apache.hadoop.hbase.Coprocessor;
    +import org.apache.hadoop.hbase.CoprocessorEnvironment;
    +import org.apache.hadoop.hbase.HTableDescriptor;
    +import org.apache.hadoop.hbase.KeyValue;
    +import org.apache.hadoop.hbase.KeyValueUtil;
    +import org.apache.hadoop.hbase.client.Scan;
    +import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
    +import org.apache.hadoop.hbase.coprocessor.CoprocessorException;
    +import org.apache.hadoop.hbase.coprocessor.CoprocessorService;
    +import org.apache.hadoop.hbase.coprocessor.ObserverContext;
    +import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
    +import org.apache.hadoop.hbase.protobuf.ResponseConverter;
    +import org.apache.hadoop.hbase.regionserver.HRegion;
    +import org.apache.hadoop.hbase.regionserver.InternalScanner;
    +import org.apache.hadoop.hbase.regionserver.KeyValueScanner;
    +import org.apache.hadoop.hbase.regionserver.RegionScanner;
    +import org.apache.hadoop.hbase.regionserver.ScanType;
    +import org.apache.hadoop.hbase.regionserver.Store;
    +import org.apache.hadoop.hbase.regionserver.StoreScanner;
    +import org.apache.hadoop.hbase.util.Bytes;
    +import org.apache.phoenix.coprocessor.generated.StatCollectorProtos;
    +import org.apache.phoenix.coprocessor.generated.StatCollectorProtos.StatCollectRequest;
    +import org.apache.phoenix.coprocessor.generated.StatCollectorProtos.StatCollectResponse;
    +import org.apache.phoenix.coprocessor.generated.StatCollectorProtos.StatCollectResponse.Builder;
    +import org.apache.phoenix.coprocessor.generated.StatCollectorProtos.StatCollectService;
    +import org.apache.phoenix.hbase.index.util.ImmutableBytesPtr;
    +import org.apache.phoenix.schema.PDataType;
    +import org.apache.phoenix.schema.PhoenixArray;
    +import org.apache.phoenix.util.SchemaUtil;
    +
    +import com.google.protobuf.RpcCallback;
    +import com.google.protobuf.RpcController;
    +import com.google.protobuf.Service;
    +
    +/**
    + * An endpoint implementation that allows to collect the stats for a given region and
groups the stat per family. This is also an
    + * RegionObserver that collects the information on compaction also. The user would be
allowed to invoke this endpoint and thus populate the
    + * Phoenix stats table with the max key, min key and guide posts for the given region.
The stats can be consumed by the stats associated
    + * with every PTable and the same can be used to parallelize the queries
    + */
    +public class StatisticsCollector extends BaseRegionObserver implements CoprocessorService,
Coprocessor,
    +        StatisticsTracker, StatCollectService.Interface {
    +
    +    public static void addToTable(HTableDescriptor desc) throws IOException {
    +        desc.addCoprocessor(StatisticsCollector.class.getName());
    +    }
    +
    +    private Map<String, byte[]> minMap = new TreeMap<String, byte[]>();
    +    private Map<String, byte[]> maxMap = new TreeMap<String, byte[]>();
    +    private long guidepostDepth;
    +    private long byteCount = 0;
    +    private TreeMap<String, List<byte[]>> guidePostsMap = new TreeMap<String,
List<byte[]>>();
    +    private Set<byte[]> familyMap = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
    +    private RegionCoprocessorEnvironment env;
    +    protected StatisticsTable stats;
    +    private static final Log LOG = LogFactory.getLog(StatCollectService.class);
    +
    +    @Override
    +    public void collectStat(RpcController controller, StatCollectRequest request, RpcCallback<StatCollectResponse>
done) {
    +        HRegion region = env.getRegion();
    +        if (familyMap != null) {
    +            familyMap.clear();
    +        }
    +        Scan scan = createScan();
    +        if (request.hasStartRow()) {
    +            scan.setStartRow(request.getStartRow().toByteArray());
    +        }
    +        if (request.hasStopRow()) {
    +            scan.setStopRow(request.getStopRow().toByteArray());
    +        }
    +        RegionScanner scanner = null;
    +        int count = 0;
    +        try {
    +            scanner = region.getScanner(scan);
    +            count = scanRegion(scanner, count);
    +        } catch (IOException e) {
    +            LOG.error(e);
    +            ResponseConverter.setControllerException(controller, e);
    +        } finally {
    +            if (scanner != null) {
    +                try {
    +                    writeStatsToStatsTable(request, region, scanner);
    +                } catch (IOException e) {
    +                    LOG.error(e);
    +                    ResponseConverter.setControllerException(controller, e);
    +                }
    +            }
    +        }
    +        Builder newBuilder = StatCollectResponse.newBuilder();
    +        newBuilder.setRowsScanned(count);
    +        StatCollectResponse result = newBuilder.build();
    +        done.run(result);
    +    }
    +
    +    private void writeStatsToStatsTable(final StatCollectRequest request, final HRegion
region,
    +            final RegionScanner scanner) throws IOException {
    +        scanner.close();
    +        try {
    +            // update the statistics table
    +            for (byte[] fam : familyMap) {
    +                String tableName = null;
    +                String schemaName = null;
    +                if (request != null) {
    +                    tableName = Bytes.toString(request.getTableNameBytes().toByteArray());
    +                    schemaName = Bytes.toString(request.getSchemaNameBytes().toByteArray());
    +                } else {
    +                    tableName = SchemaUtil.getTableNameFromFullName(region.getRegionInfo().getTable().getNameAsString());
    +                    schemaName = SchemaUtil.getSchemaNameFromFullName(tableName);
    +                }
    +                stats.updateStats(tableName, schemaName, (region.getRegionInfo().getRegionNameAsString()),
this,
    +                        Bytes.toString(fam), request.getUrl());
    +            }
    +        } catch (IOException e) {
    +            LOG.error("Failed to update statistics table!", e);
    +            throw e;
    +        }
    +    }
    +
    +    private int scanRegion(RegionScanner scanner, int count) throws IOException {
    +        List<Cell> results = new ArrayList<Cell>();
    +        boolean hasMore = true;
    +        while (hasMore) {
    +            // Am getting duplicates here. Need to avoid that
    +            hasMore = scanner.next(results);
    +            updateStat(results);
    +            count += results.size();
    +            results.clear();
    +            while (!hasMore) {
    +                break;
    +            }
    +        }
    +        return count;
    +    }
    +
    +    /**
    +     * Update the current statistics based on the lastest batch of key-values from the
underlying scanner
    +     * 
    +     * @param results
    +     *            next batch of {@link KeyValue}s
    +     */
    +    protected void updateStat(final List<Cell> results) {
    +        for (Cell c : results) {
    +            updateStatistic(KeyValueUtil.ensureKeyValue(c));
    +        }
    +    }
    +
    +    @Override
    +    public Service getService() {
    +        return StatCollectorProtos.StatCollectService.newReflectiveService(this);
    +    }
    +
    +    @Override
    +    public void start(CoprocessorEnvironment env) throws IOException {
    +        if (env instanceof RegionCoprocessorEnvironment) {
    +            this.env = (RegionCoprocessorEnvironment)env;
    +        } else {
    +            throw new CoprocessorException("Must be loaded on a table region!");
    +        }
    +        HTableDescriptor desc = ((RegionCoprocessorEnvironment)env).getRegion().getTableDesc();
    +        // Get the stats table associated with the current table on which the CP is
    +        // triggered
    +        stats = StatisticsTable.getStatisticsTableForCoprocessor(env, desc.getName());
    +        guidepostDepth = env.getConfiguration().getLong(StatisticsConstants.HISTOGRAM_BYTE_DEPTH_CONF_KEY,
    +                StatisticsConstants.HISTOGRAM_DEFAULT_BYTE_DEPTH);
    +    }
    +
    +    @Override
    +    public void stop(CoprocessorEnvironment arg0) throws IOException {
    +        stats.close();
    +    }
    +
    +    @Override
    +    public InternalScanner preCompactScannerOpen(ObserverContext<RegionCoprocessorEnvironment>
c, Store store,
    +            List<? extends KeyValueScanner> scanners, ScanType scanType, long earliestPutTs,
InternalScanner s)
    +            throws IOException {
    +        InternalScanner internalScan = s;
    +        // See if this is for Major compaction
    +        if (scanType.equals(ScanType.COMPACT_DROP_DELETES)) {
    +            // this is the first CP accessed, so we need to just create a major
    +            // compaction scanner, just
    +            // like in the compactor
    +            if (s == null) {
    +                Scan scan = new Scan();
    +                scan.setMaxVersions(store.getFamily().getMaxVersions());
    +                long smallestReadPoint = store.getSmallestReadPoint();
    +                internalScan = new StoreScanner(store, store.getScanInfo(), scan, scanners,
scanType,
    +                        smallestReadPoint, earliestPutTs);
    +            }
    +            InternalScanner scanner = getInternalScanner(c, store, internalScan, store.getColumnFamilyName());
    +            if (scanner != null) {
    +                internalScan = scanner;
    +            }
    +        }
    +        return internalScan;
    +    }
    +
    +    @Override
    +    public void postSplit(ObserverContext<RegionCoprocessorEnvironment> ctx, HRegion
l, HRegion r) throws IOException {
    --- End diff --
    
    I think NOT only analyze even when major compation happens we need to delete the older
guide posts.
    Currently we have HTable.rowMutate() where we could add different mutations for the same
row. (Put and delete).  So we would like to issue the delete for the row key with tablename,
family and region name and then create the put with the latest stats in the same rowMutate().
But as we don't know the ts of the latest rowkey if we just issue a Put and Delete it would
mask even the latest one. Hence it we have to issue deleteColumn(fam, qual) for every qualifier
in the table and then add the put to the RowMutation object. The point to be noted here is
that deletecolumn(fam, qual) is costly as it tries to get the latest ts before actually deleting
the column.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message