OmniSciDB  0264ff685a
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, 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  const int32_t fileMgrEpoch) {
73  // HeaderInfo is defined in Page.h
74 
75  // Oct 2020: Changing semantics such that fileMgrEpoch should be last checkpointed
76  // epoch, not incremented epoch. This changes some of the gt/gte/lt/lte comparison below
77  ChunkKey oldChunkKey(4);
78  int32_t oldPageId = -99;
79  int32_t oldVersionEpoch = -99;
80  int32_t skipped = 0;
81  for (size_t pageNum = 0; pageNum < numPages; ++pageNum) {
82  int32_t headerSize;
83 
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  headerSize = ints[0];
90  const bool should_delete_deleted =
91  DELETE_CONTINGENT == ints[1] && fileMgrEpoch >= ints[2];
92  const bool should_delete_rolled_off =
93  ROLLOFF_CONTINGENT == ints[1] && fileMgrEpoch >= ints[2];
94  if (should_delete_deleted || should_delete_rolled_off) {
95  int32_t zero{0};
96  File_Namespace::write(f, pageNum * pageSize, sizeof(int32_t), (int8_t*)&zero);
97  headerSize = 0;
98  }
99 
100  if (headerSize != 0) {
101  // headerSize doesn't include headerSize itself
102  // We're tying ourself to headers of ints here
103  size_t numHeaderElems = headerSize / sizeof(int32_t);
104  CHECK_GE(numHeaderElems, size_t(2));
105  // We don't want to read headerSize in our header - so start
106  // reading 4 bytes past it
107 
108  // always derive dbid/tbid from FileMgr
109  ChunkKey chunkKey(&ints[1], &ints[1 + numHeaderElems - 2]);
110  chunkKey[0] = fileMgr->get_fileMgrKey().first;
111  chunkKey[1] = fileMgr->get_fileMgrKey().second;
112  // recover page in case a crash failed deletion of this page
113  if (ints[1] == DELETE_CONTINGENT || ints[1] == ROLLOFF_CONTINGENT) {
115  pageNum * pageSize + sizeof(int32_t),
116  2 * sizeof(int32_t),
117  (int8_t*)&chunkKey[0]);
118  }
119 
120  // Last two elements of header are always PageId and Version
121  // epoch - these are not in the chunk key so seperate them
122  int32_t pageId = ints[1 + numHeaderElems - 2];
123  int32_t versionEpoch = ints[1 + numHeaderElems - 1];
124  if (chunkKey != oldChunkKey || oldPageId != pageId - (1 + skipped)) {
125  if (skipped > 0) {
126  VLOG(4) << "FId.PSz: " << fileId << "." << pageSize
127  << " Chunk key: " << show_chunk(oldChunkKey)
128  << " Page id from : " << oldPageId << " to : " << oldPageId + skipped
129  << " Epoch: " << oldVersionEpoch;
130  } else if (oldPageId != -99) {
131  VLOG(4) << "FId.PSz: " << fileId << "." << pageSize
132  << " Chunk key: " << show_chunk(oldChunkKey)
133  << " Page id: " << oldPageId << " Epoch: " << oldVersionEpoch;
134  }
135  oldPageId = pageId;
136  oldVersionEpoch = versionEpoch;
137  oldChunkKey = chunkKey;
138  skipped = 0;
139  } else {
140  skipped++;
141  }
142 
143  /* Check if version epoch is equal to
144  * or greater (note: should never be greater)
145  * than FileMgr epoch_ - this means that this
146  * page wasn't checkpointed and thus we should
147  * not use it
148  */
149  if (versionEpoch > fileMgrEpoch) {
150  // First write 0 to first four bytes of
151  // header to mark as free
152  headerSize = 0;
154  f, pageNum * pageSize, sizeof(int32_t), (int8_t*)&headerSize);
155  // Now add page to free list
156  freePages.insert(pageNum);
157  LOG(WARNING) << "Was not checkpointed: Chunk key: " << show_chunk(chunkKey)
158  << " Page id: " << pageId << " Epoch: " << versionEpoch
159  << " FileMgrEpoch " << fileMgrEpoch << endl;
160 
161  } else { // page was checkpointed properly
162  Page page(fileId, pageNum);
163  headerVec.emplace_back(chunkKey, pageId, versionEpoch, page);
164  }
165  } else { // no header for this page - insert into free list
166  freePages.insert(pageNum);
167  }
168  }
169  // printlast
170  if (oldPageId != -99) {
171  if (skipped > 0) {
172  VLOG(4) << "FId.PSz: " << fileId << "." << pageSize
173  << " Chunk key: " << show_chunk(oldChunkKey)
174  << " Page id from : " << oldPageId << " to : " << oldPageId + skipped
175  << " Epoch: " << oldVersionEpoch;
176  } else {
177  VLOG(4) << "FId.PSz: " << fileId << "." << pageSize
178  << " Chunk key: " << show_chunk(oldChunkKey) << " Page id: " << oldPageId
179  << " Epoch: " << oldVersionEpoch;
180  }
181  }
182 }
183 
184 void FileInfo::freePageDeferred(int32_t pageId) {
185  std::lock_guard<std::mutex> lock(freePagesMutex_);
186  freePages.insert(pageId);
187 }
188 
189 #ifdef ENABLE_CRASH_CORRUPTION_TEST
190 #warning "!!!!! DB corruption crash test is enabled !!!!!"
191 #include <signal.h>
192 static bool goto_crash;
193 static void sighandler(int sig) {
194  if (getenv("ENABLE_CRASH_CORRUPTION_TEST"))
195  goto_crash = true;
196 }
197 #endif
198 
199 void FileInfo::freePage(int pageId, const bool isRolloff) {
200  std::lock_guard<std::mutex> lock(readWriteMutex_);
201 #define RESILIENT_PAGE_HEADER
202 #ifdef RESILIENT_PAGE_HEADER
203  int epoch_freed_page[2] = {DELETE_CONTINGENT, fileMgr->epoch()};
204  if (isRolloff) {
205  epoch_freed_page[0] = ROLLOFF_CONTINGENT;
206  }
208  pageId * pageSize + sizeof(int32_t),
209  sizeof(epoch_freed_page),
210  (int8_t*)epoch_freed_page);
211  fileMgr->free_page(std::make_pair(this, pageId));
212 #else
213  int32_t zeroVal = 0;
214  int8_t* zeroAddr = reinterpret_cast<int8_t*>(&zeroVal);
215  File_Namespace::write(f, pageId * pageSize, sizeof(int32_t), zeroAddr);
216  std::lock_guard<std::mutex> lock(freePagesMutex_);
217  freePages.insert(pageId);
218 #endif // RESILIENT_PAGE_HEADER
219  isDirty = true;
220 
221 #ifdef ENABLE_CRASH_CORRUPTION_TEST
222  signal(SIGUSR2, sighandler);
223  if (goto_crash)
224  CHECK(pageId % 8 != 4);
225 #endif
226 }
227 
229  // returns -1 if there is no free page
230  std::lock_guard<std::mutex> lock(freePagesMutex_);
231  if (freePages.size() == 0) {
232  return -1;
233  }
234  auto pageIt = freePages.begin();
235  int32_t pageNum = *pageIt;
236  freePages.erase(pageIt);
237  return pageNum;
238 }
239 
240 void FileInfo::print(bool pagesummary) {
241  std::cout << "File: " << fileId << std::endl;
242  std::cout << "Size: " << size() << std::endl;
243  std::cout << "Used: " << used() << std::endl;
244  std::cout << "Free: " << available() << std::endl;
245  if (!pagesummary) {
246  return;
247  }
248 }
250  std::lock_guard<std::mutex> lock(readWriteMutex_);
251  if (isDirty) {
252  if (fflush(f) != 0) {
253  LOG(FATAL) << "Error trying to flush changes to disk, the error was: "
254  << std::strerror(errno);
255  }
256 #ifdef __APPLE__
257  const int32_t sync_result = fcntl(fileno(f), 51);
258 #else
259  const int32_t sync_result = omnisci::fsync(fileno(f));
260 #endif
261  if (sync_result == 0) {
262  isDirty = false;
263  }
264  return sync_result;
265  }
266  return 0; // if file was not dirty and no syncing was needed
267 }
268 
269 } // namespace File_Namespace
#define CHECK_EQ(x, y)
Definition: Logger.h:205
A logical page (Page) belongs to a file on disk.
Definition: Page.h:46
#define LOG(tag)
Definition: Logger.h:188
std::mutex readWriteMutex_
Definition: FileInfo.h:64
#define CHECK_GE(x, y)
Definition: Logger.h:210
const std::pair< const int32_t, const int32_t > get_fileMgrKey() const
Definition: FileMgr.h:308
size_t size() const
Returns the number of bytes used by the file.
Definition: FileInfo.h:92
size_t write(const size_t offset, const size_t size, int8_t *buf)
Definition: FileInfo.cpp:60
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:118
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:280
void initNewFile()
Adds all pages to freePages and zeroes first four bytes of header.
Definition: FileInfo.cpp:47
std::mutex freePagesMutex_
set of page numbers of free pages
Definition: FileInfo.h:63
int fsync(int fd)
Definition: omnisci_fs.cpp:60
void freePage(int32_t pageId, const bool isRolloff)
Definition: FileInfo.cpp:199
constexpr int32_t ROLLOFF_CONTINGENT
Definition: FileInfo.h:52
void openExistingFile(std::vector< HeaderInfo > &headerVec, const int32_t fileMgrEpoch)
Definition: FileInfo.cpp:71
~FileInfo()
Destructor.
Definition: FileInfo.cpp:40
std::string show_chunk(const ChunkKey &key)
Definition: types.h:73
size_t read(const size_t offset, const size_t size, int8_t *buf)
Definition: FileInfo.cpp:66
int32_t epoch()
Returns current value of epoch - should be one greater than recorded at last checkpoint.
Definition: FileMgr.h:245
void free_page(std::pair< FileInfo *, int32_t > &&page)
Definition: FileMgr.cpp:1177
FILE * f
unique file identifier (i.e., used for a file name)
Definition: FileInfo.h:58
size_t write(FILE *f, const size_t offset, const size_t size, int8_t *buf)
Writes the specified number of bytes to the offset position in file f from buf.
Definition: File.cpp:126
#define CHECK(condition)
Definition: Logger.h:197
std::vector< int > ChunkKey
Definition: types.h:37
void close(FILE *f)
Closes the file pointed to by the FILE pointer.
Definition: File.cpp:107
void freePageDeferred(int32_t pageId)
Definition: FileInfo.cpp:184
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:240
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:291
size_t available()
Returns the number of free bytes available.
Definition: FileInfo.h:99