Search in sources :

Example 1 with Attribute

use of com.datatorrent.api.Attribute in project apex-core by apache.

the class StramWebServices method getPortAttributes.

@GET
@Path(PATH_LOGICAL_PLAN_OPERATORS + "/{operatorName}/ports/{portName}/attributes")
@Produces(MediaType.APPLICATION_JSON)
public JSONObject getPortAttributes(@PathParam("operatorName") String operatorName, @PathParam("portName") String portName, @QueryParam("attributeName") String attributeName) {
    init();
    OperatorMeta logicalOperator = dagManager.getLogicalPlan().getOperatorMeta(operatorName);
    if (logicalOperator == null) {
        throw new NotFoundException();
    }
    HashMap<String, String> map = new HashMap<>();
    for (Map.Entry<Attribute<?>, Object> entry : dagManager.getPortAttributes(operatorName, portName).entrySet()) {
        if (attributeName == null || entry.getKey().getSimpleName().equals(attributeName)) {
            Map.Entry<Attribute<Object>, Object> entry1 = (Map.Entry<Attribute<Object>, Object>) (Map.Entry) entry;
            map.put(entry1.getKey().getSimpleName(), entry1.getKey().codec.toString(entry1.getValue()));
        }
    }
    return new JSONObject(map);
}
Also used : OperatorMeta(com.datatorrent.stram.plan.logical.LogicalPlan.OperatorMeta) JSONObject(org.codehaus.jettison.json.JSONObject) HashMap(java.util.HashMap) Attribute(com.datatorrent.api.Attribute) NotFoundException(org.apache.hadoop.yarn.webapp.NotFoundException) JSONObject(org.codehaus.jettison.json.JSONObject) Map(java.util.Map) BeanMap(org.apache.commons.beanutils.BeanMap) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with Attribute

use of com.datatorrent.api.Attribute in project apex-core by apache.

the class LogicalPlanConfigurationTest method testInvalidAttribute.

@Test
public void testInvalidAttribute() throws Exception {
    Assert.assertNotSame(0, com.datatorrent.api.Context.DAGContext.serialVersionUID);
    Attribute<String> attribute = new Attribute<>("", null);
    Field nameField = Attribute.class.getDeclaredField("name");
    nameField.setAccessible(true);
    nameField.set(attribute, "NOT_CONFIGURABLE");
    nameField.setAccessible(false);
    ContextUtils.addAttribute(com.datatorrent.api.Context.DAGContext.class, attribute);
    AttributeParseUtils.initialize();
    ConfElement.initialize();
    // attribute that cannot be configured
    Properties props = new Properties();
    props.put(StreamingApplication.APEX_PREFIX + "attr.NOT_CONFIGURABLE", "value");
    LogicalPlanConfiguration dagBuilder = new LogicalPlanConfiguration(new Configuration(false));
    dagBuilder.addFromProperties(props, null);
    try {
        dagBuilder.prepareDAG(new LogicalPlan(), null, "");
        Assert.fail("Exception expected");
    } catch (Exception e) {
        Assert.assertThat("Attribute not configurable", e.getMessage(), RegexMatcher.matches("Attribute does not support property configuration: NOT_CONFIGURABLE.*"));
    }
    ContextUtils.removeAttribute(com.datatorrent.api.Context.DAGContext.class, attribute);
    AttributeParseUtils.initialize();
    ConfElement.initialize();
    // invalid attribute name
    props = new Properties();
    String invalidAttribute = StreamingApplication.APEX_PREFIX + "attr.INVALID_NAME";
    props.put(invalidAttribute, "value");
    try {
        new LogicalPlanConfiguration(new Configuration(false)).addFromProperties(props, null);
        Assert.fail("Exception expected");
    } catch (Exception e) {
        LOG.debug("Exception message: {}", e);
        Assert.assertThat("Invalid attribute name", e.getMessage(), RegexMatcher.matches("Invalid attribute reference: " + invalidAttribute));
    }
}
Also used : DAGContext(com.datatorrent.api.Context.DAGContext) PortContext(com.datatorrent.api.Context.PortContext) TestPlanContext(com.datatorrent.stram.plan.TestPlanContext) Context(com.datatorrent.api.Context) OperatorContext(com.datatorrent.api.Context.OperatorContext) Field(java.lang.reflect.Field) Configuration(org.apache.hadoop.conf.Configuration) Attribute(com.datatorrent.api.Attribute) Integer2String(com.datatorrent.api.StringCodec.Integer2String) Properties(java.util.Properties) IOException(java.io.IOException) ValidationException(javax.validation.ValidationException) Test(org.junit.Test)

Example 3 with Attribute

use of com.datatorrent.api.Attribute in project apex-core by apache.

the class LogicalPlan method checkAttributeValueSerializable.

private void checkAttributeValueSerializable(AttributeMap attributes, String context) {
    StringBuilder sb = new StringBuilder();
    String delim = "";
    // Check all attributes got operator are serializable
    for (Entry<Attribute<?>, Object> entry : attributes.entrySet()) {
        if (entry.getValue() != null && !(entry.getValue() instanceof Serializable)) {
            sb.append(delim).append(entry.getKey().getSimpleName());
            delim = ", ";
        }
    }
    if (sb.length() > 0) {
        throw new ValidationException("Attribute value(s) for " + sb.toString() + " in " + context + " are not serializable");
    }
}
Also used : Serializable(java.io.Serializable) ValidationException(javax.validation.ValidationException) ToStringBuilder(org.apache.commons.lang.builder.ToStringBuilder) Attribute(com.datatorrent.api.Attribute)

Example 4 with Attribute

use of com.datatorrent.api.Attribute in project apex-core by apache.

the class TypeDiscoverer method getAttrDescription.

private static JSONObject getAttrDescription(Context context, Collection<Field> attributes) throws JSONException, IllegalAccessException {
    JSONObject response = new JSONObject();
    JSONArray attrArray = new JSONArray();
    response.put("attributes", attrArray);
    for (Field attrField : attributes) {
        JSONObject attrJson = new JSONObject();
        attrJson.put("name", attrField.getName());
        ParameterizedType attrType = (ParameterizedType) attrField.getGenericType();
        Attribute<?> attr = (Attribute<?>) attrField.get(context);
        Type pType = attrType.getActualTypeArguments()[0];
        TypeDiscoverer discoverer = new TypeDiscoverer();
        discoverer.resolveTypeParameters(pType, attrJson);
        if (attr.defaultValue != null) {
            attrJson.put("default", attr.defaultValue);
        }
        attrArray.put(attrJson);
    }
    return response;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JSONObject(org.codehaus.jettison.json.JSONObject) Attribute(com.datatorrent.api.Attribute) JSONArray(org.codehaus.jettison.json.JSONArray)

Example 5 with Attribute

use of com.datatorrent.api.Attribute in project apex-core by apache.

the class YarnAppLauncherImpl method launchApp.

@Override
public YarnAppHandleImpl launchApp(final StreamingApplication app, Configuration conf, Attribute.AttributeMap launchParameters) throws LauncherException {
    if (launchParameters != null) {
        for (Map.Entry<Attribute<?>, Object> entry : launchParameters.entrySet()) {
            String property = propMapping.get(entry.getKey());
            if (property != null) {
                setConfiguration(conf, property, entry.getValue());
            }
        }
    }
    try {
        String name = app.getClass().getName();
        StramAppLauncher appLauncher = new StramAppLauncher(name, conf);
        appLauncher.loadDependencies();
        StreamingAppFactory appFactory = new StreamingAppFactory(name, app.getClass()) {

            @Override
            public LogicalPlan createApp(LogicalPlanConfiguration planConfig) {
                return super.createApp(app, planConfig);
            }
        };
        ApplicationId appId = appLauncher.launchApp(appFactory);
        appLauncher.resetContextClassLoader();
        return new YarnAppHandleImpl(appId, conf);
    } catch (Exception ex) {
        throw new LauncherException(ex);
    }
}
Also used : LogicalPlanConfiguration(com.datatorrent.stram.plan.logical.LogicalPlanConfiguration) StramAppLauncher(com.datatorrent.stram.client.StramAppLauncher) Attribute(com.datatorrent.api.Attribute) StreamingAppFactory(org.apache.apex.engine.util.StreamingAppFactory) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException) YarnException(org.apache.hadoop.yarn.exceptions.YarnException)

Aggregations

Attribute (com.datatorrent.api.Attribute)10 HashMap (java.util.HashMap)5 Map (java.util.Map)5 JSONObject (org.codehaus.jettison.json.JSONObject)5 OperatorMeta (com.datatorrent.stram.plan.logical.LogicalPlan.OperatorMeta)3 Field (java.lang.reflect.Field)3 ValidationException (javax.validation.ValidationException)3 BeanMap (org.apache.commons.beanutils.BeanMap)3 Context (com.datatorrent.api.Context)2 OperatorContext (com.datatorrent.api.Context.OperatorContext)2 IOException (java.io.IOException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 NotFoundException (org.apache.hadoop.yarn.webapp.NotFoundException)2 JSONArray (org.codehaus.jettison.json.JSONArray)2 DAGContext (com.datatorrent.api.Context.DAGContext)1 PortContext (com.datatorrent.api.Context.PortContext)1 Integer2String (com.datatorrent.api.StringCodec.Integer2String)1 ObjectMapperString (com.datatorrent.common.util.ObjectMapperString)1