1 #ifndef NETSCHEDULE_GC_REGISTRY__HPP
2 #define NETSCHEDULE_GC_REGISTRY__HPP
3 
4 /*  $Id: ns_gc_registry.hpp 476355 2015-08-18 14:43:35Z satskyse $
5  * ===========================================================================
6  *
7  *                            PUBLIC DOMAIN NOTICE
8  *               National Center for Biotechnology Information
9  *
10  *  This software/database is a "United States Government Work" under the
11  *  terms of the United States Copyright Act.  It was written as part of
12  *  the author's official duties as a United States Government employee and
13  *  thus cannot be copyrighted.  This software/database is freely available
14  *  to the public for use. The National Library of Medicine and the U.S.
15  *  Government have not placed any restriction on its use or reproduction.
16  *
17  *  Although all reasonable efforts have been taken to ensure the accuracy
18  *  and reliability of the software and data, the NLM and the U.S.
19  *  Government do not and cannot warrant the performance or results that
20  *  may be obtained by using this software or data. The NLM and the U.S.
21  *  Government disclaim all warranties, express or implied, including
22  *  warranties of performance, merchantability or fitness for any particular
23  *  purpose.
24  *
25  *  Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors:  Sergey Satskiy
30  *
31  * File Description:
32  *   Net schedule garbage collection registry
33  *
34  */
35 
36 /// @file ns_gc_registry.hpp
37 /// NetSchedule garbage collection registry
38 ///
39 /// @internal
40 
41 #include <corelib/ncbimtx.hpp>
42 
43 #include <map>
44 
45 #include "ns_precise_time.hpp"
46 #include "ns_types.hpp"
47 
48 BEGIN_NCBI_SCOPE
49 
50 
51 // The job information required for garbage collector
52 // to mark a job for deletion without reading the database
53 struct SJobGCInfo
54 {
55     unsigned int    m_AffinityID;   // The job affinity ID
56     unsigned int    m_GroupID;      // The job group ID
57     CNSPreciseTime  m_LifeTime;     // The last second the job considered alive
58     CNSPreciseTime  m_SubmitTime;   // Precise submit time
59     CNSPreciseTime  m_ReadVacantTime;
60 
SJobGCInfoSJobGCInfo61     SJobGCInfo() :
62         m_AffinityID(0), m_GroupID(0), m_LifeTime()
63     {}
64 
SJobGCInfoSJobGCInfo65     SJobGCInfo(unsigned int  aff, unsigned int  group,
66                const CNSPreciseTime &  life_time) :
67         m_AffinityID(aff), m_GroupID(group), m_LifeTime(life_time)
68     {
69         m_ReadVacantTime = kTimeNever;
70     }
71 };
72 
73 
74 
75 // The garbage collector registry. It holds all the information required for
76 // garbage collecting jobs without reading from the DB.
77 class CJobGCRegistry
78 {
79     public:
80         CJobGCRegistry();
81         ~CJobGCRegistry();
82 
83         void RegisterJob(unsigned int            job_id,
84                          const CNSPreciseTime &  submit_time,
85                          unsigned int            aff_id,
86                          unsigned int            group_id,
87                          const CNSPreciseTime &  life_time);
88         void ChangeAffinityAndGroup(unsigned int    job_id,
89                                     unsigned int    aff_id,
90                                     unsigned int    group_id);
91         bool DeleteIfTimedOut(unsigned int            job_id,       // in
92                               const CNSPreciseTime &  current_time, // in
93                               unsigned int *          aff_id,       // out: if deleted
94                               unsigned int *          group_id);    // out: if deleted
95         void UpdateLifetime(unsigned int            job_id,
96                             const CNSPreciseTime &  life_time);
97         void UpdateReadVacantTime(unsigned int            job_id,
98                                   const CNSPreciseTime &  read_vacant_time);
99         CNSPreciseTime  GetLifetime(unsigned int  job_id) const;
100         unsigned int    GetAffinityID(unsigned int  job_id) const;
101         unsigned int    GetGroupID(unsigned int  job_id) const;
102         CNSPreciseTime  GetPreciseSubmitTime(unsigned int  job_id) const;
103         CNSPreciseTime  GetPreciseReadVacantTime(unsigned int  job_id) const;
104         bool  IsOutdatedJob(unsigned int            job_id,
105                             ECommandGroup           cmd_group,
106                             const CNSPreciseTime &  timeout) const;
107         void Clear(void);
108 
109     private:
110         mutable CFastMutex              m_Lock;         // Lock for the operations
111         map<unsigned int, SJobGCInfo>   m_JobsAttrs;    // Jobs with attributes
112 
113     private:
114         CJobGCRegistry(const CJobGCRegistry &);
115         CJobGCRegistry & operator=(const CJobGCRegistry &);
116 };
117 
118 
119 
120 END_NCBI_SCOPE
121 
122 #endif /* NETSCHEDULE_GC_REGISTRY__HPP */
123 
124