CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FileHandler.cc
Go to the documentation of this file.
1 // $Id: FileHandler.cc,v 1.29 2011/07/05 13:25:43 mommsen Exp $
3 
7 
11 
12 #include "boost/shared_array.hpp"
13 
14 #include <errno.h>
15 #include <iostream>
16 #include <sstream>
17 #include <fstream>
18 #include <iomanip>
19 #include <cstdio>
20 #include <sys/stat.h>
21 #include <string.h>
22 
23 namespace stor {
24 
26  (
28  const DbFileHandlerPtr dbFileHandler,
29  const DiskWritingParams& dwParams,
30  const uint64_t& maxFileSize
31  ):
32  fileRecord_(fileRecord),
33  dbFileHandler_(dbFileHandler),
34  firstEntry_(utils::getCurrentTime()),
35  lastEntry_(firstEntry_),
36  diskWritingParams_(dwParams),
37  maxFileSize_(maxFileSize),
38  cmsver_(edm::getReleaseVersion())
39  {
40  // stripp quotes if present
41  if(cmsver_[0]=='"') cmsver_=cmsver_.substr(1,cmsver_.size()-2);
42 
43  checkDirectories();
44 
45  insertFileInDatabase();
46  }
47 
49  {
50  if ( ! fileRecord_->isOpen )
51  {
52  std::ostringstream msg;
53  msg << "Tried to write an event to "
54  << fileRecord_->completeFileName()
55  << "which has already been closed.";
56  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
57  }
58 
59  do_writeEvent(event);
60 
61  fileRecord_->fileSize += event.totalDataSize();
62  ++fileRecord_->eventCount;
64  }
65 
66 
68  // File bookkeeping //
70 
72  {
73  std::ostringstream oss;
74  oss << "./closeFile.pl "
75  << " --FILENAME " << fileRecord_->fileName()
76  << " --FILECOUNTER " << fileRecord_->fileCounter
77  << " --NEVENTS " << events()
78  << " --FILESIZE " << fileSize()
79  << " --STARTTIME " << utils::secondsSinceEpoch(firstEntry_)
80  << " --STOPTIME " << utils::secondsSinceEpoch(lastEntry_)
81  << " --STATUS " << "closed"
82  << " --RUNNUMBER " << fileRecord_->runNumber
83  << " --LUMISECTION " << fileRecord_->lumiSection
84  << " --PATHNAME " << fileRecord_->filePath()
85  << " --HOSTNAME " << diskWritingParams_.hostName_
86  << " --SETUPLABEL " << diskWritingParams_.setupLabel_
87  << " --STREAM " << fileRecord_->streamLabel
88  << " --INSTANCE " << diskWritingParams_.smInstanceString_
89  << " --SAFETY " << diskWritingParams_.initialSafetyLevel_
90  << " --APPVERSION " << cmsver_
91  << " --APPNAME CMSSW"
92  << " --TYPE streamer"
93  << " --DEBUGCLOSE " << fileRecord_->whyClosed
94  << " --CHECKSUM " << std::hex << fileRecord_->adler32
95  << " --CHECKSUMIND 0"
96  << "\n";
97 
98  dbFileHandler_->writeOld( lastEntry_, oss.str() );
99  }
100 
101 
103  {
104  std::ostringstream oss;
105  oss << "./insertFile.pl "
106  << " --FILENAME " << fileRecord_->fileName()
107  << " --FILECOUNTER " << fileRecord_->fileCounter
108  << " --NEVENTS " << events()
109  << " --FILESIZE " << fileSize()
110  << " --STARTTIME " << utils::secondsSinceEpoch(firstEntry_)
111  << " --STOPTIME 0"
112  << " --STATUS open"
113  << " --RUNNUMBER " << fileRecord_->runNumber
114  << " --LUMISECTION " << fileRecord_->lumiSection
115  << " --PATHNAME " << fileRecord_->filePath()
116  << " --HOSTNAME " << diskWritingParams_.hostName_
117  << " --SETUPLABEL " << diskWritingParams_.setupLabel_
118  << " --STREAM " << fileRecord_->streamLabel
119  << " --INSTANCE " << diskWritingParams_.smInstanceString_
120  << " --SAFETY " << diskWritingParams_.initialSafetyLevel_
121  << " --APPVERSION " << cmsver_
122  << " --APPNAME CMSSW"
123  << " --TYPE streamer"
124  << " --CHECKSUM 0"
125  << " --CHECKSUMIND 0"
126  << "\n";
127 
128  dbFileHandler_->writeOld( firstEntry_, oss.str() );
129  }
130 
131 
132  bool FileHandler::tooOld(const utils::TimePoint_t currentTime)
133  {
136  {
138  return true;
139  }
140  else
141  {
142  return false;
143  }
144  }
145 
146 
147  bool FileHandler::isFromLumiSection(const uint32_t lumiSection)
148  {
149  if (lumiSection == fileRecord_->lumiSection)
150  {
152  return true;
153  }
154  else
155  {
156  return false;
157  }
158  }
159 
160 
161  bool FileHandler::tooLarge(const uint64_t& dataSize)
162  {
163  if ( ((fileSize() + dataSize) > maxFileSize_) && (events() > 0) )
164  {
166  return true;
167  }
168  else
169  {
170  return false;
171  }
172  }
173 
174 
175  uint32_t FileHandler::events() const
176  {
177  return fileRecord_->eventCount;
178  }
179 
180 
182  {
183  return fileRecord_->fileSize;
184  }
185 
186 
188  // File system interaction //
190 
191 
193  (
195  )
196  {
197  const std::string openFileName(fileRecord_->completeFileName(FilesMonitorCollection::FileRecord::open));
198  const std::string closedFileName(fileRecord_->completeFileName(FilesMonitorCollection::FileRecord::closed));
199 
200  const uint64_t openFileSize = checkFileSizeMatch(openFileName, fileSize());
201 
202  makeFileReadOnly(openFileName);
203  try
204  {
205  renameFile(openFileName, closedFileName);
206  }
207  catch (stor::exception::DiskWriting& e)
208  {
209  XCEPT_RETHROW(stor::exception::DiskWriting,
210  "Could not move streamer file to closed area.", e);
211  }
212  fileRecord_->isOpen = false;
213  fileRecord_->whyClosed = reason;
214  checkFileSizeMatch(closedFileName, openFileSize);
215  checkAdler32(closedFileName);
216  }
217 
218 
220  {
221  #if linux
222  struct stat64 statBuff;
223  int statStatus = stat64(fileName.c_str(), &statBuff);
224  #else
225  struct stat statBuff;
226  int statStatus = stat(fileName.c_str(), &statBuff);
227  #endif
228  if ( statStatus != 0 )
229  {
231  std::ostringstream msg;
232  msg << "Error checking the status of file "
233  << fileName
234  << ": " << strerror(errno);
235  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
236  }
237 
238  if ( sizeMismatch(size, statBuff.st_size) )
239  {
241  std::ostringstream msg;
242  msg << "Found an unexpected file size when trying to move"
243  << " the file to the closed state. File " << fileName
244  << " has an actual size of " << statBuff.st_size
245  << " (" << statBuff.st_blocks << " blocks)"
246  << " instead of the expected size of " << size
247  << " (" << (size/512)+1 << " blocks).";
248  XCEPT_RAISE(stor::exception::FileTruncation, msg.str());
249  }
250 
251  return static_cast<uint64_t>(statBuff.st_size);
252  }
253 
254 
255  bool FileHandler::sizeMismatch(const uint64_t& initialSize, const uint64_t& finalSize) const
256  {
257  double pctDiff = calcPctDiff(initialSize, finalSize);
258  return (pctDiff > diskWritingParams_.fileSizeTolerance_);
259  }
260 
261 
262  void FileHandler::makeFileReadOnly(const std::string& fileName) const
263  {
264  int ronly = chmod(fileName.c_str(), S_IREAD|S_IRGRP|S_IROTH);
265  if (ronly != 0) {
266  std::ostringstream msg;
267  msg << "Unable to change permissions of " << fileName
268  << " to read only: " << strerror(errno);
269  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
270  }
271  }
272 
273 
274  void FileHandler::checkAdler32(const std::string& fileName) const
275  {
276  if (!diskWritingParams_.checkAdler32_) return;
277 
278  std::ifstream file(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
279  if (!file.is_open())
280  {
281  std::ostringstream msg;
282  msg << "File " << fileName << " could not be opened.\n";
283  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
284  }
285 
286  std::ifstream::pos_type size = file.tellg();
287  file.seekg (0, std::ios::beg);
288 
289  boost::shared_array<char> ptr(new char[1024*1024]);
290  uint32_t a = 1, b = 0;
291 
292  std::ifstream::pos_type rsize = 0;
293  while(rsize < size)
294  {
295  file.read(ptr.get(), 1024*1024);
296  rsize += 1024*1024;
297  if(file.gcount())
298  cms::Adler32(ptr.get(), file.gcount(), a, b);
299  else
300  break;
301  }
302  file.close();
303 
304  const uint32 adler = (b << 16) | a;
305  if (adler != fileRecord_->adler32)
306  {
307  std::ostringstream msg;
308  msg << "Disk resident file " << fileName <<
309  " has Adler32 checksum " << std::hex << adler <<
310  ", while the expected checkum is " << std::hex << fileRecord_->adler32;
311  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
312  }
313  }
314 
315 
317  (
318  const std::string& openFileName,
319  const std::string& closedFileName
320  ) const
321  {
322  int result = rename( openFileName.c_str(), closedFileName.c_str() );
323  if (result != 0) {
324  fileRecord_->whyClosed = FilesMonitorCollection::FileRecord::notClosed;
325  std::ostringstream msg;
326  msg << "Unable to move " << openFileName << " to "
327  << closedFileName << ": " << strerror(errno);
328  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
329  }
330  }
331 
332 
334  {
336  utils::checkDirectory(fileRecord_->baseFilePath);
337  utils::checkDirectory(fileRecord_->baseFilePath + "/open");
338  utils::checkDirectory(fileRecord_->baseFilePath + "/closed");
339  }
340 
341 
342  double FileHandler::calcPctDiff(const uint64_t& value1, const uint64_t& value2) const
343  {
344  if (value1 == value2) return 0;
345  uint64_t largerValue = std::max(value1,value2);
346  uint64_t smallerValue = std::min(value1,value2);
347  return ( largerValue > 0 ? (largerValue - smallerValue) / largerValue : 0 );
348  }
349 
350 } // namespace stor
351 
TimePoint_t getCurrentTime()
Definition: Utils.h:158
uint64_t fileSize() const
Definition: FileHandler.cc:181
void moveFileToClosed(const FilesMonitorCollection::FileRecord::ClosingReason &)
Definition: FileHandler.cc:193
virtual void closeFile(const FilesMonitorCollection::FileRecord::ClosingReason &)=0
utils::Duration_t lumiSectionTimeOut_
Definition: Configuration.h:41
double seconds()
const uint64_t maxFileSize_
Definition: FileHandler.h:197
FileHandler(FilesMonitorCollection::FileRecordPtr, const DbFileHandlerPtr, const DiskWritingParams &, const uint64_t &maxFileSize)
Definition: FileHandler.cc:26
#define min(a, b)
Definition: mlp_lapack.h:161
std::string smInstanceString_
Definition: Configuration.h:51
std::string cmsver_
Definition: FileHandler.h:201
void insertFileInDatabase() const
Definition: FileHandler.cc:102
boost::shared_ptr< FileRecord > FileRecordPtr
long secondsSinceEpoch(TimePoint_t const &)
Definition: Utils.h:152
utils::TimePoint_t firstEntry_
Definition: FileHandler.h:190
const T & max(const T &a, const T &b)
bool sizeMismatch(const uint64_t &initialSize, const uint64_t &finalSize) const
Definition: FileHandler.cc:255
boost::shared_ptr< DbFileHandler > DbFileHandlerPtr
Definition: DbFileHandler.h:65
tuple result
Definition: query.py:137
utils::TimePoint_t lastEntry_
Definition: FileHandler.h:191
void updateDatabase() const
Definition: FileHandler.cc:71
boost::posix_time::ptime TimePoint_t
Definition: Utils.h:35
void writeEvent(const I2OChain &)
Definition: FileHandler.cc:48
const DiskWritingParams & diskWritingParams_
Definition: FileHandler.h:193
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
void checkDirectory(const std::string &)
Definition: Utils.cc:80
void makeFileReadOnly(const std::string &fileName) const
Definition: FileHandler.cc:262
unsigned int uint32
Definition: MsgTools.h:13
bool tooOld(const utils::TimePoint_t currentTime=utils::getCurrentTime())
Definition: FileHandler.cc:132
void renameFile(const std::string &openFileName, const std::string &closedFileName) const
Definition: FileHandler.cc:317
std::string getReleaseVersion()
void Adler32(char const *data, size_t len, uint32_t &a, uint32_t &b)
unsigned long long uint64_t
Definition: Time.h:15
virtual void do_writeEvent(const I2OChain &event)=0
double b
Definition: hdecay.h:120
FilesMonitorCollection::FileRecordPtr fileRecord_
Definition: FileHandler.h:187
uint64_t checkFileSizeMatch(const std::string &fileName, const uint64_t &size) const
Definition: FileHandler.cc:219
double a
Definition: hdecay.h:121
void checkDirectories() const
Definition: FileHandler.cc:333
bool tooLarge(const uint64_t &dataSize)
Definition: FileHandler.cc:161
const DbFileHandlerPtr dbFileHandler_
Definition: FileHandler.h:188
tuple size
Write out results.
double calcPctDiff(const uint64_t &, const uint64_t &) const
Definition: FileHandler.cc:342
bool isFromLumiSection(const uint32_t lumiSection)
Definition: FileHandler.cc:147
void checkAdler32(const std::string &fileName) const
Definition: FileHandler.cc:274
uint32_t events() const
Definition: FileHandler.cc:175