RdbmsCatalogue.hpp 45.9 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 RdbmsArchiveFileItorImpl;
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
   * @param maxTriesToConnext The maximum number of times a single method should
   * try to connect to the database in the event of LostDatabaseConnection
   * exceptions being thrown.
77
   */
78
79
  RdbmsCatalogue(
    log::Logger &log,
80
    const rdbms::Login &login,
81
    const uint64_t nbConns,
82
    const uint64_t nbArchiveFileListingConns,
83
    const uint32_t maxTriesToConnect);
84

85
86
public:

87
88
89
  /**
   * Destructor.
   */
90
  ~RdbmsCatalogue() override;
91

92
93
94
  /////////////////////////////////////////////////////////////////////
  // START OF METHODS DIRECTLY INVOLVED IN DATA TRANSFER AND SCHEDULING
  /////////////////////////////////////////////////////////////////////
95
96
97
98
99
100
101
102

  /**
   * 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.
   * @param lbpIsOn Set to true if Logical Block Protection (LBP) was enabled.
   */
103
  void tapeLabelled(const std::string &vid, const std::string &drive, const bool lbpIsOn) override;
104

105
  /**
106
107
108
   * 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.
109
110
111
112
113
114
115
116
117
118
   *
   * @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.
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
   * @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.
139
140
141
142
143
144
   */
  common::dataStructures::ArchiveFileQueueCriteria getArchiveFileQueueCriteria(
    const std::string &diskInstanceName,
    const std::string &storageClassName,
    const common::dataStructures::UserIdentity &user) override;

145
146
147
148
149
150
  /**
   * 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.
151
   * @return The list of tapes for writing.
152
   */
153
  std::list<TapeForWriting> getTapesForWriting(const std::string &logicalLibraryName) const override;
154
155

  /**
156
157
158
159
160
   * 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.
161
162
   *
   * @param vid The volume identifier of the tape.
163
   * @param drive The name of the drive where the tape was mounted.
164
   */
165
  void tapeMountedForArchive(const std::string &vid, const std::string &drive) override;
166

167
  /**
168
169
   * Prepares for a file retrieval by returning the information required to
   * queue the associated retrieve request(s).
170
   *
171
172
   * @param diskInstanceName The name of the instance from where the retrieval
   * request originated
173
174
175
176
177
   * @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.
178
   * @param lc The log context.
179
180
   *
   * @return The information required to queue the associated retrieve request(s).
181
   */
182
  common::dataStructures::RetrieveFileQueueCriteria prepareToRetrieveFile(
183
    const std::string &diskInstanceName,
184
    const uint64_t archiveFileId,
185
186
    const common::dataStructures::UserIdentity &user,
    log::LogContext &lc) override;
187

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  /**
   * Prepares for a file retrieval by returning the information required to
   * queue the associated retrieve request(s).
   *
   * @param diskInstanceName The name of the instance from where the retrieval
   * request originated
   * @param diskFileId The identifier of the source disk file which is unique
   * within it's host disk system.  Two files from different disk systems may
   * have the same identifier.  The combination of diskInstanceName and
   * diskFileId must be globally unique, in other words unique within the CTA
   * catalogue.
   * @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.
   * @param lc The log context.
   *
   * @return The information required to queue the associated retrieve request(s).
   */
208
  common::dataStructures::RetrieveFileQueueCriteria prepareToRetrieveFileByDiskFileId(
209
210
211
212
213
    const std::string &diskInstanceName,
    const std::string &diskFileId,
    const common::dataStructures::UserIdentity &user,
    log::LogContext &lc) override;

214
215
216
217
  /**
   * Notifies the CTA catalogue that the specified tape has been mounted in
   * order to retrieve files.
   *
218
219
220
   * The purpose of this method is to keep track of which drive mounted a given
   * tape for retrieving files last.
   *
221
222
223
   * @param vid The volume identifier of the tape.
   * @param drive The name of the drive where the tape was mounted.
   */
224
  void tapeMountedForRetrieve(const std::string &vid, const std::string &drive) override;
225

226
227
228
229
230
231
  /**
   * 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.
   */
232
  void noSpaceLeftOnTape(const std::string &vid) override;
233
234
235
236

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

Steven Murray's avatar
Steven Murray committed
238
  void createAdminUser(const common::dataStructures::SecurityIdentity &admin, const std::string &username, const std::string &comment) override;
239
240
  void deleteAdminUser(const std::string &username) override;
  std::list<common::dataStructures::AdminUser> getAdminUsers() const override;
Steven Murray's avatar
Steven Murray committed
241
  void modifyAdminUserComment(const common::dataStructures::SecurityIdentity &admin, const std::string &username, const std::string &comment) override;
242

243
244
245
  /**
   * Creates the specified storage class.
   *
Steven Murray's avatar
Steven Murray committed
246
   * @param admin The administrator.
247
248
   * @param storageClass The storage class.
   */
249
  void createStorageClass(
Steven Murray's avatar
Steven Murray committed
250
    const common::dataStructures::SecurityIdentity &admin,
251
    const common::dataStructures::StorageClass &storageClass) override;
252
253
254
255
256
257
258
259
260

  /**
   * 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.
   */
261
  void deleteStorageClass(const std::string &diskInstanceName, const std::string &storageClassName) override;
262

263
  std::list<common::dataStructures::StorageClass> getStorageClasses() const override;
Steven Murray's avatar
Steven Murray committed
264
265
  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;
266

267
  void createTapePool(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const std::string &vo, const uint64_t nbPartialTapes, const bool encryptionValue, const std::string &comment) override;
268
  void deleteTapePool(const std::string &name) override;
269
  std::list<TapePool> getTapePools() const override;
270
  void modifyTapePoolVo(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const std::string &vo) override;
Steven Murray's avatar
Steven Murray committed
271
272
273
  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;
274

275
  void createArchiveRoute(
Steven Murray's avatar
Steven Murray committed
276
    const common::dataStructures::SecurityIdentity &admin,
277
278
279
280
    const std::string &diskInstanceName,
    const std::string &storageClassName,
    const uint64_t copyNb,
    const std::string &tapePoolName,
281
    const std::string &comment) override;
282
283
284
285
286
287
288
289
290
291
292


  /**
   * 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.
   */
293
  void deleteArchiveRoute(
294
295
    const std::string &diskInstanceName,
    const std::string &storageClassName, 
296
    const uint64_t copyNb) override;
297

298
  std::list<common::dataStructures::ArchiveRoute> getArchiveRoutes() const override;
Steven Murray's avatar
Steven Murray committed
299
300
  void modifyArchiveRouteTapePoolName(const common::dataStructures::SecurityIdentity &admin, const std::string &instanceName, const std::string &storageClassName, const uint64_t copyNb, const std::string &tapePoolName) override;
  void modifyArchiveRouteComment(const common::dataStructures::SecurityIdentity &admin, const std::string &instanceName, const std::string &storageClassName, const uint64_t copyNb, const std::string &comment) override;
301

Steven Murray's avatar
Steven Murray committed
302
  void createLogicalLibrary(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const std::string &comment) override;
303
304
  void deleteLogicalLibrary(const std::string &name) override;
  std::list<common::dataStructures::LogicalLibrary> getLogicalLibraries() const override;
Steven Murray's avatar
Steven Murray committed
305
  void modifyLogicalLibraryComment(const common::dataStructures::SecurityIdentity &admin, const std::string &name, const std::string &comment) override;
306

307
  /**
308
   * Creates a tape.
309
   */
310
  void createTape(
Steven Murray's avatar
Steven Murray committed
311
    const common::dataStructures::SecurityIdentity &admin,
312
313
314
315
316
317
    const std::string &vid,
    const std::string &logicalLibraryName,
    const std::string &tapePoolName,
    const uint64_t capacityInBytes,
    const bool disabled,
    const bool full,
318
    const std::string &comment) override;
319

320
  void deleteTape(const std::string &vid) override;
321
322
323
324
325
326
327

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

330
331
332
333
334
335
336
337
338
  /**
   * 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.
   */
339
  common::dataStructures::VidToTapeMap getTapesByVid(const std::set<std::string> &vids) const override;
340

Steven Murray's avatar
Steven Murray committed
341
342
343
344
345
346
347
  /**
   * Returns all the tapes within the CTA catalogue.
   *
   * @return Map from tape volume identifier to tape.
   */
  common::dataStructures::VidToTapeMap getAllTapes() const override;

348
349
350
351
352
353
354
355
356
357
  /**
   * 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
358
   * @param admin The administrator.
359
360
   * @param vid The volume identifier of the tape to be reclaimed.
   */
Steven Murray's avatar
Steven Murray committed
361
  void reclaimTape(const common::dataStructures::SecurityIdentity &admin, const std::string &vid) override;
362

Steven Murray's avatar
Steven Murray committed
363
364
365
366
  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;
367
368
369
370
371
372
373

  /**
   * 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
374
   * @param admin The administrator.
375
376
377
   * @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
378
  void setTapeFull(const common::dataStructures::SecurityIdentity &admin, const std::string &vid, const bool fullValue) override;
379

Steven Murray's avatar
Steven Murray committed
380
381
  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;
382

Steven Murray's avatar
Steven Murray committed
383
384
385
386
  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;
387

388
  void createMountPolicy(
Steven Murray's avatar
Steven Murray committed
389
    const common::dataStructures::SecurityIdentity &admin,
390
391
392
393
394
395
    const std::string &name,
    const uint64_t archivePriority,
    const uint64_t minArchiveRequestAge,
    const uint64_t retrievePriority,
    const uint64_t minRetrieveRequestAge,
    const uint64_t maxDrivesAllowed,
396
    const std::string &comment) override;
397

398
  /**
399
   * Returns the list of all existing mount policies.
400
   *
401
402
   * @return the list of all existing mount policies.
   */
403
  std::list<common::dataStructures::MountPolicy> getMountPolicies() const override;
404
405
406
407
408
409

  /**
   * Deletes the specified mount policy.
   *
   * @param name The name of the mount policy.
   */
410
  void deleteMountPolicy(const std::string &name) override;
411
412
413
414
415
416
417

  /**
   * 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.
418
   *
Steven Murray's avatar
Steven Murray committed
419
   * @param admin The administrator.
420
   * @param mountPolicyName The name of the mount policy.
421
422
423
424
   * @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.
425
426
   * @param comment Comment.
   */
427
  void createRequesterMountRule(
Steven Murray's avatar
Steven Murray committed
428
    const common::dataStructures::SecurityIdentity &admin,
429
    const std::string &mountPolicyName,
430
    const std::string &diskInstance,
431
    const std::string &requesterName,
432
    const std::string &comment) override;
433
434

  /**
435
436
437
438
439
440
   * 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.
   */
441
  std::list<common::dataStructures::RequesterMountRule> getRequesterMountRules() const override;
442
443
444
445

  /**
   * Deletes the specified mount rule.
   *
446
447
448
449
   * @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.
450
   */
451
  void deleteRequesterMountRule(const std::string &diskInstanceName, const std::string &requesterName) override;
452
453
454
455

  /**
   * Creates the rule that the specified mount policy will be used for the
   * specified requester group.
456
   *
457
458
   * Please note that requester mount-rules overrule requester-group
   * mount-rules.
459
   *
Steven Murray's avatar
Steven Murray committed
460
   * @param admin The administrator.
461
   * @param mountPolicyName The name of the mount policy.
462
463
464
465
   * @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.
466
467
   * @param comment Comment.
   */
468
  void createRequesterGroupMountRule(
Steven Murray's avatar
Steven Murray committed
469
    const common::dataStructures::SecurityIdentity &admin,
470
    const std::string &mountPolicyName,
471
    const std::string &diskInstanceName,
472
    const std::string &requesterGroupName,
473
    const std::string &comment) override;
474
475
476
477
478
479
480

  /**
   * 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.
481
   */
482
  std::list<common::dataStructures::RequesterGroupMountRule> getRequesterGroupMountRules() const override;
483
484
485
486

  /**
   * Deletes the specified mount rule.
   *
487
488
489
490
   * @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.
491
   */
492
  void deleteRequesterGroupMountRule(
493
    const std::string &diskInstanceName,
494
495
    const std::string &requesterGroupName) override;

Steven Murray's avatar
Steven Murray committed
496
497
498
499
500
501
  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;
502

503
  /**
504
505
   * Throws a UserError exception if the specified searchCriteria is not valid
   * due to a user error.
506
507
508
   *
   * @param searchCriteria The search criteria.
   */
509
  void checkTapeFileSearchCriteria(const TapeFileSearchCriteria &searchCriteria) const;
510

511
  /**
512
513
   * Returns the specified archive files.  Please note that the list of files
   * is ordered by archive file ID.
514
515
   *
   * @param searchCriteria The search criteria.
516
   * @return The archive files.
517
   */
518
  ArchiveFileItor getArchiveFiles(const TapeFileSearchCriteria &searchCriteria) const override;
519

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

  /**
   * 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.
   */
539
  common::dataStructures::ArchiveFile getArchiveFileById(const uint64_t id) override;
540

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

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

557
protected:
558

559
560
561
562
563
  /**
   * Object representing the API to the CTA logging system.
   */
  log::Logger &m_log;

564
565
566
  /**
   * Mutex to be used to a take a global lock on the database.
   */
567
  threading::Mutex m_mutex;
568
569

  /**
570
571
572
   * 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.
573
   */
574
  mutable rdbms::ConnPool m_connPool;
575

576
577
578
579
580
581
  /**
   * 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;

582
583
584
585
586
587
  /**
   * The maximum number of times a single method should try to connect to the
   * database in the event of LostDatabaseConnection exceptions being thrown.
   */
  uint32_t m_maxTriesToConnect;

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

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

609
610
611
612
613
614
615
616
  /**
   * 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;

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

626
627
628
629
630
631
632
  /**
   * 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.
   */
633
  bool archiveFileIdExists(rdbms::Conn &conn, const uint64_t archiveFileId) const;
634

635
636
637
638
639
640
641
  /**
   * 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.
642
   * @return True if the disk file identifier exists.
643
   */
644
  bool diskFileIdExists(rdbms::Conn &conn, const std::string &diskInstanceName, const std::string &diskFileId) const;
645

646
647
648
649
650
  /**
   * 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
651
   * file path belongs.
652
653
654
   * @param diskFilePath The disk file path.
   * @return True if the disk file path exists.
   */
655
  bool diskFilePathExists(rdbms::Conn &conn, const std::string &diskInstanceName, const std::string &diskFilePath)
656
657
    const;

658
659
660
661
662
663
664
665
666
  /**
   * 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.
   */
667
  bool diskFileUserExists(rdbms::Conn &conn, const std::string &diskInstanceName, const std::string &diskFileUser)
668
669
    const;

670
671
672
673
674
675
  /**
   * 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.
676
   * @param diskFileGroup The name of the disk file group.
677
678
   * @return True if the disk file group exists.
   */
679
  bool diskFileGroupExists(rdbms::Conn &conn, const std::string &diskInstanceName, const std::string &diskFileGroup)
680
681
    const;

682
683
684
685
686
687
688
689
690
691
692
  /**
   * 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.
   */
693
  bool archiveRouteExists(rdbms::Conn &conn, const std::string &diskInstanceName, const std::string &storageClassName,
694
695
    const uint64_t copyNb) const;

696
697
698
699
700
701
702
703
  /**
   * 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;

704
705
706
  /**
   * Returns true if the specified tape exists.
   *
707
   * @param conn The database connection.
708
709
710
   * @param vid The volume identifier of the tape.
   * @return True if the tape exists.
   */
711
  bool tapeExists(rdbms::Conn &conn, const std::string &vid) const;
712

713
714
715
716
717
718
719
  /**
   * 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.
   */
720
  std::list<common::dataStructures::Tape> getTapes(rdbms::Conn &conn, const TapeSearchCriteria &searchCriteria) const;
721

722
723
724
  /**
   * Returns true if the specified logical library exists.
   *
725
   * @param conn The database connection.
726
727
728
   * @param logicalLibraryName The name of the logical library.
   * @return True if the logical library exists.
   */
729
  bool logicalLibraryExists(rdbms::Conn &conn, const std::string &logicalLibraryName) const;
730
731
732
733

  /**
   * Returns true if the specified mount policy exists.
   *
734
   * @param conn The database connection.
735
736
737
   * @param mountPolicyName The name of the mount policy
   * @return True if the mount policy exists.
   */
738
  bool mountPolicyExists(rdbms::Conn &conn, const std::string &mountPolicyName) const;
739

740
  /**
741
   * Returns true if the specified requester mount-rule exists.
742
   *
743
744
745
746
747
   * @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.
748
   */
749
  bool requesterMountRuleExists(rdbms::Conn &conn, const std::string &diskInstanceName,
750
    const std::string &requesterName) const;
751
752

  /**
753
754
755
756
757
758
759
760
761
762
763
764
765
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
    * 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.
793
   *
794
795
   * @param user The fully qualified user, in other words the name of the disk
   * instance and the name of the group.
796
   * @return The mount policy or nullopt if one does not exists.
797
   */
798
  optional<common::dataStructures::MountPolicy> getCachedRequesterMountPolicy(const User &user) const;
799
800
801
802
803
804
805
806
807
808
809

  /**
   * 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;
810
811

  /**
812
   * Returns true if the specified requester-group mount-rule exists.
813
   *
814
   * @param conn The database connection.
815
816
817
818
819
   * @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.
820
   */
821
  bool requesterGroupMountRuleExists(rdbms::Conn &conn, const std::string &diskInstanceName,
822
    const std::string &requesterGroupName) const;
823

824
  /**
825
826
827
828
829
830
831
832
833
834
835
836
837
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
    * 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.
867
   *
868
869
   * @param group The fully qualified group, in other words the name of the disk
   * instance and the name of the group.
870
   * @return The cached mount policy or nullopt if one does not exists.
871
   */
872
  optional<common::dataStructures::MountPolicy> getCachedRequesterGroupMountPolicy(const Group &group) const;
873
874
875
876
877
878
879
880

  /**
   * 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.
881
   * @return The mount policy or nullopt if one does not exists.
882
   */
883
884
  optional<common::dataStructures::MountPolicy> getRequesterGroupMountPolicy(rdbms::Conn &conn, const Group &group)
    const;
885

886
887
888
889
890
  /**
   * Returns the specified tape log information from the specified database
   * result set.
   *
   * @param rset The result set.
891
   * @param driveColName The name of the database column that contains the name
892
893
894
895
   * of the tape drive.
   * @param timeColNAme The name of the database column that contains the time
   * stamp.
   */
896
  optional<common::dataStructures::TapeLog> getTapeLogFromRset(const rdbms::Rset &rset,
897
898
    const std::string &driveColName, const std::string &timeColName) const;

899
  /**
900
901
   * An RdbmsCatalogue specific method that inserts the specified row into the
   * ArchiveFile table.
902
   *
903
   * @param conn The database connection.
904
   * @param autocommitMode The autocommit mode of the SQL insert statement.
905
   * @param row The row to be inserted.
906
   */
907
  void insertArchiveFile(rdbms::Conn &conn, const rdbms::AutocommitMode autocommitMode,
908
    const ArchiveFileRow &row);
909

910
911
912
913
914
  /**
   * Creates the database schema.
   */
  void createDbSchema();

915
916
917
918
  /**
   * A fully qualified storage class, in other words the name of the disk
   * instance and the name of the storage class.
   */
919
  struct StorageClass {
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
    /**
     * 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.
     */
939
    StorageClass(const std::string &d, const std::string &s): diskInstanceName(d), storageClassName(s) {
940
941
942
943
944
945
946
947
948
    }

    /**
     * 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.
     */
949
    bool operator<(const StorageClass &rhs) const {
950
951
      return diskInstanceName < rhs.diskInstanceName || storageClassName < rhs.storageClassName;
    }
952
  }; // struct StorageClass
953

954
955
956
957
958
959
960
961
962
963
964
965
  /**
   * 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.
   */
966
  uint64_t getCachedExpectedNbArchiveRoutes(const StorageClass &storageClass) const;
967

968
969
970
971
972
973
  /**
   * 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.
   *
974
   * @param conn The database connection.
975
976
   * @param storageClass The fully qualified storage class, in other words the
   * name of the disk instance and the name of the storage class.
977
978
   * @return The expected number of archive routes.
   */
979
  uint64_t getExpectedNbArchiveRoutes(rdbms::Conn &conn, const StorageClass &storageClass) const;
980

981
  /**
982
   * Inserts the specified tape file into the Tape table.
983
   *
984
   * @param conn The database connection.
985
   * @param autocommitMode The autocommit mode of the SQL insert statement.
986
987
988
989
   * @param tapeFile The tape file.
   * @param archiveFileId The identifier of the archive file of which the tape
   * file is a copy.
   */
990
  void insertTapeFile(
991
992
    rdbms::Conn &conn,
    const rdbms::AutocommitMode autocommitMode,
993
994
    const common::dataStructures::TapeFile &tapeFile,
    const uint64_t archiveFileId);
995

996
997
998
  /**
   * Sets the last FSeq of the specified tape to the specified value.
   *
999
   * @param conn The database connection.
1000
   * @param vid The volume identifier of the tape.
For faster browsing, not all history is shown. View entire blame