Search in sources :

Example 36 with StepMetaDataCombi

use of org.pentaho.di.trans.step.StepMetaDataCombi in project pentaho-kettle by pentaho.

the class Trans method killAllNoWait.

/**
 * Asks all steps to stop but doesn't wait around for it to happen. This is a special method for use with mappings.
 */
private void killAllNoWait() {
    if (steps == null) {
        return;
    }
    for (int i = 0; i < steps.size(); i++) {
        StepMetaDataCombi sid = steps.get(i);
        StepInterface step = sid.step;
        if (log.isDebug()) {
            log.logDebug(BaseMessages.getString(PKG, "Trans.Log.LookingAtStep") + step.getStepname());
        }
        step.stopAll();
        try {
            Thread.sleep(20);
        } catch (Exception e) {
            log.logError(BaseMessages.getString(PKG, "Trans.Log.TransformationErrors") + e.toString());
            return;
        }
    }
}
Also used : StepInterface(org.pentaho.di.trans.step.StepInterface) StepMetaDataCombi(org.pentaho.di.trans.step.StepMetaDataCombi) KettleExtensionPoint(org.pentaho.di.core.extension.KettleExtensionPoint) UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleTransException(org.pentaho.di.core.exception.KettleTransException) DuplicateParamException(org.pentaho.di.core.parameters.DuplicateParamException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException)

Example 37 with StepMetaDataCombi

use of org.pentaho.di.trans.step.StepMetaDataCombi in project pentaho-kettle by pentaho.

the class Trans method getTransStepExecutionStatusLookup.

/**
 * Checks the execution status of each step in the transformations.
 *
 * @return an array associated with the step list, indicating the status of that step.
 */
public StepExecutionStatus[] getTransStepExecutionStatusLookup() {
    if (steps == null) {
        return null;
    }
    // we need this snapshot for the TransGridDelegate refresh method to handle the
    // difference between a timed refresh and continual step status updates
    int totalSteps = steps.size();
    StepExecutionStatus[] tList = new StepExecutionStatus[totalSteps];
    for (int i = 0; i < totalSteps; i++) {
        StepMetaDataCombi sid = steps.get(i);
        tList[i] = sid.step.getStatus();
    }
    return tList;
}
Also used : StepExecutionStatus(org.pentaho.di.trans.step.BaseStepData.StepExecutionStatus) StepMetaDataCombi(org.pentaho.di.trans.step.StepMetaDataCombi) KettleExtensionPoint(org.pentaho.di.core.extension.KettleExtensionPoint)

Example 38 with StepMetaDataCombi

use of org.pentaho.di.trans.step.StepMetaDataCombi in project pentaho-kettle by pentaho.

the class Trans method clearError.

/**
 * Clear the error in the transformation, clear all the rows from all the row sets, to make sure the transformation
 * can continue with other data. This is intended for use when running single threaded.
 */
public void clearError() {
    setStopped(false);
    errors.set(0);
    setFinished(false);
    for (StepMetaDataCombi combi : steps) {
        StepInterface step = combi.step;
        for (RowSet rowSet : step.getInputRowSets()) {
            rowSet.clear();
        }
        step.setStopped(false);
    }
}
Also used : StepInterface(org.pentaho.di.trans.step.StepInterface) StepMetaDataCombi(org.pentaho.di.trans.step.StepMetaDataCombi) QueueRowSet(org.pentaho.di.core.QueueRowSet) SingleRowRowSet(org.pentaho.di.core.SingleRowRowSet) RowSet(org.pentaho.di.core.RowSet) BlockingBatchingRowSet(org.pentaho.di.core.BlockingBatchingRowSet) BlockingRowSet(org.pentaho.di.core.BlockingRowSet)

Example 39 with StepMetaDataCombi

use of org.pentaho.di.trans.step.StepMetaDataCombi in project pentaho-kettle by pentaho.

the class Trans method startThreads.

/**
 * Starts the threads prepared by prepareThreads(). Before you start the threads, you can add RowListeners to them.
 *
 * @throws KettleException
 *           if there is a communication error with a remote output socket.
 */
public void startThreads() throws KettleException {
    // Now prepare to start all the threads...
    // 
    nrOfFinishedSteps = 0;
    nrOfActiveSteps = 0;
    ExtensionPointHandler.callExtensionPoint(log, KettleExtensionPoint.TransformationStartThreads.id, this);
    fireTransStartedListeners();
    for (int i = 0; i < steps.size(); i++) {
        final StepMetaDataCombi sid = steps.get(i);
        sid.step.markStart();
        sid.step.initBeforeStart();
        // also attach a Step Listener to detect when we're done...
        // 
        StepListener stepListener = new StepListener() {

            @Override
            public void stepActive(Trans trans, StepMeta stepMeta, StepInterface step) {
                nrOfActiveSteps++;
                if (nrOfActiveSteps == 1) {
                    // PDI-5229 sync added
                    synchronized (transListeners) {
                        for (TransListener listener : transListeners) {
                            listener.transActive(Trans.this);
                        }
                    }
                }
            }

            @Override
            public void stepFinished(Trans trans, StepMeta stepMeta, StepInterface step) {
                synchronized (Trans.this) {
                    nrOfFinishedSteps++;
                    if (nrOfFinishedSteps >= steps.size()) {
                        // Set the finished flag
                        // 
                        setFinished(true);
                        // Grab the performance statistics one last time (if enabled)
                        // 
                        addStepPerformanceSnapShot();
                        try {
                            fireTransFinishedListeners();
                        } catch (Exception e) {
                            step.setErrors(step.getErrors() + 1L);
                            log.logError(getName() + " : " + BaseMessages.getString(PKG, "Trans.Log.UnexpectedErrorAtTransformationEnd"), e);
                        }
                    }
                    // 
                    if (step.getErrors() > 0) {
                        log.logMinimal(BaseMessages.getString(PKG, "Trans.Log.TransformationDetectedErrors"));
                        log.logMinimal(BaseMessages.getString(PKG, "Trans.Log.TransformationIsKillingTheOtherSteps"));
                        killAllNoWait();
                    }
                }
            }
        };
        // 
        if (sid.step instanceof BaseStep) {
            ((BaseStep) sid.step).getStepListeners().add(0, stepListener);
        } else {
            sid.step.addStepListener(stepListener);
        }
    }
    if (transMeta.isCapturingStepPerformanceSnapShots()) {
        stepPerformanceSnapshotSeqNr = new AtomicInteger(0);
        stepPerformanceSnapShots = new ConcurrentHashMap<>();
        // Calculate the maximum number of snapshots to be kept in memory
        // 
        String limitString = environmentSubstitute(transMeta.getStepPerformanceCapturingSizeLimit());
        if (Utils.isEmpty(limitString)) {
            limitString = EnvUtil.getSystemProperty(Const.KETTLE_STEP_PERFORMANCE_SNAPSHOT_LIMIT);
        }
        stepPerformanceSnapshotSizeLimit = Const.toInt(limitString, 0);
        // Set a timer to collect the performance data from the running threads...
        // 
        stepPerformanceSnapShotTimer = new Timer("stepPerformanceSnapShot Timer: " + transMeta.getName());
        TimerTask timerTask = new TimerTask() {

            @Override
            public void run() {
                if (!isFinished()) {
                    addStepPerformanceSnapShot();
                }
            }
        };
        stepPerformanceSnapShotTimer.schedule(timerTask, 100, transMeta.getStepPerformanceCapturingDelay());
    }
    // Now start a thread to monitor the running transformation...
    // 
    setFinished(false);
    setPaused(false);
    setStopped(false);
    transFinishedBlockingQueue = new ArrayBlockingQueue<>(10);
    TransListener transListener = new TransAdapter() {

        @Override
        public void transFinished(Trans trans) {
            try {
                shutdownHeartbeat(trans != null ? trans.heartbeat : null);
                ExtensionPointHandler.callExtensionPoint(log, KettleExtensionPoint.TransformationFinish.id, trans);
            } catch (KettleException e) {
                throw new RuntimeException("Error calling extension point at end of transformation", e);
            }
            // 
            if (transMeta.isCapturingStepPerformanceSnapShots() && stepPerformanceSnapShotTimer != null) {
                stepPerformanceSnapShotTimer.cancel();
            }
            transMeta.disposeEmbeddedMetastoreProvider();
            setFinished(true);
            // no longer running
            setRunning(false);
            log.snap(Metrics.METRIC_TRANSFORMATION_EXECUTION_STOP);
            // If the user ran with metrics gathering enabled and a metrics logging table is configured, add another
            // listener...
            // 
            MetricsLogTable metricsLogTable = transMeta.getMetricsLogTable();
            if (metricsLogTable.isDefined()) {
                try {
                    writeMetricsInformation();
                } catch (Exception e) {
                    log.logError("Error writing metrics information", e);
                    errors.incrementAndGet();
                }
            }
            // 
            if (transMeta.isUsingUniqueConnections()) {
                trans.closeUniqueDatabaseConnections(getResult());
            }
            // release unused vfs connections
            KettleVFS.freeUnusedResources();
        }
    };
    // This should always be done first so that the other listeners achieve a clean state to start from (setFinished and
    // so on)
    // 
    transListeners.add(0, transListener);
    setRunning(true);
    switch(transMeta.getTransformationType()) {
        case Normal:
            // 
            for (int i = 0; i < steps.size(); i++) {
                final StepMetaDataCombi combi = steps.get(i);
                RunThread runThread = new RunThread(combi);
                Thread thread = new Thread(runThread);
                thread.setName(getName() + " - " + combi.stepname);
                ExtensionPointHandler.callExtensionPoint(log, KettleExtensionPoint.StepBeforeStart.id, combi);
                // Call an extension point at the end of the step
                // 
                combi.step.addStepListener(new StepAdapter() {

                    @Override
                    public void stepFinished(Trans trans, StepMeta stepMeta, StepInterface step) {
                        try {
                            ExtensionPointHandler.callExtensionPoint(log, KettleExtensionPoint.StepFinished.id, combi);
                        } catch (KettleException e) {
                            throw new RuntimeException("Unexpected error in calling extension point upon step finish", e);
                        }
                    }
                });
                thread.start();
            }
            break;
        case SerialSingleThreaded:
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        // 
                        for (StepMetaDataCombi combi : steps) {
                            combi.step.setUsingThreadPriorityManagment(false);
                        }
                        // 
                        // This is a single threaded version...
                        // 
                        // Sort the steps from start to finish...
                        // 
                        Collections.sort(steps, new Comparator<StepMetaDataCombi>() {

                            @Override
                            public int compare(StepMetaDataCombi c1, StepMetaDataCombi c2) {
                                boolean c1BeforeC2 = transMeta.findPrevious(c2.stepMeta, c1.stepMeta);
                                if (c1BeforeC2) {
                                    return -1;
                                } else {
                                    return 1;
                                }
                            }
                        });
                        boolean[] stepDone = new boolean[steps.size()];
                        int nrDone = 0;
                        while (nrDone < steps.size() && !isStopped()) {
                            for (int i = 0; i < steps.size() && !isStopped(); i++) {
                                StepMetaDataCombi combi = steps.get(i);
                                if (!stepDone[i]) {
                                    // if (combi.step.canProcessOneRow() ||
                                    // !combi.step.isRunning()) {
                                    boolean cont = combi.step.processRow(combi.meta, combi.data);
                                    if (!cont) {
                                        stepDone[i] = true;
                                        nrDone++;
                                    }
                                // }
                                }
                            }
                        }
                    } catch (Exception e) {
                        errors.addAndGet(1);
                        log.logError("Error executing single threaded", e);
                    } finally {
                        for (int i = 0; i < steps.size(); i++) {
                            StepMetaDataCombi combi = steps.get(i);
                            combi.step.dispose(combi.meta, combi.data);
                            combi.step.markStop();
                        }
                    }
                }
            }).start();
            break;
        case SingleThreaded:
            // 
            break;
        default:
            break;
    }
    ExtensionPointHandler.callExtensionPoint(log, KettleExtensionPoint.TransformationStart.id, this);
    heartbeat = startHeartbeat(getHeartbeatIntervalInSeconds());
    if (steps.isEmpty()) {
        fireTransFinishedListeners();
    }
    if (log.isDetailed()) {
        log.logDetailed(BaseMessages.getString(PKG, "Trans.Log.TransformationHasAllocated", String.valueOf(steps.size()), String.valueOf(rowsets.size())));
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) MetricsLogTable(org.pentaho.di.core.logging.MetricsLogTable) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) StepInterface(org.pentaho.di.trans.step.StepInterface) TimerTask(java.util.TimerTask) StepAdapter(org.pentaho.di.trans.step.StepAdapter) StepMetaDataCombi(org.pentaho.di.trans.step.StepMetaDataCombi) RunThread(org.pentaho.di.trans.step.RunThread) BaseStep(org.pentaho.di.trans.step.BaseStep) StepMeta(org.pentaho.di.trans.step.StepMeta) KettleExtensionPoint(org.pentaho.di.core.extension.KettleExtensionPoint) UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleTransException(org.pentaho.di.core.exception.KettleTransException) DuplicateParamException(org.pentaho.di.core.parameters.DuplicateParamException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) RunThread(org.pentaho.di.trans.step.RunThread) StepInitThread(org.pentaho.di.trans.step.StepInitThread) Timer(java.util.Timer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StepListener(org.pentaho.di.trans.step.StepListener)

Example 40 with StepMetaDataCombi

use of org.pentaho.di.trans.step.StepMetaDataCombi in project pentaho-kettle by pentaho.

the class Trans method getResult.

/**
 * Gets the result of the transformation. The Result object contains such measures as the number of errors, number of
 * lines read/written/input/output/updated/rejected, etc.
 *
 * @return the Result object containing resulting measures from execution of the transformation
 */
public Result getResult() {
    if (steps == null) {
        return null;
    }
    Result result = new Result();
    result.setNrErrors(errors.longValue());
    result.setResult(errors.longValue() == 0);
    TransLogTable transLogTable = transMeta.getTransLogTable();
    for (int i = 0; i < steps.size(); i++) {
        StepMetaDataCombi sid = steps.get(i);
        StepInterface step = sid.step;
        result.setNrErrors(result.getNrErrors() + sid.step.getErrors());
        result.getResultFiles().putAll(step.getResultFiles());
        if (step.isSafeStopped()) {
            result.setSafeStop(step.isSafeStopped());
        }
        if (step.getStepname().equals(transLogTable.getSubjectString(TransLogTable.ID.LINES_READ))) {
            result.setNrLinesRead(result.getNrLinesRead() + step.getLinesRead());
        }
        if (step.getStepname().equals(transLogTable.getSubjectString(TransLogTable.ID.LINES_INPUT))) {
            result.setNrLinesInput(result.getNrLinesInput() + step.getLinesInput());
        }
        if (step.getStepname().equals(transLogTable.getSubjectString(TransLogTable.ID.LINES_WRITTEN))) {
            result.setNrLinesWritten(result.getNrLinesWritten() + step.getLinesWritten());
        }
        if (step.getStepname().equals(transLogTable.getSubjectString(TransLogTable.ID.LINES_OUTPUT))) {
            result.setNrLinesOutput(result.getNrLinesOutput() + step.getLinesOutput());
        }
        if (step.getStepname().equals(transLogTable.getSubjectString(TransLogTable.ID.LINES_UPDATED))) {
            result.setNrLinesUpdated(result.getNrLinesUpdated() + step.getLinesUpdated());
        }
        if (step.getStepname().equals(transLogTable.getSubjectString(TransLogTable.ID.LINES_REJECTED))) {
            result.setNrLinesRejected(result.getNrLinesRejected() + step.getLinesRejected());
        }
    }
    result.setRows(resultRows);
    if (!Utils.isEmpty(resultFiles)) {
        result.setResultFiles(new HashMap<String, ResultFile>());
        for (ResultFile resultFile : resultFiles) {
            result.getResultFiles().put(resultFile.toString(), resultFile);
        }
    }
    result.setStopped(isStopped());
    result.setLogChannelId(log.getLogChannelId());
    return result;
}
Also used : StepInterface(org.pentaho.di.trans.step.StepInterface) StepMetaDataCombi(org.pentaho.di.trans.step.StepMetaDataCombi) TransLogTable(org.pentaho.di.core.logging.TransLogTable) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ResultFile(org.pentaho.di.core.ResultFile) KettleExtensionPoint(org.pentaho.di.core.extension.KettleExtensionPoint) WebResult(org.pentaho.di.www.WebResult) Result(org.pentaho.di.core.Result)

Aggregations

StepMetaDataCombi (org.pentaho.di.trans.step.StepMetaDataCombi)55 StepInterface (org.pentaho.di.trans.step.StepInterface)21 KettleExtensionPoint (org.pentaho.di.core.extension.KettleExtensionPoint)18 Test (org.junit.Test)13 KettleException (org.pentaho.di.core.exception.KettleException)10 ArrayList (java.util.ArrayList)8 StepMeta (org.pentaho.di.trans.step.StepMeta)8 Trans (org.pentaho.di.trans.Trans)7 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)6 KettleValueException (org.pentaho.di.core.exception.KettleValueException)6 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)6 StepMetaInterface (org.pentaho.di.trans.step.StepMetaInterface)6 RowSet (org.pentaho.di.core.RowSet)5 UnknownParamException (org.pentaho.di.core.parameters.UnknownParamException)5 TransMeta (org.pentaho.di.trans.TransMeta)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Before (org.junit.Before)4 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)4 KettleFileException (org.pentaho.di.core.exception.KettleFileException)4