Skip to content

Commit

Permalink
Concurrency bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed May 15, 2024
1 parent 6741d67 commit 3f6f3c0
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 56 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ bunfig.toml
lab/
lab.ts
mydatabase*/
stress-test/

# Pup data
.pup
.pup
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/kv",
"version": "0.0.13",
"version": "0.9.0",
"exports": {
".": "./mod.ts"
},
Expand All @@ -13,7 +13,7 @@
"cbor-x": "npm:cbor-x@^1.5.9"
},
"publish": {
"exclude": [".github", "**/*.test.ts", "**/*.bench.ts", "stress-test"]
"exclude": [".github", "**/*.test.ts", "**/*.bench.ts"]
},
"tasks": {
"check": "deno fmt --check && deno lint && deno check mod.ts && deno doc --lint mod.ts && deno run -A mod.ts --slim --ignore-unused",
Expand Down
11 changes: 5 additions & 6 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Configurable
export const LOCK_DEFAULT_MAX_RETRIES = 32;
export const LOCK_DEFAULT_INITIAL_RETRY_INTERVAL_MS = 20; // Increased with itself on each retry, so the actual retry interval is 20, 40, 60 etc. 32 and 20 become about 10 seconds total.
export const LOCK_STALE_TIMEOUT_S = 60_000;

export const SUPPORTED_LEDGER_VERSIONS = ["ALPH"];

export const LEDGER_BASE_OFFSET = 1_024;

export const LEDGER_MAX_READ_FAILURES = 10;

export const LEDGER_PREFETCH_BYTES = 2_048;
export const SYNC_INTERVAL_MS = 1_000; // Overridable with instance configuration

export const SYNC_INTERVAL_MS = 1_000;
// Extremely constant
export const LEDGER_BASE_OFFSET = 1_024; // DO NOT CHANGE!
export const KV_KEY_ALLOWED_CHARS = /^[a-zA-Z0-9\-_@]+$/;
5 changes: 1 addition & 4 deletions src/key.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/**
* Regular expression defining the allowed characters in a KV key string.
*/
export const KV_KEY_ALLOWED_CHARS = /^[a-zA-Z0-9\-_@]+$/;
import { KV_KEY_ALLOWED_CHARS } from "./constants.ts";

/**
* Represents a range within a key.
Expand Down
2 changes: 1 addition & 1 deletion src/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ export class KV extends EventEmitter {
*
* @throws {Error} If the transaction fails or if there's an issue updating the index or data files.
*/
async runTransaction(
private async runTransaction(
pendingTransaction: KVTransaction,
): Promise<void> {
this.ensureOpen();
Expand Down
6 changes: 4 additions & 2 deletions src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,15 @@ export class KVLedger {
transaction: KVTransaction,
doLock: boolean = true,
): Promise<number> {
const offset = this.header.currentOffset;

// Compose the transaction before locking to reduce lock time
const transactionData = await transaction.toUint8Array();

if (doLock) await lock(this.dataPath);

// Update internal offset
await this.readHeader(false);
const offset = this.header.currentOffset;

let fd;
try {
fd = await rawOpen(this.dataPath, true);
Expand Down
28 changes: 0 additions & 28 deletions stress-test/kvtest.ts

This file was deleted.

12 changes: 0 additions & 12 deletions stress-test/pup.json

This file was deleted.

0 comments on commit 3f6f3c0

Please sign in to comment.