RecallTaskInjectorTest.cpp 8.88 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
38
39

#include <gtest/gtest.h>

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

43

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

    void SetUp() {
    }

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

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

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

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

    virtual void countTapeLogError(const std::string & error) {};
127
128
    protected:
      virtual void logSCSIMetrics() {};
129
  };
130
131
132
133
134
  
  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"); }
135
    virtual void setDriveStatus(cta::common::dataStructures::DriveStatus status, time_t completionTime) { throw std::runtime_error("Not implemented"); }
136
137
  };
  
138
  TEST_F(castor_tape_tapeserver_daemonTest, RecallTaskInjectorNominal) {
139
140
    const int nbJobs=15;
    const int maxNbJobsInjectedAtOnce = 6;
Victor Kotlyar's avatar
Victor Kotlyar committed
141
142
    cta::log::StringLogger log("castor_tape_tapeserver_daemon_RecallTaskInjectorTest",cta::log::DEBUG);
    cta::log::LogContext lc(log);
143
    RecallMemoryManager mm(50U, 50U, lc);
144
145
    castor::tape::tapeserver::drive::FakeDrive drive;
    
146
147
148
    cta::MockRetrieveMount trm;
    trm.createRetrieveJobs(nbJobs);
    //EXPECT_CALL(trm, internalGetNextJob()).Times(nbJobs+1);
149
    
150
151
152
153
154
    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);
155
156
    cta::log::DummyLogger dummyLog("dummy");
    cta::mediachanger::MediaChangerFacade mc(log);
157
158
159
    castor::messages::TapeserverProxyDummy initialProcess;
    castor::tape::tapeserver::daemon::VolumeInfo volume;
    volume.vid="V12345";
160
    volume.mountType=cta::common::dataStructures::MountType::Retrieve;
161
    castor::tape::tapeserver::daemon::TapeServerReporter gsr(initialProcess, cta::tape::daemon::TpconfigLine(), "0.0.0.0", volume, lc);
162
    cta::server::ProcessCapDummy cap;
163
    FakeSingleTapeReadThread tapeRead(drive, mc, gsr, volume, cap, lc);
164
    tapeserver::daemon::RecallTaskInjector rti(mm, tapeRead, diskWrite, trm, maxNbJobsInjectedAtOnce, blockSize, lc);
165

166
    ASSERT_EQ(true, rti.synchronousInjection());
167
168
    ASSERT_EQ(maxNbJobsInjectedAtOnce+1, diskWrite.m_tasks.size());
    ASSERT_EQ(maxNbJobsInjectedAtOnce+1, tapeRead.m_tasks.size());
169
170
171
172
173

    rti.startThreads();
    rti.requestInjection(false);
    rti.requestInjection(true);
    rti.finish();
174
    ASSERT_NO_THROW(rti.waitThreads());
175
    ASSERT_EQ(nbJobs+1, trm.getJobs);
176
177

    //pushed nbFile*2 files + 1 end of work
178
179
    ASSERT_EQ(nbJobs+1, diskWrite.m_tasks.size());
    ASSERT_EQ(nbJobs+1, tapeRead.m_tasks.size());
180

181
    for(int i=0; i<nbJobs; ++i)
182
183
184
185
186
    {
      delete diskWrite.m_tasks.pop();
      delete tapeRead.m_tasks.pop();
    }

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

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

Steven Murray's avatar
Steven Murray committed
227
    ASSERT_FALSE(rti.synchronousInjection());
228
229
    ASSERT_EQ(0U, diskWrite.m_tasks.size());
    ASSERT_EQ(0U, tapeRead.m_tasks.size());
230
    ASSERT_EQ(1, trm.getJobs);
231
  }
232
}