Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
|
This class is thread-safe. More...
#include <client-cache.h>
Classes | |
struct | PerHostCache |
Public Types | |
typedef boost::function < ThriftClientImpl *(const TNetworkAddress &address, ClientKey *client_key)> | ClientFactory |
Public Member Functions | |
Status | GetClient (const TNetworkAddress &address, ClientFactory factory_method, ClientKey *client_key) |
If there is an error creating the new client, *client_key will be NULL. More... | |
Status | ReopenClient (ClientFactory factory_method, ClientKey *client_key) |
void | ReleaseClient (ClientKey *client_key) |
void | CloseConnections (const TNetworkAddress &address) |
std::string | DebugString () |
Return a debug representation of the contents of this cache. More... | |
void | TestShutdown () |
Closes every connection in the cache. Used only for testing. More... | |
void | InitMetrics (MetricGroup *metrics, const std::string &key_prefix) |
Private Types | |
typedef boost::unordered_map < TNetworkAddress, boost::shared_ptr < PerHostCache > > | PerHostCacheMap |
typedef std::map< ClientKey, boost::shared_ptr < ThriftClientImpl > > | ClientMap |
Private Member Functions | |
ClientCacheHelper (uint32_t num_tries, uint64_t wait_ms, int32_t send_timeout_ms, int32_t recv_timeout_ms) | |
Private constructor so that only ClientCache can instantiate this class. More... | |
Status | CreateClient (const TNetworkAddress &address, ClientFactory factory_method, ClientKey *client_key) |
Create a new client for specific address in 'client' and put it in client_map_. More... | |
Private Attributes | |
boost::mutex | cache_lock_ |
Protects per_host_caches_. More... | |
PerHostCacheMap | per_host_caches_ |
boost::mutex | client_map_lock_ |
Protects client_map_. More... | |
ClientMap | client_map_ |
const uint32_t | num_tries_ |
Number of attempts to make to open a connection. 0 means retry indefinitely. More... | |
const uint64_t | wait_ms_ |
Time to wait between failed connection attempts. More... | |
const int32_t | send_timeout_ms_ |
Time to wait for the underlying socket to send data, e.g., for an RPC. More... | |
const int32_t | recv_timeout_ms_ |
Time to wait for the underlying socket to receive data, e.g., for an RPC response. More... | |
bool | metrics_enabled_ |
IntGauge * | clients_in_use_metric_ |
Number of clients 'checked-out' from the cache. More... | |
IntGauge * | total_clients_metric_ |
Total clients in the cache, including those in use. More... | |
Friends | |
template<class T > | |
class | ClientCache |
This class is thread-safe.
Helper class which implements the majority of the caching functionality without using templates (i.e. pointers to the superclass of all ThriftClients and a void* for the key). This class is for internal use only; the public interface is in ClientCache below. A client is either 'in-use' (the user of the cache is between GetClient() and ReleaseClient() pairs) or 'cached', in which case it is available for the next GetClient() call. Internally, this class maintains a map of all clients, in use or not, which is indexed by their ClientKey (see below), and a map from server address to a list of the keys of all clients that are not currently in use. The user of this class only sees RPC proxy classes, but we have to track the ThriftClient to manipulate the underlying transport. To do this, we use an opaque ClientKey pointer type to act as the key for a particular client. We actually know the type of the value at the end of pointer (it's the type parameter to ClientCache), but we deliberately avoid using it so that we don't have to parameterise this class by type, and thus this entire class doesn't get inlined every time it gets used.TODO: shut down clients in the background if they don't get used for a period of time TODO: More graceful handling of clients that have failed (maybe better handled by a smart-wrapper of the interface object). TODO: limits on total number of clients, and clients per-backend TODO: move this to a separate header file, so that the public interface is more prominent in this file
Definition at line 63 of file client-cache.h.
typedef boost::function<ThriftClientImpl* (const TNetworkAddress& address, ClientKey* client_key)> impala::ClientCacheHelper::ClientFactory |
Callback method which produces a client object when one cannot be found in the cache. Supplied by the ClientCache wrapper.
Definition at line 68 of file client-cache.h.
|
private |
Map from client key back to its associated ThriftClientImpl transport. This is where all the clients are actually stored, and client instances are owned by this class and persist for exactly as long as they are present in this map. We use a map (vs. unordered_map) so we get iterator consistency across operations.
Definition at line 156 of file client-cache.h.
|
private |
Map from an address to a PerHostCache containing a list of keys that have entries in client_map_ for that host. The value type is wrapped in a shared_ptr so that the copy c'tor for PerHostCache is not required.
Definition at line 146 of file client-cache.h.
|
inlineprivate |
Private constructor so that only ClientCache can instantiate this class.
Definition at line 107 of file client-cache.h.
void impala::ClientCacheHelper::CloseConnections | ( | const TNetworkAddress & | address | ) |
Close all connections to a host (e.g., in case of failure) so that on their next use they will have to be reopened via ReopenClient().
Definition at line 148 of file client-cache.cc.
References impala::ClientCacheHelper::PerHostCache::clients, and impala::ClientCacheHelper::PerHostCache::lock.
Referenced by impala::ClientCache< T >::CloseConnections().
|
private |
Create a new client for specific address in 'client' and put it in client_map_.
Definition at line 104 of file client-cache.cc.
References impala::OK, and impala::Status::ok().
string impala::ClientCacheHelper::DebugString | ( | ) |
Return a debug representation of the contents of this cache.
Definition at line 170 of file client-cache.cc.
Referenced by impala::ClientCache< T >::DebugString().
Status impala::ClientCacheHelper::GetClient | ( | const TNetworkAddress & | address, |
ClientFactory | factory_method, | ||
ClientKey * | client_key | ||
) |
If there is an error creating the new client, *client_key will be NULL.
Returns a client for the given address in 'client_key'. If a previously created client is not available (i.e. there are no entries in the per-host cache), a new client is created by calling the supplied 'factory_method'. As a postcondition, the returned client will not be present in the per-host cache.
Definition at line 40 of file client-cache.cc.
References impala::OK, and RETURN_IF_ERROR.
Referenced by impala::ClientCache< T >::GetClient().
void impala::ClientCacheHelper::InitMetrics | ( | MetricGroup * | metrics, |
const std::string & | key_prefix | ||
) |
Creates two metrics for this cache measuring the number of clients currently used, and the total number in the cache.
Definition at line 199 of file client-cache.cc.
References impala::MetricGroup::AddGauge().
Referenced by impala::ClientCache< T >::InitMetrics().
void impala::ClientCacheHelper::ReleaseClient | ( | ClientKey * | client_key | ) |
Returns a client to the cache. Upon return, *client_key will be NULL, and the associated client will be available in the per-host cache..
Definition at line 127 of file client-cache.cc.
Referenced by impala::ClientCache< T >::ReleaseClient().
Status impala::ClientCacheHelper::ReopenClient | ( | ClientFactory | factory_method, |
ClientKey * | client_key | ||
) |
Returns a newly-opened client in client_key. May reopen the existing client, or may replace it with a new one (created using 'factory_method'). Returns an error status and sets 'client_key' to NULL if a new client cannot created.
Definition at line 68 of file client-cache.cc.
References impala::Status::ok().
Referenced by impala::ClientCache< T >::ReopenClient().
void impala::ClientCacheHelper::TestShutdown | ( | ) |
Closes every connection in the cache. Used only for testing.
Definition at line 186 of file client-cache.cc.
Referenced by impala::ClientCache< T >::TestShutdown().
|
friend |
Definition at line 105 of file client-cache.h.
|
private |
Protects per_host_caches_.
Definition at line 140 of file client-cache.h.
|
private |
Definition at line 157 of file client-cache.h.
|
private |
Protects client_map_.
Definition at line 150 of file client-cache.h.
|
private |
Number of clients 'checked-out' from the cache.
Definition at line 176 of file client-cache.h.
|
private |
True if metrics have been registered (i.e. InitMetrics() was called)), and *_metric_ are valid pointers.
Definition at line 173 of file client-cache.h.
|
private |
Number of attempts to make to open a connection. 0 means retry indefinitely.
Definition at line 160 of file client-cache.h.
|
private |
Definition at line 147 of file client-cache.h.
|
private |
Time to wait for the underlying socket to receive data, e.g., for an RPC response.
Definition at line 169 of file client-cache.h.
|
private |
Time to wait for the underlying socket to send data, e.g., for an RPC.
Definition at line 166 of file client-cache.h.
|
private |
Total clients in the cache, including those in use.
Definition at line 179 of file client-cache.h.
|
private |
Time to wait between failed connection attempts.
Definition at line 163 of file client-cache.h.