NCBI C++ ToolKit
ns_gc_registry.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ns_gc_registry.cpp 89004 2020-02-11 19:47:28Z satskyse $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Sergey Satskiy
27  *
28  * File Description:
29  * Net schedule garbage collection registry
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbi_system.hpp>
35 #include <corelib/ncbistd.hpp>
37 
38 #include "ns_gc_registry.hpp"
39 
40 
42 
43 
45 {}
46 
48 {}
49 
50 
51 // Registers the job in the GC registry
52 void CJobGCRegistry::RegisterJob(unsigned int job_id,
53  const CNSPreciseTime & submit_time,
54  unsigned int aff_id,
55  unsigned int group_id,
56  const CNSPreciseTime & life_time)
57 {
58  SJobGCInfo job_attr(aff_id, group_id, life_time);
59  job_attr.m_SubmitTime = submit_time;
60 
61  pair<unsigned int, SJobGCInfo> item(job_id, job_attr);
62 
63  CFastMutexGuard guard(m_Lock);
64  m_JobsAttrs.insert(item);
65 }
66 
67 
68 void CJobGCRegistry::ChangeAffinityAndGroup(unsigned int job_id,
69  unsigned int aff_id,
70  unsigned int group_id)
71 {
72  CFastMutexGuard guard(m_Lock);
74 
75  if (attrs != m_JobsAttrs.end()) {
76  attrs->second.m_AffinityID = aff_id;
77  attrs->second.m_GroupID = group_id;
78  }
79 }
80 
81 
82 // Returns true if the record has been deleted, i.e. the has to be marked
83 // for deletion. The aff_id and group_id are filled only if the job is
84 // deleted
85 bool CJobGCRegistry::DeleteIfTimedOut(unsigned int job_id,
86  const CNSPreciseTime & current_time,
87  unsigned int * aff_id,
88  unsigned int * group_id)
89 {
90  CFastMutexGuard guard(m_Lock);
92 
93  if (attrs == m_JobsAttrs.end())
94  NCBI_THROW(CNetScheduleException, eInternalError,
95  "Testing life time of non-registered job (ID: " +
96  to_string(job_id) + ")");
97 
98  if (current_time < attrs->second.m_LifeTime)
99  return false;
100 
101  // The record should be deleted
102  *aff_id = attrs->second.m_AffinityID;
103  *group_id = attrs->second.m_GroupID;
104  m_JobsAttrs.erase(attrs);
105  return true;
106 }
107 
108 
109 // Updates the job life time
110 void CJobGCRegistry::UpdateLifetime(unsigned int job_id,
111  const CNSPreciseTime & life_time)
112 {
113  CFastMutexGuard guard(m_Lock);
115 
116  if (attrs == m_JobsAttrs.end())
117  NCBI_THROW(CNetScheduleException, eInternalError,
118  "Updating life time of non-registered job (ID: " +
119  to_string(job_id) + ")");
120 
121  attrs->second.m_LifeTime = life_time;
122 }
123 
124 
125 // Updates the time when a job became available for reading.
126 // This time must be updated once only
127 void
129  const CNSPreciseTime & read_vacant_time)
130 {
131  CFastMutexGuard guard(m_Lock);
133 
134  if (attrs == m_JobsAttrs.end())
135  NCBI_THROW(CNetScheduleException, eInternalError,
136  "Updating read vacant time of non-registered job (ID: " +
137  to_string(job_id) + ")");
138 
139  if (attrs->second.m_ReadVacantTime == kTimeNever)
140  attrs->second.m_ReadVacantTime = read_vacant_time;
141 }
142 
143 
144 CNSPreciseTime CJobGCRegistry::GetLifetime(unsigned int job_id) const
145 {
146  CFastMutexGuard guard(m_Lock);
147  map<unsigned int,
148  SJobGCInfo>::const_iterator attrs = m_JobsAttrs.find(job_id);
149 
150  if (attrs == m_JobsAttrs.end())
151  NCBI_THROW(CNetScheduleException, eInternalError,
152  "Retreiving life time of non-registered job (ID: " +
153  to_string(job_id) + ")");
154 
155  return attrs->second.m_LifeTime;
156 }
157 
158 
159 unsigned int CJobGCRegistry::GetAffinityID(unsigned int job_id) const
160 {
161  CFastMutexGuard guard(m_Lock);
162  map<unsigned int,
163  SJobGCInfo>::const_iterator attrs = m_JobsAttrs.find(job_id);
164 
165  if (attrs == m_JobsAttrs.end())
166  return 0;
167 
168  return attrs->second.m_AffinityID;
169 }
170 
171 
172 unsigned int CJobGCRegistry::GetGroupID(unsigned int job_id) const
173 {
174  CFastMutexGuard guard(m_Lock);
175  map<unsigned int,
176  SJobGCInfo>::const_iterator attrs = m_JobsAttrs.find(job_id);
177 
178  if (attrs == m_JobsAttrs.end())
179  return 0;
180 
181  return attrs->second.m_GroupID;
182 }
183 
184 
186 CJobGCRegistry::GetPreciseSubmitTime(unsigned int job_id) const
187 {
188  CFastMutexGuard guard(m_Lock);
189  map<unsigned int,
190  SJobGCInfo>::const_iterator attrs = m_JobsAttrs.find(job_id);
191 
192  if (attrs == m_JobsAttrs.end())
193  return kTimeZero;
194 
195  return attrs->second.m_SubmitTime;
196 }
197 
198 
201 {
202  CFastMutexGuard guard(m_Lock);
203  map<unsigned int,
204  SJobGCInfo>::const_iterator attrs = m_JobsAttrs.find(job_id);
205 
206  if (attrs == m_JobsAttrs.end())
207  return kTimeNever;
208 
209  return attrs->second.m_ReadVacantTime;
210 }
211 
212 
213 bool
214 CJobGCRegistry::IsOutdatedJob(unsigned int job_id,
215  ECommandGroup cmd_group,
216  const CNSPreciseTime & timeout) const
217 {
218  CNSPreciseTime available_time;
219 
220  if (cmd_group == eGet)
221  available_time = GetPreciseSubmitTime(job_id);
222  else
223  available_time = GetPreciseReadVacantTime(job_id);
224 
225  return (available_time + timeout) < CNSPreciseTime::Current();
226 }
227 
228 
230 {
231  CFastMutexGuard guard(m_Lock);
232  m_JobsAttrs.clear();
233 }
234 
235 
237 
CNSPreciseTime GetLifetime(unsigned int job_id) const
bool DeleteIfTimedOut(unsigned int job_id, const CNSPreciseTime &current_time, unsigned int *aff_id, unsigned int *group_id)
void UpdateReadVacantTime(unsigned int job_id, const CNSPreciseTime &read_vacant_time)
void RegisterJob(unsigned int job_id, const CNSPreciseTime &submit_time, unsigned int aff_id, unsigned int group_id, const CNSPreciseTime &life_time)
CNSPreciseTime GetPreciseReadVacantTime(unsigned int job_id) const
void UpdateLifetime(unsigned int job_id, const CNSPreciseTime &life_time)
unsigned int GetAffinityID(unsigned int job_id) const
CNSPreciseTime GetPreciseSubmitTime(unsigned int job_id) const
map< unsigned int, SJobGCInfo > m_JobsAttrs
unsigned int GetGroupID(unsigned int job_id) const
bool IsOutdatedJob(unsigned int job_id, ECommandGroup cmd_group, const CNSPreciseTime &timeout) const
void ChangeAffinityAndGroup(unsigned int job_id, unsigned int aff_id, unsigned int group_id)
static CNSPreciseTime Current(void)
NetSchedule internal exception.
void erase(iterator pos)
Definition: map.hpp:167
const_iterator end() const
Definition: map.hpp:152
iterator_bool insert(const value_type &val)
Definition: map.hpp:165
void clear()
Definition: map.hpp:169
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Definition: map.hpp:338
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
NetSchedule client specs.
NetSchedule garbage collection registry.
const CNSPreciseTime kTimeZero
const CNSPreciseTime kTimeNever
ECommandGroup
Definition: ns_types.hpp:54
@ eGet
Definition: ns_types.hpp:55
CNSPreciseTime m_SubmitTime
Modified on Fri Sep 20 14:58:01 2024 by modify_doxy.py rev. 669887