0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ResponseManager.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; 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 
28 #ifndef Hypertable_Master_ResponseManager_h
29 #define Hypertable_Master_ResponseManager_h
30 
31 #include "Context.h"
32 #include "Operation.h"
33 
35 
36 #include <AsyncComm/Clock.h>
37 #include <AsyncComm/Comm.h>
38 
39 #include <Common/Thread.h>
40 
41 #include <boost/graph/adjacency_list.hpp>
42 #include <boost/graph/properties.hpp>
43 #include <boost/multi_index/hashed_index.hpp>
44 #include <boost/multi_index/member.hpp>
45 #include <boost/multi_index/ordered_index.hpp>
46 #include <boost/multi_index_container.hpp>
47 #include <boost/thread/condition.hpp>
48 
49 #include <chrono>
50 #include <condition_variable>
51 #include <list>
52 #include <map>
53 #include <memory>
54 #include <mutex>
55 #include <thread>
56 
57 namespace Hypertable {
58 
63  using namespace boost::multi_index;
64 
76  public:
77 
80  comm = Comm::instance();
81  }
82 
85  class OperationRec {
86  public:
87 
91  OperationRec(OperationPtr &_op) : op(_op) { }
92 
96  ClockT::time_point expiration_time() const { return op->expiration_time(); }
97 
101  int64_t id() const { return op->id(); }
102 
105  };
106 
111  typedef boost::multi_index_container<
112  OperationRec,
113  indexed_by<
114  sequenced<>,
115  ordered_non_unique<const_mem_fun<OperationRec, ClockT::time_point,
116  &OperationRec::expiration_time> >,
117  hashed_unique<const_mem_fun<OperationRec, int64_t,
118  &OperationRec::id> >
119  >
121 
122  typedef OperationList::nth_index<0>::type OperationSequence;
123  typedef OperationList::nth_index<1>::type OperationExpirationTimeIndex;
124  typedef OperationList::nth_index<2>::type OperationIdentifierIndex;
125 
128  class DeliveryRec {
129  public:
131  ClockT::time_point expiration_time;
133  int64_t id;
136  };
137 
142  typedef boost::multi_index_container<
143  DeliveryRec,
144  indexed_by<
145  sequenced<>,
146  ordered_non_unique<member<DeliveryRec, ClockT::time_point,
147  &DeliveryRec::expiration_time> >,
148  hashed_unique<member<DeliveryRec, int64_t,
149  &DeliveryRec::id> >
150  >
152 
153  typedef DeliveryList::nth_index<0>::type DeliverySequence;
154  typedef DeliveryList::nth_index<1>::type DeliveryExpirationTimeIndex;
155  typedef DeliveryList::nth_index<2>::type DeliveryIdentifierIndex;
156 
159 
161  std::condition_variable cond;
162 
164  Comm *comm {};
165 
168 
170  bool shutdown {};
171 
174 
177 
179  std::list<OperationPtr> removal_queue;
180  };
181 
217  public:
218 
221  ResponseManager() : m_context(std::make_shared<ResponseManagerContext>()) { }
222 
238  void operator()();
239 
255  void add_operation(OperationPtr &operation);
256 
275  void add_delivery_info(int64_t operation_id, EventPtr &event);
276 
279  void set_mml_writer(MetaLog::WriterPtr &mml_writer) {
280  std::lock_guard<std::mutex> lock(m_context->mutex);
281  m_context->mml_writer = mml_writer;
282  }
283 
288  void shutdown();
289 
290  private:
292  std::shared_ptr<ResponseManagerContext> m_context;
293 
294  };
295 
298 }
299 
300 #endif // Hypertable_Master_ResponseManager_h
static Comm * instance()
Creates/returns singleton instance of the Comm class.
Definition: Comm.h:72
static std::mutex mutex
Definition: Logger.cc:43
int64_t id() const
Returns unique ID for completed operation.
OperationList expirable_ops
List of completed operations.
ClockT::time_point expiration_time() const
Returns expiration time of completed operation.
int64_t id
Corresponding operation identifier.
boost::multi_index_container< DeliveryRec, indexed_by< sequenced<>, ordered_non_unique< member< DeliveryRec, ClockT::time_point,&DeliveryRec::expiration_time > >, hashed_unique< member< DeliveryRec, int64_t,&DeliveryRec::id > > > > DeliveryList
Multi-index container holding response delivery information for operations.
chrono::time_point< fast_clock > time_point
Definition: fast_clock.h:42
Declarations for Operation.
DeliveryList delivery_list
List of delivery information records.
DeliveryList::nth_index< 1 >::type DeliveryExpirationTimeIndex
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
DeliveryList::nth_index< 0 >::type DeliverySequence
STL namespace.
ClockT::time_point expiration_time
Delivery expiration time specified by client.
Record holding completed operation information.
Declaration of ClockT.
std::shared_ptr< ResponseManagerContext > m_context
Pointer to shared context object.
std::mutex mutex
Mutex for serializing concurrent access
OperationList::nth_index< 2 >::type OperationIdentifierIndex
std::list< OperationPtr > removal_queue
Queue of completed operations to be removed from MML.
std::condition_variable cond
Condition variable used to wait for operations to expire.
Importing boost::thread and boost::thread_group into the Hypertable namespace.
Manages the sending of operation results back to clients.
Hypertable definitions
Declarations for MetaLog::Writer.
Entry point to AsyncComm service.
Definition: Comm.h:61
OperationPtr op
Smart pointer to completed operation.
MetaLog::WriterPtr mml_writer
MML writer.
DeliveryList::nth_index< 2 >::type DeliveryIdentifierIndex
std::shared_ptr< Writer > WriterPtr
Smart pointer to Writer.
Declarations for Comm.
OperationRec(OperationPtr &_op)
Constructor.
Implementation shared by ResponseManager objects.
Record holding response delivery information for an operation.
boost::multi_index_container< OperationRec, indexed_by< sequenced<>, ordered_non_unique< const_mem_fun< OperationRec, ClockT::time_point,&OperationRec::expiration_time > >, hashed_unique< const_mem_fun< OperationRec, int64_t,&OperationRec::id > > > > OperationList
Multi-index container for completed operations.
EventPtr event
Event object containing return address for operation result.
std::shared_ptr< Operation > OperationPtr
Smart pointer to Operation.
Definition: Operation.h:609
Declarations for Context.
OperationList::nth_index< 1 >::type OperationExpirationTimeIndex
void set_mml_writer(MetaLog::WriterPtr &mml_writer)
Sets MML writer.
OperationList::nth_index< 0 >::type OperationSequence