Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/kbn-lock-manager/src/lock_manager_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ export class LockManager {
def instantNow = Instant.ofEpochMilli(now);
ctx._source.createdAt = instantNow.toString();
ctx._source.expiresAt = instantNow.plusMillis(params.ttl).toString();
ctx._source.metadata = params.metadata;
ctx._source.token = params.token;
} else {
ctx.op = 'noop';
}
`,
params: {
ttl,
token: this.token,
metadata,
},
},
// @ts-expect-error
upsert: {
metadata,
token: this.token,
},
upsert: {},
},
{
retryOnTimeout: true,
Expand Down Expand Up @@ -189,7 +189,7 @@ export class LockManager {
this.logger.debug(`Lock "${this.lockId}" released with token ${this.token}.`);
return true;
case 'noop':
this.logger.debug(
this.logger.warn(
`Lock "${this.lockId}" with token = ${this.token} could not be released. Token does not match.`
);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,41 @@ export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderCon
expect(lock?.metadata).to.eql({ attempt: 'one' });
});

it('allows re-acquisition after expiration', async () => {
// Acquire with a very short TTL.
const acquired = await manager1.acquire({ ttl: 500, metadata: { attempt: 'one' } });
expect(acquired).to.be(true);
describe('when a lock by "manager1" expires, and is attempted re-acquired by "manager2"', () => {
let expiredLock: LockDocument | undefined;
let reacquireResult: boolean;
beforeEach(async () => {
// Acquire with a very short TTL.
const acquired = await manager1.acquire({ ttl: 500, metadata: { attempt: 'one' } });
expect(acquired).to.be(true);
await sleep(1000); // wait for lock to expire
expiredLock = await getLockById(es, LOCK_ID);
reacquireResult = await manager2.acquire({ metadata: { attempt: 'two' } });
});

it('can be re-acquired', async () => {
expect(reacquireResult).to.be(true);
});

it('updates the token when re-acquired', async () => {
const reacquiredLock = await getLockById(es, LOCK_ID);
expect(expiredLock?.token).not.to.be(reacquiredLock?.token);
});

await sleep(1000); // wait for lock to expire
it('updates the metadata when re-acquired', async () => {
const reacquiredLock = await getLockById(es, LOCK_ID);
expect(reacquiredLock?.metadata).to.eql({ attempt: 'two' });
});

const reacquired = await manager2.acquire({ metadata: { attempt: 'two' } });
expect(reacquired).to.be(true);
it('cannot be released by "manager1"', async () => {
const res = await manager1.release();
expect(res).to.be(false);
});

it('can be released by "manager2"', async () => {
const res = await manager2.release();
expect(res).to.be(true);
});
});
});

Expand Down