use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.
the class Topology method finalizeConfig.
private void finalizeConfig() {
JSONObject jsonConfig = builder().getConfig();
for (String key : config.keySet()) {
JSONObject cfg = getJSONConfig(jsonConfig, key);
addConfig(cfg, key, config.get(key));
}
}
use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.
the class Topology method checkpointPeriod.
/**
* Checkpoint the state of the graph periodically.
* Each stateful element in the topology
* checkpoints its state periodically according to
* {@code period} and {@code unit}. Every element persists
* its state autonomously, asynchronously with processing
* its streams.
* <BR>
* Upon a failure of an element's container the element
* will restart in a new container using its last
* checkpointed state. If no state is available, due
* to a failure before the first checkpoint, then the
* element reverts to its initial state.
* <P>
* For stream processing elements defined by Java functions
* (such as {@link #source(Supplier)} and
* {@link TStream#transform(Function)}) the state is the
* serialized form of the object representing the function.
* Synchronization is applied to ensure that checkpointed state of
* the object does not include inconsistencies due to ongoing stream processing.
* <BR>
* If the function object is immutable then no checkpointing occurs for that element.
* A function object is taken as mutable if any of these conditions are true:
* <UL>
* <LI>It contains at least one non-transient, non-final instance field.</LI>
* <LI>A final instance field is a reference to a mutable object.
* Note that identification of what is a immutable object may be limited and
* so in some cases function objects may be checkpointed even though
* they are immutable.
* </LI>
* </UL>
* Otherwise the function object is taken as immutable.
* </P>
* <P>
* Checkpointing is only supported in distributed contexts.
* </P>
*
* @param period Approximate period for checkpointing.
* @param unit Time unit of {@code period}.
*/
public void checkpointPeriod(long period, TimeUnit unit) {
JSONObject checkpoint = new JSONObject();
checkpoint.put("mode", "periodic");
checkpoint.put("period", period);
checkpoint.put("unit", unit.name());
builder().getConfig().put("checkpoint", checkpoint);
}
use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.
the class BInputPort method window.
public BInputPort window(StreamWindow.Type type, StreamWindow.Policy evictPolicy, Object evictConfig, TimeUnit evictTimeUnit, StreamWindow.Policy triggerPolicy, Object triggerConfig, TimeUnit triggerTimeUnit, boolean partitioned) {
switch(type) {
case NOT_WINDOWED:
return this;
case SLIDING:
port().sliding();
break;
case TUMBLING:
port().tumbling();
break;
}
final JSONObject winJson = new JSONObject();
winJson.put("type", type.name());
// Eviction
switch(evictPolicy) {
case COUNT:
port().evictCount(((Number) evictConfig).intValue());
break;
case TIME:
port().evictTime((Long) evictConfig, evictTimeUnit);
break;
default:
;
throw new UnsupportedOperationException(evictPolicy.name());
}
winJson.put("evictPolicy", evictPolicy.name());
winJson.put("evictConfig", evictConfig);
if (evictPolicy == StreamWindow.Policy.TIME)
winJson.put("evictTimeUnit", evictTimeUnit.name());
if (triggerPolicy != null && triggerPolicy != StreamWindow.Policy.NONE) {
switch(triggerPolicy) {
case COUNT:
port().triggerCount(((Number) triggerConfig).intValue());
break;
case TIME:
port().triggerTime((Long) triggerConfig, triggerTimeUnit);
break;
default:
;
throw new UnsupportedOperationException(evictPolicy.name());
}
winJson.put("triggerPolicy", triggerPolicy.name());
winJson.put("triggerConfig", triggerConfig);
if (triggerTimeUnit != null)
winJson.put("triggerTimeUnit", triggerTimeUnit.name());
}
if (partitioned) {
port().partitioned();
winJson.put("partitioned", partitioned);
}
json().put("window", winJson);
return this;
}
use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.
the class DependencyResolver method resolveDependencies.
/**
* Resolve the dependencies.
* Creates entries in the graph config that will
* result in files being copied into the toolkit.
*/
public void resolveDependencies() throws IOException, URISyntaxException {
JSONObject graphConfig = topology.builder().getConfig();
JSONArray includes = (JSONArray) graphConfig.get("includes");
if (includes == null)
graphConfig.put("includes", includes = new JSONArray());
for (BOperatorInvocation op : operatorToJarDependencies.keySet()) {
ArrayList<String> jars = new ArrayList<String>();
for (Path source : operatorToJarDependencies.get(op)) {
String jarName = resolveDependency(source, includes);
jars.add("impl/lib/" + jarName);
}
String[] jarPaths = jars.toArray(new String[jars.size()]);
op.setParameter("jar", jarPaths);
}
ArrayList<String> jars = new ArrayList<String>();
for (Path source : globalDependencies) {
String jarName = resolveDependency(source, includes);
jars.add("impl/lib/" + jarName);
}
List<BOperator> ops = topology.builder().getOps();
if (jars.size() != 0) {
for (BOperator op : ops) {
if (op instanceof BOperatorInvocation) {
BOperatorInvocation bop = (BOperatorInvocation) op;
if (Functional.class.isAssignableFrom(bop.op().getOperatorClass())) {
JSONObject params = (JSONObject) bop.json().get("parameters");
JSONObject op_jars = (JSONObject) params.get("jar");
if (null == op_jars) {
JSONObject val = new JSONObject();
val.put("value", new JSONArray());
params.put("jar", val);
op_jars = val;
}
JSONArray value = (JSONArray) op_jars.get("value");
for (String jar : jars) {
value.add(jar);
}
}
}
}
}
for (Artifact dep : globalFileDependencies) resolveFileDependency(dep, includes);
}
use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.
the class DependencyResolver method resolveDependency.
private String resolveDependency(Path source, JSONArray includes) {
String jarName;
if (!previouslyCopiedDependencies.containsKey(source)) {
File sourceFile = source.toFile();
JSONObject include = new JSONObject();
// If it's a file, we assume its a jar file.
if (sourceFile.isFile()) {
jarName = source.getFileName().toString();
include.put("source", source.toAbsolutePath().toString());
include.put("target", "impl/lib");
} else if (sourceFile.isDirectory()) {
// Create an entry that will convert the classes dir into a jar file
jarName = "classes" + previouslyCopiedDependencies.size() + "_" + sourceFile.getName() + ".jar";
include.put("classes", source.toAbsolutePath().toString());
include.put("name", jarName);
include.put("target", "impl/lib");
} else {
throw new IllegalArgumentException("Path not a file or directory:" + source);
}
includes.add(include);
previouslyCopiedDependencies.put(source, jarName);
} else {
jarName = previouslyCopiedDependencies.get(source);
}
// Sanity check
if (null == jarName) {
throw new IllegalStateException("Error resolving dependency " + source);
}
return jarName;
}
Aggregations