OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FileBuffer.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 
24 #include <future>
25 #include <map>
26 #include <thread>
27 #include <utility>
29 #include "Shared/File.h"
30 #include "Shared/checked_alloc.h"
31 #include "Shared/scope.h"
32 
33 using namespace std;
34 
35 namespace File_Namespace {
36 
37 FileBuffer::FileBuffer(FileMgr* fm,
38  const size_t pageSize,
39  const ChunkKey& chunkKey,
40  const size_t initialSize)
41  : AbstractBuffer(fm->getDeviceId())
42  , fm_(fm)
43  , metadataPageSize_(fm_->getMetadataPageSize())
44  , metadataPages_(metadataPageSize_)
45  , pageSize_(pageSize)
46  , chunkKey_(chunkKey) {
47  // Create a new FileBuffer
48  CHECK(fm_);
52  //@todo reintroduce initialSize - need to develop easy way of
53  // differentiating these pre-allocated pages from "written-to" pages
54  /*
55  if (initalSize > 0) {
56  // should expand to initialSize bytes
57  size_t initialNumPages = (initalSize + pageSize_ -1) / pageSize_;
58  int32_t epoch = fm_->epoch();
59  for (size_t pageNum = 0; pageNum < initialNumPages; ++pageNum) {
60  Page page = addNewMultiPage(epoch);
61  writeHeader(page,pageNum,epoch);
62  }
63  }
64  */
65 }
66 
68  const size_t pageSize,
69  const ChunkKey& chunkKey,
70  const SQLTypeInfo sqlType,
71  const size_t initialSize)
72  : AbstractBuffer(fm->getDeviceId(), sqlType)
73  , fm_(fm)
74  , metadataPageSize_(fm->getMetadataPageSize())
75  , metadataPages_(metadataPageSize_)
76  , pageSize_(pageSize)
77  , chunkKey_(chunkKey) {
78  CHECK(fm_);
81 }
82 
84  /* const size_t pageSize,*/ const ChunkKey& chunkKey,
85  const std::vector<HeaderInfo>::const_iterator& headerStartIt,
86  const std::vector<HeaderInfo>::const_iterator& headerEndIt)
87  : AbstractBuffer(fm->getDeviceId())
88  , fm_(fm)
89  , metadataPageSize_(fm->getMetadataPageSize())
90  , metadataPages_(metadataPageSize_)
91  , pageSize_(0)
92  , chunkKey_(chunkKey) {
93  // We are being assigned an existing FileBuffer on disk
94 
95  CHECK(fm_);
97  int32_t lastPageId = -1;
98  int32_t curPageId = 0;
99  for (auto vecIt = headerStartIt; vecIt != headerEndIt; ++vecIt) {
100  curPageId = vecIt->pageId;
101 
102  // We only want to read last metadata page
103  if (curPageId == -1) { // stats page
104  metadataPages_.push(vecIt->page, vecIt->versionEpoch);
105  } else {
106  if (curPageId != lastPageId) {
107  // protect from bad data on disk, and give diagnostics
108  if (fm->failOnReadError()) {
109  if (curPageId != lastPageId + 1) {
110  LOG(FATAL) << "Failure reading DB file " << show_chunk(chunkKey)
111  << " Current page " << curPageId << " last page " << lastPageId
112  << " epoch " << vecIt->versionEpoch;
113  }
114  }
115  if (lastPageId == -1) { // If we are on first real page
117  }
118  MultiPage multiPage(pageSize_);
119  multiPages_.push_back(multiPage);
120  lastPageId = curPageId;
121  }
122  multiPages_.back().push(vecIt->page, vecIt->versionEpoch);
123  }
124  }
125  if (curPageId == -1) { // meaning there was only a metadata page
127  }
128 }
129 
131  // need to free pages
132  // NOP
133 }
134 
135 void FileBuffer::reserve(const size_t numBytes) {
136  size_t numPagesRequested = (numBytes + pageSize_ - 1) / pageSize_;
137  size_t numCurrentPages = multiPages_.size();
138  auto epoch = getFileMgrEpoch();
139  for (size_t pageNum = numCurrentPages; pageNum < numPagesRequested; ++pageNum) {
140  Page page = addNewMultiPage(epoch);
141  writeHeader(page, pageNum, epoch);
142  }
143 }
144 
146  // 3 * sizeof(int32_t) is for headerSize, for pageId and versionEpoch
147  // sizeof(size_t) is for chunkSize
148  reservedHeaderSize_ = (chunkKey_.size() + 3) * sizeof(int32_t);
149  size_t headerMod = reservedHeaderSize_ % headerBufferOffset_;
150  if (headerMod > 0) {
152  }
153 }
154 
155 void FileBuffer::freePage(const Page& page) {
156  freePage(page, false);
157 }
158 
159 void FileBuffer::freePage(const Page& page, const bool isRolloff) {
160  FileInfo* fileInfo = fm_->getFileInfoForFileId(page.fileId);
161  CHECK(fileInfo);
162  fileInfo->freePage(page.pageNum, isRolloff, getFileMgrEpoch());
163 }
164 
166  size_t num_pages_freed = metadataPages_.pageVersions.size();
167  for (auto metaPageIt = metadataPages_.pageVersions.begin();
168  metaPageIt != metadataPages_.pageVersions.end();
169  ++metaPageIt) {
170  freePage(metaPageIt->page, false /* isRolloff */);
171  }
172  while (metadataPages_.pageVersions.size() > 0) {
174  }
175  return num_pages_freed;
176 }
177 
179  size_t num_pages_freed = multiPages_.size();
180  for (auto multiPageIt = multiPages_.begin(); multiPageIt != multiPages_.end();
181  ++multiPageIt) {
182  for (auto pageIt = multiPageIt->pageVersions.begin();
183  pageIt != multiPageIt->pageVersions.end();
184  ++pageIt) {
185  freePage(pageIt->page, false /* isRolloff */);
186  }
187  }
188  multiPages_.clear();
189  return num_pages_freed;
190 }
191 
193  return freeMetadataPages() + freeChunkPages();
194 }
195 
197  const int32_t targetEpoch,
198  const int32_t currentEpoch) {
199  std::vector<EpochedPage> epochedPagesToFree =
200  multiPage.freePagesBeforeEpoch(targetEpoch, currentEpoch);
201  for (const auto& epochedPageToFree : epochedPagesToFree) {
202  freePage(epochedPageToFree.page, true /* isRolloff */);
203  }
204 }
205 
206 void FileBuffer::freePagesBeforeEpoch(const int32_t targetEpoch) {
207  // This method is only safe to be called within a checkpoint, after the sync and epoch
208  // increment where a failure at any point32_t in the process would lead to a safe
209  // rollback
210  auto currentEpoch = getFileMgrEpoch();
211  CHECK_LE(targetEpoch, currentEpoch);
212  freePagesBeforeEpochForMultiPage(metadataPages_, targetEpoch, currentEpoch);
213  for (auto& multiPage : multiPages_) {
214  freePagesBeforeEpochForMultiPage(multiPage, targetEpoch, currentEpoch);
215  }
216 
217  // Check if all buffer pages can be freed
218  if (size_ == 0) {
219  size_t max_historical_buffer_size{0};
220  for (auto& epoch_page : metadataPages_.pageVersions) {
221  // Create buffer that is used to get the buffer size at the epoch version
222  FileBuffer buffer{fm_, pageSize_, chunkKey_};
223  buffer.readMetadata(epoch_page.page);
224  max_historical_buffer_size = std::max(max_historical_buffer_size, buffer.size());
225  }
226 
227  // Free all chunk pages, if none of the old chunk versions has any data
228  if (max_historical_buffer_size == 0) {
229  freeChunkPages();
230  }
231  }
232 }
233 
234 struct readThreadDS {
235  FileMgr* t_fm; // ptr to FileMgr
236  size_t t_startPage; // start page for the thread
237  size_t t_endPage; // last page for the thread
238  int8_t* t_curPtr; // pointer to the current location of the target for the thread
239  size_t t_bytesLeft; // number of bytes to be read in the thread
240  size_t t_startPageOffset; // offset - used for the first page of the buffer
241  bool t_isFirstPage; // true - for first page of the buffer, false - otherwise
242  std::vector<MultiPage> multiPages; // MultiPages of the FileBuffer passed to the thread
243 };
244 
245 static size_t readForThread(FileBuffer* fileBuffer, const readThreadDS threadDS) {
246  size_t startPage = threadDS.t_startPage; // start reading at startPage, including it
247  size_t endPage = threadDS.t_endPage; // stop reading at endPage, not including it
248  int8_t* curPtr = threadDS.t_curPtr;
249  size_t bytesLeft = threadDS.t_bytesLeft;
250  size_t totalBytesRead = 0;
251  bool isFirstPage = threadDS.t_isFirstPage;
252 
253  // Traverse the logical pages
254  for (size_t pageNum = startPage; pageNum < endPage; ++pageNum) {
255  CHECK(threadDS.multiPages[pageNum].pageSize == fileBuffer->pageSize());
256  Page page = threadDS.multiPages[pageNum].current().page;
257 
258  FileInfo* fileInfo = threadDS.t_fm->getFileInfoForFileId(page.fileId);
259  CHECK(fileInfo);
260 
261  // Read the page into the destination (dst) buffer at its
262  // current (cur) location
263  size_t bytesRead = 0;
264  if (isFirstPage) {
265  bytesRead = fileInfo->read(
266  page.pageNum * fileBuffer->pageSize() + threadDS.t_startPageOffset +
267  fileBuffer->reservedHeaderSize(),
268  min(fileBuffer->pageDataSize() - threadDS.t_startPageOffset, bytesLeft),
269  curPtr);
270  isFirstPage = false;
271  } else {
272  bytesRead = fileInfo->read(
273  page.pageNum * fileBuffer->pageSize() + fileBuffer->reservedHeaderSize(),
274  min(fileBuffer->pageDataSize(), bytesLeft),
275  curPtr);
276  }
277  curPtr += bytesRead;
278  bytesLeft -= bytesRead;
279  totalBytesRead += bytesRead;
280  }
281  CHECK(bytesLeft == 0);
282 
283  return (totalBytesRead);
284 }
285 
286 void FileBuffer::read(int8_t* const dst,
287  const size_t numBytes,
288  const size_t offset,
289  const MemoryLevel dstBufferType,
290  const int32_t deviceId) {
291  if (dstBufferType != CPU_LEVEL) {
292  LOG(FATAL) << "Unsupported Buffer type";
293  }
294 
295  // variable declarations
296  size_t startPage = offset / pageDataSize_;
297  size_t startPageOffset = offset % pageDataSize_;
298  size_t numPagesToRead =
299  (numBytes + startPageOffset + pageDataSize_ - 1) / pageDataSize_;
300  /*
301  if (startPage + numPagesToRead > multiPages_.size()) {
302  cout << "Start page: " << startPage << endl;
303  cout << "Num pages to read: " << numPagesToRead << endl;
304  cout << "Num multipages: " << multiPages_.size() << endl;
305  cout << "Offset: " << offset << endl;
306  cout << "Num bytes: " << numBytes << endl;
307  }
308  */
309 
310  CHECK(startPage + numPagesToRead <= multiPages_.size())
311  << "Requested page out of bounds";
312 
313  size_t numPagesPerThread = 0;
314  size_t numBytesCurrent = numBytes; // total number of bytes still to be read
315  size_t bytesRead = 0; // total number of bytes already being read
316  size_t bytesLeftForThread = 0; // number of bytes to be read in the thread
317  size_t numExtraPages = 0; // extra pages to be assigned one per thread as needed
318  size_t numThreads = fm_->getNumReaderThreads();
319  std::vector<readThreadDS>
320  threadDSArr; // array of threadDS, needed to avoid racing conditions
321 
322  if (numPagesToRead > numThreads) {
323  numPagesPerThread = numPagesToRead / numThreads;
324  numExtraPages = numPagesToRead - (numThreads * numPagesPerThread);
325  } else {
326  numThreads = numPagesToRead;
327  numPagesPerThread = 1;
328  }
329 
330  /* set threadDS for the first thread */
331  readThreadDS threadDS;
332  threadDS.t_fm = fm_;
333  threadDS.t_startPage = offset / pageDataSize_;
334  if (numExtraPages > 0) {
335  threadDS.t_endPage = threadDS.t_startPage + numPagesPerThread + 1;
336  numExtraPages--;
337  } else {
338  threadDS.t_endPage = threadDS.t_startPage + numPagesPerThread;
339  }
340  threadDS.t_curPtr = dst;
341  threadDS.t_startPageOffset = offset % pageDataSize_;
342  threadDS.t_isFirstPage = true;
343 
344  bytesLeftForThread = min(((threadDS.t_endPage - threadDS.t_startPage) * pageDataSize_ -
345  threadDS.t_startPageOffset),
346  numBytesCurrent);
347  threadDS.t_bytesLeft = bytesLeftForThread;
348  threadDS.multiPages = getMultiPage();
349 
350  if (numThreads == 1) {
351  bytesRead += readForThread(this, threadDS);
352  } else {
353  std::vector<std::future<size_t>> threads;
354 
355  for (size_t i = 0; i < numThreads; i++) {
356  threadDSArr.push_back(threadDS);
357  threads.push_back(
358  std::async(std::launch::async, readForThread, this, threadDSArr[i]));
359 
360  // calculate elements of threadDS
361  threadDS.t_fm = fm_;
362  threadDS.t_isFirstPage = false;
363  threadDS.t_curPtr += bytesLeftForThread;
364  threadDS.t_startPage +=
365  threadDS.t_endPage -
366  threadDS.t_startPage; // based on # of pages read on previous iteration
367  if (numExtraPages > 0) {
368  threadDS.t_endPage = threadDS.t_startPage + numPagesPerThread + 1;
369  numExtraPages--;
370  } else {
371  threadDS.t_endPage = threadDS.t_startPage + numPagesPerThread;
372  }
373  numBytesCurrent -= bytesLeftForThread;
374  bytesLeftForThread = min(
375  ((threadDS.t_endPage - threadDS.t_startPage) * pageDataSize_), numBytesCurrent);
376  threadDS.t_bytesLeft = bytesLeftForThread;
377  threadDS.multiPages = getMultiPage();
378  }
379 
380  for (auto& p : threads) {
381  p.wait();
382  }
383  for (auto& p : threads) {
384  bytesRead += p.get();
385  }
386  }
387  CHECK(bytesRead == numBytes);
388 }
389 
391  Page& destPage,
392  const size_t numBytes,
393  const size_t offset) {
394  CHECK_LE(offset + numBytes, pageDataSize_);
395  FileInfo* srcFileInfo = fm_->getFileInfoForFileId(srcPage.fileId);
396  FileInfo* destFileInfo = fm_->getFileInfoForFileId(destPage.fileId);
397 
398  int8_t* buffer = reinterpret_cast<int8_t*>(checked_malloc(numBytes));
399  ScopeGuard guard = [&buffer] { free(buffer); };
400  size_t bytesRead = srcFileInfo->read(
401  srcPage.pageNum * pageSize_ + offset + reservedHeaderSize_, numBytes, buffer);
402  CHECK(bytesRead == numBytes);
403  size_t bytesWritten = destFileInfo->write(
404  destPage.pageNum * pageSize_ + offset + reservedHeaderSize_, numBytes, buffer);
405  CHECK(bytesWritten == numBytes);
406 }
407 
408 Page FileBuffer::addNewMultiPage(const int32_t epoch) {
409  Page page = fm_->requestFreePage(pageSize_, false);
410  MultiPage multiPage(pageSize_);
411  multiPage.push(page, epoch);
412  multiPages_.emplace_back(multiPage);
413  return page;
414 }
415 
417  const int32_t pageId,
418  const int32_t epoch,
419  const bool writeMetadata) {
420  int32_t intHeaderSize = chunkKey_.size() + 3; // does not include chunkSize
421  vector<int32_t> header(intHeaderSize);
422  // in addition to chunkkey we need size of header, pageId, version
423  header[0] =
424  (intHeaderSize - 1) * sizeof(int32_t); // don't need to include size of headerSize
425  // value - sizeof(size_t) is for chunkSize
426  std::copy(chunkKey_.begin(), chunkKey_.end(), header.begin() + 1);
427  header[intHeaderSize - 2] = pageId;
428  header[intHeaderSize - 1] = epoch;
429  FileInfo* fileInfo = fm_->getFileInfoForFileId(page.fileId);
430  size_t pageSize = writeMetadata ? metadataPageSize_ : pageSize_;
431  fileInfo->write(
432  page.pageNum * pageSize, (intHeaderSize) * sizeof(int32_t), (int8_t*)&header[0]);
433 }
434 
435 void FileBuffer::readMetadata(const Page& page) {
436  FILE* f = fm_->getFileForFileId(page.fileId);
437  fseek(f, page.pageNum * metadataPageSize_ + reservedHeaderSize_, SEEK_SET);
438  fread((int8_t*)&pageSize_, sizeof(size_t), 1, f);
439  fread((int8_t*)&size_, sizeof(size_t), 1, f);
440  vector<int32_t> typeData(
441  NUM_METADATA); // assumes we will encode hasEncoder, bufferType,
442  // encodingType, encodingBits all as int
443  fread((int8_t*)&(typeData[0]), sizeof(int32_t), typeData.size(), f);
444  int32_t version = typeData[0];
445  CHECK(version == METADATA_VERSION); // add backward compatibility code here
446  bool has_encoder = static_cast<bool>(typeData[1]);
447  if (has_encoder) {
448  sql_type_.set_type(static_cast<SQLTypes>(typeData[2]));
449  sql_type_.set_subtype(static_cast<SQLTypes>(typeData[3]));
450  sql_type_.set_dimension(typeData[4]);
451  sql_type_.set_scale(typeData[5]);
452  sql_type_.set_notnull(static_cast<bool>(typeData[6]));
453  sql_type_.set_compression(static_cast<EncodingType>(typeData[7]));
454  sql_type_.set_comp_param(typeData[8]);
455  sql_type_.set_size(typeData[9]);
457  encoder_->readMetadata(f);
458  }
459 }
460 
461 void FileBuffer::writeMetadata(const int32_t epoch) {
462  // Right now stats page is size_ (in bytes), bufferType, encodingType,
463  // encodingDataType, numElements
464  Page page = fm_->requestFreePage(metadataPageSize_, true);
465  writeHeader(page, -1, epoch, true);
466  FILE* f = fm_->getFileForFileId(page.fileId);
467  fseek(f, page.pageNum * metadataPageSize_ + reservedHeaderSize_, SEEK_SET);
468  fwrite((int8_t*)&pageSize_, sizeof(size_t), 1, f);
469  fwrite((int8_t*)&size_, sizeof(size_t), 1, f);
470  vector<int32_t> typeData(
471  NUM_METADATA); // assumes we will encode hasEncoder, bufferType,
472  // encodingType, encodingBits all as int32_t
473  typeData[0] = METADATA_VERSION;
474  typeData[1] = static_cast<int32_t>(hasEncoder());
475  if (hasEncoder()) {
476  typeData[2] = static_cast<int32_t>(sql_type_.get_type());
477  typeData[3] = static_cast<int32_t>(sql_type_.get_subtype());
478  typeData[4] = sql_type_.get_dimension();
479  typeData[5] = sql_type_.get_scale();
480  typeData[6] = static_cast<int32_t>(sql_type_.get_notnull());
481  typeData[7] = static_cast<int32_t>(sql_type_.get_compression());
482  typeData[8] = sql_type_.get_comp_param();
483  typeData[9] = sql_type_.get_size();
484  }
485  fwrite((int8_t*)&(typeData[0]), sizeof(int32_t), typeData.size(), f);
486  if (hasEncoder()) { // redundant
487  encoder_->writeMetadata(f);
488  }
489  metadataPages_.push(page, epoch);
490 }
491 
492 void FileBuffer::append(int8_t* src,
493  const size_t numBytes,
494  const MemoryLevel srcBufferType,
495  const int32_t deviceId) {
496  setAppended();
497 
498  size_t startPage = size_ / pageDataSize_;
499  size_t startPageOffset = size_ % pageDataSize_;
500  size_t numPagesToWrite =
501  (numBytes + startPageOffset + pageDataSize_ - 1) / pageDataSize_;
502  size_t bytesLeft = numBytes;
503  int8_t* curPtr = src; // a pointer to the current location in dst being written to
504  size_t initialNumPages = multiPages_.size();
505  size_ = size_ + numBytes;
506  auto epoch = getFileMgrEpoch();
507  for (size_t pageNum = startPage; pageNum < startPage + numPagesToWrite; ++pageNum) {
508  Page page;
509  if (pageNum >= initialNumPages) {
510  page = addNewMultiPage(epoch);
511  writeHeader(page, pageNum, epoch);
512  } else {
513  // we already have a new page at current
514  // epoch for this page - just grab this page
515  page = multiPages_[pageNum].current().page;
516  }
517  CHECK(page.fileId >= 0); // make sure page was initialized
518  FileInfo* fileInfo = fm_->getFileInfoForFileId(page.fileId);
519  size_t bytesWritten;
520  if (pageNum == startPage) {
521  bytesWritten = fileInfo->write(
522  page.pageNum * pageSize_ + startPageOffset + reservedHeaderSize_,
523  min(pageDataSize_ - startPageOffset, bytesLeft),
524  curPtr);
525  } else {
526  bytesWritten = fileInfo->write(page.pageNum * pageSize_ + reservedHeaderSize_,
527  min(pageDataSize_, bytesLeft),
528  curPtr);
529  }
530  curPtr += bytesWritten;
531  bytesLeft -= bytesWritten;
532  }
533  CHECK(bytesLeft == 0);
534 }
535 
536 void FileBuffer::write(int8_t* src,
537  const size_t numBytes,
538  const size_t offset,
539  const MemoryLevel srcBufferType,
540  const int32_t deviceId) {
541  CHECK(srcBufferType == CPU_LEVEL) << "Unsupported Buffer type";
542 
543  bool tempIsAppended = false;
544  setDirty();
545  if (offset < size_) {
546  setUpdated();
547  }
548  if (offset + numBytes > size_) {
549  tempIsAppended = true; // because is_appended_ could have already been true - to
550  // avoid rewriting header
551  setAppended();
552  size_ = offset + numBytes;
553  }
554 
555  size_t startPage = offset / pageDataSize_;
556  size_t startPageOffset = offset % pageDataSize_;
557  size_t numPagesToWrite =
558  (numBytes + startPageOffset + pageDataSize_ - 1) / pageDataSize_;
559  size_t bytesLeft = numBytes;
560  int8_t* curPtr = src; // a pointer to the current location in dst being written to
561  size_t initialNumPages = multiPages_.size();
562  auto epoch = getFileMgrEpoch();
563 
564  if (startPage >
565  initialNumPages) { // means there is a gap we need to allocate pages for
566  for (size_t pageNum = initialNumPages; pageNum < startPage; ++pageNum) {
567  Page page = addNewMultiPage(epoch);
568  writeHeader(page, pageNum, epoch);
569  }
570  }
571  for (size_t pageNum = startPage; pageNum < startPage + numPagesToWrite; ++pageNum) {
572  Page page;
573  if (pageNum >= initialNumPages) {
574  page = addNewMultiPage(epoch);
575  writeHeader(page, pageNum, epoch);
576  } else if (multiPages_[pageNum].current().epoch <
577  epoch) { // need to create new page b/c this current one lags epoch and we
578  // can't overwrite it also need to copy if we are on first or
579  // last page
580  Page lastPage = multiPages_[pageNum].current().page;
581  page = fm_->requestFreePage(pageSize_, false);
582  multiPages_[pageNum].push(page, epoch);
583  if (pageNum == startPage && startPageOffset > 0) {
584  // copyPage takes care of header offset so don't worry
585  // about it
586  copyPage(lastPage, page, startPageOffset, 0);
587  }
588  if (pageNum == (startPage + numPagesToWrite - 1) &&
589  bytesLeft > 0) { // bytesLeft should always > 0
590  copyPage(lastPage,
591  page,
592  pageDataSize_ - bytesLeft,
593  bytesLeft); // these would be empty if we're appending but we won't
594  // worry about it right now
595  }
596  writeHeader(page, pageNum, epoch);
597  } else {
598  // we already have a new page at current
599  // epoch for this page - just grab this page
600  page = multiPages_[pageNum].current().page;
601  }
602  CHECK(page.fileId >= 0); // make sure page was initialized
603  FileInfo* fileInfo = fm_->getFileInfoForFileId(page.fileId);
604  size_t bytesWritten;
605  if (pageNum == startPage) {
606  bytesWritten = fileInfo->write(
607  page.pageNum * pageSize_ + startPageOffset + reservedHeaderSize_,
608  min(pageDataSize_ - startPageOffset, bytesLeft),
609  curPtr);
610  } else {
611  bytesWritten = fileInfo->write(page.pageNum * pageSize_ + reservedHeaderSize_,
612  min(pageDataSize_, bytesLeft),
613  curPtr);
614  }
615  curPtr += bytesWritten;
616  bytesLeft -= bytesWritten;
617  if (tempIsAppended && pageNum == startPage + numPagesToWrite - 1) { // if last page
618  //@todo below can lead to undefined - we're overwriting num
619  // bytes valid at checkpoint
620  writeHeader(page, 0, multiPages_[0].current().epoch, true);
621  }
622  }
623  CHECK(bytesLeft == 0);
624 }
625 
627  auto [db_id, tb_id] = get_table_prefix(chunkKey_);
628  return fm_->epoch(db_id, tb_id);
629 }
630 
631 std::string FileBuffer::dump() const {
632  std::stringstream ss;
633  ss << "chunk_key = " << show_chunk(chunkKey_) << "\n";
634  ss << "has_encoder = " << (hasEncoder() ? "true\n" : "false\n");
635  ss << "size_ = " << size_ << "\n";
636  return ss.str();
637 }
638 
640  CHECK(metadataPages_.current().page.fileId != -1); // was initialized
643 }
644 
646  // Detect the case where a page is missing by comparing the amount of pages read
647  // with the metadata size.
648  return ((size() + pageDataSize_ - 1) / pageDataSize_ != multiPages_.size());
649 }
650 
652  size_t total_size = 0;
653  for (const auto& multi_page : multiPages_) {
654  total_size += multi_page.pageVersions.size();
655  }
656  return total_size;
657 }
658 
659 } // namespace File_Namespace
virtual std::vector< MultiPage > getMultiPage() const
Returns vector of MultiPages in the FileBuffer.
Definition: FileBuffer.h:147
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:392
void set_compression(EncodingType c)
Definition: sqltypes.h:479
void set_size(int s)
Definition: sqltypes.h:476
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:281
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:64
void freePagesBeforeEpochForMultiPage(MultiPage &multiPage, const int32_t targetEpoch, const int32_t currentEpoch)
Definition: FileBuffer.cpp:196
HOST DEVICE int get_size() const
Definition: sqltypes.h:403
virtual Page requestFreePage(size_t pagesize, const bool isMetadata)
Definition: FileMgr.cpp:877
void freePagesBeforeEpoch(const int32_t targetEpoch)
Definition: FileBuffer.cpp:206
static size_t readForThread(FileBuffer *fileBuffer, const readThreadDS threadDS)
Definition: FileBuffer.cpp:245
A logical page (Page) belongs to a file on disk.
Definition: Page.h:46
void pop()
Purges the oldest Page.
Definition: Page.h:110
#define LOG(tag)
Definition: Logger.h:285
size_t numChunkPages() const
Definition: FileBuffer.cpp:651
Page addNewMultiPage(const int32_t epoch)
Definition: FileBuffer.cpp:408
HOST DEVICE int get_scale() const
Definition: sqltypes.h:396
void writeMetadata(const int32_t epoch)
Definition: FileBuffer.cpp:461
void write(int8_t *src, const size_t numBytes, const size_t offset=0, const MemoryLevel srcMemoryLevel=CPU_LEVEL, const int32_t deviceId=-1) override
Writes the contents of source (src) into new versions of the affected logical pages.
Definition: FileBuffer.cpp:536
This file includes the class specification for the FILE manager (FileMgr), and related data structure...
HOST DEVICE void set_subtype(SQLTypes st)
Definition: sqltypes.h:469
const size_t metadataPageSize_
Definition: FileBuffer.h:196
std::vector< MultiPage > multiPages_
Definition: FileBuffer.h:198
std::vector< MultiPage > multiPages
Definition: FileBuffer.cpp:242
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:391
Represents/provides access to contiguous data stored in the file system.
Definition: FileBuffer.h:57
#define CHECK_GT(x, y)
Definition: Logger.h:305
void initEncoder(const SQLTypeInfo &tmp_sql_type)
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:187
std::deque< EpochedPage > pageVersions
Definition: Page.h:81
future< Result > async(Fn &&fn, Args &&...args)
void * checked_malloc(const size_t size)
Definition: checked_alloc.h:45
DEVICE auto copy(ARGS &&...args)
Definition: gpu_enabled.h:51
int32_t fileId
Definition: Page.h:47
size_t pageSize() const override
Returns the size in bytes of each page in the FileBuffer.
Definition: FileBuffer.h:137
string version
Definition: setup.in.py:73
void set_scale(int s)
Definition: sqltypes.h:473
std::string dump() const
Definition: FileBuffer.cpp:631
FILE * getFileForFileId(const int32_t fileId)
Returns FILE pointer associated with requested fileId.
Definition: FileMgr.cpp:1003
An AbstractBuffer is a unit of data management for a data manager.
size_t pageNum
unique identifier of the owning file
Definition: Page.h:48
void append(int8_t *src, const size_t numBytes, const MemoryLevel srcMemoryLevel=CPU_LEVEL, const int32_t deviceId=-1) override
Definition: FileBuffer.cpp:492
void set_comp_param(int p)
Definition: sqltypes.h:480
void writeHeader(Page &page, const int32_t pageId, const int32_t epoch, const bool writeMetadata=false)
Write header writes header at top of page in format.
Definition: FileBuffer.cpp:416
virtual size_t reservedHeaderSize() const
Definition: FileBuffer.h:144
#define CHECK_LE(x, y)
Definition: Logger.h:304
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:399
#define NUM_METADATA
Definition: FileBuffer.h:35
void set_dimension(int d)
Definition: sqltypes.h:470
void push(const Page &page, const int epoch)
Pushes a new page with epoch value.
Definition: Page.h:102
~FileBuffer() override
Destructor.
Definition: FileBuffer.cpp:130
size_t read(const size_t offset, const size_t size, int8_t *buf)
Definition: FileInfo.cpp:70
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:393
virtual bool failOnReadError() const
True if a read error should cause a fatal error.
Definition: FileMgr.h:366
torch::Tensor f(torch::Tensor x, torch::Tensor W_target, torch::Tensor b_target)
std::unique_ptr< Encoder > encoder_
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:402
FileBuffer(FileMgr *fm, const size_t pageSize, const ChunkKey &chunkKey, const size_t initialSize=0)
Constructs a FileBuffer object.
Definition: FileBuffer.cpp:37
std::pair< int, int > get_table_prefix(const ChunkKey &key)
Definition: types.h:62
void set_notnull(bool n)
Definition: sqltypes.h:475
#define CHECK(condition)
Definition: Logger.h:291
void reserve(const size_t numBytes) override
Definition: FileBuffer.cpp:135
list header
Definition: report.py:113
FileInfo * getFileInfoForFileId(const int32_t fileId) const
Definition: FileMgr.h:229
std::vector< EpochedPage > freePagesBeforeEpoch(const int32_t target_epoch, const int32_t current_epoch)
Definition: Page.h:117
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:398
void read(int8_t *const dst, const size_t numBytes=0, const size_t offset=0, const MemoryLevel dstMemoryLevel=CPU_LEVEL, const int32_t deviceId=-1) override
Definition: FileBuffer.cpp:286
void readMetadata(const Page &page)
Definition: FileBuffer.cpp:435
void copyPage(Page &srcPage, Page &destPage, const size_t numBytes, const size_t offset=0)
Definition: FileBuffer.cpp:390
EpochedPage current() const
Returns a reference to the most recent version of the page.
Definition: Page.h:94
virtual size_t pageDataSize() const
Returns the size in bytes of the data portion of each page in the FileBuffer.
Definition: FileBuffer.h:140
#define METADATA_VERSION
Definition: FileBuffer.h:36
The MultiPage stores versions of the same logical page in a deque.
Definition: Page.h:79
A selection of helper methods for File I/O.
size_t getNumReaderThreads()
Returns number of threads defined by parameter num-reader-threads which should be used during initial...
Definition: FileMgr.h:316
void freePage(const Page &page)
Definition: FileBuffer.cpp:155
bool isMissingPages() const
Definition: FileBuffer.cpp:645
static constexpr size_t headerBufferOffset_
Definition: FileBuffer.h:165
HOST DEVICE void set_type(SQLTypes t)
Definition: sqltypes.h:468