MongoSandboxExecutionGuard
public final class MongoSandboxExecutionGuard implements SandboxExecutionGuard, AutoCloseable
io/agentscope/extensions/mongodb/sandbox/MongoSandboxExecutionGuard.java:87–437Mongo 沙箱执行守卫,在多次调用之间保存和恢复会话与记忆数据。位于 io.agentscope.extensions.mongodb.sandbox 包,实现 SandboxExecutionGuard、AutoCloseable。核心成员包括 builder、tryEnter、close。
MongoDB-based SandboxExecutionGuard.
Uses a dedicated MongoDB collection as a distributed lock mechanism. Each lock is a document
with a unique _id derived from the SandboxIsolationKey and a TTL index on
expiresAt to auto-release stale locks.
Lock acquisition uses a two-step approach for correct mutual exclusion:
-
Attempt
insertOne— atomic under the unique_idindex; only one concurrent caller succeeds. -
If the lock document already exists (duplicate key), attempt
findOneAndUpdate(non-upsert) with a filter that matches only whenexpiresAt <= now— reclaiming an expired lock.
Acquisition polls until the lock is obtained or the acquisition timeout (lockTimeout) expires. The lock document itself lives for leaseTtl; while the returned
SandboxLease is open, a background watchdog renews the lease every leaseTtl / 3
by pushing expiresAt forward, so an execution that outlives the initial TTL does not
lose its own lock to reclamation. If renewal stops working (lock deleted or stolen), the
watchdog only warns — like the Redis guard, TTL expiry remains a safety valve against permanent
deadlock rather than a correctness guarantee.
Each acquisition generates a random UUID token that is written into the lock document.
Renewal and release filter by this token, not by a process-level identifier, so that two guard
instances in the same JVM (or two processes with the same PID) cannot accidentally release
each other's locks. This mirrors the Redis guard's SET NX + Lua CAS token pattern.
The renewal executor uses a fixed-size thread pool (2 threads) so that a slow
updateOne on one lease does not block renewal of other leases. The guard implements
AutoCloseable and should be closed by io.agentscope.harness.agent.DistributedStore
to shut down the executor when the store is closed.
嵌套类摘要Nested Class Summary
| 类Class | 类型Kind | 说明Description |
|---|---|---|
Builder | 类class | `MongoSandboxExecutionGuard`的构建器。 Builder for `MongoSandboxExecutionGuard`. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public static Builder builder(MongoClient mongoClient)static | 创建新的构建器。 Creates a new builder. |
public SandboxLease tryEnter(SandboxIsolationKey key) throws InterruptedException | 设置该参数。 Performs the try enter operation. |
public void close() | 关闭连接并释放相关资源。 Releases resources and connections. |
方法详情Method Detail
builder
public static Builder builder(MongoClient mongoClient)
创建新的构建器。
Creates a new builder.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
mongoClient | MongoClient | MongoDB 客户端。the MongoDB client |
Builderio/agentscope/extensions/mongodb/sandbox/MongoSandboxExecutionGuard.java:128–130tryEnter
public SandboxLease tryEnter(SandboxIsolationKey key) throws InterruptedException
设置该参数。
Performs the try enter operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | SandboxIsolationKey | 键(类型 `SandboxIsolationKey`)。The key. |
SandboxLeaseInterruptedException可能抛出 `InterruptedException`。Thrown when the operation cannot be completed.
io/agentscope/extensions/mongodb/sandbox/MongoSandboxExecutionGuard.java:132–216close
public void close()
关闭连接并释放相关资源。
Releases resources and connections.
io/agentscope/extensions/mongodb/sandbox/MongoSandboxExecutionGuard.java:236–246嵌套类详情Nested Class Detail
Builder
public static final class BuilderMongoSandboxExecutionGuard的构建器。位于 io.agentscope.extensions.mongodb.sandbox 包。核心成员包括 databaseName、collectionName、lockTimeout、leaseTtl、retryInterval、owner。
Builder for MongoSandboxExecutionGuard.