Search in sources :

Example 1 with MethodVisitor

use of co.cask.cdap.internal.lang.MethodVisitor in project cdap by caskdata.

the class FlowUtils method addConsumerGroup.

/**
   * Finds all consumer group for the given queue from the given flowlet.
   */
private static void addConsumerGroup(final QueueSpecification queueSpec, final Type flowletType, final long groupId, final int groupSize, final SchemaGenerator schemaGenerator, final Collection<ConsumerGroupConfig> groupConfigs) {
    final Set<FlowletMethod> seenMethods = Sets.newHashSet();
    Reflections.visit(null, flowletType, new MethodVisitor() {

        @Override
        public void visit(Object instance, Type inspectType, Type declareType, Method method) throws Exception {
            if (!seenMethods.add(FlowletMethod.create(method, inspectType))) {
                // up the class hierarchy.
                return;
            }
            ProcessInput processInputAnnotation = method.getAnnotation(ProcessInput.class);
            if (processInputAnnotation == null) {
                // Consumer has to be process method
                return;
            }
            Set<String> inputNames = Sets.newHashSet(processInputAnnotation.value());
            if (inputNames.isEmpty()) {
                // If there is no input name, it would be ANY_INPUT
                inputNames.add(FlowletDefinition.ANY_INPUT);
            }
            TypeToken<?> inspectTypeToken = TypeToken.of(inspectType);
            TypeToken<?> dataType = inspectTypeToken.resolveType(method.getGenericParameterTypes()[0]);
            // For batch mode and if the parameter is Iterator, need to get the actual data type from the Iterator.
            if (method.isAnnotationPresent(Batch.class) && Iterator.class.equals(dataType.getRawType())) {
                Preconditions.checkArgument(dataType.getType() instanceof ParameterizedType, "Only ParameterizedType is supported for batch Iterator.");
                dataType = inspectTypeToken.resolveType(((ParameterizedType) dataType.getType()).getActualTypeArguments()[0]);
            }
            Schema schema = schemaGenerator.generate(dataType.getType());
            if (queueSpec.getInputSchema().equals(schema) && (inputNames.contains(queueSpec.getQueueName().getSimpleName()) || inputNames.contains(FlowletDefinition.ANY_INPUT))) {
                groupConfigs.add(createConsumerGroupConfig(groupId, groupSize, method));
            }
        }
    });
}
Also used : Set(java.util.Set) Schema(co.cask.cdap.api.data.schema.Schema) FlowletMethod(co.cask.cdap.internal.specification.FlowletMethod) Method(java.lang.reflect.Method) FlowletMethod(co.cask.cdap.internal.specification.FlowletMethod) IOException(java.io.IOException) MethodVisitor(co.cask.cdap.internal.lang.MethodVisitor) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) TypeToken(com.google.common.reflect.TypeToken) ProcessInput(co.cask.cdap.api.annotation.ProcessInput)

Aggregations

ProcessInput (co.cask.cdap.api.annotation.ProcessInput)1 Schema (co.cask.cdap.api.data.schema.Schema)1 MethodVisitor (co.cask.cdap.internal.lang.MethodVisitor)1 FlowletMethod (co.cask.cdap.internal.specification.FlowletMethod)1 TypeToken (com.google.common.reflect.TypeToken)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Set (java.util.Set)1