Search in sources :

Example 6 with ApexPythonInterpreterException

use of org.apache.apex.malhar.python.base.ApexPythonInterpreterException in project apex-malhar by apache.

the class JepPythonEngine method executeScript.

/**
 *   See {@link ApexPythonEngine#executeScript(WorkerExecutionMode, long, long, PythonInterpreterRequest)} for more
 *  details. Note that if the worker execution mode {@link WorkerExecutionMode} is BROADCAST, then the time SLA
 *  set is the total time for all workers i.e. each worker is given a ( total time / N ) where N is the current
 *   number of worker threads
 * @param executionMode  If the method call needs to be invoked on all workers or any single worker
 * @param windowId used to select the worker from the worker pool.Can be any long if an operator is not using this.
 * @param requestId used to select the worker from the worker pool. Can be any long if an operator is not using this.
 * @param req Represents the request to be processed.
 * @return
 * @throws ApexPythonInterpreterException
 */
@Override
public Map<String, PythonRequestResponse<Void>> executeScript(WorkerExecutionMode executionMode, long windowId, long requestId, PythonInterpreterRequest<Void> req) throws ApexPythonInterpreterException {
    checkNotNullConditions(req);
    checkNotNull(req.getScriptExecutionRequestPayload(), "Script execution info not set");
    checkNotNull(req.getScriptExecutionRequestPayload().getScriptName(), "Script name not set");
    Map<String, PythonRequestResponse<Void>> returnStatus = new HashMap<>();
    PythonRequestResponse lastSuccessfullySubmittedRequest = null;
    try {
        if (executionMode.equals(WorkerExecutionMode.BROADCAST)) {
            long timeOutPerWorker = TimeUnit.NANOSECONDS.convert(req.getTimeout(), req.getTimeUnit()) / numWorkerThreads;
            if (timeOutPerWorker == 0) {
                timeOutPerWorker = 1;
            }
            req.setTimeout(timeOutPerWorker);
            req.setTimeUnit(TimeUnit.NANOSECONDS);
            for (InterpreterWrapper wrapper : workers) {
                lastSuccessfullySubmittedRequest = wrapper.executeScript(windowId, requestId, req);
                if (lastSuccessfullySubmittedRequest != null) {
                    returnStatus.put(wrapper.getInterpreterId(), lastSuccessfullySubmittedRequest);
                }
            }
            if (returnStatus.size() > 0) {
                commandHistory.add(lastSuccessfullySubmittedRequest);
            }
        } else {
            InterpreterWrapper currentThread = null;
            if (executionMode.equals(WorkerExecutionMode.ANY)) {
                currentThread = selectWorkerForCurrentCall(requestId);
            }
            if (executionMode.equals(WorkerExecutionMode.STICKY)) {
                currentThread = workers.get(req.hashCode() % numWorkerThreads);
                LOG.debug(" Choosing sticky worker " + currentThread.getInterpreterId());
            }
            if (currentThread != null) {
                lastSuccessfullySubmittedRequest = currentThread.executeScript(windowId, requestId, req);
                if (lastSuccessfullySubmittedRequest != null) {
                    returnStatus.put(currentThread.getInterpreterId(), lastSuccessfullySubmittedRequest);
                }
            } else {
                throw new ApexPythonInterpreterException("No free interpreter threads available." + " Consider increasing workers and relaunch");
            }
        }
    } catch (InterruptedException e) {
        throw new ApexPythonInterpreterException(e);
    }
    return returnStatus;
}
Also used : HashMap(java.util.HashMap) PythonRequestResponse(org.apache.apex.malhar.python.base.requestresponse.PythonRequestResponse) ApexPythonInterpreterException(org.apache.apex.malhar.python.base.ApexPythonInterpreterException)

Example 7 with ApexPythonInterpreterException

use of org.apache.apex.malhar.python.base.ApexPythonInterpreterException in project apex-malhar by apache.

the class JepPythonEngine method eval.

/**
 *  See {@link ApexPythonEngine#eval(WorkerExecutionMode, long, long, PythonInterpreterRequest)} for more
 *  details. Note that if the worker execution mode {@link WorkerExecutionMode} is BROADCAST, then the time SLA
 *  set is the total time for all workers i.e. each worker is given a ( total time / N ) where N is the current
 *   number of worker threads
 * @param executionMode If the method call needs to be invoked on all workers or any single worker
 * @param windowId used to select the worker from the worker pool.Can be any long if an operator is not using this.
 * @param requestId used to select the worker from the worker pool. Can be any long if an operator is not using this.
 * @param request
 * @param <T>
 * @return
 * @throws ApexPythonInterpreterException
 */
@Override
public <T> Map<String, PythonRequestResponse<T>> eval(WorkerExecutionMode executionMode, long windowId, long requestId, PythonInterpreterRequest<T> request) throws ApexPythonInterpreterException {
    checkNotNullConditions(request);
    checkNotNull(request.getEvalCommandRequestPayload(), "Eval command info not set");
    checkNotNull(request.getEvalCommandRequestPayload().getEvalCommand(), "Eval command not set");
    Map<String, PythonRequestResponse<T>> statusOfEval = new HashMap<>();
    PythonRequestResponse lastSuccessfullySubmittedRequest = null;
    try {
        if (executionMode.equals(WorkerExecutionMode.BROADCAST)) {
            long timeOutPerWorker = TimeUnit.NANOSECONDS.convert(request.getTimeout(), request.getTimeUnit()) / numWorkerThreads;
            if (timeOutPerWorker == 0) {
                timeOutPerWorker = 1;
            }
            request.setTimeout(timeOutPerWorker);
            request.setTimeUnit(TimeUnit.NANOSECONDS);
            for (InterpreterWrapper wrapper : workers) {
                lastSuccessfullySubmittedRequest = wrapper.eval(windowId, requestId, request);
                if (lastSuccessfullySubmittedRequest != null) {
                    statusOfEval.put(wrapper.getInterpreterId(), lastSuccessfullySubmittedRequest);
                }
            }
            commandHistory.add(lastSuccessfullySubmittedRequest);
        } else {
            InterpreterWrapper currentThread = null;
            if (executionMode.equals(WorkerExecutionMode.ANY)) {
                currentThread = selectWorkerForCurrentCall(requestId);
            }
            if (executionMode.equals(WorkerExecutionMode.STICKY)) {
                currentThread = workers.get(request.hashCode() % numWorkerThreads);
                LOG.debug(" Choosing sticky worker " + currentThread.getInterpreterId());
            }
            if (currentThread != null) {
                lastSuccessfullySubmittedRequest = currentThread.eval(windowId, requestId, request);
                if (lastSuccessfullySubmittedRequest != null) {
                    statusOfEval.put(currentThread.getInterpreterId(), lastSuccessfullySubmittedRequest);
                }
            } else {
                throw new ApexPythonInterpreterException("No free interpreter threads available." + " Consider increasing workers and relaunch");
            }
        }
    } catch (InterruptedException e) {
        throw new ApexPythonInterpreterException(e);
    }
    return statusOfEval;
}
Also used : HashMap(java.util.HashMap) PythonRequestResponse(org.apache.apex.malhar.python.base.requestresponse.PythonRequestResponse) ApexPythonInterpreterException(org.apache.apex.malhar.python.base.ApexPythonInterpreterException)

Example 8 with ApexPythonInterpreterException

use of org.apache.apex.malhar.python.base.ApexPythonInterpreterException in project apex-malhar by apache.

the class JepPythonEngine method runCommands.

/**
 * See {@link ApexPythonEngine#runCommands(WorkerExecutionMode, long, long, PythonInterpreterRequest)} for more
 *  details. Note that if the worker execution mode {@link WorkerExecutionMode} is BROADCAST, then the time SLA
 *  set is the total time for all workers i.e. each worker is given a ( total time / N ) where N is the current
 *   number of worker threads
 * @param executionMode Whether these commands need to be run on all worker nodes or any of the worker node
 * @param windowId used to select the worker from the worker pool.Can be any long if an operator is not using this.
 * @param requestId used to select the worker from the worker pool. Can be any long if an operator is not using this.
 * @param request Represents the request to be processed.
 * @return A map containing the command as key and boolean representing success or failure as the value.
 * @throws ApexPythonInterpreterException
 */
@Override
public Map<String, PythonRequestResponse<Void>> runCommands(WorkerExecutionMode executionMode, long windowId, long requestId, PythonInterpreterRequest<Void> request) throws ApexPythonInterpreterException {
    checkNotNullConditions(request);
    checkNotNull(request.getGenericCommandsRequestPayload(), "Run commands payload not set");
    checkNotNull(request.getGenericCommandsRequestPayload().getGenericCommands(), "Commands that need to be run not set");
    Map<String, PythonRequestResponse<Void>> returnStatus = new HashMap<>();
    PythonRequestResponse lastSuccessfullySubmittedRequest = null;
    try {
        if (executionMode.equals(WorkerExecutionMode.BROADCAST)) {
            LOG.debug("Executing run commands on all of the interpreter worker threads");
            long timeOutPerWorker = TimeUnit.NANOSECONDS.convert(request.getTimeout(), request.getTimeUnit()) / numWorkerThreads;
            LOG.debug("Allocating " + timeOutPerWorker + " nanoseconds for each of the worker threads");
            if (timeOutPerWorker == 0) {
                timeOutPerWorker = 1;
            }
            request.setTimeout(timeOutPerWorker);
            request.setTimeUnit(TimeUnit.NANOSECONDS);
            for (InterpreterWrapper wrapper : workers) {
                lastSuccessfullySubmittedRequest = wrapper.runCommands(windowId, requestId, request);
                if (lastSuccessfullySubmittedRequest != null) {
                    returnStatus.put(wrapper.getInterpreterId(), lastSuccessfullySubmittedRequest);
                }
            }
            if (returnStatus.size() > 0) {
                commandHistory.add(lastSuccessfullySubmittedRequest);
            }
        } else {
            InterpreterWrapper currentThread = null;
            if (executionMode.equals(WorkerExecutionMode.ANY)) {
                LOG.debug("Executing run commands on a single interpreter worker thread");
                currentThread = selectWorkerForCurrentCall(requestId);
            }
            if (executionMode.equals(WorkerExecutionMode.STICKY)) {
                currentThread = workers.get(request.hashCode() % numWorkerThreads);
                LOG.debug(" Choosing sticky worker " + currentThread.getInterpreterId());
            }
            if (currentThread != null) {
                lastSuccessfullySubmittedRequest = currentThread.runCommands(windowId, requestId, request);
                if (lastSuccessfullySubmittedRequest != null) {
                    returnStatus.put(currentThread.getInterpreterId(), lastSuccessfullySubmittedRequest);
                }
            } else {
                throw new ApexPythonInterpreterException("No free interpreter threads available." + " Consider increasing workers and relaunch");
            }
        }
    } catch (InterruptedException e) {
        throw new ApexPythonInterpreterException(e);
    }
    return returnStatus;
}
Also used : HashMap(java.util.HashMap) PythonRequestResponse(org.apache.apex.malhar.python.base.requestresponse.PythonRequestResponse) ApexPythonInterpreterException(org.apache.apex.malhar.python.base.ApexPythonInterpreterException)

Example 9 with ApexPythonInterpreterException

use of org.apache.apex.malhar.python.base.ApexPythonInterpreterException in project apex-malhar by apache.

the class BaseJEPTest method setCommonConstructsForRequestResponseObject.

private void setCommonConstructsForRequestResponseObject(PythonCommandType commandType, PythonInterpreterRequest request, PythonRequestResponse requestResponse) throws ApexPythonInterpreterException {
    request.setCommandType(commandType);
    requestResponse.setRequestStartTime(System.currentTimeMillis());
    requestResponse.setRequestId(1L);
    requestResponse.setWindowId(1L);
    switch(commandType) {
        case EVAL_COMMAND:
            EvalCommandRequestPayload payload = new EvalCommandRequestPayload();
            request.setEvalCommandRequestPayload(payload);
            break;
        case METHOD_INVOCATION_COMMAND:
            MethodCallRequestPayload methodCallRequest = new MethodCallRequestPayload();
            request.setMethodCallRequest(methodCallRequest);
            break;
        case SCRIPT_COMMAND:
            ScriptExecutionRequestPayload scriptPayload = new ScriptExecutionRequestPayload();
            request.setScriptExecutionRequestPayload(scriptPayload);
            break;
        case GENERIC_COMMANDS:
            GenericCommandsRequestPayload payloadForGenericCommands = new GenericCommandsRequestPayload();
            request.setGenericCommandsRequestPayload(payloadForGenericCommands);
            break;
        default:
            throw new ApexPythonInterpreterException("Unsupported command type");
    }
}
Also used : EvalCommandRequestPayload(org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload) GenericCommandsRequestPayload(org.apache.apex.malhar.python.base.requestresponse.GenericCommandsRequestPayload) ScriptExecutionRequestPayload(org.apache.apex.malhar.python.base.requestresponse.ScriptExecutionRequestPayload) ApexPythonInterpreterException(org.apache.apex.malhar.python.base.ApexPythonInterpreterException) MethodCallRequestPayload(org.apache.apex.malhar.python.base.requestresponse.MethodCallRequestPayload)

Aggregations

ApexPythonInterpreterException (org.apache.apex.malhar.python.base.ApexPythonInterpreterException)9 HashMap (java.util.HashMap)5 PythonRequestResponse (org.apache.apex.malhar.python.base.requestresponse.PythonRequestResponse)5 JepException (jep.JepException)4 EvalCommandRequestPayload (org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload)2 MethodCallRequestPayload (org.apache.apex.malhar.python.base.requestresponse.MethodCallRequestPayload)2 ScriptExecutionRequestPayload (org.apache.apex.malhar.python.base.requestresponse.ScriptExecutionRequestPayload)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 Jep (jep.Jep)1 JepConfig (jep.JepConfig)1 GenericCommandsRequestPayload (org.apache.apex.malhar.python.base.requestresponse.GenericCommandsRequestPayload)1