cmccabe commented on code in PR #12513: URL: https://github.com/apache/kafka/pull/12513#discussion_r950399632
########## metadata/src/main/java/org/apache/kafka/metadata/bootstrap/BootstrapDirectory.java: ########## @@ -0,0 +1,134 @@ +/* + * 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.metadata.bootstrap; + +import org.apache.kafka.metadata.util.BatchFileReader; +import org.apache.kafka.metadata.util.BatchFileReader.BatchAndType; +import org.apache.kafka.metadata.util.BatchFileWriter; +import org.apache.kafka.server.common.ApiMessageAndVersion; +import org.apache.kafka.server.common.MetadataVersion; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import static java.nio.file.StandardCopyOption.ATOMIC_MOVE; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; +import static org.apache.kafka.server.common.MetadataVersion.MINIMUM_KRAFT_VERSION; + + +/** + * A read-only class that holds the controller bootstrap metadata. A file named "bootstrap.snapshot" is used and the + * format is the same as a KRaft snapshot. + */ +public class BootstrapDirectory { + final static String INTER_BROKER_PROTOCOL_CONFIG_KEY = "inter.broker.protocol.version"; + final static String BINARY_BOOTSTRAP = "bootstrap.checkpoint"; + + public static String ibpStringFromConfigMap(Map<String, Object> staticConfig) { + Object value = staticConfig.get(INTER_BROKER_PROTOCOL_CONFIG_KEY); + return value == null ? "" : value.toString(); + } + + private final String directoryPath; + private final String ibp; + + /** + * Create a new BootstrapDirectory object. + * + * @param directoryPath The path to the directory with the bootstrap file. + * @param ibp The configured value of inter.broker.protocol, or the empty string + * if it is not configured. + */ + public BootstrapDirectory( + String directoryPath, + String ibp + ) { + Objects.requireNonNull(directoryPath); + Objects.requireNonNull(ibp); + this.directoryPath = directoryPath; + this.ibp = ibp; + } + + public BootstrapMetadata read() throws Exception { + if (!Files.isDirectory(Paths.get(directoryPath))) { + throw new RuntimeException("No such directory as " + directoryPath); + } + Path binaryBootstrapPath = Paths.get(directoryPath, BINARY_BOOTSTRAP); + if (!Files.exists(binaryBootstrapPath)) { + return readFromConfiguration(); + } else { + return readFromBinaryFile(binaryBootstrapPath.toString()); + } + } + + BootstrapMetadata readFromConfiguration() { + if (ibp.isEmpty()) { + return BootstrapMetadata.fromVersion(MetadataVersion.latest(), + "the default bootstrap file which sets the latest metadata.version, " + + "since no bootstrap file was found, and " + INTER_BROKER_PROTOCOL_CONFIG_KEY + + " was not configured."); + } + MetadataVersion version = MetadataVersion.fromVersionString(ibp); + if (version.isLessThan(MINIMUM_KRAFT_VERSION)) { + return BootstrapMetadata.fromVersion(MINIMUM_KRAFT_VERSION, + "a default bootstrap file setting the minimum supported KRaft metadata " + Review Comment: OK. I will reorganize this so that "source" is always at the end of the exception text, and make it a little bit shorter. I still think we should err on the side of being a bit verbose and explaining exactly what is happening, even if it's a little janky. Having to guess about this stuff is very very bad. Often all you have to go on in these problems is an exception, log message, etc. so we should be sure that it actually does explain what happened. -- 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: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org