danny0405 commented on code in PR #9565: URL: https://github.com/apache/hudi/pull/9565#discussion_r1311266196
########## hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/timeline/CompletionTimeQueryView.java: ########## @@ -0,0 +1,149 @@ +/* + * 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.hudi.client.timeline; + +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.table.timeline.HoodieArchivedTimeline; +import org.apache.hudi.common.table.timeline.HoodieInstant; +import org.apache.hudi.common.table.timeline.HoodieTimeline; +import org.apache.hudi.common.util.Option; + +import org.apache.avro.generic.GenericRecord; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import static org.apache.hudi.common.table.timeline.HoodieArchivedTimeline.COMPLETION_TIME_ARCHIVED_META_FIELD; +import static org.apache.hudi.common.table.timeline.HoodieTimeline.EQUALS; +import static org.apache.hudi.common.table.timeline.HoodieTimeline.GREATER_THAN; + +/** + * Query view for instant completion time. + */ +public class CompletionTimeQueryView implements AutoCloseable { + private final HoodieTableMetaClient metaClient; + + /** + * Mapping from instant start time -> completion time. + * Should be thread-safe data structure. + */ + private final Map<String, String> startToCompletionInstantTimeMap; + + /** + * The start instant time to eagerly load from, by default load last 3 days of completed instants. + */ + private final String startInstant; + + /** + * The first instant on the active timeline, used for query optimization. + */ + private final String firstInstantOnActiveTimeline; + + /** + * The constructor. + * + * @param metaClient The table meta client. + * @param startInstant The earliest instant time to eagerly load from, by default load last 3 days of completed instants. + */ + public CompletionTimeQueryView(HoodieTableMetaClient metaClient, String startInstant) { + this.metaClient = metaClient; + this.startToCompletionInstantTimeMap = new ConcurrentHashMap<>(); + this.startInstant = startInstant; + this.firstInstantOnActiveTimeline = metaClient.getActiveTimeline().firstInstant().map(HoodieInstant::getTimestamp).orElse(""); + load(); + } + + /** + * Queries the instant completion time with given start time. + * + * @param startTime The start time. + * + * @return The completion time if the instant finished or empty if it is still pending. + */ + public Option<String> getCompletionTime(String startTime) { + String completionTime = this.startToCompletionInstantTimeMap.get(startTime); + if (completionTime != null) { + return Option.of(completionTime); + } + if (HoodieTimeline.compareTimestamps(startTime, GREATER_THAN, this.firstInstantOnActiveTimeline)) { Review Comment: > Not following this logic. If startTime > firstInstantOnActiveTimeline, it doesn't mean that instant is still pending right. It means it is pending, because in the `#load` method, we already put all the completed instants of the active timeline into the map, if the map does not contain the startTime as a key, then it means the instant is pending. -- 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]
