|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.cassandra.db.compression; |
| 20 | + |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.BeforeClass; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import org.apache.cassandra.SchemaLoader; |
| 28 | +import org.apache.cassandra.ServerTestUtils; |
| 29 | +import org.apache.cassandra.db.ColumnFamilyStore; |
| 30 | +import org.apache.cassandra.db.Keyspace; |
| 31 | +import org.apache.cassandra.schema.CompressionParams; |
| 32 | +import org.apache.cassandra.schema.KeyspaceParams; |
| 33 | +import org.apache.cassandra.schema.TableMetadata; |
| 34 | +import org.apache.cassandra.utils.MBeanWrapper; |
| 35 | +import org.apache.cassandra.utils.MBeanWrapper.OnException; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | + |
| 39 | +public class CompressionDictionaryManagerMBeanTest |
| 40 | +{ |
| 41 | + private static final String KEYSPACE_WITH_DICT = "keyspace_mbean_test"; |
| 42 | + private static final String TABLE = "test_table"; |
| 43 | + |
| 44 | + private static ColumnFamilyStore cfsWithDict; |
| 45 | + |
| 46 | + @BeforeClass |
| 47 | + public static void setUpClass() throws Exception |
| 48 | + { |
| 49 | + ServerTestUtils.prepareServer(); |
| 50 | + CompressionParams compressionParams = CompressionParams.zstd(CompressionParams.DEFAULT_CHUNK_LENGTH, true, |
| 51 | + Map.of("compression_level", "3")); |
| 52 | + TableMetadata.Builder tableBuilder = TableMetadata.builder(KEYSPACE_WITH_DICT, TABLE) |
| 53 | + .addPartitionKeyColumn("pk", org.apache.cassandra.db.marshal.UTF8Type.instance) |
| 54 | + .addRegularColumn("data", org.apache.cassandra.db.marshal.UTF8Type.instance) |
| 55 | + .compression(compressionParams); |
| 56 | + SchemaLoader.createKeyspace(KEYSPACE_WITH_DICT, |
| 57 | + KeyspaceParams.simple(1), |
| 58 | + tableBuilder); |
| 59 | + cfsWithDict = Keyspace.open(KEYSPACE_WITH_DICT).getColumnFamilyStore(TABLE); |
| 60 | + } |
| 61 | + |
| 62 | + // Ensure no mbean is registered at the begining of the test |
| 63 | + @Before |
| 64 | + public void cleanup() |
| 65 | + { |
| 66 | + String mbeanName = CompressionDictionaryManager.mbeanName(KEYSPACE_WITH_DICT, TABLE); |
| 67 | + MBeanWrapper.instance.unregisterMBean(mbeanName, OnException.IGNORE); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testMBeanRegisteredWhenBookkeepingEnabled() |
| 72 | + { |
| 73 | + String mbeanName = CompressionDictionaryManager.mbeanName(KEYSPACE_WITH_DICT, TABLE); |
| 74 | + // Create manager with bookkeeping enabled |
| 75 | + try (CompressionDictionaryManager manager = new CompressionDictionaryManager(cfsWithDict, true)) |
| 76 | + { |
| 77 | + // Verify MBean is registered |
| 78 | + assertThat(MBeanWrapper.instance.isRegistered(mbeanName)) |
| 79 | + .as("MBean should be registered when bookkeeping is enabled") |
| 80 | + .isTrue(); |
| 81 | + } |
| 82 | + // Closing manager should unregister the mbean; Verify it is unregistered |
| 83 | + assertThat(MBeanWrapper.instance.isRegistered(mbeanName)) |
| 84 | + .as("MBean should be unregistered after unregisterMbean() call") |
| 85 | + .isFalse(); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testMBeanNotRegisteredWhenBookkeepingDisabled() |
| 90 | + { |
| 91 | + // Create manager with bookkeeping disabled |
| 92 | + try (CompressionDictionaryManager manager = new CompressionDictionaryManager(cfsWithDict, false)) |
| 93 | + { |
| 94 | + // Verify MBean is NOT registered |
| 95 | + String mbeanName = CompressionDictionaryManager.mbeanName(KEYSPACE_WITH_DICT, TABLE);; |
| 96 | + assertThat(MBeanWrapper.instance.isRegistered(mbeanName)) |
| 97 | + .as("MBean should not be registered when bookkeeping is disabled") |
| 98 | + .isFalse(); |
| 99 | + } |
| 100 | + // Closing manager should not throw due to mbean not registered |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testMBeanUnregisteredOnCFSInvalidation() |
| 105 | + { |
| 106 | + String testKeyspace = "test_invalidation_mbean_ks"; |
| 107 | + String testTable = "test_invalidation_mbean_table"; |
| 108 | + |
| 109 | + CompressionParams compressionParams = CompressionParams.zstd(CompressionParams.DEFAULT_CHUNK_LENGTH, true, |
| 110 | + Map.of("compression_level", "3")); |
| 111 | + |
| 112 | + TableMetadata.Builder tableBuilder = TableMetadata.builder(testKeyspace, testTable) |
| 113 | + .addPartitionKeyColumn("pk", org.apache.cassandra.db.marshal.UTF8Type.instance) |
| 114 | + .addRegularColumn("data", org.apache.cassandra.db.marshal.UTF8Type.instance) |
| 115 | + .compression(compressionParams); |
| 116 | + |
| 117 | + SchemaLoader.createKeyspace(testKeyspace, |
| 118 | + KeyspaceParams.simple(1), |
| 119 | + tableBuilder); |
| 120 | + |
| 121 | + ColumnFamilyStore cfs = Keyspace.open(testKeyspace).getColumnFamilyStore(testTable); |
| 122 | + |
| 123 | + String mbeanName = CompressionDictionaryManager.mbeanName(testKeyspace, testTable); |
| 124 | + |
| 125 | + // Verify MBean is registered (CFS registers it during creation) |
| 126 | + assertThat(MBeanWrapper.instance.isRegistered(mbeanName)) |
| 127 | + .as("MBean should be registered after CFS creation") |
| 128 | + .isTrue(); |
| 129 | + |
| 130 | + // Invalidate the CFS (which should unregister the MBean) |
| 131 | + cfs.invalidate(true, true); |
| 132 | + |
| 133 | + // Verify MBean is unregistered |
| 134 | + assertThat(MBeanWrapper.instance.isRegistered(mbeanName)) |
| 135 | + .as("MBean should be unregistered after CFS invalidation") |
| 136 | + .isFalse(); |
| 137 | + } |
| 138 | +} |
0 commit comments