use of com.datatorrent.api.Operator in project apex-core by apache.
the class PhysicalPlan method getPartitioner.
private Partitioner<Operator> getPartitioner(PMapping currentMapping) {
Operator operator = currentMapping.logicalOperator.getOperator();
Partitioner<Operator> partitioner = null;
if (currentMapping.logicalOperator.getAttributes().contains(OperatorContext.PARTITIONER)) {
@SuppressWarnings("unchecked") Partitioner<Operator> tmp = (Partitioner<Operator>) currentMapping.logicalOperator.getValue(OperatorContext.PARTITIONER);
partitioner = tmp;
} else if (operator instanceof Partitioner) {
@SuppressWarnings("unchecked") Partitioner<Operator> tmp = (Partitioner<Operator>) operator;
partitioner = tmp;
}
return partitioner;
}
use of com.datatorrent.api.Operator in project apex-core by apache.
the class StreamMapping method createSlidingUnifier.
public static PTOperator createSlidingUnifier(StreamMeta streamMeta, PhysicalPlan plan, int operatorApplicationWindowCount, int slidingWindowCount) {
int gcd = IntMath.gcd(operatorApplicationWindowCount, slidingWindowCount);
OperatorMeta um = streamMeta.getSource().getSlidingUnifier(operatorApplicationWindowCount / gcd, gcd, slidingWindowCount / gcd);
PTOperator pu = plan.newOperator(um, um.getName());
Operator unifier = um.getOperator();
PortMappingDescriptor mergeDesc = new PortMappingDescriptor();
Operators.describe(unifier, mergeDesc);
if (mergeDesc.outputPorts.size() != 1) {
throw new AssertionError("Unifier must have a single output port, instead found : " + mergeDesc.outputPorts);
}
pu.unifiedOperatorMeta = streamMeta.getSource().getOperatorMeta();
pu.outputs.add(new PTOutput(mergeDesc.outputPorts.keySet().iterator().next(), streamMeta, pu));
plan.newOpers.put(pu, unifier);
return pu;
}
use of com.datatorrent.api.Operator in project apex-core by apache.
the class StreamingContainerAgent method createOperatorDeployInfo.
/**
* Create deploy info for operator.
* <p>
*
* @return {@link com.datatorrent.stram.api.OperatorDeployInfo}
*/
private OperatorDeployInfo createOperatorDeployInfo(PTOperator oper) {
OperatorDeployInfo ndi;
if (oper.isUnifier()) {
UnifierDeployInfo udi = new UnifierDeployInfo();
/* the constructor auto sets the type */
try {
udi.operatorAttributes = oper.getUnifiedOperatorMeta().getAttributes().clone();
} catch (CloneNotSupportedException ex) {
throw new RuntimeException("Cannot clone unifier attributes", ex);
}
ndi = udi;
} else {
ndi = new OperatorDeployInfo();
Operator operator = oper.getOperatorMeta().getOperator();
if (operator instanceof InputOperator) {
ndi.type = OperatorType.INPUT;
if (!oper.getInputs().isEmpty()) {
//we check if any input port is connected which would make it a Generic operator.
for (PTOperator.PTInput ptInput : oper.getInputs()) {
if (ptInput.logicalStream != null && ptInput.logicalStream.getSource() != null) {
ndi.type = OperatorType.GENERIC;
break;
}
}
}
} else {
ndi.type = OperatorType.GENERIC;
}
}
Checkpoint checkpoint = oper.getRecoveryCheckpoint();
ProcessingMode pm = oper.getOperatorMeta().getValue(OperatorContext.PROCESSING_MODE);
if (pm == ProcessingMode.AT_MOST_ONCE || pm == ProcessingMode.EXACTLY_ONCE) {
// TODO: following should be handled in the container at deploy time
// for exactly once container should also purge previous checkpoint
// whenever new checkpoint is written.
StorageAgent agent = oper.getOperatorMeta().getAttributes().get(OperatorContext.STORAGE_AGENT);
if (agent == null) {
agent = initCtx.getValue(OperatorContext.STORAGE_AGENT);
}
// pick checkpoint most recently written
try {
long[] windowIds = agent.getWindowIds(oper.getId());
long checkpointId = Stateless.WINDOW_ID;
for (long windowId : windowIds) {
if (windowId > checkpointId) {
checkpointId = windowId;
}
}
if (checkpoint == null || checkpoint.windowId != checkpointId) {
checkpoint = new Checkpoint(checkpointId, 0, 0);
}
} catch (Exception e) {
throw new RuntimeException("Failed to determine checkpoint window id " + oper, e);
}
}
LOG.debug("{} recovery checkpoint {}", oper, checkpoint);
ndi.checkpoint = checkpoint;
ndi.name = oper.getOperatorMeta().getName();
ndi.id = oper.getId();
try {
// clone map before modifying it
ndi.contextAttributes = oper.getOperatorMeta().getAttributes().clone();
} catch (CloneNotSupportedException ex) {
throw new RuntimeException("Cannot clone operator attributes", ex);
}
if (oper.isOperatorStateLess()) {
ndi.contextAttributes.put(OperatorContext.STATELESS, true);
}
return ndi;
}
use of com.datatorrent.api.Operator in project apex-core by apache.
the class PhysicalPlanTest method testDefaultRepartitioning.
@Test
public void testDefaultRepartitioning() {
List<PartitionKeys> twoBitPartitionKeys = Arrays.asList(newPartitionKeys("11", "00"), newPartitionKeys("11", "10"), newPartitionKeys("11", "01"), newPartitionKeys("11", "11"));
GenericTestOperator operator = new GenericTestOperator();
Set<PartitionKeys> initialPartitionKeys = Sets.newHashSet(newPartitionKeys("1", "0"), newPartitionKeys("1", "1"));
final ArrayList<Partition<Operator>> partitions = new ArrayList<>();
for (PartitionKeys pks : initialPartitionKeys) {
Map<InputPort<?>, PartitionKeys> p1Keys = new HashMap<>();
p1Keys.put(operator.inport1, pks);
partitions.add(new DefaultPartition<Operator>(operator, p1Keys, 1, null));
}
ArrayList<Partition<Operator>> lowLoadPartitions = new ArrayList<>();
for (Partition<Operator> p : partitions) {
lowLoadPartitions.add(new DefaultPartition<>(p.getPartitionedInstance(), p.getPartitionKeys(), -1, null));
}
// merge to single partition
List<Partition<Operator>> newPartitions = Lists.newArrayList();
Collection<Partition<Operator>> tempNewPartitions = StatelessPartitioner.repartition(lowLoadPartitions);
newPartitions.addAll(tempNewPartitions);
Assert.assertEquals("" + newPartitions, 1, newPartitions.size());
Assert.assertEquals("" + newPartitions.get(0).getPartitionKeys(), 0, newPartitions.get(0).getPartitionKeys().values().iterator().next().mask);
List<Partition<Operator>> tempList = Collections.singletonList((Partition<Operator>) new DefaultPartition<Operator>(operator, newPartitions.get(0).getPartitionKeys(), -1, null));
tempNewPartitions = StatelessPartitioner.repartition(tempList);
newPartitions.clear();
newPartitions.addAll(tempNewPartitions);
Assert.assertEquals("" + newPartitions, 1, newPartitions.size());
// split back into two
tempList = Collections.singletonList((Partition<Operator>) new DefaultPartition<Operator>(operator, newPartitions.get(0).getPartitionKeys(), 1, null));
tempNewPartitions = StatelessPartitioner.repartition(tempList);
newPartitions.clear();
newPartitions.addAll(tempNewPartitions);
Assert.assertEquals("" + newPartitions, 2, newPartitions.size());
// split partitions
tempNewPartitions = StatelessPartitioner.repartition(partitions);
newPartitions.clear();
newPartitions.addAll(tempNewPartitions);
Assert.assertEquals("" + newPartitions, 4, newPartitions.size());
Set<PartitionKeys> expectedPartitionKeys = Sets.newHashSet(twoBitPartitionKeys);
for (Partition<?> p : newPartitions) {
Assert.assertEquals("" + p.getPartitionKeys(), 1, p.getPartitionKeys().size());
Assert.assertEquals("" + p.getPartitionKeys(), operator.inport1, p.getPartitionKeys().keySet().iterator().next());
PartitionKeys pks = p.getPartitionKeys().values().iterator().next();
expectedPartitionKeys.remove(pks);
}
Assert.assertTrue("" + expectedPartitionKeys, expectedPartitionKeys.isEmpty());
// partition merge
List<HashSet<PartitionKeys>> expectedKeysSets = Arrays.asList(Sets.newHashSet(newPartitionKeys("11", "00"), newPartitionKeys("11", "10"), newPartitionKeys("1", "1")), Sets.newHashSet(newPartitionKeys("1", "0"), newPartitionKeys("11", "01"), newPartitionKeys("11", "11")));
for (Set<PartitionKeys> expectedKeys : expectedKeysSets) {
List<Partition<Operator>> clonePartitions = Lists.newArrayList();
for (PartitionKeys pks : twoBitPartitionKeys) {
Map<InputPort<?>, PartitionKeys> p1Keys = new HashMap<>();
p1Keys.put(operator.inport1, pks);
int load = expectedKeys.contains(pks) ? 0 : -1;
clonePartitions.add(new DefaultPartition<Operator>(operator, p1Keys, load, null));
}
tempNewPartitions = StatelessPartitioner.repartition(clonePartitions);
newPartitions.clear();
newPartitions.addAll(tempNewPartitions);
Assert.assertEquals("" + newPartitions, 3, newPartitions.size());
for (Partition<?> p : newPartitions) {
Assert.assertEquals("" + p.getPartitionKeys(), 1, p.getPartitionKeys().size());
Assert.assertEquals("" + p.getPartitionKeys(), operator.inport1, p.getPartitionKeys().keySet().iterator().next());
PartitionKeys pks = p.getPartitionKeys().values().iterator().next();
expectedKeys.remove(pks);
}
Assert.assertTrue("" + expectedKeys, expectedKeys.isEmpty());
}
// merge 2 into single partition
lowLoadPartitions = Lists.newArrayList();
for (Partition<?> p : partitions) {
lowLoadPartitions.add(new DefaultPartition<Operator>(operator, p.getPartitionKeys(), -1, null));
}
tempNewPartitions = StatelessPartitioner.repartition(lowLoadPartitions);
newPartitions.clear();
newPartitions.addAll(tempNewPartitions);
Assert.assertEquals("" + newPartitions, 1, newPartitions.size());
for (Partition<?> p : newPartitions) {
Assert.assertEquals("" + p.getPartitionKeys(), 1, p.getPartitionKeys().size());
PartitionKeys pks = p.getPartitionKeys().values().iterator().next();
Assert.assertEquals("" + pks, 0, pks.mask);
Assert.assertEquals("" + pks, Sets.newHashSet(0), pks.partitions);
}
}
use of com.datatorrent.api.Operator in project apex-core by apache.
the class LogicalPlanConfigurationTest method testOperatorConfigurationLookup.
@Test
public void testOperatorConfigurationLookup() {
Properties props = new Properties();
// match operator by name
props.put(StreamingApplication.APEX_PREFIX + "template.matchId1.matchIdRegExp", ".*operator1.*");
props.put(StreamingApplication.APEX_PREFIX + "template.matchId1.stringProperty2", "stringProperty2Value-matchId1");
props.put(StreamingApplication.APEX_PREFIX + "template.matchId1.nested.property", "nested.propertyValue-matchId1");
// match class name, lower priority
props.put(StreamingApplication.APEX_PREFIX + "template.matchClass1.matchClassNameRegExp", ".*" + ValidationTestOperator.class.getSimpleName());
props.put(StreamingApplication.APEX_PREFIX + "template.matchClass1.stringProperty2", "stringProperty2Value-matchClass1");
// match class name
props.put(StreamingApplication.APEX_PREFIX + "template.t2.matchClassNameRegExp", ".*" + GenericTestOperator.class.getSimpleName());
props.put(StreamingApplication.APEX_PREFIX + "template.t2.myStringProperty", "myStringPropertyValue");
// direct setting
props.put(StreamingApplication.APEX_PREFIX + "operator.operator3.emitFormat", "emitFormatValue");
LogicalPlan dag = new LogicalPlan();
Operator operator1 = dag.addOperator("operator1", new ValidationTestOperator());
Operator operator2 = dag.addOperator("operator2", new ValidationTestOperator());
Operator operator3 = dag.addOperator("operator3", new GenericTestOperator());
LogicalPlanConfiguration pb = new LogicalPlanConfiguration(new Configuration(false));
LOG.debug("calling addFromProperties");
pb.addFromProperties(props, null);
Map<String, String> configProps = pb.getProperties(dag.getMeta(operator1), "appName");
Assert.assertEquals("" + configProps, 2, configProps.size());
Assert.assertEquals("" + configProps, "stringProperty2Value-matchId1", configProps.get("stringProperty2"));
Assert.assertEquals("" + configProps, "nested.propertyValue-matchId1", configProps.get("nested.property"));
configProps = pb.getProperties(dag.getMeta(operator2), "appName");
Assert.assertEquals("" + configProps, 1, configProps.size());
Assert.assertEquals("" + configProps, "stringProperty2Value-matchClass1", configProps.get("stringProperty2"));
configProps = pb.getProperties(dag.getMeta(operator3), "appName");
Assert.assertEquals("" + configProps, 2, configProps.size());
Assert.assertEquals("" + configProps, "myStringPropertyValue", configProps.get("myStringProperty"));
Assert.assertEquals("" + configProps, "emitFormatValue", configProps.get("emitFormat"));
}
Aggregations