Search in sources :

Example 1 with ReducingKeyIterator

use of org.apache.cassandra.io.sstable.ReducingKeyIterator in project cassandra by apache.

the class ViewBuilder method run.

public void run() {
    logger.trace("Running view builder for {}.{}", baseCfs.metadata.keyspace, view.name);
    UUID localHostId = SystemKeyspace.getLocalHostId();
    String ksname = baseCfs.metadata.keyspace, viewName = view.name;
    if (SystemKeyspace.isViewBuilt(ksname, viewName)) {
        if (!SystemKeyspace.isViewStatusReplicated(ksname, viewName))
            updateDistributed(ksname, viewName, localHostId);
        return;
    }
    Iterable<Range<Token>> ranges = StorageService.instance.getLocalRanges(baseCfs.metadata.keyspace);
    final Pair<Integer, Token> buildStatus = SystemKeyspace.getViewBuildStatus(ksname, viewName);
    Token lastToken;
    Function<org.apache.cassandra.db.lifecycle.View, Iterable<SSTableReader>> function;
    if (buildStatus == null) {
        baseCfs.forceBlockingFlush();
        function = org.apache.cassandra.db.lifecycle.View.selectFunction(SSTableSet.CANONICAL);
        int generation = Integer.MIN_VALUE;
        try (Refs<SSTableReader> temp = baseCfs.selectAndReference(function).refs) {
            for (SSTableReader reader : temp) {
                generation = Math.max(reader.descriptor.generation, generation);
            }
        }
        SystemKeyspace.beginViewBuild(ksname, viewName, generation);
        lastToken = null;
    } else {
        function = new Function<org.apache.cassandra.db.lifecycle.View, Iterable<SSTableReader>>() {

            @Nullable
            public Iterable<SSTableReader> apply(org.apache.cassandra.db.lifecycle.View view) {
                Iterable<SSTableReader> readers = org.apache.cassandra.db.lifecycle.View.selectFunction(SSTableSet.CANONICAL).apply(view);
                if (readers != null)
                    return Iterables.filter(readers, ssTableReader -> ssTableReader.descriptor.generation <= buildStatus.left);
                return null;
            }
        };
        lastToken = buildStatus.right;
    }
    prevToken = lastToken;
    try (Refs<SSTableReader> sstables = baseCfs.selectAndReference(function).refs;
        ReducingKeyIterator iter = new ReducingKeyIterator(sstables)) {
        SystemDistributedKeyspace.startViewBuild(ksname, viewName, localHostId);
        while (!isStopped && iter.hasNext()) {
            DecoratedKey key = iter.next();
            Token token = key.getToken();
            if (lastToken == null || lastToken.compareTo(token) < 0) {
                for (Range<Token> range : ranges) {
                    if (range.contains(token)) {
                        buildKey(key);
                        if (prevToken == null || prevToken.compareTo(token) != 0) {
                            SystemKeyspace.updateViewBuildStatus(ksname, viewName, key.getToken());
                            prevToken = token;
                        }
                    }
                }
                lastToken = null;
            }
        }
        if (!isStopped) {
            SystemKeyspace.finishViewBuildStatus(ksname, viewName);
            updateDistributed(ksname, viewName, localHostId);
        }
    } catch (Exception e) {
        ScheduledExecutors.nonPeriodicTasks.schedule(() -> CompactionManager.instance.submitViewBuilder(this), 5, TimeUnit.MINUTES);
        logger.warn("Materialized View failed to complete, sleeping 5 minutes before restarting", e);
    }
}
Also used : Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) ReducingKeyIterator(org.apache.cassandra.io.sstable.ReducingKeyIterator) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) UUID(java.util.UUID) org.apache.cassandra.db(org.apache.cassandra.db) Nullable(javax.annotation.Nullable)

Example 2 with ReducingKeyIterator

use of org.apache.cassandra.io.sstable.ReducingKeyIterator in project spoon by INRIA.

the class SecondaryIndexManager method buildIndexesBlocking.

private void buildIndexesBlocking(Collection<SSTableReader> sstables, Set<Index> indexes) {
    if (indexes.isEmpty())
        return;
    logger.info("Submitting index build of {} for data in {}", indexes.stream().map(i -> i.getIndexMetadata().name).collect(Collectors.joining(",")), sstables.stream().map(SSTableReader::toString).collect(Collectors.joining(",")));
    SecondaryIndexBuilder builder = new SecondaryIndexBuilder(baseCfs, indexes, new ReducingKeyIterator(sstables));
    Future<?> future = CompactionManager.instance.submitIndexBuild(builder);
    FBUtilities.waitOnFuture(future);
    flushIndexesBlocking(indexes);
    logger.info("Index build of {} complete", indexes.stream().map(i -> i.getIndexMetadata().name).collect(Collectors.joining(",")));
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ReducingKeyIterator(org.apache.cassandra.io.sstable.ReducingKeyIterator)

Example 3 with ReducingKeyIterator

use of org.apache.cassandra.io.sstable.ReducingKeyIterator in project cassandra by apache.

the class CassandraIndex method buildBlocking.

@SuppressWarnings("resource")
private void buildBlocking() {
    baseCfs.forceBlockingFlush();
    try (ColumnFamilyStore.RefViewFragment viewFragment = baseCfs.selectAndReference(View.selectFunction(SSTableSet.CANONICAL));
        Refs<SSTableReader> sstables = viewFragment.refs) {
        if (sstables.isEmpty()) {
            logger.info("No SSTable data for {}.{} to build index {} from, marking empty index as built", baseCfs.metadata.keyspace, baseCfs.metadata.name, metadata.name);
            return;
        }
        logger.info("Submitting index build of {} for data in {}", metadata.name, getSSTableNames(sstables));
        SecondaryIndexBuilder builder = new CollatedViewIndexBuilder(baseCfs, Collections.singleton(this), new ReducingKeyIterator(sstables), ImmutableSet.copyOf(sstables));
        Future<?> future = CompactionManager.instance.submitIndexBuild(builder);
        FBUtilities.waitOnFuture(future);
        indexCfs.forceBlockingFlush();
    }
    logger.info("Index build of {} complete", metadata.name);
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ReducingKeyIterator(org.apache.cassandra.io.sstable.ReducingKeyIterator)

Example 4 with ReducingKeyIterator

use of org.apache.cassandra.io.sstable.ReducingKeyIterator in project cassandra by apache.

the class CompactionAllocationTest method testIndexingWidePartitions.

private static void testIndexingWidePartitions(String name, int numSSTable, int sstablePartitions, IndexDef... indexes) throws Throwable {
    String ksname = "ks_" + name.toLowerCase();
    SchemaLoader.createKeyspace(ksname, KeyspaceParams.simple(1), CreateTableStatement.parse("CREATE TABLE tbl (k text, c text, v1 text, v2 text, v3 text, v4 text, PRIMARY KEY (k, c))", ksname).build());
    ColumnFamilyStore cfs = Schema.instance.getColumnFamilyStoreInstance(Schema.instance.getTableMetadata(ksname, "tbl").id);
    Assert.assertNotNull(cfs);
    cfs.disableAutoCompaction();
    int rowWidth = 100;
    int rowsPerPartition = 1000;
    measure(new Workload() {

        @SuppressWarnings("UnstableApiUsage")
        public void setup() {
            cfs.disableAutoCompaction();
            String insert = String.format("INSERT INTO %s.%s (k, c, v1, v2, v3, v4) VALUES (?, ?, ?, ?, ?, ?)", ksname, "tbl");
            for (int f = 0; f < numSSTable; f++) {
                for (int p = 0; p < sstablePartitions; p++) {
                    String key = String.format("%08d", (f * sstablePartitions) + p);
                    for (int r = 0; r < rowsPerPartition; r++) {
                        QueryProcessor.executeInternal(insert, key, makeRandomString(6, -1), makeRandomString(rowWidth >> 2), makeRandomString(rowWidth >> 2), makeRandomString(rowWidth >> 2), makeRandomString(rowWidth >> 2));
                    }
                }
                cfs.forceBlockingFlush();
            }
            for (IndexDef index : indexes) {
                QueryProcessor.executeInternal(String.format(index.cql, index.name, ksname, "tbl"));
                while (!cfs.indexManager.getBuiltIndexNames().contains(index.name)) Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
            }
            Assert.assertEquals(numSSTable, cfs.getLiveSSTables().size());
        }

        public ColumnFamilyStore getCfs() {
            return cfs;
        }

        public List<Runnable> getReads() {
            return new ArrayList<>();
        }

        public String name() {
            return name;
        }

        public int executeReads() {
            // return 1 to avoid divide by zero error
            return 1;
        }

        public void executeCompactions() {
            logger.info("Starting index re-build");
            try (ColumnFamilyStore.RefViewFragment viewFragment = cfs.selectAndReference(View.selectFunction(SSTableSet.CANONICAL));
                Refs<SSTableReader> sstables = viewFragment.refs) {
                Set<Index> indexes = new HashSet<>(cfs.indexManager.listIndexes());
                SecondaryIndexBuilder builder = new CollatedViewIndexBuilder(cfs, indexes, new ReducingKeyIterator(sstables), ImmutableSet.copyOf(sstables));
                builder.build();
            }
            logger.info("Index re-build complete");
        }

        public int[] getSSTableStats() {
            int numPartitions = cfs.getLiveSSTables().stream().mapToInt(sstable -> Ints.checkedCast(sstable.getSSTableMetadata().estimatedPartitionSize.count())).sum();
            int numRows = cfs.getLiveSSTables().stream().mapToInt(sstable -> Ints.checkedCast(sstable.getSSTableMetadata().totalRows)).sum();
            return new int[] { numPartitions, numRows };
        }
    });
}
Also used : SSTableSet(org.apache.cassandra.db.lifecycle.SSTableSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) Refs(org.apache.cassandra.utils.concurrent.Refs) ReducingKeyIterator(org.apache.cassandra.io.sstable.ReducingKeyIterator) SecondaryIndexBuilder(org.apache.cassandra.index.SecondaryIndexBuilder) CollatedViewIndexBuilder(org.apache.cassandra.index.internal.CollatedViewIndexBuilder) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) List(java.util.List) ArrayList(java.util.ArrayList)

Example 5 with ReducingKeyIterator

use of org.apache.cassandra.io.sstable.ReducingKeyIterator in project cassandra by apache.

the class ViewBuilderTask method call.

public Long call() {
    String ksName = baseCfs.metadata.keyspace;
    if (prevToken == null)
        logger.debug("Starting new view build for range {}", range);
    else
        logger.debug("Resuming view build for range {} from token {} with {} covered keys", range, prevToken, keysBuilt);
    /*
         * It's possible for view building to start before MV creation got propagated to other nodes. For this reason
         * we should wait for schema to converge before attempting to send any view mutations to other nodes, or else
         * face UnknownTableException upon Mutation deserialization on the nodes that haven't processed the schema change.
         */
    boolean schemaConverged = Gossiper.instance.waitForSchemaAgreement(10, TimeUnit.SECONDS, () -> this.isStopped);
    if (!schemaConverged)
        logger.warn("Failed to get schema to converge before building view {}.{}", baseCfs.keyspace.getName(), view.name);
    Function<org.apache.cassandra.db.lifecycle.View, Iterable<SSTableReader>> function;
    function = org.apache.cassandra.db.lifecycle.View.select(SSTableSet.CANONICAL, s -> range.intersects(s.getBounds()));
    try (ColumnFamilyStore.RefViewFragment viewFragment = baseCfs.selectAndReference(function);
        Refs<SSTableReader> sstables = viewFragment.refs;
        ReducingKeyIterator keyIter = new ReducingKeyIterator(sstables)) {
        PeekingIterator<DecoratedKey> iter = Iterators.peekingIterator(keyIter);
        while (!isStopped && iter.hasNext()) {
            DecoratedKey key = iter.next();
            Token token = key.getToken();
            // skip tokens already built or not present in range
            if (range.contains(token) && (prevToken == null || token.compareTo(prevToken) > 0)) {
                buildKey(key);
                ++keysBuilt;
                // build other keys sharing the same token
                while (iter.hasNext() && iter.peek().getToken().equals(token)) {
                    key = iter.next();
                    buildKey(key);
                    ++keysBuilt;
                }
                if (keysBuilt % ROWS_BETWEEN_CHECKPOINTS == 1)
                    SystemKeyspace.updateViewBuildStatus(ksName, view.name, range, token, keysBuilt);
                prevToken = token;
            }
        }
    }
    finish();
    return keysBuilt;
}
Also used : ReadExecutionController(org.apache.cassandra.db.ReadExecutionController) Rows(org.apache.cassandra.db.rows.Rows) ReadQuery(org.apache.cassandra.db.ReadQuery) SSTableSet(org.apache.cassandra.db.lifecycle.SSTableSet) LoggerFactory(org.slf4j.LoggerFactory) Range(org.apache.cassandra.dht.Range) Callable(java.util.concurrent.Callable) CompactionInterruptedException(org.apache.cassandra.db.compaction.CompactionInterruptedException) Global.nanoTime(org.apache.cassandra.utils.Clock.Global.nanoTime) PeekingIterator(com.google.common.collect.PeekingIterator) Iterators(com.google.common.collect.Iterators) SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) Gossiper(org.apache.cassandra.gms.Gossiper) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) DecoratedKey(org.apache.cassandra.db.DecoratedKey) Token(org.apache.cassandra.dht.Token) Mutation(org.apache.cassandra.db.Mutation) OperationType(org.apache.cassandra.db.compaction.OperationType) UnfilteredRowIterator(org.apache.cassandra.db.rows.UnfilteredRowIterator) SinglePartitionReadCommand(org.apache.cassandra.db.SinglePartitionReadCommand) Objects(com.google.common.base.Objects) ReducingKeyIterator(org.apache.cassandra.io.sstable.ReducingKeyIterator) Refs(org.apache.cassandra.utils.concurrent.Refs) Unit(org.apache.cassandra.db.compaction.CompactionInfo.Unit) Function(com.google.common.base.Function) Logger(org.slf4j.Logger) FBUtilities(org.apache.cassandra.utils.FBUtilities) Iterator(java.util.Iterator) Collection(java.util.Collection) UnfilteredRowIterators(org.apache.cassandra.db.rows.UnfilteredRowIterators) UUID(java.util.UUID) CompactionInfo(org.apache.cassandra.db.compaction.CompactionInfo) UUIDGen(org.apache.cassandra.utils.UUIDGen) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) DeletionTime(org.apache.cassandra.db.DeletionTime) StorageProxy(org.apache.cassandra.service.StorageProxy) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) UnfilteredPartitionIterators(org.apache.cassandra.db.partitions.UnfilteredPartitionIterators) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) DecoratedKey(org.apache.cassandra.db.DecoratedKey) Token(org.apache.cassandra.dht.Token) ReducingKeyIterator(org.apache.cassandra.io.sstable.ReducingKeyIterator) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore)

Aggregations

ReducingKeyIterator (org.apache.cassandra.io.sstable.ReducingKeyIterator)9 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)6 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)3 UUID (java.util.UUID)2 SSTableSet (org.apache.cassandra.db.lifecycle.SSTableSet)2 Range (org.apache.cassandra.dht.Range)2 Token (org.apache.cassandra.dht.Token)2 SecondaryIndexBuilder (org.apache.cassandra.index.SecondaryIndexBuilder)2 Refs (org.apache.cassandra.utils.concurrent.Refs)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Function (com.google.common.base.Function)1 Objects (com.google.common.base.Objects)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterators (com.google.common.collect.Iterators)1 PeekingIterator (com.google.common.collect.PeekingIterator)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1