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