mannoopj commented on code in PR #14389: URL: https://github.com/apache/kafka/pull/14389#discussion_r1466452243
########## tools/src/main/java/org/apache/kafka/tools/SchemaChecker/MetadataSchemaChecker.java: ########## @@ -0,0 +1,359 @@ +/* + * 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.tools.SchemaChecker; + + + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; + +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.ObjectLoader; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevTree; +import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.treewalk.filter.PathFilter; + + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; + + +public class MetadataSchemaChecker { + + static int latestTag = -1; + static int latestTagVersion = -1; + static int oldLatestVersion = -1; + static int oldFirstVersion = -1; + static int newLatestVersion = -1; + static int newFirstVersion = -1; + static String[] filesCheckMetadata = new File(System.getProperty("user.dir") + "/metadata/src/main/resources/common/metadata/").list(); + public static void main(String[] args) throws Exception { + + try { + List<String> localContent = new ArrayList<>(); + for(String fileName: filesCheckMetadata) { + final String dir = System.getProperty("user.dir"); + String path = dir + "/metadata/src/main/resources/common/metadata/" + fileName; + BufferedReader reader = new BufferedReader(new FileReader(path)); + for (int i = 0; i < 15; i++) { + reader.readLine(); + } + StringBuilder content = new StringBuilder(); + boolean print = false; + for (String line = reader.readLine(); line != null; line = reader.readLine()) { + if (line.charAt(0) == '{') { + print = true; + } + if (print && !line.contains("//")) { + content.append(line); + } + } + localContent.add(content.toString()); + } + + List<String> gitContent = GetDataFromGit(); + if (localContent.size() != gitContent.size()) { + throw new IllegalStateException("missing schemas"); + } + for(int i = 0; i < localContent.size(); i++) { + if (Objects.equals(localContent.get(i), gitContent.get(i))) { + continue; + } + + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode1 = objectMapper.readTree(gitContent.get(i)); + JsonNode jsonNode2 = objectMapper.readTree(localContent.get(i)); + + checkApiTypeVersions(jsonNode1, jsonNode2); + parser((ArrayNode) jsonNode1.get("fields"), (ArrayNode) jsonNode2.get("fields")); + } + + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static List<String> GetDataFromGit() throws IOException, GitAPIException { + List<String> gitSchemas = new ArrayList<>(); + + Git git = Git.open(new File(System.getProperty("user.dir") + "/.git")); + Repository repository = git.getRepository(); + Ref head = git.getRepository().getRefDatabase().firstExactRef("refs/heads/trunk"); + + try (RevWalk revWalk = new RevWalk(repository)) { + RevCommit commit = revWalk.parseCommit(head.getObjectId()); + RevTree tree = commit.getTree(); + for (String fileName : filesCheckMetadata) { + StringBuilder stringBuilder = new StringBuilder(); + try (TreeWalk treeWalk = new TreeWalk(repository)) { + treeWalk.addTree(tree); + treeWalk.setRecursive(true); + treeWalk.setFilter(PathFilter.create("metadata/src/main/resources/common/metadata/" + fileName)); + if (!treeWalk.next()) { + throw new IllegalStateException("Did not find expected file /metadata/src/main/resources/common/metadata/" + fileName); + } + ObjectId objectId = treeWalk.getObjectId(0); + ObjectLoader loader = repository.open(objectId); + + String content = new String(loader.getBytes()); + String[] lines = content.split("\n"); + boolean print = false; + for (int i = 15; i < lines.length; i++) { + if (lines[i].charAt(0) == '{') { + print = true; + } + if (print && !lines[i].contains("//")) { + stringBuilder.append(lines[i]); + } + } + } + gitSchemas.add(stringBuilder.toString()); + } + revWalk.dispose(); + } + return gitSchemas; + } + + private static void checkApiTypeVersions(JsonNode original, JsonNode edited) { + if (!Objects.equals(original.get("apiKey"), edited.get("apiKey"))) { + throw new IllegalStateException("New schema has wrong api key, Original api key: " + edited.get("apiKey") + + ", New api key: " + original.get("apiKey")); + } + if (!Objects.equals(original.get("type"), edited.get("type"))) { + throw new IllegalStateException("New schema has wrong record type, Original type: " + edited.get("type") + + ", New Type: " + original.get("type")); + } + String[] oldValidVersions = cleanUpVersionStrings(String.valueOf(original.get("validVersions"))); + String[] newValidVersions = cleanUpVersionStrings(String.valueOf(original.get("validVersions"))); + oldFirstVersion = Integer.parseInt(oldValidVersions[0]); + newFirstVersion = Integer.parseInt(newValidVersions[0]); + if (oldValidVersions.length == 1) { + oldLatestVersion = Integer.parseInt(oldValidVersions[0]); + } else { + oldLatestVersion = Integer.parseInt(oldValidVersions[1]); + } + if (newValidVersions.length == 1) { + newLatestVersion = Integer.parseInt(newValidVersions[0]); + } else { + newLatestVersion = Integer.parseInt(newValidVersions[1]); + } + + if (oldLatestVersion != newLatestVersion && oldLatestVersion + 1 != newLatestVersion) { + throw new IllegalStateException("Invalid latest version, can at most be one higher than the previous. Original version: " + + oldLatestVersion + ", New Version: " + newLatestVersion); + } + if (oldFirstVersion != newFirstVersion) { + throw new IllegalStateException("cannot change lower end of valid versions"); + } + } + + private static void parser(ArrayNode fieldsOrig, ArrayNode fieldsNew) { + Iterator<JsonNode> iterNewNode = fieldsNew.iterator(); + Iterator<JsonNode> iterOldNode = fieldsOrig.iterator(); + boolean isNewField = false; + JsonNode nodeOrig = null; + while (iterOldNode.hasNext() || isNewField) { + if (!isNewField) { + nodeOrig = iterOldNode.next(); + } + isNewField = false; + + if (!iterNewNode.hasNext()) { + throw new IllegalStateException("New schema is missing field "); + } + JsonNode nodeNew = iterNewNode.next(); + + if (checkTaggedFieldIfTag(nodeOrig, nodeNew)) { + isNewField = true; + continue; + } + + if (parseVersion(nodeOrig, nodeNew)) { + isNewField = true; + continue; + } + + if (!Objects.equals(nodeOrig.get("type"), nodeNew.get("type"))) { + throw new IllegalStateException("Fields must have same type. Expected: " + nodeOrig.get("type") + " Received: " + nodeNew.get("type")); + } + + parseNullableVersion(nodeOrig, nodeNew); + + + if (nodeOrig.has("fields")) { + while(!nodeNew.has("fields")) { + parseNewField(nodeNew); + nodeNew = iterNewNode.next(); + } + parser((ArrayNode) nodeOrig.get("fields"), (ArrayNode) nodeNew.get("fields")); + } + } + + while (iterNewNode.hasNext()) { + parseNewField(iterNewNode.next()); + } + } + + private static boolean parseVersion(JsonNode nodeOrig, JsonNode nodeNew) { + String[] oldVersion = cleanUpVersionStrings(String.valueOf(nodeOrig.get("versions"))); Review Comment: I used `FinalizedVersionRange` here instead of `VersionRange` since I was unable to import `VersionRange`. It seems like a acceptable substitute to me but if not I can change it. -- 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