AmatyaAvadhanula commented on code in PR #17653: URL: https://github.com/apache/druid/pull/17653#discussion_r1944905579
########## server/src/main/java/org/apache/druid/metadata/segment/cache/HeapMemoryDatasourceSegmentCache.java: ########## @@ -0,0 +1,621 @@ +/* + * 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.metadata.segment.cache; + +import org.apache.druid.error.DruidException; +import org.apache.druid.metadata.PendingSegmentRecord; +import org.apache.druid.segment.realtime.appenderator.SegmentIdWithShardSpec; +import org.apache.druid.server.http.DataSegmentPlus; +import org.apache.druid.timeline.DataSegment; +import org.apache.druid.timeline.SegmentId; +import org.apache.druid.timeline.SegmentTimeline; +import org.joda.time.DateTime; +import org.joda.time.Interval; + +import javax.annotation.Nullable; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +/** + * In-memory cache for segments and pending segments of a single datasource. + */ +class HeapMemoryDatasourceSegmentCache extends ReadWriteCache +{ + private final String dataSource; + private final Map<String, DataSegmentPlus> idToUsedSegment = new HashMap<>(); + private final Map<SegmentId, DateTime> unusedSegmentIdToUpdatedTime = new HashMap<>(); + + /** + * Not being used right now. Could allow lookup of visible segments for a given interval. + */ + private final SegmentTimeline usedSegmentTimeline = SegmentTimeline.forSegments(Set.of()); + + /** + * Map from interval to segment ID to pending segment record. + */ + private final Map<Interval, Map<String, PendingSegmentRecord>> + intervalToPendingSegments = new HashMap<>(); + + private final Map<Interval, Map<String, Integer>> + intervalVersionToHighestUnusedPartitionNumber = new HashMap<>(); + + HeapMemoryDatasourceSegmentCache(String dataSource) + { + super(true); Review Comment: Could you please add more details explaining the usage of a fair lock? I recall a discussion where we wanted to change the lock in VersionedIntervalTimeline to false as well. ########## server/src/main/java/org/apache/druid/metadata/segment/cache/HeapMemoryDatasourceSegmentCache.java: ########## @@ -0,0 +1,621 @@ +/* + * 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.metadata.segment.cache; + +import org.apache.druid.error.DruidException; +import org.apache.druid.metadata.PendingSegmentRecord; +import org.apache.druid.segment.realtime.appenderator.SegmentIdWithShardSpec; +import org.apache.druid.server.http.DataSegmentPlus; +import org.apache.druid.timeline.DataSegment; +import org.apache.druid.timeline.SegmentId; +import org.apache.druid.timeline.SegmentTimeline; +import org.joda.time.DateTime; +import org.joda.time.Interval; + +import javax.annotation.Nullable; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +/** + * In-memory cache for segments and pending segments of a single datasource. + */ +class HeapMemoryDatasourceSegmentCache extends ReadWriteCache +{ + private final String dataSource; + private final Map<String, DataSegmentPlus> idToUsedSegment = new HashMap<>(); + private final Map<SegmentId, DateTime> unusedSegmentIdToUpdatedTime = new HashMap<>(); + + /** + * Not being used right now. Could allow lookup of visible segments for a given interval. + */ + private final SegmentTimeline usedSegmentTimeline = SegmentTimeline.forSegments(Set.of()); Review Comment: Adding a timeline with potentially expensive operations because of its own lock seems risky and wasteful given that we are not using it. Is there a reason we aren't using a TreeMap: Interval -> (DataSegment / SegmentId) and use it instead? I believe the perf impact would be significant when there are several intervals and segments. We are creating a Timeline after fetching segments anyway. -- 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]
