-
Notifications
You must be signed in to change notification settings - Fork 496
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
HDDS-11149. Have a generic version Validator for validating Requests #6932
Draft
swamirishi
wants to merge
12
commits into
apache:master
Choose a base branch
from
swamirishi:HDDS-11149
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bde95f7
HDDS-11149. Add a maximum client version in the RequestFeatureValidat…
swamirishi f1676be
HDDS-11149. Set conditions default value to {}
swamirishi b77d1bf
Merge remote-tracking branch 'origin/master' into HDDS-11149
adoroszlai 561d010
HDDS-11149. Create Base validator for generic version validation
swamirishi 363d9fa
Merge remote-tracking branch 'apache/master' into HEAD
swamirishi e623fdb
HDDS-11149. Add register validator processor
swamirishi b270bf6
HDDS-11149. Fix checkstyle
swamirishi ca3746d
HDDS-11149. Fix findbugs & rat failures
swamirishi a84749b
HDDS-11149. Move to enum maps
swamirishi fc57929
HDDS-11149. Move validation to ozone-common and fix docs
swamirishi 778229a
HDDS-11149. Fix typo while extracting version
swamirishi 9cf34be
HDDS-11149. Address review comments
swamirishi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
142 changes: 142 additions & 0 deletions
142
...ds/annotations/src/main/java/org/apache/ozone/annotations/RegisterValidatorProcessor.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,142 @@ | ||
/* | ||
* 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.type.ArrayType; | ||
import javax.lang.model.type.TypeKind; | ||
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.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 APPLY_UNTIL_METHOD_NAME = "applyUntil"; | ||
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 " + APPLY_UNTIL_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 validateArrayMethod(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 || | ||
TypeKind.ARRAY.equals(method.getReturnType().getKind()) && | ||
types.asElement(((ArrayType)method.getReturnType()).getComponentType()).getKind() == expectedReturnType) && | ||
(expectedReturnInterface == null || | ||
types.isAssignable(types.asElement(method.getReturnType()).asType(), expectedReturnInterface.asType())); | ||
} | ||
|
||
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 hasApplyUntilMethod = 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; | ||
hasApplyUntilMethod = hasApplyUntilMethod || validateMethod(method, APPLY_UNTIL_METHOD_NAME, ElementKind.ENUM, | ||
VERSION_CLASS_NAME); | ||
hasRequestType = hasRequestType || validateArrayMethod(method, REQUEST_TYPE_METHOD_NAME, ElementKind.ENUM, | ||
null); | ||
hasRequestProcessPhase = hasRequestProcessPhase || validateMethod(method, PROCESSING_PHASE_METHOD_NAME, | ||
ElementKind.ENUM, REQUEST_PROCESSING_PHASE_CLASS_NAME); | ||
} | ||
} | ||
if (!hasApplyUntilMethod) { | ||
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); | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/Version.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,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 version(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?