satishd commented on a change in pull request #11058: URL: https://github.com/apache/kafka/pull/11058#discussion_r714971397
########## File path: storage/src/main/java/org/apache/kafka/server/log/remote/metadata/storage/CommittedOffsetsFile.java ########## @@ -0,0 +1,80 @@ +/* + * 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.kafka.server.log.remote.metadata.storage; + +import org.apache.kafka.common.utils.Utils; +import org.apache.kafka.server.common.CheckpointFile; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.regex.Pattern; + +/** + * This class represents a file containing the committed offsets of remote log metadata partitions. + */ +public class CommittedOffsetsFile { + private static final int CURRENT_VERSION = 0; + private static final String SEPARATOR = " "; + + private static final Pattern MINIMUM_ONE_WHITESPACE = Pattern.compile("\\s+"); + private final CheckpointFile<Map.Entry<Integer, Long>> checkpointFile; + + CommittedOffsetsFile(File offsetsFile) throws IOException { + CheckpointFile.EntryFormatter<Map.Entry<Integer, Long>> formatter = new EntryFormatter(); + checkpointFile = new CheckpointFile<>(offsetsFile, CURRENT_VERSION, formatter); + } + + private static class EntryFormatter implements CheckpointFile.EntryFormatter<Map.Entry<Integer, Long>> { + + @Override + public String toString(Map.Entry<Integer, Long> entry) { + // Each entry is stored in a new line as <partition-num offset> + return entry.getKey() + SEPARATOR + entry.getValue(); + } + + @Override + public Optional<Map.Entry<Integer, Long>> fromString(String line) { + String[] strings = MINIMUM_ONE_WHITESPACE.split(line); + if (strings.length != 2) { + return Optional.empty(); + } + int partition = Integer.parseInt(strings[0]); + long offset = Long.parseLong(strings[1]); Review comment: An error will be thrown to the caller. As you suggested, it is good to catch that and return empty optional so that the caller throws an error with the details including the line. Addressed it in the latest commit. -- 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]
