Search in sources :

Example 1 with ObjectPartitionStatisticsImpl

use of org.apache.ignite.internal.processors.query.stat.ObjectPartitionStatisticsImpl in project ignite by apache.

the class GatherPartitionStatistics method processPartition.

/**
 * Decide what column should be gathered and what partition statistics already has and either fix it or
 * collect new data.
 *
 * @param cctx Cache context to get partition from.
 * @return New partition statistics.
 */
private ObjectPartitionStatisticsImpl processPartition(GridCacheContext<?, ?> cctx) {
    ObjectPartitionStatisticsImpl partStat = statRepo.getLocalPartitionStatistics(gathCtx.configuration().key(), partId);
    Map<String, StatisticsColumnConfiguration> colsToCollect = getColumnsToCollect(partStat);
    Set<String> colsToRemove = getColumnsToRemove(partStat);
    // Try to use existing statitsics.
    if (F.isEmpty(colsToCollect))
        return fixExisting(partStat, colsToRemove);
    else
        return recollectPartition(cctx, partStat, colsToCollect, colsToRemove);
}
Also used : ObjectPartitionStatisticsImpl(org.apache.ignite.internal.processors.query.stat.ObjectPartitionStatisticsImpl) StatisticsColumnConfiguration(org.apache.ignite.internal.processors.query.stat.config.StatisticsColumnConfiguration)

Example 2 with ObjectPartitionStatisticsImpl

use of org.apache.ignite.internal.processors.query.stat.ObjectPartitionStatisticsImpl in project ignite by apache.

the class ColumnPartitionDataViewSupplier method columnPartitionStatisticsViewSupplier.

/**
 * Statistics column partition data view filterable supplier.
 *
 * @param filter Filter.
 * @return Iterable with statistics column partition data views.
 */
public Iterable<StatisticsColumnPartitionDataView> columnPartitionStatisticsViewSupplier(Map<String, Object> filter) {
    String type = (String) filter.get(StatisticsColumnPartitionDataViewWalker.TYPE_FILTER);
    if (type != null && !StatisticsColumnConfigurationView.TABLE_TYPE.equalsIgnoreCase(type))
        return Collections.emptyList();
    String schema = (String) filter.get(StatisticsColumnPartitionDataViewWalker.SCHEMA_FILTER);
    String name = (String) filter.get(StatisticsColumnPartitionDataViewWalker.NAME_FILTER);
    Integer partId = (Integer) filter.get(StatisticsColumnPartitionDataViewWalker.PARTITION_FILTER);
    String column = (String) filter.get(StatisticsColumnPartitionDataViewWalker.COLUMN_FILTER);
    Map<StatisticsKey, Collection<ObjectPartitionStatisticsImpl>> partsStatsMap;
    if (!F.isEmpty(schema) && !F.isEmpty(name)) {
        StatisticsKey key = new StatisticsKey(schema, name);
        Collection<ObjectPartitionStatisticsImpl> keyStat;
        if (partId == null)
            keyStat = store.getLocalPartitionsStatistics(key);
        else {
            ObjectPartitionStatisticsImpl partStat = store.getLocalPartitionStatistics(key, partId);
            keyStat = (partStat == null) ? Collections.emptyList() : Collections.singletonList(partStat);
        }
        partsStatsMap = Collections.singletonMap(key, keyStat);
    } else
        partsStatsMap = store.getAllLocalPartitionsStatistics(schema);
    List<StatisticsColumnPartitionDataView> res = new ArrayList<>();
    for (Map.Entry<StatisticsKey, Collection<ObjectPartitionStatisticsImpl>> partsStatsEntry : partsStatsMap.entrySet()) {
        StatisticsKey key = partsStatsEntry.getKey();
        for (ObjectPartitionStatisticsImpl partStat : partsStatsEntry.getValue()) {
            if (column == null) {
                for (Map.Entry<String, ColumnStatistics> colStatEntry : partStat.columnsStatistics().entrySet()) res.add(new StatisticsColumnPartitionDataView(key, colStatEntry.getKey(), partStat));
            } else {
                ColumnStatistics colStat = partStat.columnStatistics(column);
                if (colStat != null)
                    res.add(new StatisticsColumnPartitionDataView(key, column, partStat));
            }
        }
    }
    return res;
}
Also used : ColumnStatistics(org.apache.ignite.internal.processors.query.stat.ColumnStatistics) ArrayList(java.util.ArrayList) ObjectPartitionStatisticsImpl(org.apache.ignite.internal.processors.query.stat.ObjectPartitionStatisticsImpl) StatisticsKey(org.apache.ignite.internal.processors.query.stat.StatisticsKey) Collection(java.util.Collection) Map(java.util.Map)

Example 3 with ObjectPartitionStatisticsImpl

use of org.apache.ignite.internal.processors.query.stat.ObjectPartitionStatisticsImpl in project ignite by apache.

the class GatherPartitionStatistics method fixExisting.

/**
 * Fix existing partition statistics, update repo and return resulting partition statistics.
 *
 * @param partStat Partition statistics to fix.
 * @param colsToRemove Columns to remove.
 * @return New "fixed" partition statistics or existing, if colsToRemove is empty.
 */
private ObjectPartitionStatisticsImpl fixExisting(ObjectPartitionStatisticsImpl partStat, Set<String> colsToRemove) {
    if (log.isDebugEnabled())
        log.debug("Existing parititon statistics fit to configuration requirements. " + "Skipping recollection for " + gathCtx.configuration().key() + "[" + partId + "].");
    ObjectPartitionStatisticsImpl res;
    if (F.isEmpty(colsToRemove))
        // No changes - no need to write existing parition back.
        res = partStat;
    else {
        Map<String, ColumnStatistics> allCols = new HashMap<>(partStat.columnsStatistics());
        for (String col : colsToRemove) allCols.remove(col);
        res = new ObjectPartitionStatisticsImpl(partStat.partId(), getRowCount(allCols), partStat.updCnt(), allCols);
        assert !allCols.isEmpty() : "No columns left after fixing existing partition statistics.";
        statRepo.replaceLocalPartitionStatistics(gathCtx.configuration().key(), res);
    }
    return res;
}
Also used : ObjectPartitionStatisticsImpl(org.apache.ignite.internal.processors.query.stat.ObjectPartitionStatisticsImpl) ColumnStatistics(org.apache.ignite.internal.processors.query.stat.ColumnStatistics) HashMap(java.util.HashMap)

Example 4 with ObjectPartitionStatisticsImpl

use of org.apache.ignite.internal.processors.query.stat.ObjectPartitionStatisticsImpl in project ignite by apache.

the class GatherPartitionStatistics method recollectPartition.

/**
 * Collect some statistics, fix existing in repo and return resulting partition statistics.
 *
 * @param cctx Cache context to get partition from.
 * @param partStat Existing partition statistics to fix or use as a base.
 * @param colsToCollect Columns to collect.
 * @param colsToRemove Columns to remove.
 * @return New partition statistics.
 */
private ObjectPartitionStatisticsImpl recollectPartition(GridCacheContext<?, ?> cctx, ObjectPartitionStatisticsImpl partStat, Map<String, StatisticsColumnConfiguration> colsToCollect, Set<String> colsToRemove) {
    CacheGroupContext grp = cctx.group();
    GridDhtPartitionTopology top = grp.topology();
    AffinityTopologyVersion topVer = top.readyTopologyVersion();
    GridDhtLocalPartition locPart = top.localPartition(partId, topVer, false);
    if (locPart == null)
        throw new GatherStatisticCancelException();
    boolean reserved = locPart.reserve();
    GridH2Table tbl = gathCtx.table();
    ObjectPartitionStatisticsImpl res = null;
    try {
        if (!reserved || (locPart.state() != OWNING)) {
            if (log.isDebugEnabled()) {
                log.debug("Partition not owning. Need to retry [part=" + partId + ", tbl=" + tbl.identifier() + ']');
            }
            throw new GatherStatisticCancelException();
        }
        Column[] cols = IgniteStatisticsHelper.filterColumns(tbl.getColumns(), colsToCollect.keySet());
        ColumnStatisticsCollector[] collectors = new ColumnStatisticsCollector[cols.length];
        for (int i = 0; i < cols.length; ++i) {
            long colCfgVer = colsToCollect.get(cols[i].getName()).version();
            collectors[i] = new ColumnStatisticsCollector(cols[i], tbl::compareTypeSafe, colCfgVer);
        }
        GridH2RowDescriptor rowDesc = tbl.rowDescriptor();
        GridQueryTypeDescriptor typeDesc = rowDesc.type();
        try {
            int checkInt = CANCELLED_CHECK_INTERVAL;
            if (log.isDebugEnabled()) {
                log.debug("Start partition scan [part=" + partId + ", tbl=" + gathCtx.table().identifier() + ']');
            }
            for (CacheDataRow row : grp.offheap().cachePartitionIterator(gathCtx.table().cacheId(), partId, null, false)) {
                if (--checkInt == 0) {
                    if (gathCtx.future().isCancelled())
                        throw new GatherStatisticCancelException();
                    checkInt = CANCELLED_CHECK_INTERVAL;
                }
                if (!typeDesc.matchType(row.value()) || wasExpired(row))
                    continue;
                H2Row h2row = rowDesc.createRow(row);
                for (ColumnStatisticsCollector colStat : collectors) colStat.add(h2row.getValue(colStat.col().getColumnId()));
            }
        } catch (IgniteCheckedException e) {
            log.warning(String.format("Unable to collect partition level statistics by %s.%s:%d due to %s", tbl.identifier().schema(), tbl.identifier().table(), partId, e.getMessage()));
            throw new IgniteException("Unable to collect partition level statistics", e);
        }
        Map<String, ColumnStatistics> colStats = Arrays.stream(collectors).collect(Collectors.toMap(csc -> csc.col().getName(), ColumnStatisticsCollector::finish));
        // Add existing to full replace existing statistics with new one.
        if (partStat != null) {
            for (Map.Entry<String, ColumnStatistics> oldColStat : partStat.columnsStatistics().entrySet()) {
                if (!colsToRemove.contains(oldColStat.getKey()))
                    colStats.putIfAbsent(oldColStat.getKey(), oldColStat.getValue());
            }
        }
        res = new ObjectPartitionStatisticsImpl(partId, getRowCount(colStats), locPart.updateCounter(), colStats);
    } finally {
        if (reserved)
            locPart.release();
    }
    statRepo.replaceLocalPartitionStatistics(gathCtx.configuration().key(), res);
    if (gathCtx.configuration().columns().size() == colsToCollect.size())
        statRepo.refreshObsolescence(gathCtx.configuration().key(), partId);
    return res;
}
Also used : Arrays(java.util.Arrays) IgniteStatisticsRepository(org.apache.ignite.internal.processors.query.stat.IgniteStatisticsRepository) IgniteStatisticsHelper(org.apache.ignite.internal.processors.query.stat.IgniteStatisticsHelper) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) IgniteLogger(org.apache.ignite.IgniteLogger) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) ColumnStatisticsCollector(org.apache.ignite.internal.processors.query.stat.ColumnStatisticsCollector) ColumnStatistics(org.apache.ignite.internal.processors.query.stat.ColumnStatistics) HashSet(java.util.HashSet) Column(org.h2.table.Column) LocalStatisticsGatheringContext(org.apache.ignite.internal.processors.query.stat.LocalStatisticsGatheringContext) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GatherStatisticCancelException(org.apache.ignite.internal.processors.query.stat.GatherStatisticCancelException) Map(java.util.Map) StatisticsColumnConfiguration(org.apache.ignite.internal.processors.query.stat.config.StatisticsColumnConfiguration) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor) F(org.apache.ignite.internal.util.typedef.F) ObjectPartitionStatisticsImpl(org.apache.ignite.internal.processors.query.stat.ObjectPartitionStatisticsImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) Set(java.util.Set) OWNING(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState.OWNING) CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) Collectors(java.util.stream.Collectors) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) Nullable(org.jetbrains.annotations.Nullable) H2Row(org.apache.ignite.internal.processors.query.h2.opt.H2Row) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) Collections(java.util.Collections) ColumnStatisticsCollector(org.apache.ignite.internal.processors.query.stat.ColumnStatisticsCollector) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) GatherStatisticCancelException(org.apache.ignite.internal.processors.query.stat.GatherStatisticCancelException) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor) ObjectPartitionStatisticsImpl(org.apache.ignite.internal.processors.query.stat.ObjectPartitionStatisticsImpl) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Column(org.h2.table.Column) IgniteException(org.apache.ignite.IgniteException) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) H2Row(org.apache.ignite.internal.processors.query.h2.opt.H2Row) CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) ColumnStatistics(org.apache.ignite.internal.processors.query.stat.ColumnStatistics) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ObjectPartitionStatisticsImpl (org.apache.ignite.internal.processors.query.stat.ObjectPartitionStatisticsImpl)4 ColumnStatistics (org.apache.ignite.internal.processors.query.stat.ColumnStatistics)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 StatisticsColumnConfiguration (org.apache.ignite.internal.processors.query.stat.config.StatisticsColumnConfiguration)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Callable (java.util.concurrent.Callable)1 Collectors (java.util.stream.Collectors)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteException (org.apache.ignite.IgniteException)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)1 CacheGroupContext (org.apache.ignite.internal.processors.cache.CacheGroupContext)1 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)1 GridDhtLocalPartition (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition)1