RdbmsCatalogue.hpp 48.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * The CERN Tape Archive (CTA) project
 * Copyright (C) 2015  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 3 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, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "catalogue/Catalogue.hpp"
22
#include "catalogue/RequesterAndGroupMountPolicies.hpp"
Steven Murray's avatar
Steven Murray committed
23
#include "catalogue/TimeBasedCache.hpp"
24
#include "common/threading/Mutex.hpp"
25
#include "rdbms/ConnPool.hpp"
26
#include "rdbms/Login.hpp"
27

28
#include <memory>
29

30
31
32
33
34
35
36
namespace cta {
namespace common {
namespace dataStructures {

/**
 * Forward declaration.
 */
37
class TapeFile;
38
39
40
41
42

} // namespace dataStructures
} // namespace catalogue
} // namespace cta

43
44
45
namespace cta {
namespace catalogue {

46
47
48
49
50
/**
 * Forward declaration.
 */
class ArchiveFileRow;

51
52
53
/**
 * Forward declaration.
 */
54
class RdbmsCatalogueGetArchiveFilesItor;
55

56
/**
Steven Murray's avatar
Steven Murray committed
57
 * CTA catalogue implemented using a relational database backend.
58
 */
59
class RdbmsCatalogue: public Catalogue {
60
protected:
61

62
63
64
  /**
   * Protected constructor only to be called by sub-classes.
   *
65
   * @param log Object representing the API to the CTA logging system.
66
67
   * @param login The database login details to be used to create new
   * connections.
68
69
70
71
72
73
   * @param nbConns The maximum number of concurrent connections to the
   * underlying relational database for all operations accept listing archive
   * files which can be relatively long operations.
   * @param nbArchiveFileListingConns The maximum number of concurrent
   * connections to the underlying relational database for the sole purpose of
   * listing archive files.
74
   */
75
76
  RdbmsCatalogue(
    log::Logger &log,
77
    const rdbms::Login &login,
78
    const uint64_t nbConns,
79
    const uint64_t nbArchiveFileListingConns);
80

81
82
public:

83
84
85
  /**
   * Destructor.
   */
86
  ~RdbmsCatalogue() override;
87

88
89
90
  /////////////////////////////////////////////////////////////////////
  // START OF METHODS DIRECTLY INVOLVED IN DATA TRANSFER AND SCHEDULING
  /////////////////////////////////////////////////////////////////////
91
92
93
94
95
96
97

  /**
   * Notifies the catalogue that the specified tape was labelled.
   *
   * @param vid The volume identifier of the tape.
   * @param drive The name of tape drive that was used to label the tape.
   */
98
  void tapeLabelled(const std::string &vid, const std::string &drive) override;
99

100
  /**
101
102
103
   * Checks the specified archival could take place and returns a new and
   * unique archive file identifier that can be used by a new archive file
   * within the catalogue.
104
105
106
107
108
109
110
111
112
113
   *
   * @param diskInstanceName The name of the disk instance to which the
   * storage class belongs.
   * @param storageClassName The name of the storage class of the file to be
   * archived.  The storage class name is only guaranteed to be unique within
   * its disk instance.  The storage class name will be used by the Catalogue
   * to determine the destination tape pool for each tape copy.
   * @param user The user for whom the file is to be archived.  This will be
   * used by the Catalogue to determine the mount policy to be used when
   * archiving the file.
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
   * @return The new archive file identifier.
   */
  uint64_t checkAndGetNextArchiveFileId(
    const std::string &diskInstanceName,
    const std::string &storageClassName,
    const common::dataStructures::UserIdentity &user) override;

  /**
   * Returns the information required to queue an archive request.
   *
   * @param diskInstanceName The name of the disk instance to which the
   * storage class belongs.
   * @param storageClassName The name of the storage class of the file to be
   * archived.  The storage class name is only guaranteed to be unique within
   * its disk instance.  The storage class name will be used by the Catalogue
   * to determine the destination tape pool for each tape copy.
   * @param user The user for whom the file is to be archived.  This will be
   * used by the Catalogue to determine the mount policy to be used when
   * archiving the file.
   * @return The information required to queue an archive request.
134
135
136
137
138
139
   */
  common::dataStructures::ArchiveFileQueueCriteria getArchiveFileQueueCriteria(
    const std::string &diskInstanceName,
    const std::string &storageClassName,
    const common::dataStructures::UserIdentity &user) override;

140
141
142
143
144
145
  /**
   * Returns the list of tapes that can be written to by a tape drive in the
   * specified logical library, in other words tapes that are labelled, not
   * disabled, not full and are in the specified logical library.
   *
   * @param logicalLibraryName The name of the logical library.
146
   * @return The list of tapes for writing.
147
   */
148
  std::list<TapeForWriting> getTapesForWriting(const std::string &logicalLibraryName) const override;
149
150

  /**
151
152
153
154
155
   * Notifies the CTA catalogue that the specified tape has been mounted in
   * order to archive files.
   *
   * The purpose of this method is to keep track of which drive mounted a given
   * tape for archiving files last.
156
157
   *
   * @param vid The volume identifier of the tape.
158
   * @param drive The name of the drive where the tape was mounted.
159
   */
160
  void tapeMountedForArchive(const std::string &vid, const std::string &drive) override;
161

162
  /**
163
164
   * Prepares for a file retrieval by returning the information required to
   * queue the associated retrieve request(s).
165
   *
166
167
   * @param diskInstanceName The name of the instance from where the retrieval
   * request originated
168
169
170
171
172
   * @param archiveFileId The unique identifier of the archived file that is
   * to be retrieved.
   * @param user The user for whom the file is to be retrieved.  This will be
   * used by the Catalogue to determine the mount policy to be used when
   * retrieving the file.
173
   * @param lc The log context.
174
175
   *
   * @return The information required to queue the associated retrieve request(s).
176
   */
177
  common::dataStructures::RetrieveFileQueueCriteria prepareToRetrieveFile(
178
    const std::string &diskInstanceName,
179
    const uint64_t archiveFileId,
180
181
    const common::dataStructures::UserIdentity &user,
    log::LogContext &lc) override;
182
183
184
185
186

  /**
   * Notifies the CTA catalogue that the specified tape has been mounted in
   * order to retrieve files.
   *
187
188
189
   * The purpose of this method is to keep track of which drive mounted a given
   * tape for retrieving files last.
   *
190
191
192
   * @param vid The volume identifier of the tape.
   * @param drive The name of the drive where the tape was mounted.
   */
193
  void tapeMountedForRetrieve(const std::string &vid, const std::string &drive) override;
194

195
196
197
198
199
200
  /**
   * This method notifies the CTA catalogue that there is no more free space on
   * the specified tape.
   *
   * @param vid The volume identifier of the tape.
   */
201
  void noSpaceLeftOnTape(const std::string &vid) override;
202
203
204
205

  ///////////////////////////////////////////////////////////////////
  // END OF METHODS DIRECTLY INVOLVED IN DATA TRANSFER AND SCHEDULING
  ///////////////////////////////////////////////////////////////////
206

Steven Murray's avatar
Steven Murray committed
207
  void createAdminUser(const common::dataStructures::SecurityIdentity &admin, const std::string &username, const std::string &comment) override;
208
209
  void deleteAdminUser(const std::string &username) override;
  std::list<common::dataStructures::AdminUser> getAdminUsers() const override;
Steven Murray's avatar
Steven Murray committed
210
  void modifyAdminUserComment(const common::dataStructures::SecurityIdentity &admin, const std::string &username, const std::string &comment) override;
211

212
213
214
  /**
   * Creates the specified storage class.
   *
Steven Murray's avatar
Steven Murray committed
215
   * @param admin The administrator.
216
217
   * @param storageClass The storage class.
   */
218
  void createStorageClass(
Steven Murray's avatar
Steven Murray committed
219
    const common::dataStructures::SecurityIdentity &admin,
220
    const common::dataStructures::StorageClass &storageClass) override;
221
222
223
224
225
226
227
228
229

  /**
   * Deletes the specified storage class.
   *
   * @param diskInstanceName The name of the disk instance to which the
   * storage class belongs.
   * @param stoargeClassName The name of the storage class which is only
   * guaranteed to be unique within its disk isntance.
   */
230
  void deleteStorageClass(const std::string &diskInstanceName, const std::string &storageClassName) override;
231

232
  std::list<common::dataStructures::StorageClass> getStorageClasses() const override;
Steven Murray's avatar
Steven Murray committed
233
234
  void modifyStorageClassNbCopies(const common::dataStructures::SecurityIdentity &admin, const std::string &instanceName, const std::string &name, const uint64_t nbCopies) override;
  void modifyStorageClassComment(const common::dataStructures::SecurityIdentity &admin, const std::string &instanceName, const std::string &name, const std::string &comment) override;
235

236
  void createTapePool(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const std::string &vo, const uint64_t nbPartialTapes, const bool encryptionValue, const cta::optional<std::string> &supply, const std::string &comment) override;
237
  void deleteTapePool(const std::string &name) override;
238
  std::list<TapePool> getTapePools() const override;
239
  void modifyTapePoolVo(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const std::string &vo) override;
Steven Murray's avatar
Steven Murray committed
240
241
242
  void modifyTapePoolNbPartialTapes(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const uint64_t nbPartialTapes) override;
  void modifyTapePoolComment(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const std::string &comment) override;
  void setTapePoolEncryption(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const bool encryptionValue) override;
243

244
  void createArchiveRoute(
Steven Murray's avatar
Steven Murray committed
245
    const common::dataStructures::SecurityIdentity &admin,
246
247
    const std::string &diskInstanceName,
    const std::string &storageClassName,
248
    const uint32_t copyNb,
249
    const std::string &tapePoolName,
250
    const std::string &comment) override;
251
252
253
254
255
256
257
258
259
260
261


  /**
   * Deletes the specified archive route.
   *
   * @param diskInstanceName The name of the disk instance to which the storage
   * class belongs.
   * @param storageClassName The name of the storage class which is only
   * guaranteed to be unique within its disk instance.
   * @param copyNb The copy number of the tape file.
   */
262
  void deleteArchiveRoute(
263
264
    const std::string &diskInstanceName,
    const std::string &storageClassName, 
265
    const uint32_t copyNb) override;
266

267
  std::list<common::dataStructures::ArchiveRoute> getArchiveRoutes() const override;
268
269
  void modifyArchiveRouteTapePoolName(const common::dataStructures::SecurityIdentity &admin, const std::string &instanceName, const std::string &storageClassName, const uint32_t copyNb, const std::string &tapePoolName) override;
  void modifyArchiveRouteComment(const common::dataStructures::SecurityIdentity &admin, const std::string &instanceName, const std::string &storageClassName, const uint32_t copyNb, const std::string &comment) override;
270

Steven Murray's avatar
Steven Murray committed
271
  void createLogicalLibrary(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const std::string &comment) override;
272
273
  void deleteLogicalLibrary(const std::string &name) override;
  std::list<common::dataStructures::LogicalLibrary> getLogicalLibraries() const override;
Steven Murray's avatar
Steven Murray committed
274
  void modifyLogicalLibraryComment(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const std::string &comment) override;
275

276
  /**
277
   * Creates a tape.
278
   */
279
  void createTape(
Steven Murray's avatar
Steven Murray committed
280
    const common::dataStructures::SecurityIdentity &admin,
281
    const std::string &vid,
282
283
    const std::string &mediaType,
    const std::string &vendor,
284
285
286
287
288
    const std::string &logicalLibraryName,
    const std::string &tapePoolName,
    const uint64_t capacityInBytes,
    const bool disabled,
    const bool full,
289
    const std::string &comment) override;
290

291
  void deleteTape(const std::string &vid) override;
292
293
294
295
296
297
298

  /**
   * Returns the list of tapes that meet the specified search criteria.
   *
   * @param searchCriteria The search criteria.
   * @return The list of tapes.
   */
299
  std::list<common::dataStructures::Tape> getTapes(const TapeSearchCriteria &searchCriteria) const override;
300

301
302
303
304
305
306
307
308
309
  /**
   * Returns the tapes with the specified volume identifiers.
   *
   * This method will throw an exception if it cannot find ALL of the specified
   * tapes.
   *
   * @param vids The tape volume identifiers (VIDs).
   * @return Map from tape volume identifier to tape.
   */
310
  common::dataStructures::VidToTapeMap getTapesByVid(const std::set<std::string> &vids) const override;
311

Steven Murray's avatar
Steven Murray committed
312
313
314
315
316
317
318
  /**
   * Returns all the tapes within the CTA catalogue.
   *
   * @return Map from tape volume identifier to tape.
   */
  common::dataStructures::VidToTapeMap getAllTapes() const override;

319
320
321
322
323
324
325
326
327
328
  /**
   * Reclaims the specified tape.
   *
   * This method will throw an exception if the specified tape does not exist.
   *
   * This method will throw an exception if the specified tape is not FULL.
   *
   * This method will throw an exception if there is still at least one tape
   * file recorded in the cataligue as being on the specified tape.
   *
Steven Murray's avatar
Steven Murray committed
329
   * @param admin The administrator.
330
331
   * @param vid The volume identifier of the tape to be reclaimed.
   */
Steven Murray's avatar
Steven Murray committed
332
  void reclaimTape(const common::dataStructures::SecurityIdentity &admin, const std::string &vid) override;
333
  void fakeReclaimTapeForTests(const common::dataStructures::SecurityIdentity& admin, const std::string& vid) override;
334
335
  void modifyTapeMediaType(const common::dataStructures::SecurityIdentity &admin, const std::string &vid, const std::string &mediaType) override;
  void modifyTapeVendor(const common::dataStructures::SecurityIdentity &admin, const std::string &vid, const std::string &vendor) override;
Steven Murray's avatar
Steven Murray committed
336
337
338
339
  void modifyTapeLogicalLibraryName(const common::dataStructures::SecurityIdentity &admin, const std::string &vid, const std::string &logicalLibraryName) override;
  void modifyTapeTapePoolName(const common::dataStructures::SecurityIdentity &admin, const std::string &vid, const std::string &tapePoolName) override;
  void modifyTapeCapacityInBytes(const common::dataStructures::SecurityIdentity &admin, const std::string &vid, const uint64_t capacityInBytes) override;
  void modifyTapeEncryptionKey(const common::dataStructures::SecurityIdentity &admin, const std::string &vid, const std::string &encryptionKey) override;
340
341
342
343
344
345
346

  /**
   * Sets the full status of the specified tape.
   *
   * Please note that this method is to be called by the CTA front-end in
   * response to a command from the CTA command-line interface (CLI).
   *
Steven Murray's avatar
Steven Murray committed
347
   * @param admin The administrator.
348
349
350
   * @param vid The volume identifier of the tape to be marked as full.
   * @param fullValue Set to true if the tape is full.
   */
Steven Murray's avatar
Steven Murray committed
351
  void setTapeFull(const common::dataStructures::SecurityIdentity &admin, const std::string &vid, const bool fullValue) override;
352

Steven Murray's avatar
Steven Murray committed
353
354
  void setTapeDisabled(const common::dataStructures::SecurityIdentity &admin, const std::string &vid, const bool disabledValue) override;
  void modifyTapeComment(const common::dataStructures::SecurityIdentity &admin, const std::string &vid, const std::string &comment) override;
355

Steven Murray's avatar
Steven Murray committed
356
357
358
359
  void modifyRequesterMountRulePolicy(const common::dataStructures::SecurityIdentity &admin, const std::string &instanceName, const std::string &requesterName, const std::string &mountPolicy) override;
  void modifyRequesteMountRuleComment(const common::dataStructures::SecurityIdentity &admin, const std::string &instanceName, const std::string &requesterName, const std::string &comment) override;
  void modifyRequesterGroupMountRulePolicy(const common::dataStructures::SecurityIdentity &admin, const std::string &instanceName, const std::string &requesterGroupName, const std::string &mountPolicy) override;
  void modifyRequesterGroupMountRuleComment(const common::dataStructures::SecurityIdentity &admin, const std::string &instanceName, const std::string &requesterGroupName, const std::string &comment) override;
360

361
  void createMountPolicy(
Steven Murray's avatar
Steven Murray committed
362
    const common::dataStructures::SecurityIdentity &admin,
363
364
365
366
367
368
    const std::string &name,
    const uint64_t archivePriority,
    const uint64_t minArchiveRequestAge,
    const uint64_t retrievePriority,
    const uint64_t minRetrieveRequestAge,
    const uint64_t maxDrivesAllowed,
369
    const std::string &comment) override;
370

371
  /**
372
   * Returns the list of all existing mount policies.
373
   *
374
375
   * @return the list of all existing mount policies.
   */
376
  std::list<common::dataStructures::MountPolicy> getMountPolicies() const override;
377
378
379
380
381
382

  /**
   * Deletes the specified mount policy.
   *
   * @param name The name of the mount policy.
   */
383
  void deleteMountPolicy(const std::string &name) override;
384
385
386
387
388
389
390

  /**
   * Creates the rule that the specified mount policy will be used for the
   * specified requester.
   *
   * Please note that requester mount-rules overrule requester-group
   * mount-rules.
391
   *
Steven Murray's avatar
Steven Murray committed
392
   * @param admin The administrator.
393
   * @param mountPolicyName The name of the mount policy.
394
395
396
397
   * @param diskInstance The name of the disk instance to which the requester
   * belongs.
   * @param requesterName The name of the requester which is only guarantted to
   * be unique within its disk instance.
398
399
   * @param comment Comment.
   */
400
  void createRequesterMountRule(
Steven Murray's avatar
Steven Murray committed
401
    const common::dataStructures::SecurityIdentity &admin,
402
    const std::string &mountPolicyName,
403
    const std::string &diskInstance,
404
    const std::string &requesterName,
405
    const std::string &comment) override;
406
407

  /**
408
409
410
411
412
413
   * Returns the rules that specify which mount policy is be used for which
   * requester.
   *
   * @return the rules that specify which mount policy is be used for which
   * requester.
   */
414
  std::list<common::dataStructures::RequesterMountRule> getRequesterMountRules() const override;
415
416
417
418

  /**
   * Deletes the specified mount rule.
   *
419
420
421
422
   * @param diskInstanceName The name of the disk instance to which the
   * requester belongs.
   * @param requesterName The name of the requester which is only guaranteed to
   * be unique within its disk instance.
423
   */
424
  void deleteRequesterMountRule(const std::string &diskInstanceName, const std::string &requesterName) override;
425
426
427
428

  /**
   * Creates the rule that the specified mount policy will be used for the
   * specified requester group.
429
   *
430
431
   * Please note that requester mount-rules overrule requester-group
   * mount-rules.
432
   *
Steven Murray's avatar
Steven Murray committed
433
   * @param admin The administrator.
434
   * @param mountPolicyName The name of the mount policy.
435
436
437
438
   * @param diskInstanceName The name of the disk instance to which the
   * requester group belongs.
   * @param requesterGroupName The name of the requester group which is only
   * guarantted to be unique within its disk instance.
439
440
   * @param comment Comment.
   */
441
  void createRequesterGroupMountRule(
Steven Murray's avatar
Steven Murray committed
442
    const common::dataStructures::SecurityIdentity &admin,
443
    const std::string &mountPolicyName,
444
    const std::string &diskInstanceName,
445
    const std::string &requesterGroupName,
446
    const std::string &comment) override;
447
448
449
450
451
452
453

  /**
   * Returns the rules that specify which mount policy is be used for which
   * requester group.
   *
   * @return the rules that specify which mount policy is be used for which
   * requester group.
454
   */
455
  std::list<common::dataStructures::RequesterGroupMountRule> getRequesterGroupMountRules() const override;
456
457
458
459

  /**
   * Deletes the specified mount rule.
   *
460
461
462
463
   * @param diskInstanceName The name of the disk instance to which the
   * requester group belongs.
   * @param requesterGroupName The name of the requester group which is only
   * guaranteed to be unique within its disk instance.
464
   */
465
  void deleteRequesterGroupMountRule(
466
    const std::string &diskInstanceName,
467
468
    const std::string &requesterGroupName) override;

Steven Murray's avatar
Steven Murray committed
469
470
471
472
473
474
  void modifyMountPolicyArchivePriority(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const uint64_t archivePriority) override;
  void modifyMountPolicyArchiveMinRequestAge(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const uint64_t minArchiveRequestAge) override;
  void modifyMountPolicyRetrievePriority(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const uint64_t retrievePriority) override;
  void modifyMountPolicyRetrieveMinRequestAge(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const uint64_t minRetrieveRequestAge) override;
  void modifyMountPolicyMaxDrivesAllowed(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const uint64_t maxDrivesAllowed) override;
  void modifyMountPolicyComment(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const std::string &comment) override;
475

476
  /**
477
478
   * Throws a UserError exception if the specified searchCriteria is not valid
   * due to a user error.
479
480
481
   *
   * @param searchCriteria The search criteria.
   */
482
  void checkTapeFileSearchCriteria(const TapeFileSearchCriteria &searchCriteria) const;
483

484
  /**
485
486
   * Returns the specified archive files.  Please note that the list of files
   * is ordered by archive file ID.
487
488
   *
   * @param searchCriteria The search criteria.
489
   * @return The archive files.
490
   */
491
  ArchiveFileItor getArchiveFilesItor(const TapeFileSearchCriteria &searchCriteria) const override;
492

493
  /**
494
   * Returns the specified files in tape file sequence order.
495
496
497
498
499
   *
   * @param vid The volume identifier of the tape.
   * @param startFSeq The file sequence number of the first file.  Please note
   * that there might not be a file with this exact file sequence number.
   * @param maxNbFiles The maximum number of files to be returned.
500
   * @return The specified files in tape file sequence order.
501
   */
502
  std::list<common::dataStructures::ArchiveFile> getFilesForRepack(
503
504
505
506
    const std::string &vid,
    const uint64_t startFSeq,
    const uint64_t maxNbFiles) const override;

507
508
509
510
511
512
513
514
515
516
517
518
519
520
  /**
   * Returns all the tape copies (no matter their VIDs) of the archive files
   * associated with the tape files on the specified tape in FSEQ order
   * starting at the specified startFSeq.
   *
   * @param vid The volume identifier of the tape.
   * @param startFSeq The file sequence number of the first file.  Please note
   * that there might not be a file with this exact file sequence number.
   * @return The specified files in FSEQ order.
   */
  ArchiveFileItor getArchiveFilesForRepackItor(
    const std::string &vid,
    const uint64_t startFSeq) const override;

521
  /**
522
   * Returns a summary of the tape files that meet the specified search
523
524
525
526
527
   * criteria.
   *
   * @param searchCriteria The search criteria.
   * @return The summary.
   */
528
  common::dataStructures::ArchiveFileSummary getTapeFileSummary(
529
    const TapeFileSearchCriteria &searchCriteria) const override;
530
531
532
533
534
535
536
537
538
539

  /**
   * Returns the archive file with the specified unique identifier.
   *
   * This method assumes that the archive file being requested exists and will
   * therefore throw an exception if it does not.
   *
   * @param id The unique identifier of the archive file.
   * @return The archive file.
   */
540
  common::dataStructures::ArchiveFile getArchiveFileById(const uint64_t id) override;
541

542
  /**
543
   * Returns true if the specified user has administrator privileges.
544
   *
Steven Murray's avatar
Steven Murray committed
545
   * @param admin The administrator.
546
   * @return True if the specified user has administrator privileges.
547
   */
Steven Murray's avatar
Steven Murray committed
548
  bool isAdmin(const common::dataStructures::SecurityIdentity &admin) const override;
549

550
551
552
553
554
555
  /**
   * Checks that the most trivial query goes through. Returns true on success,
   * false on failure.
   * 
   * @return True if the query went through.
   */
556
  void ping() override;
557

558
559
560
561
562
563
564
565
566
  /**
   * Returns the names of all the tables in the database schema in alphabetical
   * order.
   *
   * @return The names of all the tables in the database schema in alphabetical
   * order.
   */
  std::list<std::string> getTableNames() const;

567
protected:
568

569
570
571
572
573
  /**
   * Object representing the API to the CTA logging system.
   */
  log::Logger &m_log;

574
575
576
  /**
   * Mutex to be used to a take a global lock on the database.
   */
577
  threading::Mutex m_mutex;
578
579

  /**
580
581
582
   * The pool of connections to the underlying relational database to be used
   * for all operations accept listing archive files which can be relatively
   * long operations.
583
   */
584
  mutable rdbms::ConnPool m_connPool;
585

586
587
588
589
590
591
  /**
   * The pool of connections to the underlying relational database to be used
   * for the sole purpose of listing archive files.
   */
  mutable rdbms::ConnPool m_archiveFileListingConnPool;

592
  /**
593
   * Returns true if the specified admin user exists.
594
   *
595
   * @param conn The database connection.
596
597
   * @param adminUsername The name of the admin user.
   * @return True if the admin user exists.
598
   */
599
  bool adminUserExists(rdbms::Conn &conn, const std::string adminUsername) const;
600
601
602
603

  /**
   * Returns true if the specified storage class exists.
   *
604
   * @param conn The database connection.
605
   * @param diskInstanceName The name of the disk instance to which the storage
606
   * class belongs.
607
   * @param storageClassName The name of the storage class.
608
609
   * @return True if the storage class exists.
   */
610
  bool storageClassExists(rdbms::Conn &conn, const std::string &diskInstanceName, const std::string &storageClassName)
611
    const;
612

613
614
615
616
617
618
619
620
  /**
   * Returns true if the specified tape pool exists.
   *
   * @param tapePoolName The name of the tape pool.
   * @return True if the tape pool exists.
   */
  bool tapePoolExists(const std::string &tapePoolName) const override;

621
622
623
  /**
   * Returns true if the specified tape pool exists.
   *
624
   * @param conn The database connection.
625
626
627
   * @param tapePoolName The name of the tape pool.
   * @return True if the tape pool exists.
   */
628
  bool tapePoolExists(rdbms::Conn &conn, const std::string &tapePoolName) const;
629

630
631
632
633
634
635
636
  /**
   * Returns true if the specified archive file identifier exists.
   *
   * @param conn The database connection.
   * @param archiveFileId The archive file identifier.
   * @return True if the archive file identifier exists.
   */
637
  bool archiveFileIdExists(rdbms::Conn &conn, const uint64_t archiveFileId) const;
638

639
640
641
642
643
644
645
  /**
   * Returns true if the specified disk file identifier exists.
   *
   * @param conn The database connection.
   * @param diskInstanceName The name of the disk instance to which the disk
   * file identifier belongs.
   * @param diskFileId The disk file identifier.
646
   * @return True if the disk file identifier exists.
647
   */
648
  bool diskFileIdExists(rdbms::Conn &conn, const std::string &diskInstanceName, const std::string &diskFileId) const;
649

650
651
652
653
654
  /**
   * Returns true if the specified disk file path exists.
   *
   * @param conn The database connection.
   * @param diskInstanceName The name of the disk instance to which the disk
655
   * file path belongs.
656
657
658
   * @param diskFilePath The disk file path.
   * @return True if the disk file path exists.
   */
659
  bool diskFilePathExists(rdbms::Conn &conn, const std::string &diskInstanceName, const std::string &diskFilePath)
660
661
    const;

662
663
664
665
666
667
668
669
670
  /**
   * Returns true if the specified disk file user exists.
   *
   * @param conn The database connection.
   * @param diskInstanceName The name of the disk instance to which the disk
   * file user belongs.
   * @param diskFileUSer The name of the disk file user.
   * @return True if the disk file user exists.
   */
671
  bool diskFileUserExists(rdbms::Conn &conn, const std::string &diskInstanceName, const std::string &diskFileUser)
672
673
    const;

674
675
676
677
678
679
  /**
   * Returns true if the specified disk file group exists.
   *
   * @param conn The database connection.
   * @param diskInstanceName The name of the disk instance to which the disk
   * file group belongs.
680
   * @param diskFileGroup The name of the disk file group.
681
682
   * @return True if the disk file group exists.
   */
683
  bool diskFileGroupExists(rdbms::Conn &conn, const std::string &diskInstanceName, const std::string &diskFileGroup)
684
685
    const;

686
687
688
689
690
691
692
693
694
695
696
  /**
   * Returns true if the specified archive route exists.
   *
   * @param conn The database connection.
   * @param diskInstanceName The name of the disk instance to which the storage
   * class belongs.
   * @param storageClassName The name of the storage class which is only
   * guaranteed to be unique within its disk instance.
   * @param copyNb The copy number of the tape file.
   * @return True if the archive route exists.
   */
697
  bool archiveRouteExists(rdbms::Conn &conn, const std::string &diskInstanceName, const std::string &storageClassName,
698
    const uint32_t copyNb) const;
699

700
701
702
703
704
705
706
707
  /**
   * Returns true if the specified tape exists.
   *
   * @param vid The volume identifier of the tape.
   * @return True if the tape exists.
   */
  bool tapeExists(const std::string &vid) const override;

708
709
710
  /**
   * Returns true if the specified tape exists.
   *
711
   * @param conn The database connection.
712
713
714
   * @param vid The volume identifier of the tape.
   * @return True if the tape exists.
   */
715
  bool tapeExists(rdbms::Conn &conn, const std::string &vid) const;
716
717
718
  
  /**
   * Returns true if non superseded files exist after fSeq in the tape where vid is passed in parameter
719
   * If there is only superseded files after fSeq, these tape files will be deleted
720
721
722
723
724
   * 
   * @param vid the vid of the tape to check if non superseded files exist after fSeq
   * @param fSeq the fSeq after which we want to check if non superseded files exist
   * @return true if non superseded files exist, false otherwise
   */
725
  bool existNonSupersededFilesAfterFSeqAndDeleteTapeFilesForWriting(const std::string& vid, const uint64_t fSeq) const override;
726

727

728
729
730
731
732
733
734
  /**
   * Returns the list of tapes that meet the specified search criteria.
   *
   * @param conn The database connection.
   * @param searchCriteria The search criteria.
   * @return The list of tapes.
   */
735
  std::list<common::dataStructures::Tape> getTapes(rdbms::Conn &conn, const TapeSearchCriteria &searchCriteria) const;
736

737
738
739
  /**
   * Returns true if the specified logical library exists.
   *
740
   * @param conn The database connection.
741
742
743
   * @param logicalLibraryName The name of the logical library.
   * @return True if the logical library exists.
   */
744
  bool logicalLibraryExists(rdbms::Conn &conn, const std::string &logicalLibraryName) const;
745
746
747
748

  /**
   * Returns true if the specified mount policy exists.
   *
749
   * @param conn The database connection.
750
751
752
   * @param mountPolicyName The name of the mount policy
   * @return True if the mount policy exists.
   */
753
  bool mountPolicyExists(rdbms::Conn &conn, const std::string &mountPolicyName) const;
754

755
  /**
756
   * Returns true if the specified requester mount-rule exists.
757
   *
758
759
760
761
762
   * @param diskInstanceName The name of the disk instance to which the
   * requester belongs.
   * @param requesterName The username of the requester which is only guaranteed
   * to be unique within its disk instance.
   * @return True if the requester mount-rule exists.
763
   */
764
  bool requesterMountRuleExists(rdbms::Conn &conn, const std::string &diskInstanceName,
765
    const std::string &requesterName) const;
766
767

  /**
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
    * A fully qualified user, in other words the name of the disk instance and
    * the name of the group.
    */
  struct User {
    /**
     * The name of the disk instance to which the user name belongs.
     */
    std::string diskInstanceName;

    /**
     * The name of the user which is only guaranteed to be unique within its
     * disk instance.
     */
    std::string username;

    /**
     * Constructor.
     *
     * @param d The name of the disk instance to which the group name belongs.
     * @param u The name of the group which is only guaranteed to be unique
     * within its disk instance.
     */
    User(const std::string &d, const std::string &u): diskInstanceName(d), username(u) {
    }

    /**
     * Less than operator.
     *
     * @param rhs The argument on the right hand side of the operator.
     * @return True if this object is less than the argument on the right hand
     * side of the operator.
     */
    bool operator<(const User &rhs) const {
      return diskInstanceName < rhs.diskInstanceName || username < rhs.username;
    }
  }; // struct User

  /**
   * Returns a cached version of the specified requester mount-policy or nullopt
   * if one does not exist.
808
   *
809
810
   * @param user The fully qualified user, in other words the name of the disk
   * instance and the name of the group.
811
   * @return The mount policy or nullopt if one does not exists.
812
   */
813
  optional<common::dataStructures::MountPolicy> getCachedRequesterMountPolicy(const User &user) const;
814
815
816
817
818
819
820
821
822
823
824

  /**
   * Returns the specified requester mount-policy or nullopt if one does not
   * exist.
   *
   * @param conn The database connection.
   * @param user The fully qualified user, in other words the name of the disk
   * instance and the name of the group.
   * @return The mount policy or nullopt if one does not exists.
   */
  optional<common::dataStructures::MountPolicy> getRequesterMountPolicy(rdbms::Conn &conn, const User &user) const;
825
826

  /**
827
   * Returns true if the specified requester-group mount-rule exists.
828
   *
829
   * @param conn The database connection.
830
831
832
833
834
   * @param diskInstanceName The name of the disk instance to which the
   * requester group belongs.
   * @param requesterGroupName The name of the requester group which is only
   * guaranteed to be unique within its disk instance.
   * @return True if the requester-group mount-rule exists.
835
   */
836
  bool requesterGroupMountRuleExists(rdbms::Conn &conn, const std::string &diskInstanceName,
837
    const std::string &requesterGroupName) const;
838

839
  /**
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
    * A fully qualified user group, in other words the name of the disk instance
    * and the name of the group.
    */
  struct Group {
    /**
     * The name of the disk instance to which the group name belongs.
     */
    std::string diskInstanceName;

    /**
     * The name of the group which is only guaranteed to be unique within its
     * disk instance.
     */
    std::string groupName;

    /**
     * Constructor.
     *
     * @param d The name of the disk instance to which the group name belongs.
     * @param g The name of the group which is only guaranteed to be unique
     * within its disk instance.
     */
    Group(const std::string &d, const std::string &g): diskInstanceName(d), groupName(g) {
    }

    /**
     * Less than operator.
     *
     * @param rhs The argument on the right hand side of the operator.
     * @return True if this object is less than the argument on the right hand
     * side of the operator.
     */
    bool operator<(const Group &rhs) const {
      return diskInstanceName < rhs.diskInstanceName || groupName < rhs.groupName;
    }
  }; // struct Group

  /**
   * Returns a cached version of the specified requester-group mount-policy or
   * nullptr if one does not exist.
   *
   * This method updates the cache when necessary.
882
   *
883
884
   * @param group The fully qualified group, in other words the name of the disk
   * instance and the name of the group.
885
   * @return The cached mount policy or nullopt if one does not exists.
886
   */
887
  optional<common::dataStructures::MountPolicy> getCachedRequesterGroupMountPolicy(const Group &group) const;
888
889
890
891
892
893
894
895

  /**
   * Returns the specified requester-group mount-policy or nullptr if one does
   * not exist.
   *
   * @param conn The database connection.
   * @param group The fully qualified group, in other words the name of the disk
   * instance and the name of the group.
896
   * @return The mount policy or nullopt if one does not exists.
897
   */
898
899
  optional<common::dataStructures::MountPolicy> getRequesterGroupMountPolicy(rdbms::Conn &conn, const Group &group)
    const;
900

901
902
903
904
905
  /**
   * Returns the specified tape log information from the specified database
   * result set.
   *
   * @param rset The result set.
906
   * @param driveColName The name of the database column that contains the name
907
908
909
910
   * of the tape drive.
   * @param timeColNAme The name of the database column that contains the time
   * stamp.
   */
911
  optional<common::dataStructures::TapeLog> getTapeLogFromRset(const rdbms::Rset &rset,
912
913
    const std::string &driveColName, const std::string &timeColName) const;

914
  /**
915
916
   * An RdbmsCatalogue specific method that inserts the specified row into the
   * ArchiveFile table.
917
   *
918
   * @param conn The database connection.
919
   * @param row The row to be inserted.
920
   */
921
  void insertArchiveFile(rdbms::Conn &conn, const ArchiveFileRow &row);
922

923
924
925
926
927
  /**
   * Creates the database schema.
   */
  void createDbSchema();

928
929
930
931
  /**
   * A fully qualified storage class, in other words the name of the disk
   * instance and the name of the storage class.
   */
932
  struct StorageClass {
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
    /**
     * The name of the disk instance to which the storage class belongs.
     */
    std::string diskInstanceName;

    /**
     * The name of the storage class which is only guaranteed to be unique
     * within its disk instance.
     */
    std::string storageClassName;

    /**
     * Constructor.
     *
     * @param dIN The name of the disk instance to which the storage class
     * belongs.
     * @param sN The name of the storage class which is only guaranteed to be
     * unique within its disk instance.
     */
952
    StorageClass(const std::string &d, const std::string &s): diskInstanceName(d), storageClassName(s) {
953
954
955
956
957
958
959
960
961
    }

    /**
     * Less than operator.
     *
     * @param rhs The argument on the right hand side of the operator.
     * @return True if this object is less than the argument on the right hand
     * side of the operator.
     */
962
    bool operator<(const StorageClass &rhs) const {
963
964
      return diskInstanceName < rhs.diskInstanceName || storageClassName < rhs.storageClassName;
    }
965
  }; // struct StorageClass
966

967
968
969
970
971
972
973
974
975
976
977
978
  /**
   * Returns a cached version of the expected number of archive routes for the
   * specified storage class as specified by the call to the
   * createStorageClass() method as opposed to the actual number entered so far
   * using the createArchiveRoute() method.
   *
   * This method updates the cache when necessary.
   *
   * @param storageClass The fully qualified storage class, in other words the
   * name of the disk instance and the name of the storage class.
   * @return The expected number of archive routes.
   */
979
  uint64_t getCachedExpectedNbArchiveRoutes(const StorageClass &storageClass) const;
980

981
982
983
984
985
986
  /**
   * Returns the expected number of archive routes for the specified storage
   * class as specified by the call to the createStorageClass() method as
   * opposed to the actual number entered so far using the createArchiveRoute()
   * method.
   *
987
   * @param conn The database connection.
988
989
   * @param storageClass The fully qualified storage class, in other words the
   * name of the disk instance and the name of the storage class.
990
991
   * @return The expected number of archive routes.
   */
992
  uint64_t getExpectedNbArchiveRoutes(rdbms::Conn &conn, const StorageClass &storageClass) const;
993

994
  /**
995
   * Inserts the specified tape file into the Tape table.
996
   *
997
   * @param conn The database connection.
998
999
1000
   * @param tapeFile The tape file.
   * @param archiveFileId The identifier of the archive file of which the tape
   * file is a copy.