TapeServerReporter.cpp 6.46 KB
Newer Older
1
2
/*
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2003-2021 CERN
4
5
6
7
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17

Victor Kotlyar's avatar
Victor Kotlyar committed
18
19
#include "common/log/LogContext.hpp"
#include "common/log/Logger.hpp"
20
#include "tapeserver/daemon/TapedProxy.hpp"
21
#include "castor/tape/tapeserver/daemon/TapeServerReporter.hpp"
22

Steven Murray's avatar
Steven Murray committed
23
24
25
#include <sys/types.h>
#include <unistd.h>

26
27
28
29
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
30

31
//-----------------------------------------------------------------------------
32
//constructor
33
//------------------------------------------------------------------------------  
34
TapeServerReporter::TapeServerReporter(
35
  cta::tape::daemon::TapedProxy& tapeserverProxy,
36
  const cta::tape::daemon::TpconfigLine& driveConfig,
37
  const std::string &hostname,
38
  const castor::tape::tapeserver::daemon::VolumeInfo &volume,
Victor Kotlyar's avatar
Victor Kotlyar committed
39
  cta::log::LogContext lc):
40
  m_threadRunnig(false),
41
  m_tapeserverProxy(tapeserverProxy),
42
43
  m_lc(lc),
  m_server(hostname),
44
45
  m_unitName(driveConfig.unitName),
  m_logicalLibrary(driveConfig.logicalLibrary),
46
47
48
  m_volume(volume),
  m_sessionPid(getpid()){
  //change the thread's name in the log
Victor Kotlyar's avatar
Victor Kotlyar committed
49
  m_lc.pushOrReplace(cta::log::Param("thread","TapeServerReporter"));
50
}
51
52
53
54
  
//------------------------------------------------------------------------------
//finish
//------------------------------------------------------------------------------
55
56
57
58
void TapeServerReporter::finish(){
  m_fifo.push(NULL);
}

59
//------------------------------------------------------------------------------
60
//startThreads
61
//------------------------------------------------------------------------------   
62
63
64
65
66
void TapeServerReporter::startThreads(){
  start();
  m_threadRunnig=true;
}

67
//------------------------------------------------------------------------------
68
//waitThreads
69
//------------------------------------------------------------------------------     
70
71
72
73
74
void TapeServerReporter::waitThreads(){
  try{
    wait();
    m_threadRunnig=false;
  }catch(const std::exception& e){
Victor Kotlyar's avatar
Victor Kotlyar committed
75
      cta::log::ScopedParamContainer sp(m_lc);
76
      sp.add("what",e.what());
Victor Kotlyar's avatar
Victor Kotlyar committed
77
      m_lc.log(cta::log::ERR,"error caught while waiting");
78
    }catch(...){
Victor Kotlyar's avatar
Victor Kotlyar committed
79
      m_lc.log(cta::log::ERR,"unknown error while waiting");
80
81
82
    }
}

83
//------------------------------------------------------------------------------
84
//reportState
85
//------------------------------------------------------------------------------  
86
87
88
89
90
void TapeServerReporter::reportState(cta::tape::session::SessionState state,
  cta::tape::session::SessionType type) {
  m_fifo.push(new ReportStateChange(state, type));
}

91
//------------------------------------------------------------------------------
92
93
94
95
96
97
//reportTapeUnmountedForRetrieve
//------------------------------------------------------------------------------ 
void TapeServerReporter::reportTapeUnmountedForRetrieve() {
  m_fifo.push(new ReportTapeUnmountedForRetrieve());
}

98
//------------------------------------------------------------------------------
99
100
101
102
103
104
//reportDiskCompleteForRetrieve
//------------------------------------------------------------------------------ 
void TapeServerReporter::reportDiskCompleteForRetrieve() {
  m_fifo.push(new ReportDiskCompleteForRetrieve());
}

105
106
//------------------------------------------------------------------------------
//run
107
//------------------------------------------------------------------------------  
108
109
110
111
112
113
114
115
116
void TapeServerReporter::run(){
  while(1){
    std::unique_ptr<Report> currentReport(m_fifo.pop());
    if(NULL==currentReport.get()) {
      break;
    }
    try{
      currentReport->execute(*this); 
    }catch(const std::exception& e){
Victor Kotlyar's avatar
Victor Kotlyar committed
117
      cta::log::ScopedParamContainer sp(m_lc);
118
      sp.add("what",e.what());
Victor Kotlyar's avatar
Victor Kotlyar committed
119
      m_lc.log(cta::log::ERR,"TapeServerReporter error caught");
120
121
    }
  }
122
123
}

124
//------------------------------------------------------------------------------
125
126
127
128
// ReportStateChange::ReportStateChange())
//------------------------------------------------------------------------------   
TapeServerReporter::ReportStateChange::ReportStateChange(cta::tape::session::SessionState state,
  cta::tape::session::SessionType type): m_state(state), m_type(type) { }
129

130
//------------------------------------------------------------------------------
131
132
133
134
135
136
// ReportStateChange::execute())
//------------------------------------------------------------------------------  
void TapeServerReporter::ReportStateChange::execute(TapeServerReporter& parent) {
  parent.m_tapeserverProxy.reportState(m_state, m_type, parent.m_volume.vid);
}

137
//------------------------------------------------------------------------------
138
139
140
141
142
// ReportTapeUnmountedForRetrieve::execute())
//------------------------------------------------------------------------------  
void TapeServerReporter::ReportTapeUnmountedForRetrieve::execute(TapeServerReporter& parent) {
  parent.m_tapeUnmountedForRecall=true;
  if (parent.m_diskCompleteForRecall) {
143
    parent.m_tapeserverProxy.reportState(cta::tape::session::SessionState::ShuttingDown, 
144
145
146
147
148
149
150
      cta::tape::session::SessionType::Retrieve, parent.m_volume.vid);
  } else {
    parent.m_tapeserverProxy.reportState(cta::tape::session::SessionState::DrainingToDisk, 
      cta::tape::session::SessionType::Retrieve, parent.m_volume.vid);
  }
}

151
//------------------------------------------------------------------------------
152
153
154
155
156
// ReportDiskCompleteForRetrieve::execute())
//------------------------------------------------------------------------------ 
void TapeServerReporter::ReportDiskCompleteForRetrieve::execute(TapeServerReporter& parent) {
  parent.m_diskCompleteForRecall=true;
  if (parent.m_tapeUnmountedForRecall) {
157
    parent.m_tapeserverProxy.reportState(cta::tape::session::SessionState::ShuttingDown, 
158
159
160
      cta::tape::session::SessionType::Retrieve, parent.m_volume.vid);
  }
}
161

162
}}}} // namespace castor::tape::tapeserver::daemon
163