swamirishi commented on code in PR #8404: URL: https://github.com/apache/ozone/pull/8404#discussion_r2103208733
########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/validation/ValidatorRegistry.java: ########## @@ -42,156 +47,274 @@ * Registry that loads and stores the request validators to be applied by * a service. */ -public class ValidatorRegistry { +public class ValidatorRegistry<RequestType extends Enum<RequestType>> { - private final EnumMap<ValidationCondition, - EnumMap<Type, EnumMap<RequestProcessingPhase, List<Method>>>> - validators = new EnumMap<>(ValidationCondition.class); + /** + * A validator registered should have the following parameters: + * applyBeforeVersion: Enum extending Version + * RequestType: Enum signifying the type of request. + * RequestProcessingPhase: Signifying if the validator is supposed to run pre or post submitting the request. + * Based on the afforementioned parameters a complete map is built which stores the validators in a sorted order of + * the applyBeforeVersion value of the validator method. + * Thus when a request comes with a certain version value, all validators containing `applyBeforeVersion` parameter + * greater than the request versions get triggered. + * {@link #validationsFor(Enum, RequestProcessingPhase, Class, Versioned)} + */ + private final Map<Class<? extends Versioned>, EnumMap<RequestType, + EnumMap<RequestProcessingPhase, IndexedItems<Method, Integer>>>> indexedValidatorMap; /** * Creates a {@link ValidatorRegistry} instance that discovers validation * methods in the provided package and the packages in the same resource. - * A validation method is recognized by the {@link RequestFeatureValidator} - * annotation that contains important information about how and when to use - * the validator. + * A validation method is recognized by all the annotations classes which + * are annotated by {@link RegisterValidator} annotation that contains + * important information about how and when to use the validator. + * @param requestType class of request type enum. * @param validatorPackage the main package inside which validatiors should * be discovered. + * @param allowedValidators a set containing the various types of version allowed to be registered. + * @param allowedProcessingPhases set of request processing phases which would be allowed to be registered to + * registry. + * */ - ValidatorRegistry(String validatorPackage) { - this(ClasspathHelper.forPackage(validatorPackage)); + public ValidatorRegistry(Class<RequestType> requestType, + String validatorPackage, + Set<Class<? extends Annotation>> allowedValidators, + Set<RequestProcessingPhase> allowedProcessingPhases) { + this(requestType, ClasspathHelper.forPackage(validatorPackage), allowedValidators, allowedProcessingPhases); } /** * Creates a {@link ValidatorRegistry} instance that discovers validation * methods under the provided URL. - * A validation method is recognized by the {@link RequestFeatureValidator} + * A validation method is recognized by all annotations annotated by the {@link RegisterValidator} * annotation that contains important information about how and when to use * the validator. + * @param requestType class of request type enum. * @param searchUrls the path in which the annotated methods are searched. + * @param allowedValidators a set containing the various types of validator annotation allowed to be registered. + * @param allowedProcessingPhases set of request processing phases which would be allowed to be registered to + * registry. */ - ValidatorRegistry(Collection<URL> searchUrls) { + public ValidatorRegistry(Class<RequestType> requestType, + Collection<URL> searchUrls, + Set<Class<? extends Annotation>> allowedValidators, + Set<RequestProcessingPhase> allowedProcessingPhases) { + Class<RequestType[]> requestArrayClass = (Class<RequestType[]>) Array.newInstance(requestType, 0) + .getClass(); + Set<Class<? extends Annotation>> validatorsToBeRegistered = + new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage("org.apache.hadoop")) + .setScanners(Scanners.TypesAnnotated) + .setParallel(true)).getTypesAnnotatedWith(RegisterValidator.class).stream() + .filter(allowedValidators::contains) + .filter(annotationClass -> getReturnTypeOfAnnotationMethod((Class<? extends Annotation>) annotationClass, + RegisterValidator.REQUEST_TYPE_METHOD_NAME) + .equals(requestArrayClass)) + .map(annotationClass -> (Class<? extends Annotation>) annotationClass) + .collect(Collectors.toSet()); + this.indexedValidatorMap = + allowedValidators.stream().collect(ImmutableMap.toImmutableMap(annotationClass -> + (Class<? extends Versioned>) getReturnTypeOfAnnotationMethod(annotationClass, + RegisterValidator.APPLY_BEFORE_METHOD_NAME), + validatorClass -> new EnumMap<>(requestType))); Reflections reflections = new Reflections(new ConfigurationBuilder() .setUrls(searchUrls) .setScanners(Scanners.MethodsAnnotated) .setParallel(true) ); - - Set<Method> describedValidators = - reflections.getMethodsAnnotatedWith(RequestFeatureValidator.class); - initMaps(describedValidators); + initMaps(requestArrayClass, allowedProcessingPhases, validatorsToBeRegistered, reflections); } /** - * Get the validators that has to be run in the given list of - * {@link ValidationCondition}s, for the given requestType and + * Get the validators that has to be run in the given list of, + * for the given requestType and for the given request versions. * {@link RequestProcessingPhase}. * - * @param conditions conditions that are present for the request * @param requestType the type of the protocol message * @param phase the request processing phase + * @param requestVersions different versions extracted from the request. * @return the list of validation methods that has to run. */ - List<Method> validationsFor( - List<ValidationCondition> conditions, - Type requestType, - RequestProcessingPhase phase) { - - if (conditions.isEmpty() || validators.isEmpty()) { - return Collections.emptyList(); - } - - Set<Method> returnValue = new HashSet<>(); - - for (ValidationCondition condition: conditions) { - returnValue.addAll(validationsFor(condition, requestType, phase)); - } - return new ArrayList<>(returnValue); + public List<Method> validationsFor(RequestType requestType, + RequestProcessingPhase phase, + Map<Class<? extends Annotation>, ? extends Versioned> requestVersions) { Review Comment: done -- 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: issues-unsubscr...@ozone.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@ozone.apache.org For additional commands, e-mail: issues-h...@ozone.apache.org