sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / wasm / tests / opfs / concurrency / worker.js
| 1 | 'use strict'; |
| 2 | const urlArgs = new URL(globalThis.location.href).searchParams; |
| 3 | const options = { |
| 4 | workerName: urlArgs.get('workerId') || Math.round(Math.random()*10000), |
| 5 | unlockAsap: urlArgs.get('opfs-unlock-asap') || 0, |
| 6 | vfs: urlArgs.get('vfs') |
| 7 | }; |
| 8 | const jsSqlite = urlArgs.get('sqlite3.dir') |
| 9 | +'/sqlite3.js?opfs-async-proxy-id=' |
| 10 | +options.workerName; |
| 11 | importScripts(jsSqlite)/*Sigh - URL args are not propagated this way*/; |
| 12 | //const sqlite3InitModule = (await import(jsSqlite)).default; |
| 13 | globalThis.sqlite3InitModule.__isUnderTest = |
| 14 | true /* causes sqlite3.opfs to be retained */; |
| 15 | globalThis.sqlite3InitModule().then(async function(sqlite3){ |
| 16 | const wPost = (type,...payload)=>{ |
| 17 | postMessage({type, worker: options.workerName, payload}); |
| 18 | }; |
| 19 | const stdout = (...args)=>wPost('stdout',...args); |
| 20 | const stderr = (...args)=>wPost('stderr',...args); |
| 21 | if(!sqlite3.opfs){ |
| 22 | stderr("This code requires the (private) sqlite3.opfs object. Aborting."); |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | const wait = async (ms)=>{ |
| 27 | return new Promise((resolve)=>setTimeout(resolve,ms)); |
| 28 | }; |
| 29 | |
| 30 | const dbName = 'concurrency-tester.db'; |
| 31 | if(urlArgs.has('unlink-db')){ |
| 32 | await sqlite3.opfs.unlink(dbName); |
| 33 | stdout("Unlinked",dbName); |
| 34 | } |
| 35 | wPost('loaded'); |
| 36 | let db; |
| 37 | const interval = Object.assign(Object.create(null),{ |
| 38 | delay: urlArgs.has('interval') ? (+urlArgs.get('interval') || 750) : 750, |
| 39 | handle: undefined, |
| 40 | count: 0 |
| 41 | }); |
| 42 | const finish = ()=>{ |
| 43 | if(db?.pointer){ |
| 44 | db.close(); |
| 45 | } |
| 46 | if(interval.error){ |
| 47 | stderr("Ending work at interval #"+interval.count, |
| 48 | "due to error:", interval.error); |
| 49 | wPost('failed', "at interval #"+interval.count, |
| 50 | "due to error:",interval.error); |
| 51 | }else{ |
| 52 | wPost('finished',"Ending work after",interval.count,"intervals."); |
| 53 | } |
| 54 | }; |
| 55 | const run = async function(){ |
| 56 | const Ctors = Object.assign(Object.create(null),{ |
| 57 | opfs: sqlite3.oo1.OpfsDb, |
| 58 | 'opfs-wl': sqlite3.oo1.OpfsWlDb |
| 59 | }); |
| 60 | const ctor = Ctors[options.vfs]; |
| 61 | if( !ctor ){ |
| 62 | stderr("Invalid VFS name:",vfs); |
| 63 | return; |
| 64 | } |
| 65 | stdout("ctor:", options.vfs); |
| 66 | while(true){ |
| 67 | try{ |
| 68 | db = new ctor({ |
| 69 | filename: 'file:'+dbName+'?opfs-unlock-asap='+options.unlockAsap, |
| 70 | flags: 'c' |
| 71 | }); |
| 72 | break; |
| 73 | }catch(e){ |
| 74 | if(e instanceof sqlite3.SQLite3Error |
| 75 | && sqlite3.capi.SQLITE_BUSY===e.resultCode){ |
| 76 | stderr("Retrying open() for BUSY:",e.message); |
| 77 | continue; |
| 78 | } |
| 79 | throw e; |
| 80 | } |
| 81 | } |
| 82 | sqlite3.capi.sqlite3_busy_timeout(db.pointer, 5000); |
| 83 | sqlite3.capi.sqlite3_js_retry_busy(-1, ()=>{ |
| 84 | db.transaction((db)=>{ |
| 85 | db.exec([ |
| 86 | "create table if not exists t1(w TEXT UNIQUE ON CONFLICT REPLACE,v);", |
| 87 | "create table if not exists t2(w TEXT UNIQUE ON CONFLICT REPLACE,v);" |
| 88 | ]); |
| 89 | }); |
| 90 | }); |
| 91 | |
| 92 | const maxIterations = |
| 93 | urlArgs.has('iterations') ? (+urlArgs.get('iterations') || 10) : 10; |
| 94 | stdout("Starting",maxIterations,"interval-based db updates with delay of",interval.delay,"ms."); |
| 95 | const doWork = async ()=>{ |
| 96 | const tm = new Date().getTime(); |
| 97 | ++interval.count; |
| 98 | const prefix = "v(#"+interval.count+")"; |
| 99 | stdout("Setting",prefix,"=",tm); |
| 100 | try{ |
| 101 | sqlite3.capi.sqlite3_js_retry_busy(-1, ()=>{ |
| 102 | db.exec({ |
| 103 | sql:"INSERT OR REPLACE INTO t1(w,v) VALUES(?,?)", |
| 104 | bind: [options.workerName, new Date().getTime()] |
| 105 | }); |
| 106 | }, (n)=>{ |
| 107 | stderr("Attempt #",n,"for interval #",interval.count,"due to BUSY"); |
| 108 | }); |
| 109 | }catch(e){ |
| 110 | stderr("Error: ",e.message); |
| 111 | interval.error = e; |
| 112 | throw e; |
| 113 | } |
| 114 | //stdout("doWork()",prefix,"error ",interval.error); |
| 115 | }; |
| 116 | setTimeout(async function timer(){ |
| 117 | await doWork(); |
| 118 | if(interval.error || maxIterations === interval.count){ |
| 119 | finish(); |
| 120 | }else{ |
| 121 | const jitter = (Math.random()>=0.5) |
| 122 | ? ((Math.random()*100 - Math.random()*100) | 0) |
| 123 | : 0; |
| 124 | const n = interval.delay + jitter; |
| 125 | setTimeout(timer, n<50 ? interval.delay : n); |
| 126 | } |
| 127 | }, interval.delay); |
| 128 | }/*run()*/; |
| 129 | |
| 130 | globalThis.onmessage = function({data}){ |
| 131 | switch(data.type){ |
| 132 | case 'run': |
| 133 | run().catch((e)=>{ |
| 134 | if(!interval.error) interval.error = e; |
| 135 | }).catch(e=>{ |
| 136 | /* Don't do this in finally() - it fires too soon. */ |
| 137 | finish(); |
| 138 | throw e; |
| 139 | }); |
| 140 | break; |
| 141 | default: |
| 142 | stderr("Unhandled message type '"+data.type+"'."); |
| 143 | break; |
| 144 | } |
| 145 | }; |
| 146 | }); |