RecallTaskInjectorTest.cpp 10 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/messages/TapeserverProxyDummy.hpp"
25
26
27
28
#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"
29
#include "castor/tape/tapeserver/daemon/TaskWatchDog.hpp"
30
#include "castor/tape/tapeserver/drive/FakeDrive.hpp"
31
32
33
#include "common/log/DummyLogger.hpp"
#include "common/log/StringLogger.hpp"
#include "common/processCap/ProcessCapDummy.hpp"
34
#include "mediachanger/MediaChangerFacade.hpp"
35
36
#include "scheduler/SchedulerDatabase.hpp"
#include "scheduler/testingMocks/MockRetrieveMount.hpp"
37
#include "scheduler/TapeMountDummy.hpp"
38
39
40

#include <gtest/gtest.h>

41
42
using namespace castor::tape::tapeserver::daemon;
using namespace castor::tape;
43

44

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

    void SetUp() {
    }

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

  class FakeSingleTapeReadThread : public TapeSingleThreadInterface<TapeReadTask>
99
  {
100
101
102
  public:
    using TapeSingleThreadInterface<TapeReadTask>::m_tasks;

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

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

    virtual void countTapeLogError(const std::string & error) {};
128
129
    protected:
      virtual void logSCSIMetrics() {};
130
  };
131
132
  
  class TestingDatabaseRetrieveMount: public cta::SchedulerDatabase::RetrieveMount {
133
    const MountInfo & getMountInfo() override { throw std::runtime_error("Not implemented"); }
134
    std::list<std::unique_ptr<cta::SchedulerDatabase::RetrieveJob> > getNextJobBatch(uint64_t filesRequested, uint64_t bytesRequested,
135
136
137
138
      const std::set<std::string> &fullDiskSystems, cta::log::LogContext& logContext) override { throw std::runtime_error("Not implemented");}
    void requeueJobBatch(std::list<std::unique_ptr<cta::SchedulerDatabase::RetrieveJob> >& jobBatch) override { throw std::runtime_error("Not implemented");}
    void reserveDiskSpace(const cta::SchedulerDatabase::DiskSpaceReservationRequest& diskSpaceReservation) override { throw std::runtime_error("Not implemented");}
    void releaseDiskSpace(const std::string& reservingAgent, const std::string& diskSystemName, uint64_t size) override { throw std::runtime_error("Not implemented");}
139
140
    void complete(time_t completionTime) override { throw std::runtime_error("Not implemented"); }
    void setDriveStatus(cta::common::dataStructures::DriveStatus status, time_t completionTime) override { throw std::runtime_error("Not implemented"); }
141
    void setTapeSessionStats(const castor::tape::tapeserver::daemon::TapeSessionStats &stats) override { throw std::runtime_error("Not implemented"); }
142
    void flushAsyncSuccessReports(std::list<cta::SchedulerDatabase::RetrieveJob*>& jobsBatch, cta::log::LogContext& lc) override { throw std::runtime_error("Not implemented"); }
143
144
  };
  
145
  TEST_F(castor_tape_tapeserver_daemonTest, RecallTaskInjectorNominal) {
146
147
    const int nbJobs=15;
    const int maxNbJobsInjectedAtOnce = 6;
148
    cta::log::StringLogger log("dummy","castor_tape_tapeserver_daemon_RecallTaskInjectorTest",cta::log::DEBUG);
Victor Kotlyar's avatar
Victor Kotlyar committed
149
    cta::log::LogContext lc(log);
150
    RecallMemoryManager mm(50U, 50U, lc);
151
152
    castor::tape::tapeserver::drive::FakeDrive drive;
    
153
154
155
    cta::MockRetrieveMount trm;
    trm.createRetrieveJobs(nbJobs);
    //EXPECT_CALL(trm, internalGetNextJob()).Times(nbJobs+1);
156
    
157
    castor::messages::TapeserverProxyDummy tspd;
158
159
    cta::TapeMountDummy tmd;
    RecallWatchDog rwd(1,1,tspd,tmd,"",lc);
160
161
162
    std::unique_ptr<cta::SchedulerDatabase::RetrieveMount> dbrm(new TestingDatabaseRetrieveMount());
    MockRecallReportPacker mrrp(&trm,lc);
    FakeDiskWriteThreadPool diskWrite(mrrp,rwd,lc);
163
    cta::log::DummyLogger dummyLog("dummy","dummy");
164
    cta::mediachanger::MediaChangerFacade mc(dummyLog);
165
166
167
    castor::messages::TapeserverProxyDummy initialProcess;
    castor::tape::tapeserver::daemon::VolumeInfo volume;
    volume.vid="V12345";
168
    volume.mountType=cta::common::dataStructures::MountType::Retrieve;
169
    castor::tape::tapeserver::daemon::TapeServerReporter gsr(initialProcess, cta::tape::daemon::TpconfigLine(), "0.0.0.0", volume, lc);
170
    cta::server::ProcessCapDummy cap;
171
    FakeSingleTapeReadThread tapeRead(drive, mc, gsr, volume, cap, lc);
172
    tapeserver::daemon::RecallTaskInjector rti(mm, tapeRead, diskWrite, trm, maxNbJobsInjectedAtOnce, blockSize, lc);
173

174
    ASSERT_EQ(true, rti.synchronousFetch());
175
176
    ASSERT_EQ(maxNbJobsInjectedAtOnce, diskWrite.m_tasks.size());
    ASSERT_EQ(maxNbJobsInjectedAtOnce, tapeRead.m_tasks.size());
177
178
179
180
181

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

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

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

195
    for(int i=0; i<1; ++i)
196
197
198
199
200
    {
      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
201
202
      ASSERT_EQ(static_cast<DiskWriteTask*>(NULL), diskWriteTask);
      ASSERT_EQ(static_cast<TapeReadTask*>(NULL), tapeReadTask);
203
204
205
206
      delete diskWriteTask;
      delete tapeReadTask;
    }
  }
207
208
  
  TEST_F(castor_tape_tapeserver_daemonTest, RecallTaskInjectorNoFiles) {
209
    cta::log::StringLogger log("dummy","castor_tape_tapeserver_daemon_RecallTaskInjectorTest",cta::log::DEBUG);
Victor Kotlyar's avatar
Victor Kotlyar committed
210
    cta::log::LogContext lc(log);
211
    RecallMemoryManager mm(50U, 50U, lc);
212
    castor::tape::tapeserver::drive::FakeDrive drive;
213
    
214
215
216
    cta::MockRetrieveMount trm;
    trm.createRetrieveJobs(0);
    //EXPECT_CALL(trm, internalGetNextJob()).Times(1); //no work: single call to getnextjob
217
    
218
    castor::messages::TapeserverProxyDummy tspd;
219
220
    cta::TapeMountDummy tmd;
    RecallWatchDog rwd(1,1,tspd,tmd,"",lc);
221
222
223
    std::unique_ptr<cta::SchedulerDatabase::RetrieveMount> dbrm(new TestingDatabaseRetrieveMount());
    MockRecallReportPacker mrrp(&trm,lc);
    FakeDiskWriteThreadPool diskWrite(mrrp,rwd,lc);
224
    cta::log::DummyLogger dummyLog("dummy","dummy");
225
    cta::mediachanger::MediaChangerFacade mc(dummyLog);
226
227
228
    castor::messages::TapeserverProxyDummy initialProcess;  
    castor::tape::tapeserver::daemon::VolumeInfo volume;
    volume.vid="V12345";
229
    volume.mountType=cta::common::dataStructures::MountType::Retrieve;
230
    cta::server::ProcessCapDummy cap;
231
    castor::tape::tapeserver::daemon::TapeServerReporter tsr(initialProcess, cta::tape::daemon::TpconfigLine(), "0.0.0.0", volume, lc);  
232
    FakeSingleTapeReadThread tapeRead(drive, mc, tsr, volume, cap, lc);
233

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

236
    ASSERT_FALSE(rti.synchronousFetch());
237
238
    ASSERT_EQ(0U, diskWrite.m_tasks.size());
    ASSERT_EQ(0U, tapeRead.m_tasks.size());
239
    ASSERT_EQ(1, trm.getJobs);
240
  }
241
}