Skip to content

Commit

Permalink
[GAE-Java] Automatically load grape_jvm_opt when create jvm instanc…
Browse files Browse the repository at this point in the history
…es through JNI (#2327)

* Introduce env var `GRAPHSCOPE_HOME` when launching GAE instances.
* Automatically load config file`grape_jvm_opts`.
* Fix SSSPContext to output to `GSVertexArray` rather than file.
  • Loading branch information
zhanglei1949 committed Dec 14, 2022
1 parent 7a3c215 commit 3f96376
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 32 deletions.
33 changes: 33 additions & 0 deletions analytical_engine/core/context/java_context_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ static constexpr const char* IFRAGMENT_HELPER_CLASS =
static constexpr const char* SET_CLASS_LOADER_METHOD_SIG =
"(Ljava/net/URLClassLoader;)V";

std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}

std::string generate_jvm_opts() {
char* gs_home = getenv("GRAPHSCOPE_HOME");
if (!gs_home) {
LOG(ERROR) << "No GRAPHSCOPE_HOME found in env";
return "";
}
std::string cmd =
std::string("source ") + std::string(gs_home) + "/conf/grape_jvm_opts";
std::string res = exec(cmd.c_str());
VLOG(10) << "jvm opts res: " << res;
return res;
}

/**
* @brief JavaContextBase is the base class for JavaPropertyContext and
* JavaProjectedContext.
Expand Down Expand Up @@ -355,6 +381,13 @@ class JavaContextBase : public grape::ContextBase {

// JVM runtime opt should consists of java.libaray.path and
// java.class.path maybe this should be set by the backend not user.
std::string grape_jvm_opt = generate_jvm_opts();
if (!grape_jvm_opt.empty()) {
putenv(const_cast<char*>(grape_jvm_opt.data()));
VLOG(10) << "Find GRAPE_JVM_OPTS in params, setting to env..."
<< grape_jvm_opt;
}

if (getenv("GRAPE_JVM_OPTS")) {
VLOG(1) << "OK, GRAPE_JVM_OPTS has been set.";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import com.alibaba.fastjson.JSONObject;
import com.alibaba.graphscope.context.ParallelContextBase;
import com.alibaba.graphscope.context.VertexDataContext;
import com.alibaba.graphscope.ds.GSVertexArray;
import com.alibaba.graphscope.ds.Vertex;
import com.alibaba.graphscope.ds.VertexRange;
import com.alibaba.graphscope.ds.VertexSet;
import com.alibaba.graphscope.fragment.IFragment;
import com.alibaba.graphscope.parallel.ParallelMessageManager;
Expand All @@ -30,9 +30,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -92,29 +89,12 @@ public void Output(IFragment<Long, Long, Long, Long> frag) {
logger.info(
"frag: " + frag.fid() + " receiveMessageTime: " + receiveMessageTime / 1000000000);
logger.info("frag: " + frag.fid() + " execTime: " + execTime / 1000000000);
String prefix = "/tmp/sssp_parallel_output_threadNum_" + threadNum + "_";
String filePath = prefix + "_frag_" + frag.fid();
try {
FileWriter fileWritter = new FileWriter(filePath);
BufferedWriter bufferedWriter = new BufferedWriter(fileWritter);
VertexRange<Long> innerNodes = frag.innerVertices();
logger.info(
frag.getInnerVerticesNum()
+ " "
+ innerNodes.beginValue()
+ " "
+ innerNodes.endValue());

Vertex<Long> cur = FFITypeFactoryhelper.newVertexLong();
for (long index = 0; index < frag.getInnerVerticesNum(); ++index) {
cur.setValue(index);
Long oid = frag.getId(cur);
bufferedWriter.write(
cur.getValue() + "\t" + oid + "\t" + partialResults.get(index) + "\n");
}
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();

GSVertexArray<Double> vertexArray = data();
Vertex<Long> cur = FFITypeFactoryhelper.newVertexLong();
for (long vid = 0; vid < frag.getInnerVerticesNum(); ++vid) {
cur.setValue(vid);
vertexArray.setValue(cur, (double) partialResults.get(vid));
}
}

Expand Down
7 changes: 2 additions & 5 deletions analytical_engine/java/grape_jvm_opts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ then
echo "Infered GRAPHSCOPE_HOME "${gs_runtime}
fi

if [[ "$SPARK_HOME"x == x ]];
then
echo "[warn] No SPARK_HOME found!"
fi

GRAPHX_GRAPE_SDK=`ls ${GRAPHSCOPE_HOME}/lib/grape-graphx-*.jar`
GRAPE_RUNTIME_JAR=`ls ${GRAPHSCOPE_HOME}/lib/grape-runtime-*.jar`
Expand Down Expand Up @@ -57,7 +53,8 @@ fi

export GRAPE_JVM_OPTS="-Dcom.alibaba.fastffi.rvBuffer=2147483648 -XX:+StartAttachListener -XX:+PreserveFramePointer \
-XX:+UseG1GC -XX:G1HeapRegionSize=32M \
-XX:+PrintGCDetails -XX:+PrintGCDateStamps \
${_JVM_OPTS} \
-XX:+UnlockDiagnosticVMOptions -XX:LoopUnrollLimit=1 -XX:-TieredCompilation \
-Djava.library.path=${GRAPHSCOPE_HOME}/lib \
-Djava.class.path=${class_path}"
echo "GRAPE_JVM_OPTS=${GRAPE_JVM_OPTS}"
1 change: 1 addition & 0 deletions coordinator/gscoordinator/kubernetes_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,7 @@ def create_analytical_instance(self):
logger.info("Analytical engine launching command: {}".format(" ".join(cmd)))

env = os.environ.copy()
env["GRAPHSCOPE_HOME"] = GRAPHSCOPE_HOME
env.update(mpi_env)

self._analytical_engine_process = subprocess.Popen(
Expand Down
1 change: 1 addition & 0 deletions coordinator/gscoordinator/local_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def create_analytical_instance(self):

env = os.environ.copy()
env.update(mpi_env)
env["GRAPHSCOPE_HOME"] = GRAPHSCOPE_HOME

logger.info("Launch analytical engine with command: %s", " ".join(cmd))

Expand Down

0 comments on commit 3f96376

Please sign in to comment.