1 #ifndef CONNECT_SERVICES__NETSERVICE_PARAMS_HPP
2 #define CONNECT_SERVICES__NETSERVICE_PARAMS_HPP
3 
4 /*  $Id: netservice_params.hpp 620111 2020-11-16 19:50:00Z sadyrovr $
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  * File Description:
30  *   Declarations of the configuration parameters for connect/services.
31  *
32  * Authors:
33  *   Dmitry Kazimirov
34  *
35  */
36 
37 #include <connect/ncbi_types.h>
38 
39 #include <corelib/ncbi_param.hpp>
40 #include <corelib/ncbi_config.hpp>
41 
42 #include <memory>
43 
44 // The number of connection attemps
45 #define CONNECTION_MAX_RETRIES 4
46 
47 // Delay between two successive connection attempts in seconds.
48 #define RETRY_DELAY_DEFAULT 1.0
49 
50 #define COMMIT_JOB_INTERVAL_DEFAULT 2
51 
52 BEGIN_NCBI_SCOPE
53 
54 NCBI_PARAM_DECL(unsigned int, netservice_api, connection_max_retries);
55 typedef NCBI_PARAM_TYPE(netservice_api, connection_max_retries)
56     TServConn_ConnMaxRetries;
57 
58 NCBI_PARAM_DECL(double, netservice_api, retry_delay);
59 typedef NCBI_PARAM_TYPE(netservice_api, retry_delay)
60     TServConn_RetryDelay;
61 
62 NCBI_PARAM_DECL(bool, netservice_api, use_linger2);
63 typedef NCBI_PARAM_TYPE(netservice_api, use_linger2)
64     TServConn_UserLinger2;
65 
66 NCBI_PARAM_DECL(int, netservice_api, max_find_lbname_retries);
67 typedef NCBI_PARAM_TYPE(netservice_api, max_find_lbname_retries)
68     TServConn_MaxFineLBNameRetries;
69 
70 NCBI_PARAM_DECL(string, netcache_api, fallback_server);
71 typedef NCBI_PARAM_TYPE(netcache_api, fallback_server)
72     TCGI_NetCacheFallbackServer;
73 
74 NCBI_PARAM_DECL(int, netservice_api, max_connection_pool_size);
75 typedef NCBI_PARAM_TYPE(netservice_api, max_connection_pool_size)
76     TServConn_MaxConnPoolSize;
77 
78 NCBI_PARAM_DECL(bool, netservice_api, connection_data_logging);
79 typedef NCBI_PARAM_TYPE(netservice_api, connection_data_logging)
80     TServConn_ConnDataLogging;
81 
82 NCBI_PARAM_DECL(bool, netservice_api, warn_on_unexpected_reply);
83 typedef NCBI_PARAM_TYPE(netservice_api, warn_on_unexpected_reply) TServConn_WarnOnUnexpectedReply;
84 
85 // Worker node-specific parameters
86 
87 // Determine how long the worker node should wait for the
88 // configured NetSchedule servers to appear before quitting.
89 // When none of the servers are accessible, the worker node
90 // will keep trying to connect until it succeeds, or the specified
91 // timeout elapses.
92 NCBI_PARAM_DECL(unsigned, server, max_wait_for_servers);
93 typedef NCBI_PARAM_TYPE(server, max_wait_for_servers)
94     TWorkerNode_MaxWaitForServers;
95 
96 NCBI_PARAM_DECL(bool, server, stop_on_job_errors);
97 typedef NCBI_PARAM_TYPE(server, stop_on_job_errors)
98     TWorkerNode_StopOnJobErrors;
99 
100 NCBI_PARAM_DECL(bool, server, allow_implicit_job_return);
101 typedef NCBI_PARAM_TYPE(server, allow_implicit_job_return)
102     TWorkerNode_AllowImplicitJobReturn;
103 
104 
105 class NCBI_XCONNECT_EXPORT CConfigRegistry : public IRegistry
106 {
107 public:
108     CConfigRegistry(CConfig* config = nullptr);
109     void Reset(CConfig* config = nullptr);
110 
111 private:
112     bool x_Empty(TFlags flags) const override;
113     const string& x_Get(const string& section, const string& name, TFlags flags) const override;
114     bool x_HasEntry(const string& section, const string& name, TFlags flags) const override;
115     const string& x_GetComment(const string& section, const string& name, TFlags flags) const override;
116     void x_Enumerate(const string& section, list<string>& entries, TFlags flags) const override;
117 
118     CConfig* GetSubConfig(const string& section) const;
119 
120     CConfig* m_Config;
121     mutable map<string, unique_ptr<CConfig>> m_SubConfigs;
122 };
123 
124 struct SRegSynonyms : vector<CTempString>
125 {
SRegSynonymsSRegSynonyms126     SRegSynonyms(const char*   s) { Append(s); }
SRegSynonymsSRegSynonyms127     SRegSynonyms(const string& s) { Append(s); }
SRegSynonymsSRegSynonyms128     SRegSynonyms(initializer_list<SRegSynonyms> src) { for (auto& v : src) for (auto& s : v) Append(s); }
129 
InsertSRegSynonyms130     void Insert(CTempString s) { if (NotFound(s)) insert(begin(), s); }
131 
132 private:
NotFoundSRegSynonyms133     bool NotFound(CTempString s) const { return !s.empty() && find(begin(), end(), s) == end(); }
AppendSRegSynonyms134     void Append(CTempString s) { if (NotFound(s)) push_back(s); }
135 };
136 
137 class NCBI_XCONNECT_EXPORT CSynRegistry
138 {
139     template <typename TType> struct TR;
140     class CReport;
141     class CInclude;
142     class CAlert;
143 
144 public:
145     using TPtr = shared_ptr<CSynRegistry>;
146 
147     CSynRegistry();
148     ~CSynRegistry();
149 
150     template <typename TType>
Get(const SRegSynonyms & sections,SRegSynonyms names,TType default_value)151     typename TR<TType>::T Get(const SRegSynonyms& sections, SRegSynonyms names, TType default_value)
152     {
153         return TGet(sections, names, static_cast<typename TR<TType>::T>(default_value));
154     }
155 
156     template <typename TType>
157     TType TGet(const SRegSynonyms& sections, SRegSynonyms names, TType default_value);
158 
159     bool Has(const SRegSynonyms& sections, SRegSynonyms names);
160 
161     void Add(const IRegistry& registry);
162     IRegistry& GetIRegistry();
163     void Report(ostream& os) const;
164     void Alerts(ostream& os) const;
165     bool AckAlert(size_t id);
166 
167 private:
168     CCompoundRegistry m_Registry;
169     int m_Priority = 0;
170     unique_ptr<CReport> m_Report;
171     unique_ptr<CInclude> m_Include;
172     unique_ptr<CAlert> m_Alert;
173 };
174 
175 // Class to create CSynRegistry from CConfig, IRegistry or CNcbiApplication automatically
176 struct NCBI_XCONNECT_EXPORT CSynRegistryBuilder
177 {
178     CSynRegistryBuilder(const IRegistry& registry);
179     CSynRegistryBuilder(CConfig* config = nullptr);
180     CSynRegistryBuilder(const CNcbiApplicationAPI& app);
181 
GetCSynRegistryBuilder182     CSynRegistry::TPtr Get() { return m_Registry; }
operator CSynRegistry&CSynRegistryBuilder183     operator CSynRegistry&() { return *m_Registry; }
184 
185 private:
186     CSynRegistry::TPtr m_Registry;
187 };
188 
189 class NCBI_XCONNECT_EXPORT CSynRegistryToIRegistry : public IRegistry
190 {
191 public:
192     CSynRegistryToIRegistry(CSynRegistry::TPtr registry);
193 
194     const string& Get(const string& section, const string& name, TFlags flags = 0) const final;
195     bool HasEntry(const string& section, const string& name = kEmptyStr, TFlags flags = 0) const final;
196     string GetString(const string& section, const string& name, const string& default_value, TFlags flags = 0) const final;
197     int GetInt(const string& section, const string& name, int default_value, TFlags flags = 0, EErrAction err_action = eThrow) const final;
198     bool GetBool(const string& section, const string& name, bool default_value, TFlags flags = 0, EErrAction err_action = eThrow) const final;
199     double GetDouble(const string& section, const string& name, double default_value, TFlags flags = 0, EErrAction err_action = eThrow) const final;
200     const string& GetComment(const string& section = kEmptyStr, const string& name = kEmptyStr, TFlags flags = 0) const final;
201     void EnumerateInSectionComments(const string& section, list<string>* comments, TFlags flags = fAllLayers) const final;
202     void EnumerateSections(list<string>* sections, TFlags flags = fAllLayers) const final;
203     void EnumerateEntries(const string& section, list<string>* entries, TFlags flags = fAllLayers) const final;
204 
205 private:
206     bool x_Empty(TFlags flags) const final;
207     bool x_Modified(TFlags flags) const final;
208     void x_SetModifiedFlag(bool modified, TFlags flags) final;
209     const string& x_Get(const string& section, const string& name, TFlags flags) const final;
210     bool x_HasEntry(const string& section, const string& name, TFlags flags) const final;
211     const string& x_GetComment(const string& section, const string& name, TFlags flags) const final;
212     void x_Enumerate(const string& section, list<string>& entries, TFlags flags) const final;
213     void x_ChildLockAction(FLockAction action) final;
214 
GetIRegistry() const215     IRegistry& GetIRegistry() const { _ASSERT(m_Registry); return m_Registry->GetIRegistry(); }
216 
217     CSynRegistry::TPtr m_Registry;
218 };
219 
220 template <typename TType> struct CSynRegistry::TR              { using T = TType;  };
221 template <>               struct CSynRegistry::TR<const char*> { using T = string; };
222 template <>               struct CSynRegistry::TR<unsigned>    { using T = int;    };
223 
224 extern template NCBI_XCONNECT_EXPORT string CSynRegistry::TGet(const SRegSynonyms& sections, SRegSynonyms names, string default_value);
225 extern template NCBI_XCONNECT_EXPORT bool   CSynRegistry::TGet(const SRegSynonyms& sections, SRegSynonyms names, bool default_value);
226 extern template NCBI_XCONNECT_EXPORT int    CSynRegistry::TGet(const SRegSynonyms& sections, SRegSynonyms names, int default_value);
227 extern template NCBI_XCONNECT_EXPORT double CSynRegistry::TGet(const SRegSynonyms& sections, SRegSynonyms names, double default_value);
228 
229 END_NCBI_SCOPE
230 
231 #endif  /* CONNECT_SERVICES__NETSERVICE_PARAMS_HPP */
232