Java Code Examples for org.apache.hadoop.util.ReflectionUtils#getDeclaredFieldsIncludingInherited()

The following examples show how to use org.apache.hadoop.util.ReflectionUtils#getDeclaredFieldsIncludingInherited() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Node.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Node(OPERATOR operator, OperatorContext context)
{
  this.operator = operator;
  this.context = context;
  executorService = Executors.newSingleThreadExecutor();
  taskQueue = new LinkedList<>();

  outputs = new HashMap<>();

  descriptor = new PortMappingDescriptor();
  Operators.describe(operator, descriptor);

  endWindowDequeueTimes = new HashMap<>();
  tmb = ManagementFactory.getThreadMXBean();
  commandResponse = new LinkedBlockingQueue<>();

  metricFields = Lists.newArrayList();
  for (Field field : ReflectionUtils.getDeclaredFieldsIncludingInherited(operator.getClass())) {
    if (field.isAnnotationPresent(AutoMetric.class)) {
      metricFields.add(field);
      field.setAccessible(true);
    }
  }

  metricMethods = Maps.newHashMap();
  try {
    for (PropertyDescriptor pd : Introspector.getBeanInfo(operator.getClass()).getPropertyDescriptors()) {
      Method readMethod = pd.getReadMethod();
      if (readMethod != null) {
        AutoMetric rfa = readMethod.getAnnotation(AutoMetric.class);
        if (rfa != null) {
          metricMethods.put(pd.getName(), readMethod);
        }
      }
    }
  } catch (IntrospectionException e) {
    throw new RuntimeException("introspecting {}", e);
  }
}
 
Example 2
Source File: MetricsSourceBuilder.java    From hadoop with Apache License 2.0 5 votes vote down vote up
MetricsSourceBuilder(Object source, MutableMetricsFactory factory) {
  this.source = checkNotNull(source, "source");
  this.factory = checkNotNull(factory, "mutable metrics factory");
  Class<?> cls = source.getClass();
  registry = initRegistry(source);

  for (Field field : ReflectionUtils.getDeclaredFieldsIncludingInherited(cls)) {
    add(source, field);
  }
  for (Method method : ReflectionUtils.getDeclaredMethodsIncludingInherited(cls)) {
    add(source, method);
  }
}
 
Example 3
Source File: MetricsSourceBuilder.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private MetricsRegistry initRegistry(Object source) {
  Class<?> cls = source.getClass();
  MetricsRegistry r = null;
  // Get the registry if it already exists.
  for (Field field : ReflectionUtils.getDeclaredFieldsIncludingInherited(cls)) {
    if (field.getType() != MetricsRegistry.class) continue;
    try {
      field.setAccessible(true);
      r = (MetricsRegistry) field.get(source);
      hasRegistry = r != null;
      break;
    }
    catch (Exception e) {
      LOG.warn("Error accessing field "+ field, e);
      continue;
    }
  }
  // Create a new registry according to annotation
  for (Annotation annotation : cls.getAnnotations()) {
    if (annotation instanceof Metrics) {
      Metrics ma = (Metrics) annotation;
      info = factory.getInfo(cls, ma);
      if (r == null) {
        r = new MetricsRegistry(info);
      }
      r.setContext(ma.context());
    }
  }
  if (r == null) return new MetricsRegistry(cls.getSimpleName());
  return r;
}
 
Example 4
Source File: MetricsSourceBuilder.java    From big-c with Apache License 2.0 5 votes vote down vote up
MetricsSourceBuilder(Object source, MutableMetricsFactory factory) {
  this.source = checkNotNull(source, "source");
  this.factory = checkNotNull(factory, "mutable metrics factory");
  Class<?> cls = source.getClass();
  registry = initRegistry(source);

  for (Field field : ReflectionUtils.getDeclaredFieldsIncludingInherited(cls)) {
    add(source, field);
  }
  for (Method method : ReflectionUtils.getDeclaredMethodsIncludingInherited(cls)) {
    add(source, method);
  }
}
 
Example 5
Source File: MetricsSourceBuilder.java    From big-c with Apache License 2.0 5 votes vote down vote up
private MetricsRegistry initRegistry(Object source) {
  Class<?> cls = source.getClass();
  MetricsRegistry r = null;
  // Get the registry if it already exists.
  for (Field field : ReflectionUtils.getDeclaredFieldsIncludingInherited(cls)) {
    if (field.getType() != MetricsRegistry.class) continue;
    try {
      field.setAccessible(true);
      r = (MetricsRegistry) field.get(source);
      hasRegistry = r != null;
      break;
    }
    catch (Exception e) {
      LOG.warn("Error accessing field "+ field, e);
      continue;
    }
  }
  // Create a new registry according to annotation
  for (Annotation annotation : cls.getAnnotations()) {
    if (annotation instanceof Metrics) {
      Metrics ma = (Metrics) annotation;
      info = factory.getInfo(cls, ma);
      if (r == null) {
        r = new MetricsRegistry(info);
      }
      r.setContext(ma.context());
    }
  }
  if (r == null) return new MetricsRegistry(cls.getSimpleName());
  return r;
}
 
Example 6
Source File: Node.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
public Node(OPERATOR operator, OperatorContext context)
{
  this.operator = operator;
  this.context = context;
  executorService = Executors.newSingleThreadExecutor();
  taskQueue = new LinkedList<>();

  outputs = new HashMap<>();

  descriptor = new PortMappingDescriptor();
  Operators.describe(operator, descriptor);

  endWindowDequeueTimes = new HashMap<>();
  tmb = ManagementFactory.getThreadMXBean();
  commandResponse = new LinkedBlockingQueue<>();

  metricFields = Lists.newArrayList();
  for (Field field : ReflectionUtils.getDeclaredFieldsIncludingInherited(operator.getClass())) {
    if (field.isAnnotationPresent(AutoMetric.class)) {
      metricFields.add(field);
      field.setAccessible(true);
    }
  }

  metricMethods = Maps.newHashMap();
  try {
    for (PropertyDescriptor pd : Introspector.getBeanInfo(operator.getClass()).getPropertyDescriptors()) {
      Method readMethod = pd.getReadMethod();
      if (readMethod != null) {
        AutoMetric rfa = readMethod.getAnnotation(AutoMetric.class);
        if (rfa != null) {
          metricMethods.put(pd.getName(), readMethod);
        }
      }
    }
  } catch (IntrospectionException e) {
    throw new RuntimeException("introspecting {}", e);
  }
}
 
Example 7
Source File: LogicalPlan.java    From Bats with Apache License 2.0 4 votes vote down vote up
protected void populateAggregatorMeta()
{
  AutoMetric.Aggregator aggregator = getValue(OperatorContext.METRICS_AGGREGATOR);
  if (aggregator == null && operator instanceof AutoMetric.Aggregator) {
    aggregator = new MetricAggregatorMeta.MetricsAggregatorProxy(this);
  }
  if (aggregator == null) {
    MetricsAggregator defAggregator = null;
    Set<String> metricNames = Sets.newHashSet();

    for (Field field : ReflectionUtils.getDeclaredFieldsIncludingInherited(operator.getClass())) {

      if (field.isAnnotationPresent(AutoMetric.class)) {
        metricNames.add(field.getName());

        if (field.getType() == int.class || field.getType() == Integer.class ||
            field.getType() == long.class || field.getType() == Long.class) {
          if (defAggregator == null) {
            defAggregator = new MetricsAggregator();
          }
          defAggregator.addAggregators(field.getName(), new SingleMetricAggregator[]{new LongSumAggregator()});
        } else if (field.getType() == float.class || field.getType() == Float.class ||
            field.getType() == double.class || field.getType() == Double.class) {
          if (defAggregator == null) {
            defAggregator = new MetricsAggregator();
          }
          defAggregator.addAggregators(field.getName(), new SingleMetricAggregator[]{new DoubleSumAggregator()});
        }
      }
    }

    try {
      for (PropertyDescriptor pd : Introspector.getBeanInfo(operator.getClass()).getPropertyDescriptors()) {
        Method readMethod = pd.getReadMethod();
        if (readMethod != null) {
          AutoMetric rfa = readMethod.getAnnotation(AutoMetric.class);
          if (rfa != null) {
            String propName = pd.getName();
            if (metricNames.contains(propName)) {
              continue;
            }

            if (readMethod.getReturnType() == int.class || readMethod.getReturnType() == Integer.class ||
                readMethod.getReturnType() == long.class || readMethod.getReturnType() == Long.class) {

              if (defAggregator == null) {
                defAggregator = new MetricsAggregator();
              }
              defAggregator.addAggregators(propName, new SingleMetricAggregator[]{new LongSumAggregator()});

            } else if (readMethod.getReturnType() == float.class || readMethod.getReturnType() == Float.class ||
                readMethod.getReturnType() == double.class || readMethod.getReturnType() == Double.class) {

              if (defAggregator == null) {
                defAggregator = new MetricsAggregator();
              }
              defAggregator.addAggregators(propName, new SingleMetricAggregator[]{new DoubleSumAggregator()});
            }
          }
        }
      }
    } catch (IntrospectionException e) {
      throw new RuntimeException("finding methods", e);
    }

    if (defAggregator != null) {
      aggregator = defAggregator;
    }
  }
  this.metricAggregatorMeta = new MetricAggregatorMeta(aggregator,
      getValue(OperatorContext.METRICS_DIMENSIONS_SCHEME));
}
 
Example 8
Source File: LogicalPlan.java    From attic-apex-core with Apache License 2.0 4 votes vote down vote up
protected void populateAggregatorMeta()
{
  AutoMetric.Aggregator aggregator = getValue(OperatorContext.METRICS_AGGREGATOR);
  if (aggregator == null && operator instanceof AutoMetric.Aggregator) {
    aggregator = new MetricAggregatorMeta.MetricsAggregatorProxy(this);
  }
  if (aggregator == null) {
    MetricsAggregator defAggregator = null;
    Set<String> metricNames = Sets.newHashSet();

    for (Field field : ReflectionUtils.getDeclaredFieldsIncludingInherited(operator.getClass())) {

      if (field.isAnnotationPresent(AutoMetric.class)) {
        metricNames.add(field.getName());

        if (field.getType() == int.class || field.getType() == Integer.class ||
            field.getType() == long.class || field.getType() == Long.class) {
          if (defAggregator == null) {
            defAggregator = new MetricsAggregator();
          }
          defAggregator.addAggregators(field.getName(), new SingleMetricAggregator[]{new LongSumAggregator()});
        } else if (field.getType() == float.class || field.getType() == Float.class ||
            field.getType() == double.class || field.getType() == Double.class) {
          if (defAggregator == null) {
            defAggregator = new MetricsAggregator();
          }
          defAggregator.addAggregators(field.getName(), new SingleMetricAggregator[]{new DoubleSumAggregator()});
        }
      }
    }

    try {
      for (PropertyDescriptor pd : Introspector.getBeanInfo(operator.getClass()).getPropertyDescriptors()) {
        Method readMethod = pd.getReadMethod();
        if (readMethod != null) {
          AutoMetric rfa = readMethod.getAnnotation(AutoMetric.class);
          if (rfa != null) {
            String propName = pd.getName();
            if (metricNames.contains(propName)) {
              continue;
            }

            if (readMethod.getReturnType() == int.class || readMethod.getReturnType() == Integer.class ||
                readMethod.getReturnType() == long.class || readMethod.getReturnType() == Long.class) {

              if (defAggregator == null) {
                defAggregator = new MetricsAggregator();
              }
              defAggregator.addAggregators(propName, new SingleMetricAggregator[]{new LongSumAggregator()});

            } else if (readMethod.getReturnType() == float.class || readMethod.getReturnType() == Float.class ||
                readMethod.getReturnType() == double.class || readMethod.getReturnType() == Double.class) {

              if (defAggregator == null) {
                defAggregator = new MetricsAggregator();
              }
              defAggregator.addAggregators(propName, new SingleMetricAggregator[]{new DoubleSumAggregator()});
            }
          }
        }
      }
    } catch (IntrospectionException e) {
      throw new RuntimeException("finding methods", e);
    }

    if (defAggregator != null) {
      aggregator = defAggregator;
    }
  }
  this.metricAggregatorMeta = new MetricAggregatorMeta(aggregator,
      getValue(OperatorContext.METRICS_DIMENSIONS_SCHEME));
}