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.32 2012/04/04 14:53:16 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 uint64_t& maxFileSize
30  ):
31  fileRecord_(fileRecord),
32  dbFileHandler_(dbFileHandler),
33  firstEntry_(utils::getCurrentTime()),
34  lastEntry_(firstEntry_),
35  diskWritingParams_(dbFileHandler->getDiskWritingParams()),
36  maxFileSize_(maxFileSize),
37  cmsver_(edm::getReleaseVersion())
38  {
39  // stripp quotes if present
40  if(cmsver_[0]=='"') cmsver_=cmsver_.substr(1,cmsver_.size()-2);
41 
42  checkDirectories();
43 
44  insertFileInDatabase();
45  }
46 
48  {
49  if ( ! fileRecord_->isOpen )
50  {
51  std::ostringstream msg;
52  msg << "Tried to write an event to "
53  << fileRecord_->completeFileName()
54  << "which has already been closed.";
55  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
56  }
57 
58  do_writeEvent(event);
59 
60  fileRecord_->fileSize += event.totalDataSize();
61  ++fileRecord_->eventCount;
63  }
64 
65 
67  // File bookkeeping //
69 
71  {
72  std::ostringstream oss;
73  oss << "./closeFile.pl "
74  << " --FILENAME " << fileRecord_->fileName()
75  << " --FILECOUNTER " << fileRecord_->fileCounter
76  << " --NEVENTS " << events()
77  << " --FILESIZE " << fileSize()
78  << " --STARTTIME " << utils::secondsSinceEpoch(firstEntry_)
79  << " --STOPTIME " << utils::secondsSinceEpoch(lastEntry_)
80  << " --STATUS " << "closed"
81  << " --RUNNUMBER " << fileRecord_->runNumber
82  << " --LUMISECTION " << fileRecord_->lumiSection
83  << " --PATHNAME " << fileRecord_->filePath()
84  << " --HOSTNAME " << diskWritingParams_.hostName_
85  << " --SETUPLABEL " << diskWritingParams_.setupLabel_
86  << " --STREAM " << fileRecord_->streamLabel
87  << " --INSTANCE " << diskWritingParams_.smInstanceString_
88  << " --SAFETY " << diskWritingParams_.initialSafetyLevel_
89  << " --APPVERSION " << cmsver_
90  << " --APPNAME CMSSW"
91  << " --TYPE streamer"
92  << " --DEBUGCLOSE " << fileRecord_->whyClosed
93  << " --CHECKSUM " << std::hex << fileRecord_->adler32
94  << " --CHECKSUMIND 0";
95 
96  dbFileHandler_->writeOld( lastEntry_, oss.str() );
97  }
98 
99 
101  {
102  std::ostringstream oss;
103  oss << "./insertFile.pl "
104  << " --FILENAME " << fileRecord_->fileName()
105  << " --FILECOUNTER " << fileRecord_->fileCounter
106  << " --NEVENTS " << events()
107  << " --FILESIZE " << fileSize()
108  << " --STARTTIME " << utils::secondsSinceEpoch(firstEntry_)
109  << " --STOPTIME 0"
110  << " --STATUS open"
111  << " --RUNNUMBER " << fileRecord_->runNumber
112  << " --LUMISECTION " << fileRecord_->lumiSection
113  << " --PATHNAME " << fileRecord_->filePath()
114  << " --HOSTNAME " << diskWritingParams_.hostName_
115  << " --SETUPLABEL " << diskWritingParams_.setupLabel_
116  << " --STREAM " << fileRecord_->streamLabel
117  << " --INSTANCE " << diskWritingParams_.smInstanceString_
118  << " --SAFETY " << diskWritingParams_.initialSafetyLevel_
119  << " --APPVERSION " << cmsver_
120  << " --APPNAME CMSSW"
121  << " --TYPE streamer"
122  << " --CHECKSUM 0"
123  << " --CHECKSUMIND 0";
124 
125  dbFileHandler_->writeOld( firstEntry_, oss.str() );
126  }
127 
128 
129  bool FileHandler::tooOld(const utils::TimePoint_t currentTime)
130  {
133  {
135  return true;
136  }
137  else
138  {
139  return false;
140  }
141  }
142 
143 
144  bool FileHandler::isFromLumiSection(const uint32_t lumiSection)
145  {
146  if (lumiSection == fileRecord_->lumiSection)
147  {
149  return true;
150  }
151  else
152  {
153  return false;
154  }
155  }
156 
157 
158  bool FileHandler::tooLarge(const uint64_t& dataSize)
159  {
160  if ( ((fileSize() + dataSize) > maxFileSize_) && (events() > 0) )
161  {
163  return true;
164  }
165  else
166  {
167  return false;
168  }
169  }
170 
171 
172  uint32_t FileHandler::events() const
173  {
174  return fileRecord_->eventCount;
175  }
176 
177 
179  {
180  return fileRecord_->fileSize;
181  }
182 
183 
185  // File system interaction //
187 
188 
190  (
192  )
193  {
194  const std::string openFileName(fileRecord_->completeFileName(FilesMonitorCollection::FileRecord::open));
195  const std::string closedFileName(fileRecord_->completeFileName(FilesMonitorCollection::FileRecord::closed));
196 
197  const uint64_t openFileSize = checkFileSizeMatch(openFileName, fileSize());
198 
199  makeFileReadOnly(openFileName);
200  try
201  {
202  renameFile(openFileName, closedFileName);
203  }
204  catch (stor::exception::DiskWriting& e)
205  {
206  XCEPT_RETHROW(stor::exception::DiskWriting,
207  "Could not move streamer file to closed area.", e);
208  }
209  fileRecord_->isOpen = false;
210  fileRecord_->whyClosed = reason;
211  checkFileSizeMatch(closedFileName, openFileSize);
212  checkAdler32(closedFileName);
213  }
214 
215 
217  {
218  #if linux
219  struct stat64 statBuff;
220  int statStatus = stat64(fileName.c_str(), &statBuff);
221  #else
222  struct stat statBuff;
223  int statStatus = stat(fileName.c_str(), &statBuff);
224  #endif
225  if ( statStatus != 0 )
226  {
228  std::ostringstream msg;
229  msg << "Error checking the status of file "
230  << fileName
231  << ": " << strerror(errno);
232  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
233  }
234 
235  if ( sizeMismatch(size, statBuff.st_size) )
236  {
238  std::ostringstream msg;
239  msg << "Found an unexpected file size when trying to move"
240  << " the file to the closed state. File " << fileName
241  << " has an actual size of " << statBuff.st_size
242  << " (" << statBuff.st_blocks << " blocks)"
243  << " instead of the expected size of " << size
244  << " (" << (size/512)+1 << " blocks).";
245  XCEPT_RAISE(stor::exception::FileTruncation, msg.str());
246  }
247 
248  return static_cast<uint64_t>(statBuff.st_size);
249  }
250 
251 
252  bool FileHandler::sizeMismatch(const uint64_t& initialSize, const uint64_t& finalSize) const
253  {
254  double pctDiff = calcPctDiff(initialSize, finalSize);
255  return (pctDiff > diskWritingParams_.fileSizeTolerance_);
256  }
257 
258 
260  {
261  int ronly = chmod(fileName.c_str(), S_IREAD|S_IRGRP|S_IROTH);
262  if (ronly != 0) {
263  std::ostringstream msg;
264  msg << "Unable to change permissions of " << fileName
265  << " to read only: " << strerror(errno);
266  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
267  }
268  }
269 
270 
272  {
273  if (!diskWritingParams_.checkAdler32_) return;
274 
275  std::ifstream file(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
276  if (!file.is_open())
277  {
278  std::ostringstream msg;
279  msg << "File " << fileName << " could not be opened.\n";
280  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
281  }
282 
283  std::ifstream::pos_type size = file.tellg();
284  file.seekg (0, std::ios::beg);
285 
286  boost::shared_array<char> ptr(new char[1024*1024]);
287  uint32_t a = 1, b = 0;
288 
289  std::ifstream::pos_type rsize = 0;
290  while(rsize < size)
291  {
292  file.read(ptr.get(), 1024*1024);
293  rsize += 1024*1024;
294  if(file.gcount())
295  cms::Adler32(ptr.get(), file.gcount(), a, b);
296  else
297  break;
298  }
299  file.close();
300 
301  const uint32 adler = (b << 16) | a;
302  if (adler != fileRecord_->adler32)
303  {
304  std::ostringstream msg;
305  msg << "Disk resident file " << fileName <<
306  " has Adler32 checksum " << std::hex << adler <<
307  ", while the expected checkum is " << std::hex << fileRecord_->adler32;
308  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
309  }
310  }
311 
312 
314  (
315  const std::string& openFileName,
316  const std::string& closedFileName
317  ) const
318  {
319  int result = rename( openFileName.c_str(), closedFileName.c_str() );
320  if (result != 0) {
321  fileRecord_->whyClosed = FilesMonitorCollection::FileRecord::notClosed;
322  std::ostringstream msg;
323  msg << "Unable to move " << openFileName << " to "
324  << closedFileName << ": " << strerror(errno);
325  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
326  }
327  }
328 
329 
331  {
333  utils::checkDirectory(fileRecord_->baseFilePath);
334  utils::checkDirectory(fileRecord_->baseFilePath + "/open");
335  utils::checkDirectory(fileRecord_->baseFilePath + "/closed");
336  }
337 
338 
339  double FileHandler::calcPctDiff(const uint64_t& value1, const uint64_t& value2) const
340  {
341  if (value1 == value2) return 0;
342  uint64_t largerValue = std::max(value1,value2);
343  uint64_t smallerValue = std::min(value1,value2);
344  return ( largerValue > 0 ? (largerValue - smallerValue) / largerValue : 0 );
345  }
346 
347 } // namespace stor
348 
TimePoint_t getCurrentTime()
Definition: Utils.h:158
uint64_t fileSize() const
Definition: FileHandler.cc:178
void moveFileToClosed(const FilesMonitorCollection::FileRecord::ClosingReason &)
Definition: FileHandler.cc:190
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:191
string reason
Definition: Association.py:177
#define min(a, b)
Definition: mlp_lapack.h:161
std::string smInstanceString_
Definition: Configuration.h:51
std::string cmsver_
Definition: FileHandler.h:195
void insertFileInDatabase() const
Definition: FileHandler.cc:100
boost::shared_ptr< FileRecord > FileRecordPtr
long secondsSinceEpoch(TimePoint_t const &)
Definition: Utils.h:152
utils::TimePoint_t firstEntry_
Definition: FileHandler.h:184
const T & max(const T &a, const T &b)
bool sizeMismatch(const uint64_t &initialSize, const uint64_t &finalSize) const
Definition: FileHandler.cc:252
boost::shared_ptr< DbFileHandler > DbFileHandlerPtr
Definition: DbFileHandler.h:71
tuple result
Definition: query.py:137
FileHandler(FilesMonitorCollection::FileRecordPtr, const DbFileHandlerPtr, const uint64_t &maxFileSize)
Definition: FileHandler.cc:26
utils::TimePoint_t lastEntry_
Definition: FileHandler.h:185
void updateDatabase() const
Definition: FileHandler.cc:70
boost::posix_time::ptime TimePoint_t
Definition: Utils.h:35
void writeEvent(const I2OChain &)
Definition: FileHandler.cc:47
const DiskWritingParams & diskWritingParams_
Definition: FileHandler.h:187
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:259
unsigned int uint32
Definition: MsgTools.h:13
bool tooOld(const utils::TimePoint_t currentTime=utils::getCurrentTime())
Definition: FileHandler.cc:129
void renameFile(const std::string &openFileName, const std::string &closedFileName) const
Definition: FileHandler.cc:314
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:181
uint64_t checkFileSizeMatch(const std::string &fileName, const uint64_t &size) const
Definition: FileHandler.cc:216
double a
Definition: hdecay.h:121
void checkDirectories() const
Definition: FileHandler.cc:330
bool tooLarge(const uint64_t &dataSize)
Definition: FileHandler.cc:158
const DbFileHandlerPtr dbFileHandler_
Definition: FileHandler.h:182
tuple size
Write out results.
double calcPctDiff(const uint64_t &, const uint64_t &) const
Definition: FileHandler.cc:339
bool isFromLumiSection(const uint32_t lumiSection)
Definition: FileHandler.cc:144
void checkAdler32(const std::string &fileName) const
Definition: FileHandler.cc:271
uint32_t events() const
Definition: FileHandler.cc:172