0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RangeServerConnectionManager.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #ifndef Hypertable_Master_RangeServerConnectionManager_h
23 #define Hypertable_Master_RangeServerConnectionManager_h
24 
26 
27 #include <AsyncComm/Comm.h>
28 
29 #include <Common/StringExt.h>
30 
31 #include <boost/multi_index_container.hpp>
32 #include <boost/multi_index/hashed_index.hpp>
33 #include <boost/multi_index/mem_fun.hpp>
34 #include <boost/multi_index/sequenced_index.hpp>
35 #include <boost/multi_index/member.hpp>
36 
37 #include <condition_variable>
38 #include <memory>
39 #include <mutex>
40 #include <set>
41 #include <utility>
42 
43 namespace Hypertable {
44 
45  using namespace boost::multi_index;
46 
49  double disk_usage;
50  };
51 
53  public:
55 
58  void add_server(RangeServerConnectionPtr &rsc);
59 
68  bool remove_server(const std::string &location, RangeServerConnectionPtr &rsc);
69 
70  bool connect_server(RangeServerConnectionPtr &rsc, const String &hostname, InetAddr local_addr, InetAddr public_addr);
71  bool disconnect_server(RangeServerConnectionPtr &rsc);
72  bool is_connected(const String &location);
73  bool find_server_by_location(const String &location, RangeServerConnectionPtr &rsc);
74  bool find_server_by_hostname(const String &hostname, RangeServerConnectionPtr &rsc);
75  bool find_server_by_public_addr(InetAddr addr, RangeServerConnectionPtr &rsc);
76  bool find_server_by_local_addr(InetAddr addr, RangeServerConnectionPtr &rsc);
77  bool next_available_server(RangeServerConnectionPtr &rsc, bool urgent=false);
78  void set_servers_balanced(const std::vector<RangeServerConnectionPtr> &servers);
79  bool exist_unbalanced_servers();
80  size_t server_count();
81  void get_servers(std::vector<RangeServerConnectionPtr> &servers);
82  void get_valid_connections(StringSet &locations,
83  std::vector<RangeServerConnectionPtr> &connections);
84  size_t connection_count() { std::lock_guard<std::mutex> lock(m_mutex); return m_conn_count; }
85  void set_range_server_state(std::vector<RangeServerState> &states);
86 
87  private:
88 
89  bool find_server_by_location_unlocked(const String &location, RangeServerConnectionPtr &rsc);
90 
91  typedef std::map<String, uint64_t> HandleMap;
92 
94  public:
97  const String& location() const { return rsc->location(); }
98  const String& hostname() const { return rsc->hostname(); }
99  InetAddr public_addr() const { return rsc->public_addr(); }
100  InetAddr local_addr() const { return rsc->local_addr(); }
101  bool connected() const { return rsc->connected(); }
102  };
103 
104  struct InetAddrHash {
105  std::size_t operator()(InetAddr addr) const {
106  return (std::size_t)(addr.sin_addr.s_addr ^ addr.sin_port);
107  }
108  };
109 
110  typedef boost::multi_index_container<
111  RangeServerConnectionEntry,
112  indexed_by<
113  sequenced<>,
114  hashed_unique<const_mem_fun<RangeServerConnectionEntry, const String&,
115  &RangeServerConnectionEntry::location> >,
116  hashed_non_unique<const_mem_fun<RangeServerConnectionEntry, const String&,
117  &RangeServerConnectionEntry::hostname> >,
118  hashed_unique<const_mem_fun<RangeServerConnectionEntry, InetAddr,
119  &RangeServerConnectionEntry::public_addr>, InetAddrHash>,
120  hashed_non_unique<const_mem_fun<RangeServerConnectionEntry, InetAddr,
121  &RangeServerConnectionEntry::local_addr>, InetAddrHash>
122  >
124 
125  typedef ConnectionList::nth_index<0>::type Sequence;
126  typedef ConnectionList::nth_index<1>::type LocationIndex;
127  typedef ConnectionList::nth_index<2>::type HostnameIndex;
128  typedef ConnectionList::nth_index<3>::type PublicAddrIndex;
129  typedef ConnectionList::nth_index<4>::type LocalAddrIndex;
130 
133 
135 
136  ConnectionList::iterator m_connections_iter;
137 
138  size_t m_conn_count {};
139  int32_t m_disk_threshold {};
140  };
141 
142  typedef std::shared_ptr<RangeServerConnectionManager> RangeServerConnectionManagerPtr;
143 
144 }
145 
146 #endif // Hypertable_Master_RangeServerConnectionManager_h
std::set< String > StringSet
STL Set managing Strings.
Definition: StringExt.h:42
ConnectionList::nth_index< 1 >::type LocationIndex
bool next_available_server(ContextPtr &context, String &location, bool urgent)
Gets name of next available server.
Definition: Utility.cc:304
static std::mutex mutex
Definition: Logger.cc:43
std::shared_ptr< RangeServerConnection > RangeServerConnectionPtr
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
InetAddr local_addr() const
std::mutex m_mutex
Mutex for serializing member access
InetAddr public_addr() const
boost::multi_index_container< RangeServerConnectionEntry, indexed_by< sequenced<>, hashed_unique< const_mem_fun< RangeServerConnectionEntry, const String &,&RangeServerConnectionEntry::location > >, hashed_non_unique< const_mem_fun< RangeServerConnectionEntry, const String &,&RangeServerConnectionEntry::hostname > >, hashed_unique< const_mem_fun< RangeServerConnectionEntry, InetAddr,&RangeServerConnectionEntry::public_addr >, InetAddrHash >, hashed_non_unique< const_mem_fun< RangeServerConnectionEntry, InetAddr,&RangeServerConnectionEntry::local_addr >, InetAddrHash > > > ConnectionList
const String & hostname() const
Encapsulate an internet address.
Definition: InetAddr.h:66
ConnectionList::nth_index< 0 >::type Sequence
const String & location() const
RangeServerConnectionEntry(RangeServerConnectionPtr &_rsc)
ConnectionList::nth_index< 2 >::type HostnameIndex
ConnectionList::nth_index< 3 >::type PublicAddrIndex
Hypertable definitions
Declarations for Comm.
ConnectionList::nth_index< 4 >::type LocalAddrIndex
std::shared_ptr< RangeServerConnectionManager > RangeServerConnectionManagerPtr
RangeServerConnectionPtr rsc
bool connected() const
String extensions and helpers: sets, maps, append operators etc.