1818#include < map>
1919#include < string>
2020#include < sisl/logging/logging.h>
21+ #include < sisl/utility/obj_life_counter.hpp>
22+ #include < homestore/crc.h>
2123#include < homestore/homestore.hpp>
2224#include < homestore/index/index_table.hpp>
2325#include < homestore/superblk_handler.hpp>
@@ -30,6 +32,14 @@ namespace homeblocks {
3032
3133class Volume ;
3234
35+ ENUM (HomeBlksMsgType, uint8_t , READ, WRITE, UNMAP);
36+
37+ struct VolJournalEntry {
38+ lba_t start_lba;
39+ lba_count_t nlbas;
40+ uint16_t num_old_blks;
41+ };
42+
3343class HomeBlocksImpl : public HomeBlocks , public VolumeManager , public std ::enable_shared_from_this< HomeBlocksImpl > {
3444 struct homeblks_sb_t {
3545 uint64_t magic;
@@ -94,17 +104,14 @@ class HomeBlocksImpl : public HomeBlocks, public VolumeManager, public std::enab
94104
95105 VolumePtr lookup_volume (const volume_id_t & id) final ;
96106
97- NullAsyncResult write (const VolumePtr& vol, const vol_interface_req_ptr& req,
98- bool part_of_batch = false ) final ;
107+ NullAsyncResult write (const VolumePtr& vol, const vol_interface_req_ptr& req, bool part_of_batch = false ) final ;
99108
100- NullAsyncResult read (const VolumePtr& vol, const vol_interface_req_ptr& req,
101- bool part_of_batch = false ) final ;
109+ NullAsyncResult read (const VolumePtr& vol, const vol_interface_req_ptr& req, bool part_of_batch = false ) final ;
102110
103111 NullAsyncResult unmap (const VolumePtr& vol, const vol_interface_req_ptr& req) final ;
104112
105113 void submit_io_batch () final ;
106114
107-
108115 // see api comments in base class;
109116 bool get_stats (volume_id_t id, VolumeStats& stats) const final ;
110117 void get_volume_ids (std::vector< volume_id_t >& vol_ids) const final ;
@@ -120,6 +127,9 @@ class HomeBlocksImpl : public HomeBlocks, public VolumeManager, public std::enab
120127
121128 void on_init_complete ();
122129
130+ void on_write (int64_t lsn, const sisl::blob& header, const sisl::blob& key,
131+ const std::vector< homestore::MultiBlkId >& blkids, cintrusive< homestore::repl_req_ctx >& ctx);
132+
123133private:
124134 // Should only be called for first-time-boot
125135 void superblk_init ();
@@ -134,6 +144,10 @@ class HomeBlocksImpl : public HomeBlocks, public VolumeManager, public std::enab
134144 // recovery apis
135145 void on_hb_meta_blk_found (sisl::byte_view const & buf, void * cookie);
136146 void on_vol_meta_blk_found (sisl::byte_view const & buf, void * cookie);
147+
148+ VolumeManager::Result< folly::Unit > write_to_index (const VolumePtr& vol_ptr, const vol_interface_req_ptr& vol_req,
149+ lba_t start_lba, lba_t end_lba,
150+ std::unordered_map< lba_t , BlockInfo >& blocks_info);
137151};
138152
139153class HBIndexSvcCB : public homestore ::IndexServiceCallbacks {
@@ -148,4 +162,87 @@ class HBIndexSvcCB : public homestore::IndexServiceCallbacks {
148162private:
149163 HomeBlocksImpl* hb_;
150164};
165+
166+ const homestore::csum_t init_crc_16 = 0x8005 ;
167+
168+ struct HomeBlksMessageHeader {
169+ HomeBlksMessageHeader () = default ;
170+ HomeBlksMsgType msg_type;
171+ volume_id_t volume_id;
172+
173+ std::string to_string () const {
174+ return fmt::format (" msg_type={}volume={}\n " , enum_name (msg_type), boost::uuids::to_string (volume_id));
175+ }
176+ };
177+
178+ struct homeblks_repl_ctx : public homestore ::repl_req_ctx {
179+ sisl::io_blob_safe hdr_buf_;
180+ sisl::io_blob_safe key_buf_;
181+
182+ // Data bufs corresponding to data_sgs_. Since data_sgs are raw pointers, we need to keep the data bufs alive
183+ folly::small_vector< sisl::io_blob_safe, 3 > data_bufs_;
184+ sisl::sg_list data_sgs_;
185+
186+ homeblks_repl_ctx (uint32_t hdr_extn_size, uint32_t key_size = 0 ) : homestore::repl_req_ctx{} {
187+ hdr_buf_ = std::move (sisl::io_blob_safe{uint32_cast (sizeof (HomeBlksMessageHeader) + hdr_extn_size), 0 });
188+ new (hdr_buf_.bytes ()) HomeBlksMessageHeader ();
189+
190+ if (key_size) { key_buf_ = std::move (sisl::io_blob_safe{key_size, 0 }); }
191+ data_sgs_.size = 0 ;
192+ }
193+
194+ ~homeblks_repl_ctx () {
195+ if (hdr_buf_.bytes ()) { header ()->~HomeBlksMessageHeader (); }
196+ }
197+
198+ template < typename T >
199+ T* to () {
200+ return r_cast< T* >(this );
201+ }
202+
203+ HomeBlksMessageHeader* header () { return r_cast< HomeBlksMessageHeader* >(hdr_buf_.bytes ()); }
204+ uint8_t * header_extn () { return hdr_buf_.bytes () + sizeof (HomeBlksMessageHeader); }
205+
206+ sisl::io_blob_safe& header_buf () { return hdr_buf_; }
207+ sisl::io_blob_safe const & cheader_buf () const { return hdr_buf_; }
208+
209+ sisl::io_blob_safe& key_buf () { return key_buf_; }
210+ sisl::io_blob_safe const & ckey_buf () const { return key_buf_; }
211+
212+ void add_data_sg (uint8_t * buf, uint32_t size) {
213+ data_sgs_.iovs .emplace_back (iovec{.iov_base = buf, .iov_len = size});
214+ data_sgs_.size += size;
215+ }
216+
217+ void add_data_sg (sisl::io_blob_safe&& buf) {
218+ add_data_sg (buf.bytes (), buf.size ());
219+ data_bufs_.emplace_back (std::move (buf));
220+ }
221+
222+ sisl::sg_list& data_sgs () { return data_sgs_; }
223+ std::string data_sgs_string () const {
224+ fmt::memory_buffer buf;
225+ fmt::format_to (fmt::appender (buf), " total_size={} iovcnt={} [" , data_sgs_.size , data_sgs_.iovs .size ());
226+ for (auto const & iov : data_sgs_.iovs ) {
227+ fmt::format_to (fmt::appender (buf), " <base={},len={}> " , iov.iov_base , iov.iov_len );
228+ }
229+ fmt::format_to (fmt::appender (buf), " ]" );
230+ return fmt::to_string (buf);
231+ }
232+ };
233+
234+ template < typename T >
235+ struct repl_result_ctx : public homeblks_repl_ctx {
236+ folly::Promise< T > promise_;
237+ VolumePtr vol_ptr_{nullptr };
238+
239+ template < typename ... Args >
240+ static intrusive< repl_result_ctx< T > > make (Args&&... args) {
241+ return intrusive< repl_result_ctx< T > >{new repl_result_ctx< T >(std::forward< Args >(args)...)};
242+ }
243+
244+ repl_result_ctx (uint32_t hdr_extn_size, uint32_t key_size = 0 ) : homeblks_repl_ctx{hdr_extn_size, key_size} {}
245+ folly::SemiFuture< T > result () { return promise_.getSemiFuture (); }
246+ };
247+
151248} // namespace homeblocks
0 commit comments