Repository navigation
Fix file adapter to handle large files #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // | ||
| // Simple Juttle adapter that implements a single file "database". | ||
| // | ||
| // The "file" option indicates a file to read/write from and it must contain | ||
| // a JSON array of objects. | ||
| // | ||
| // The filters passed in the read parameterrs are executed to filter the points | ||
| // from the file. | ||
|
|
||
| function FileAdapter(config, Juttle) { | ||
| return { | ||
| name: 'file', | ||
| read: require('./read'), | ||
| write: require('./write') | ||
| }; | ||
| } | ||
|
|
||
| module.exports = FileAdapter; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| var _ = require('underscore'); | ||
| var Juttle = require('../../runtime/index').Juttle; | ||
| var Promise = require('bluebird'); | ||
| var fs = Promise.promisifyAll(require('fs')); | ||
|
|
||
| var Write = Juttle.proc.sink.extend({ | ||
| procName: 'write-file', | ||
| initialize: function(options, params) { | ||
| var allowed_options = ['file', 'bufferLimit', 'maxFilesize', 'flushFrequency']; | ||
| var unknown = _.difference(_.keys(options), allowed_options); | ||
| if (unknown.length > 0) { | ||
| throw this.compile_error('RT-UNKNOWN-OPTION-ERROR', { | ||
| proc: 'write file', | ||
| option: unknown[0] | ||
| }); | ||
| } | ||
|
|
||
| if (!_.has(options, 'file')) { | ||
| throw this.compile_error('RT-MISSING-OPTION-ERROR', { | ||
| proc: 'write file', | ||
| option: "file" | ||
| }); | ||
| } | ||
|
|
||
| this.bufferLimit = options.bufferLimit || 100000; | ||
| if (!_.isNumber(this.bufferLimit) || this.bufferLimit < 0 ) { | ||
| throw this.compile_error('RT-OPTION-SHOULD-BE-POSITIVE-INTEGER', { | ||
| option: "bufferLimit" | ||
| }); | ||
| } | ||
|
|
||
| // 200MB bufferLimit on file | ||
| this.maxFilesize = options.maxFilesize || 200*1024*1024; | ||
| if (!_.isNumber(this.maxFilesize) || this.maxFilesize < 0 ) { | ||
| throw this.compile_error('RT-OPTION-SHOULD-BE-POSITIVE-INTEGER', { | ||
| option: "maxFilesize" | ||
| }); | ||
| } | ||
|
|
||
| // flush every 100 points | ||
| this.flushFrequency = options.flushFrequency ? options.flushFrequency : 100; | ||
| if (!_.isNumber(this.flushFrequency) || this.flushFrequency < 0 ) { | ||
| throw this.compile_error('RT-OPTION-SHOULD-BE-POSITIVE-INTEGER', { | ||
| option: "flushFrequency" | ||
| }); | ||
| } | ||
|
|
||
| this.filename = options.file; | ||
| this.queue = []; | ||
| }, | ||
|
|
||
| process: function(points) { | ||
| this.logger.debug('process', points); | ||
| this.queue = this.queue.concat(points); | ||
| // if we're due for a flush and don't have a flush in course then | ||
| // fire off another flush | ||
| if (this.queue.length > this.flushFrequency && !this.flushPromise) { | ||
| this.flush(); | ||
| } | ||
| }, | ||
|
|
||
| flush: function() { | ||
| var self = this; | ||
| var points = []; | ||
|
|
||
| this.flushPromise = fs.statAsync(this.filename) | ||
| .then(function(stats) { | ||
| if (stats.size > self.maxFilesize) { | ||
| throw self.runtime_error('RT-OPTION-VALUE-EXCEEDED', { | ||
| option: 'maxFilesize', | ||
| value: self.maxFilesize + ' bytes' | ||
| }); | ||
| } | ||
| // we can only read the file if it exists which would be here | ||
| // and only after verifying the maxFileSize has not been | ||
| // exceeded | ||
| return fs.readFileAsync(self.filename, 'utf8') | ||
| .then(function(data) { | ||
| points = JSON.parse(data); | ||
| }); | ||
| }) | ||
| .catch(function(err) { | ||
| // ignore the ENOENT since that means we don't have an output | ||
| // file to begin with | ||
| if (err.code !== 'ENOENT') { | ||
| throw err; | ||
| } | ||
| }) | ||
| .then(function() { | ||
| if (points.length > self.bufferLimit) { | ||
| self.trigger('error', self.runtime_error('RT-OPTION-VALUE-EXCEEDED', { | ||
| option: 'bufferLimit', | ||
| value: self.bufferLimit, | ||
| extra: ', droppping points.' | ||
| })); | ||
| return Promise.resolve(); | ||
| } else { | ||
| var space_left = self.bufferLimit - points.length; | ||
| if (space_left < self.queue.length) { | ||
| points = points.concat(self.queue.slice(0, space_left)); | ||
| // error since we really can't take any more points after this and | ||
| // should stop the whole pipeline | ||
| self.trigger('error', self.runtime_error('RT-OPTION-VALUE-EXCEEDED', { | ||
| option: 'bufferLimit', | ||
| value: self.bufferLimit, | ||
| extra: ', droppping points.' | ||
| })); | ||
| } else { | ||
| // lets write out some more points | ||
| points = points.concat(self.queue); | ||
| } | ||
| var data = JSON.stringify(points, null, 4); | ||
| self.queue = []; | ||
| return fs.writeFileAsync(self.filename, data); | ||
| } | ||
| }) | ||
| .catch(function(err) { | ||
| self.trigger('error', err); | ||
| }).finally(function() { | ||
| self.flushPromise = null; | ||
| }); | ||
|
|
||
| return this.flushPromise; | ||
| }, | ||
|
|
||
| eof: function() { | ||
| this.logger.debug('eof'); | ||
| var self = this; | ||
| var currentFlush = this.flushPromise || Promise.resolve(); | ||
| currentFlush.then(function() { | ||
| // after the current flush is done lets make sure to fire off | ||
| // another one as the previous one could have been somewhere | ||
| // in the middle of the async write and without another flush we | ||
| // would leave points on the self.queue | ||
| return self.flush(); | ||
| }) | ||
| .then(function() { | ||
| self.done(); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| module.exports = Write; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A thing to keep in mind (we don't have to fix it now) is that this introduces a potential race condition - other file sink / other process could do a write while we keep the points in memory, and we'd then overwrite that write's content.