|
22 | 22 | import org.apache.cassandra.index.sai.SAITester; |
23 | 23 |
|
24 | 24 | import static org.junit.Assert.assertEquals; |
| 25 | +import static org.junit.Assert.assertTrue; |
25 | 26 |
|
26 | 27 | public class DiskSpaceTest extends SAITester |
27 | 28 | { |
@@ -52,4 +53,63 @@ public void testTableTotalDiskSpaceUsed() throws Throwable |
52 | 53 | assertEquals(sstableSize, totalDiskSpaceUsed()); |
53 | 54 | verifyIndexComponentsNotIncludedInSSTable(); |
54 | 55 | } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testDiskUsageWithCompactionAfterUpdates() throws Throwable |
| 59 | + { |
| 60 | + createTable(CREATE_TABLE_TEMPLATE); |
| 61 | + |
| 62 | + // Insert initial data and flush to create first SSTable |
| 63 | + int rows = 100; |
| 64 | + for (int j = 0; j < rows; j++) |
| 65 | + execute("INSERT INTO %s (id1, v1) VALUES (?, ?)", Integer.toString(j), j); |
| 66 | + |
| 67 | + flush(); |
| 68 | + |
| 69 | + assertEquals("Should have 1 SSTable after first flush", 1, getCurrentColumnFamilyStore().getLiveSSTables().size()); |
| 70 | + long sstableSizeAfterFirstFlush = totalDiskSpaceUsed(); |
| 71 | + |
| 72 | + // Update the same keys with different values to create overlapping data |
| 73 | + for (int j = 0; j < rows; j++) |
| 74 | + execute("INSERT INTO %s (id1, v1) VALUES (?, ?)", Integer.toString(j), j + 1000); |
| 75 | + |
| 76 | + flush(); |
| 77 | + |
| 78 | + assertEquals("Should have 2 SSTables after second flush", 2, getCurrentColumnFamilyStore().getLiveSSTables().size()); |
| 79 | + long sstableSizeBeforeCompaction = totalDiskSpaceUsed(); |
| 80 | + |
| 81 | + // Verify that we have more disk usage with 2 SSTables containing overlapping data |
| 82 | + assertTrue("Disk usage should increase with overlapping SSTables", |
| 83 | + sstableSizeBeforeCompaction > sstableSizeAfterFirstFlush); |
| 84 | + |
| 85 | + // Compact to merge the updates before creating index |
| 86 | + compact(); |
| 87 | + waitForCompactionsFinished(); |
| 88 | + |
| 89 | + assertEquals("Should have 1 SSTable after compaction", 1, getCurrentColumnFamilyStore().getLiveSSTables().size()); |
| 90 | + long sstableSizeAfterCompaction = totalDiskSpaceUsed(); |
| 91 | + |
| 92 | + // Compaction should reduce disk usage by merging duplicate keys |
| 93 | + assertTrue("Compaction should reduce disk usage by merging duplicates", |
| 94 | + sstableSizeAfterCompaction < sstableSizeBeforeCompaction); |
| 95 | + |
| 96 | + // Create index on the compacted SSTable |
| 97 | + String indexName = createIndex(String.format(CREATE_INDEX_TEMPLATE, "v1")); |
| 98 | + |
| 99 | + long indexSize = indexDiskSpaceUse(); |
| 100 | + long sstableSizeWithIndex = totalDiskSpaceUsed(); |
| 101 | + |
| 102 | + assertTrue("Index size should be positive", indexSize > 0); |
| 103 | + |
| 104 | + // Verify that total disk usage equals base table plus index after compaction |
| 105 | + assertEquals("Total disk should equal base table plus index after compaction", |
| 106 | + sstableSizeAfterCompaction + indexSize, sstableSizeWithIndex); |
| 107 | + verifyIndexComponentsIncludedInSSTable(); |
| 108 | + |
| 109 | + // Drop index and verify disk space accounting |
| 110 | + dropIndex("DROP INDEX %s." + indexName); |
| 111 | + assertEquals("After dropping index, disk usage should equal base table only", |
| 112 | + sstableSizeAfterCompaction, totalDiskSpaceUsed()); |
| 113 | + verifyIndexComponentsNotIncludedInSSTable(); |
| 114 | + } |
55 | 115 | } |
0 commit comments