RecallTaskInjectorTest.cpp 9.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/****************************************************************************** 
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * 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 2
 * of the License, or (at your option) any later version.
 * 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.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * 
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/
23
24

#include "castor/legacymsg/RmcProxyDummy.hpp"
Victor Kotlyar's avatar
Victor Kotlyar committed
25
#include "common/log/StringLogger.hpp"
26
27
28
#include "castor/mediachanger/MediaChangerFacade.hpp"
#include "castor/mediachanger/MmcProxyDummy.hpp"
#include "castor/messages/AcsProxyDummy.hpp"
29
#include "castor/messages/TapeserverProxyDummy.hpp"
30
#include "common/processCap/ProcessCapDummy.hpp"
31
32
33
34
#include "castor/tape/tapeserver/daemon/DiskWriteThreadPool.hpp"
#include "castor/tape/tapeserver/daemon/RecallTaskInjector.hpp"
#include "castor/tape/tapeserver/daemon/TapeServerReporter.hpp"
#include "castor/tape/tapeserver/daemon/TapeReadSingleThread.hpp"
35
#include "castor/tape/tapeserver/daemon/TpconfigLine.hpp"
36
#include "castor/tape/tapeserver/daemon/TaskWatchDog.hpp"
37
#include "castor/tape/tapeserver/drive/FakeDrive.hpp"
38
#include "castor/utils/utils.hpp"
39
40
#include "scheduler/SchedulerDatabase.hpp"
#include "scheduler/testingMocks/MockRetrieveMount.hpp"
41
42
43

#include <gtest/gtest.h>

44
45
using namespace castor::tape::tapeserver::daemon;
using namespace castor::tape;
46

47

48
namespace unitTests
49
{
50
  const int blockSize=4096;
51
52
53
54
55
56
57
58
  class castor_tape_tapeserver_daemonTest: public ::testing::Test {
  protected:

    void SetUp() {
    }

    void TearDown() {
    }
59
    
60
  }; // class castor_tape_tapeserver_daemonTest
61
62
63
  
  struct MockRecallReportPacker : public RecallReportPacker {
    void reportCompletedJob(std::unique_ptr<cta::RetrieveJob> successfulRetrieveJob) {
64
      cta::threading::MutexLocker ml(m_mutex);
65
66
67
      completeJobs++;
    }
    void reportFailedJob(std::unique_ptr<cta::RetrieveJob> failedRetrieveJob) {
68
      cta::threading::MutexLocker ml(m_mutex);
69
70
71
72
      failedJobs++;
    }
    void disableBulk() {}
    void reportEndOfSession() {
73
      cta::threading::MutexLocker ml(m_mutex);
74
75
76
      endSessions++;
    }
    void reportEndOfSessionWithErrors(const std::string msg, int error_code) {
77
      cta::threading::MutexLocker ml(m_mutex);
78
79
      endSessionsWithError++;
    }
Victor Kotlyar's avatar
Victor Kotlyar committed
80
    MockRecallReportPacker(cta::RetrieveMount *rm,cta::log::LogContext lc):
81
82
     RecallReportPacker(rm,lc), completeJobs(0), failedJobs(0),
      endSessions(0), endSessionsWithError(0) {}
83
    cta::threading::Mutex m_mutex;
84
85
86
87
88
89
    int completeJobs;
    int failedJobs;
    int endSessions;
    int endSessionsWithError;
  };
  
90
  class FakeDiskWriteThreadPool: public DiskWriteThreadPool
91
  {
92
93
  public:
    using DiskWriteThreadPool::m_tasks;
94
    FakeDiskWriteThreadPool(RecallReportPacker &rrp, RecallWatchDog &rwd, 
Victor Kotlyar's avatar
Victor Kotlyar committed
95
      cta::log::LogContext & lc):
96
      DiskWriteThreadPool(1,rrp,
97
      rwd,lc, "RFIO","/dev/null", 0){}
98
99
100
101
    virtual ~FakeDiskWriteThreadPool() {};
  };

  class FakeSingleTapeReadThread : public TapeSingleThreadInterface<TapeReadTask>
102
  {
103
104
105
  public:
    using TapeSingleThreadInterface<TapeReadTask>::m_tasks;

106
    FakeSingleTapeReadThread(tapeserver::drive::DriveInterface& drive,
107
      castor::mediachanger::MediaChangerFacade & mc,
108
109
      tapeserver::daemon::TapeServerReporter & tsr,
      const tapeserver::daemon::VolumeInfo& volInfo, 
110
      cta::server::ProcessCap& cap,
Victor Kotlyar's avatar
Victor Kotlyar committed
111
      cta::log::LogContext & lc):
112
113
114
115
116
117
118
119
    TapeSingleThreadInterface<TapeReadTask>(drive, mc, tsr, volInfo,cap, lc){}

    ~FakeSingleTapeReadThread(){
      const unsigned int size= m_tasks.size();
      for(unsigned int i=0;i<size;++i){
        delete m_tasks.pop();
      }
    }
120
121
    
    virtual void run () 
122
    {
123
      m_tasks.push(NULL);
124
    }
125
    
126
127
128
129
130
    virtual void push(TapeReadTask* t){
      m_tasks.push(t);
    }

    virtual void countTapeLogError(const std::string & error) {};
131
132
    protected:
      virtual void logSCSIMetrics() {};
133
  };
134
135
136
137
138
  
  class TestingDatabaseRetrieveMount: public cta::SchedulerDatabase::RetrieveMount {
    virtual const MountInfo & getMountInfo() { throw std::runtime_error("Not implemented"); }
    virtual std::unique_ptr<cta::SchedulerDatabase::RetrieveJob> getNextJob() { throw std::runtime_error("Not implemented");}
    virtual void complete(time_t completionTime) { throw std::runtime_error("Not implemented"); }
139
    virtual void setDriveStatus(cta::common::DriveStatus status, time_t completionTime) { throw std::runtime_error("Not implemented"); }
140
141
  };
  
142
  TEST_F(castor_tape_tapeserver_daemonTest, RecallTaskInjectorNominal) {
143
144
    const int nbJobs=15;
    const int maxNbJobsInjectedAtOnce = 6;
Victor Kotlyar's avatar
Victor Kotlyar committed
145
146
    cta::log::StringLogger log("castor_tape_tapeserver_daemon_RecallTaskInjectorTest",cta::log::DEBUG);
    cta::log::LogContext lc(log);
147
    RecallMemoryManager mm(50U, 50U, lc);
148
149
    castor::tape::tapeserver::drive::FakeDrive drive;
    
150
151
152
    cta::MockRetrieveMount trm;
    trm.createRetrieveJobs(nbJobs);
    //EXPECT_CALL(trm, internalGetNextJob()).Times(nbJobs+1);
153
    
154
155
156
157
158
    castor::messages::TapeserverProxyDummy tspd;
    RecallWatchDog rwd(1,1,tspd,"",lc);
    std::unique_ptr<cta::SchedulerDatabase::RetrieveMount> dbrm(new TestingDatabaseRetrieveMount());
    MockRecallReportPacker mrrp(&trm,lc);
    FakeDiskWriteThreadPool diskWrite(mrrp,rwd,lc);
159
160
161
162
163
164
165
166
    castor::messages::AcsProxyDummy acs;
    castor::mediachanger::MmcProxyDummy mmc;
    castor::legacymsg::RmcProxyDummy rmc;
    castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
    castor::messages::TapeserverProxyDummy initialProcess;
    castor::tape::tapeserver::daemon::VolumeInfo volume;
    volume.vid="V12345";
    volume.mountType=cta::MountType::RETRIEVE;
167
    castor::tape::tapeserver::daemon::TapeServerReporter gsr(initialProcess, DriveConfig(), "0.0.0.0", volume, lc);
168
    cta::server::ProcessCapDummy cap;
169
    FakeSingleTapeReadThread tapeRead(drive, mc, gsr, volume, cap, lc);
170
    tapeserver::daemon::RecallTaskInjector rti(mm, tapeRead, diskWrite, trm, maxNbJobsInjectedAtOnce, blockSize, lc);
171

172
    ASSERT_EQ(true, rti.synchronousInjection());
173
174
    ASSERT_EQ(maxNbJobsInjectedAtOnce+1, diskWrite.m_tasks.size());
    ASSERT_EQ(maxNbJobsInjectedAtOnce+1, tapeRead.m_tasks.size());
175
176
177
178
179

    rti.startThreads();
    rti.requestInjection(false);
    rti.requestInjection(true);
    rti.finish();
180
    ASSERT_NO_THROW(rti.waitThreads());
181
    ASSERT_EQ(nbJobs+1, trm.getJobs);
182
183

    //pushed nbFile*2 files + 1 end of work
184
185
    ASSERT_EQ(nbJobs+1, diskWrite.m_tasks.size());
    ASSERT_EQ(nbJobs+1, tapeRead.m_tasks.size());
186

187
    for(int i=0; i<nbJobs; ++i)
188
189
190
191
192
    {
      delete diskWrite.m_tasks.pop();
      delete tapeRead.m_tasks.pop();
    }

193
    for(int i=0; i<1; ++i)
194
195
196
197
198
    {
      DiskWriteTask* diskWriteTask=diskWrite.m_tasks.pop();
      TapeReadTask* tapeReadTask=tapeRead.m_tasks.pop();

      //static_cast is needed otherwise compilation fails on SL5 with a raw NULL
199
200
      ASSERT_EQ(static_cast<DiskWriteTask*>(NULL), diskWriteTask);
      ASSERT_EQ(static_cast<TapeReadTask*>(NULL), tapeReadTask);
201
202
203
204
      delete diskWriteTask;
      delete tapeReadTask;
    }
  }
205
206
  
  TEST_F(castor_tape_tapeserver_daemonTest, RecallTaskInjectorNoFiles) {
Victor Kotlyar's avatar
Victor Kotlyar committed
207
208
    cta::log::StringLogger log("castor_tape_tapeserver_daemon_RecallTaskInjectorTest",cta::log::DEBUG);
    cta::log::LogContext lc(log);
209
    RecallMemoryManager mm(50U, 50U, lc);
210
    castor::tape::tapeserver::drive::FakeDrive drive;
211
    
212
213
214
    cta::MockRetrieveMount trm;
    trm.createRetrieveJobs(0);
    //EXPECT_CALL(trm, internalGetNextJob()).Times(1); //no work: single call to getnextjob
215
    
216
217
218
219
220
    castor::messages::TapeserverProxyDummy tspd;
    RecallWatchDog rwd(1,1,tspd,"",lc);
    std::unique_ptr<cta::SchedulerDatabase::RetrieveMount> dbrm(new TestingDatabaseRetrieveMount());
    MockRecallReportPacker mrrp(&trm,lc);
    FakeDiskWriteThreadPool diskWrite(mrrp,rwd,lc);
221
222
223
224
225
226
227
228
    castor::messages::AcsProxyDummy acs;
    castor::mediachanger::MmcProxyDummy mmc;
    castor::legacymsg::RmcProxyDummy rmc;
    castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
    castor::messages::TapeserverProxyDummy initialProcess;  
    castor::tape::tapeserver::daemon::VolumeInfo volume;
    volume.vid="V12345";
    volume.mountType=cta::MountType::RETRIEVE;
229
    cta::server::ProcessCapDummy cap;
230
231
    castor::tape::tapeserver::daemon::TapeServerReporter tsr(initialProcess, DriveConfig(), "0.0.0.0", volume, lc);  
    FakeSingleTapeReadThread tapeRead(drive, mc, tsr, volume, cap, lc);
232

233
    tapeserver::daemon::RecallTaskInjector rti(mm, tapeRead, diskWrite, trm, 6, blockSize, lc);
234

Steven Murray's avatar
Steven Murray committed
235
    ASSERT_FALSE(rti.synchronousInjection());
236
237
    ASSERT_EQ(0U, diskWrite.m_tasks.size());
    ASSERT_EQ(0U, tapeRead.m_tasks.size());
238
    ASSERT_EQ(1, trm.getJobs);
239
  }
240
}