0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ProxyMap.cc
Go to the documentation of this file.
1 /*
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; either version 3
9  * of the 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 
29 #include <Common/Compat.h>
30 
31 #include "ProxyMap.h"
32 
33 #include <Common/StringExt.h>
34 
35 using namespace Hypertable;
36 using namespace std;
37 
38 void ProxyMap::update_mapping(const String &proxy, const String &hostname,
39  const InetAddr &addr, ProxyMapT &invalidated_map,
40  ProxyMapT &new_map) {
41  lock_guard<mutex> lock(m_mutex);
42  ProxyMapT::iterator iter = m_forward_map.find(proxy);
43  if (iter != m_forward_map.end() && (*iter).second.addr == addr) {
44  if ((*iter).second.hostname != hostname)
45  (*iter).second.hostname = hostname;
46  return;
47  }
48  invalidate_old_mapping(proxy, addr, invalidated_map);
49  new_map[proxy] = ProxyAddressInfo(hostname, addr);
50  m_forward_map[proxy] = ProxyAddressInfo(hostname, addr);
51  m_reverse_map[addr] = proxy;
52 }
53 
54 
55 void ProxyMap::update_mappings(String &mappings, ProxyMapT &invalidated_map,
56  ProxyMapT &new_map) {
57  lock_guard<mutex> lock(m_mutex);
58  char *line, *proxy, *hostname, *addr_str, *end_nl, *end_tab;
59 
60  for (line = strtok_r((char *)mappings.c_str(), "\n", &end_nl); line;
61  line = strtok_r(0, "\n", &end_nl)) {
62  proxy = strtok_r(line, "\t", &end_tab);
63  HT_ASSERT(proxy);
64  hostname = strtok_r(0, "\t", &end_tab);
65  HT_ASSERT(hostname);
66  addr_str = strtok_r(0, "\t", &end_tab);
67  HT_ASSERT(addr_str);
68  InetAddr addr(addr_str);
69 
70  ProxyMapT::iterator iter = m_forward_map.find(proxy);
71 
72  if (!strcmp(hostname, "--DELETED--")) {
73  if (iter != m_forward_map.end()) {
74  if ((*iter).second.addr != addr) {
75  HT_WARNF("Proxy map removal message for %s contains %s, but map "
76  "contains %s", proxy, addr_str,
77  (*iter).second.addr.format().c_str());
78  }
79  invalidate(proxy, invalidated_map);
80  }
81  else {
82  HT_WARNF("Removal message received for %s, but not in map.", proxy);
83  }
84  }
85  else {
86  if (iter != m_forward_map.end() && (*iter).second.addr == addr) {
87  if ((*iter).second.hostname != hostname)
88  (*iter).second.hostname = hostname;
89  continue;
90  }
91  invalidate_old_mapping(proxy, addr, invalidated_map);
92  new_map[proxy] = ProxyAddressInfo(hostname, addr);
93  m_forward_map[proxy] = ProxyAddressInfo(hostname, addr);
94  m_reverse_map[addr] = proxy;
95  }
96  }
97 }
98 
99 
100 void ProxyMap::remove_mapping(const String &proxy, ProxyMapT &remove_map) {
101  lock_guard<mutex> lock(m_mutex);
102  invalidate(proxy, remove_map);
103  return;
104 }
105 
106 
107 
108 bool ProxyMap::get_mapping(const String &proxy, String &hostname, InetAddr &addr) {
109  lock_guard<mutex> lock(m_mutex);
110  ProxyMapT::iterator iter = m_forward_map.find(proxy);
111  if (iter == m_forward_map.end())
112  return false;
113  addr = (*iter).second.addr;
114  hostname = (*iter).second.hostname;
115  return true;
116 }
117 
119  lock_guard<mutex> lock(m_mutex);
120  SockAddrMap<String>::iterator iter = m_reverse_map.find(addr);
121  if (iter != m_reverse_map.end())
122  return (*iter).second;
123  return "";
124 }
125 
126 
128  lock_guard<mutex> lock(m_mutex);
129  String payload;
130  CommHeader header;
132  for (ProxyMapT::iterator iter = m_forward_map.begin(); iter != m_forward_map.end(); ++iter)
133  payload += (*iter).first + "\t" + (*iter).second.hostname + "\t" + (*iter).second.addr.format() + "\n";
134  CommBufPtr cbuf = make_shared<CommBuf>(header, payload.length());
135  if (payload.length())
136  cbuf->append_bytes((uint8_t *)payload.c_str(), payload.length());
137  return cbuf;
138 }
139 
140 
141 void
143  ProxyMapT &invalidated_map) {
144  ProxyMapT::iterator iter;
146 
147  // Invalidate entries from forward map, if changed
148  if ((iter = m_forward_map.find(proxy)) != m_forward_map.end()) {
149  if ((*iter).second.addr != addr) {
150  invalidated_map[(*iter).first] = (*iter).second;
151  m_reverse_map.erase( (*iter).second.addr );
152  m_forward_map.erase((*iter).first);
153  }
154  }
155 
156  // Invalidate entries from reverse map, if changed
157  if ((rev_iter = m_reverse_map.find(addr)) != m_reverse_map.end()) {
158  if ((*rev_iter).second != proxy) {
159  invalidated_map[(*rev_iter).second] = ProxyAddressInfo("unknown", (*rev_iter).first);
160  m_forward_map.erase((*rev_iter).second);
161  m_reverse_map.erase((*rev_iter).first);
162  }
163  }
164 
165 }
166 
167 void ProxyMap::invalidate(const String &proxy, ProxyMapT &invalidated_map) {
168  ProxyMapT::iterator iter;
170  InetAddr addr;
171 
172  // Invalidate entries from forward map
173  if ((iter = m_forward_map.find(proxy)) != m_forward_map.end()) {
174  (*iter).second.hostname = "--DELETED--";
175  invalidated_map[(*iter).first] = (*iter).second;
176  addr = (*iter).second.addr;
177  m_forward_map.erase((*iter).first);
178  }
179  else
180  return;
181 
182  // Invalidate entries from reverse map
183  if ((rev_iter = m_reverse_map.find(addr)) != m_reverse_map.end()) {
184  if ((*rev_iter).second == proxy)
185  m_reverse_map.erase((*rev_iter).first);
186  }
187 }
188 
190  lock_guard<mutex> lock(m_mutex);
191  ProxyMapT::iterator iter;
192  String str;
193  for (iter = m_forward_map.begin(); iter != m_forward_map.end(); ++iter)
194  str += format("(%s,%s,%s),", (*iter).first.c_str(),
195  (*iter).second.hostname.c_str(), (*iter).second.addr.format().c_str());
196  return str;
197 }
#define HT_WARNF(msg,...)
Definition: Logger.h:290
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
bool get_mapping(const String &proxy, String &hostname, InetAddr &addr)
Returns proxy map data for proxy.
Definition: ProxyMap.cc:108
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
Definition: String.cc:37
uint16_t flags
Flags.
Definition: CommHeader.h:139
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
STL namespace.
CommBufPtr create_update_message()
Creates a proxy map update message.
Definition: ProxyMap.cc:127
String get_proxy(InetAddr &addr)
Returns proxy name for addr.
Definition: ProxyMap.cc:118
#define HT_ASSERT(_e_)
Definition: Logger.h:396
Declarations for ProxyMap.
Unordered map specialization for InetAddr keys.
Definition: SockAddrMap.h:57
Encapsulate an internet address.
Definition: InetAddr.h:66
std::shared_ptr< CommBuf > CommBufPtr
Smart pointer to CommBuf.
Definition: CommBuf.h:305
Holds address and hostname associated with a proxy name.
Definition: ProxyMap.h:48
void invalidate_old_mapping(const String &proxy, const InetAddr &addr, ProxyMapT &invalidated_mappings)
Invalidates (removes) mapping, if changed, from forward and reverse maps.
Definition: ProxyMap.cc:142
std::unordered_map< String, ProxyAddressInfo > ProxyMapT
Forward mapping hash type from proxy name to ProxyAddressInfo.
Definition: ProxyMap.h:57
Compatibility Macros for C/C++.
Hypertable definitions
void remove_mapping(const String &proxy, ProxyMapT &remove_map)
Removes a mapping.
Definition: ProxyMap.cc:100
Header for messages transmitted via AsyncComm.
Definition: CommHeader.h:40
void update_mappings(String &mappings, ProxyMapT &invalidated_map, ProxyMapT &new_map)
Update mappings from proxy map update message string.
Definition: ProxyMap.cc:55
void invalidate(const String &proxy, ProxyMapT &invalidated_mappings)
Invalidates (removes) mapping from forward and reverse maps.
Definition: ProxyMap.cc:167
void update_mapping(const String &proxy, const String &hostname, const InetAddr &addr, ProxyMapT &invalidated_map, ProxyMapT &new_map)
Updates a proxy name mapping.
Definition: ProxyMap.cc:38
String extensions and helpers: sets, maps, append operators etc.