use of com.datatorrent.stram.plan.logical.LogicalPlan.OperatorMeta in project apex-core by apache.
the class LogicalPlanSerializer method convertToMap.
/**
*
* @param dag
* @return
*/
public static Map<String, Object> convertToMap(LogicalPlan dag, boolean includeModules) {
HashMap<String, Object> result = new HashMap<>();
ArrayList<Object> operatorArray = new ArrayList<>();
ArrayList<Object> streamMap = new ArrayList<>();
//result.put("applicationName", appConfig.getName());
result.put("operators", operatorArray);
result.put("streams", streamMap);
//LogicalPlan dag = StramAppLauncher.prepareDAG(appConfig, StreamingApplication.LAUNCHMODE_YARN);
//
// should we put the DAGContext info here?
Map<String, Object> dagAttrs = new HashMap<>();
for (Map.Entry<Attribute<Object>, Object> e : Attribute.AttributeMap.AttributeInitializer.getAllAttributes(dag, Context.DAGContext.class).entrySet()) {
dagAttrs.put(e.getKey().getSimpleName(), e.getValue());
}
result.put("attributes", dagAttrs);
Collection<OperatorMeta> allOperators = dag.getAllOperators();
ObjectMapper propertyObjectMapper = new ObjectMapper();
propertyObjectMapper.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
propertyObjectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
StdTypeResolverBuilder typer = new PropertyTypeResolverBuilder();
typer.init(JsonTypeInfo.Id.CLASS, null);
typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
propertyObjectMapper.setDefaultTyping(typer);
for (OperatorMeta operatorMeta : allOperators) {
HashMap<String, Object> operatorDetailMap = new HashMap<>();
ArrayList<Map<String, Object>> portList = new ArrayList<>();
Map<String, Object> attributeMap = new HashMap<>();
String operatorName = operatorMeta.getName();
operatorArray.add(operatorDetailMap);
operatorDetailMap.put("name", operatorName);
operatorDetailMap.put("ports", portList);
operatorDetailMap.put("class", operatorMeta.getOperator().getClass().getName());
operatorDetailMap.put("attributes", attributeMap);
Map<Attribute<Object>, Object> rawAttributes = Attribute.AttributeMap.AttributeInitializer.getAllAttributes(operatorMeta, Context.OperatorContext.class);
for (Map.Entry<Attribute<Object>, Object> entry : rawAttributes.entrySet()) {
attributeMap.put(entry.getKey().getSimpleName(), entry.getValue());
}
ObjectMapperString str;
try {
str = new ObjectMapperString(propertyObjectMapper.writeValueAsString(operatorMeta.getOperator()));
} catch (Throwable ex) {
LOG.error("Got exception when trying to get properties for operator {}", operatorMeta.getName(), ex);
str = null;
}
operatorDetailMap.put("properties", str);
Operators.PortMappingDescriptor pmd = new Operators.PortMappingDescriptor();
Operators.describe(operatorMeta.getOperator(), pmd);
for (Map.Entry<String, PortContextPair<InputPort<?>>> entry : pmd.inputPorts.entrySet()) {
HashMap<String, Object> portDetailMap = new HashMap<>();
HashMap<String, Object> portAttributeMap = new HashMap<>();
InputPortMeta portMeta = operatorMeta.getMeta(entry.getValue().component);
String portName = portMeta.getPortName();
portDetailMap.put("name", portName);
portDetailMap.put("type", "input");
portDetailMap.put("attributes", portAttributeMap);
rawAttributes = Attribute.AttributeMap.AttributeInitializer.getAllAttributes(portMeta, Context.PortContext.class);
for (Map.Entry<Attribute<Object>, Object> attEntry : rawAttributes.entrySet()) {
portAttributeMap.put(attEntry.getKey().getSimpleName(), attEntry.getValue());
}
portList.add(portDetailMap);
}
for (Map.Entry<String, PortContextPair<OutputPort<?>>> entry : pmd.outputPorts.entrySet()) {
HashMap<String, Object> portDetailMap = new HashMap<>();
HashMap<String, Object> portAttributeMap = new HashMap<>();
OutputPortMeta portMeta = operatorMeta.getMeta(entry.getValue().component);
String portName = portMeta.getPortName();
portDetailMap.put("name", portName);
portDetailMap.put("type", "output");
portDetailMap.put("attributes", portAttributeMap);
rawAttributes = Attribute.AttributeMap.AttributeInitializer.getAllAttributes(portMeta, Context.PortContext.class);
for (Map.Entry<Attribute<Object>, Object> attEntry : rawAttributes.entrySet()) {
portAttributeMap.put(attEntry.getKey().getSimpleName(), attEntry.getValue());
}
portList.add(portDetailMap);
}
}
Collection<StreamMeta> allStreams = dag.getAllStreams();
for (StreamMeta streamMeta : allStreams) {
HashMap<String, Object> streamDetailMap = new HashMap<>();
String streamName = streamMeta.getName();
streamMap.add(streamDetailMap);
String sourcePortName = streamMeta.getSource().getPortName();
OperatorMeta operatorMeta = streamMeta.getSource().getOperatorMeta();
HashMap<String, Object> sourcePortDetailMap = new HashMap<>();
sourcePortDetailMap.put("operatorName", operatorMeta.getName());
sourcePortDetailMap.put("portName", sourcePortName);
streamDetailMap.put("name", streamName);
streamDetailMap.put("source", sourcePortDetailMap);
Collection<InputPortMeta> sinks = streamMeta.getSinks();
ArrayList<HashMap<String, Object>> sinkPortList = new ArrayList<>();
for (InputPortMeta sinkPort : sinks) {
HashMap<String, Object> sinkPortDetailMap = new HashMap<>();
sinkPortDetailMap.put("operatorName", sinkPort.getOperatorMeta().getName());
sinkPortDetailMap.put("portName", sinkPort.getPortName());
sinkPortList.add(sinkPortDetailMap);
}
streamDetailMap.put("sinks", sinkPortList);
if (streamMeta.getLocality() != null) {
streamDetailMap.put("locality", streamMeta.getLocality().name());
}
}
if (includeModules) {
ArrayList<Map<String, Object>> modulesArray = new ArrayList<>();
result.put("modules", modulesArray);
for (LogicalPlan.ModuleMeta meta : dag.getAllModules()) {
modulesArray.add(getLogicalModuleDetails(dag, meta));
}
}
return result;
}
use of com.datatorrent.stram.plan.logical.LogicalPlan.OperatorMeta in project apex-core by apache.
the class LogicalPlanModificationTest method testAddStream.
@Test
public void testAddStream() {
GenericTestOperator o1 = dag.addOperator("o1", GenericTestOperator.class);
GenericTestOperator o2 = dag.addOperator("o2", GenericTestOperator.class);
TestPlanContext ctx = new TestPlanContext();
dag.setAttribute(OperatorContext.STORAGE_AGENT, ctx);
PhysicalPlan plan = new PhysicalPlan(dag, ctx);
List<PTOperator> o1Instances = plan.getOperators(dag.getMeta(o1));
Assert.assertEquals("o1Instances " + o1Instances, 1, o1Instances.size());
PTOperator o1p1 = o1Instances.get(0);
OperatorMeta om2 = dag.getMeta(o2);
List<PTOperator> o2Instances = plan.getOperators(om2);
Assert.assertEquals("o2Instances " + o2Instances, 1, o2Instances.size());
PTOperator o2p1 = o2Instances.get(0);
Assert.assertEquals("outputs " + o1p1, 0, o1p1.getOutputs().size());
Assert.assertEquals("inputs " + o2p1, 0, o2p1.getInputs().size());
PlanModifier pm = new PlanModifier(plan);
pm.addStream("o1.outport1", o1.outport1, o2.inport1);
pm.addStream("o1.outport1", o1.outport1, o2.inport2);
pm.applyChanges(ctx);
Assert.assertEquals("undeploy " + ctx.undeploy, 2, ctx.undeploy.size());
Assert.assertEquals("deploy " + ctx.deploy, 2, ctx.deploy.size());
Assert.assertEquals("outputs " + o1p1, 1, o1p1.getOutputs().size());
Assert.assertEquals("inputs " + o2p1, 2, o2p1.getInputs().size());
Set<String> portNames = Sets.newHashSet();
for (PTOperator.PTInput in : o2p1.getInputs()) {
portNames.add(in.portName);
}
Set<String> expPortNames = Sets.newHashSet(GenericTestOperator.IPORT1, GenericTestOperator.IPORT2);
Assert.assertEquals("input port names " + o2p1.getInputs(), expPortNames, portNames);
}
use of com.datatorrent.stram.plan.logical.LogicalPlan.OperatorMeta in project apex-core by apache.
the class LogicalPlanModificationTest method testRemoveOperator.
@Test
public void testRemoveOperator() {
GenericTestOperator o1 = dag.addOperator("o1", GenericTestOperator.class);
OperatorMeta o1Meta = dag.getMeta(o1);
GenericTestOperator o12 = dag.addOperator("o12", GenericTestOperator.class);
OperatorMeta o12Meta = dag.getMeta(o12);
GenericTestOperator o2 = dag.addOperator("o2", GenericTestOperator.class);
OperatorMeta o2Meta = dag.getMeta(o2);
GenericTestOperator o3 = dag.addOperator("o3", GenericTestOperator.class);
OperatorMeta o3Meta = dag.getMeta(o3);
LogicalPlan.StreamMeta s1 = dag.addStream("o1.outport1", o1.outport1, o2.inport1, o12.inport1);
LogicalPlan.StreamMeta s2 = dag.addStream("o2.outport1", o2.outport1, o3.inport1);
TestPlanContext ctx = new TestPlanContext();
dag.setAttribute(OperatorContext.STORAGE_AGENT, ctx);
PhysicalPlan plan = new PhysicalPlan(dag, ctx);
ctx.deploy.clear();
ctx.undeploy.clear();
Assert.assertEquals("containers " + plan.getContainers(), 4, plan.getContainers().size());
Assert.assertEquals("physical operators " + plan.getAllOperators(), 4, plan.getAllOperators().size());
Assert.assertEquals("sinks s1 " + s1.getSinks(), 2, s1.getSinks().size());
List<PTOperator> o2PhysicalOpers = plan.getOperators(o2Meta);
Assert.assertEquals("instances " + o2Meta, 1, o2PhysicalOpers.size());
PlanModifier pm = new PlanModifier(plan);
try {
pm.removeOperator(o2Meta.getName());
Assert.fail("validation error (connected output stream) expected");
} catch (ValidationException ve) {
// all good
}
// remove output stream required before removing operator
pm.removeStream(s2.getName());
pm.removeOperator(o2Meta.getName());
pm.applyChanges(ctx);
Assert.assertEquals("sinks s1 " + s1.getSinks(), 1, s1.getSinks().size());
Assert.assertTrue("undeploy " + ctx.undeploy, ctx.undeploy.containsAll(o2PhysicalOpers));
Assert.assertTrue("deploy " + ctx.deploy, !ctx.deploy.containsAll(o2PhysicalOpers));
Assert.assertEquals("streams " + dag.getAllStreams(), 1, dag.getAllStreams().size());
Assert.assertEquals("operators " + dag.getAllOperators(), 3, dag.getAllOperators().size());
Assert.assertTrue("operators " + dag.getAllOperators(), dag.getAllOperators().containsAll(Sets.newHashSet(o1Meta, o3Meta)));
try {
plan.getOperators(o2Meta);
Assert.fail("removed from physical plan: " + o2Meta);
} catch (Exception e) {
// all good
}
Assert.assertEquals("containers " + plan.getContainers(), 3, plan.getContainers().size());
Assert.assertEquals("physical operators " + plan.getAllOperators(), 3, plan.getAllOperators().size());
Assert.assertEquals("removed containers " + ctx.releaseContainers, 1, ctx.releaseContainers.size());
try {
pm.removeOperator(o12Meta.getName());
Assert.fail("cannot remove operator prior to removing input stream");
} catch (ValidationException ve) {
Assert.assertTrue("" + ve.getMessage(), ve.getMessage().matches(".*Operator o12 connected to input streams.*"));
}
pm.removeStream(s1.getName());
pm.removeOperator(o12Meta.getName());
}
use of com.datatorrent.stram.plan.logical.LogicalPlan.OperatorMeta in project apex-core by apache.
the class CheckpointTest method testUpdateCheckpointsRecovery.
@Test
public void testUpdateCheckpointsRecovery() {
MockClock clock = new MockClock();
dag.setAttribute(com.datatorrent.api.Context.OperatorContext.STORAGE_AGENT, new MemoryStorageAgent());
dag.setAttribute(LogicalPlan.STREAMING_WINDOW_SIZE_MILLIS, 1);
GenericTestOperator o1 = dag.addOperator("o1", GenericTestOperator.class);
StatelessOperator o2SL = dag.addOperator("o2SL", StatelessOperator.class);
StatelessOperator o3SL = dag.addOperator("o3SL", StatelessOperator.class);
GenericTestOperator o4 = dag.addOperator("o4", GenericTestOperator.class);
dag.addStream("o1.outport1", o1.outport1, o2SL.inport1);
dag.addStream("o2SL.outport1", o2SL.outport1, o3SL.inport1, o4.inport1);
StreamingContainerManager dnm = new StreamingContainerManager(dag, clock);
PhysicalPlan plan = dnm.getPhysicalPlan();
for (PTOperator oper : plan.getAllOperators().values()) {
Assert.assertEquals("activation windowId " + oper, Checkpoint.INITIAL_CHECKPOINT, oper.getRecoveryCheckpoint());
Assert.assertEquals("checkpoints " + oper, Collections.emptyList(), oper.checkpoints);
}
PTOperator o1p1 = plan.getOperators(dag.getMeta(o1)).get(0);
PTOperator o2SLp1 = plan.getOperators(dag.getMeta(o2SL)).get(0);
PTOperator o3SLp1 = plan.getOperators(dag.getMeta(o3SL)).get(0);
PTOperator o4p1 = plan.getOperators(dag.getMeta(o4)).get(0);
Checkpoint leafCheckpoint = new Checkpoint(2L, 0, 0);
clock.time = 3;
o4p1.checkpoints.add(leafCheckpoint);
UpdateCheckpointsContext ctx;
dnm.updateRecoveryCheckpoints(o1p1, ctx = new UpdateCheckpointsContext(clock, true, Collections.<OperatorMeta, Set<OperatorMeta>>emptyMap()), false);
Assert.assertEquals("initial checkpoint " + o1p1, Checkpoint.INITIAL_CHECKPOINT, o1p1.getRecoveryCheckpoint());
Assert.assertEquals("initial checkpoint " + o2SLp1, leafCheckpoint, o2SLp1.getRecoveryCheckpoint());
Assert.assertEquals("initial checkpoint " + o3SLp1, new Checkpoint(clock.getTime(), 0, 0), o3SLp1.getRecoveryCheckpoint());
Assert.assertEquals("number dependencies " + ctx.visited, plan.getAllOperators().size(), ctx.visited.size());
}
use of com.datatorrent.stram.plan.logical.LogicalPlan.OperatorMeta in project apex-core by apache.
the class CheckpointTest method testUpdateRecoveryCheckpointWithCycle.
@Test
public void testUpdateRecoveryCheckpointWithCycle() throws Exception {
Clock clock = new SystemClock();
dag.setAttribute(com.datatorrent.api.Context.OperatorContext.STORAGE_AGENT, new MemoryStorageAgent());
// Simulate a DAG with a loop which has a unifier operator
TestGeneratorInputOperator o1 = dag.addOperator("o1", TestGeneratorInputOperator.class);
GenericTestOperator o2 = dag.addOperator("o2", GenericTestOperator.class);
GenericTestOperator o3 = dag.addOperator("o3", GenericTestOperator.class);
GenericTestOperator o4 = dag.addOperator("o4", GenericTestOperator.class);
DefaultDelayOperator d = dag.addOperator("d", DefaultDelayOperator.class);
dag.addStream("o1.output1", o1.outport, o2.inport1);
dag.addStream("o2.output1", o2.outport1, o3.inport1);
dag.addStream("o3.output1", o3.outport1, o4.inport1);
dag.addStream("o4.output1", o4.outport1, d.input);
dag.addStream("d.output", d.output, o2.inport2);
dag.setOperatorAttribute(o3, Context.OperatorContext.PARTITIONER, new StatelessPartitioner<Operator>(2));
dag.validate();
StreamingContainerManager dnm = new StreamingContainerManager(dag);
PhysicalPlan plan = dnm.getPhysicalPlan();
for (PTOperator oper : plan.getAllOperators().values()) {
Assert.assertEquals("Initial activation windowId" + oper, Checkpoint.INITIAL_CHECKPOINT, oper.getRecoveryCheckpoint());
Assert.assertEquals("Checkpoints empty" + oper, Collections.emptyList(), oper.checkpoints);
}
Checkpoint cp1 = new Checkpoint(1L, 0, 0);
Checkpoint cp2 = new Checkpoint(2L, 0, 0);
Map<OperatorMeta, Set<OperatorMeta>> checkpointGroups = dnm.getCheckpointGroups();
Map<Integer, PTOperator> allOperators = plan.getAllOperators();
for (PTOperator operator : allOperators.values()) {
operator.setState(PTOperator.State.ACTIVE);
operator.checkpoints.add(cp1);
dnm.updateRecoveryCheckpoints(operator, new UpdateCheckpointsContext(clock, false, checkpointGroups), false);
}
List<PTOperator> physicalO1 = plan.getOperators(dag.getOperatorMeta("o1"));
physicalO1.get(0).checkpoints.add(cp2);
dnm.updateRecoveryCheckpoints(physicalO1.get(0), new UpdateCheckpointsContext(clock, false, checkpointGroups), false);
Assert.assertEquals("Recovery checkpoint updated ", physicalO1.get(0).getRecoveryCheckpoint(), cp1);
}
Aggregations