Search in sources :

Example 1 with CollatedViewIndexBuilder

use of org.apache.cassandra.index.internal.CollatedViewIndexBuilder 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 2 with CollatedViewIndexBuilder

use of org.apache.cassandra.index.internal.CollatedViewIndexBuilder in project cassandra by apache.

the class CancelCompactionsTest method testIndexRebuild.

/**
 * Make sure index rebuilds get cancelled
 */
@Test
public void testIndexRebuild() throws ExecutionException, InterruptedException {
    ColumnFamilyStore cfs = MockSchema.newCFS();
    List<SSTableReader> sstables = createSSTables(cfs, 5, 0);
    Index idx = new StubIndex(cfs, null);
    CountDownLatch indexBuildStarted = new CountDownLatch(1);
    CountDownLatch indexBuildRunning = new CountDownLatch(1);
    CountDownLatch compactionsStopped = new CountDownLatch(1);
    ReducingKeyIterator reducingKeyIterator = new ReducingKeyIterator(sstables) {

        @Override
        public boolean hasNext() {
            indexBuildStarted.countDown();
            try {
                indexBuildRunning.await();
            } catch (InterruptedException e) {
                throw new RuntimeException();
            }
            return false;
        }
    };
    Future<?> f = CompactionManager.instance.submitIndexBuild(new CollatedViewIndexBuilder(cfs, Collections.singleton(idx), reducingKeyIterator, ImmutableSet.copyOf(sstables)));
    // wait for hasNext to get called
    indexBuildStarted.await();
    assertEquals(1, getActiveCompactionsForTable(cfs).size());
    boolean foundCompaction = false;
    for (CompactionInfo.Holder holder : getActiveCompactionsForTable(cfs)) {
        if (holder.getCompactionInfo().getSSTables().equals(new HashSet<>(sstables))) {
            assertFalse(holder.isStopRequested());
            foundCompaction = true;
        }
    }
    assertTrue(foundCompaction);
    cfs.runWithCompactionsDisabled(() -> {
        compactionsStopped.countDown();
        return null;
    }, (sstable) -> true, false, false, true);
    // wait for the runWithCompactionsDisabled callable
    compactionsStopped.await();
    assertEquals(1, getActiveCompactionsForTable(cfs).size());
    foundCompaction = false;
    for (CompactionInfo.Holder holder : getActiveCompactionsForTable(cfs)) {
        if (holder.getCompactionInfo().getSSTables().equals(new HashSet<>(sstables))) {
            assertTrue(holder.isStopRequested());
            foundCompaction = true;
        }
    }
    assertTrue(foundCompaction);
    // signal that the index build should be finished
    indexBuildRunning.countDown();
    f.get();
    assertTrue(getActiveCompactionsForTable(cfs).isEmpty());
}
Also used : StubIndex(org.apache.cassandra.index.StubIndex) Index(org.apache.cassandra.index.Index) StubIndex(org.apache.cassandra.index.StubIndex) CountDownLatch(java.util.concurrent.CountDownLatch) ReducingKeyIterator(org.apache.cassandra.io.sstable.ReducingKeyIterator) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) CollatedViewIndexBuilder(org.apache.cassandra.index.internal.CollatedViewIndexBuilder) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Aggregations

ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)2 CollatedViewIndexBuilder (org.apache.cassandra.index.internal.CollatedViewIndexBuilder)2 ReducingKeyIterator (org.apache.cassandra.io.sstable.ReducingKeyIterator)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 SSTableSet (org.apache.cassandra.db.lifecycle.SSTableSet)1 Index (org.apache.cassandra.index.Index)1 SecondaryIndexBuilder (org.apache.cassandra.index.SecondaryIndexBuilder)1 StubIndex (org.apache.cassandra.index.StubIndex)1 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)1 Refs (org.apache.cassandra.utils.concurrent.Refs)1 Test (org.junit.Test)1