forked from apache/kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KAFKA-16469: Metadata schema checker (apache#15995)
Create a schema checker that can validate that later versions of a KRPC schema are compatible with earlier ones. Reviewers: David Arthur <[email protected]>
- Loading branch information
Showing
22 changed files
with
1,717 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
generator/src/main/java/org/apache/kafka/message/checker/CheckerUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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.message.checker; | ||
|
||
import org.apache.kafka.message.FieldSpec; | ||
import org.apache.kafka.message.MessageGenerator; | ||
import org.apache.kafka.message.MessageSpec; | ||
import org.apache.kafka.message.Versions; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
/** | ||
* Utilities for the metadata schema checker. | ||
*/ | ||
class CheckerUtils { | ||
/** | ||
* A min function defined for shorts. | ||
* | ||
* @param a The first short integer to compare. | ||
* @param b The second short integer to compare. | ||
* @return The minimum short integer. | ||
*/ | ||
static short min(short a, short b) { | ||
return a < b ? a : b; | ||
} | ||
|
||
/** | ||
* A max function defined for shorts. | ||
* | ||
* @param a The first short integer to compare. | ||
* @param b The second short integer to compare. | ||
* @return The maximum short integer. | ||
*/ | ||
static short max(short a, short b) { | ||
return a > b ? a : b; | ||
} | ||
|
||
/** | ||
* Validate the a field doesn't have tagged versions that are outside of the top-level flexible | ||
* versions. | ||
* | ||
* @param what A description of the field. | ||
* @param field The field to validate. | ||
* @param topLevelFlexibleVersions The top-level flexible versions. | ||
*/ | ||
static void validateTaggedVersions( | ||
String what, | ||
FieldSpec field, | ||
Versions topLevelFlexibleVersions | ||
) { | ||
if (!field.flexibleVersions().isPresent()) { | ||
if (!topLevelFlexibleVersions.contains(field.taggedVersions())) { | ||
throw new RuntimeException("Tagged versions for " + what + " " + | ||
field.name() + " are " + field.taggedVersions() + ", but top " + | ||
"level flexible versions are " + topLevelFlexibleVersions); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Read a MessageSpec file from a path. | ||
* | ||
* @param schemaPath The path to read the file from. | ||
* @return The MessageSpec. | ||
*/ | ||
static MessageSpec readMessageSpecFromFile(String schemaPath) { | ||
if (!Files.isReadable(Paths.get(schemaPath))) { | ||
throw new RuntimeException("Path " + schemaPath + " does not point to " + | ||
"a readable file."); | ||
} | ||
try { | ||
return MessageGenerator.JSON_SERDE.readValue(new File(schemaPath), MessageSpec.class); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Unable to parse file as MessageSpec: " + schemaPath, e); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
generator/src/main/java/org/apache/kafka/message/checker/EvolutionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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.message.checker; | ||
|
||
/** | ||
* An exception thrown when a schema is evolved an invalid way. | ||
*/ | ||
public class EvolutionException extends RuntimeException { | ||
public EvolutionException(String message, Throwable t) { | ||
super(message, t); | ||
} | ||
|
||
public EvolutionException(String message) { | ||
super(message, null); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
generator/src/main/java/org/apache/kafka/message/checker/EvolutionVerifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* 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.message.checker; | ||
|
||
import org.apache.kafka.message.FieldSpec; | ||
import org.apache.kafka.message.MessageSpec; | ||
import org.apache.kafka.message.StructSpec; | ||
|
||
public class EvolutionVerifier { | ||
private final MessageSpec topLevelMessage1; | ||
private final MessageSpec topLevelMessage2; | ||
|
||
public EvolutionVerifier( | ||
MessageSpec topLevelMessage1, | ||
MessageSpec topLevelMessage2 | ||
) { | ||
this.topLevelMessage1 = topLevelMessage1; | ||
this.topLevelMessage2 = topLevelMessage2; | ||
} | ||
|
||
public void verify() throws Exception { | ||
verifyTopLevelMessages(topLevelMessage1, topLevelMessage2); | ||
verifyVersionsMatchTopLevelMessage("message1", topLevelMessage1); | ||
verifyVersionsMatchTopLevelMessage("message2", topLevelMessage2); | ||
Unifier unifier = new Unifier(topLevelMessage1, topLevelMessage2); | ||
unifier.unify(); | ||
} | ||
|
||
static void verifyTopLevelMessages(MessageSpec topLevelMessage1, MessageSpec topLevelMessage2) { | ||
if (!topLevelMessage1.apiKey().equals(topLevelMessage2.apiKey())) { | ||
throw new EvolutionException("Initial apiKey " + topLevelMessage1.apiKey() + | ||
" does not match final apiKey " + topLevelMessage2.apiKey()); | ||
} | ||
if (!topLevelMessage1.type().equals(topLevelMessage2.type())) { | ||
throw new EvolutionException("Initial type " + topLevelMessage1.type() + | ||
" does not match final type " + topLevelMessage2.type()); | ||
} | ||
if (!topLevelMessage2.flexibleVersions().contains(topLevelMessage1.flexibleVersions())) { | ||
throw new EvolutionException("Initial flexibleVersions " + topLevelMessage1.flexibleVersions() + | ||
" must be a subset of final flexibleVersions " + topLevelMessage2.flexibleVersions()); | ||
} | ||
if (topLevelMessage2.validVersions().highest() < topLevelMessage1.validVersions().highest()) { | ||
throw new EvolutionException("Initial maximum valid version " + | ||
topLevelMessage1.validVersions().highest() + " must not be higher than final " + | ||
"maximum valid version " + topLevelMessage2.validVersions().highest()); | ||
} | ||
if (topLevelMessage2.validVersions().lowest() < topLevelMessage1.validVersions().lowest()) { | ||
throw new EvolutionException("Initial minimum valid version " + | ||
topLevelMessage1.validVersions().lowest() + " must not be higher than final " + | ||
"minimum valid version " + topLevelMessage2.validVersions().lowest()); | ||
} | ||
} | ||
|
||
static void verifyVersionsMatchTopLevelMessage( | ||
String what, | ||
MessageSpec topLevelMessage | ||
) { | ||
for (FieldSpec field : topLevelMessage.fields()) { | ||
verifyVersionsMatchTopLevelMessage(what, topLevelMessage, field); | ||
} | ||
for (StructSpec struct : topLevelMessage.commonStructs()) { | ||
for (FieldSpec field : topLevelMessage.fields()) { | ||
verifyVersionsMatchTopLevelMessage(what, topLevelMessage, field); | ||
} | ||
} | ||
} | ||
|
||
static void verifyVersionsMatchTopLevelMessage( | ||
String what, | ||
MessageSpec topLevelMessage, | ||
FieldSpec field | ||
) { | ||
if (topLevelMessage.validVersions().intersect(field.versions()).empty()) { | ||
throw new EvolutionException("Field " + field.name() + " in " + what + " has versions " + | ||
field.versions() + ", but the message versions are only " + | ||
topLevelMessage.validVersions() + "."); | ||
} | ||
if (!field.nullableVersions().empty()) { | ||
if (topLevelMessage.validVersions().intersect(field.nullableVersions()).empty()) { | ||
throw new EvolutionException("Field " + field.name() + " in " + what + | ||
" has nullableVersions " + field.nullableVersions() + ", but the message " + | ||
"versions are only " + topLevelMessage.validVersions() + "."); | ||
} | ||
} | ||
if (field.tag().isPresent()) { | ||
if (topLevelMessage.validVersions().intersect(field.taggedVersions()).empty()) { | ||
throw new EvolutionException("Field " + field.name() + " in " + what + | ||
" has taggedVersions " + field.taggedVersions() + ", but the message " + | ||
"versions are only " + topLevelMessage.validVersions() + "."); | ||
} | ||
} | ||
field.flexibleVersions().ifPresent(v -> { | ||
if (topLevelMessage.validVersions().intersect(v).empty()) { | ||
throw new EvolutionException("Field " + field.name() + " in " + what + | ||
" has flexibleVersions " + v + ", but the message versions are only " + | ||
topLevelMessage.validVersions() + "."); | ||
} | ||
|
||
}); | ||
for (FieldSpec child : field.fields()) { | ||
verifyVersionsMatchTopLevelMessage(what, topLevelMessage, child); | ||
} | ||
} | ||
} |
Oops, something went wrong.