cmccabe commented on code in PR #12513:
URL: https://github.com/apache/kafka/pull/12513#discussion_r949671940


##########
metadata/src/main/java/org/apache/kafka/metadata/bootstrap/BootstrapMetadata.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.common.metadata.FeatureLevelRecord;
+import org.apache.kafka.common.protocol.ApiMessage;
+import org.apache.kafka.server.common.ApiMessageAndVersion;
+import org.apache.kafka.server.common.MetadataVersion;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+
+/**
+ * The bootstrap metadata. On startup, if the metadata log is empty, we will 
populate the log with
+ * these records. Alternately, if log is not empty, but the metadata version 
is not set, we will
+ * use the version specified here.
+ */
+public class BootstrapMetadata {
+    private final List<ApiMessageAndVersion> records;
+    private final MetadataVersion metadataVersion;
+    private final String source;
+
+    public static BootstrapMetadata fromVersion(MetadataVersion 
metadataVersion, String source) {
+        List<ApiMessageAndVersion> records = Collections.singletonList(
+            new ApiMessageAndVersion(new FeatureLevelRecord().
+                setName(MetadataVersion.FEATURE_NAME).
+                setFeatureLevel(metadataVersion.featureLevel()), (short) 0));
+        return new BootstrapMetadata(records, metadataVersion, source);
+    }
+
+    public static BootstrapMetadata fromRecords(List<ApiMessageAndVersion> 
records, String source) {
+        MetadataVersion metadataVersion = null;
+        for (ApiMessageAndVersion record : records) {
+            Optional<MetadataVersion> version = 
recordToMetadataVersion(record.message());
+            if (version.isPresent()) {
+                metadataVersion = version.get();
+            }
+        }
+        if (metadataVersion == null) {
+            throw new RuntimeException("No FeatureLevelRecord for " + 
MetadataVersion.FEATURE_NAME +
+                    " was found in the bootstrap metadata from " + source);
+        }
+        return new BootstrapMetadata(records, metadataVersion, source);
+    }
+
+    public static Optional<MetadataVersion> recordToMetadataVersion(ApiMessage 
record) {
+        if (record instanceof FeatureLevelRecord) {
+            FeatureLevelRecord featureLevel = (FeatureLevelRecord) record;
+            if (featureLevel.name().equals(MetadataVersion.FEATURE_NAME)) {
+                return 
Optional.of(MetadataVersion.fromFeatureLevel(featureLevel.featureLevel()));
+            }
+        }
+        return Optional.empty();
+    }
+
+    BootstrapMetadata(
+        List<ApiMessageAndVersion> records,
+        MetadataVersion metadataVersion,
+        String source
+    ) {
+        Objects.requireNonNull(records);

Review Comment:
   > nit: usual idiom is this: this.records = Objects.requireNonNull(records);
   
   Fixed
   
   > Perhaps justifiable to skip extra validations if we can make the 
constructor private.
   
   It's package-private, which in practice will keep anyone from doing 
something weird because the users are outside `package 
org.apache.kafka.metadata.bootstrap`
   
   I wanted to do something slightly weird in a unit test, which is why it was 
package-private rather than fully private. I can't recall what at the moment. 
Should be ok?



-- 
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

Reply via email to