Bug in storage manager making possible to delete permanent event fixed
authorjniedzie <jeremi.niedziela@cern.ch>
Fri, 10 Oct 2014 09:24:55 +0000 (11:24 +0200)
committerjniedzie <jeremi.niedziela@cern.ch>
Mon, 13 Oct 2014 06:18:04 +0000 (08:18 +0200)
MONITOR/alistoragemanager/AliStorageClientThread.cxx
MONITOR/alistoragemanager/AliStorageDatabase.cxx
MONITOR/alistoragemanager/AliStorageDatabase.h
MONITOR/alistoragemanager/AliStorageServerThread.cxx

index 55f2c32..2154103 100644 (file)
@@ -281,22 +281,18 @@ Long64_t AliStorageClientThread::GetSizeOfAllChunks()
 void AliStorageClientThread::CollectData()
 {
        AliStorageEventManager *eventManager = AliStorageEventManager::GetEventManagerInstance();
-       if(eventManager->CreateSocket(EVENTS_SERVER_SUB))
-       {
-               fConnectionStatus=STATUS_OK;
-       }
-       else
-       {
-               fConnectionStatus=STATUS_ERROR;
-       }
+       if(eventManager->CreateSocket(EVENTS_SERVER_SUB)){fConnectionStatus=STATUS_OK;}
+       else{fConnectionStatus=STATUS_ERROR;}
        
        int chunkNumber=0;
        int previousChunkNumber=-1;
        int eventsInChunk=0;
        int previousRunNumber=-1;
        AliESDEvent *event = NULL;
-//        TTree *tree = NULL;
-        
+       vector<struct eventStruct> eventsToUpdate;
+        struct eventStruct currentEvent;
+       
+
        while(!gClientQuit)
        {               
           event = eventManager->GetEvent(EVENTS_SERVER_SUB);
@@ -353,6 +349,17 @@ void AliStorageClientThread::CollectData()
                                        delete fCurrentFile;
                                        fCurrentFile=0;
                                }
+                               for(unsigned int i=0;i<eventsToUpdate.size();i++)
+                                 {
+                                   fDatabase->UpdateEventPath(eventsToUpdate[i],
+                                                              Form("%s/run%d/chunk%d.root", 
+                                                                   fStoragePath.c_str(),
+                                                                   event->GetRunNumber(),
+                                                                   chunkNumber-1));
+                                 }
+                               eventsToUpdate.clear();
+
+
                                fCurrentStorageSize=GetSizeOfAllChunks();
                                CheckCurrentStorageSize();
                                
@@ -360,14 +367,23 @@ void AliStorageClientThread::CollectData()
 
                                previousChunkNumber = chunkNumber;
                        }
-                       
-                       if(0 != fCurrentFile->WriteObject(event,Form("event%d",event->GetEventNumberInFile())))//if event was written to file
+
+                       //create new directory for this run
+                       TDirectory *currentRun;
+                       if((currentRun = fCurrentFile->mkdir(Form("run%d",event->GetRunNumber()))))
+                         {
+                           cout<<"CLIENT -- creating new directory for this run"<<endl;
+                           currentRun->cd();
+                         }
+                       else
+                         {
+                           cout<<"CLIENT -- opening existing directory for this run"<<endl;
+                           fCurrentFile->cd(Form("run%d",event->GetRunNumber()));
+                         }
+
+                       if(0 != event->Write(Form("event%d",event->GetEventNumberInFile()))) 
+                         //fCurrentFile->WriteObject(event,Form("event%d",event->GetEventNumberInFile())))//if event was written to file
                        {
-                               fDatabase->InsertEvent(event->GetRunNumber(),
-                                             event->GetEventNumberInFile(),
-                                             (char*)event->GetBeamType(),
-                                                      event->GetMultiplicity()->GetNumberOfTracklets(),Form("%s/run%d/chunk%d.root",fStoragePath.c_str(),event->GetRunNumber(),chunkNumber));
-                               
                                eventsInChunk++;
                                
                                if(eventsInChunk == fNumberOfEventsInFile)//if max events number in file was reached
@@ -385,6 +401,40 @@ void AliStorageClientThread::CollectData()
                        {
                                fSavingStatus=STATUS_ERROR;
                        }
+
+               // save to event file as well:
+
+               TFile *eventFile = new TFile(Form("%s/run%d/event%d.root", fStoragePath.c_str(),event->GetRunNumber(),eventsInChunk),"recreate");
+
+               if((currentRun = eventFile->mkdir(Form("run%d",event->GetRunNumber()))))
+                         {
+                           cout<<"CLIENT -- creating new directory for this run"<<endl;
+                           currentRun->cd();
+                         }
+                       else
+                         {
+                           cout<<"CLIENT -- opening existing directory for this run"<<endl;
+                           eventFile->cd(Form("run%d",event->GetRunNumber()));
+                         }
+
+               if(0 == event->Write(Form("event%d",event->GetEventNumberInFile())) && 
+                  fSavingStatus!=STATUS_ERROR){fSavingStatus=STATUS_ERROR;}
+               else
+                 {
+                   eventFile->Close();
+                   delete eventFile;
+                   fDatabase->InsertEvent(event->GetRunNumber(),
+                                          event->GetEventNumberInFile(),
+                                          (char*)event->GetBeamType(),
+                                          event->GetMultiplicity()->GetNumberOfTracklets(),
+                                          Form("%s/run%d/event%d.root",fStoragePath.c_str(),
+                                               event->GetRunNumber(),
+                                               eventsInChunk));
+                   
+                   currentEvent.runNumber = event->GetRunNumber();
+                   currentEvent.eventNumber = event->GetEventNumberInFile();
+                   eventsToUpdate.push_back(currentEvent);
+                 }
                        delete event;event=0;
                         //delete tree;
                }
index cc39401..606b92d 100644 (file)
@@ -100,16 +100,52 @@ bool AliStorageDatabase::MarkEvent(struct eventStruct event)
 {  
   TSQLResult* res;
   res = fServer->Query(Form("UPDATE %s SET permanent = 1 WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
-  if(res) {
-    delete res;
-    return 1;
-  }
-  else {
-    delete res;
-    return 0;
-  }
+  if(!res) 
+    {
+      cout<<"DATABASE -- couldn't update permanent flag"<<endl;
+      delete res;
+      return 0;
+    }
+  else 
+    {  
+      cout<<"DATABASE -- permanent flag updated"<<endl;
+      
+      res = fServer->Query(Form("UPDATE %s SET file_path = '%s' WHERE run_number = %d AND event_number = %d;",fTable.c_str(),Form("%s/permEvents.root",fStoragePath.c_str()), event.runNumber,event.eventNumber));
+      if(!res) 
+       {
+         cout<<"DATABASE -- couldn't update file's path. Unsetting permanent flag"<<endl;
+         res = fServer->Query(Form("UPDATE %s SET permanent = 0 WHERE run_number = %d AND event_number = %d;",fTable.c_str(),event.runNumber,event.eventNumber));
+         delete res;
+         return 0;
+       }
+      else
+       {
+         cout<<"DATABASE -- event marked"<<endl;
+         delete res;
+         return 1;
+       }
+    }
+}
+
+bool AliStorageDatabase::UpdateEventPath(struct eventStruct event,const char *newPath)
+{  
+  TSQLResult* res;
+  res = fServer->Query(Form("UPDATE %s SET file_path = '%s' WHERE run_number = %d AND event_number = %d;",fTable.c_str(),newPath,event.runNumber,event.eventNumber));
+  if(!res) 
+    {
+      cout<<"DATABASE -- couldn't update file's path"<<endl;
+      delete res;
+      return 0;
+    }
+  else 
+    {
+      cout<<"DATABASE -- path updated for event:"<<event.eventNumber<<endl;
+      delete res;
+      return 1;
+    }
 }
 
+
 vector<serverListStruct> AliStorageDatabase::GetList(struct listRequestStruct list)
 {
        TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE run_number >= %d AND run_number <= %d AND event_number >= %d AND event_number <= %d AND multiplicity >= %d AND multiplicity <= %d AND (permanent = %d OR permanent = %d) AND (system = '%s' OR system = '%s') ORDER BY run_number,event_number;",
@@ -164,7 +200,11 @@ AliESDEvent* AliStorageDatabase::GetEvent(struct eventStruct event)
                return NULL;
        }
        AliESDEvent *data;
-       tmpFile->GetObject(Form("event%d;1",event.eventNumber),data);
+       tmpFile->cd(Form("run%d",event.runNumber));
+       data = (AliESDEvent*)gDirectory->Get(Form("event%d;1",event.eventNumber));
+
+
+       //      tmpFile->GetObject(Form("event%d;1",event.eventNumber),data);
 
        return data;
 }
@@ -261,7 +301,7 @@ AliESDEvent* AliStorageDatabase::GetPrevEvent(struct eventStruct event)
 
 struct eventStruct AliStorageDatabase::GetOldestEvent()
 {
-    TSQLResult *result = fServer->Query(Form("SELECT * FROM %s ORDER BY run_number,event_number;",fTable.c_str()));
+    TSQLResult *result = fServer->Query(Form("SELECT * FROM %s WHERE permanent = 0 ORDER BY run_number,event_number;",fTable.c_str()));
     
     TSQLRow *row;
     struct eventStruct oldestEvent = {0,0};
index 2ca2374..dfc92d8 100644 (file)
@@ -22,6 +22,7 @@ public:
                         char *filePath); //more parameters of the event can be added to this method
 
        bool MarkEvent(struct eventStruct event);
+       bool UpdateEventPath(struct eventStruct event,const char *newPath);
        void RemoveEvent(struct eventStruct event);
     void RemoveEventsWithPath(std::string path);
        std::string GetFilePath(struct eventStruct event);
index 34b2bcd..73545a8 100644 (file)
@@ -134,7 +134,10 @@ bool AliStorageServerThread::MarkEvent(struct eventStruct event)
       cout<<"SERVER -- couldn't open temp file"<<endl;
       return false;
     }
-  AliESDEvent *eventToMark = (AliESDEvent*)tmpFile->Get(Form("event%d",event.eventNumber));
+
+  tmpFile->cd(Form("run%d",event.runNumber));
+
+  AliESDEvent *eventToMark = (AliESDEvent*)gDirectory->Get(Form("event%d",event.eventNumber));
   if(!eventToMark)
     {
       cout<<"SERVER -- couldn't find such event"<<endl;
@@ -143,47 +146,47 @@ bool AliStorageServerThread::MarkEvent(struct eventStruct event)
     }
   cout<<"SERVER -- Marking event:"<<eventToMark->GetEventNumberInFile()<<endl;
                
-       TFile *permFile = new TFile(Form("%s/permEvents.root",fStoragePath.c_str()),"update");//open/create perm file
+  TFile *permFile = new TFile(Form("%s/permEvents.root",fStoragePath.c_str()),"update");//open/create perm file
        
-       if(!permFile)
-       {
-               cout<<"SERVER -- Couldn't open perm file"<<endl;
-               if(tmpFile){delete tmpFile;}
-               if(eventToMark){delete eventToMark;}
-               return false;
-       }
+  if(!permFile)
+    {
+      cout<<"SERVER -- Couldn't open perm file"<<endl;
+      if(tmpFile){delete tmpFile;}
+      if(eventToMark){delete eventToMark;}
+      return false;
+    }
 
-       //create new directory for this run
-       TDirectory *currentRun;
-       if((currentRun = permFile->mkdir(Form("run%d",event.runNumber))))
-       {
-               cout<<"SERVER -- creating new directory for this run"<<endl;
-               currentRun->cd();
-       }
-       else
-       {
-               cout<<"SERVER -- opening existing directory for this run"<<endl;
-               permFile->cd(Form("run%d",event.runNumber));
-       }
+  //create new directory for this run
+  TDirectory *currentRun;
+  if((currentRun = permFile->mkdir(Form("run%d",event.runNumber))))
+    {
+      cout<<"SERVER -- creating new directory for this run"<<endl;
+      currentRun->cd();
+    }
+  else
+    {
+      cout<<"SERVER -- opening existing directory for this run"<<endl;
+      permFile->cd(Form("run%d",event.runNumber));
+    }
 
-       //try to add record to the database
-       if(!fDatabase->MarkEvent(event))
-       {
-               cout<<"SERVER -- could not mark event in the database"<<endl;
-               if(tmpFile){delete tmpFile;}
-               if(eventToMark){delete eventToMark;}
-               if(permFile){delete permFile;}
-               return false;
-       }
+  //try to add record to the database
+  if(!fDatabase->MarkEvent(event))
+    {
+      cout<<"SERVER -- could not mark event in the database"<<endl;
+      if(tmpFile){delete tmpFile;}
+      if(eventToMark){delete eventToMark;}
+      if(permFile){delete permFile;}
+      return false;
+    }
 
-       eventToMark->Write(Form("event%d",event.eventNumber));
-       permFile->Close();
-       tmpFile->Close();
+  eventToMark->Write(Form("event%d",event.eventNumber));
+  permFile->Close();
+  tmpFile->Close();
 
-       if(tmpFile){delete tmpFile;}
-       if(eventToMark){delete eventToMark;}
-       if(permFile){delete permFile;}
-//     if(currentRun)delete currentRun;//this line crashes if there is no permanent file yet
-       return true;
+  if(tmpFile){delete tmpFile;}
+  if(eventToMark){delete eventToMark;}
+  if(permFile){delete permFile;}
+  //   if(currentRun)delete currentRun;//this line crashes if there is no permanent file yet
+  return true;
 }