node-haystack Episode 8: Simple Recovery And Verification
2016-09-10 14:43
721 查看
Any crafts made by mankind have defects. Some extra work must be done to save our ass.
Recovery
Recovery happens at volume level. To simplify the problem, we just use file copy as recovery. The demonstration code will show up in next episode.Verification
The verification just compares two volumes byte by byte. It assumes the source is complete health. If another one, the destination, has just one byte difference from the source, the verification do fail. Sorry for the callback hell, I know it difficulty for reading. Probably it’s the time to buy yourself a new wider monitor./*! \brief Verify two volumes byte by byte. \return "Return of the function" */ void Verify(const std::string& src, const std::string& dst, const cb_verify_t& cb) { AsyncFile afs_src; AsyncFile afs_dst; u64 off = 0; afs_src.Stat(src, [&, cb](int err, const AsyncFile::file_stat_t& src_fstat, void*) mutable { if (err) { LOG("Failed to stat source"); return cb(err, "existence.source", 0); } afs_dst.Stat(dst, [&, cb](int err, const AsyncFile::file_stat_t& dst_fstat, void*) mutable { if (err) { LOG("Failed to stat destination"); return cb(err, "existence.destination", 0); } if (src_fstat.size != dst_fstat.size) { return cb(_ERR(VOL_NONIDENTICAL), "validate.size", 0); } afs_src.Open(src, [&, cb](int err, void*) mutable { if (err) { LOG("Failed to open source"); return cb(err, "open.source", 0); } afs_dst.Open(dst, [&, cb](int err, void*) mutable { if (err) { LOG("Failed to open destination"); return cb(err, "open.destination", 0); } bst::function<void()> do_verify; do_verify = [&]() { afs_src.Read(off, READ_BUFFER_SIZE, [&, cb](int err, shared_t<vec_t<char>>& src_buf, void*) mutable { if (err) { return cb(err, "read.source", 0); } afs_dst.Read(off, READ_BUFFER_SIZE, [&](int err, shared_t<vec_t<char>>& dst_buf, void*) mutable { if (err) { return cb(err, "read.destination", 0); } if (src_buf->size() != dst_buf->size()) { return cb(_ERR(VOL_NONIDENTICAL), "validate.result", 0); } vec_t<char>& src_vec = *src_buf.get(); vec_t<char>& dst_vec = *dst_buf.get(); if (0 != std::memcmp(&src_vec[0], &dst_vec[0], src_vec.size())) { return cb(_ERR(VOL_NONIDENTICAL), "validate.data", 0); } // Notify progress cb(_ERR(NOERROR), "progress", (off + dst_buf->size() * 100) / src_fstat.size); if (src_buf->size() < READ_BUFFER_SIZE) { return cb(_ERR(NOERROR), "done", 0); // Succeeded } do_verify(); }); // dst read }); // src read }; // do_verify do_verify(); }); // dst open }); // src open }); // dst stat }); // src stat }
相关文章推荐
- node-haystack Episode 1: What is it and why
- node-haystack Episode 2: Asynchronous and Threading
- Simple example - Node.js, Restify, MongoDb and Mongoose
- THE DEAD-SIMPLE STEP-BY-STEP GUIDE FOR FRONT-END DEVELOPERS TO GETTING UP AND RUNNING WITH NODE.JS,
- node-haystack Episode 3: Callback model in C++
- node-haystack Episode-4: Wrapper of libuv
- node-haystack Episode - 12 : A Better Random Generator
- A Simple MVC Framework With Node and Express
- node-haystack Episode 12: problem of C++ closure
- node-haystack Episode 5: Volume
- node-haystack Episode 6: Data Structure And Constants
- node-haystack Episode 7: Asynchronously manipulate blocks
- node-haystack Episode 10: Node.js add-on
- node-haystack Episode 9: Manipulate Volume
- node-haystack Episode 11: node object of Volume
- [Javascript] Some very simple functions for Word and Excel handling
- Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms
- 深入理解MySQL 5.7 GTID系列(七)binlog_gtid_simple_recovery参数的影响总结
- CXF simple frontend, allow all SSL certificates and set basic authentication credentials
- C# XML - XmlNode and XmlAttribute