OmniSciDB  c0231cc57d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FileInfo.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "FileInfo.h"
18 #include <iostream>
19 #include "../../Shared/File.h"
20 #include "FileMgr.h"
21 #include "Page.h"
22 
23 #include <utility>
24 using namespace std;
25 
26 extern bool g_read_only;
27 extern bool g_multi_instance;
28 
29 namespace File_Namespace {
30 
31 FileInfo::FileInfo(FileMgr* fileMgr,
32  const int32_t fileId,
33  FILE* f,
34  const size_t pageSize,
35  size_t numPages,
36  bool init)
37  : fileMgr(fileMgr), fileId(fileId), f(f), pageSize(pageSize), numPages(numPages) {
38  if (init) {
39  initNewFile();
40  }
41 }
42 
44  // close file, if applicable
45  if (f) {
46  close(f);
47  }
48 }
49 
51  // initialize pages and free page list
52  // Also zeroes out first four bytes of every header
53 
54  int32_t headerSize = 0;
55  int8_t* headerSizePtr = (int8_t*)(&headerSize);
56  for (size_t pageId = 0; pageId < numPages; ++pageId) {
57  File_Namespace::write(f, pageId * pageSize, sizeof(int32_t), headerSizePtr);
58  freePages.insert(pageId);
59  }
60  isDirty = true;
61 }
62 
63 size_t FileInfo::write(const size_t offset, const size_t size, const int8_t* buf) {
64  std::lock_guard<std::mutex> lock(readWriteMutex_);
65  isDirty = true;
66  return File_Namespace::write(f, offset, size, buf);
67 }
68 
69 size_t FileInfo::read(const size_t offset, const size_t size, int8_t* buf) {
70  std::lock_guard<std::mutex> lock(readWriteMutex_);
71  return File_Namespace::read(f, offset, size, buf);
72 }
73 
74 void FileInfo::openExistingFile(std::vector<HeaderInfo>& headerVec) {
75  // HeaderInfo is defined in Page.h
76 
77  // Oct 2020: Changing semantics such that fileMgrEpoch should be last checkpointed
78  // epoch, not incremented epoch. This changes some of the gt/gte/lt/lte comparison below
79  ChunkKey oldChunkKey(4);
80  int32_t oldPageId = -99;
81  int32_t oldVersionEpoch = -99;
82  int32_t skipped = 0;
83  for (size_t pageNum = 0; pageNum < numPages; ++pageNum) {
84  // TODO(Misiu): It would be nice to replace this array with a struct that would
85  // clarify what is being read and have a single definition (currently this code is
86  // replicated in TableArchiver and possibly elsewhere).
87  constexpr size_t MAX_INTS_TO_READ{10}; // currently use 1+6 ints
88  int32_t ints[MAX_INTS_TO_READ];
89  CHECK_EQ(fseek(f, pageNum * pageSize, SEEK_SET), 0);
90  CHECK_EQ(fread(ints, sizeof(int32_t), MAX_INTS_TO_READ, f), MAX_INTS_TO_READ);
91 
92  auto headerSize = ints[0];
93  if (headerSize == 0) {
94  // no header for this page - insert into free list
95  freePages.insert(pageNum);
96  continue;
97  }
98 
99  // headerSize doesn't include headerSize itself
100  // We're tying ourself to headers of ints here
101  size_t numHeaderElems = headerSize / sizeof(int32_t);
102  CHECK_GE(numHeaderElems, size_t(2));
103  // We don't want to read headerSize in our header - so start
104  // reading 4 bytes past it
105  ChunkKey chunkKey(&ints[1], &ints[1 + numHeaderElems - 2]);
106  if (fileMgr->updatePageIfDeleted(this, chunkKey, ints[1], ints[2], pageNum)) {
107  continue;
108  }
109  // Last two elements of header are always PageId and Version
110  // epoch - these are not in the chunk key so seperate them
111  int32_t pageId = ints[1 + numHeaderElems - 2];
112  int32_t versionEpoch = ints[1 + numHeaderElems - 1];
113  if (chunkKey != oldChunkKey || oldPageId != pageId - (1 + skipped)) {
114  if (skipped > 0) {
115  VLOG(4) << "FId.PSz: " << fileId << "." << pageSize
116  << " Chunk key: " << show_chunk(oldChunkKey)
117  << " Page id from : " << oldPageId << " to : " << oldPageId + skipped
118  << " Epoch: " << oldVersionEpoch;
119  } else if (oldPageId != -99) {
120  VLOG(4) << "FId.PSz: " << fileId << "." << pageSize
121  << " Chunk key: " << show_chunk(oldChunkKey) << " Page id: " << oldPageId
122  << " Epoch: " << oldVersionEpoch;
123  }
124  oldPageId = pageId;
125  oldVersionEpoch = versionEpoch;
126  oldChunkKey = chunkKey;
127  skipped = 0;
128  } else {
129  skipped++;
130  }
131 
132  /* Check if version epoch is equal to
133  * or greater (note: should never be greater)
134  * than FileMgr epoch_ - this means that this
135  * page wasn't checkpointed and thus we should
136  * not use it
137  */
138  int32_t fileMgrEpoch =
139  fileMgr->epoch(chunkKey[CHUNK_KEY_DB_IDX], chunkKey[CHUNK_KEY_TABLE_IDX]);
140  if (versionEpoch > fileMgrEpoch) {
141  // First write 0 to first four bytes of
142  // header to mark as free
143  if (!g_read_only && !g_multi_instance) {
144  // TODO(sy): Confirm that proper locking is held before writing here.
145  freePageImmediate(pageNum);
146  LOG(WARNING) << "Was not checkpointed: Chunk key: " << show_chunk(chunkKey)
147  << " Page id: " << pageId << " Epoch: " << versionEpoch
148  << " FileMgrEpoch " << fileMgrEpoch << endl;
149  }
150  } else { // page was checkpointed properly
151  Page page(fileId, pageNum);
152  headerVec.emplace_back(chunkKey, pageId, versionEpoch, page);
153  }
154  }
155  // printlast
156  if (oldPageId != -99) {
157  if (skipped > 0) {
158  VLOG(4) << "FId.PSz: " << fileId << "." << pageSize
159  << " Chunk key: " << show_chunk(oldChunkKey)
160  << " Page id from : " << oldPageId << " to : " << oldPageId + skipped
161  << " Epoch: " << oldVersionEpoch;
162  } else {
163  VLOG(4) << "FId.PSz: " << fileId << "." << pageSize
164  << " Chunk key: " << show_chunk(oldChunkKey) << " Page id: " << oldPageId
165  << " Epoch: " << oldVersionEpoch;
166  }
167  }
168 }
169 
170 void FileInfo::freePageDeferred(int32_t pageId) {
171  std::lock_guard<std::mutex> lock(freePagesMutex_);
172  freePages.insert(pageId);
173 }
174 
175 #ifdef ENABLE_CRASH_CORRUPTION_TEST
176 #warning "!!!!! DB corruption crash test is enabled !!!!!"
177 #include <signal.h>
178 static bool goto_crash;
179 static void sighandler(int sig) {
180  if (getenv("ENABLE_CRASH_CORRUPTION_TEST"))
181  goto_crash = true;
182 }
183 #endif
184 
185 void FileInfo::freePage(int pageId, const bool isRolloff, int32_t epoch) {
186  std::lock_guard<std::mutex> lock(readWriteMutex_);
187  int32_t epoch_freed_page[2] = {DELETE_CONTINGENT, epoch};
188  if (isRolloff) {
189  epoch_freed_page[0] = ROLLOFF_CONTINGENT;
190  }
192  pageId * pageSize + sizeof(int32_t),
193  sizeof(epoch_freed_page),
194  reinterpret_cast<const int8_t*>(epoch_freed_page));
195  fileMgr->free_page(std::make_pair(this, pageId));
196  isDirty = true;
197 
198 #ifdef ENABLE_CRASH_CORRUPTION_TEST
199  signal(SIGUSR2, sighandler);
200  if (goto_crash)
201  CHECK(pageId % 8 != 4);
202 #endif
203 }
204 
206  // returns -1 if there is no free page
207  std::lock_guard<std::mutex> lock(freePagesMutex_);
208  if (freePages.size() == 0) {
209  return -1;
210  }
211  auto pageIt = freePages.begin();
212  int32_t pageNum = *pageIt;
213  freePages.erase(pageIt);
214  return pageNum;
215 }
216 
217 std::string FileInfo::print() const {
218  std::stringstream ss;
219  ss << "File: " << fileId << std::endl;
220  ss << "Size: " << size() << std::endl;
221  ss << "Used: " << used() << std::endl;
222  ss << "Free: " << available() << std::endl;
223  return ss.str();
224 }
225 
227  std::lock_guard<std::mutex> lock(readWriteMutex_);
228  if (isDirty) {
229  if (fflush(f) != 0) {
230  LOG(FATAL) << "Error trying to flush changes to disk, the error was: "
231  << std::strerror(errno);
232  }
233 #ifdef __APPLE__
234  const int32_t sync_result = fcntl(fileno(f), 51);
235 #else
236  const int32_t sync_result = heavyai::fsync(fileno(f));
237 #endif
238  if (sync_result == 0) {
239  isDirty = false;
240  }
241  return sync_result;
242  }
243  return 0; // if file was not dirty and no syncing was needed
244 }
245 
246 void FileInfo::freePageImmediate(int32_t page_num) {
247  // we should not get here but putting protection in place
248  // as it seems we are no guaranteed to have f/synced so
249  // protecting from RO trying to write
250  if (!g_read_only && !g_multi_instance) {
251  int32_t zero{0};
253  f, page_num * pageSize, sizeof(int32_t), reinterpret_cast<const int8_t*>(&zero));
254  freePageDeferred(page_num);
255  }
256 }
257 
258 // Overwrites delete/rollback contingents by re-writing chunk key to page.
259 void FileInfo::recoverPage(const ChunkKey& chunk_key, int32_t page_num) {
260  // we should not get here but putting protection in place
261  // as it seems we are no guaranteed to have f/synced so
262  // protecting from RO trying to write
263  if (!g_read_only && !g_multi_instance) {
265  page_num * pageSize + sizeof(int32_t),
266  2 * sizeof(int32_t),
267  reinterpret_cast<const int8_t*>(chunk_key.data()));
268  }
269 }
270 
271 bool is_page_deleted_with_checkpoint(int32_t table_epoch,
272  int32_t page_epoch,
273  int32_t contingent) {
274  const bool delete_contingent =
275  (contingent == DELETE_CONTINGENT || contingent == ROLLOFF_CONTINGENT);
276  // Check if page was deleted with a checkpointed epoch
277  if (delete_contingent && (table_epoch >= page_epoch)) {
278  return true;
279  }
280  return false;
281 }
282 
283 bool is_page_deleted_without_checkpoint(int32_t table_epoch,
284  int32_t page_epoch,
285  int32_t contingent) {
286  const bool delete_contingent =
287  (contingent == DELETE_CONTINGENT || contingent == ROLLOFF_CONTINGENT);
288  // Check if page was deleted but the epoch was not yet checkpointed.
289  if (delete_contingent && (table_epoch < page_epoch)) {
290  return true;
291  }
292  return false;
293 }
294 } // namespace File_Namespace
virtual int32_t epoch(int32_t db_id, int32_t tb_id) const
Returns current value of epoch - should be one greater than recorded at last checkpoint. Because FileMgr only contains buffers from one table we can just return the FileMgr&#39;s epoch instead of finding a table-specific epoch.
Definition: FileMgr.h:277
#define CHECK_EQ(x, y)
Definition: Logger.h:230
std::vector< int > ChunkKey
Definition: types.h:36
size_t write(const size_t offset, const size_t size, const int8_t *buf)
Definition: FileInfo.cpp:63
bool g_multi_instance
Definition: heavyai_locks.h:21
bool is_page_deleted_without_checkpoint(int32_t table_epoch, int32_t page_epoch, int32_t contingent)
Definition: FileInfo.cpp:283
A logical page (Page) belongs to a file on disk.
Definition: Page.h:46
#define LOG(tag)
Definition: Logger.h:216
std::mutex readWriteMutex_
Definition: FileInfo.h:64
#define CHUNK_KEY_DB_IDX
Definition: types.h:38
void freePageImmediate(int32_t page_num)
Definition: FileInfo.cpp:246
This file includes the class specification for the FILE manager (FileMgr), and related data structure...
#define CHECK_GE(x, y)
Definition: Logger.h:235
std::string print() const
Prints a summary of the file to stdout.
Definition: FileInfo.cpp:217
virtual bool updatePageIfDeleted(FileInfo *file_info, ChunkKey &chunk_key, int32_t contingent, int32_t page_epoch, int32_t page_num)
deletes or recovers a page based on last checkpointed epoch.
Definition: FileMgr.cpp:1615
constexpr double f
Definition: Utm.h:31
std::string show_chunk(const ChunkKey &key)
Definition: types.h:98
void freePage(int32_t pageId, const bool isRolloff, int32_t epoch)
Definition: FileInfo.cpp:185
size_t write(FILE *f, const size_t offset, const size_t size, const int8_t *buf)
Writes the specified number of bytes to the offset position in file f from buf.
Definition: File.cpp:150
std::set< size_t > freePages
Definition: FileInfo.h:62
size_t pageSize
file stream object for the represented file
Definition: FileInfo.h:59
size_t read(FILE *f, const size_t offset, const size_t size, int8_t *buf)
Reads the specified number of bytes from the offset position in file f into buf.
Definition: File.cpp:142
constexpr int32_t DELETE_CONTINGENT
A FileInfo type has a file pointer and metadata about a file.
Definition: FileInfo.h:51
void init(LogOptions const &log_opts)
Definition: Logger.cpp:308
size_t used() const
Returns the amount of used bytes; size() - available()
Definition: FileInfo.h:114
void initNewFile()
Adds all pages to freePages and zeroes first four bytes of header.
Definition: FileInfo.cpp:50
size_t size() const
Returns the number of bytes used by the file.
Definition: FileInfo.h:93
#define CHUNK_KEY_TABLE_IDX
Definition: types.h:39
std::mutex freePagesMutex_
set of page numbers of free pages
Definition: FileInfo.h:63
constexpr int32_t ROLLOFF_CONTINGENT
Definition: FileInfo.h:52
bool g_read_only
Definition: File.cpp:40
~FileInfo()
Destructor.
Definition: FileInfo.cpp:43
size_t read(const size_t offset, const size_t size, int8_t *buf)
Definition: FileInfo.cpp:69
bool is_page_deleted_with_checkpoint(int32_t table_epoch, int32_t page_epoch, int32_t contingent)
Definition: FileInfo.cpp:271
virtual void free_page(std::pair< FileInfo *, int32_t > &&page)
Definition: FileMgr.cpp:1201
FILE * f
unique file identifier (i.e., used for a file name)
Definition: FileInfo.h:58
int fsync(int fd)
Definition: heavyai_fs.cpp:62
This file contains the declaration and definition of a Page type and a MultiPage type.
void openExistingFile(std::vector< HeaderInfo > &headerVec)
Definition: FileInfo.cpp:74
#define CHECK(condition)
Definition: Logger.h:222
void recoverPage(const ChunkKey &chunk_key, int32_t page_num)
Definition: FileInfo.cpp:259
void close(FILE *f)
Closes the file pointed to by the FILE pointer.
Definition: File.cpp:128
void freePageDeferred(int32_t pageId)
Definition: FileInfo.cpp:170
size_t available() const
Returns the number of free bytes available.
Definition: FileInfo.h:100
size_t numPages
the fixed size of each page in the file
Definition: FileInfo.h:60
bool isDirty
the number of pages in the file
Definition: FileInfo.h:61
#define VLOG(n)
Definition: Logger.h:316