mirror of
https://github.com/sqlite/sqlite.git
synced 2025-07-29 08:01:23 +03:00
Replace time-based auto-unlock of opfs sync handles with lock acquisition/release via sqlite3_io_methods::xLock/xUnlock().
FossilOrigin-Name: 2625b7cfe1640c1d7e779ec1f37db970541598c0dc3e22e5eecf3c772d95ad40
This commit is contained in:
@ -169,6 +169,7 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){
|
||||
W.onerror = function(err){
|
||||
// The error object doesn't contain any useful info when the
|
||||
// failure is, e.g., that the remote script is 404.
|
||||
error("Error initializing OPFS asyncer:",err);
|
||||
promiseReject(new Error("Loading OPFS async Worker failed for unknown reasons."));
|
||||
};
|
||||
const pDVfs = capi.sqlite3_vfs_find(null)/*pointer to default VFS*/;
|
||||
@ -202,7 +203,6 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){
|
||||
environment or the other when sqlite3_os_end() is called (_if_ it
|
||||
gets called at all in a wasm build, which is undefined).
|
||||
*/
|
||||
|
||||
/**
|
||||
State which we send to the async-api Worker or share with it.
|
||||
This object must initially contain only cloneable or sharable
|
||||
@ -224,7 +224,12 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){
|
||||
*/
|
||||
const state = Object.create(null);
|
||||
state.verbose = options.verbose;
|
||||
state.littleEndian = true;
|
||||
state.littleEndian = (()=>{
|
||||
const buffer = new ArrayBuffer(2);
|
||||
new DataView(buffer).setInt16(0, 256, true /* littleEndian */);
|
||||
// Int16Array uses the platform's endianness.
|
||||
return new Int16Array(buffer)[0] === 256;
|
||||
})();
|
||||
/** Whether the async counterpart should log exceptions to
|
||||
the serialization channel. That produces a great deal of
|
||||
noise for seemingly innocuous things like xAccess() checks
|
||||
@ -265,7 +270,7 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){
|
||||
between both workers. This worker writes to it and the other
|
||||
listens for changes. */
|
||||
state.opIds.whichOp = i++;
|
||||
/* Slot for storing return values. This work listens to that
|
||||
/* Slot for storing return values. This worker listens to that
|
||||
slot and the other worker writes to it. */
|
||||
state.opIds.rc = i++;
|
||||
/* Each function gets an ID which this worker writes to
|
||||
@ -278,11 +283,13 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){
|
||||
state.opIds.xDeleteNoWait = i++;
|
||||
state.opIds.xFileControl = i++;
|
||||
state.opIds.xFileSize = i++;
|
||||
state.opIds.xLock = i++;
|
||||
state.opIds.xOpen = i++;
|
||||
state.opIds.xRead = i++;
|
||||
state.opIds.xSleep = i++;
|
||||
state.opIds.xSync = i++;
|
||||
state.opIds.xTruncate = i++;
|
||||
state.opIds.xUnlock = i++;
|
||||
state.opIds.xWrite = i++;
|
||||
state.opIds.mkdir = i++;
|
||||
state.opIds['opfs-async-metrics'] = i++;
|
||||
@ -290,7 +297,6 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){
|
||||
state.sabOP = new SharedArrayBuffer(i * 4/*sizeof int32*/);
|
||||
opfsUtil.metrics.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
SQLITE_xxx constants to export to the async worker
|
||||
counterpart...
|
||||
@ -304,10 +310,17 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){
|
||||
'SQLITE_IOERR_TRUNCATE', 'SQLITE_IOERR_DELETE',
|
||||
'SQLITE_IOERR_ACCESS', 'SQLITE_IOERR_CLOSE',
|
||||
'SQLITE_IOERR_DELETE',
|
||||
'SQLITE_LOCK_NONE',
|
||||
'SQLITE_LOCK_SHARED',
|
||||
'SQLITE_LOCK_RESERVED',
|
||||
'SQLITE_LOCK_PENDING',
|
||||
'SQLITE_LOCK_EXCLUSIVE',
|
||||
'SQLITE_OPEN_CREATE', 'SQLITE_OPEN_DELETEONCLOSE',
|
||||
'SQLITE_OPEN_READONLY'
|
||||
].forEach(function(k){
|
||||
state.sq3Codes[k] = capi[k] || toss("Maintenance required: not found:",k);
|
||||
].forEach((k)=>{
|
||||
if(undefined === (state.sq3Codes[k] = capi[k])){
|
||||
toss("Maintenance required: not found:",k);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
@ -605,7 +618,8 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){
|
||||
xCheckReservedLock: function(pFile,pOut){
|
||||
// Exclusive lock is automatically acquired when opened
|
||||
//warn("xCheckReservedLock(",arguments,") is a no-op");
|
||||
wasm.setMemValue(pOut,1,'i32');
|
||||
const f = __openFiles[pFile];
|
||||
wasm.setMemValue(pOut, f.lockMode ? 1 : 0, 'i32');
|
||||
return 0;
|
||||
},
|
||||
xClose: function(pFile){
|
||||
@ -643,9 +657,17 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){
|
||||
return rc;
|
||||
},
|
||||
xLock: function(pFile,lockType){
|
||||
//2022-09: OPFS handles lock when opened
|
||||
//warn("xLock(",arguments,") is a no-op");
|
||||
return 0;
|
||||
mTimeStart('xLock');
|
||||
const f = __openFiles[pFile];
|
||||
let rc = 0;
|
||||
if( capi.SQLITE_LOCK_NONE === f.lockType ) {
|
||||
rc = opRun('xLock', pFile, lockType);
|
||||
if( 0===rc ) f.lockType = lockType;
|
||||
}else{
|
||||
f.lockType = lockType;
|
||||
}
|
||||
mTimeEnd();
|
||||
return rc;
|
||||
},
|
||||
xRead: function(pFile,pDest,n,offset64){
|
||||
/* int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst) */
|
||||
@ -676,9 +698,16 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){
|
||||
return rc;
|
||||
},
|
||||
xUnlock: function(pFile,lockType){
|
||||
//2022-09: OPFS handles lock when opened
|
||||
//warn("xUnlock(",arguments,") is a no-op");
|
||||
return 0;
|
||||
mTimeStart('xUnlock');
|
||||
const f = __openFiles[pFile];
|
||||
let rc = 0;
|
||||
if( capi.SQLITE_LOCK_NONE === lockType
|
||||
&& f.lockType ){
|
||||
rc = opRun('xUnlock', pFile, lockType);
|
||||
}
|
||||
if( 0===rc ) f.lockType = lockType;
|
||||
mTimeEnd();
|
||||
return rc;
|
||||
},
|
||||
xWrite: function(pFile,pSrc,n,offset64){
|
||||
/* int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst) */
|
||||
@ -696,7 +725,7 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){
|
||||
return rc;
|
||||
}
|
||||
}/*ioSyncWrappers*/;
|
||||
|
||||
|
||||
/**
|
||||
Impls for the sqlite3_vfs methods. Maintenance reminder: members
|
||||
are in alphabetical order to simplify finding them.
|
||||
@ -790,6 +819,7 @@ const installOpfsVfs = function callee(asyncProxyUri = callee.defaultProxyUri){
|
||||
fh.sabView = state.sabFileBufView;
|
||||
fh.sq3File = new sqlite3_file(pFile);
|
||||
fh.sq3File.$pMethods = opfsIoMethods.pointer;
|
||||
fh.lockType = capi.SQLITE_LOCK_NONE;
|
||||
}
|
||||
mTimeEnd();
|
||||
return rc;
|
||||
@ -1061,5 +1091,12 @@ installOpfsVfs.defaultProxyUri =
|
||||
//self.location.pathname.replace(/[^/]*$/, "sqlite3-opfs-async-proxy.js");
|
||||
"sqlite3-opfs-async-proxy.js";
|
||||
//console.warn("sqlite3.installOpfsVfs.defaultProxyUri =",sqlite3.installOpfsVfs.defaultProxyUri);
|
||||
self.sqlite3ApiBootstrap.initializersAsync.push(async (sqlite3)=>installOpfsVfs());
|
||||
self.sqlite3ApiBootstrap.initializersAsync.push(async (sqlite3)=>{
|
||||
try{
|
||||
return installOpfsVfs();
|
||||
}catch(e){
|
||||
console.error("installOpfsVfs() exception:",e);
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
}/*sqlite3ApiBootstrap.initializers.push()*/);
|
||||
|
@ -1312,14 +1312,25 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap(
|
||||
// Is it okay to resolve these in parallel or do we need them
|
||||
// to resolve in order? We currently only have 1, so it
|
||||
// makes no difference.
|
||||
lip = lip.map((f)=>f(sqlite3).catch(()=>{}));
|
||||
lip = lip.map((f)=>f(sqlite3).catch((e)=>{
|
||||
console.error("An async sqlite3 initializer failed:",e);
|
||||
}));
|
||||
//let p = lip.shift();
|
||||
//while(lip.length) p = p.then(lip.shift());
|
||||
//return p.then(()=>sqlite3);
|
||||
return Promise.all(lip).then(()=>sqlite3);
|
||||
}
|
||||
};
|
||||
sqlite3ApiBootstrap.initializers.forEach((f)=>f(sqlite3));
|
||||
try{
|
||||
sqlite3ApiBootstrap.initializers.forEach((f)=>{
|
||||
f(sqlite3);
|
||||
});
|
||||
}catch(e){
|
||||
/* If we don't report this here, it can get completely swallowed
|
||||
up and disappear into the abyss of Promises and Workers. */
|
||||
console.error("sqlite3 bootstrap initializer threw:",e);
|
||||
throw e;
|
||||
}
|
||||
delete sqlite3ApiBootstrap.initializers;
|
||||
sqlite3ApiBootstrap.sqlite3 = sqlite3;
|
||||
return sqlite3;
|
||||
|
@ -41,6 +41,9 @@ if(self.window === self){
|
||||
}else if(!navigator.storage.getDirectory){
|
||||
toss("This API requires navigator.storage.getDirectory.");
|
||||
}
|
||||
|
||||
//warn("This file is very much experimental and under construction.",self.location.pathname);
|
||||
|
||||
/**
|
||||
Will hold state copied to this object from the syncronous side of
|
||||
this API.
|
||||
@ -97,8 +100,6 @@ metrics.dump = ()=>{
|
||||
console.log("Serialization metrics:",metrics.s11n);
|
||||
};
|
||||
|
||||
//warn("This file is very much experimental and under construction.",self.location.pathname);
|
||||
|
||||
/**
|
||||
Map of sqlite3_file pointers (integers) to metadata related to a
|
||||
given OPFS file handles. The pointers are, in this side of the
|
||||
@ -142,8 +143,7 @@ const getDirForFilename = async function f(absFilename, createDirs = false){
|
||||
/**
|
||||
Returns the sync access handle associated with the given file
|
||||
handle object (which must be a valid handle object), lazily opening
|
||||
it if needed. Timestamps the handle for use in relinquishing it
|
||||
during idle time.
|
||||
it if needed.
|
||||
|
||||
In order to help alleviate cross-tab contention for a dabase,
|
||||
if an exception is thrown while acquiring the handle, this routine
|
||||
@ -177,13 +177,12 @@ const getSyncHandle = async (fh)=>{
|
||||
}
|
||||
log("Got sync handle for",fh.filenameAbs,'in',performance.now() - t,'ms');
|
||||
}
|
||||
fh.syncHandleTime = performance.now();
|
||||
return fh.syncHandle;
|
||||
};
|
||||
|
||||
const closeSyncHandle = async (fh)=>{
|
||||
if(fh.syncHandle){
|
||||
//warn("Closing sync handle for",fh.filenameAbs);
|
||||
log("Closing sync handle for",fh.filenameAbs);
|
||||
const h = fh.syncHandle;
|
||||
delete fh.syncHandle;
|
||||
return h.close();
|
||||
@ -239,6 +238,7 @@ const wTimeEnd = ()=>(
|
||||
*/
|
||||
let flagAsyncShutdown = false;
|
||||
|
||||
|
||||
/**
|
||||
Asynchronous wrappers for sqlite3_vfs and sqlite3_io_methods
|
||||
methods. Maintenance reminder: members are in alphabetical order
|
||||
@ -373,6 +373,20 @@ const vfsAsyncImpls = {
|
||||
storeAndNotify('xFileSize', sz);
|
||||
mTimeEnd();
|
||||
},
|
||||
xLock: async function(fid,lockType){
|
||||
mTimeStart('xLock');
|
||||
const fh = __openFiles[fid];
|
||||
let rc = 0;
|
||||
if( !fh.syncHandle ){
|
||||
try { await getSyncHandle(fh) }
|
||||
catch(e){
|
||||
state.s11n.storeException(1,e);
|
||||
rc = state.sq3Codes.SQLITE_IOERR;
|
||||
}
|
||||
}
|
||||
storeAndNotify('xLock',rc);
|
||||
mTimeEnd();
|
||||
},
|
||||
xOpen: async function(fid/*sqlite3_file pointer*/, filename, flags){
|
||||
const opName = 'xOpen';
|
||||
mTimeStart(opName);
|
||||
@ -473,6 +487,23 @@ const vfsAsyncImpls = {
|
||||
storeAndNotify('xTruncate',rc);
|
||||
mTimeEnd();
|
||||
},
|
||||
xUnlock: async function(fid,lockType){
|
||||
mTimeStart('xUnlock');
|
||||
let rc = 0;
|
||||
const fh = __openFiles[fid];
|
||||
if( state.sq3Codes.SQLITE_LOCK_NONE===lockType
|
||||
&& fh.syncHandle ){
|
||||
try { await closeSyncHandle(fh) }
|
||||
catch(e){
|
||||
state.s11n.storeException(1,e);
|
||||
rc = state.sq3Codes.SQLITE_IOERR;
|
||||
/* Maybe we want to not report this? "Destructors do not
|
||||
throw." */
|
||||
}
|
||||
}
|
||||
storeAndNotify('xUnlock',rc);
|
||||
mTimeEnd();
|
||||
},
|
||||
xWrite: async function(fid,n,offset){
|
||||
mTimeStart('xWrite');
|
||||
let rc;
|
||||
@ -495,7 +526,7 @@ const vfsAsyncImpls = {
|
||||
storeAndNotify('xWrite',rc);
|
||||
mTimeEnd();
|
||||
}
|
||||
};
|
||||
}/*vfsAsyncImpls*/;
|
||||
|
||||
const initS11n = ()=>{
|
||||
/**
|
||||
@ -617,21 +648,7 @@ const waitLoop = async function f(){
|
||||
We need to wake up periodically to give the thread a chance
|
||||
to do other things.
|
||||
*/
|
||||
const waitTime = 500;
|
||||
/**
|
||||
relinquishTime defines the_approximate_ number of ms after which
|
||||
a db sync access handle will be relinquished so that we do not
|
||||
hold a persistent lock on it. When the following loop times out
|
||||
while waiting, every (approximate) increment of this value it
|
||||
will relinquish any db handles which have been idle for at least
|
||||
this much time.
|
||||
|
||||
Reaquisition of a sync handle seems to take an average of
|
||||
0.6-0.9ms on this dev machine but takes anywhere from 1-3ms every
|
||||
once in a while (maybe 1 time in 5 or 10). Outliers as long as
|
||||
7ms have been witnessed, but they're rare.
|
||||
*/
|
||||
const relinquishTime = 500;
|
||||
const waitTime = 1000;
|
||||
let lastOpTime = performance.now();
|
||||
let now;
|
||||
while(!flagAsyncShutdown){
|
||||
@ -639,21 +656,6 @@ const waitLoop = async function f(){
|
||||
if('timed-out'===Atomics.wait(
|
||||
state.sabOPView, state.opIds.whichOp, 0, waitTime
|
||||
)){
|
||||
if(relinquishTime &&
|
||||
(lastOpTime + relinquishTime <= (now = performance.now()))){
|
||||
for(const fh of Object.values(__openFiles)){
|
||||
if(fh.syncHandle && (
|
||||
now - relinquishTime >= fh.syncHandleTime
|
||||
)){
|
||||
log("Relinquishing for timeout:",fh.filenameAbs);
|
||||
await closeSyncHandle(fh)
|
||||
/* Testing shows that we have to wait on this async
|
||||
op to finish, else we might try to re-open it
|
||||
before the close has run. The FS layer does not
|
||||
retain the order those operations, apparently. */;
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
lastOpTime = performance.now();
|
||||
@ -719,4 +721,4 @@ navigator.storage.getDirectory().then(function(d){
|
||||
}
|
||||
};
|
||||
wMsg('opfs-async-loaded');
|
||||
}).catch((e)=>error(e));
|
||||
}).catch((e)=>error("error initializing OPFS asyncer:",e));
|
||||
|
@ -34,10 +34,6 @@ const tryOpfsVfs = async function(sqlite3){
|
||||
const wait = async (ms)=>{
|
||||
return new Promise((resolve)=>setTimeout(resolve, ms));
|
||||
};
|
||||
const waitForRelinquish = async ()=>{
|
||||
log("Waiting briefly to test sync handle relinquishing...");
|
||||
return wait(1500);
|
||||
};
|
||||
|
||||
const urlArgs = new URL(self.location.href).searchParams;
|
||||
const dbFile = "my-persistent.db";
|
||||
@ -45,13 +41,11 @@ const tryOpfsVfs = async function(sqlite3){
|
||||
|
||||
const db = new opfs.OpfsDb(dbFile,'ct');
|
||||
log("db file:",db.filename);
|
||||
await waitForRelinquish();
|
||||
try{
|
||||
if(opfs.entryExists(dbFile)){
|
||||
let n = db.selectValue("select count(*) from sqlite_schema");
|
||||
log("Persistent data found. sqlite_schema entry count =",n);
|
||||
}
|
||||
await waitForRelinquish();
|
||||
db.transaction((db)=>{
|
||||
db.exec({
|
||||
sql:[
|
||||
@ -63,7 +57,6 @@ const tryOpfsVfs = async function(sqlite3){
|
||||
(performance.now() |0) / 4]
|
||||
});
|
||||
});
|
||||
await waitForRelinquish();
|
||||
log("count(*) from t =",db.selectValue("select count(*) from t"));
|
||||
|
||||
// Some sanity checks of the opfs utility functions...
|
||||
|
Reference in New Issue
Block a user