Skip to content

feat: query cancellation #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 23, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ Thumbs.db

# Next.js
.next

# NX
.nx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import axios from 'axios';
import { spawn } from 'child_process';
import { ChildProcess, spawn } from 'child_process';
import * as puppeteer from 'puppeteer';

describe('Benchmarking DBMs', () => {
let page;
let browser;
let appProcess;
let page: puppeteer.Page;
let browser: puppeteer.Browser;
let appProcess: ChildProcess;

let totalTimeForMemoryDB: number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const RawDBMProvider = ({ children }: { children: JSX.Element }) => {
/**
* Making the queryWithTableNames simply run the queries without sequence which is the default behavior
*/
dbm.queryWithTableNames = async (query, tableNames) => {
dbm.queryWithTableNames = async ({ query, tableNames }) => {
return dbm.query(query);
};
setdbm(dbm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const QueryBenchmarking = () => {
const eachQueryStart = performance.now();

const promiseObj = dbm
.queryWithTableNames(TEST_QUERIES[i], ['taxi'])
.queryWithTableNames({ query: TEST_QUERIES[i], tableNames: ['taxi'] })
.then((results) => {
const end = performance.now();
const time = end - eachQueryStart;
Expand Down
5 changes: 3 additions & 2 deletions meerkat-dbm/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "@devrev/meerkat-dbm",
"version": "0.0.142",
"version": "0.0.143",
"dependencies": {
"tslib": "^2.3.0",
"@duckdb/duckdb-wasm": "^1.28.0",
"dexie": "^3.2.4",
"loglevel": "^1.8.1"
"loglevel": "^1.8.1",
"uuid": "^9.0.1"
},
"repository": {
"type": "git",
Expand Down
141 changes: 110 additions & 31 deletions meerkat-dbm/src/dbm/dbm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { FileData, Table, TableWiseFiles } from '../types';
import { DBM } from './dbm';
import { InstanceManagerType } from './instance-manager';
import { DBMConstructorOptions } from './types';

export class MockFileManager implements FileManagerType {
private fileBufferStore: Record<string, FileBufferStore> = {};
private tables: Record<string, Table> = {};
Expand Down Expand Up @@ -139,6 +140,13 @@ const mockDB = {
}, 200);
});
},
cancelSent: async () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, 100);
});
},
close: async () => {
// do nothing
},
Expand Down Expand Up @@ -203,13 +211,13 @@ describe('DBM', () => {
buffer: new Uint8Array(),
});

const result = await dbm.queryWithTableNames(
'SELECT * FROM table1',
['table1'],
{
const result = await dbm.queryWithTableNames({
query: 'SELECT * FROM table1',
tableNames: ['table1'],
options: {
preQuery,
}
);
},
});

expect(preQuery).toBeCalledTimes(1);

Expand All @@ -222,19 +230,22 @@ describe('DBM', () => {
});

it('should execute a query with table names', async () => {
const result = await dbm.queryWithTableNames('SELECT * FROM table1', [
'table1',
]);
const result = await dbm.queryWithTableNames({
query: 'SELECT * FROM table1',
tableNames: ['table1'],
});
expect(result).toEqual(['SELECT * FROM table1']);
});

it('should execute multiple queries with table names', async () => {
const promise1 = dbm.queryWithTableNames('SELECT * FROM table1', [
'table1',
]);
const promise2 = dbm.queryWithTableNames('SELECT * FROM table2', [
'table1',
]);
const promise1 = dbm.queryWithTableNames({
query: 'SELECT * FROM table1',
tableNames: ['table1'],
});
const promise2 = dbm.queryWithTableNames({
query: 'SELECT * FROM table2',
tableNames: ['table1'],
});
/**
* Number of queries in the queue should be 1 as the first query is running
*/
Expand All @@ -260,9 +271,10 @@ describe('DBM', () => {
/**
* Execute another query
*/
const promise3 = dbm.queryWithTableNames('SELECT * FROM table3', [
'table1',
]);
const promise3 = dbm.queryWithTableNames({
query: 'SELECT * FROM table3',
tableNames: ['table1'],
});

/**
* Now the queue should be running
Expand Down Expand Up @@ -302,16 +314,18 @@ describe('DBM', () => {
/**
* Execute a query
*/
const promise1 = dbm.queryWithTableNames('SELECT * FROM table1', [
'table1',
]);
const promise1 = dbm.queryWithTableNames({
query: 'SELECT * FROM table1',
tableNames: ['table1'],
});

/**
* Execute another query
*/
const promise2 = dbm.queryWithTableNames('SELECT * FROM table2', [
'table1',
]);
const promise2 = dbm.queryWithTableNames({
query: 'SELECT * FROM table2',
tableNames: ['table1'],
});

/**
* Wait for the queries to complete
Expand Down Expand Up @@ -361,16 +375,18 @@ describe('DBM', () => {
/**
* Execute a query
*/
const promise1 = dbm.queryWithTableNames('SELECT * FROM table1', [
'table1',
]);
const promise1 = dbm.queryWithTableNames({
query: 'SELECT * FROM table1',
tableNames: ['table1'],
});

/**
* Execute another query
*/
const promise2 = dbm.queryWithTableNames('SELECT * FROM table2', [
'table1',
]);
const promise2 = dbm.queryWithTableNames({
query: 'SELECT * FROM table2',
tableNames: ['table1'],
});

/**
* Wait for the queries to complete
Expand Down Expand Up @@ -404,7 +420,10 @@ describe('DBM', () => {
/**
* Execute a query
*/
await dbm.queryWithTableNames('SELECT * FROM table1', ['table1']);
await dbm.queryWithTableNames({
query: 'SELECT * FROM table1',
tableNames: ['table1'],
});

/**
* wait for 200ms
Expand All @@ -417,4 +436,64 @@ describe('DBM', () => {
expect(instanceManager.terminateDB).not.toBeCalled();
});
});

describe('cancel query execution', () => {
it('should cancel the current executing query when abort is emitted', async () => {
const abortController1 = new AbortController();

// check the current query throws error abort is emitted
try {
const promise = dbm.queryWithTableNames({
query: 'SELECT * FROM table1',
tableNames: ['table1'],
options: {
signal: abortController1.signal,
},
});

abortController1.abort();

await promise;

expect(promise).not.toBeDefined();
} catch (e) {
expect(e).toBeDefined();
}
});

it('should cancel the query in the queue when abort is emitted', async () => {
const abortController1 = new AbortController();
const abortController2 = new AbortController();

const mockDBMQuery = jest.spyOn(dbm, 'query');

const promise1 = dbm.queryWithTableNames({
query: 'SELECT * FROM table1',
tableNames: ['table1'],
options: {
signal: abortController1.signal,
},
});

const promise2 = dbm.queryWithTableNames({
query: 'SELECT * FROM table2',
tableNames: ['table2'],
options: {
signal: abortController2.signal,
},
});

abortController2.abort();

const promises = await Promise.allSettled([promise1, promise2]);

// the first query should be fulfilled
expect(mockDBMQuery).toBeCalledWith('SELECT * FROM table1');
expect(promises[0].status).toBe('fulfilled');

// the second query should be rejected as it was aborted
expect(mockDBMQuery).not.toBeCalledWith('SELECT * FROM table2');
expect(promises[1].status).toBe('rejected');
});
});
});
Loading