Search in sources :

Example 6 with StatisticsColumnConfiguration

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

the class StatisticsAbstractTest method makeStatistics.

/**
 * Collect or refresh statistics.
 *
 * @param collect If {@code true} - collect new statistics, if {@code false} - update existing.
 * @param targets
 */
private void makeStatistics(boolean collect, StatisticsTarget... targets) {
    try {
        Map<StatisticsTarget, Long> expectedVersion = new HashMap<>();
        IgniteStatisticsManagerImpl statMgr = statisticsMgr(0);
        for (StatisticsTarget target : targets) {
            StatisticsObjectConfiguration currCfg = statMgr.statisticConfiguration().config(target.key());
            Predicate<StatisticsColumnConfiguration> pred;
            if (F.isEmpty(target.columns()))
                pred = c -> true;
            else {
                Set<String> cols = Arrays.stream(target.columns()).collect(Collectors.toSet());
                pred = c -> cols.contains(c.name());
            }
            Long expVer = (currCfg == null) ? 1L : currCfg.columnsAll().values().stream().filter(pred).mapToLong(StatisticsColumnConfiguration::version).min().orElse(0L) + 1;
            expectedVersion.put(target, expVer);
        }
        if (collect)
            statisticsMgr(0).collectStatistics(buildDefaultConfigurations(targets));
        else
            statisticsMgr(0).refreshStatistics(targets);
        awaitStatistics(TIMEOUT, expectedVersion);
    } catch (Exception ex) {
        throw new IgniteException(ex);
    }
}
Also used : Arrays(java.util.Arrays) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) IgniteEx(org.apache.ignite.internal.IgniteEx) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Matcher(java.util.regex.Matcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing) StatisticsObjectConfiguration(org.apache.ignite.internal.processors.query.stat.config.StatisticsObjectConfiguration) IgniteThreadPoolExecutor(org.apache.ignite.thread.IgniteThreadPoolExecutor) StatisticsColumnConfiguration(org.apache.ignite.internal.processors.query.stat.config.StatisticsColumnConfiguration) G(org.apache.ignite.internal.util.typedef.G) F(org.apache.ignite.internal.util.typedef.F) IgniteStatisticsHelper.buildDefaultConfigurations(org.apache.ignite.internal.processors.query.stat.IgniteStatisticsHelper.buildDefaultConfigurations) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Predicate(java.util.function.Predicate) IgniteException(org.apache.ignite.IgniteException) Set(java.util.Set) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) List(java.util.List) Lock(java.util.concurrent.locks.Lock) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) HashMap(java.util.HashMap) StatisticsColumnConfiguration(org.apache.ignite.internal.processors.query.stat.config.StatisticsColumnConfiguration) StatisticsObjectConfiguration(org.apache.ignite.internal.processors.query.stat.config.StatisticsObjectConfiguration) IgniteException(org.apache.ignite.IgniteException) IgniteException(org.apache.ignite.IgniteException)

Example 7 with StatisticsColumnConfiguration

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

the class IgniteStatisticsHelper method aggregateLocalStatistics.

/**
 * Aggregate partition level statistics to local level one or local statistics to global one.
 *
 * @param tbl Table to aggregate statistics by.
 * @param cfg Statistics object configuration.
 * @param stats Collection of partition level or local level statistics to aggregate.
 * @param log Logger.
 * @return Local level statistics.
 */
public static ObjectStatisticsImpl aggregateLocalStatistics(GridH2Table tbl, StatisticsObjectConfiguration cfg, Collection<? extends ObjectStatisticsImpl> stats, IgniteLogger log) {
    assert !stats.isEmpty();
    Column[] selectedCols = filterColumns(tbl.getColumns(), cfg.columns().keySet());
    Map<Column, List<ColumnStatistics>> colPartStats = new HashMap<>(selectedCols.length);
    long rowCnt = 0;
    for (Column col : selectedCols) colPartStats.put(col, new ArrayList<>());
    for (ObjectStatisticsImpl partStat : stats) {
        for (Column col : selectedCols) {
            ColumnStatistics colPartStat = partStat.columnStatistics(col.getName());
            if (colPartStat != null)
                colPartStats.get(col).add(colPartStat);
        }
        rowCnt += partStat.rowCount();
    }
    Map<String, ColumnStatistics> colStats = new HashMap<>(selectedCols.length);
    for (Column col : selectedCols) {
        StatisticsColumnConfiguration colCfg = cfg.columns().get(col.getName());
        ColumnStatistics stat = ColumnStatisticsCollector.aggregate(tbl::compareTypeSafe, colPartStats.get(col), colCfg.overrides());
        if (log.isDebugEnabled())
            log.debug("Aggregate column statistic done [col=" + col.getName() + ", stat=" + stat + ']');
        colStats.put(col.getName(), stat);
    }
    rowCnt = calculateRowCount(cfg, rowCnt);
    return new ObjectStatisticsImpl(rowCnt, colStats);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StatisticsColumnConfiguration(org.apache.ignite.internal.processors.query.stat.config.StatisticsColumnConfiguration) Column(org.h2.table.Column) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with StatisticsColumnConfiguration

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

the class IgniteStatisticsHelper method buildDefaultConfigurations.

/**
 * Build object configurations array with all default parameters from specified targets.
 *
 * @param targets Targets to build configurations from.
 * @return StatisticsObjectConfiguration array.
 */
public static StatisticsObjectConfiguration[] buildDefaultConfigurations(StatisticsTarget... targets) {
    StatisticsObjectConfiguration[] res = Arrays.stream(targets).map(t -> {
        List<StatisticsColumnConfiguration> colCfgs;
        if (t.columns() == null)
            colCfgs = Collections.emptyList();
        else
            colCfgs = Arrays.stream(t.columns()).map(name -> new StatisticsColumnConfiguration(name, null)).collect(Collectors.toList());
        return new StatisticsObjectConfiguration(t.key(), colCfgs, StatisticsObjectConfiguration.DEFAULT_OBSOLESCENCE_MAX_PERCENT);
    }).toArray(StatisticsObjectConfiguration[]::new);
    return res;
}
Also used : SchemaManager(org.apache.ignite.internal.processors.query.h2.SchemaManager) F(org.apache.ignite.internal.util.typedef.F) Arrays(java.util.Arrays) QueryUtils(org.apache.ignite.internal.processors.query.QueryUtils) Collection(java.util.Collection) Set(java.util.Set) HashMap(java.util.HashMap) IgniteLogger(org.apache.ignite.IgniteLogger) UUID(java.util.UUID) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Column(org.h2.table.Column) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) StatisticsKeyMessage(org.apache.ignite.internal.processors.query.stat.messages.StatisticsKeyMessage) Map(java.util.Map) StatisticsObjectConfiguration(org.apache.ignite.internal.processors.query.stat.config.StatisticsObjectConfiguration) StatisticsColumnConfiguration(org.apache.ignite.internal.processors.query.stat.config.StatisticsColumnConfiguration) Collections(java.util.Collections) ArrayList(java.util.ArrayList) List(java.util.List) StatisticsColumnConfiguration(org.apache.ignite.internal.processors.query.stat.config.StatisticsColumnConfiguration) StatisticsObjectConfiguration(org.apache.ignite.internal.processors.query.stat.config.StatisticsObjectConfiguration)

Example 9 with StatisticsColumnConfiguration

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

the class IgniteStatisticsHelper method calculateRowCount.

/**
 * Calculate effective row count. If there are some overrides in statistics configuration - maximum value will be
 * choosen. If not - will return actualRowCount.
 *
 * @param cfg Statistics configuration to dig overrides row count from.
 * @param actualRowCount Actual row count.
 * @return Effective row count.
 */
public static long calculateRowCount(StatisticsObjectConfiguration cfg, long actualRowCount) {
    long overridedRowCnt = -1;
    for (StatisticsColumnConfiguration ccfg : cfg.columns().values()) {
        if (ccfg.overrides() != null && ccfg.overrides().total() != null) {
            Long colRowCnt = ccfg.overrides().total();
            overridedRowCnt = Math.max(overridedRowCnt, colRowCnt);
        }
    }
    return (overridedRowCnt == -1) ? actualRowCount : overridedRowCnt;
}
Also used : StatisticsColumnConfiguration(org.apache.ignite.internal.processors.query.stat.config.StatisticsColumnConfiguration)

Example 10 with StatisticsColumnConfiguration

use of org.apache.ignite.internal.processors.query.stat.config.StatisticsColumnConfiguration 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

StatisticsColumnConfiguration (org.apache.ignite.internal.processors.query.stat.config.StatisticsColumnConfiguration)10 StatisticsObjectConfiguration (org.apache.ignite.internal.processors.query.stat.config.StatisticsObjectConfiguration)6 ArrayList (java.util.ArrayList)5 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 F (org.apache.ignite.internal.util.typedef.F)4 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Function (java.util.function.Function)3 IgniteLogger (org.apache.ignite.IgniteLogger)3 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)3 Column (org.h2.table.Column)3 Collection (java.util.Collection)2 IgniteException (org.apache.ignite.IgniteException)2 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)2