Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sysio::trace_api::slice_directory Class Reference

#include <store_provider.hpp>

Classes

struct  index_header
 

Public Types

enum class  open_state { read , write }
 

Public Member Functions

 slice_directory (const boost::filesystem::path &slice_dir, uint32_t width, std::optional< uint32_t > minimum_irreversible_history_blocks, std::optional< uint32_t > minimum_uncompressed_irreversible_history_blocks, size_t compression_seek_point_stride)
 
uint32_t slice_number (uint32_t block_height) const
 
bool find_or_create_index_slice (uint32_t slice_number, open_state state, fc::cfile &index_file) const
 
bool find_index_slice (uint32_t slice_number, open_state state, fc::cfile &index_file, bool open_file=true) const
 
bool find_or_create_trace_slice (uint32_t slice_number, open_state state, fc::cfile &trace_file) const
 
bool find_trace_slice (uint32_t slice_number, open_state state, fc::cfile &trace_file, bool open_file=true) const
 
std::optional< compressed_filefind_compressed_trace_slice (uint32_t slice_number, bool open_file=true) const
 
void find_or_create_slice_pair (uint32_t slice_number, open_state state, fc::cfile &trace, fc::cfile &index)
 
bool find_or_create_trx_id_slice (uint32_t slice_number, open_state state, fc::cfile &trx_id_file) const
 
bool find_trx_id_slice (uint32_t slice_number, open_state state, fc::cfile &trx_id_file, bool open_file=true) const
 
void set_lib (uint32_t lib)
 
void start_maintenance_thread (log_handler log)
 
void stop_maintenance_thread ()
 
void run_maintenance_tasks (uint32_t lib, const log_handler &log)
 

Detailed Description

Provides access to the slice directory. It is only intended to be used by store_provider and unit tests.

Definition at line 96 of file store_provider.hpp.

Member Enumeration Documentation

◆ open_state

Enumerator
read 
write 

Definition at line 102 of file store_provider.hpp.

102{ read /*read from front to back*/, write /*write to end of file*/ };

Constructor & Destructor Documentation

◆ slice_directory()

sysio::trace_api::slice_directory::slice_directory ( const boost::filesystem::path & slice_dir,
uint32_t width,
std::optional< uint32_t > minimum_irreversible_history_blocks,
std::optional< uint32_t > minimum_uncompressed_irreversible_history_blocks,
size_t compression_seek_point_stride )

Definition at line 153 of file store_provider.cpp.

154 : _slice_dir(slice_dir)
155 , _width(width)
156 , _minimum_irreversible_history_blocks(minimum_irreversible_history_blocks)
157 , _minimum_uncompressed_irreversible_history_blocks(minimum_uncompressed_irreversible_history_blocks)
158 , _compression_seek_point_stride(compression_seek_point_stride)
159 , _best_known_lib(0) {
160 if (!exists(_slice_dir)) {
161 bfs::create_directories(slice_dir);
162 }
163 }
bool exists(const path &p)
Here is the call graph for this function:

Member Function Documentation

◆ find_compressed_trace_slice()

std::optional< compressed_file > sysio::trace_api::slice_directory::find_compressed_trace_slice ( uint32_t slice_number,
bool open_file = true ) const

Find the read-only compressed trace file associated with the indicated slice_number

Parameters
slice_number: slice number of the requested slice file
open_file: indicate if the file should be opened (if found) or not
Returns
if file was found (i.e. already existed) returns an optional containing a compressed_file which is open (or not) depending on the open_file paraneter, Otherwise, the returned optional is empty

Definition at line 227 of file store_provider.cpp.

227 {
228 auto filename = make_filename(_trace_prefix, _compressed_trace_ext, slice_number, _width);
229 const path slice_path = _slice_dir / filename;
230 const bool file_exists = exists(slice_path);
231
232 if (file_exists) {
233 auto result = compressed_file(slice_path);
234 if (open_file) {
235 result.open();
236 }
237
238 return std::move(result);
239 } else {
240 return {};
241 }
242 }
uint32_t slice_number(uint32_t block_height) const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_index_slice()

bool sysio::trace_api::slice_directory::find_index_slice ( uint32_t slice_number,
open_state state,
fc::cfile & index_file,
bool open_file = true ) const

Find the index file associated with the indicated slice_number

Parameters
slice_number: slice number of the requested slice file
state: indicate if the file is going to be written to (appended) or read
index_file: the cfile that will be set to the appropriate slice filename (always) and opened to that file (if it was found)
open_file: indicate if the file should be opened (if found) or not
Returns
the true if file was found (i.e. already existed), if not found index_file is set to the appropriate file, but not open

Definition at line 173 of file store_provider.cpp.

173 {
174 const bool found = find_slice(_trace_index_prefix, slice_number, index_file, open_file);
175 if( !found || !open_file ) {
176 return found;
177 }
178
179 validate_existing_index_slice_file(index_file, state);
180 return true;
181 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_or_create_index_slice()

bool sysio::trace_api::slice_directory::find_or_create_index_slice ( uint32_t slice_number,
open_state state,
fc::cfile & index_file ) const

Find or create the index file associated with the indicated slice_number

Parameters
slice_number: slice number of the requested slice file
state: indicate if the file is going to be written to (appended) or read
index_file: the cfile that will be set to the appropriate slice filename and opened to that file
Returns
the true if file was found (i.e. already existed)

Definition at line 165 of file store_provider.cpp.

165 {
166 const bool found = find_index_slice(slice_number, state, index_file);
167 if( !found ) {
168 create_new_index_slice_file(index_file);
169 }
170 return found;
171 }
bool find_index_slice(uint32_t slice_number, open_state state, fc::cfile &index_file, bool open_file=true) const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_or_create_slice_pair()

void sysio::trace_api::slice_directory::find_or_create_slice_pair ( uint32_t slice_number,
open_state state,
fc::cfile & trace,
fc::cfile & index )

Find or create a trace and index file pair

Parameters
slice_number: slice number of the requested slice file
state: indicate if the file is going to be written to (appended) or read
trace: the cfile that will be set to the appropriate slice filename and opened to that file

Definition at line 262 of file store_provider.cpp.

262 {
263 const bool trace_found = find_or_create_trace_slice(slice_number, state, trace);
264 const bool index_found = find_or_create_index_slice(slice_number, state, index);
265 if (trace_found != index_found) {
266 const std::string trace_status = trace_found ? "existing" : "new";
267 const std::string index_status = index_found ? "existing" : "new";
268 elog("Trace file is ${ts}, but it's metadata file is ${is}. This means the files are not consistent.", ("ts", trace_status)("is", index_status));
269 }
270 }
bool find_or_create_index_slice(uint32_t slice_number, open_state state, fc::cfile &index_file) const
bool find_or_create_trace_slice(uint32_t slice_number, open_state state, fc::cfile &trace_file) const
#define elog(FORMAT,...)
Definition logger.hpp:130
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_or_create_trace_slice()

bool sysio::trace_api::slice_directory::find_or_create_trace_slice ( uint32_t slice_number,
open_state state,
fc::cfile & trace_file ) const

Find or create the trace file associated with the indicated slice_number

Parameters
slice_number: slice number of the requested slice file
state: indicate if the file is going to be written to (appended) or read
trace_file: the cfile that will be set to the appropriate slice filename and opened to that file
Returns
the true if file was found (i.e. already existed)

Definition at line 201 of file store_provider.cpp.

201 {
202 const bool found = find_trace_slice(slice_number, state, trace_file);
203
204 if( !found ) {
206 }
207
208 return found;
209 }
static constexpr const char * create_or_update_rw_mode
Definition cfile.hpp:57
void open(const char *mode)
Definition cfile.hpp:65
bool find_trace_slice(uint32_t slice_number, open_state state, fc::cfile &trace_file, bool open_file=true) const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_or_create_trx_id_slice()

bool sysio::trace_api::slice_directory::find_or_create_trx_id_slice ( uint32_t slice_number,
open_state state,
fc::cfile & trx_id_file ) const

Find or create a trx id file that contains all the transaction ids and associated block numbers

Parameters
slice_number: slice number of the requested slice file
state: indicate if the file is going to be written to (appended) or read
trx_id_file: the cfile
Returns
true if file was found (i.e. already existed)

Definition at line 272 of file store_provider.cpp.

272 {
273 const bool found = find_trx_id_slice(slice_number, state, trx_id_file);
274 if( !found ) {
276 }
277 return found;
278 }
bool find_trx_id_slice(uint32_t slice_number, open_state state, fc::cfile &trx_id_file, bool open_file=true) const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_trace_slice()

bool sysio::trace_api::slice_directory::find_trace_slice ( uint32_t slice_number,
open_state state,
fc::cfile & trace_file,
bool open_file = true ) const

Find the trace file associated with the indicated slice_number

Parameters
slice_number: slice number of the requested slice file
state: indicate if the file is going to be written to (appended) or read
trace_file: the cfile that will be set to the appropriate slice filename (always) and opened to that file (if it was found)
open_file: indicate if the file should be opened (if found) or not
Returns
the true if file was found (i.e. already existed), if not found index_file is set to the appropriate file, but not open

Definition at line 211 of file store_provider.cpp.

211 {
212 const bool found = find_slice(_trace_prefix, slice_number, trace_file, open_file);
213
214 if( !found || !open_file ) {
215 return found;
216 }
217
218 if( state == open_state::write ) {
219 trace_file.seek_end(0);
220 }
221 else {
222 trace_file.seek(0); // ensure we are at the start of the file
223 }
224 return true;
225 }
void seek_end(long loc)
Definition cfile.hpp:96
void seek(long loc)
Definition cfile.hpp:87
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_trx_id_slice()

bool sysio::trace_api::slice_directory::find_trx_id_slice ( uint32_t slice_number,
open_state state,
fc::cfile & trx_id_file,
bool open_file = true ) const

Find the trx id file

Parameters
slice_number: slice number of the requested slice file
state: indicate if the file is going to be written to (appended) or read
trx_id_file: the cfile
open_file: indicate if the file should be opened (if found) or not
Returns
true if file was found (i.e. already existed), if not found trx_id_file is set to the appropriate file, but not opened

Definition at line 280 of file store_provider.cpp.

280 {
281 const bool found = find_slice(_trace_trx_id_prefix, slice_number, trx_id_file, open_file);
282 if( !found || !open_file ) {
283 return found;
284 }
285 if( state == open_state::write ) {
286 trx_id_file.seek_end(0);
287 }
288 return true;
289 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run_maintenance_tasks()

void sysio::trace_api::slice_directory::run_maintenance_tasks ( uint32_t lib,
const log_handler & log )

Cleans up all slices that are no longer needed to maintain the minimum number of blocks past lib Compresses up all slices that can be compressed

Parameters
lib: block number of the current lib

Definition at line 355 of file store_provider.cpp.

355 {
356 if (_minimum_irreversible_history_blocks) {
357 process_irreversible_slice_range(lib, *_minimum_irreversible_history_blocks, _last_cleaned_up_slice, [this, &log](uint32_t slice_to_clean){
358 fc::cfile trace;
359 fc::cfile index;
360 fc::cfile trx_id;
361
362 log(std::string("Attempting Prune of slice: ") + std::to_string(slice_to_clean));
363
364 // cleanup index first to reduce the likelihood of reader finding index, but not finding trace
365 const bool dont_open_file = false;
366 const bool index_found = find_index_slice(slice_to_clean, open_state::read, index, dont_open_file);
367 if (index_found) {
368 log(std::string("Removing: ") + index.get_file_path().generic_string());
369 bfs::remove(index.get_file_path());
370 }
371 const bool trace_found = find_trace_slice(slice_to_clean, open_state::read, trace, dont_open_file);
372 if (trace_found) {
373 log(std::string("Removing: ") + trace.get_file_path().generic_string());
374 bfs::remove(trace.get_file_path());
375 }
376 const bool trx_id_found = find_trx_id_slice(slice_to_clean, open_state::read, trx_id, dont_open_file);
377 if (trx_id_found) {
378 log(std::string("Removing: ") + trx_id.get_file_path().generic_string());
379 bfs::remove(trx_id.get_file_path());
380 }
381
382 auto ctrace = find_compressed_trace_slice(slice_to_clean, dont_open_file);
383 if (ctrace) {
384 log(std::string("Removing: ") + ctrace->get_file_path().generic_string());
385 bfs::remove(ctrace->get_file_path());
386 }
387 });
388 }
389
390 // Only process compression if its configured AND there is a range of irreversible blocks which would not also
391 // be deleted
392 if (_minimum_uncompressed_irreversible_history_blocks &&
393 (!_minimum_irreversible_history_blocks || *_minimum_uncompressed_irreversible_history_blocks < *_minimum_irreversible_history_blocks) )
394 {
395 process_irreversible_slice_range(lib, *_minimum_uncompressed_irreversible_history_blocks, _last_compressed_slice, [this, &log](uint32_t slice_to_compress){
396 fc::cfile trace;
397 const bool dont_open_file = false;
398 const bool trace_found = find_trace_slice(slice_to_compress, open_state::read, trace, dont_open_file);
399
400 log(std::string("Attempting compression of slice: ") + std::to_string(slice_to_compress));
401
402 if (trace_found) {
403 auto compressed_path = trace.get_file_path();
404 compressed_path.replace_extension(_compressed_trace_ext);
405
406 log(std::string("Compressing: ") + trace.get_file_path().generic_string());
407 compressed_file::process(trace.get_file_path(), compressed_path.generic_string(), _compression_seek_point_stride);
408
409 // after compression is complete, delete the old uncompressed file
410 log(std::string("Removing: ") + trace.get_file_path().generic_string());
411 bfs::remove(trace.get_file_path());
412 }
413 });
414 }
415 }
fc::path get_file_path() const
Definition cfile.hpp:41
std::string generic_string() const
void replace_extension(const fc::path &e)
static bool process(const fc::path &input_path, const fc::path &output_path, size_t seek_point_stride)
std::optional< compressed_file > find_compressed_trace_slice(uint32_t slice_number, bool open_file=true) const
unsigned int uint32_t
Definition stdint.h:126
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_lib()

void sysio::trace_api::slice_directory::set_lib ( uint32_t lib)

set the LIB for maintenance

Parameters
lib

Definition at line 291 of file store_provider.cpp.

291 {
292 {
293 std::scoped_lock lock(_maintenance_mtx);
294 _best_known_lib = lib;
295 }
296 _maintenance_condition.notify_one();
297 }
void lock()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ slice_number()

uint32_t sysio::trace_api::slice_directory::slice_number ( uint32_t block_height) const
inline

Return the slice number that would include the passed in block_height

Parameters
block_height: height of the requested data
Returns
the slice number for the block_height

Definition at line 112 of file store_provider.hpp.

112 {
113 return block_height / _width;
114 }
Here is the caller graph for this function:

◆ start_maintenance_thread()

void sysio::trace_api::slice_directory::start_maintenance_thread ( log_handler log)

Start a thread which does background maintenance

Definition at line 299 of file store_provider.cpp.

299 {
300 _maintenance_thread = std::thread([this, log=std::move(log)](){
301 fc::set_os_thread_name( "trace-mx" );
302 uint32_t last_lib = 0;
303
304 while(true) {
305 std::unique_lock<std::mutex> lock(_maintenance_mtx);
306 while ( last_lib >= _best_known_lib && !_maintenance_shutdown ) {
307 _maintenance_condition.wait(lock);
308 }
309
310 if (_maintenance_shutdown) {
311 break;
312 }
313
314 uint32_t best_known_lib = _best_known_lib;
315 lock.unlock();
316
317 log(std::string("Waking up to handle lib: ") + std::to_string(best_known_lib));
318
319 if (last_lib < best_known_lib) {
320 try {
321 run_maintenance_tasks(best_known_lib, log);
322 last_lib = best_known_lib;
323 } FC_LOG_AND_DROP();
324 }
325 }
326 });
327 }
void run_maintenance_tasks(uint32_t lib, const log_handler &log)
#define FC_LOG_AND_DROP(...)
void set_os_thread_name(const string &name)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop_maintenance_thread()

void sysio::trace_api::slice_directory::stop_maintenance_thread ( )

Stop and join the thread doing background maintenance

Definition at line 329 of file store_provider.cpp.

329 {
330 {
331 std::scoped_lock lock(_maintenance_mtx);
332 _maintenance_shutdown = true;
333 }
334 _maintenance_condition.notify_one();
335 _maintenance_thread.join();
336 }
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: