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.26.4.1 2011/03/07 11:33:05 mommsen Exp $
3 
7 
10 
11 #include <errno.h>
12 #include <iostream>
13 #include <sstream>
14 #include <fstream>
15 #include <iomanip>
16 #include <cstdio>
17 #include <sys/stat.h>
18 #include <string.h>
19 
20 namespace stor {
21 
23  (
25  const DbFileHandlerPtr dbFileHandler,
26  const DiskWritingParams& dwParams,
27  const uint64_t& maxFileSize
28  ):
29  fileRecord_(fileRecord),
30  dbFileHandler_(dbFileHandler),
31  firstEntry_(utils::getCurrentTime()),
32  lastEntry_(firstEntry_),
33  diskWritingParams_(dwParams),
34  maxFileSize_(maxFileSize),
35  cmsver_(edm::getReleaseVersion()),
36  adler_(0)
37  {
38  // stripp quotes if present
39  if(cmsver_[0]=='"') cmsver_=cmsver_.substr(1,cmsver_.size()-2);
40 
41  checkDirectories();
42 
43  insertFileInDatabase();
44  }
45 
47  {
48  if ( ! fileRecord_->isOpen )
49  {
50  std::ostringstream msg;
51  msg << "Tried to write an event to "
52  << fileRecord_->completeFileName()
53  << "which has already been closed.";
54  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
55  }
56 
57  do_writeEvent(event);
58 
59  fileRecord_->fileSize += event.totalDataSize();
60  ++fileRecord_->eventCount;
62  }
63 
64 
66  // File bookkeeping //
68 
70  {
71  std::ostringstream currentStat;
72  std::string ind(":");
73  currentStat
74  << fileRecord_->filePath() << ind
75  << fileRecord_->fileName() << ind
76  << fileSize() << ind
77  << events() << ind
78  << utils::timeStamp(lastEntry_) << ind
79  << (lastEntry_ - firstEntry_).total_seconds() << ind
80  << fileRecord_->whyClosed << std::endl;
81  std::string currentStatString (currentStat.str());
82  std::ofstream of(diskWritingParams_.fileCatalog_.c_str(), std::ios_base::ate | std::ios_base::out | std::ios_base::app );
83  of << currentStatString;
84  of.close();
85  }
86 
87 
89  {
90  std::ostringstream oss;
91  oss << "./closeFile.pl "
92  << " --FILENAME " << fileRecord_->fileName()
93  << " --FILECOUNTER " << fileRecord_->fileCounter
94  << " --NEVENTS " << events()
95  << " --FILESIZE " << fileSize()
96  << " --STARTTIME " << utils::secondsSinceEpoch(firstEntry_)
97  << " --STOPTIME " << utils::secondsSinceEpoch(lastEntry_)
98  << " --STATUS " << "closed"
99  << " --RUNNUMBER " << fileRecord_->runNumber
100  << " --LUMISECTION " << fileRecord_->lumiSection
101  << " --PATHNAME " << fileRecord_->filePath()
102  << " --HOSTNAME " << diskWritingParams_.hostName_
103  << " --SETUPLABEL " << diskWritingParams_.setupLabel_
104  << " --STREAM " << fileRecord_->streamLabel
105  << " --INSTANCE " << diskWritingParams_.smInstanceString_
106  << " --SAFETY " << diskWritingParams_.initialSafetyLevel_
107  << " --APPVERSION " << cmsver_
108  << " --APPNAME CMSSW"
109  << " --TYPE streamer"
110  << " --DEBUGCLOSE " << fileRecord_->whyClosed
111  << " --CHECKSUM " << std::hex << adler_
112  << " --CHECKSUMIND " << std::hex << 0
113  << "\n";
114 
115  dbFileHandler_->writeOld( lastEntry_, oss.str() );
116  }
117 
118 
120  {
121  std::ostringstream oss;
122  oss << "./insertFile.pl "
123  << " --FILENAME " << fileRecord_->fileName()
124  << " --FILECOUNTER " << fileRecord_->fileCounter
125  << " --NEVENTS " << events()
126  << " --FILESIZE " << fileSize()
127  << " --STARTTIME " << utils::secondsSinceEpoch(firstEntry_)
128  << " --STOPTIME 0"
129  << " --STATUS open"
130  << " --RUNNUMBER " << fileRecord_->runNumber
131  << " --LUMISECTION " << fileRecord_->lumiSection
132  << " --PATHNAME " << fileRecord_->filePath()
133  << " --HOSTNAME " << diskWritingParams_.hostName_
134  << " --SETUPLABEL " << diskWritingParams_.setupLabel_
135  << " --STREAM " << fileRecord_->streamLabel
136  << " --INSTANCE " << diskWritingParams_.smInstanceString_
137  << " --SAFETY " << diskWritingParams_.initialSafetyLevel_
138  << " --APPVERSION " << cmsver_
139  << " --APPNAME CMSSW"
140  << " --TYPE streamer"
141  << " --CHECKSUM 0"
142  << " --CHECKSUMIND 0"
143  << "\n";
144 
145  dbFileHandler_->writeOld( firstEntry_, oss.str() );
146  }
147 
148 
149  bool FileHandler::tooOld(const utils::TimePoint_t currentTime)
150  {
153  {
155  return true;
156  }
157  else
158  {
159  return false;
160  }
161  }
162 
163 
164  bool FileHandler::isFromLumiSection(const uint32_t lumiSection)
165  {
166  if (lumiSection == fileRecord_->lumiSection)
167  {
169  return true;
170  }
171  else
172  {
173  return false;
174  }
175  }
176 
177 
178  bool FileHandler::tooLarge(const uint64_t& dataSize)
179  {
180  if ( ((fileSize() + dataSize) > maxFileSize_) && (events() > 0) )
181  {
183  return true;
184  }
185  else
186  {
187  return false;
188  }
189  }
190 
191 
192  uint32_t FileHandler::events() const
193  {
194  return fileRecord_->eventCount;
195  }
196 
197 
199  {
200  return fileRecord_->fileSize;
201  }
202 
203 
205  // File system interaction //
207 
208 
210  (
212  )
213  {
214  const std::string openFileName(fileRecord_->completeFileName(FilesMonitorCollection::FileRecord::open));
215  const std::string closedFileName(fileRecord_->completeFileName(FilesMonitorCollection::FileRecord::closed));
216 
217  const uint64_t openFileSize = checkFileSizeMatch(openFileName, fileSize());
218 
219  makeFileReadOnly(openFileName);
220  try
221  {
222  renameFile(openFileName, closedFileName);
223  }
224  catch (stor::exception::DiskWriting& e)
225  {
226  XCEPT_RETHROW(stor::exception::DiskWriting,
227  "Could not move streamer file to closed area.", e);
228  }
229  fileRecord_->isOpen = false;
230  fileRecord_->whyClosed = reason;
231  checkFileSizeMatch(closedFileName, openFileSize);
232  }
233 
234 
236  {
237  #if linux
238  struct stat64 statBuff;
239  int statStatus = stat64(fileName.c_str(), &statBuff);
240  #else
241  struct stat statBuff;
242  int statStatus = stat(fileName.c_str(), &statBuff);
243  #endif
244  if ( statStatus != 0 )
245  {
247  std::ostringstream msg;
248  msg << "Error checking the status of file "
249  << fileName
250  << ": " << strerror(errno);
251  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
252  }
253 
254  if ( sizeMismatch(size, statBuff.st_size) )
255  {
257  std::ostringstream msg;
258  msg << "Found an unexpected file size when trying to move"
259  << " the file to the closed state. File " << fileName
260  << " has an actual size of " << statBuff.st_size
261  << " (" << statBuff.st_blocks << " blocks)"
262  << " instead of the expected size of " << size
263  << " (" << (size/512)+1 << " blocks).";
264  XCEPT_RAISE(stor::exception::FileTruncation, msg.str());
265  }
266 
267  return static_cast<uint64_t>(statBuff.st_size);
268  }
269 
270 
271  bool FileHandler::sizeMismatch(const uint64_t& initialSize, const uint64_t& finalSize) const
272  {
273  double pctDiff = calcPctDiff(initialSize, finalSize);
274  return (pctDiff > diskWritingParams_.fileSizeTolerance_);
275  }
276 
277 
278  void FileHandler::makeFileReadOnly(const std::string& fileName) const
279  {
280  int ronly = chmod(fileName.c_str(), S_IREAD|S_IRGRP|S_IROTH);
281  if (ronly != 0) {
282  std::ostringstream msg;
283  msg << "Unable to change permissions of " << fileName
284  << " to read only: " << strerror(errno);
285  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
286  }
287  }
288 
289 
291  (
292  const std::string& openFileName,
293  const std::string& closedFileName
294  ) const
295  {
296  int result = rename( openFileName.c_str(), closedFileName.c_str() );
297  if (result != 0) {
298  fileRecord_->whyClosed = FilesMonitorCollection::FileRecord::notClosed;
299  std::ostringstream msg;
300  msg << "Unable to move " << openFileName << " to "
301  << closedFileName << ": " << strerror(errno);
302  XCEPT_RAISE(stor::exception::DiskWriting, msg.str());
303  }
304  }
305 
306 
308  {
310  utils::checkDirectory(fileRecord_->baseFilePath);
311  utils::checkDirectory(fileRecord_->baseFilePath + "/open");
312  utils::checkDirectory(fileRecord_->baseFilePath + "/closed");
313  }
314 
315 
316  double FileHandler::calcPctDiff(const uint64_t& value1, const uint64_t& value2) const
317  {
318  if (value1 == value2) return 0;
319  uint64_t largerValue = std::max(value1,value2);
320  uint64_t smallerValue = std::min(value1,value2);
321  return ( largerValue > 0 ? (largerValue - smallerValue) / largerValue : 0 );
322  }
323 
324 } // namespace stor
325 
TimePoint_t getCurrentTime()
Definition: Utils.h:158
uint64_t fileSize() const
Definition: FileHandler.cc:198
void moveFileToClosed(const FilesMonitorCollection::FileRecord::ClosingReason &)
Definition: FileHandler.cc:210
virtual void closeFile(const FilesMonitorCollection::FileRecord::ClosingReason &)=0
utils::Duration_t lumiSectionTimeOut_
Definition: Configuration.h:42
double seconds()
const uint64_t maxFileSize_
Definition: FileHandler.h:208
string reason
Definition: Association.py:132
FileHandler(FilesMonitorCollection::FileRecordPtr, const DbFileHandlerPtr, const DiskWritingParams &, const uint64_t &maxFileSize)
Definition: FileHandler.cc:23
#define min(a, b)
Definition: mlp_lapack.h:161
std::string smInstanceString_
Definition: Configuration.h:51
std::string cmsver_
Definition: FileHandler.h:212
void insertFileInDatabase() const
Definition: FileHandler.cc:119
boost::shared_ptr< FileRecord > FileRecordPtr
long secondsSinceEpoch(TimePoint_t const &)
Definition: Utils.h:152
utils::TimePoint_t firstEntry_
Definition: FileHandler.h:201
const T & max(const T &a, const T &b)
bool sizeMismatch(const uint64_t &initialSize, const uint64_t &finalSize) const
Definition: FileHandler.cc:271
boost::shared_ptr< DbFileHandler > DbFileHandlerPtr
Definition: DbFileHandler.h:65
tuple result
Definition: query.py:137
utils::TimePoint_t lastEntry_
Definition: FileHandler.h:202
void updateDatabase() const
Definition: FileHandler.cc:88
boost::posix_time::ptime TimePoint_t
Definition: Utils.h:35
void writeEvent(const I2OChain &)
Definition: FileHandler.cc:46
const DiskWritingParams & diskWritingParams_
Definition: FileHandler.h:204
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:278
bool tooOld(const utils::TimePoint_t currentTime=utils::getCurrentTime())
Definition: FileHandler.cc:149
void renameFile(const std::string &openFileName, const std::string &closedFileName) const
Definition: FileHandler.cc:291
tuple out
Definition: dbtoconf.py:99
std::string getReleaseVersion()
unsigned long long uint64_t
Definition: Time.h:15
virtual void do_writeEvent(const I2OChain &event)=0
FilesMonitorCollection::FileRecordPtr fileRecord_
Definition: FileHandler.h:198
uint64_t checkFileSizeMatch(const std::string &fileName, const uint64_t &size) const
Definition: FileHandler.cc:235
void checkDirectories() const
Definition: FileHandler.cc:307
bool tooLarge(const uint64_t &dataSize)
Definition: FileHandler.cc:178
const DbFileHandlerPtr dbFileHandler_
Definition: FileHandler.h:199
tuple size
Write out results.
double calcPctDiff(const uint64_t &, const uint64_t &) const
Definition: FileHandler.cc:316
void writeToSummaryCatalog() const
Definition: FileHandler.cc:69
bool isFromLumiSection(const uint32_t lumiSection)
Definition: FileHandler.cc:164
std::string timeStamp(TimePoint_t)
Definition: Utils.cc:23
uint32_t events() const
Definition: FileHandler.cc:192