NCBI C++ ToolKit
ncbi_pool_balancer.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CORELIB___NCBI_POOL_BALANCER__HPP
2 #define CORELIB___NCBI_POOL_BALANCER__HPP
3 
4 /* $Id: ncbi_pool_balancer.hpp 100862 2023-09-20 19:38:20Z ucko $
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  * Author: Aaron Ucko
30  *
31  */
32 
33 /// @file ncbi_pool_balancer.hpp
34 /// Help distribute connections within a pool across servers.
35 
37 
39 
40 
42 {
43 public:
44  virtual ~IBalanceable()
45  { }
46  virtual const string& ServerName(void) const = 0;
47  virtual Uint4 Host(void) const = 0;
48  virtual Uint2 Port(void) const = 0;
49 };
50 
51 
53 {
54 public:
55  enum EFlags {
56  fIgnoreRawIPs = 1 << 0,
57  fNoPooling = 1 << 1
58  };
60 
61  CPoolBalancer(const string& service_name,
62  const IDBServiceMapper::TOptions& options,
63  TFlags flags);
64 
65  CPoolBalancer(const string& service_name,
66  const IDBServiceMapper::TOptions& options,
67  bool ignore_raw_ips)
68  : CPoolBalancer(service_name, options,
69  TFlags(ignore_raw_ips ? fIgnoreRawIPs
70  : static_cast<EFlags>(0)))
71  { }
72 
74  { return x_GetServer(nullptr, nullptr); }
75 
76  void LocallyPenalize(TSvrRef server);
77 
78 protected:
79  typedef map<string, unsigned int> TCounts; // by server name
80 
81  void x_InitFromCounts(const TCounts& counts);
82  TSvrRef x_GetServer(const void* params, IBalanceable** conn);
83  bool x_NoPooling(void) const
84  { return m_Flags & fNoPooling; }
85 
86  virtual IBalanceable* x_TryPool(const void* /* params */)
87  { return nullptr; }
88  virtual unsigned int x_GetCount(const void* /* params */,
89  const string& /* name */)
90  { return 0u; }
91  virtual unsigned int x_GetPoolMax(const void* /* params */)
92  { return 0u; }
93  virtual void x_Discard(const void* /* params */, IBalanceable* /* conn */)
94  { }
95 
96 private:
97  struct SEndpointInfo {
99  : effective_ranking(0.0), ideal_count(0.0), actual_count(0U),
100  penalty_level(0U)
101  { }
102 
105  double ideal_count;
106  unsigned int actual_count;
107  unsigned int penalty_level;
108  };
110 
111  CEndpointKey x_NameToKey(CTempString& name) const;
112  TEndpoints::iterator x_FindEndpoint(CEndpointKey key, CTempString name);
113  TEndpoints::iterator x_FindEndpointAsIs(CEndpointKey key,
114  CTempString name);
115 
119  unsigned int m_TotalCount;
120  TFlags m_Flags;
121 };
122 
124 
125 
127 
128 #endif /* CORELIB___NCBI_POOL_BALANCER__HPP */
Lightweight representation of just a host and a port.
CObject –.
Definition: ncbiobj.hpp:180
virtual IBalanceable * x_TryPool(const void *)
map< string, unsigned int > TCounts
DECLARE_SAFE_FLAGS_TYPE(EFlags, TFlags)
multimap< CEndpointKey, SEndpointInfo > TEndpoints
multiset< double > m_Rankings
unsigned int m_TotalCount
virtual void x_Discard(const void *, IBalanceable *)
TSvrRef GetServer(void)
bool x_NoPooling(void) const
CPoolBalancer(const string &service_name, const IDBServiceMapper::TOptions &options, bool ignore_raw_ips)
virtual unsigned int x_GetPoolMax(const void *)
virtual unsigned int x_GetCount(const void *, const string &)
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
virtual const string & ServerName(void) const =0
virtual Uint4 Host(void) const =0
virtual Uint2 Port(void) const =0
virtual ~IBalanceable()
list< CRef< CDBServerOption > > TOptions
static CS_CONNECTION * conn
Definition: ct_dynamic.c:25
static uch flags
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
uint16_t Uint2
2-byte (16-bit) unsigned integer
Definition: ncbitype.h:101
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define NCBI_XNCBI_EXPORT
Definition: ncbi_export.h:1283
const struct ncbi::grid::netcache::search::fields::KEY key
DECLARE_SAFE_FLAGS(CPoolBalancer::EFlags)
Modified on Fri Dec 01 04:48:33 2023 by modify_doxy.py rev. 669887