use of org.knime.core.node.workflow.NodeContext in project knime-core by knime.
the class SandboxedNodeCreator method createSandbox.
/**
* Creates that temporary mini workflow that is executed remotely on the cluster/stream executor.
* The returned value should be {@link SandboxedNode#close()} when done (using try-with-resources). After this
* method is called no other set-method should be called.
*
* @param exec for progress/cancelation
* @return the index of the node that represents this node (the node to execute) in the temporary mini workflow
* @throws InvalidSettingsException
* @throws IOException
* @throws CanceledExecutionException
* @throws LockFailedException
* @throws InterruptedException
*/
public SandboxedNode createSandbox(final ExecutionMonitor exec) throws InvalidSettingsException, IOException, CanceledExecutionException, LockFailedException, InterruptedException {
exec.setMessage("Creating virtual workflow");
final WorkflowManager parent = m_nc.getParent();
// derive workflow context via NodeContext as the parent could only a be a metanode in a metanode...
final WorkflowContext origContext = NodeContext.getContext().getWorkflowManager().getContext();
WorkflowContext.Factory ctxFactory;
// (specifically reading knime://knime.workflow files)
if (!m_copyDataIntoNewContext) {
ctxFactory = new WorkflowContext.Factory(origContext);
if (m_localWorkflowDir != null) {
ctxFactory.setOriginalLocation(origContext.getCurrentLocation()).setCurrentLocation(m_localWorkflowDir);
}
} else if (m_localWorkflowDir != null) {
ctxFactory = new WorkflowContext.Factory(m_localWorkflowDir);
} else {
ctxFactory = new WorkflowContext.Factory(FileUtil.createTempDir("sandbox-" + m_nc.getNameWithID()));
}
// We have to use the same location for the temporary files
ctxFactory.setTempLocation(origContext.getTempLocation());
origContext.getMountpointURI().ifPresent(u -> ctxFactory.setMountpointURI(u));
WorkflowCreationHelper creationHelper = new WorkflowCreationHelper();
creationHelper.setWorkflowContext(ctxFactory.createContext());
if (!m_copyDataIntoNewContext) {
creationHelper.setDataHandlers(parent.getGlobalTableRepository(), parent.getFileStoreHandlerRepository());
}
WorkflowManager tempWFM = m_rootWFM.createAndAddProject("Sandbox Exec on " + m_nc.getNameWithID(), creationHelper);
// Add the workflow variables
List<FlowVariable> workflowVariables = parent.getProjectWFM().getWorkflowVariables();
tempWFM.addWorkflowVariables(true, workflowVariables.toArray(new FlowVariable[workflowVariables.size()]));
// update credentials store of the workflow
CredentialsStore cs = tempWFM.getCredentialsStore();
workflowVariables.stream().filter(f -> f.getType().equals(FlowVariable.Type.CREDENTIALS)).filter(f -> !cs.contains(f.getName())).forEach(cs::addFromFlowVariable);
final int inCnt = m_inData.length;
// port object IDs in static port object map, one entry for
// each connected input (no value for unconnected optional inputs)
List<Integer> portObjectRepositoryIDs = new ArrayList<Integer>(inCnt);
try {
NodeID[] ins = new NodeID[inCnt];
for (int i = 0; i < inCnt; i++) {
final PortObject in = m_inData[i];
final NodeInPort inPort = m_nc.getInPort(i);
final PortType portType = inPort.getPortType();
if (in == null) {
// unconnected optional input
CheckUtils.checkState(portType.isOptional(), "No data at port %d, although port is mandatory (port type %s)", i, portType.getName());
continue;
}
int portObjectRepositoryID = PortObjectRepository.add(in);
portObjectRepositoryIDs.add(portObjectRepositoryID);
boolean isTable = BufferedDataTable.TYPE.equals(portType);
NodeID inID = tempWFM.createAndAddNode(isTable ? TABLE_READ_NODE_FACTORY : OBJECT_READ_NODE_FACTORY);
NodeSettings s = new NodeSettings("temp_data_in");
tempWFM.saveNodeSettings(inID, s);
List<FlowVariable> flowVars = getFlowVariablesOnPort(i);
PortObjectInNodeModel.setInputNodeSettings(s, portObjectRepositoryID, flowVars, m_copyDataIntoNewContext);
// update credentials store of the workflow
flowVars.stream().filter(f -> f.getType().equals(FlowVariable.Type.CREDENTIALS)).filter(f -> !cs.contains(f.getName())).forEach(cs::addFromFlowVariable);
tempWFM.loadNodeSettings(inID, s);
ins[i] = inID;
}
// execute inPort object nodes to store the input data in them
if (ins.length > 0 && !tempWFM.executeAllAndWaitUntilDoneInterruptibly()) {
String error = "Unable to execute virtual workflow, status sent to log facilities";
LOGGER.debug(error + ":");
LOGGER.debug(tempWFM.toString());
throw new RuntimeException(error);
}
// add the target node to the workflow
WorkflowCopyContent.Builder content = WorkflowCopyContent.builder();
content.setNodeIDs(m_nc.getID());
final NodeID targetNodeID = tempWFM.copyFromAndPasteHere(parent, content.build()).getNodeIDs()[0];
NodeContainer targetNode = tempWFM.getNodeContainer(targetNodeID);
// connect target node to inPort object nodes, skipping unconnected (optional) inputs
IntStream.range(0, inCnt).filter(i -> ins[i] != null).forEach(i -> tempWFM.addConnection(ins[i], 1, targetNodeID, i));
if (m_forwardConnectionProgressEvents) {
setupConnectionProgressEventListeners(m_nc, targetNode);
}
// copy the existing tables into the (meta) node (e.g. an executed file reader that's necessary
// for other nodes to execute)
exec.setMessage("Copying tables into temp flow");
NodeContainerExecutionResult origResult = m_nc.createExecutionResult(exec);
ExecutionMonitor copyExec = exec.createSubProgress(0.0);
copyExistingTablesIntoSandboxContainer(origResult, m_nc, targetNode, copyExec, m_copyDataIntoNewContext);
CopyContentIntoTempFlowNodeExecutionJobManager copyDataIntoTmpFlow = new CopyContentIntoTempFlowNodeExecutionJobManager(origResult);
NodeExecutionJobManager oldJobManager = targetNode.getJobManager();
tempWFM.setJobManager(targetNodeID, copyDataIntoTmpFlow);
tempWFM.executeAllAndWaitUntilDoneInterruptibly();
tempWFM.setJobManager(targetNodeID, oldJobManager);
// do not use the cluster executor on the cluster...
tempWFM.setJobManager(targetNodeID, NodeExecutionJobManagerPool.getDefaultJobManagerFactory().getInstance());
if (!m_copyDataIntoNewContext) {
copyFileStoreHandlerReference(targetNode, parent, false);
}
// save workflow in the local job dir
if (m_localWorkflowDir != null) {
tempWFM.save(m_localWorkflowDir, exec, true);
deepCopyFilesInWorkflowDir(m_nc, tempWFM);
}
return new SandboxedNode(tempWFM, targetNodeID);
} finally {
portObjectRepositoryIDs.stream().forEach(PortObjectRepository::remove);
}
}
use of org.knime.core.node.workflow.NodeContext in project knime-core by knime.
the class CreateTempDirectoryNodeModel method computeFileName.
private File computeFileName(final String id) {
File rootDir = null;
// get the flow's tmp dir from its context
NodeContext nodeContext = NodeContext.getContext();
if (nodeContext != null) {
WorkflowContext workflowContext = nodeContext.getWorkflowManager().getContext();
if (workflowContext != null) {
rootDir = workflowContext.getTempLocation();
}
}
if (rootDir == null) {
// use the standard tmp dir then.
rootDir = new File(KNIMEConstants.getKNIMETempDir());
}
String baseName = m_configuration.getBaseName();
return new File(rootDir, baseName + id);
}
use of org.knime.core.node.workflow.NodeContext in project knime-core by knime.
the class DatabasePortObject method getViews.
/**
* {@inheritDoc}
*/
@Override
public JComponent[] getViews() {
JComponent[] superViews = super.getViews();
final JComponent[] panels = new JComponent[superViews.length + 1];
@SuppressWarnings("serial") final BufferedDataTableView dataView = new BufferedDataTableView(null) {
@Override
public String getName() {
return "Table Preview";
}
};
final JButton b = new JButton("Cache no. of rows: ");
final JPanel p = new JPanel(new FlowLayout());
final JTextField cacheRows = new JTextField("100");
cacheRows.setMinimumSize(new Dimension(50, 20));
cacheRows.setPreferredSize(new Dimension(50, 20));
p.add(b);
p.add(cacheRows);
panels[0] = new DatabaseOutPortPanel(new BorderLayout());
panels[0].setName(dataView.getName());
panels[0].add(p, BorderLayout.NORTH);
panels[0].add(dataView, BorderLayout.CENTER);
// store the NodeContext to explicitly set the NodeContext when the fetch rows button is pressed
final NodeContext nodeContext = NodeContext.getContext();
b.addActionListener(new ActionListener() {
/**
* {@inheritDoc}
*/
@Override
public void actionPerformed(final ActionEvent e) {
// explicitly set the NodeContext to get the current workflow user that is used in Kerberos secured
// db connection
NodeContext.pushContext(nodeContext);
try {
loadTablePreview(panels, p, cacheRows);
} finally {
// remove the previously set NodeContext
NodeContext.removeLastContext();
}
}
});
for (int i = 1; i < panels.length; i++) {
panels[i] = superViews[i - 1];
}
return panels;
}
use of org.knime.core.node.workflow.NodeContext in project knime-core by knime.
the class NodeLogger method getLogObject.
/**
* @param message the logging message
* @return a KNIMELogMessage that not only contains the log message but also the information about the workflow
* and node that belong to the log message if applicable
*/
private Object getLogObject(final Object message) {
if (!LOG_NODE_ID && !LOG_IN_WF_DIR && !LOG_WF_DIR) {
return message;
}
final NodeContext context = NodeContext.getContext();
NodeID nodeID = null;
String nodeName = null;
File workflowDir = null;
if (context != null) {
if (LOG_NODE_ID) {
// retrieve and store the node id only if the user has requested to log it
final NodeContainer nodeContainer = context.getNodeContainer();
if (nodeContainer != null) {
nodeID = nodeContainer.getID();
nodeName = nodeContainer.getName();
}
}
if (LOG_IN_WF_DIR || LOG_WF_DIR) {
final WorkflowManager workflowManager = context.getWorkflowManager();
if (workflowManager != null) {
final WorkflowContext workflowContext = workflowManager.getContext();
if (workflowContext != null) {
workflowDir = workflowContext.getCurrentLocation();
}
}
}
}
return new KNIMELogMessage(nodeID, nodeName, workflowDir, message);
}
use of org.knime.core.node.workflow.NodeContext in project knime-core by knime.
the class FileUtil method getWorkflowTempDir.
/**
* Reads the temporary directory associated with the current workflow (set in the {@link WorkflowContext}), or - if
* that is <code>null</code> - the global temp dir (see {@link KNIMEConstants#getKNIMETempPath()}).
*
* @return the temp dir of the current {@link WorkflowContext} or the standard temp dir, if no context is set.
* @since 3.5
*/
public static File getWorkflowTempDir() {
final File fallbackDir = KNIMEConstants.getKNIMETempPath().toFile();
NodeContext nodeContext = NodeContext.getContext();
if (nodeContext == null) {
return fallbackDir;
}
WorkflowManager wfm = nodeContext.getWorkflowManager();
if (wfm == null) {
return fallbackDir;
}
WorkflowContext workflowContext = wfm.getContext();
if (workflowContext == null) {
return fallbackDir;
}
if (!workflowContext.getTempLocation().isDirectory()) {
LOGGER.error("Temp folder \"" + workflowContext.getTempLocation().getAbsolutePath() + "\" does not exist (associated with node context \"" + nodeContext + "\") - using fallback temp folder (\"" + fallbackDir.getAbsolutePath() + "\"");
return fallbackDir;
} else {
return workflowContext.getTempLocation();
}
}
Aggregations