You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by "tcataldo (via GitHub)" <gi...@apache.org> on 2023/06/09 15:08:39 UTC

[GitHub] [cassandra] tcataldo opened a new pull request, #2405: CASSANDRA-14351 Feat: notify systemd when startup is complete

tcataldo opened a new pull request, #2405:
URL: https://github.com/apache/cassandra/pull/2405

   Notifying systemd will permit writing proper service files with cleaner starting behavior. 
   
   https://issues.apache.org/jira/browse/CASSANDRA-14351
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


[GitHub] [cassandra] tcataldo commented on a diff in pull request #2405: CASSANDRA-14351 Feat: notify systemd when startup is complete

Posted by "tcataldo (via GitHub)" <gi...@apache.org>.
tcataldo commented on code in PR #2405:
URL: https://github.com/apache/cassandra/pull/2405#discussion_r1297439016


##########
src/java/org/apache/cassandra/service/SystemD.java:
##########
@@ -0,0 +1,102 @@
+package org.apache.cassandra.service;
+
+import java.io.File;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.sun.jna.Library; // NOSONAR
+import com.sun.jna.Native; // NOSONAR
+
+public class SystemD {
+
+	private static final Logger logger = LoggerFactory.getLogger(SystemD.class);
+	private static final Api INSTANCE = init();
+
+	private static final CLibrary LIBC = (CLibrary) Native.loadLibrary("c", CLibrary.class);
+
+	private interface CLibrary extends Library {
+
+		int getpid();
+	}
+
+	public static enum SystemDLocation {
+		Centos("/usr/lib64/libsystemd.so.0"),
+
+		// 16.04, 18.04, stretch
+		OldUbuntuDebian("/lib/x86_64-linux-gnu/libsystemd.so.0"),
+
+		// /lib link to /usr/lib
+		UbuntuDebian("/usr/lib/x86_64-linux-gnu/libsystemd.so.0");
+
+		public final String systemdLibLocation;
+
+		private SystemDLocation(String lib) {
+			this.systemdLibLocation = lib;
+		}
+	}
+
+	public static class Api {
+
+		private final RawApi impl;
+
+		private Api(RawApi rawApi) {
+			this.impl = rawApi;
+		}
+
+		public void notifyReady() {
+			int pid = LIBC.getpid();
+			logger.info("Notify ready through systemd for PID {}...", pid);
+			int errorCode = impl.sd_pid_notify(pid, 0, "READY=1");
+			if (errorCode <= 0) {
+				logger.error("Notify failed: {}", errorCode);
+			} else {
+				logger.info("Notified for {}, errorCode: {}", pid, errorCode);
+			}
+		}
+
+	}
+
+	private interface RawApi extends Library {
+
+		int sd_pid_notify(int pid, int unset, String state); // NOSONAR
+
+	}
+
+	private static SystemDLocation figureOutLocation() {
+		for (SystemDLocation loc : SystemDLocation.values()) {
+			if (new File(loc.systemdLibLocation).exists()) {
+				logger.info("Selected location {}", loc);
+				return loc;
+			}
+		}
+		return null;
+	}
+
+	@VisibleForTesting
+	public static boolean isAvailable() {
+		if (INSTANCE == null) {
+			return false;
+		}
+		return true;
+	}
+
+	@VisibleForTesting
+	public static Api get() {
+		if (!isAvailable()) {
+			throw new RuntimeException("SystemD is not available");
+		}
+		return INSTANCE;
+	}
+
+	private static Api init() {
+		SystemDLocation loc = figureOutLocation();
+		if (loc != null) {
+			RawApi rawApi = (RawApi) Native.loadLibrary(loc.systemdLibLocation, RawApi.class);

Review Comment:
   Maybe Native.loadLibrary("systemd") would work & would avoid trying multiple locations



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Re: [PR] CASSANDRA-14351 Feat: notify systemd when startup is complete [cassandra]

Posted by "tcataldo (via GitHub)" <gi...@apache.org>.
tcataldo commented on PR #2405:
URL: https://github.com/apache/cassandra/pull/2405#issuecomment-1744121592

   Hi, I updated the PR to let the linker figure the systemd library location instead of hardcoding some path. Hope this is ok.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org