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

Go to the SVN repository for this file.

1 /* $Id: ns_scope.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 scopes
30  *
31  */
32 
33 
34 #include <ncbi_pch.hpp>
35 
36 #include "ns_scope.hpp"
37 #include "ns_queue.hpp"
38 
39 
41 
42 
44 {}
45 
46 
48 {
49  Clear();
50 }
51 
52 
53 size_t CNSScopeRegistry::size(void) const
54 {
55  CMutexGuard guard(m_Lock);
56  return m_ScopeToJobs.size();
57 }
58 
59 
60 bool CNSScopeRegistry::CanAccept(const string & scope,
61  size_t max_records) const
62 {
63  CMutexGuard guard(m_Lock);
64  if (m_ScopeToJobs.size() < max_records)
65  return true;
66 
67  if (scope == kNoScopeOnly)
68  return true;
69 
70  return m_ScopeToJobs.find(scope) != m_ScopeToJobs.end();
71 }
72 
73 
74 TNSBitVector CNSScopeRegistry::GetJobs(const string & scope) const
75 {
77 
78  CMutexGuard guard(m_Lock);
79  scope_it = m_ScopeToJobs.find(scope);
80  if (scope_it == m_ScopeToJobs.end())
81  return kEmptyBitVector;
82  return scope_it->second;
83 }
84 
85 
87 {
88  CMutexGuard guard(m_Lock);
89  return m_AllScopedJobs;
90 }
91 
92 
93 void CNSScopeRegistry::AddJob(const string & scope,
94  unsigned int job_id)
95 {
96  if (job_id == 0 || scope.empty() || scope == kNoScopeOnly)
97  return;
98 
100 
101  CMutexGuard guard(m_Lock);
102  if (m_AllScopedJobs[job_id])
103  return;
104 
105  m_AllScopedJobs[job_id] = true;
106  scope_it = m_ScopeToJobs.find(scope);
107  if (scope_it == m_ScopeToJobs.end()) {
108  TNSBitVector scope_jobs;
109  scope_jobs[job_id] = true;
110  m_ScopeToJobs[scope] = scope_jobs;
111  } else {
112  scope_it->second[job_id] = true;
113  }
114 }
115 
116 
117 void CNSScopeRegistry::AddJobs(const string & scope,
118  unsigned int first_job_id,
119  unsigned int count)
120 {
121  if (first_job_id == 0 || count == 0 ||
122  scope.empty() || scope == kNoScopeOnly )
123  return;
124 
125  TScopeToJobsMap::iterator scope_it;
126 
127  CMutexGuard guard(m_Lock);
128  scope_it = m_ScopeToJobs.find(scope);
129  if (scope_it == m_ScopeToJobs.end()) {
130  TNSBitVector scope_jobs;
131  scope_jobs.set_range(first_job_id, first_job_id + count - 1);
132  m_ScopeToJobs[scope] = scope_jobs;
133  } else {
134  scope_it->second.set_range(first_job_id, first_job_id + count - 1);
135  }
136 }
137 
138 
139 void CNSScopeRegistry::RemoveJob(unsigned int job_id)
140 {
141  CMutexGuard guard(m_Lock);
142  if (!m_AllScopedJobs[job_id])
143  return;
144 
145  m_AllScopedJobs[job_id] = false;
147  k != m_ScopeToJobs.end(); ++k) {
148  if (k->second[job_id]) {
149  k->second[job_id] = false;
150  break;
151  }
152  }
153 }
154 
155 
156 string CNSScopeRegistry::Print(const CQueue * queue,
157  size_t batch_size,
158  bool verbose) const
159 {
160  string result;
161  deque<string> scope_names = GetScopeNames();
162  deque<string> batch;
163 
164  while (!scope_names.empty()) {
165  batch.push_back(scope_names.front());
166  scope_names.pop_front();
167 
168  if (batch.size() >= batch_size) {
169  result += x_PrintSelected(batch, queue, verbose);
170  batch.clear();
171  }
172  }
173 
174  if (!batch.empty())
175  result += x_PrintSelected(batch, queue, verbose);
176  return result;
177 }
178 
179 
180 string
181 CNSScopeRegistry::x_PrintSelected(const deque<string> & batch,
182  const CQueue * queue,
183  bool verbose) const
184 {
185  string buffer;
186  deque<string>::const_iterator k = batch.begin();
188 
189  CMutexGuard guard(m_Lock);
190  for ( ; k != batch.end(); ++k ) {
191  scope_it = m_ScopeToJobs.find(*k);
192  if (scope_it != m_ScopeToJobs.end())
193  buffer += x_PrintOne(*k, scope_it->second, queue, verbose);
194  }
195 
196  return buffer;
197 }
198 
199 
200 string
201 CNSScopeRegistry::x_PrintOne(const string & scope_name,
202  const TNSBitVector & jobs,
203  const CQueue * queue,
204  bool verbose) const
205 {
206  string buffer;
207 
208  buffer += "OK:SCOPE: '" +
209  NStr::PrintableString(scope_name) + "'\n"
210  "OK: NUMBER OF JOBS: " +
211  to_string(jobs.count()) + "\n";
212 
213  if (verbose) {
214  if (jobs.any()) {
215  buffer += "OK: JOBS:\n";
216 
217  TNSBitVector::enumerator en(jobs.first());
218  for ( ; en.valid(); ++en) {
219  unsigned int job_id = *en;
220  TJobStatus status = queue->GetJobStatus(job_id);
221  buffer += "OK: " + queue->MakeJobKey(job_id) + " " +
222  CNetScheduleAPI::StatusToString(status) + "\n";
223  }
224  }
225  else
226  buffer += "OK: JOBS: NONE\n";
227  }
228 
229  return buffer;
230 }
231 
232 
234 {
235  CMutexGuard guard(m_Lock);
238 }
239 
240 
241 deque<string> CNSScopeRegistry::GetScopeNames(void) const
242 {
243  deque<string> result;
244 
245  CMutexGuard guard(m_Lock);
247  k != m_ScopeToJobs.end(); ++k) {
248  result.push_back(k->first);
249  }
250  return result;
251 }
252 
253 
254 string CNSScopeRegistry::GetJobScope(unsigned int job_id) const
255 {
256  CMutexGuard guard(m_Lock);
257  if (m_AllScopedJobs[job_id] == false)
258  return kEmptyStr;
259 
261  k != m_ScopeToJobs.end(); ++k) {
262  if (k->second[job_id])
263  return k->first;
264  }
265  return kEmptyStr;
266 }
267 
268 
269 unsigned int CNSScopeRegistry::CollectGarbage(unsigned int max_to_del)
270 {
271  unsigned int del_count = 0;
273  deque<string> to_be_deleted;
274  CMutexGuard guard(m_Lock);
275 
276  for (it = m_ScopeToJobs.begin(); it != m_ScopeToJobs.end(); ++it) {
277  if (!it->second.any()) {
278  to_be_deleted.push_back(it->first);
279  ++del_count;
280  if (del_count >= max_to_del)
281  break;
282  }
283  }
284 
285  for (deque<string>::const_iterator k = to_be_deleted.begin();
286  k != to_be_deleted.end(); ++k)
287  m_ScopeToJobs.erase(*k);
288  return to_be_deleted.size();
289 }
290 
291 
293 {
294  unsigned int count = 0;
296  CMutexGuard guard(m_Lock);
297 
298  for (it = m_ScopeToJobs.begin(); it != m_ScopeToJobs.end(); ++it)
299  if (!it->second.any())
300  ++count;
301 
302  return count;
303 }
304 
305 
307 
unsigned int CheckRemoveCandidates(void)
Definition: ns_scope.cpp:292
TNSBitVector GetAllJobsInScopes(void) const
Definition: ns_scope.cpp:86
deque< string > GetScopeNames(void) const
Definition: ns_scope.cpp:241
void Clear(void)
Definition: ns_scope.cpp:233
void AddJobs(const string &scope, unsigned int first_job_id, unsigned int count)
Definition: ns_scope.cpp:117
string x_PrintOne(const string &scope_name, const TNSBitVector &jobs, const CQueue *queue, bool verbose) const
Definition: ns_scope.cpp:201
string Print(const CQueue *queue, size_t batch_size, bool verbose) const
Definition: ns_scope.cpp:156
bool CanAccept(const string &scope, size_t max_records) const
Definition: ns_scope.cpp:60
TNSBitVector GetJobs(const string &scope) const
Definition: ns_scope.cpp:74
string x_PrintSelected(const deque< string > &batch, const CQueue *queue, bool verbose) const
Definition: ns_scope.cpp:181
unsigned int CollectGarbage(unsigned int max_to_del)
Definition: ns_scope.cpp:269
TNSBitVector m_AllScopedJobs
Definition: ns_scope.hpp:94
string GetJobScope(unsigned int job_id) const
Definition: ns_scope.cpp:254
size_t size(void) const
Definition: ns_scope.cpp:53
void RemoveJob(unsigned int job_id)
Definition: ns_scope.cpp:139
TScopeToJobsMap m_ScopeToJobs
Definition: ns_scope.hpp:91
void AddJob(const string &scope, unsigned int job_id)
Definition: ns_scope.cpp:93
string MakeJobKey(unsigned int job_id) const
Definition: ns_queue.cpp:4055
TJobStatus GetJobStatus(unsigned job_id) const
Definition: ns_queue.cpp:1946
Constant iterator designed to enumerate "ON" bits.
Definition: bm.h:603
bool valid() const noexcept
Checks if iterator is still valid.
Definition: bm.h:283
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
bool any() const noexcept
Returns true if any bits in this bitset are set, and otherwise returns false.
Definition: bm.h:2451
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:1871
bvector< Alloc > & set_range(size_type left, size_type right, bool value=true)
Sets all bits in the specified closed interval [left,right] Interval must be inside the bvector's siz...
Definition: bm.h:2368
void clear(const size_type *ids, size_type ids_size, bm::sort_order so=bm::BM_UNKNOWN)
clear list of bits in this bitset
Definition: bm.h:4149
size_type count() const noexcept
population count (count of ON bits)
Definition: bm.h:2401
void erase(iterator pos)
Definition: map.hpp:167
size_type size() const
Definition: map.hpp:148
const_iterator begin() const
Definition: map.hpp:151
const_iterator end() const
Definition: map.hpp:152
void clear()
Definition: map.hpp:169
const_iterator find(const key_type &key) const
Definition: map.hpp:153
EJobStatus
Job status codes.
static string StatusToString(EJobStatus status)
Printable status type.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static string PrintableString(const CTempString str, TPrintableMode mode=fNewLine_Quote|fNonAscii_Passthru)
Get a printable version of the specified string.
Definition: ncbistr.cpp:3944
#define kEmptyStr
Definition: ncbistr.hpp:123
const string kNoScopeOnly
Definition: ns_scope.hpp:55
const TNSBitVector kEmptyBitVector
Definition: ns_types.hpp:105
#define count
static uint8_t * buffer
Definition: pcre2test.c:1016
true_type verbose
Definition: processing.cpp:878
else result
Definition: token2.c:20
Modified on Fri Sep 20 14:58:05 2024 by modify_doxy.py rev. 669887