Skip to content

Commit

Permalink
Extended logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
paberr committed Dec 15, 2017
1 parent 5339e2b commit d7ce1fc
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/main/generic/CombinedTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,9 @@ class CombinedTransaction {
get objectStore() {
throw new Error('Unsupported operation');
}

toString() {
return `CombinedTransaction{size=${this._transactions.length}, states=[${this._transactions.map(tx => tx.state)}]}`;
}
}
Class.register(CombinedTransaction);
12 changes: 12 additions & 0 deletions src/main/generic/ObjectStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ class ObjectStore {

// Create new layer on stack (might be immediately removed by a state flattening).
if (this._stateStack.length >= ObjectStore.MAX_STACK_SIZE) {
Log.e(ObjectStore, `Transaction stack size exceeded ${this.toStringFull()}`);
throw new Error('Transaction stack size exceeded');
}
this._stateStack.push(tx);
Expand Down Expand Up @@ -566,6 +567,17 @@ class ObjectStore {
}
return this.__backend.close();
}

toStringFull() {
return `ObjectStore{
stack=[${this._stateStack.map(tx => tx.toStringShort())}],
db=${this._db}
}`;
}

toString() {
return `ObjectStore{stackSize=${this._stateStack.length}, db=${this._db}}`;
}
}
/** @type {number} The maximum number of states to stack. */
ObjectStore.MAX_STACK_SIZE = 10;
Expand Down
14 changes: 9 additions & 5 deletions src/main/generic/Transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Transaction {
/**
* This constructor should only be called by an ObjectStore object.
* Our transactions have a watchdog enabled by default,
* aborting them after a certain time specified by WATCHDOG_TIMER.
* logging a warning after a certain time specified by WATCHDOG_TIMER.
* This helps to detect unclosed transactions preventing to store the state in
* the persistent backend.
* @param {ObjectStore} objectStore The object store this transaction belongs to.
Expand All @@ -21,7 +21,7 @@ class Transaction {
* @param {ICommittable} [managingBackend] The object store managing the transactions,
* i.e., the ObjectStore object.
* @param {boolean} [enableWatchdog] If this is is set to true (default),
* transactions will be automatically aborted if left open for longer than WATCHDOG_TIMER.
* a warning will be logged if left open for longer than WATCHDOG_TIMER.
* @protected
*/
constructor(objectStore, backend, managingBackend, enableWatchdog=true) {
Expand Down Expand Up @@ -53,7 +53,7 @@ class Transaction {
this._enableWatchdog = enableWatchdog;
if (this._enableWatchdog) {
this._watchdog = setTimeout(() => {
Log.w(Transaction, `Violation: tx id ${this._id} ${this._modified.size+this._removed.size} entries${this._truncated ? ' and truncate' : ''}) took longer than expected (still open after ${Transaction.WATCHDOG_TIMER/1000}s).`);
Log.w(Transaction, `Violation: tx id ${this._id} took longer than expected (still open after ${Transaction.WATCHDOG_TIMER/1000}s), ${this.toString()}.`);
}, Transaction.WATCHDOG_TIMER);
}
}
Expand Down Expand Up @@ -253,7 +253,7 @@ class Transaction {
const executionTime = Date.now() - startTime;
functionName = functionName ? ` function '${functionName}'` : '';
if (executionTime > Transaction.WATCHDOG_TIMER) {
Log.w(Transaction, `Violation: tx id ${this._id}${functionName} ${this._modified.size+this._removed.size} entries${this._truncated ? ' and truncate' : ''}) took ${(executionTime/1000).toFixed(2)}s.`);
Log.w(Transaction, `Violation: tx id ${this._id}${functionName} took ${(executionTime/1000).toFixed(2)}s (${this.toString()}).`);
}
}

Expand Down Expand Up @@ -816,7 +816,11 @@ class Transaction {
}

toString() {
return `${this._id}`;
return `Transaction{id=${this._id}, changes=±${this._modified.size+this._removed.size}, truncated=${this._truncated}, objectStore=${this._objectStore}, state=${this._state}, dependency=${this._dependency}}`;
}

toStringShort() {
return `Transaction{id=${this._id}, changes=±${this._modified.size+this._removed.size}, truncated=${this._truncated}, state=${this._state}, dependency=${this._dependency}}`;
}
}
/** @type {number} Milliseconds to wait until automatically aborting transaction. */
Expand Down
4 changes: 4 additions & 0 deletions src/main/platform/browser/JungleDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,9 @@ class JungleDB {
const ctx = new CombinedTransaction(...txs);
return ctx.commit();
}

toString() {
return `JungleDB{name=${this._databaseDir}}`;
}
}
Class.register(JungleDB);
4 changes: 4 additions & 0 deletions src/main/platform/nodejs/JungleDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ class JungleDB {
const ctx = new CombinedTransaction(...txs);
return ctx.commit();
}

toString() {
return `JungleDB{name=${this._databaseDir}}`;
}
}
/**
* A LevelDB JSON encoding that can handle Uint8Arrays and Sets.
Expand Down
4 changes: 4 additions & 0 deletions src/test/generic/DummyBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,9 @@ class DummyBackend {
transaction() {
throw 'Unsupported operation';
}

toString() {
return 'DummyBackend{}';
}
}
JDB.Class.register(DummyBackend);
21 changes: 21 additions & 0 deletions src/test/generic/ObjectStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,25 @@ describe('ObjectStore', () => {
expect(tx4.state).toBe(JDB.Transaction.STATE.ABORTED);
})().then(done, done.fail);
});

it('throws error when stack size is exceeded', (done) => {
(async function () {
let txC;

for (let i=0; i<JDB.ObjectStore.MAX_STACK_SIZE; ++i) {
objectStore.transaction();
txC = objectStore.transaction();
await txC.commit();
}

// Another transaction should throw a detailed error.
objectStore.transaction();
txC = objectStore.transaction();
let threw = false;
await txC.commit().catch(() => {
threw = true;
});
expect(threw).toBe(true);
})().then(done, done.fail);
});
});

0 comments on commit d7ce1fc

Please sign in to comment.