1 #ifndef PSGS_OSGMAPPER__HPP
2 #define PSGS_OSGMAPPER__HPP
3 
4 /*  $Id: osg_mapper.hpp 629837 2021-04-22 12:47:49Z ivanov $
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:  Eugene Vasilchenko, Aaron Ucko
30  *
31  */
32 
33 /// @file osgservicemapper.hpp
34 /// PSG to OSG connection service mapper.
35 
36 
37 #include <util/random_gen.hpp>
38 #include <connect/ext/ncbi_dblb_svcmapper.hpp>
39 
40 
41 /** @addtogroup OS_Gateway
42  *
43  * @{
44  */
45 
46 
47 BEGIN_NCBI_NAMESPACE;
48 BEGIN_NAMESPACE(psg);
49 BEGIN_NAMESPACE(osg);
50 
51 
52 typedef Uint8 TOSGEndpointKey;
53 
54 inline
g_OSG_MakeEndpointKey(Uint4 host,Uint2 port)55 TOSGEndpointKey g_OSG_MakeEndpointKey(Uint4 host, Uint2 port)
56 {
57     return (static_cast<TOSGEndpointKey>(host) << 16) | port;
58 }
59 
60 inline
g_OSG_GetHost(TOSGEndpointKey key)61 Uint4 g_OSG_GetHost(TOSGEndpointKey key)
62 {
63     return key >> 16;
64 }
65 
66 inline
g_OSG_GetPort(TOSGEndpointKey key)67 Uint2 g_OSG_GetPort(TOSGEndpointKey key)
68 {
69     return key & 0xFFFF;
70 }
71 
72 
73 class COSGServiceMapper : public CDBLB_ServiceMapper
74 {
75 public:
76     COSGServiceMapper(const IRegistry* registry = NULL);
77     virtual ~COSGServiceMapper();
78 
79     static void InitDefaults(IRWRegistry& reg);
80 
81     void    Configure     (const IRegistry* registry = NULL);
GetServer(const string & service)82     TSvrRef GetServer     (const string& service)
83         { return x_GetServer(service, NULL); }
84     void    Exclude       (const string& service, const TSvrRef& server);
85     void    CleanExcluded (const string& service);
86     void    GetServersList(const string& service, list<string>* serv_list)
87         const;
88     void    GetServerOptions(const string& service, TOptions* options);
89 
90     typedef vector<TOSGEndpointKey> TTried;
91     enum EFeedback {
92         eNegativeFeedback,
93         ePositiveFeedback
94     };
GetServer(const string & service,const TTried & tried)95     TSvrRef GetServer(const string& service, const TTried& tried)
96         { return x_GetServer(service, &tried); }
97     void    AcceptFeedback(const string& service,
98                            unsigned int host, unsigned short port,
99                            EFeedback feedback);
100 
101     string GetMainServiceName(const string& service);
102 
Factory(const IRegistry * registry)103     static IDBServiceMapper* Factory(const IRegistry* registry)
104         { return new COSGServiceMapper(registry); }
105 
106 private:
107     typedef CDBLB_ServiceMapper TParent;
108 
109     struct SServerRating : public CObject {
110         TSvrRef ref;
111         double  penalty; // 0.0 = always accept, 1.0 = always reject
112         bool    excluded;
113     };
114     typedef map<TOSGEndpointKey, SServerRating>  TServerRatings;
115     typedef map<string,          TServerRatings> TAllServerRatings;
116 
117     TSvrRef x_GetServer(const string& service, const TTried* tried);
118     void x_NormalizePenalties(void);
119     SServerRating& x_SetRating(TServerRatings& ratings,
120                                Uint4 host, Uint2 port, bool* was_new,
121                                CTempString name = kEmptyStr) const;
122 
123     map<string, string> m_MainServiceNameMap;
124     double m_PositiveFeedbackWeight;
125     double m_NegativeFeedbackWeight;
126     double m_PenaltyNormalizationInterval;
127     double m_PenaltyDecayRate;  // precalculated from half-life
128     double m_InitialPenalty;
129 
130     mutable TAllServerRatings m_AllServerRatings;
131     mutable CFastRWLock       m_AllServerRatingsLock;
132     CStopWatch m_AllServerRatingsTimer;
133     CFastMutex m_DBLBExclusionsMutex;
134     CRandom    m_Random;
135     CFastMutex m_RandomMutex;
136 };
137 
138 
139 //////////////////////////////////////////////////////////////////////
140 
141 
142 inline
GetMainServiceName(const string & service)143 string COSGServiceMapper::GetMainServiceName(const string& service)
144 {
145     auto it = m_MainServiceNameMap.find(service);
146     return it == m_MainServiceNameMap.end() ? service : it->second;
147 }
148 
149 
150 END_NAMESPACE(osg);
151 END_NAMESPACE(psg);
152 END_NCBI_NAMESPACE;
153 
154 
155 /* @} */
156 
157 #endif  // PSGS_OSGMAPPER__HPP
158