jon-wei commented on a change in pull request #7547: Add support minor compaction with segment locking URL: https://github.com/apache/incubator-druid/pull/7547#discussion_r294503816
########## File path: core/src/main/java/org/apache/druid/timeline/partition/AtomicUpdateGroup.java ########## @@ -0,0 +1,200 @@ +/* + * 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.druid.timeline.partition; + +import com.google.common.base.Preconditions; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.timeline.Overshadowable; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * A set of {@link PartitionChunk}s which should be atomically visible or not in the timeline. + * This is usually a set of single partitionChunk for first-generation segments. + * For non-first-generation segments generated by overwriting tasks, segments of the same interval generated by + * the same task become an atomicUpdateGroup. As a result, all segments in an atomicUpdateGroup have the same + * rootPartitionp range, majorVersion, minorVersion, and atomicUpdateGroupSize. + */ +class AtomicUpdateGroup<T extends Overshadowable<T>> implements Overshadowable<AtomicUpdateGroup<T>> +{ + // Perhaps it would be worth to store these in a map of (partitionId -> partitionChunk) + // because sometimes we need to search for a particular partitionChunk corresponding to a partitionId. + // However, there's a tradeoff between time and space. Storing in a map would be faster than storing in a list, + // but it would take at least additional 4 bytes per chunk to store its key. + // This may matter if there are a lot of segments to keep in memory as in brokers or the coordinator. + private final List<PartitionChunk<T>> chunks = new ArrayList<>(); + + public AtomicUpdateGroup(PartitionChunk<T> chunk) + { + this.chunks.add(chunk); + } + + public void add(PartitionChunk<T> chunk) + { + final PartitionChunk<T> existing = replaceChunkWith(chunk); + if (existing == null) { + if (isFull()) { + throw new IAE("Can't add more chunk[%s] to atomicUpdateGroup[%s]", chunk, chunks); + } + if (!isSameAtomicUpdateGroup(chunks.get(0), chunk)) { + throw new IAE("Can't add chunk[%s] to a different atomicUpdateGroup[%s]", chunk, chunks); + } + chunks.add(chunk); + } + } + + public void remove(PartitionChunk<T> chunk) + { + if (chunks.isEmpty()) { + throw new ISE("Can't remove chunk[%s] from empty atomicUpdateGroup", chunk); + } + + if (!isSameAtomicUpdateGroup(chunks.get(0), chunk)) { + throw new IAE("Can't remove chunk[%s] from a different atomicUpdateGroup[%s]", chunk, chunks); + } + + chunks.remove(chunk); + } + + public boolean isFull() + { + return chunks.size() == chunks.get(0).getObject().getAtomicUpdateGroupSize(); Review comment: Should this check that chunks.get(0) exists? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
