Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions indexes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ indexes:
key:
filename: 1

- collection: pending
key: gfs # actual collection name is options[gfs].pending
index:
name: mailpendingexpires
key:
expires: 1

- collection: suppressionlist
index:
name: suppressed_address
Expand Down
20 changes: 16 additions & 4 deletions lib/api-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,29 @@ class APIServer {
return next();
}

this.queue.store(id, req, err => {
this.queue.store(id, req, (err, storedId, filesId) => {
if (err) {
res.json(500, {
error: err.message
});
} else {
return next();
}

// Nothing queues what this endpoint stores, so the marker ends with the upload.
// A marker left behind has the GC remove the body, so say so rather than 200.
this.queue.clearPending(filesId, pendingErr => {
if (pendingErr) {
log.error('API', '%s stored but its marker could not be cleared, so it will be collected. %s', id, pendingErr.message);
res.json(500, {
error: pendingErr.message
});
return next();
}
res.json(200, {
success: true
});
}
next();
next();
});
});
});

Expand Down
187 changes: 187 additions & 0 deletions lib/gridfs-pending.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
'use strict';

const log = require('npmlog');

// Marker bookkeeping for GridFS operations, shared by both queue implementations.
//
// GridFS writes chunks as they arrive but inserts the files document only when the upload
// finishes. A 'writing' marker records files_id before the first chunk, so an interrupted
// upload remains discoverable even if it never produces a files document. Its heartbeat
// continues through scanning, metadata updates and queue insertion, which write no chunks.
//
// A 'releasing' marker is written before removing a delivery. It covers the gap between the
// last delivery disappearing and body deletion starting. A 'deleting' marker then covers
// removal of the chunks followed by the files document. Markers for shared bodies remain
// until the body is removed, because another delivery release may already depend on them.
// A slow upload also keeps its writing marker after queueing: its older files_id must keep
// bounding the age-based GC watermark even though the delivery documents are much newer.
//
// Each marker carries an absolute deadline renewed by the process holding the operation.
// A collector in another process needs only that deadline and the clock; it does not need
// to know the writer's configuration. If the writer dies or its event loop stops running,
// its timer cannot renew the marker, so cleanup does not depend on a shutdown handler.

const HEARTBEAT_INTERVAL = 60 * 1000;

// The three-interval lease tolerates two missed beats before collection can claim an
// operation. A successful renewal extends it; an expired lease gives the collector ownership.
const MARKER_TTL = 3 * HEARTBEAT_INTERVAL;

// The coarse GC subtracts this from its watermark. An upload that predates queue insertion
// by this much needs its marker after queueing too, or the delivery's newer _id cannot protect it.
const GC_GRACE_PERIOD = 10 * 60 * 1000;

// Heartbeats in flight in this process, by marker id. Module level because the id is unique and
// because both queues, and the API path, hand the same marker between them.
const heartbeats = new Map();

function key(filesId) {
return String(filesId);
}

function deadline() {
return new Date(Date.now() + MARKER_TTL);
}

function writeMarker(mongodb, gfs, filesId, state, callback) {
// Upserted rather than inserted: a delete of a file whose upload marker was never
// cleared has to take the marker over instead of colliding with it.
// An upsert against a claimed marker fails on its unique _id rather than taking
// cleanup away from the collector.
mongodb.collection(gfs + '.pending').updateOne(
{ _id: filesId, collecting: { $exists: false } },
{ $set: { state, expires: deadline() } },
{ upsert: true },
err => callback(err)
);
}

/**
* Pushes an existing marker's deadline forward, and reports whether there was one
*
* Deliberately not an upsert. The operation an id belongs to can be ended by a different process
* than the one beating for it - a child hands its cleanup to the master over IPC - and a beat
* that could create would raise the marker from the dead there and then go on beating for it
* forever. Only extending means the beat has nothing to say once the marker is gone, which is
* also how it learns to stop.
*/
function beat(mongodb, gfs, filesId, callback) {
mongodb.collection(gfs + '.pending').updateOne({ _id: filesId, collecting: { $exists: false } }, { $set: { expires: deadline() } }, (err, result) => {
if (err) {
return callback(err);
}
callback(null, !!result && result.matchedCount > 0);
});
}

/**
* Records that an upload or a delete is in flight, and starts its heartbeat
*
* @param {Object} mongodb Database handle
* @param {String} gfs Bucket name, so the collection is <gfs>.pending
* @param {ObjectId} filesId Identifier of the GridFS file
* @param {String} state 'writing', 'releasing', or 'deleting'
* @param {Function} callback
* @param {Function} [onLost] Aborts an upload whose marker was claimed or removed
*/
function markPending(mongodb, gfs, filesId, state, callback, onLost) {
writeMarker(mongodb, gfs, filesId, state, err => {
if (err) {
return callback(err);
}

stopPending(filesId);

let timer = setInterval(() => {
beat(mongodb, gfs, filesId, (err, found) => {
// Stopping a timer does not cancel a Mongo request already in flight. An old
// response must not stop a new operation's heartbeat or call its former owner.
if (heartbeats.get(key(filesId)) !== timer) {
return;
}
if (err) {
// Leave the last deadline in place. Later beats can recover, but if no
// renewal succeeds before expiry the collector may claim the operation.
return log.info('Queue', 'Failed to refresh the marker for %s. %s', filesId, err.message);
}
if (!found) {
// The operation was completed elsewhere or claimed for collection.
stopPending(filesId);
if (onLost) {
onLost(new Error('GridFS operation lost its pending marker'));
}
}
});
}, HEARTBEAT_INTERVAL);

// Nothing here is worth holding the process open for.
timer.unref();
heartbeats.set(key(filesId), timer);

callback();
});
}

/**
* Stops the heartbeat but leaves the marker, for an operation that failed partway
*
* The chunks it wrote are exactly what the collector is looking for, so the marker has to be
* allowed to go stale rather than be taken back.
*
* @param {ObjectId} filesId Identifier of the GridFS file
*/
function stopPending(filesId) {
let timer = heartbeats.get(key(filesId));
if (timer) {
clearInterval(timer);
heartbeats.delete(key(filesId));
}
}

/**
* Removes the marker for a completed upload or delete, and stops its heartbeat
*
* @param {Object} mongodb Database handle
* @param {String} gfs Bucket name, so the collection is <gfs>.pending
* @param {ObjectId} filesId Identifier of the GridFS file
* @param {Function} callback
* @param {String} [state] Expected marker state
*/
function clearPending(mongodb, gfs, filesId, callback, state) {
stopPending(filesId);
let query = { _id: filesId, collecting: { $exists: false } };
if (state) {
// A delivery may finish before its upload caller clears the writing marker. That
// caller must not remove a releasing marker now protecting the delivery's release.
query.state = state;
}
mongodb.collection(gfs + '.pending').deleteOne(query, (err, result) => {
callback(err || (!result.deletedCount ? new Error('GridFS operation lost its pending marker') : null));
});
}

/**
* Ends the upload heartbeat once deliveries own the body
*
* Usually the writing marker can now be removed. A slow upload has an older files_id than
* its delivery documents, though, and can fall below the age-based GC watermark immediately.
* Keep its marker until the last delivery is released so that pass still accounts for the body.
* The store-only API uses clearPending directly because it does not create deliveries.
*/
function completePending(mongodb, gfs, filesId, callback) {
if (filesId.getTimestamp().getTime() <= Date.now() - GC_GRACE_PERIOD) {
stopPending(filesId);
return setImmediate(callback);
}
clearPending(mongodb, gfs, filesId, callback, 'writing');
}

// Renew immediately before reporting storage success or handing the body to the queue.
// A claimed marker is fenced out even if the next interval has not fired yet.
function renewPending(mongodb, gfs, filesId, callback) {
beat(mongodb, gfs, filesId, (err, found) => {
callback(err || (!found ? new Error('GridFS operation lost its pending marker') : null));
});
}

module.exports = { renewPending, markPending, clearPending, completePending, stopPending, HEARTBEAT_INTERVAL, MARKER_TTL, GC_GRACE_PERIOD };
83 changes: 83 additions & 0 deletions lib/gridfs-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use strict';

const gridfsPending = require('./gridfs-pending');

// Shared by the local API queue and the SMTP child's remote queue. The upload heartbeat
// continues after finish until the caller queues the message or explicitly releases it.
// A marker can remain after that handoff when the queued body still needs age-based GC protection.
module.exports = (queue, gfs, id, source, callback, onSourceError) => {
let store = queue.gridstore.openUploadStream('message ' + id, {
contentType: 'message/rfc822',
metadata: { created: new Date() }
});
let returned = false;
let sourceError;

const done = err => {
if (returned) {
return;
}
returned = true;
if (err) {
source.unpipe(store);
gridfsPending.stopPending(store.id);
}
callback(err, id, store.id);
};

source.once('error', err => {
if (returned || sourceError) {
return;
}
sourceError = err;
source.unpipe(store);
gridfsPending.stopPending(store.id);
if (onSourceError) {
onSourceError(err);
}
// Finish the partial upload so removeMessage can find it. If this also fails, the
// error handler must still notify the caller; the marker covers any leftover chunks.
store.end();
});

store.on('error', err => done(sourceError || err));
store.once('finish', () => {
if (returned) {
return;
}
if (sourceError) {
return queue.removeMessage(id, () => done(sourceError));
}
// The files document now exists, but a stored message without a delivery still
// belongs to nobody. Keep the heartbeat running through scanning and queue insertion.
// Return files_id so the caller can complete the marker only after that handoff succeeds.
// Renew here as well: collection may have claimed the marker before the next beat.
gridfsPending.renewPending(queue.mongodb, gfs, store.id, done);
});

// The pipe waits for the marker write, so hold the source until then. Callers normally
// pass a paused stream, but a source already flowing would otherwise lose data while
// Mongo acknowledges the marker.
source.pause();

// openUploadStream assigns files_id immediately, but GridFS does not insert the files
// document until finish. Write the marker before piping the first chunk so the collector
// can distinguish an abandoned upload from a live one throughout that window.
gridfsPending.markPending(queue.mongodb, gfs, store.id, 'writing', err => {
if (returned || sourceError) {
// The source failed while the marker write was pending. Its error handler stopped
// before this heartbeat existed, so this callback must stop the newly created timer.
gridfsPending.stopPending(store.id);
return;
}
if (err) {
// Nothing was piped into the upload, and nobody will end the unused handle now.
store.destroy();
return done(err);
}
source.pipe(store);
}, err => {
source.unpipe(store);
store.destroy(err);
});
};
Loading
Loading