-
Notifications
You must be signed in to change notification settings - Fork 587
HDDS-11149. Have a generic version Validator for validating Requests #6932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
bde95f7
f1676be
b77d1bf
561d010
363d9fa
e623fdb
b270bf6
ca3746d
a84749b
fc57929
778229a
9cf34be
66027c2
dc9f93b
c33f78e
a394670
d024eb9
889e081
436dfbd
c1b690d
b9ef7d0
ba133d2
df698f8
0b5ceb5
db76527
49efb20
b819b63
bfa7618
ded3450
568603e
2124f03
4004663
2337f72
56e95eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,12 +28,14 @@ | |
| import javax.lang.model.element.ElementKind; | ||
| import javax.lang.model.element.ExecutableElement; | ||
| import javax.lang.model.element.Modifier; | ||
| import javax.lang.model.element.Name; | ||
| import javax.lang.model.element.TypeElement; | ||
| import javax.lang.model.element.VariableElement; | ||
| import javax.lang.model.type.ExecutableType; | ||
| import javax.lang.model.type.TypeMirror; | ||
| import javax.lang.model.util.SimpleAnnotationValueVisitor8; | ||
| import javax.tools.Diagnostic; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map.Entry; | ||
| import java.util.Set; | ||
|
|
@@ -50,14 +52,11 @@ | |
| * META-INF/services/javax.annotation.processing.Processor file in the module's | ||
| * resources folder. | ||
| */ | ||
| @SupportedAnnotationTypes( | ||
| "org.apache.hadoop.ozone.om.request.validation.RequestFeatureValidator") | ||
| @SupportedAnnotationTypes({ | ||
| "org.apache.hadoop.ozone.om.request.validation.OMClientVersionValidator", | ||
| "org.apache.hadoop.ozone.om.request.validation.OMLayoutVersionValidator"}) | ||
| @SupportedSourceVersion(SourceVersion.RELEASE_8) | ||
| public class RequestFeatureValidatorProcessor extends AbstractProcessor { | ||
|
|
||
| public static final String ERROR_CONDITION_IS_EMPTY = | ||
| "RequestFeatureValidator has an empty condition list. Please define the" | ||
| + " ValidationCondition in which the validator has to be applied."; | ||
| public class OmRequestFeatureValidatorProcessor extends AbstractProcessor { | ||
| public static final String ERROR_ANNOTATED_ELEMENT_IS_NOT_A_METHOD = | ||
| "RequestFeatureValidator annotation is not applied to a method."; | ||
| public static final String ERROR_VALIDATOR_METHOD_HAS_TO_BE_STATIC = | ||
|
|
@@ -90,21 +89,25 @@ public class RequestFeatureValidatorProcessor extends AbstractProcessor { | |
| public static final String VALIDATION_CONTEXT_CLASS_NAME = | ||
| "org.apache.hadoop.ozone.om.request.validation.ValidationContext"; | ||
|
|
||
| public static final String ANNOTATION_SIMPLE_NAME = "RequestFeatureValidator"; | ||
| private static final List<String> ANNOTATION_SIMPLE_NAMES = Arrays.asList("OMClientVersionValidator", | ||
| "OMLayoutVersionValidator"); | ||
| public static final String ANNOTATION_CONDITIONS_PROPERTY_NAME = "conditions"; | ||
| public static final String ANNOTATION_PROCESSING_PHASE_PROPERTY_NAME = | ||
| "processingPhase"; | ||
| public static final String ANNOTATION_MAX_CLIENT_VERSION_PROPERTY_NAME = "maxClientVersion"; | ||
|
|
||
| public static final String PROCESSING_PHASE_PRE_PROCESS = "PRE_PROCESS"; | ||
| public static final String PROCESSING_PHASE_POST_PROCESS = "POST_PROCESS"; | ||
| public static final String ERROR_NO_PROCESSING_PHASE_DEFINED = | ||
| "RequestFeatureValidator has an invalid ProcessingPhase defined."; | ||
|
|
||
| public static final String MAX_CLIENT_VERSION_FUTURE_VERSION = "FUTURE_VERSION"; | ||
|
|
||
| @Override | ||
| public boolean process(Set<? extends TypeElement> annotations, | ||
| RoundEnvironment roundEnv) { | ||
| for (TypeElement annotation : annotations) { | ||
| if (!annotation.getSimpleName().contentEquals(ANNOTATION_SIMPLE_NAME)) { | ||
| if (!ANNOTATION_SIMPLE_NAMES.contains(annotation.getSimpleName().toString())) { | ||
| continue; | ||
| } | ||
| processElements(roundEnv.getElementsAnnotatedWith(annotation)); | ||
|
|
@@ -172,7 +175,7 @@ private void ensurePostProcessorReturnsOMResponse( | |
| ExecutableElement elem, boolean isPreprocessor) { | ||
| if (!isPreprocessor && !elem.getReturnType().toString() | ||
| .equals(OM_RESPONSE_CLASS_NAME)) { | ||
| emitErrorMsg(ERROR_VALIDATOR_METHOD_HAS_TO_RETURN_OMRESPONSE); | ||
| emitErrorMsg(ERROR_VALIDATOR_METHOD_HAS_TO_RETURN_OMRESPONSE + " " + elem.getSimpleName()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -201,14 +204,12 @@ private boolean checkAndEvaluateAnnotation( | |
| boolean isPreprocessor = false; | ||
| for (Entry<? extends ExecutableElement, ? extends AnnotationValue> | ||
| entry : methodAnnotation.getElementValues().entrySet()) { | ||
|
|
||
| if (hasInvalidValidationCondition(entry)) { | ||
| emitErrorMsg(ERROR_CONDITION_IS_EMPTY); | ||
| } | ||
|
|
||
| if (isProcessingPhaseValue(entry)) { | ||
| isPreprocessor = evaluateProcessingPhase(entry); | ||
| } | ||
| } | ||
|
|
||
| return isPreprocessor; | ||
| } | ||
|
|
||
|
|
@@ -223,15 +224,29 @@ private boolean evaluateProcessingPhase( | |
| return false; | ||
| } | ||
|
|
||
| private boolean hasValidMaxClientVersion( | ||
| Entry<? extends ExecutableElement, ? extends AnnotationValue> entry) { | ||
| if (isPropertyNamedAs(entry, ANNOTATION_MAX_CLIENT_VERSION_PROPERTY_NAME)) { | ||
| Name maxClientVersion = visit(entry, new MaxClientVersionVisitor()); | ||
| return !maxClientVersion.contentEquals(MAX_CLIENT_VERSION_FUTURE_VERSION); | ||
|
||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private boolean isProcessingPhaseValue( | ||
| Entry<? extends ExecutableElement, ? extends AnnotationValue> entry) { | ||
| return isPropertyNamedAs(entry, ANNOTATION_PROCESSING_PHASE_PROPERTY_NAME); | ||
| } | ||
|
|
||
| private boolean hasInvalidValidationCondition( | ||
| private boolean isMaxClientVersionValue( | ||
| Entry<? extends ExecutableElement, ? extends AnnotationValue> entry) { | ||
| return isPropertyNamedAs(entry, ANNOTATION_MAX_CLIENT_VERSION_PROPERTY_NAME); | ||
| } | ||
|
|
||
| private boolean hasValidValidationCondition( | ||
| Entry<? extends ExecutableElement, ? extends AnnotationValue> entry) { | ||
| return isPropertyNamedAs(entry, ANNOTATION_CONDITIONS_PROPERTY_NAME) | ||
| && !visit(entry, new ConditionValidator()); | ||
| && visit(entry, new ConditionValidator()); | ||
| } | ||
|
|
||
| private boolean isPropertyNamedAs( | ||
|
|
@@ -250,7 +265,7 @@ private static class ConditionValidator | |
| extends SimpleAnnotationValueVisitor8<Boolean, Void> { | ||
|
|
||
| ConditionValidator() { | ||
| super(Boolean.TRUE); | ||
| super(Boolean.FALSE); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -264,6 +279,18 @@ public Boolean visitArray(List<? extends AnnotationValue> vals, | |
|
|
||
| } | ||
|
|
||
| private static class MaxClientVersionVisitor | ||
| extends SimpleAnnotationValueVisitor8<Name, Void> { | ||
|
|
||
| MaxClientVersionVisitor() { | ||
| } | ||
|
|
||
| @Override | ||
| public Name visitEnumConstant(VariableElement c, Void unused) { | ||
| return c.getSimpleName(); | ||
| } | ||
| } | ||
|
|
||
| private static class ProcessingPhaseVisitor | ||
| extends SimpleAnnotationValueVisitor8<String, Void> { | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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.ozone.annotations; | ||
|
|
||
| import javax.annotation.processing.AbstractProcessor; | ||
| import javax.annotation.processing.RoundEnvironment; | ||
| import javax.annotation.processing.SupportedAnnotationTypes; | ||
| import javax.annotation.processing.SupportedSourceVersion; | ||
| import javax.lang.model.SourceVersion; | ||
| import javax.lang.model.element.Element; | ||
| import javax.lang.model.element.ElementKind; | ||
| import javax.lang.model.element.ExecutableElement; | ||
| import javax.lang.model.element.TypeElement; | ||
| import javax.lang.model.util.Elements; | ||
| import javax.lang.model.util.Types; | ||
| import javax.tools.Diagnostic; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * This class is an annotation processor that is hooked into the java compiler | ||
| * and is used to validate the RegisterValidator annotations in the | ||
| * codebase, to ensure that the annotated classes have the proper methods returning appropriate object types. | ||
| * | ||
| * The module is compiled in a different execution via Maven before anything | ||
| * else is compiled, and then javac picks this class up as an annotation | ||
| * processor from the classpath via a ServiceLoader, based on the | ||
| * META-INF/services/javax.annotation.processing.Processor file in the module's | ||
| * resources folder. | ||
| */ | ||
| @SupportedAnnotationTypes("org.apache.hadoop.ozone.om.request.validation.RegisterValidator") | ||
| @SupportedSourceVersion(SourceVersion.RELEASE_8) | ||
| public class RegisterValidatorProcessor extends AbstractProcessor { | ||
|
|
||
| public static final String ANNOTATION_SIMPLE_NAME = "RegisterValidator"; | ||
| public static final String VERSION_CLASS_NAME = "org.apache.hadoop.ozone.Version"; | ||
| public static final String REQUEST_PROCESSING_PHASE_CLASS_NAME = "org.apache.hadoop.ozone.om.request.validation" + | ||
| ".RequestProcessingPhase"; | ||
| public static final String MAX_VERSION_METHOD_NAME = "maxVersion"; | ||
| public static final String REQUEST_TYPE_METHOD_NAME = "requestType"; | ||
| public static final String PROCESSING_PHASE_METHOD_NAME = "processingPhase"; | ||
|
|
||
| public static final String MAX_VERSION_NOT_FOUND_ERROR_MESSAGE = "Method " + MAX_VERSION_METHOD_NAME + | ||
| " returning an enum implementing " + VERSION_CLASS_NAME + " not found"; | ||
| public static final String REQUEST_TYPE_NOT_FOUND_ERROR_MESSAGE = "Method " + REQUEST_TYPE_METHOD_NAME + | ||
| " returning an enum not found"; | ||
| public static final String PROCESSING_PHASE_NOT_FOUND_ERROR_MESSAGE = "Method " + PROCESSING_PHASE_METHOD_NAME | ||
| + " returning an enum implementing " + REQUEST_PROCESSING_PHASE_CLASS_NAME + " not found"; | ||
|
|
||
| @Override | ||
| public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | ||
| for (TypeElement annotation : annotations) { | ||
| if (!annotation.getSimpleName().contentEquals(ANNOTATION_SIMPLE_NAME)) { | ||
| continue; | ||
| } | ||
| processElements(roundEnv.getElementsAnnotatedWith(annotation)); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private boolean validateMethod(ExecutableElement method, String expectedMethodName, ElementKind expectedReturnType, | ||
| String expectedReturnClass) { | ||
| Elements elementUtils = processingEnv.getElementUtils(); | ||
| Types types = processingEnv.getTypeUtils(); | ||
| TypeElement expectedReturnInterface = expectedReturnClass == null || expectedReturnClass.equals("") ? null : | ||
| elementUtils.getTypeElement(expectedReturnClass); | ||
| return method.getSimpleName().toString().equals(expectedMethodName) && (expectedReturnType == null || | ||
| types.asElement(method.getReturnType()) != null && | ||
| types.asElement(method.getReturnType()).getKind() == expectedReturnType) && | ||
| (expectedReturnInterface == null || | ||
| types.isAssignable(types.asElement(method.getReturnType()).asType(), expectedReturnInterface.asType())); | ||
| } | ||
|
|
||
| private void processElements(Set<? extends Element> annotatedElements) { | ||
| for (Element element : annotatedElements) { | ||
| if (element.getKind().equals(ElementKind.ANNOTATION_TYPE)) { | ||
| boolean hasMaxVersion = false; | ||
| boolean hasRequestType = false; | ||
| boolean hasRequestProcessPhase = false; | ||
| for (Element enclosedElement : element.getEnclosedElements()) { | ||
| // Check if the annotation has a method called "validatorName" returning a String | ||
| if (enclosedElement instanceof ExecutableElement) { | ||
| ExecutableElement method = (ExecutableElement) enclosedElement; | ||
| hasMaxVersion = hasMaxVersion || validateMethod(method, MAX_VERSION_METHOD_NAME, ElementKind.ENUM, | ||
| VERSION_CLASS_NAME); | ||
| hasRequestType = hasRequestType || validateMethod(method, REQUEST_TYPE_METHOD_NAME, ElementKind.ENUM, | ||
| null); | ||
| hasRequestProcessPhase = hasRequestProcessPhase || validateMethod(method, PROCESSING_PHASE_METHOD_NAME, | ||
| ElementKind.ENUM, REQUEST_PROCESSING_PHASE_CLASS_NAME); | ||
| } | ||
| } | ||
| if (!hasMaxVersion) { | ||
|
||
| emitErrorMsg(MAX_VERSION_NOT_FOUND_ERROR_MESSAGE + " for " + | ||
| element.getSimpleName().toString()); | ||
| } | ||
| if (!hasRequestType) { | ||
| emitErrorMsg(REQUEST_TYPE_NOT_FOUND_ERROR_MESSAGE + " for " + | ||
| element.getSimpleName().toString()); | ||
| } | ||
| if (!hasRequestProcessPhase) { | ||
| emitErrorMsg(PROCESSING_PHASE_NOT_FOUND_ERROR_MESSAGE + " for " + | ||
| element.getSimpleName().toString()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private void emitErrorMsg(String s) { | ||
| processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, s); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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.hadoop.ozone; | ||
|
|
||
|
|
||
| /** | ||
| * Base class defining the version in the entire system. | ||
| */ | ||
| public interface Version { | ||
| int getVersion(); | ||
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that you have added the methods to validate the max version, but those methods are not used anywhere in the processor itself, therefore if an invalid value is present (FUTURE_VERSION), the processor does not emit an error message in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want the annotation processor to validate all that? I would rather put this check someplace else. Probably in a place like https://github.com/swamirishi/ozone/blob/fc57929908cbb05a6304366e67f64317dbf9505d/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/request/validation/ValidatorRegistry.java#L85-L88 where instead of just a set of allowedVersionTypes. I would pass a Map of version class to the allowed values. What do you think of that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I missed this reply...
The goal of the annotation processor is to signal erroneously annotated methods at compile time, so if this is testable properly, then I would prefer to cause a compile time error, as runtime issues may or may not be detected in the future, especially if you look at our compat tests, that are not really testing whether the actual validators are properly applied.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need not validate versions. We should allow future version validators as well. In that case we should just execute future versions. When the version is negative we run validators which match exactly that version.
ozone/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/validation/VersionExtractor.java
Lines 51 to 52 in 9cf34be
ozone/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/request/validation/ValidatorRegistry.java
Lines 160 to 162 in 889e081
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of disagree here still...
In the actual codebase, you can not specify a version that is not part of the enum already as you use the enum.
To define a future version validator correctly, you need to see into the future, which does not seem to be possibile.
So, even though it is probably not something one would do, but I think we may validate that at compile time.
I accept if we don't, but the only invalid value that can be specified is the FUTURE_VERSION as of now, so why not?