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
Dataloss possible when system crashes during loadDatabase #92
Comments
Hello, This is actually a very good point, I hadn't considered this case. Your solution is also the first idea I had, I need a bit more thought on this but that's definitely something I want to do. Thanks ! |
This would break hard links. But it prevents copying the whole file. This is how emacs behaves more or less so I guess it's acceptable. |
Hah, this just happened to me in real life. But this proves this is real issue, not just theoretical speculation. |
+1 to implement this |
I've been planning to do it for the last month but your last message gave me the nudge :) I'm on it. |
I finally had the time to code this :) Since this is a crucial part of NeDB and you are the two would raised this issue, it would be great if you two could take a look at the code and tell me if you think there is a problem! The branch is The two parts of the failsafe
There is also a test that simulates a crash during a I'm planning on merging this in master and releasing the new version on Thursday, if you don't have time to check this no worries, it is well tested. Cheers, |
Thanks a lot. I will look into it tomorrow. |
Looks good! |
Ideally, it would be interesting to dig into vim/emacs startegy to see if they lock the files while reading/moving/writing them. I guess that's not a "necessity" as you explicitely say that nedb does not support parallel accesses to a DB file. In any case, I'm looking forward to seeing this merged in master! |
I have few thought about testing suite.
|
1- Perfectly possible indeed! You're right. |
@spolu it solves nothing I think. If you have malformed files with data inside there is no other way to tell which one to use as by checking which is the newest and integral at the same time. In nedb case this is actually hard, because data is always appended to the end of file, so you can't tell which should be the trully last line of data just by looking at it. |
@szwacz in that very case it does solve the problem. If the file is incomplete it means that the final The fact that the |
Ok, now I understand. You want to: Saving:
Initial reading:
So the additional rename works as ensure step that file is complete. It indeed might work. |
That's it! |
Hi, Same issue here : var Datastore = require('nedb'), db = new Datastore('database.db'); //database = 5ko
db.loadDatabase(); //database = 0ko |
@FlorianBruniaux I'm pretty certain snippet you provided is not enough to cause any problem. If you have similar issues please tell more about how it happens. |
Hi, here is my whole snippet : var requirejs = require('requirejs'),
Datastore = require('nedb'), db = new Datastore('database.db');
requirejs.config({
nodeRequire: require
});
requirejs([
], function () {
exports.colisages = function(req, res) {
db.loadDatabase();
db.find({}, function (err, docs) {
res.send(docs);
console.log(docs);
});
};
}); |
I think it have nothing to do with this topic :). |
Yeah, exact. Sorry' But is it normal that this single line : Datastore = require('nedb'), db = new Datastore({ filename: 'database.db', autoload: true }); delete the whole data of database.db ? (Maybe I missed something .. ^^' ) |
@FlorianBruniaux For sure this is not normal, and looks like so huge bug that I can't believe it is the case. Please investigate your problem a little more and if you still think you spoted a bug, report it in separate issue. |
The problem came from the fact I used json data from an other test (with an other db solution). I had modified the id of my object "id":"0001" instead of "_id":"mjoNZvETbK3mBVeS" Okey. I'm an idiot 👍 ! Pb solved with a bit of attention ^^' |
No further comments. Thanks for awesome work :) |
Thanks :) It's now published, as v0.8.13 |
Awesome |
This implementation was in fact not correct and prone to dataloss in case of OS (not process) crash, if kernel buffer cache was not flushed to disk. The new implementation is very much inspired from Redis AOF persistence, see this very interesting article by antirez: http://oldblog.antirez.com/post/redis-persistence-demystified.html Implementation here if you want to look at it: https://github.com/louischatriot/nedb/blob/master/lib/storage.js You'll notice it doesn't need two temp files to ensure data integrity, but hwat it does need and were not present are fsync calls after data change. |
Hello @louischatriot
loadDatabase
while running is rewriting the whole database file. This is quite fragile moment. If the system crashes at this time, data will be lost.I don't know how probable is this issue, but I have run some dummy test...
...and data indeed was lost.
Possible fix would be ofcourse something like:
For file
test.db
test.db.bac
loadDatabase
and savetest.db
test.db.bac
And while loading data first time we have to look first for
test.db.bac
What do you think? Is it a real problem or too edge case to bother? Heh, life taught me there is no such thing like too edge case ;)
The text was updated successfully, but these errors were encountered: