2525
2626import com .datastax .driver .core .ResultSet ;
2727
28+ import static org .apache .cassandra .config .CassandraRelevantProperties .SAI_TABLE_STATE_METRICS_ENABLED ;
2829import static org .apache .cassandra .index .sai .metrics .TableStateMetrics .TABLE_STATE_METRIC_TYPE ;
2930import static org .assertj .core .api .Assertions .assertThatThrownBy ;
3031import static org .junit .Assert .assertEquals ;
@@ -49,9 +50,9 @@ public void testMetricRelease()
4950 createTable (String .format (CREATE_TABLE_TEMPLATE , keyspace , table ));
5051 createIndex (String .format (CREATE_INDEX_TEMPLATE , index , keyspace , table , "v1" ));
5152
52- execute ("INSERT INTO " + keyspace + "." + table + " (id1, v1, v2) VALUES ('0', 0, '0')" );
53+ execute ("INSERT INTO " + keyspace + '.' + table + " (id1, v1, v2) VALUES ('0', 0, '0')" );
5354
54- ResultSet rows = executeNet ("SELECT id1 FROM " + keyspace + "." + table + " WHERE v1 = 0" );
55+ ResultSet rows = executeNet ("SELECT id1 FROM " + keyspace + '.' + table + " WHERE v1 = 0" );
5556 assertEquals (1 , rows .all ().size ());
5657 assertEquals (1L , getTableStateMetrics (keyspace , table , "TotalIndexCount" ));
5758
@@ -75,14 +76,14 @@ public void testMetricCreation()
7576 createIndex (String .format (CREATE_INDEX_TEMPLATE , index +"_v1" , keyspace , table , "v1" ));
7677 createIndex (String .format (CREATE_INDEX_TEMPLATE , index +"_v2" , keyspace , table , "v2" ));
7778
78- execute ("INSERT INTO " + keyspace + "." + table + " (id1, v1, v2) VALUES ('0', 0, '0')" );
79- execute ("INSERT INTO " + keyspace + "." + table + " (id1, v1, v2) VALUES ('1', 1, '1')" );
80- execute ("INSERT INTO " + keyspace + "." + table + " (id1, v1, v2) VALUES ('2', 2, '2')" );
81- execute ("INSERT INTO " + keyspace + "." + table + " (id1, v1, v2) VALUES ('3', 3, '3')" );
79+ execute ("INSERT INTO " + keyspace + '.' + table + " (id1, v1, v2) VALUES ('0', 0, '0')" );
80+ execute ("INSERT INTO " + keyspace + '.' + table + " (id1, v1, v2) VALUES ('1', 1, '1')" );
81+ execute ("INSERT INTO " + keyspace + '.' + table + " (id1, v1, v2) VALUES ('2', 2, '2')" );
82+ execute ("INSERT INTO " + keyspace + '.' + table + " (id1, v1, v2) VALUES ('3', 3, '3')" );
8283
8384 flush (keyspace , table );
8485
85- ResultSet rows = executeNet ("SELECT id1, v1, v2 FROM " + keyspace + "." + table + " WHERE v1 >= 0" );
86+ ResultSet rows = executeNet ("SELECT id1, v1, v2 FROM " + keyspace + '.' + table + " WHERE v1 >= 0" );
8687
8788 int actualRows = rows .all ().size ();
8889 assertEquals (4 , actualRows );
@@ -98,4 +99,55 @@ private int getTableStateMetrics(String keyspace, String table, String metricsNa
9899 {
99100 return (int ) getMetricValue (objectNameNoIndex (metricsName , keyspace , table , TABLE_STATE_METRIC_TYPE ));
100101 }
102+
103+ @ Test
104+ public void testTableStateMetricsEnabledAndDisabled ()
105+ {
106+ testTableStateMetrics (true );
107+ testTableStateMetrics (false );
108+ }
109+
110+ private void testTableStateMetrics (boolean metricsEnabled )
111+ {
112+ SAI_TABLE_STATE_METRICS_ENABLED .setBoolean (metricsEnabled );
113+
114+ try
115+ {
116+ String table = "test_table_state_metrics_" + (metricsEnabled ? "enabled" : "disabled" );
117+ String index = "test_index_" + (metricsEnabled ? "enabled" : "disabled" );
118+
119+ String keyspace = createKeyspace (CREATE_KEYSPACE_TEMPLATE );
120+ createTable (String .format (CREATE_TABLE_TEMPLATE , keyspace , table ));
121+ createIndex (String .format (CREATE_INDEX_TEMPLATE , index , keyspace , table , "v1" ));
122+
123+ // Test all TableStateMetrics Gauge metrics
124+ assertTableStateMetricExistsIfEnabled (metricsEnabled , "TotalIndexCount" , keyspace , table );
125+ assertTableStateMetricExistsIfEnabled (metricsEnabled , "TotalQueryableIndexCount" , keyspace , table );
126+ assertTableStateMetricExistsIfEnabled (metricsEnabled , "TotalIndexBuildsInProgress" , keyspace , table );
127+ assertTableStateMetricExistsIfEnabled (metricsEnabled , "DiskUsedBytes" , keyspace , table );
128+ assertTableStateMetricExistsIfEnabled (metricsEnabled , "DiskPercentageOfBaseTable" , keyspace , table );
129+
130+ // Test indexing operations to ensure null stateMetrics is handled gracefully
131+ execute ("INSERT INTO " + keyspace + '.' + table + " (id1, v1, v2) VALUES ('0', 0, '0')" );
132+ execute ("INSERT INTO " + keyspace + '.' + table + " (id1, v1, v2) VALUES ('1', 1, '1')" );
133+ execute ("INSERT INTO " + keyspace + '.' + table + " (id1, v1, v2) VALUES ('2', 2, '2')" );
134+
135+ // Verify metrics still behave correctly after operations
136+ assertTableStateMetricExistsIfEnabled (metricsEnabled , "TotalIndexCount" , keyspace , table );
137+ assertTableStateMetricExistsIfEnabled (metricsEnabled , "DiskUsedBytes" , keyspace , table );
138+ }
139+ finally
140+ {
141+ // Reset property to default
142+ SAI_TABLE_STATE_METRICS_ENABLED .setBoolean (true );
143+ }
144+ }
145+
146+ void assertTableStateMetricExistsIfEnabled (boolean shouldExist , String metricName , String keyspace , String table )
147+ {
148+ if (shouldExist )
149+ assertMetricExists (objectNameNoIndex (metricName , keyspace , table , TABLE_STATE_METRIC_TYPE ));
150+ else
151+ assertMetricDoesNotExist (objectNameNoIndex (metricName , keyspace , table , TABLE_STATE_METRIC_TYPE ));
152+ }
101153}
0 commit comments