forked from backstage/backstage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the locks part of the task manager
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
- Loading branch information
Showing
15 changed files
with
490 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2021 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// This file makes it possible to run "yarn knex migrate:make some_file_name" | ||
// to assist in making new migrations | ||
module.exports = { | ||
client: 'sqlite3', | ||
connection: ':memory:', | ||
useNullAsDefault: true, | ||
migrations: { | ||
directory: './migrations', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2020 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// @ts-check | ||
|
||
/** | ||
* @param {import('knex').Knex} knex | ||
*/ | ||
exports.up = async function up(knex) { | ||
// | ||
// locking | ||
// | ||
await knex.schema.createTable( | ||
'backstage_backend_common__task_locks', | ||
table => { | ||
table.comment('Locks used for mutual exclusion among multiple workers'); | ||
table | ||
.text('id') | ||
.primary() | ||
.notNullable() | ||
.comment('The unique id of this particular lock'); | ||
table | ||
.text('acquired_ticket') | ||
.nullable() | ||
.comment('A unique ticket for the current lock acquiral, if any'); | ||
table | ||
.dateTime('acquired_at') | ||
.nullable() | ||
.comment('The time when the lock was acquired, if locked'); | ||
table | ||
.dateTime('expires_at') | ||
.nullable() | ||
.comment('The time when an acquired lock will time out and expire'); | ||
table.index('id', 'task_locks_id_idx'); | ||
}, | ||
); | ||
// | ||
// tasks | ||
// | ||
await knex.schema.createTable('backstage_backend_common__tasks', table => { | ||
table.comment('Tasks used for scheduling work on multiple workers'); | ||
table | ||
.text('id') | ||
.primary() | ||
.notNullable() | ||
.comment('The unique id of this particular task'); | ||
}); | ||
}; | ||
|
||
/** | ||
* @param {import('knex').Knex} knex | ||
*/ | ||
exports.down = async function down(knex) { | ||
await knex.schema.alterTable('task_locks', table => { | ||
table.dropIndex([], 'task_locks_id_idx'); | ||
}); | ||
await knex.schema.dropTable('task_locks'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/backend-common/src/database/migrateBackendCommon.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2021 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Knex } from 'knex'; | ||
import { resolvePackagePath } from '../paths'; | ||
|
||
const migrationsDir = resolvePackagePath( | ||
'@backstage/backend-common', | ||
'migrations', | ||
); | ||
|
||
export async function migrateBackendCommon(knex: Knex): Promise<void> { | ||
await knex.migrate.latest({ | ||
directory: migrationsDir, | ||
tableName: 'knex_migrations_backstage_backend_common', | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2021 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export type DbTaskLocksRow = { | ||
id: string; | ||
acquired_ticket?: string; | ||
acquired_at?: Date; | ||
expires_at?: Date; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2021 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils'; | ||
import { Duration } from 'luxon'; | ||
import { DatabaseManager } from '../database'; | ||
import { TaskManager } from './TaskManager'; | ||
|
||
describe('TaskManager', () => { | ||
const databases = TestDatabases.create({ | ||
ids: ['POSTGRES_13', 'POSTGRES_9', 'SQLITE_3'], | ||
}); | ||
|
||
async function createDatabase( | ||
databaseId: TestDatabaseId, | ||
): Promise<DatabaseManager> { | ||
const knex = await databases.init(databaseId); | ||
const databaseManager: Partial<DatabaseManager> = { | ||
forPlugin: () => ({ | ||
getClient: async () => knex, | ||
}), | ||
}; | ||
return databaseManager as DatabaseManager; | ||
} | ||
|
||
describe('locking', () => { | ||
it.each(databases.eachSupportedId())( | ||
'can run the happy path, %p', | ||
async databaseId => { | ||
const database = await createDatabase(databaseId); | ||
const manager = new TaskManager(database).forPlugin('test'); | ||
|
||
const lock1 = await manager.acquireLock('lock1', { | ||
timeout: Duration.fromMillis(5000), | ||
}); | ||
const lock2 = await manager.acquireLock('lock2', { | ||
timeout: Duration.fromMillis(5000), | ||
}); | ||
|
||
expect(lock1.acquired).toBe(true); | ||
expect(lock2.acquired).toBe(true); | ||
|
||
await expect( | ||
manager.acquireLock('lock1', { | ||
timeout: Duration.fromMillis(5000), | ||
}), | ||
).resolves.toEqual({ acquired: false }); | ||
|
||
await (lock1 as any).release(); | ||
await (lock2 as any).release(); | ||
|
||
const lock1Again = await manager.acquireLock('lock1', { | ||
timeout: Duration.fromMillis(5000), | ||
}); | ||
expect(lock1Again.acquired).toBe(true); | ||
await (lock1Again as any).release(); | ||
}, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.