0.9.3 • Published 8 years ago

wal v0.9.3

Weekly downloads
5
License
MIT
Repository
github
Last release
8 years ago

write-ahead-log Circle CI

An implementation of write-ahead logging (WAL) for nodejs.

Why

Write-ahead logging (WAL) is a building block used to improve automicity and durability in distributed systems. WAL improves these properties by providing persistent, sequenced storage for Log Entries as well as a record of which Log Entries have been committed. Since networks, software systems, and storage devices are unreliable, write-ahead logging also provides a mechanism to recover from failure.

Keep it Simple

wal is a simple, abstract, write-ahead log. It won't provide much value unless you define a semantic for your application.

In our implementation, Log Entries are opaque binary values, defined and interpreted by you. Likewise, what it means to commit is also defined by you. For example, if you were to use wal in a data replication utility, committed may mean the Log Entry has been successfully distributed and acknowledged by your replicas.

We also define Log Serial Number (LSN) as an integer identfier numbered from 0 (zero) and incremented for each Log Entry. Our implementation guarantees that committed LSNs will never repeat or be re-issued.

We provide a simple recovery mechanism that visits each uncommitted entry, in order, and commits each, as long as the handler you supply indicates it is safe to do so by returning a truthy value; when your handler returns a falsy value, remaining uncommitted Log Entries are truncated. The LSN/index associated with those uncommitted Log Entries will be re-issued whan a subsequent Log Entry is made. This scheme enables our algorithm to be simple and fast. If your code requires guaranteed unique LSNs across restarts (a requirement for most database systems), we feel we have provided the collaboration semantics necessary for you to implement such guarantees on top of wal as an additional level of LSN indirection.

Install

npm install wal 

Use

es5

var WriteAheadLog = require('wal').WriteAheadLog;

es6

import { WriteAheadLog } from 'wal';

Write-Work-Commit Cycle

Write-ahead logging is accomplished through in a write-work-commit cycle.

  • First write a log entry containing enough information to describe the activity and recover it if there is a subsequent failure,
  • Second perform the work,
  • Third when the activity is completed, commit the log entry; otherwise, if the activity cannot be completed, truncate the log at the log entry's LSN.
const WriteAheadLog = require('wal');
const stdout = console.log.bind(console);
const stderr = console.error.bind(console);

function job(jobId) {
  // this is a simulation, you would probably do something more interesting.
  return new Promise((resolve) => {
    stdout(`performing job: ${jobId}`);
    setTimeout(() => {
      stdout(`done with job: ${jobId}`);
      resolve();
    }, 1000);
  });
}

const path = __filename + '.wal';
const writable = true;

WriteAheadLog.openOrCreate({ path, writable })
  // usually when you open a log you'll want to run recovery in case of prior failure.
  //   here we're telling wal to truncate all uncommitted entries.
  .then(wal => wal.recover(false))
  .then(wal => {
    let committed = wal.commitHead;

    // We'll just use the LSN as our job number...
    let jobId = committed + 1;
    let data = new Buffer(`job #${jobId}\r\n`);

    // 1. Log some data; this data is opaque to the log...
    return wal.write(data)
      .then(lsn => {

        // 2. Do the actual work...
        return job(jobId).then(() => {

          // 3. Commit the log up to the LSN.
          return wal.commit(lsn)
            .then(() => {
              stdout(`committed job: ${jobId}`);
            });
        });
      })
      // perform a normal close.
      .then(() => wal.close());
  })
  .catch(err => stderr('' + err));

Chaos can occur at any time, for an illustration of simple failure and recovery, run the example script chaotic-operations and monitor what happens in both the log and index. This should give you a feel for how to scaffold on top of wal.

Sequencing

wal imposes sequencing of commits; LSN/indexes must be committed in order. Obviously parallel operations, such as those waiting on IO will often complete out of order. It is the responsibility of the caller to ensure that out of order completion is resequenced before being applied to the log for commit.

More Documentation

Detailed documentation can be generated locally using npm scripts:

git clone git@github.com:LeisureLink/write-ahead-log.git
cd write-ahead-log
npm install && npm run doc
open doc/index.html

Module API

Classes:

  • WriteAheadLog : class a simple write-ahead logging implementation.

Methods:

  • .create(options) creates a write-ahead log using the specified options.
  • .open(options) opens an existing a write-ahead log using the specified options.
  • .openOrCreate(options) opens a specified write-ahead log if it exists; otherwise creates and opens the log.

WriteAheadLog Class

An encapsulation of write-ahead logging behavior.

Properties:

  • .name the name of the log file (fully qualified path).
  • .index the name of the log file's index (fully qualified path).
  • .writable indicates whether the log was opened in a writable mode.
  • .size the log files size in bytes.
  • .head the log's next LSN/index (the write head).
  • .commitHead the LSN/index of the most recently committed log entry.

Methods:

  • .close() performs a normal close.
  • .commit(index) commits the specified LSN/index.
  • .isCommitted(index) determines if the specified LSN/index has been committed.
  • .read(index) reads the log entry at the specified LSN/index.
  • .readRange(first, count) streams the log entries starting at the first specified LSN/index until the specified number of log entries have been returned.
  • .recover(handler) performs recovery logic against uncommitted log entries.
  • .truncate(from) truncates uncommitted log entries starting at the specified LSN/index.
  • .write(data) writes the specified data buffer next log entry.

License

MIT