Class SharedServerManager
- All Implemented Interfaces:
Closeable,AutoCloseable,ServerManager
This mode reduces memory overhead by running one JVM instead of N JVMs, at the cost of reduced isolation: one crash affects all in-flight requests.
Not recommended for production use. See Tika Pipes documentation for details.
The shared server creates a ServerSocket and accepts incoming connections from clients.
ensureRunning() is synchronized to prevent multiple clients from attempting
to restart the server simultaneously.
Security: The server port and a 32-byte auth token are passed to the child
process via environment variables (not command-line args), so they are not visible
in /proc/<pid>/cmdline. Each client connection must present the token before
the server will accept it. This prevents CVE-style abuse from untrusted local
processes. Note: if a malicious actor has same-uid access to your host and can read
/proc/<pid>/environ, that is beyond Tika's security model.
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionSharedServerManager(PipesConfig pipesConfig, Path tikaConfigPath, int numConnections) Creates a SharedServerManager. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()connect(int socketTimeoutMs) Establishes a connection to the server and returns a connected Socket.voidEnsures the shared server is running, starting it if necessary.intgetPort()Returns the current server port, blocking if a restart is in progress.Returns the path to the temporary directory used by the server.voidincrementFilesProcessed(long maxFilesPerProcess) Increments the count of files processed and marks for restart if limit reached.booleanChecks if the server process is currently running.voidMarks the server for restart due to a fatal error (OOM, timeout).booleanChecks if the server has been marked for restart.voidshutdown()Shuts down the server process and cleans up resources.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.tika.pipes.core.ServerManager
handleCrashAndGetExitCode
-
Field Details
-
SOCKET_CONNECT_TIMEOUT_MS
public static final int SOCKET_CONNECT_TIMEOUT_MS- See Also:
-
-
Constructor Details
-
SharedServerManager
Creates a SharedServerManager.- Parameters:
pipesConfig- the pipes configurationtikaConfigPath- path to the tika config filenumConnections- number of concurrent connections the server should support
-
-
Method Details
-
getPort
public int getPort()Returns the current server port, blocking if a restart is in progress. This ensures clients always see a consistent port after restart completes.- Specified by:
getPortin interfaceServerManager- Returns:
- the server port, or -1 if not started
-
getTempDirectory
Description copied from interface:ServerManagerReturns the path to the temporary directory used by the server. May return null if the server hasn't been started yet.- Specified by:
getTempDirectoryin interfaceServerManager- Returns:
- the temp directory path, or null if not started
-
isRunning
public boolean isRunning()Description copied from interface:ServerManagerChecks if the server process is currently running.- Specified by:
isRunningin interfaceServerManager- Returns:
- true if the server process is running
-
ensureRunning
public void ensureRunning() throws IOException, InterruptedException, TimeoutException, ServerInitializationExceptionEnsures the shared server is running, starting it if necessary.This method is synchronized - only one client will start/restart the server while others wait. The restarting flag is set during restart so that getPort() blocks until the new server is ready.
- Specified by:
ensureRunningin interfaceServerManager- Throws:
IOException- if the server cannot be startedServerInitializationException- if the server fails to initializeInterruptedException- if interrupted while waiting for server startupTimeoutException- if the server doesn't start within the configured timeout
-
markServerForRestart
public void markServerForRestart()Marks the server for restart due to a fatal error (OOM, timeout).This is called by clients when they receive OOM or TIMEOUT status. It signals that the server process is stopping (System.exit was called), even if isRunning() might still return true briefly.
The next call to ensureRunning() will wait for the process to fully exit and then restart the server.
- Specified by:
markServerForRestartin interfaceServerManager
-
incrementFilesProcessed
public void incrementFilesProcessed(long maxFilesPerProcess) Increments the count of files processed and marks for restart if limit reached.- Specified by:
incrementFilesProcessedin interfaceServerManager- Parameters:
maxFilesPerProcess- the maximum files before restart (0 means unlimited)
-
needsRestart
public boolean needsRestart()Checks if the server has been marked for restart.- Specified by:
needsRestartin interfaceServerManager- Returns:
- true if the server has been marked for restart
-
connect
Description copied from interface:ServerManagerEstablishes a connection to the server and returns a connected Socket.The behavior differs by implementation:
- Per-client mode: Accepts incoming connection from the dedicated server
- Shared mode: Connects out to the shared server
This method should be called after
ServerManager.ensureRunning().- Specified by:
connectin interfaceServerManager- Parameters:
socketTimeoutMs- the socket timeout in milliseconds- Returns:
- a connected Socket ready for communication
- Throws:
IOException- if connection failsServerInitializationException- if the server died before connecting
-
shutdown
Description copied from interface:ServerManagerShuts down the server process and cleans up resources. After calling this method,ServerManager.ensureRunning()can be called to restart.- Specified by:
shutdownin interfaceServerManager- Throws:
InterruptedException- if interrupted while waiting for shutdown
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-