Skip to content

Commit d9bd35f

Browse files
CNDB-15920: Add a test for disk usage table state metric after compaction
1 parent bf45d2c commit d9bd35f

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

test/unit/org/apache/cassandra/index/sai/functional/DiskSpaceTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.cassandra.index.sai.SAITester;
2323

2424
import static org.junit.Assert.assertEquals;
25+
import static org.junit.Assert.assertTrue;
2526

2627
public class DiskSpaceTest extends SAITester
2728
{
@@ -52,4 +53,63 @@ public void testTableTotalDiskSpaceUsed() throws Throwable
5253
assertEquals(sstableSize, totalDiskSpaceUsed());
5354
verifyIndexComponentsNotIncludedInSSTable();
5455
}
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+
}
55115
}

0 commit comments

Comments
 (0)