|
| 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.config; |
| 20 | + |
| 21 | +import org.assertj.core.api.Assertions; |
| 22 | +import org.junit.BeforeClass; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import static org.junit.Assert.assertEquals; |
| 26 | +import static org.junit.Assert.assertNull; |
| 27 | + |
| 28 | +/** |
| 29 | + * Tests the guardrail thresholds a {@code cassandra.yaml} can carry, which {@link GuardrailsOptions} validates on |
| 30 | + * construction. See CASSANDRA-21517 for the treatment of zero. |
| 31 | + */ |
| 32 | +public class GuardrailsOptionsTest |
| 33 | +{ |
| 34 | + @BeforeClass |
| 35 | + public static void setupDatabaseDescriptor() |
| 36 | + { |
| 37 | + DatabaseDescriptor.daemonInitialization(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testZeroThresholdsAreAccepted() |
| 42 | + { |
| 43 | + Config config = new Config(); |
| 44 | + config.tables_fail_threshold = 0; |
| 45 | + config.keyspaces_warn_threshold = 0; |
| 46 | + config.partition_tombstones_fail_threshold = 0; |
| 47 | + config.data_disk_usage_percentage_warn_threshold = 0; |
| 48 | + config.partition_size_fail_threshold = new DataStorageSpec.LongBytesBound("0B"); |
| 49 | + config.column_value_size_warn_threshold = new DataStorageSpec.LongBytesBound("0B"); |
| 50 | + |
| 51 | + GuardrailsOptions options = new GuardrailsOptions(config); |
| 52 | + |
| 53 | + assertEquals(0, options.getTablesFailThreshold()); |
| 54 | + assertEquals(0, options.getKeyspacesWarnThreshold()); |
| 55 | + assertEquals(0, options.getPartitionTombstonesFailThreshold()); |
| 56 | + assertEquals(0, options.getDataDiskUsagePercentageWarnThreshold()); |
| 57 | + assertEquals(0, options.getPartitionSizeFailThreshold().toBytes()); |
| 58 | + assertEquals(0, options.getColumnValueSizeWarnThreshold().toBytes()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testMinusOneAndNullDisableThresholds() |
| 63 | + { |
| 64 | + Config config = new Config(); |
| 65 | + config.tables_fail_threshold = -1; |
| 66 | + config.partition_size_fail_threshold = null; |
| 67 | + |
| 68 | + GuardrailsOptions options = new GuardrailsOptions(config); |
| 69 | + |
| 70 | + assertEquals(-1, options.getTablesFailThreshold()); |
| 71 | + assertNull(options.getPartitionSizeFailThreshold()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testValuesBelowMinusOneAreRejected() |
| 76 | + { |
| 77 | + Config config = new Config(); |
| 78 | + config.tables_fail_threshold = -2; |
| 79 | + |
| 80 | + Assertions.assertThatThrownBy(() -> new GuardrailsOptions(config)) |
| 81 | + .isInstanceOf(IllegalArgumentException.class) |
| 82 | + .hasMessageContaining("negative values are not allowed, outside of -1 which disables the guardrail"); |
| 83 | + } |
| 84 | +} |
0 commit comments