RecallTaskInjectorTest.cpp 9.81 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

18
#include "castor/messages/TapeserverProxyDummy.hpp"
19
20
21
22
#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"
23
#include "castor/tape/tapeserver/daemon/TaskWatchDog.hpp"
24
#include "castor/tape/tapeserver/drive/FakeDrive.hpp"
25
26
27
#include "common/log/DummyLogger.hpp"
#include "common/log/StringLogger.hpp"
#include "common/processCap/ProcessCapDummy.hpp"
28
#include "mediachanger/MediaChangerFacade.hpp"
29
30
#include "scheduler/SchedulerDatabase.hpp"
#include "scheduler/testingMocks/MockRetrieveMount.hpp"
31
#include "scheduler/TapeMountDummy.hpp"
32
33
34

#include <gtest/gtest.h>

35
36
using namespace castor::tape::tapeserver::daemon;
using namespace castor::tape;
37

38

39
namespace unitTests
40
{
41
  const int blockSize=4096;
42
43
44
45
46
47
48
49
  class castor_tape_tapeserver_daemonTest: public ::testing::Test {
  protected:

    void SetUp() {
    }

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

  class FakeSingleTapeReadThread : public TapeSingleThreadInterface<TapeReadTask>
93
  {
94
95
96
  public:
    using TapeSingleThreadInterface<TapeReadTask>::m_tasks;

97
    FakeSingleTapeReadThread(tapeserver::drive::DriveInterface& drive,
98
      cta::mediachanger::MediaChangerFacade & mc,
99
100
      tapeserver::daemon::TapeServerReporter & tsr,
      const tapeserver::daemon::VolumeInfo& volInfo, 
101
102
      cta::server::ProcessCap& cap, 
      const uint32_t tapeLoadTimeout,
Victor Kotlyar's avatar
Victor Kotlyar committed
103
      cta::log::LogContext & lc):
104
    TapeSingleThreadInterface<TapeReadTask>(drive, mc, tsr, volInfo,cap, lc, "", tapeLoadTimeout){}
105
106
107
108
109
110
111

    ~FakeSingleTapeReadThread(){
      const unsigned int size= m_tasks.size();
      for(unsigned int i=0;i<size;++i){
        delete m_tasks.pop();
      }
    }
112
113
    
    virtual void run () 
114
    {
115
      m_tasks.push(NULL);
116
    }
117
    
118
119
120
121
122
    virtual void push(TapeReadTask* t){
      m_tasks.push(t);
    }

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

167
168
169
    bool noFilesToRecall;
    ASSERT_EQ(true, rti.synchronousFetch(noFilesToRecall));
    ASSERT_FALSE(noFilesToRecall);
170
171
    ASSERT_EQ(maxNbJobsInjectedAtOnce, diskWrite.m_tasks.size());
    ASSERT_EQ(maxNbJobsInjectedAtOnce, tapeRead.m_tasks.size());
172
173
174
175
176

    rti.startThreads();
    rti.requestInjection(false);
    rti.requestInjection(true);
    rti.finish();
177
    ASSERT_NO_THROW(rti.waitThreads());
178
    ASSERT_EQ(nbJobs+1, trm.getJobs);
179
180

    //pushed nbFile*2 files + 1 end of work
181
182
    ASSERT_EQ(nbJobs+1, diskWrite.m_tasks.size());
    ASSERT_EQ(nbJobs+1, tapeRead.m_tasks.size());
183

184
    for(int i=0; i<nbJobs; ++i)
185
186
187
188
189
    {
      delete diskWrite.m_tasks.pop();
      delete tapeRead.m_tasks.pop();
    }

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

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

231
232
    bool noFilesToRecall;
    ASSERT_FALSE(rti.synchronousFetch(noFilesToRecall));
233
234
    ASSERT_EQ(0U, diskWrite.m_tasks.size());
    ASSERT_EQ(0U, tapeRead.m_tasks.size());
235
    ASSERT_EQ(1, trm.getJobs);
236
    ASSERT_TRUE(noFilesToRecall);
237
  }
238
}