|
16 | 16 | package fr.aneo.armonik.client; |
17 | 17 |
|
18 | 18 | import fr.aneo.armonik.api.grpc.v1.results.ResultsGrpc; |
| 19 | +import fr.aneo.armonik.api.grpc.v1.sessions.SessionsGrpc; |
19 | 20 | import fr.aneo.armonik.client.definition.SessionDefinition; |
20 | 21 | import fr.aneo.armonik.client.definition.TaskDefinition; |
21 | 22 | import fr.aneo.armonik.client.definition.blob.BlobDefinition; |
|
29 | 30 | import java.util.function.Function; |
30 | 31 |
|
31 | 32 | import static fr.aneo.armonik.client.internal.grpc.mappers.BlobMapper.toResultMetaDataRequest; |
| 33 | +import static fr.aneo.armonik.client.internal.grpc.mappers.SessionMapper.*; |
32 | 34 | import static java.util.Objects.requireNonNull; |
33 | 35 |
|
34 | 36 | /** |
@@ -187,6 +189,176 @@ public BlobHandle createBlob(InputBlobDefinition blobDefinition) { |
187 | 189 | return blobHandle; |
188 | 190 | } |
189 | 191 |
|
| 192 | + /** |
| 193 | + * Requests cancellation of this session in the ArmoniK cluster. |
| 194 | + * <p> |
| 195 | + * Cancelling a session signals the control plane to stop all remaining work associated |
| 196 | + * with this session. Depending on the server configuration and current task states, |
| 197 | + * running tasks may be interrupted and queued tasks will no longer be scheduled. |
| 198 | + * Results that have already been produced remain accessible unless the session |
| 199 | + * is subsequently purged or deleted. |
| 200 | + * <p> |
| 201 | + * The returned completion stage is completed asynchronously with the updated |
| 202 | + * {@link SessionState} once the cancellation request has been processed by the |
| 203 | + * Sessions service, or completed exceptionally if the request fails. |
| 204 | + * |
| 205 | + * @return a completion stage that yields the updated state of this session after |
| 206 | + * the cancellation request has been applied |
| 207 | + * |
| 208 | + * @see SessionState |
| 209 | + */ |
| 210 | + public CompletionStage<SessionState> cancel() { |
| 211 | + return channelPool.executeAsync(channel -> { |
| 212 | + var sessionsFutureStub = SessionsGrpc.newFutureStub(channel); |
| 213 | + return Futures.toCompletionStage(sessionsFutureStub.cancelSession(toCancelSessionRequest(sessionInfo.id()))) |
| 214 | + .thenApply(response -> toSessionState(response.getSession())); |
| 215 | + }); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Pauses this session in the ArmoniK cluster. |
| 220 | + * <p> |
| 221 | + * Pausing a session temporarily suspends the scheduling of new tasks in the session. |
| 222 | + * Tasks that are already running continue until completion, but pending tasks are |
| 223 | + * not picked up for execution until the session is resumed via {@link #resume()}. |
| 224 | + * <p> |
| 225 | + * This operation is useful when you need to temporarily throttle or stop processing |
| 226 | + * without cancelling the session or losing its state. |
| 227 | + * The returned completion stage is completed asynchronously with the updated |
| 228 | + * {@link SessionState} once the pause request has been processed by the Sessions |
| 229 | + * service, or completed exceptionally if the request fails. |
| 230 | + * |
| 231 | + * @return a completion stage that yields the updated state of this session after |
| 232 | + * the pause request has been applied |
| 233 | + * |
| 234 | + * @see #resume() |
| 235 | + * @see SessionState |
| 236 | + */ |
| 237 | + public CompletionStage<SessionState> pause() { |
| 238 | + return channelPool.executeAsync(channel -> { |
| 239 | + var sessionsFutureStub = SessionsGrpc.newFutureStub(channel); |
| 240 | + return Futures.toCompletionStage(sessionsFutureStub.pauseSession(toPauseSessionRequest(sessionInfo.id()))) |
| 241 | + .thenApply(response -> toSessionState(response.getSession())); |
| 242 | + }); |
| 243 | + } |
| 244 | + /** |
| 245 | + * Resumes a previously paused session in the ArmoniK cluster. |
| 246 | + * <p> |
| 247 | + * Resuming a session re-enables scheduling of tasks that were previously held |
| 248 | + * while the session was paused. Any pending tasks become eligible for execution |
| 249 | + * again according to the cluster scheduling policies. |
| 250 | + * <p> |
| 251 | + * Calling this method on a session that is not paused is safe; the Sessions service |
| 252 | + * will simply return the current state of the session. |
| 253 | + * The returned completion stage is completed asynchronously with the updated |
| 254 | + * {@link SessionState} once the resume request has been processed by the Sessions |
| 255 | + * service, or completed exceptionally if the request fails. |
| 256 | + * |
| 257 | + * @return a completion stage that yields the updated state of this session after |
| 258 | + * the resume request has been applied |
| 259 | + * |
| 260 | + * @see #pause() |
| 261 | + * @see SessionState |
| 262 | + */ |
| 263 | + public CompletionStage<SessionState> resume() { |
| 264 | + return channelPool.executeAsync(channel -> { |
| 265 | + var sessionsFutureStub = SessionsGrpc.newFutureStub(channel); |
| 266 | + return Futures.toCompletionStage(sessionsFutureStub.resumeSession(toResumeSessionRequest(sessionInfo.id()))) |
| 267 | + .thenApply(response -> toSessionState(response.getSession())); |
| 268 | + }); |
| 269 | + } |
| 270 | + |
| 271 | + /** |
| 272 | + * Closes this session in the ArmoniK cluster. |
| 273 | + * <p> |
| 274 | + * Closing a session finalizes it and prevents any new task submissions, while |
| 275 | + * preserving existing tasks, results, and metadata. This is the recommended way |
| 276 | + * to indicate that no further work will be submitted for this session once all |
| 277 | + * expected tasks have been created. |
| 278 | + * <p> |
| 279 | + * Closing a session does not remove its data. To free up storage or completely |
| 280 | + * remove the session, combine this operation with {@link #purge()} and |
| 281 | + * {@link #delete()} as appropriate. |
| 282 | + * The returned completion stage is completed asynchronously with the updated |
| 283 | + * {@link SessionState} once the close request has been processed by the Sessions |
| 284 | + * service, or completed exceptionally if the request fails. |
| 285 | + * |
| 286 | + * @return a completion stage that yields the updated state of this session after |
| 287 | + * the close request has been applied |
| 288 | + * |
| 289 | + * @see #purge() |
| 290 | + * @see #delete() |
| 291 | + * @see SessionState |
| 292 | + */ |
| 293 | + public CompletionStage<SessionState> close() { |
| 294 | + return channelPool.executeAsync(channel -> { |
| 295 | + var sessionsFutureStub = SessionsGrpc.newFutureStub(channel); |
| 296 | + return Futures.toCompletionStage(sessionsFutureStub.closeSession(toCloseSessionRequest(sessionInfo.id()))) |
| 297 | + .thenApply(response -> toSessionState(response.getSession())); |
| 298 | + }); |
| 299 | + } |
| 300 | + |
| 301 | + /** |
| 302 | + * Purges this session's data in the ArmoniK cluster. |
| 303 | + * <p> |
| 304 | + * Purging a session removes the underlying data for its blobs (task inputs and |
| 305 | + * outputs) from the storage layer while keeping the session and task metadata. |
| 306 | + * This operation is useful to reclaim storage space once the actual data is no |
| 307 | + * longer needed but you still want to keep an audit trail or execution history. |
| 308 | + * <p> |
| 309 | + * After a purge, attempts to download blob data associated with this session |
| 310 | + * will fail, but session and task information remain available until the session |
| 311 | + * is deleted. |
| 312 | + * The returned completion stage is completed asynchronously with the updated |
| 313 | + * {@link SessionState} once the purge request has been processed by the Sessions |
| 314 | + * service, or completed exceptionally if the request fails. |
| 315 | + * |
| 316 | + * @return a completion stage that yields the updated state of this session after |
| 317 | + * the purge request has been applied |
| 318 | + * |
| 319 | + * @see #delete() |
| 320 | + * @see SessionState |
| 321 | + */ |
| 322 | + public CompletionStage<SessionState> purge() { |
| 323 | + return channelPool.executeAsync(channel -> { |
| 324 | + var sessionsFutureStub = SessionsGrpc.newFutureStub(channel); |
| 325 | + return Futures.toCompletionStage(sessionsFutureStub.purgeSession(toPurgeSessionRequest(sessionInfo.id()))) |
| 326 | + .thenApply(response -> toSessionState(response.getSession())); |
| 327 | + }); |
| 328 | + } |
| 329 | + |
| 330 | + /** |
| 331 | + * Deletes this session from the ArmoniK cluster. |
| 332 | + * <p> |
| 333 | + * Deleting a session permanently removes its metadata from the Sessions, Tasks, |
| 334 | + * and Blobs. This is typically the final step in the lifecycle of a |
| 335 | + * session once it has been closed and, optionally, purged. |
| 336 | + * <p> |
| 337 | + * After deletion, this handle still exists as a local object but any further |
| 338 | + * interaction with the remote session (such as submitting tasks or querying |
| 339 | + * state) will fail because the session no longer exists in the cluster. |
| 340 | + * Clients are expected to discard the handle after deletion. |
| 341 | + * The returned completion stage is completed asynchronously with the updated |
| 342 | + * {@link SessionState} reported by the Sessions service just before removal, |
| 343 | + * or completed exceptionally if the request fails. |
| 344 | + * |
| 345 | + * @return a completion stage that yields the last known state of this session |
| 346 | + * before it is removed from the cluster |
| 347 | + * |
| 348 | + * @see #close() |
| 349 | + * @see #purge() |
| 350 | + * @see SessionState |
| 351 | + */ |
| 352 | + public CompletionStage<SessionState> delete() { |
| 353 | + return channelPool.executeAsync(channel -> { |
| 354 | + var sessionsFutureStub = SessionsGrpc.newFutureStub(channel); |
| 355 | + return Futures.toCompletionStage(sessionsFutureStub.deleteSession(toDeleteSessionRequest(sessionInfo.id()))) |
| 356 | + .thenApply(response -> toSessionState(response.getSession())); |
| 357 | + }); |
| 358 | + } |
| 359 | + |
| 360 | + |
| 361 | + |
190 | 362 | private Function<ManagedChannel, CompletionStage<BlobInfo>> createBlobInfo(BlobDefinition blobDefinition) { |
191 | 363 | return channel -> { |
192 | 364 | var request = toResultMetaDataRequest(sessionInfo.id(), List.of(blobDefinition)); |
|
0 commit comments