OmniSciDB  085a039ca4
 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 2017 MapD Technologies, 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 namespace File_Namespace {
27 
28 FileInfo::FileInfo(FileMgr* fileMgr,
29  const int32_t fileId,
30  FILE* f,
31  const size_t pageSize,
32  size_t numPages,
33  bool init)
34  : fileMgr(fileMgr), fileId(fileId), f(f), pageSize(pageSize), numPages(numPages) {
35  if (init) {
36  initNewFile();
37  }
38 }
39 
41  // close file, if applicable
42  if (f) {
43  close(f);
44  }
45 }
46 
48  // initialize pages and free page list
49  // Also zeroes out first four bytes of every header
50 
51  int32_t headerSize = 0;
52  int8_t* headerSizePtr = (int8_t*)(&headerSize);
53  for (size_t pageId = 0; pageId < numPages; ++pageId) {
54  File_Namespace::write(f, pageId * pageSize, sizeof(int32_t), headerSizePtr);
55  freePages.insert(pageId);
56  }
57  isDirty = true;
58 }
59 
60 size_t FileInfo::write(const size_t offset, const size_t size, const int8_t* buf) {
61  std::lock_guard<std::mutex> lock(readWriteMutex_);
62  isDirty = true;
63  return File_Namespace::write(f, offset, size, buf);
64 }
65 
66 size_t FileInfo::read(const size_t offset, const size_t size, int8_t* buf) {
67  std::lock_guard<std::mutex> lock(readWriteMutex_);
68  return File_Namespace::read(f, offset, size, buf);
69 }
70 
71 void FileInfo::openExistingFile(std::vector<HeaderInfo>& headerVec) {
72  // HeaderInfo is defined in Page.h
73 
74  // Oct 2020: Changing semantics such that fileMgrEpoch should be last checkpointed
75  // epoch, not incremented epoch. This changes some of the gt/gte/lt/lte comparison below
76  ChunkKey oldChunkKey(4);
77  int32_t oldPageId = -99;
78  int32_t oldVersionEpoch = -99;
79  int32_t skipped = 0;
80  for (size_t pageNum = 0; pageNum < numPages; ++pageNum) {
81  // TODO(Misiu): It would be nice to replace this array with a struct that would
82  // clarify what is being read and have a single definition (currently this code is
83  // replicated in TableArchiver and possibly elsewhere).
84  constexpr size_t MAX_INTS_TO_READ{10}; // currently use 1+6 ints
85  int32_t ints[MAX_INTS_TO_READ];
86  CHECK_EQ(fseek(f, pageNum * pageSize, SEEK_SET), 0);
87  CHECK_EQ(fread(ints, sizeof(int32_t), MAX_INTS_TO_READ, f), MAX_INTS_TO_READ);
88 
89  auto headerSize = ints[0];
90  if (headerSize == 0) {
91  // no header for this page - insert into free list
92  freePages.insert(pageNum);
93  continue;
94  }
95 
96  // headerSize doesn't include headerSize itself
97  // We're tying ourself to headers of ints here
98  size_t numHeaderElems = headerSize / sizeof(int32_t);
99  CHECK_GE(numHeaderElems, size_t(2));
100  // We don't want to read headerSize in our header - so start
101  // reading 4 bytes past it
102  ChunkKey chunkKey(&ints[1], &ints[1 + numHeaderElems - 2]);
103  if (fileMgr->updatePageIfDeleted(this, chunkKey, ints[1], ints[2], pageNum)) {
104  continue;
105  }
106  // Last two elements of header are always PageId and Version
107  // epoch - these are not in the chunk key so seperate them
108  int32_t pageId = ints[1 + numHeaderElems - 2];
109  int32_t versionEpoch = ints[1 + numHeaderElems - 1];
110  if (chunkKey != oldChunkKey || oldPageId != pageId - (1 + skipped)) {
111  if (skipped > 0) {
112  VLOG(4) << "FId.PSz: " << fileId << "." << pageSize
113  << " Chunk key: " << show_chunk(oldChunkKey)
114  << " Page id from : " << oldPageId << " to : " << oldPageId + skipped
115  << " Epoch: " << oldVersionEpoch;
116  } else if (oldPageId != -99) {
117  VLOG(4) << "FId.PSz: " << fileId << "." << pageSize
118  << " Chunk key: " << show_chunk(oldChunkKey) << " Page id: " << oldPageId
119  << " Epoch: " << oldVersionEpoch;
120  }
121  oldPageId = pageId;
122  oldVersionEpoch = versionEpoch;
123  oldChunkKey = chunkKey;
124  skipped = 0;
125  } else {
126  skipped++;
127  }
128 
129  /* Check if version epoch is equal to
130  * or greater (note: should never be greater)
131  * than FileMgr epoch_ - this means that this
132  * page wasn't checkpointed and thus we should
133  * not use it
134  */
135  int32_t fileMgrEpoch =
136  fileMgr->epoch(chunkKey[CHUNK_KEY_DB_IDX], chunkKey[CHUNK_KEY_TABLE_IDX]);
137  if (versionEpoch > fileMgrEpoch) {
138  // First write 0 to first four bytes of
139  // header to mark as free
140  if (!g_read_only) {
141  freePageImmediate(pageNum);
142  }
143  LOG(WARNING) << "Was not checkpointed: Chunk key: " << show_chunk(chunkKey)
144  << " Page id: " << pageId << " Epoch: " << versionEpoch
145  << " FileMgrEpoch " << fileMgrEpoch << endl;
146  } else { // page was checkpointed properly
147  Page page(fileId, pageNum);
148  headerVec.emplace_back(chunkKey, pageId, versionEpoch, page);
149  }
150  }
151  // printlast
152  if (oldPageId != -99) {
153  if (skipped > 0) {
154  VLOG(4) << "FId.PSz: " << fileId << "." << pageSize
155  << " Chunk key: " << show_chunk(oldChunkKey)
156  << " Page id from : " << oldPageId << " to : " << oldPageId + skipped
157  << " Epoch: " << oldVersionEpoch;
158  } else {
159  VLOG(4) << "FId.PSz: " << fileId << "." << pageSize
160  << " Chunk key: " << show_chunk(oldChunkKey) << " Page id: " << oldPageId
161  << " Epoch: " << oldVersionEpoch;
162  }
163  }
164 }
165 
166 void FileInfo::freePageDeferred(int32_t pageId) {
167  std::lock_guard<std::mutex> lock(freePagesMutex_);
168  freePages.insert(pageId);
169 }
170 
171 #ifdef ENABLE_CRASH_CORRUPTION_TEST
172 #warning "!!!!! DB corruption crash test is enabled !!!!!"
173 #include <signal.h>
174 static bool goto_crash;
175 static void sighandler(int sig) {
176  if (getenv("ENABLE_CRASH_CORRUPTION_TEST"))
177  goto_crash = true;
178 }
179 #endif
180 
181 void FileInfo::freePage(int pageId, const bool isRolloff, int32_t epoch) {
182  std::lock_guard<std::mutex> lock(readWriteMutex_);
183  int32_t epoch_freed_page[2] = {DELETE_CONTINGENT, epoch};
184  if (isRolloff) {
185  epoch_freed_page[0] = ROLLOFF_CONTINGENT;
186  }
188  pageId * pageSize + sizeof(int32_t),
189  sizeof(epoch_freed_page),
190  reinterpret_cast<const int8_t*>(epoch_freed_page));
191  fileMgr->free_page(std::make_pair(this, pageId));
192  isDirty = true;
193 
194 #ifdef ENABLE_CRASH_CORRUPTION_TEST
195  signal(SIGUSR2, sighandler);
196  if (goto_crash)
197  CHECK(pageId % 8 != 4);
198 #endif
199 }
200 
202  // returns -1 if there is no free page
203  std::lock_guard<std::mutex> lock(freePagesMutex_);
204  if (freePages.size() == 0) {
205  return -1;
206  }
207  auto pageIt = freePages.begin();
208  int32_t pageNum = *pageIt;
209  freePages.erase(pageIt);
210  return pageNum;
211 }
212 
213 void FileInfo::print(bool pagesummary) {
214  std::cout << "File: " << fileId << std::endl;
215  std::cout << "Size: " << size() << std::endl;
216  std::cout << "Used: " << used() << std::endl;
217  std::cout << "Free: " << available() << std::endl;
218  if (!pagesummary) {
219  return;
220  }
221 }
223  std::lock_guard<std::mutex> lock(readWriteMutex_);
224  if (isDirty) {
225  if (fflush(f) != 0) {
226  LOG(FATAL) << "Error trying to flush changes to disk, the error was: "
227  << std::strerror(errno);
228  }
229 #ifdef __APPLE__
230  const int32_t sync_result = fcntl(fileno(f), 51);
231 #else
232  const int32_t sync_result = heavyai::fsync(fileno(f));
233 #endif
234  if (sync_result == 0) {
235  isDirty = false;
236  }
237  return sync_result;
238  }
239  return 0; // if file was not dirty and no syncing was needed
240 }
241 
242 void FileInfo::freePageImmediate(int32_t page_num) {
243  // we should not get here but putting protection in place
244  // as it seems we are no guaranteed to have f/synced so
245  // protecting from RO trying to write
246  if (!g_read_only) {
247  int32_t zero{0};
249  f, page_num * pageSize, sizeof(int32_t), reinterpret_cast<const int8_t*>(&zero));
250  freePageDeferred(page_num);
251  }
252 }
253 
254 // Overwrites delete/rollback contingents by re-writing chunk key to page.
255 void FileInfo::recoverPage(const ChunkKey& chunk_key, int32_t page_num) {
256  // we should not get here but putting protection in place
257  // as it seems we are no guaranteed to have f/synced so
258  // protecting from RO trying to write
259  if (!g_read_only) {
261  page_num * pageSize + sizeof(int32_t),
262  2 * sizeof(int32_t),
263  reinterpret_cast<const int8_t*>(chunk_key.data()));
264  }
265 }
266 
267 bool is_page_deleted_with_checkpoint(int32_t table_epoch,
268  int32_t page_epoch,
269  int32_t contingent) {
270  const bool delete_contingent =
271  (contingent == DELETE_CONTINGENT || contingent == ROLLOFF_CONTINGENT);
272  // Check if page was deleted with a checkpointed epoch
273  if (delete_contingent && (table_epoch >= page_epoch)) {
274  return true;
275  }
276  return false;
277 }
278 
279 bool is_page_deleted_without_checkpoint(int32_t table_epoch,
280  int32_t page_epoch,
281  int32_t contingent) {
282  const bool delete_contingent =
283  (contingent == DELETE_CONTINGENT || contingent == ROLLOFF_CONTINGENT);
284  // Check if page was deleted but the epoch was not yet checkpointed.
285  if (delete_contingent && (table_epoch < page_epoch)) {
286  return true;
287  }
288  return false;
289 }
290 } // 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:280
#define CHECK_EQ(x, y)
Definition: Logger.h:231
std::vector< int > ChunkKey
Definition: types.h:37
size_t write(const size_t offset, const size_t size, const int8_t *buf)
Definition: FileInfo.cpp:60
bool is_page_deleted_without_checkpoint(int32_t table_epoch, int32_t page_epoch, int32_t contingent)
Definition: FileInfo.cpp:279
A logical page (Page) belongs to a file on disk.
Definition: Page.h:46
#define LOG(tag)
Definition: Logger.h:217
std::mutex readWriteMutex_
Definition: FileInfo.h:64
#define CHUNK_KEY_DB_IDX
Definition: types.h:39
void freePageImmediate(int32_t page_num)
Definition: FileInfo.cpp:242
#define CHECK_GE(x, y)
Definition: Logger.h:236
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:1600
constexpr double f
Definition: Utm.h:31
std::string show_chunk(const ChunkKey &key)
Definition: types.h:99
void freePage(int32_t pageId, const bool isRolloff, int32_t epoch)
Definition: FileInfo.cpp:181
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:306
void initNewFile()
Adds all pages to freePages and zeroes first four bytes of header.
Definition: FileInfo.cpp:47
size_t size() const
Returns the number of bytes used by the file.
Definition: FileInfo.h:92
#define CHUNK_KEY_TABLE_IDX
Definition: types.h:40
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:40
size_t read(const size_t offset, const size_t size, int8_t *buf)
Definition: FileInfo.cpp:66
bool is_page_deleted_with_checkpoint(int32_t table_epoch, int32_t page_epoch, int32_t contingent)
Definition: FileInfo.cpp:267
virtual void free_page(std::pair< FileInfo *, int32_t > &&page)
Definition: FileMgr.cpp:1186
FILE * f
unique file identifier (i.e., used for a file name)
Definition: FileInfo.h:58
int fsync(int fd)
Definition: omnisci_fs.cpp:60
void openExistingFile(std::vector< HeaderInfo > &headerVec)
Definition: FileInfo.cpp:71
#define CHECK(condition)
Definition: Logger.h:223
void recoverPage(const ChunkKey &chunk_key, int32_t page_num)
Definition: FileInfo.cpp:255
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:166
size_t used()
Returns the amount of used bytes; size() - available()
Definition: FileInfo.h:108
void print(bool pagesummary)
Prints a summary of the file to stdout.
Definition: FileInfo.cpp:213
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:317
size_t available()
Returns the number of free bytes available.
Definition: FileInfo.h:99