Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
301 ashish 1
//  Copyright (c) 2007-2008 Facebook
2
//
3
//  Licensed under the Apache License, Version 2.0 (the "License");
4
//  you may not use this file except in compliance with the License.
5
//  You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//  Unless required by applicable law or agreed to in writing, software
10
//  distributed under the License is distributed on an "AS IS" BASIS,
11
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//  See the License for the specific language governing permissions and
13
//  limitations under the License.
14
//
15
// See accompanying file LICENSE or visit the Scribe site at:
16
// http://developers.facebook.com/scribe/
17
//
18
// @author Bobby Johnson
19
// @author James Wang
20
// @author Jason Sobel
21
 
22
#ifndef SCRIBE_CONN_POOL_H
23
#define SCRIBE_CONN_POOL_H
24
 
25
#include "common.h"
26
 
27
class scribeConn {
28
 public:
29
  scribeConn(const std::string& host, unsigned long port, int timeout);
30
  scribeConn(const std::string &service, const server_vector_t &servers, int timeout);
31
  virtual ~scribeConn();
32
 
33
  void addRef();
34
  void releaseRef();
35
  unsigned getRef();
36
 
37
  void lock();
38
  void unlock();
39
 
40
  bool isOpen();
41
  bool open();
42
  void close();
43
  bool send(boost::shared_ptr<logentry_vector_t> messages);
44
 
45
 private:
46
  std::string connectionString();
47
 
48
 protected:
49
  boost::shared_ptr<apache::thrift::transport::TSocket> socket;
50
  boost::shared_ptr<apache::thrift::transport::TFramedTransport> framedTransport;
51
  boost::shared_ptr<apache::thrift::protocol::TBinaryProtocol> protocol;
52
  boost::shared_ptr<scribe::thrift::scribeClient> resendClient;
53
 
54
  unsigned refCount;
55
 
56
  bool smcBased;
57
  std::string smcService;
58
  server_vector_t serverList;
59
  std::string remoteHost;
60
  unsigned long remotePort;
61
  int timeout; // connection, send, and recv timeout
62
  pthread_mutex_t mutex;
63
};
64
 
65
// key is hostname:port or the smc_service
66
typedef std::map<std::string, boost::shared_ptr<scribeConn> > conn_map_t;
67
 
68
// Scribe class to manage connection pooling
69
// Maintains a map of (<host,port> or smc_service) to scribeConn class.
70
// used to ensure that there is only one connection from one particular
71
// scribe server to any host,port or smc_service.
72
// see the global g_connPool in store.cpp
73
class ConnPool {
74
 public:
75
  ConnPool();
76
  virtual ~ConnPool();
77
 
78
  bool open(const std::string& host, unsigned long port, int timeout);
79
  bool open(const std::string &service, const server_vector_t &servers, int timeout);
80
 
81
  void close(const std::string& host, unsigned long port);
82
  void close(const std::string &service);
83
 
84
  bool send(const std::string& host, unsigned long port,
85
            boost::shared_ptr<logentry_vector_t> messages);
86
  bool send(const std::string &service,
87
            boost::shared_ptr<logentry_vector_t> messages);
88
 
89
 private:
90
  bool openCommon(const std::string &key, boost::shared_ptr<scribeConn> conn);
91
  void closeCommon(const std::string &key);
92
  bool sendCommon(const std::string &key,
93
                  boost::shared_ptr<logentry_vector_t> messages);
94
 
95
 protected:
96
  std::string makeKey(const std::string& name, unsigned long port);
97
 
98
  pthread_mutex_t mapMutex;
99
  conn_map_t connMap;
100
};
101
 
102
#endif // !defined SCRIBE_CONN_POOL_H