#ifndef NETSCHEDULE_QUEUE_DATABASE__HPP
#define NETSCHEDULE_QUEUE_DATABASE__HPP


/*  $Id: queue_database.hpp 84641 2018-11-26 13:25:46Z satskyse $
 * ===========================================================================
 *
 *                            PUBLIC DOMAIN NOTICE
 *               National Center for Biotechnology Information
 *
 *  This software/database is a "United States Government Work" under the
 *  terms of the United States Copyright Act.  It was written as part of
 *  the author's official duties as a United States Government employee and
 *  thus cannot be copyrighted.  This software/database is freely available
 *  to the public for use. The National Library of Medicine and the U.S.
 *  Government have not placed any restriction on its use or reproduction.
 *
 *  Although all reasonable efforts have been taken to ensure the accuracy
 *  and reliability of the software and data, the NLM and the U.S.
 *  Government do not and cannot warrant the performance or results that
 *  may be obtained by using this software or data. The NLM and the U.S.
 *  Government disclaim all warranties, express or implied, including
 *  warranties of performance, merchantability or fitness for any particular
 *  purpose.
 *
 *  Please cite the author in any work or product based on this material.
 *
 * ===========================================================================
 *
 * Authors:  Anatoliy Kuznetsov, Victor Joukov
 *
 * File Description:
 *   Top level queue database (Thread-Safe, synchronized).
 *
 */


#include <corelib/ncbimtx.hpp>
#include <corelib/ncbicntr.hpp>

#include <utility>

#include <connect/services/netschedule_api.hpp>
#include <connect/services/json_over_uttp.hpp>
#include "ns_util.hpp"
#include "job_status.hpp"
#include "queue_clean_thread.hpp"
#include "ns_notifications.hpp"
#include "ns_queue.hpp"
#include "queue_vc.hpp"
#include "background_host.hpp"
#include "ns_service_thread.hpp"
#include "ns_precise_time.hpp"

BEGIN_NCBI_SCOPE

class CNetScheduleServer;


// Holds parameters together with a queue instance
typedef map<string,
            pair<SQueueParameters, CRef<CQueue> >,
            PNocase >   TQueueInfo;


// Top level queue database. (Thread-Safe, synchronized.)
class CQueueDataBase
{
public:
    CQueueDataBase(CNetScheduleServer *  server,
                   const string &  path,
                   unsigned int  max_queues,
                   bool  diskless,
                   bool  reinit);
    ~CQueueDataBase();

    // Read queue information from registry and configure queues
    // accordingly.
    // returns minimum run timeout, necessary for watcher thread
    time_t  Configure(const IRegistry &  reg,
                      CJsonNode &        diff);

    // Count Pending and Running jobs in all queues
    unsigned int  CountActiveJobs(void) const;
    unsigned int  CountAllJobs(void) const;
    bool  AnyJobs(void) const;

    CRef<CQueue> OpenQueue(const string &  name);

    void CreateDynamicQueue(const CNSClientId &  client,
                            const string &  qname, const string &  qclass,
                            const string &  description = "");
    void DeleteDynamicQueue(const CNSClientId &  client,
                            const string &  qname);
    SQueueParameters QueueInfo(const string &  qname) const;
    string GetQueueNames(const string &  sep) const;

    void Close(void);
    bool QueueExists(const string &  qname) const;

    // Remove old jobs
    void Purge(void);
    void StopPurge(void);
    void RunPurgeThread(void);
    void StopPurgeThread(void);

    // Collect garbage from affinities
    void PurgeAffinities(void);
    void PurgeGroups(void);
    void StaleWNodes(void);
    void PurgeBlacklistedJobs(void);
    void PurgeClientRegistry(void);

    // Notify all listeners
    void NotifyListeners(void);
    void RunNotifThread(void);
    void StopNotifThread(void);
    void WakeupNotifThread(void);
    CNSPreciseTime SendExactNotifications(void);

    // Print statistics
    void PrintStatistics(size_t &  aff_count);
    void PrintJobCounters(void);
    void RunServiceThread(void);
    void StopServiceThread(void);

    void CheckExecutionTimeout(bool  logging);
    void RunExecutionWatcherThread(const CNSPreciseTime &  run_delay);
    void StopExecutionWatcherThread(void);

    string PrintTransitionCounters(void);
    string PrintJobsStat(const CNSClientId &  client);
    string GetQueueClassesInfo(void) const;
    string GetQueueClassesConfig(void) const;
    string GetQueueInfo(void) const;
    string GetQueueConfig(void) const;
    string GetLinkedSectionConfig(void) const;

    map<string, string>  GetLinkedSection(const string &  section_name) const;

    // map: queue name -> pause state (integer, CQueue::EPauseState)
    // the only paused queues are reported
    map<string, int> GetPauseQueues(void) const;
    vector<string> GetRefuseSubmitQueues(void) const;
    string GetDataPath(void) const
    { return m_DataPath; }

private:
    // No copy
    CQueueDataBase(const CQueueDataBase&);
    CQueueDataBase& operator=(const CQueueDataBase&);

protected:
    // get next job id (counter increment)
    unsigned int  GetNextId();

    // Returns first id for the batch
    unsigned int  GetNextIdBatch(unsigned int  count);

private:
    void x_Open(bool  reinit);
    void x_CreateAndMountQueue(const string &            qname,
                               const SQueueParameters &  params);

    unsigned x_PurgeUnconditional(void);
    void     x_OptimizeStatusMatrix(const CNSPreciseTime &  current_time);
    bool     x_CheckStopPurge(void);
    SQueueParameters x_SingleQueueInfo(TQueueInfo::const_iterator  found) const;

    CBackgroundHost &    m_Host;
    string               m_DataPath;
    string               m_DumpPath;
    unsigned int         m_MaxQueues;
    bool                 m_Diskless;

    mutable CFastMutex   m_ConfigureLock;

    // Effective queue classes
    TQueueParams         m_QueueClasses;
    // Effective queues
    TQueueInfo           m_Queues;

    bool                 m_StopPurge;         // Purge stop flag
    CFastMutex           m_PurgeLock;
    unsigned int         m_FreeStatusMemCnt;  // Free memory counter
    time_t               m_LastFreeMem;       // time of the last memory opt

    CRef<CJobQueueCleanerThread>            m_PurgeThread;
    CRef<CServiceThread>                    m_ServiceThread;
    CRef<CGetJobNotificationThread>         m_NotifThread;
    CRef<CJobQueueExecutionWatcherThread>   m_ExeWatchThread;

    CNetScheduleServer *                    m_Server;

private:
    // Last scan attributes
    string              m_PurgeQueue;           // The queue name
    size_t              m_PurgeStatusIndex;     // Scanned status index
    unsigned int        m_PurgeJobScanned;      // Scanned job ID within status

    // Linked sections support
    mutable CFastMutex                      m_LinkedSectionsGuard;
    // Section name -> section values
    map< string, map< string, string > >    m_LinkedSections;

    bool x_PurgeQueue(CQueue &                queue,
                      size_t                  status_to_start,
                      size_t                  status_to_end,
                      unsigned int            start_job_id,
                      unsigned int            end_job_id,
                      size_t                  max_scanned,
                      size_t                  max_mark_deleted,
                      const CNSPreciseTime &  current_time,
                      size_t &                total_scanned,
                      size_t &                total_mark_deleted);
    void  x_DeleteQueuesAndClasses(void);
    CRef<CQueue>  x_GetLastPurged(void);
    CRef<CQueue>  x_GetFirst(void);
    CRef<CQueue>  x_GetNext(const string &  current_name);

    // Crash detect support:
    // - upon start the server creates CRASH_FLAG file
    // - when gracefully finished the file is deleted
    // - at the start it is checked if the file is there. If it is then
    //   it means the server crashed
    void  x_CreateCrashFlagFile(void);
    bool  x_DoesCrashFlagFileExist(void) const;
    void  x_RemoveCrashFlagFile(void);

    // Dump problem detect support:
    // - upon dtart the server creates DUMP_ERROR_FLAG file
    // - if all the NS info was dumped successfully the file is deleted
    // - at the start it is checked if the file is there. If it is then
    //   it means the previous instance had problems dumping something
    void  x_CreateDumpErrorFlagFile(void);
    bool  x_DoesDumpErrorFlagFileExist(void) const;
    void  x_RemoveDumpErrorFlagFile(void);


    bool  x_ConfigureQueueClasses(const TQueueParams &  classes_from_ini,
                                  CJsonNode &           diff);
    bool  x_ConfigureQueues(const TQueueParams &  queues_from_ini,
                            CJsonNode &           diff);

    TQueueParams  x_ReadIniFileQueueClassDescriptions(const IRegistry &   reg);
    TQueueParams  x_ReadIniFileQueueDescriptions(const IRegistry &     reg,
                                                 const TQueueParams &  classes);
    void  x_ReadLinkedSections(const IRegistry &  reg,
                               CJsonNode &        diff);
    CJsonNode  x_DetectChangesInLinkedSection(
                                const map<string, string> &  old_values,
                                const map<string, string> &  new_values);

    void  x_ValidateConfiguration(const TQueueParams &  queues_from_ini) const;
    unsigned int
    x_CountQueuesToAdd(const TQueueParams &  queues_from_ini) const;

    CRef<CQueue>  x_GetQueueAt(unsigned int  index);

    void x_Dump(void);
    void x_DumpQueueOrClass(FILE *  f,
                            const string &  qname, const string &  qclass,
                            bool  is_queue,
                            const SQueueParameters &  params);
    void x_DumpLinkedSection(FILE *  f, const string &  sname,
                             const map<string, string> &  values);
    void x_RemoveDump(void);
    void x_RemoveDataFiles(void);
    void x_CreateStorageVersionFile(void);

    bool x_CheckOpenPreconditions(bool  reinit);
    void x_ReadDumpQueueDesrc(set<string, PNocase> &  dump_static_queues,
                              map<string, string,
                                  PNocase> &  dump_dynamic_queues,
                              TQueueParams &  dump_queue_classes);
    set<string, PNocase> x_GetConfigQueues(void);
    void x_AppendDumpLinkedSections(void);
    CNSPreciseTime CalculateRuntimePrecision(void) const;
    void x_BackupDump(void);
    void x_CreateSpaceReserveFile(void);
    bool x_RemoveSpaceReserveFile(void);
    string x_GetDumpSpaceFileName(void) const;
    void x_RestorePauseState(const map<string, int> &  paused_queues);
    void x_RestoreRefuseSubmitState(const vector<string> &  refuse_submit_queues);
}; // CQueueDataBase


END_NCBI_SCOPE

#endif /* NETSCHEDULE_QUEUE_DATABASE__HPP */
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291