|
| 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; |
| 20 | + |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import org.apache.cassandra.config.DataStorageSpec; |
| 29 | +import org.apache.cassandra.config.DatabaseDescriptor; |
| 30 | +import org.apache.cassandra.metrics.TopPartitionTracker; |
| 31 | +import org.apache.cassandra.net.ParamType; |
| 32 | +import org.apache.cassandra.schema.Schema; |
| 33 | +import org.apache.cassandra.schema.TableId; |
| 34 | +import org.apache.cassandra.schema.TableMetadata; |
| 35 | +import org.apache.cassandra.utils.NoSpamLogger; |
| 36 | + |
| 37 | +/** |
| 38 | + * Utility class for checking write threshold warnings on replicas. |
| 39 | + * CASSANDRA-17258: paxos and accord do complex thread hand off and custom write logic which makes this patch complex, so was deferred |
| 40 | + */ |
| 41 | +public class WriteThresholds |
| 42 | +{ |
| 43 | + private static final Logger logger = LoggerFactory.getLogger(WriteThresholds.class); |
| 44 | + private static final NoSpamLogger noSpamLogger = NoSpamLogger.getLogger(logger, 1, TimeUnit.MINUTES); |
| 45 | + |
| 46 | + /** |
| 47 | + * Check write thresholds for a mutation by comparing the estimated partition size and tombstone count |
| 48 | + * from {@link TopPartitionTracker} against configured warn thresholds. If a threshold is breached, |
| 49 | + * a warning is logged and the corresponding {@link ParamType} is added to {@link MessageParams} |
| 50 | + * for propagation back to the coordinator. |
| 51 | + * |
| 52 | + * @param mutation the mutation containing one or more partition updates |
| 53 | + */ |
| 54 | + public static void checkWriteThresholds(Mutation mutation) |
| 55 | + { |
| 56 | + if (!DatabaseDescriptor.isDaemonInitialized() || !DatabaseDescriptor.getWriteThresholdsEnabled()) |
| 57 | + return; |
| 58 | + |
| 59 | + DataStorageSpec.LongBytesBound sizeWarnThreshold = DatabaseDescriptor.getWriteSizeWarnThreshold(); |
| 60 | + int tombstoneWarnThreshold = DatabaseDescriptor.getWriteTombstoneWarnThreshold(); |
| 61 | + |
| 62 | + if (sizeWarnThreshold == null && tombstoneWarnThreshold == -1) |
| 63 | + return; |
| 64 | + |
| 65 | + long sizeWarnBytes = sizeWarnThreshold != null ? sizeWarnThreshold.toBytes() : -1; |
| 66 | + DecoratedKey key = mutation.key(); |
| 67 | + |
| 68 | + Map<TableId, Long> sizeWarnings = null; |
| 69 | + Map<TableId, Long> tombstoneWarnings = null; |
| 70 | + |
| 71 | + for (TableId tableId : mutation.getTableIds()) |
| 72 | + { |
| 73 | + ColumnFamilyStore cfs = Schema.instance.getColumnFamilyStoreInstance(tableId); |
| 74 | + if (cfs == null || cfs.topPartitions == null) |
| 75 | + continue; |
| 76 | + |
| 77 | + TableMetadata metadata = cfs.metadata(); |
| 78 | + if (sizeWarnBytes != -1) |
| 79 | + { |
| 80 | + long estimatedSize = cfs.topPartitions.topSizes().getEstimate(key); |
| 81 | + if (estimatedSize > sizeWarnBytes) |
| 82 | + { |
| 83 | + if (sizeWarnings == null) |
| 84 | + sizeWarnings = new HashMap<>(); |
| 85 | + sizeWarnings.put(tableId, estimatedSize); |
| 86 | + noSpamLogger.warn("Write to {} partition {} triggered size warning; " + |
| 87 | + "estimated size is {} bytes, threshold is {} bytes (see write_size_warn_threshold)", |
| 88 | + metadata, metadata.partitionKeyType.toCQLString(key.getKey()), estimatedSize, sizeWarnBytes); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (tombstoneWarnThreshold != -1) |
| 93 | + { |
| 94 | + long estimatedTombstones = cfs.topPartitions.topTombstones().getEstimate(key); |
| 95 | + if (estimatedTombstones > tombstoneWarnThreshold) |
| 96 | + { |
| 97 | + if (tombstoneWarnings == null) |
| 98 | + tombstoneWarnings = new HashMap<>(); |
| 99 | + tombstoneWarnings.put(tableId, estimatedTombstones); |
| 100 | + noSpamLogger.warn("Write to {} partition {} triggered tombstone warning; " + |
| 101 | + "estimated tombstone count is {}, threshold is {} (see write_tombstone_warn_threshold)", |
| 102 | + metadata, metadata.partitionKeyType.toCQLString(key.getKey()), estimatedTombstones, tombstoneWarnThreshold); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (sizeWarnings != null) |
| 108 | + MessageParams.add(ParamType.WRITE_SIZE_WARN, sizeWarnings); |
| 109 | + if (tombstoneWarnings != null) |
| 110 | + MessageParams.add(ParamType.WRITE_TOMBSTONE_WARN, tombstoneWarnings); |
| 111 | + } |
| 112 | +} |
0 commit comments