Class PerClientServerManager
- All Implemented Interfaces:
Closeable,AutoCloseable,ServerManager
This is the default mode where each PipesClient has its own server process, providing isolation at the cost of memory overhead.
Connection model: The client creates a ServerSocket and the server connects TO it. This is the reverse of typical client-server patterns but allows the client to control the port assignment.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionPerClientServerManager(PipesConfig pipesConfig, Path tikaConfigPath, int clientId) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()connect(int socketTimeoutMillis) Establishes a connection to the server and returns a connected Socket.voidSignals that the client walked away from its connection, whether mid-handshake or with a request still in flight.voidEnsures the server is running, starting or restarting it if necessary.longCounts forks soPipesParser.getGeneration()is meaningful in per-client mode too.intgetPort()Returns the port number the server is listening on.Returns the server process.longgetRestartCount(RestartReason reason) Restarts performed so far forreason; monotonic, never reset.Returns the ServerSocket for accepting client connections.Returns the path to the temporary directory used by the server.inthandleCrashAndGetExitCode(long generation) Handles a crash by checking the process exit code and marking for restart.voidincrementFilesProcessed(long maxFilesPerProcess) Increments the count of files processed and marks for restart if limit reached.booleanChecks if the server process is currently running.voidmarkServerForRestart(RestartReason reason, long ignoredGeneration) One client owns one manager here, sogenerationcarries no information a sibling could invalidate and is accepted only to satisfy the singleServerManagerspelling.booleanChecks if the server has been marked for restart.voidshutdown()Shuts down the server process and cleans up resources.
-
Field Details
-
SOCKET_CONNECT_TIMEOUT_MS
public static final int SOCKET_CONNECT_TIMEOUT_MS- See Also:
-
-
Constructor Details
-
PerClientServerManager
-
-
Method Details
-
getPort
public int getPort()Description copied from interface:ServerManagerReturns the port number the server is listening on. May return -1 if the server hasn't been started yet.- 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
-
incrementFilesProcessed
public void incrementFilesProcessed(long maxFilesPerProcess) Description copied from interface:ServerManagerIncrements the count of files processed and marks for restart if limit reached.This tracks progress toward the maxFilesProcessedPerProcess limit. When the limit is reached,
ServerManager.needsRestart()will return true and the next call toServerManager.ensureRunning()will restart the server.- Specified by:
incrementFilesProcessedin interfaceServerManager- Parameters:
maxFilesPerProcess- the maximum files before restart (0 means unlimited)
-
needsRestart
public boolean needsRestart()Description copied from interface:ServerManagerChecks if the server has been marked for restart.This allows clients to detect that a restart is pending before attempting to use an existing connection that might be stale.
- Specified by:
needsRestartin interfaceServerManager- Returns:
- true if the server has been marked for restart
-
markServerForRestart
One client owns one manager here, sogenerationcarries no information a sibling could invalidate and is accepted only to satisfy the singleServerManagerspelling. Shared mode is where staleness is real.- Specified by:
markServerForRestartin interfaceServerManager
-
getGeneration
public long getGeneration()Counts forks soPipesParser.getGeneration()is meaningful in per-client mode too.- Specified by:
getGenerationin interfaceServerManager
-
getRestartCount
Description copied from interface:ServerManagerRestarts performed so far forreason; monotonic, never reset.- Specified by:
getRestartCountin interfaceServerManager
-
connectionAbandoned
public void connectionAbandoned()Description copied from interface:ServerManagerSignals that the client walked away from its connection, whether mid-handshake or with a request still in flight.A per-client worker dials the parent once and never dials back, so an abandoned worker must be recycled or the next
ServerManager.connect(int)waits out the full accept timeout against a process that will never call. A shared server outlives its clients and needs nothing here; its connection handlers already detect the dead socket themselves.- Specified by:
connectionAbandonedin interfaceServerManager
-
handleCrashAndGetExitCode
public int handleCrashAndGetExitCode(long generation) Description copied from interface:ServerManagerHandles a crash by checking the process exit code and marking for restart.In per-client mode, waits briefly for the process to exit and checks the exit code to determine if this was an OOM or TIMEOUT. In shared mode, just marks for restart (exit code checking is not reliable since multiple clients share the process).
- Specified by:
handleCrashAndGetExitCodein interfaceServerManager- Returns:
- the exit code if available, or -1 if the process is still running or unavailable
-
ensureRunning
public void ensureRunning() throws IOException, InterruptedException, TimeoutException, ServerInitializationExceptionDescription copied from interface:ServerManagerEnsures the server is running, starting or restarting it if necessary.In shared mode, this method is synchronized to prevent multiple clients from attempting to restart the server simultaneously.
- Specified by:
ensureRunningin interfaceServerManager- Throws:
IOException- if the server cannot be startedInterruptedException- if interrupted while waiting for server startupTimeoutException- if the server doesn't start within the configured timeoutServerInitializationException- if the server fails to initialize
-
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:
socketTimeoutMillis- 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
-
getServerSocket
Returns the ServerSocket for accepting client connections. Used by PipesClient to accept the server's incoming connection.- Returns:
- the server socket
-
getProcess
Returns the server process.- Returns:
- the process, or null if not started
-