Skip to content

Commit

Permalink
Fix repl get task result bug (#5036)
Browse files Browse the repository at this point in the history
* Fix repl get task result bug

* Fix repl get task result bug
  • Loading branch information
ChengJie1053 authored Dec 13, 2023
1 parent d4dc8af commit 7766933
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package org.apache.linkis.engineplugin.repl.executor;

import org.apache.linkis.common.io.MetaData;
import org.apache.linkis.common.io.Record;
import org.apache.linkis.common.io.resultset.ResultSetWriter;
import org.apache.linkis.common.log.LogUtils;
import org.apache.linkis.common.utils.OverloadUtils;
import org.apache.linkis.engineconn.computation.executor.entity.EngineConnTask;
Expand All @@ -36,15 +39,23 @@
import org.apache.linkis.manager.label.entity.engine.UserCreatorLabel;
import org.apache.linkis.protocol.engine.JobProgressInfo;
import org.apache.linkis.rpc.Sender;
import org.apache.linkis.scheduler.executer.ErrorExecuteResponse;
import org.apache.linkis.scheduler.executer.ExecuteResponse;
import org.apache.linkis.scheduler.executer.SuccessExecuteResponse;
import org.apache.linkis.storage.LineMetaData;
import org.apache.linkis.storage.LineRecord;
import org.apache.linkis.storage.resultset.ResultSetFactory;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import org.springframework.util.CollectionUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -132,14 +143,33 @@ public ExecuteResponse executeLine(EngineExecutionContext engineExecutorContext,

threadCache.put(taskId, Thread.currentThread());

ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
PrintStream cacheStream = new PrintStream(outputStream);
PrintStream oldStream = System.out;
System.setOut(cacheStream);

try {
replAdapter.executorCode(realCode, classpathDir, methodName);
} catch (Exception e) {
String errorMessage = ExceptionUtils.getStackTrace(e);
logger.error("Repl engine execute failed : {}", errorMessage);
engineExecutorContext.appendStdout(LogUtils.generateERROR(errorMessage));
return new ErrorExecuteResponse(errorMessage, null);
}

String message = outputStream.toString();
System.setOut(oldStream);
engineExecutorContext.appendStdout(message);
ResultSetWriter<? extends MetaData, ? extends Record> resultSetWriter =
engineExecutorContext.createResultSetWriter(ResultSetFactory.TEXT_TYPE);
try {
resultSetWriter.addMetaData(new LineMetaData());
resultSetWriter.addRecord(new LineRecord(message));
} catch (IOException e) {
logger.error("Failed to get the task result");
} finally {
IOUtils.closeQuietly(resultSetWriter);
}
return new SuccessExecuteResponse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class JavaReplCompiler {
Pattern.compile("\\s+implements\\s+([\\w\\.]+)\\s*\\{\n");

private static final Pattern METHODS_PATTERN =
Pattern.compile("\n(private|public|protected)\\s+");
Pattern.compile("(?<=\\})\\s+(private|public|protected)\\s+");

private static final Pattern FIELD_PATTERN = Pattern.compile("[^\n]+=[^\n]+;");

Expand Down
37 changes: 37 additions & 0 deletions linkis-engineconn-plugins/repl/src/main/resources/repl-ec.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,43 @@ import org.apache.commons.lang3.StringUtils;
}
```

#### 1.3. Multiple methods

```text
import org.apache.commons.lang3.StringUtils;
public void sayHello() {
System.out.println("hello");
System.out.println(StringUtils.isEmpty("hello"));
}
public void sayHi() {
System.out.println("hi");
System.out.println(StringUtils.isEmpty("hi"));
}
```

```json
{
"executionContent":{
"code":"import org.apache.commons.lang3.StringUtils;\n\n public void sayHello() {\n System.out.println(\"hello\");\n System.out.println(StringUtils.isEmpty(\"hello\"));\n }\n public void sayHi() {\n System.out.println(\"hi\");\n System.out.println(StringUtils.isEmpty(\"hi\"));\n }",
"runType":"repl"
},
"params":{
"configuration":{
"runtime":{
"linkis.repl.type":"java",
"linkis.repl.method.name":"sayHi"
}
}
},
"labels":{
"engineType":"repl-1",
"userCreator":"linkis-IDE"
}
}
```


### 2. Submitting scala tasks with Restful API

```text
Expand Down

0 comments on commit 7766933

Please sign in to comment.