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


##########
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);
+        this.records = records;
+        if (metadataVersion.isLessThan(MetadataVersion.MINIMUM_KRAFT_VERSION)) 
{
+            throw new RuntimeException("Unable to load metadata from " + 
source + ": bootstrap " +
+                "metadata versions less than " + 
MetadataVersion.MINIMUM_KRAFT_VERSION + " are " +
+                "not supported.");
+        }
+        this.metadataVersion = metadataVersion;
+        Objects.requireNonNull(source);
+        this.source = source;
+    }
+
+    public List<ApiMessageAndVersion> records() {
+        return records;
+    }
+
+    public MetadataVersion metadataVersion() {
+        return metadataVersion;
+    }
+
+    public String source() {
+        return source;
+    }
+
+    public BootstrapMetadata copyWithOnlyVersion() {
+        List<ApiMessageAndVersion> newRecords = new ArrayList<>();
+        for (ApiMessageAndVersion record : records) {
+            if (recordToMetadataVersion(record.message()).isPresent()) {
+                newRecords.clear();
+                newRecords.add(record);
+            }
+        }
+        if (newRecords.isEmpty()) {
+            throw new RuntimeException("No FeatureLevelRecord for " + 
MetadataVersion.FEATURE_NAME +
+                    " was found in the bootstrap metadata from " + source);
+        }
+        return new BootstrapMetadata(Collections.unmodifiableList(newRecords),
+                metadataVersion, source);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(records, metadataVersion, source);
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (o == null || !(o instanceof BootstrapMetadata)) return false;

Review Comment:
   oh, I wanted to write `!o.getClass().equals(this.getClass()))`. Let me do 
that.
   
   Sigh. Record types can't come soon enough...



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