Search in sources :

Example 1 with MethodCallRequestPayload

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

the class InterpreterThread method processCommand.

/**
 * Responsible for polling the request queue and formatting the request payload to make it compatible to the
 *  individual processing logic of the functionalities provided by the interpreter API methods.
 * @param <T> Java templating signature enforcement
 * @throws ApexPythonInterpreterException if an unrecognized command is issued.
 * @throws InterruptedException if interrupted while trying to wait for a request from request queue
 */
private <T> void processCommand() throws ApexPythonInterpreterException, InterruptedException {
    PythonRequestResponse requestResponseHandle = requestQueue.poll(timeOutToPollFromRequestQueue, timeUnitsToPollFromRequestQueue);
    if (requestResponseHandle != null) {
        LOG.debug("Processing command " + requestResponseHandle.getPythonInterpreterRequest().getCommandType());
        busyFlag = true;
        if (errorEncountered) {
            LOG.debug("Error state detected from a previous command. Resetting state to  the previous" + " state of the error");
            try {
                JEP_INSTANCE.eval(null);
                errorEncountered = false;
            } catch (JepException e) {
                LOG.error("Error while trying to clear the state of the interpreter due to previous command" + " " + e.getMessage(), e);
            }
        }
        PythonInterpreterRequest<T> request = requestResponseHandle.getPythonInterpreterRequest();
        PythonInterpreterResponse<T> response = requestResponseHandle.getPythonInterpreterResponse();
        Map<String, Boolean> commandStatus = new HashMap<>(1);
        switch(request.getCommandType()) {
            case EVAL_COMMAND:
                EvalCommandRequestPayload evalPayload = request.getEvalCommandRequestPayload();
                T responseVal = eval(evalPayload.getEvalCommand(), evalPayload.getVariableNameToExtractInEvalCall(), evalPayload.getParamsForEvalCommand(), evalPayload.isDeleteVariableAfterEvalCall(), request.getExpectedReturnType());
                response.setResponse(responseVal);
                if (responseVal != null) {
                    commandStatus.put(evalPayload.getEvalCommand(), Boolean.TRUE);
                } else {
                    commandStatus.put(evalPayload.getEvalCommand(), Boolean.FALSE);
                }
                response.setCommandStatus(commandStatus);
                break;
            case SCRIPT_COMMAND:
                ScriptExecutionRequestPayload scriptPayload = request.getScriptExecutionRequestPayload();
                if (executeScript(scriptPayload.getScriptName())) {
                    commandStatus.put(scriptPayload.getScriptName(), Boolean.TRUE);
                } else {
                    commandStatus.put(scriptPayload.getScriptName(), Boolean.FALSE);
                }
                response.setCommandStatus(commandStatus);
                break;
            case METHOD_INVOCATION_COMMAND:
                MethodCallRequestPayload requestpayload = request.getMethodCallRequest();
                response.setResponse(executeMethodCall(requestpayload.getNameOfMethod(), requestpayload.getArgs(), request.getExpectedReturnType()));
                if (response.getResponse() == null) {
                    commandStatus.put(requestpayload.getNameOfMethod(), Boolean.FALSE);
                } else {
                    commandStatus.put(requestpayload.getNameOfMethod(), Boolean.TRUE);
                }
                response.setCommandStatus(commandStatus);
                break;
            case GENERIC_COMMANDS:
                response.setCommandStatus(runCommands(request.getGenericCommandsRequestPayload().getGenericCommands()));
                break;
            default:
                throw new ApexPythonInterpreterException(new Exception("Unspecified Interpreter command"));
        }
        requestResponseHandle.setRequestCompletionTime(System.currentTimeMillis());
        responseQueue.put(requestResponseHandle);
        LOG.debug("Submitted the response and executed " + response.getCommandStatus().size() + " instances of command");
    }
    busyFlag = false;
}
Also used : EvalCommandRequestPayload(org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload) HashMap(java.util.HashMap) PythonRequestResponse(org.apache.apex.malhar.python.base.requestresponse.PythonRequestResponse) JepException(jep.JepException) JepException(jep.JepException) ApexPythonInterpreterException(org.apache.apex.malhar.python.base.ApexPythonInterpreterException) 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)

Example 2 with MethodCallRequestPayload

use of org.apache.apex.malhar.python.base.requestresponse.MethodCallRequestPayload 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)

Example 3 with MethodCallRequestPayload

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

the class PythonRequestResponseUtil method buildRequestForMethodCallCommand.

/**
 * Builds the request object for the Method call command. See
 * {@link ApexPythonEngine#executeMethodCall(WorkerExecutionMode, long, long, PythonInterpreterRequest)} for details
 * @param methodName Name of the method
 * @param methodParams parames to the method
 * @param timeOut Time allocated for completing the API
 * @param timeUnit The units of time
 * @param clazz The Class that represents the return type
 * @param <T> Java templating signature
 * @return The request object that can be used for method calls
 */
public static <T> PythonInterpreterRequest<T> buildRequestForMethodCallCommand(String methodName, List<Object> methodParams, long timeOut, TimeUnit timeUnit, Class<T> clazz) {
    PythonInterpreterRequest<T> request = new PythonInterpreterRequest<>(clazz);
    MethodCallRequestPayload methodCallRequestPayload = new MethodCallRequestPayload();
    methodCallRequestPayload.setNameOfMethod(methodName);
    methodCallRequestPayload.setArgs(methodParams);
    request.setTimeUnit(timeUnit);
    request.setTimeout(timeOut);
    request.setMethodCallRequest(methodCallRequestPayload);
    return request;
}
Also used : MethodCallRequestPayload(org.apache.apex.malhar.python.base.requestresponse.MethodCallRequestPayload) PythonInterpreterRequest(org.apache.apex.malhar.python.base.requestresponse.PythonInterpreterRequest)

Example 4 with MethodCallRequestPayload

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

the class InterpreterThreadTest method testMethodCall.

@JepPythonTestContext(jepPythonBasedTest = true)
@Test
public void testMethodCall() throws Exception {
    String methodName = "jepMultiply";
    List<String> commands = new ArrayList();
    commands.add("def " + methodName + "(firstnum, secondnum):\n" + // Note that this cannot be split as multiple commands
    "\treturn (firstnum * secondnum)\n");
    runCommands(commands);
    List<Object> params = new ArrayList<>();
    params.add(5L);
    params.add(25L);
    PythonRequestResponse<Long> methodCallRequest = buildRequestResponseObjectForLongPayload(PythonCommandType.METHOD_INVOCATION_COMMAND);
    MethodCallRequestPayload requestPayload = methodCallRequest.getPythonInterpreterRequest().getMethodCallRequest();
    requestPayload.setNameOfMethod(methodName);
    requestPayload.setArgs(params);
    methodCallRequest.getPythonInterpreterRequest().setExpectedReturnType(Long.class);
    pythonEngineThread.getRequestQueue().put(methodCallRequest);
    // wait for command to be processed
    Thread.sleep(1000);
    PythonRequestResponse<Long> methodCallResponse = pythonEngineThread.getResponseQueue().poll(1, TimeUnit.SECONDS);
    assertEquals(methodCallResponse.getPythonInterpreterResponse().getResponse(), 125L);
    Map<String, Boolean> commandStatus = methodCallResponse.getPythonInterpreterResponse().getCommandStatus();
    assertTrue(commandStatus.get(methodName));
    params.remove(1);
    methodCallRequest = buildRequestResponseObjectForLongPayload(PythonCommandType.METHOD_INVOCATION_COMMAND);
    requestPayload = methodCallRequest.getPythonInterpreterRequest().getMethodCallRequest();
    requestPayload.setNameOfMethod(methodName);
    requestPayload.setArgs(params);
    methodCallRequest.getPythonInterpreterRequest().setExpectedReturnType(Long.class);
    pythonEngineThread.getRequestQueue().put(methodCallRequest);
    // wait for command to be processed
    Thread.sleep(1000);
    methodCallResponse = pythonEngineThread.getResponseQueue().poll(1, TimeUnit.SECONDS);
    commandStatus = methodCallResponse.getPythonInterpreterResponse().getCommandStatus();
    assertFalse(commandStatus.get(methodName));
}
Also used : ArrayList(java.util.ArrayList) MethodCallRequestPayload(org.apache.apex.malhar.python.base.requestresponse.MethodCallRequestPayload) JepPythonTestContext(org.apache.apex.malhar.python.test.JepPythonTestContext) Test(org.junit.Test)

Aggregations

MethodCallRequestPayload (org.apache.apex.malhar.python.base.requestresponse.MethodCallRequestPayload)4 ApexPythonInterpreterException (org.apache.apex.malhar.python.base.ApexPythonInterpreterException)2 EvalCommandRequestPayload (org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload)2 ScriptExecutionRequestPayload (org.apache.apex.malhar.python.base.requestresponse.ScriptExecutionRequestPayload)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JepException (jep.JepException)1 GenericCommandsRequestPayload (org.apache.apex.malhar.python.base.requestresponse.GenericCommandsRequestPayload)1 PythonInterpreterRequest (org.apache.apex.malhar.python.base.requestresponse.PythonInterpreterRequest)1 PythonRequestResponse (org.apache.apex.malhar.python.base.requestresponse.PythonRequestResponse)1 JepPythonTestContext (org.apache.apex.malhar.python.test.JepPythonTestContext)1 Test (org.junit.Test)1