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

Go to the SVN repository for this file.

1 /* $Id: rmtfilestatus.cpp 41935 2018-11-15 18:19:14Z rudnev $
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: Dmitry Rudnev
27  *
28  * File Description:
29  *
30  */
31 #include <ncbi_pch.hpp>
32 #include <corelib/ncbimisc.hpp>
33 #include <util/checksum.hpp>
34 
36 
37 
38 
40 
41 unique_ptr<CNetICacheClient> CRmtFileStatus::m_pCacheClient;
45 
47 {
49  return NULL;
50  }
51  if(!m_pCacheClient) {
52  try {
54  m_MaxCount = reg.GetInt("RmtFileStatusCache", "max_count", 0);
55  m_MaxCheckInterval = reg.GetInt("RmtFileStatusCache", "max_check_interval", 0);
56  if(m_MaxCount != 0 && m_MaxCheckInterval != 0) {
57  string cache_svc = reg.GetString("RmtFileStatusCache", "service", "IC_SV_TrackHubs_PROD");
58  string cache_name = reg.GetString("RmtFileStatusCache", "cache", "rmttrackstatus");
59  m_pCacheClient.reset(new CNetICacheClient(cache_svc, cache_name, "sviewer"));
60  // the following will provoke an exception if the service does not exist/is not available so we will not have to
61  // try to access it again
62  if(m_pCacheClient) {
63  int probe(0);
64  m_pCacheClient->Store("probe", 0, "", &probe, sizeof(probe));
65  }
66  } else {
68  return NULL;
69  }
70  } catch(...) {
72  return NULL;
73  }
74  }
75  return m_pCacheClient.get();
76 }
77 
78 bool CRmtFileStatus::x_Read(CNetICacheClient* cache, const string& sNCKey, int version, const string& sSubKey, void* data, size_t data_size)
79 {
80  if(!cache) {
81  return false;
82  }
83  try {
84  return cache->Read(sNCKey, version, sSubKey, data, data_size);
85  } catch(...) {
86  return false;
87  }
88 }
89 
90 void CRmtFileStatus::x_ResetSkipLimits(const string& sNCKey)
91 {
92  CNetICacheClient* cache(x_Init());
93  if(!cache) {
94  return;
95  }
96  TCounter count_zero(0);
97  cache->Store(sNCKey, 0, "counter", &count_zero, sizeof(count_zero));
98  TTimeStamp time_stamp(time(NULL));
99  cache->Store(sNCKey, 0, "time_stamp", &time_stamp, sizeof(time_stamp));
100  // initial check time interval is 10 min / 600 seconds
101  TTimeStamp time_interval(600);
102  cache->Store(sNCKey, 0, "time_interval", &time_interval, sizeof(time_interval));
103 }
104 
105 // gives suggested course of action for a given file
107 {
108  LOG_POST(Warning<< "CRmtFileStatus::Check(" << sKey << ")");
109  CNetICacheClient* cache(x_Init());
110 
111  // all checks are off if nothing is configured in .ini file or NC is not available
112  if(!cache) {
113  LOG_POST(Warning<< "CRmtFileStatus::Check(): " << " return forced Access");
115  }
116  string sNCKey(x_KeyToNCKey(sKey));
117  ESuggestedAction CurrentAccessibility;
118  bool isCurrentAccessibilityKnown{x_Read(cache, sNCKey, 0, "access_action", &CurrentAccessibility, sizeof(CurrentAccessibility))};
119 
120  if(CurrentAccessibility == ESuggestedAction_Access ||
121  !isCurrentAccessibilityKnown) {
122  LOG_POST(Warning<< "CRmtFileStatus::Check(): " << "return Access, CurrentAccessibility: " << CurrentAccessibility << ", isCurrentAccessibilityKnown: " << isCurrentAccessibilityKnown);
124  }
125 
126  TCounter CurrentCounter(0);
127  bool isCurrentCounterKnown{x_Read(cache, sNCKey, 0, "counter", &CurrentCounter, sizeof(CurrentCounter))};
128  if(!isCurrentCounterKnown) {
129  CurrentCounter = 0;
130  }
131  if(m_MaxCount != 0) {
132  if(++CurrentCounter > m_MaxCount) {
133  x_ResetSkipLimits(sNCKey);
134  LOG_POST(Warning<< "CRmtFileStatus::Check(): " << "return Access, CurrentCounter: " << CurrentCounter);
136  }
137  cache->Store(sNCKey, 0, "counter", &CurrentCounter, sizeof(CurrentCounter));
138  }
139  TTimeStamp TimeStamp(0);
140  bool isCurrentTimeStampKnown{x_Read(cache, sNCKey, 0, "time_stamp", &TimeStamp, sizeof(TimeStamp))};
141  TTimeStamp TimeInterval(0);
142  bool isCurrentTimeIntervalKnown{x_Read(cache, sNCKey, 0, "time_interval", &TimeInterval, sizeof(TimeInterval))};
143 
144  if(m_MaxCheckInterval != 0) {
145  if(!isCurrentTimeStampKnown || !isCurrentTimeIntervalKnown) {
146  x_ResetSkipLimits(sNCKey);
147  LOG_POST(Warning<< "CRmtFileStatus::Check(): " << "return Access, no time record");
149  }
150  if(time(NULL) - TimeStamp > TimeInterval) {
151  LOG_POST(Warning<< "CRmtFileStatus::Check(): " << "return Access, time stamp: " << TimeStamp << ", time interval: " << TimeInterval << ", actual time diff: " << (time(NULL) - TimeStamp));
152  TimeStamp = time(NULL);
153  cache->Store(sNCKey, 0, "time_stamp", &TimeStamp, sizeof(TimeStamp));
154  TimeInterval *= 2;
155  if(TimeInterval < m_MaxCheckInterval) {
156  cache->Store(sNCKey, 0, "time_interval", &TimeInterval, sizeof(TimeInterval));
157  }
159  }
160  }
161  LOG_POST(Warning<< "CRmtFileStatus::Check(): " << "return Skip, CurrentCounter: " << CurrentCounter << ", time stamp: " << TimeStamp << ", time interval: " << TimeInterval << ", actual time diff: " << (time(NULL) - TimeStamp));
162  return ESuggestedAction_Skip;
163 }
164 
165 // this must be called when accessibility of the file is known
166 void CRmtFileStatus::Set(const string& sKey,
167  CRmtFileStatus::ESuggestedAction KnownAccessibility)
168 {
169  CNetICacheClient* cache(x_Init());
170 
171  if(!cache) {
172  return;
173  }
174  string sNCKey(x_KeyToNCKey(sKey));
175  ESuggestedAction CurrentAccessibility;
176  bool isCurrentAccessibilityKnown{x_Read(cache, sNCKey, 0, "access_action", &CurrentAccessibility, sizeof(CurrentAccessibility))};
177  cache->Store(sNCKey, 0, "access_action", &KnownAccessibility, sizeof(KnownAccessibility));
178  if(KnownAccessibility == ESuggestedAction_Access ||
179  !isCurrentAccessibilityKnown ||
180  (KnownAccessibility == ESuggestedAction_Skip && CurrentAccessibility == ESuggestedAction_Access)) {
181  x_ResetSkipLimits(sNCKey);
182  }
183  LOG_POST(Warning<< "CRmtFileStatus::Set(" << sKey << ") was " << CurrentAccessibility << ", set to: " << KnownAccessibility);
184 }
185 
186 // convert any string to a key that can be an NC blob key
187 string CRmtFileStatus::x_KeyToNCKey(const string& sKey)
188 {
190  cs.AddLine(sKey);
191  string result(cs.GetResultHex());
192  return result;
193 }
194 
195 
197 
199 {
200  if(!m_Timeout) {
202  m_Timeout = reg.GetInt("RmtPipeline", "script_timeout", 160);
203  }
204  return m_Timeout;
205 }
206 
207 
209 
Checksum and hash calculation classes.
CChecksum – Checksum calculator.
Definition: checksum.hpp:302
static CNcbiApplication * Instance(void)
Singleton method.
Definition: ncbiapp.cpp:264
CNcbiRegistry –.
Definition: ncbireg.hpp:913
Client to NetCache server (implements ICache interface)
static string x_KeyToNCKey(const string &sKey)
time_t TTimeStamp
in seconds
static TTimeStamp m_MaxCheckInterval
@ ESuggestedAction_Access
try to access the remote file
@ ESuggestedAction_Skip
do not access the file
static unique_ptr< CNetICacheClient > m_pCacheClient
static bool x_Read(CNetICacheClient *cache, const string &sNCKey, int version, const string &sSubKey, void *data, size_t data_size)
static bool m_isStatusNotAvailable
static TCounter m_MaxCount
static CNetICacheClient * x_Init()
static void Set(const string &sKey, ESuggestedAction KnownAccessibility)
static void x_ResetSkipLimits(const string &sNCKey)
static ESuggestedAction Check(const string &sKey)
static time_t Get()
static time_t m_Timeout
char data[12]
Definition: iconv.c:80
const CNcbiRegistry & GetConfig(void) const
Get the application's cached configuration parameters (read-only).
#define NULL
Definition: ncbistd.hpp:225
void AddLine(const char *line, size_t len)
Definition: checksum.hpp:609
string GetResultHex(void) const
Return string with checksum/hash in hexadecimal form.
Definition: checksum.cpp:153
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
void Warning(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1191
virtual int GetInt(const string &section, const string &name, int default_value, TFlags flags=0, EErrAction err_action=eThrow) const
Get integer value of specified parameter name.
Definition: ncbireg.cpp:362
virtual string GetString(const string &section, const string &name, const string &default_value, TFlags flags=0) const
Get the parameter string value.
Definition: ncbireg.cpp:321
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
const string version
version string
Definition: variables.hpp:66
Miscellaneous common-use basic types and functionality.
else result
Definition: token2.c:20
Modified on Fri Sep 20 14:57:43 2024 by modify_doxy.py rev. 669887