|
| 1 | +package com.alibaba.graphscope.groot.servers; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | + |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStreamReader; |
| 9 | +import java.io.PrintWriter; |
| 10 | +import java.net.ServerSocket; |
| 11 | +import java.net.Socket; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Optional; |
| 16 | + |
| 17 | +public class GrootGraphDaemon { |
| 18 | + private static final Logger logger = LoggerFactory.getLogger(GrootGraphDaemon.class); |
| 19 | + private static final String GROOT_STORE_POD_ADMIN_PORT = "GROOT_STORE_POD_ADMIN_PORT"; |
| 20 | + private static final int DEFAULT_PORT = 10001; |
| 21 | + private static final String GROOTGRAPH_NAME = "com.alibaba.graphscope.groot.servers.GrootGraph"; |
| 22 | + |
| 23 | + private int healthCheckPort; |
| 24 | + private volatile boolean isShuttingDown = false; |
| 25 | + |
| 26 | + public GrootGraphDaemon() { |
| 27 | + this.healthCheckPort = getHealthCheckPort(); |
| 28 | + } |
| 29 | + |
| 30 | + private int getHealthCheckPort() { |
| 31 | + String port = System.getenv(GROOT_STORE_POD_ADMIN_PORT); |
| 32 | + if (port != null) { |
| 33 | + try { |
| 34 | + return Integer.parseInt(port); |
| 35 | + } catch (NumberFormatException e) { |
| 36 | + logger.error( |
| 37 | + "Failed to parse " |
| 38 | + + GROOT_STORE_POD_ADMIN_PORT |
| 39 | + + " from environment variable " |
| 40 | + + port |
| 41 | + + ", using default port " |
| 42 | + + DEFAULT_PORT); |
| 43 | + return DEFAULT_PORT; |
| 44 | + } |
| 45 | + } |
| 46 | + return DEFAULT_PORT; |
| 47 | + } |
| 48 | + |
| 49 | + public static void main(String[] args) { |
| 50 | + GrootGraphDaemon daemon = new GrootGraphDaemon(); |
| 51 | + daemon.start(); |
| 52 | + } |
| 53 | + |
| 54 | + public void start() { |
| 55 | + try (ServerSocket serverSocket = new ServerSocket(healthCheckPort)) { |
| 56 | + logger.info("GrootGraph daemon started on port " + healthCheckPort); |
| 57 | + while (!isShuttingDown) { |
| 58 | + try (Socket clientSocket = serverSocket.accept()) { |
| 59 | + handleClientRequest(clientSocket); |
| 60 | + } catch (IOException e) { |
| 61 | + if (!isShuttingDown) { |
| 62 | + logger.error("Failed to accept client connection", e); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } catch (IOException e) { |
| 67 | + logger.error("Failed to start daemon", e); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private void handleClientRequest(Socket clientSocket) { |
| 72 | + try { |
| 73 | + // Read the request from client, only simple shutdown command expected |
| 74 | + byte[] buffer = new byte[1024]; |
| 75 | + int readBytes = clientSocket.getInputStream().read(buffer); |
| 76 | + String request = new String(buffer, 0, readBytes, StandardCharsets.UTF_8).trim(); |
| 77 | + |
| 78 | + // Respond to client |
| 79 | + PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); |
| 80 | + |
| 81 | + if (request.toLowerCase().contains("shutdown")) { |
| 82 | + isShuttingDown = true; |
| 83 | + // Shutdown GrootGraph |
| 84 | + shutdownGrootGraph(); |
| 85 | + // Respond to client |
| 86 | + out.println("HTTP/1.0 200 OK"); |
| 87 | + out.println("Content-Type: text/plain"); |
| 88 | + out.println(); |
| 89 | + out.println("GrootGraph has been shutdown successfully."); |
| 90 | + out.flush(); |
| 91 | + isShuttingDown = false; |
| 92 | + } else { |
| 93 | + out.println("HTTP/1.0 400 Bad Request"); |
| 94 | + out.println("Content-Type: text/plain"); |
| 95 | + out.println(); |
| 96 | + out.println("unknown command, only 'shutdown' is supported"); |
| 97 | + out.flush(); |
| 98 | + } |
| 99 | + } catch (IOException e) { |
| 100 | + e.printStackTrace(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void shutdownGrootGraph() { |
| 105 | + try { |
| 106 | + // Execute the command to get the PID of the GrootGraph process |
| 107 | + String[] cmd = { |
| 108 | + "/bin/sh", "-c", "ps -ef | grep -w " + GROOTGRAPH_NAME + " | grep -v grep" |
| 109 | + }; |
| 110 | + Process process = Runtime.getRuntime().exec(cmd); |
| 111 | + BufferedReader reader = |
| 112 | + new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 113 | + List<Long> pids = new ArrayList<>(); |
| 114 | + String line; |
| 115 | + while ((line = reader.readLine()) != null) { |
| 116 | + String[] parts = line.trim().split("\\s+"); |
| 117 | + if (parts.length > 1) { |
| 118 | + try { |
| 119 | + long pid = Long.parseLong(parts[1]); |
| 120 | + pids.add(pid); |
| 121 | + } catch (NumberFormatException e) { |
| 122 | + logger.error("Failed to parse PID from: " + line); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + reader.close(); |
| 127 | + |
| 128 | + for (Long pid : pids) { |
| 129 | + Optional<ProcessHandle> grootGraphProcess = ProcessHandle.of(pid); |
| 130 | + if (grootGraphProcess.isPresent()) { |
| 131 | + ProcessHandle processHandle = grootGraphProcess.get(); |
| 132 | + processHandle.destroy(); |
| 133 | + if (processHandle.isAlive()) { |
| 134 | + processHandle.onExit().join(); |
| 135 | + } |
| 136 | + logger.debug( |
| 137 | + "GrootGraph process with PID " |
| 138 | + + pid |
| 139 | + + " has been shut down successfully."); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + } catch (Exception e) { |
| 144 | + logger.error("Failed to shutdown GrootGraph", e); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments