libfoedus-core
FOEDUS Core Library
foedus::cache::SnapshotFileSet Class Referencefinal

Holds a set of read-only file objects for snapshot files. More...

Detailed Description

Holds a set of read-only file objects for snapshot files.

In essense, this is a map<snapshot_id, map<node_id, DirectIoFile*> >. Each thread memory internally maintains this file set. This is because DirectIoFile (or underlying linux read()) cannot be concurrently used. Each thread thus obtains its own file descriptors using this object. As it's thread-local, no synchronization is needed in this object.

This design might hit the maximum number of file descriptors per process. Check cat /proc/sys/fs/file-max if that happens. Google how to change it (soft AND hard limits).

Todo:
So far we really use std::map. But, this is not ideal in terms of performance. node-id is up to 256, snapshots are almost always very few, so we can do array-based something.

Definition at line 52 of file snapshot_file_set.hpp.

#include <snapshot_file_set.hpp>

Inheritance diagram for foedus::cache::SnapshotFileSet:
Collaboration diagram for foedus::cache::SnapshotFileSet:

Public Member Functions

 SnapshotFileSet (Engine *engine)
 
ErrorStack initialize_once () override
 
ErrorStack uninitialize_once () override
 
void close_all ()
 
Engineget_engine ()
 
 SnapshotFileSet ()=delete
 
 SnapshotFileSet (const SnapshotFileSet &other)=delete
 
SnapshotFileSetoperator= (const SnapshotFileSet &other)=delete
 
ErrorCode get_or_open_file (storage::SnapshotPagePointer page_pointer, fs::DirectIoFile **out)
 
ErrorCode get_or_open_file (snapshot::SnapshotId snapshot_id, thread::ThreadGroupId node_id, fs::DirectIoFile **out)
 
ErrorCode read_page (storage::SnapshotPagePointer page_id, void *out)
 
ErrorCode read_pages (storage::SnapshotPagePointer page_id_begin, uint32_t page_count, void *out)
 Read contiguous pages in one shot. More...
 
- Public Member Functions inherited from foedus::DefaultInitializable
 DefaultInitializable ()
 
virtual ~DefaultInitializable ()
 
 DefaultInitializable (const DefaultInitializable &)=delete
 
DefaultInitializableoperator= (const DefaultInitializable &)=delete
 
ErrorStack initialize () override final
 Typical implementation of Initializable::initialize() that provides initialize-once semantics. More...
 
ErrorStack uninitialize () override final
 Typical implementation of Initializable::uninitialize() that provides uninitialize-once semantics. More...
 
bool is_initialized () const override final
 Returns whether the object has been already initialized or not. More...
 
- Public Member Functions inherited from foedus::Initializable
virtual ~Initializable ()
 

Friends

std::ostream & operator<< (std::ostream &o, const SnapshotFileSet &v)
 

Constructor & Destructor Documentation

foedus::cache::SnapshotFileSet::SnapshotFileSet ( Engine engine)
explicit

Definition at line 34 of file snapshot_file_set.cpp.

34  : engine_(engine) {
35 }
foedus::cache::SnapshotFileSet::SnapshotFileSet ( )
delete
foedus::cache::SnapshotFileSet::SnapshotFileSet ( const SnapshotFileSet other)
delete

Member Function Documentation

void foedus::cache::SnapshotFileSet::close_all ( )

Definition at line 47 of file snapshot_file_set.cpp.

Referenced by uninitialize_once().

47  {
48  for (auto& files_in_a_snapshot : files_) {
49  auto& values = files_in_a_snapshot.second;
50  for (auto& file : values) {
51  file.second->close();
52  delete file.second;
53  }
54  values.clear();
55  }
56  files_.clear();
57 }

Here is the caller graph for this function:

Engine* foedus::cache::SnapshotFileSet::get_engine ( )
inline

Definition at line 59 of file snapshot_file_set.hpp.

59 { return engine_; }
ErrorCode foedus::cache::SnapshotFileSet::get_or_open_file ( storage::SnapshotPagePointer  page_pointer,
fs::DirectIoFile **  out 
)
inline

Definition at line 65 of file snapshot_file_set.hpp.

References foedus::storage::extract_numa_node_from_snapshot_pointer(), and foedus::storage::extract_snapshot_id_from_snapshot_pointer().

Referenced by read_page(), and read_pages().

65  {
66  snapshot::SnapshotId snapshot_id
69  return get_or_open_file(snapshot_id, node_id, out);
70  }
uint16_t extract_snapshot_id_from_snapshot_pointer(SnapshotPagePointer pointer)
Definition: storage_id.hpp:98
uint8_t extract_numa_node_from_snapshot_pointer(SnapshotPagePointer pointer)
Definition: storage_id.hpp:95
uint16_t SnapshotId
Unique ID of Snapshot.
Definition: snapshot_id.hpp:43
ErrorCode get_or_open_file(storage::SnapshotPagePointer page_pointer, fs::DirectIoFile **out)
uint8_t ThreadGroupId
Typedef for an ID of ThreadGroup (NUMA node).
Definition: thread_id.hpp:38

Here is the call graph for this function:

Here is the caller graph for this function:

ErrorCode foedus::cache::SnapshotFileSet::get_or_open_file ( snapshot::SnapshotId  snapshot_id,
thread::ThreadGroupId  node_id,
fs::DirectIoFile **  out 
)

Definition at line 59 of file snapshot_file_set.cpp.

References ASSERT_ND, foedus::snapshot::SnapshotOptions::construct_snapshot_file_path(), foedus::Engine::get_options(), foedus::kErrorCodeOk, and foedus::EngineOptions::snapshot_.

62  {
63  *out = nullptr;
64  auto snapshot = files_.find(snapshot_id);
65  if (snapshot == files_.end()) {
66  files_.insert(
67  std::pair<snapshot::SnapshotId, std::map< thread::ThreadGroupId, fs::DirectIoFile* > >(
68  snapshot_id, std::map< thread::ThreadGroupId, fs::DirectIoFile* >()));
69  snapshot = files_.find(snapshot_id);
70  }
71  ASSERT_ND(snapshot != files_.end());
72  ASSERT_ND(snapshot->first == snapshot_id);
73  auto& the_map = snapshot->second;
74  auto node = the_map.find(node_id);
75  if (node != the_map.end()) {
76  *out = node->second;
77  return kErrorCodeOk;
78  } else {
79  fs::Path path(engine_->get_options().snapshot_.construct_snapshot_file_path(
80  snapshot_id,
81  node_id));
82  fs::DirectIoFile* file = new fs::DirectIoFile(path);
83  ErrorCode open_error = file->open(true, false, false, false);
84  if (open_error != kErrorCodeOk) {
85  delete file;
86  return open_error;
87  }
88  std::pair< thread::ThreadGroupId, fs::DirectIoFile* > entry(node_id, file);
89  the_map.insert(entry);
90  *out = file;
91  return kErrorCodeOk;
92  }
93 }
const EngineOptions & get_options() const
Definition: engine.cpp:39
0 means no-error.
Definition: error_code.hpp:87
snapshot::SnapshotOptions snapshot_
uint16_t SnapshotId
Unique ID of Snapshot.
Definition: snapshot_id.hpp:43
std::string construct_snapshot_file_path(int snapshot_id, int node) const
'folder_path'/snapshot_'snapshot-id'node'node-id'.data.
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
ErrorCode
Enum of error codes defined in error_code.xmacro.
Definition: error_code.hpp:85

Here is the call graph for this function:

ErrorStack foedus::cache::SnapshotFileSet::initialize_once ( )
overridevirtual

Implements foedus::DefaultInitializable.

Definition at line 37 of file snapshot_file_set.cpp.

References foedus::kRetOk.

37  {
38  return kRetOk;
39 }
const ErrorStack kRetOk
Normal return value for no-error case.
SnapshotFileSet& foedus::cache::SnapshotFileSet::operator= ( const SnapshotFileSet other)
delete
ErrorCode foedus::cache::SnapshotFileSet::read_page ( storage::SnapshotPagePointer  page_id,
void *  out 
)

Definition at line 95 of file snapshot_file_set.cpp.

References ASSERT_ND, CHECK_ERROR_CODE, foedus::storage::extract_local_page_id_from_snapshot_pointer(), get_or_open_file(), foedus::fs::DirectIoFile::kDirectIoSeekSet, foedus::kErrorCodeOk, foedus::fs::DirectIoFile::read_raw(), and foedus::fs::DirectIoFile::seek().

Referenced by foedus::storage::array::ArrayComposer::construct_root(), foedus::storage::hash::HashComposer::construct_root(), foedus::storage::sequential::SequentialComposer::construct_root(), foedus::storage::masstree::MasstreeStoragePimpl::debugout_single_thread_follow(), foedus::storage::masstree::MasstreePartitioner::design_partition(), foedus::storage::hash::TmpSnashotPage::init(), foedus::memory::EngineMemory::load_one_volatile_page(), foedus::storage::hash::ComposedBinsMergedStream::open_path(), foedus::thread::ThreadPimpl::read_a_snapshot_page(), and foedus::storage::StorageManagerPimpl::reinitialize_for_recovered_snapshot().

95  {
96  fs::DirectIoFile* file;
97  CHECK_ERROR_CODE(get_or_open_file(page_id, &file));
98  storage::SnapshotLocalPageId local_page_id
101  file->seek(local_page_id * sizeof(storage::Page), fs::DirectIoFile::kDirectIoSeekSet));
102  CHECK_ERROR_CODE(file->read_raw(sizeof(storage::Page), out));
103  ASSERT_ND(reinterpret_cast<storage::Page*>(out)->get_header().page_id_ == page_id);
104  return kErrorCodeOk;
105 }
uint64_t SnapshotLocalPageId
Represents a local page ID in each one snapshot file in some NUMA node.
Definition: storage_id.hpp:89
SnapshotLocalPageId extract_local_page_id_from_snapshot_pointer(SnapshotPagePointer pointer)
Definition: storage_id.hpp:91
0 means no-error.
Definition: error_code.hpp:87
#define CHECK_ERROR_CODE(x)
This macro calls x and checks its returned error code.
Definition: error_code.hpp:155
ErrorCode get_or_open_file(storage::SnapshotPagePointer page_pointer, fs::DirectIoFile **out)
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
The offset is set to offset bytes.

Here is the call graph for this function:

Here is the caller graph for this function:

ErrorCode foedus::cache::SnapshotFileSet::read_pages ( storage::SnapshotPagePointer  page_id_begin,
uint32_t  page_count,
void *  out 
)

Read contiguous pages in one shot.

Definition at line 107 of file snapshot_file_set.cpp.

References ASSERT_ND, CHECK_ERROR_CODE, foedus::storage::extract_local_page_id_from_snapshot_pointer(), get_or_open_file(), foedus::fs::DirectIoFile::kDirectIoSeekSet, foedus::kErrorCodeOk, foedus::fs::DirectIoFile::read_raw(), and foedus::fs::DirectIoFile::seek().

Referenced by foedus::storage::hash::ComposedBinsBuffer::next_pages(), and foedus::thread::ThreadPimpl::read_snapshot_pages().

110  {
111  fs::DirectIoFile* file;
112  CHECK_ERROR_CODE(get_or_open_file(page_id_begin, &file));
113  storage::SnapshotLocalPageId local_page_id_begin
116  file->seek(local_page_id_begin * sizeof(storage::Page), fs::DirectIoFile::kDirectIoSeekSet));
117  CHECK_ERROR_CODE(file->read_raw(sizeof(storage::Page) * page_count, out));
118 #ifndef NDEBUG
119  storage::Page* pages = reinterpret_cast<storage::Page*>(out);
120  for (uint32_t i = 0; i < page_count; ++i) {
121  ASSERT_ND(pages[i].get_header().page_id_ == page_id_begin + i);
122  }
123 #endif // NDEBUG
124  return kErrorCodeOk;
125 }
uint64_t SnapshotLocalPageId
Represents a local page ID in each one snapshot file in some NUMA node.
Definition: storage_id.hpp:89
SnapshotLocalPageId extract_local_page_id_from_snapshot_pointer(SnapshotPagePointer pointer)
Definition: storage_id.hpp:91
0 means no-error.
Definition: error_code.hpp:87
#define CHECK_ERROR_CODE(x)
This macro calls x and checks its returned error code.
Definition: error_code.hpp:155
ErrorCode get_or_open_file(storage::SnapshotPagePointer page_pointer, fs::DirectIoFile **out)
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
The offset is set to offset bytes.

Here is the call graph for this function:

Here is the caller graph for this function:

ErrorStack foedus::cache::SnapshotFileSet::uninitialize_once ( )
overridevirtual

Implements foedus::DefaultInitializable.

Definition at line 41 of file snapshot_file_set.cpp.

References close_all(), and SUMMARIZE_ERROR_BATCH.

41  {
42  ErrorStackBatch batch;
43  close_all();
44  return SUMMARIZE_ERROR_BATCH(batch);
45 }
#define SUMMARIZE_ERROR_BATCH(x)
This macro calls ErrorStackBatch::summarize() with automatically provided parameters.

Here is the call graph for this function:

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  o,
const SnapshotFileSet v 
)
friend

Definition at line 127 of file snapshot_file_set.cpp.

127  {
128  o << "<SnapshotFileSet>";
129  for (const auto& snapshot : v.files_) {
130  o << "<snapshot id=\"" << snapshot.first << "\">";
131  for (const auto& entry : snapshot.second) {
132  o << "<node id=\"" << entry.first
133  << "\" fd=\"" << entry.second->get_descriptor() << "\" />";
134  }
135  o << "</snapshot>";
136  }
137  return o;
138 }

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