Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef COLLECTOR_H_ 7 : #define COLLECTOR_H_ 8 : 9 : #include <boost/asio/ip/tcp.hpp> 10 : #include <boost/ptr_container/ptr_map.hpp> 11 : #include <boost/uuid/uuid.hpp> 12 : #include <boost/tuple/tuple_comparison.hpp> 13 : #include <boost/assign/list_of.hpp> 14 : 15 : #if __GNUC_PREREQ(4, 6) 16 : #pragma GCC diagnostic push 17 : #pragma GCC diagnostic ignored "-Wunused-result" 18 : #endif 19 : #include <boost/uuid/uuid_generators.hpp> 20 : #if __GNUC_PREREQ(4, 6) 21 : #pragma GCC diagnostic pop 22 : #endif 23 : 24 : #include "base/parse_object.h" 25 : #include "base/random_generator.h" 26 : #include <sandesh/sandesh_server.h> 27 : #include <sandesh/sandesh_session.h> 28 : 29 : #include <analytics/viz_constants.h> 30 : #include "generator.h" 31 : #include <mutex> 32 : #include <string> 33 : #include <analytics/collector_uve_types.h> 34 : #include "db_handler.h" 35 : #include "base/logging.h" 36 : #include "base/task.h" 37 : #include "base/parse_object.h" 38 : #include <base/connection_info.h> 39 : #include "io/event_manager.h" 40 : #include "io/ssl_session.h" 41 : #include "base/sandesh/process_info_types.h" 42 : 43 : #include <sandesh/sandesh_ctrl_types.h> 44 : #include <sandesh/sandesh_uve_types.h> 45 : #include <sandesh/sandesh_statistics.h> 46 : #include <sandesh/sandesh_session.h> 47 : #include <sandesh/sandesh_connection.h> 48 : using process::ConnectionState; 49 : using process::ConnectionType; 50 : using process::ConnectionStatus; 51 : 52 : class DbHandler; 53 : class OpServerProxy; 54 : class EventManager; 55 : class SandeshStateMachine; 56 : 57 : class Collector : public SandeshServer { 58 : public: 59 : const static std::string kDbTask; 60 : const static int kQSizeHighWaterMark; 61 : const static int kQSizeLowWaterMark; 62 : 63 : typedef boost::function<bool(const VizMsg*, bool, DbHandler *, 64 : GenDb::GenDbIf::DbAddColumnCb db_cb)> VizCallback; 65 : 66 : Collector(EventManager *evm, short server_port, 67 : const std::string &server_ip, 68 : const SandeshConfig &sandesh_config, 69 : DbHandlerPtr db_handler, OpServerProxy *osp, 70 : VizCallback cb); 71 : virtual ~Collector(); 72 : virtual void Shutdown(); 73 : virtual void SessionShutdown(); 74 : 75 : virtual bool ReceiveResourceUpdate(SandeshSession *session, 76 : bool rsc); 77 : virtual bool ReceiveSandeshMsg(SandeshSession *session, 78 : const SandeshMessage *msg, bool rsc); 79 : virtual bool ReceiveSandeshCtrlMsg(SandeshStateMachine *state_machine, 80 : SandeshSession *session, const Sandesh *sandesh); 81 : 82 : void GetGeneratorSummaryInfo(std::vector<GeneratorSummaryInfo> *genlist); 83 : void GetGeneratorUVEInfo(std::vector<ModuleServerState> &genlist); 84 : bool SendRemote(const std::string& destination, 85 : const std::string &dec_sandesh); 86 : 87 : struct QueueType { 88 : enum type { 89 : Db, 90 : Sm, 91 : }; 92 : }; 93 : void SetDbQueueWaterMarkInfo(Sandesh::QueueWaterMarkInfo &wm); 94 : void ResetDbQueueWaterMarkInfo(); 95 : void GetDbQueueWaterMarkInfo( 96 : std::vector<Sandesh::QueueWaterMarkInfo> &wm_info) const; 97 : void SetSmQueueWaterMarkInfo(Sandesh::QueueWaterMarkInfo &wm); 98 : void ResetSmQueueWaterMarkInfo(); 99 : void GetSmQueueWaterMarkInfo( 100 : std::vector<Sandesh::QueueWaterMarkInfo> &wm_info) const; 101 : DbHandlerPtr GetDbHandlerPtr(); 102 : void GetQueueWaterMarkInfo(QueueType::type type, 103 : std::vector<Sandesh::QueueWaterMarkInfo> &wm_info) const; 104 : 105 0 : OpServerProxy * GetOSP() const { return osp_; } 106 0 : EventManager * event_manager() const { return evm_; } 107 0 : VizCallback ProcessSandeshMsgCb() const { return cb_; } 108 : void RedisUpdate(bool rsc); 109 : 110 : static const std::string &GetProgramName() { return prog_name_; }; 111 : static void SetProgramName(const char *name) { prog_name_ = name; }; 112 : static std::string GetSelfIp() { return self_ip_; } 113 : static void SetSelfIp(std::string ip) { self_ip_ = ip; } 114 : 115 : int db_task_id(); 116 : const CollectorStats &GetStats() const { return stats_; } 117 : void SendGeneratorStatistics(); 118 : 119 : const static std::string DbGlobalName(bool dup=false, 120 : const std::string &host_ip="127.0.0.1"); 121 : void CloseGeneratorSession(std::string source, std::string module, 122 : std::string instance, std::string node_type); 123 : protected: 124 : virtual SslSession *AllocSession(SslSocket *socket); 125 : virtual void DisconnectSession(SandeshSession *session); 126 : 127 : private: 128 : void SetQueueWaterMarkInfo(QueueType::type type, 129 : Sandesh::QueueWaterMarkInfo &wm); 130 : void ResetQueueWaterMarkInfo(QueueType::type type); 131 : 132 0 : void inline increment_no_session_error() { 133 0 : stats_.no_session_error++; 134 0 : } 135 0 : void inline increment_no_generator_error() { 136 0 : stats_.no_generator_error++; 137 0 : } 138 0 : void inline increment_session_mismatch_error() { 139 0 : stats_.session_mismatch_error++; 140 0 : } 141 0 : void inline increment_redis_error() { 142 0 : stats_.redis_error++; 143 0 : } 144 0 : void inline increment_sandesh_type_mismatch_error() { 145 0 : stats_.sandesh_type_mismatch_error++; 146 0 : } 147 : 148 : DbHandlerPtr db_handler_; 149 : OpServerProxy * const osp_; 150 : EventManager * const evm_; 151 : VizCallback cb_; 152 : 153 : int db_task_id_; 154 : 155 : // SandeshGenerator map 156 : typedef boost::ptr_map<SandeshGenerator::GeneratorId, SandeshGenerator> GeneratorMap; 157 : mutable std::mutex gen_map_mutex_; 158 : GeneratorMap gen_map_; 159 : 160 : // Random generator for UUIDs 161 : ThreadSafeUuidGenerator umn_gen_; 162 : CollectorStats stats_; 163 : std::vector<Sandesh::QueueWaterMarkInfo> db_queue_wm_info_; 164 : std::vector<Sandesh::QueueWaterMarkInfo> sm_queue_wm_info_; 165 : static std::string prog_name_; 166 : static std::string self_ip_; 167 : static bool task_policy_set_; 168 : static const std::vector<Sandesh::QueueWaterMarkInfo> kDbQueueWaterMarkInfo; 169 : static const std::vector<Sandesh::QueueWaterMarkInfo> kSmQueueWaterMarkInfo; 170 : 171 : DISALLOW_COPY_AND_ASSIGN(Collector); 172 : }; 173 : 174 : class VizSession : public SandeshSession { 175 : public: 176 0 : VizSession(SslServer *client, SslSocket *socket, int task_instance, 177 0 : int writer_task_id, int reader_task_id) : 178 : SandeshSession(client, socket, task_instance, writer_task_id, 179 : reader_task_id), 180 0 : gen_(NULL) { } 181 0 : void set_generator(SandeshGenerator *gen) { gen_ = gen; } 182 0 : SandeshGenerator* generator() { return gen_; } 183 : private: 184 : SandeshGenerator *gen_; 185 : }; 186 : 187 : #endif /* COLLECTOR_H_ */