maedhroz commented on code in PR #2498: URL: https://github.com/apache/cassandra/pull/2498#discussion_r1270102370
########## src/java/org/apache/cassandra/index/sai/utils/SegmentMemoryLimiter.java: ########## @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cassandra.index.sai.utils; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import javax.annotation.concurrent.ThreadSafe; + +import com.google.common.annotations.VisibleForTesting; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.config.DatabaseDescriptor; + +/** + * A simple, thread-safe memory usage tracker, named to reflect a particular scope. + */ +@ThreadSafe +public final class SegmentMemoryLimiter +{ + private static final Logger logger = LoggerFactory.getLogger(SegmentMemoryLimiter.class); + + /** + * Global limit on heap consumed by all index segment building that occurs outside the context of Memtable flush. + * <p> + * Note that to avoid flushing small index segments, a segment is only flushed when + * both the global size of all building segments has breached the limit and the size of the + * segment in question reaches (segment_write_buffer_space_mb / # currently building column indexes). + * <p> + * ex. If there is only one column index building, it can buffer up to segment_write_buffer_space_mb. + * <p> + * ex. If there is one column index building per table across 8 compactors, each index will be + * eligible to flush once it reaches (segment_write_buffer_space_mb / 8) MBs. + */ + public static final long DEFAULT_SEGMENT_BUILD_MEMORY_LIMIT = DatabaseDescriptor.getSAISegmentWriteBufferSpace().toBytes(); + + private static final AtomicLong bytesUsed = new AtomicLong(0); + + private static volatile long limitBytes = DEFAULT_SEGMENT_BUILD_MEMORY_LIMIT; Review Comment: The only thing that I'm not 100% sold on is that `limitBytes` is non-final here (and therefore requires volatile access) because of testing. Taking a quick look at it's usage in `SortedTermsWriter` at least, we could probably still avoid making everything here `static` and instead have a singleton limiter that's passed to the things that use it. It might actually make some of the testing easier that way? (i.e. We could just pass new limiters instead of having to reset the global one? ...and in theory, we could test things without having to hit DD?) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

