From d0fbf44d18d3645e349240e72ce7894a4b1af296 Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 14 Feb 2018 21:52:40 +0800 Subject: [PATCH] For #464, support config for origin cluster. --- trunk/conf/full.conf | 47 +++++++++++++++---- trunk/conf/origin.cluster.serverA.conf | 19 ++++++++ trunk/conf/origin.cluster.serverB.conf | 19 ++++++++ trunk/src/app/srs_app_config.cpp | 29 +++++++++++- trunk/src/app/srs_app_config.hpp | 64 ++++++++++++++------------ trunk/src/core/srs_core.hpp | 2 +- 6 files changed, 140 insertions(+), 40 deletions(-) create mode 100644 trunk/conf/origin.cluster.serverA.conf create mode 100644 trunk/conf/origin.cluster.serverB.conf diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index 492253f515..64f8bfde83 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -329,20 +329,21 @@ vhost removed.srs.com { # vhost for stream cluster for RTMP/FLV vhost cluster.srs.com { - # stream RTMP/FLV cluster config. + # The config for cluster. cluster { - # the mode of vhost, local or remote. - # local: vhost is origin vhost, which provides stream source. - # remote: vhost is edge vhost, which pull/push to origin. + # The cluster mode, local or remote. + # local: It's an origin server, serve streams itself. + # remote: It's an edge server, fetch or push stream to origin server. # default: local mode remote; - # for edge(remote mode), user must specifies the origin server + + # For edge(mode remote), user must specifies the origin server # format as: [:port] # @remark user can specifies multiple origin for error backup, by space, # for example, 192.168.1.100:1935 192.168.1.101:1935 192.168.1.102:1935 origin 127.0.0.1:1935 localhost:1935; - # for edge, whether open the token traverse mode, + # For edge(mode remote), whether open the token traverse mode, # if token traverse on, all connections of edge will forward to origin to check(auth), # it's very important for the edge to do the token auth. # the better way is use http callback to do the token auth by the edge, @@ -350,19 +351,24 @@ vhost cluster.srs.com { # default: off token_traverse off; - # the vhost to transform for edge, + # For edge(mode remote), the vhost to transform for edge, # to fetch from the specified vhost at origin, # if not specified, use the current vhost of edge in origin, the variable [vhost]. # default: [vhost] vhost same.edge.srs.com; - # when upnode(forward to, edge push to, edge pull from) is srs, + # For edge(mode remote), when upnode(forward to, edge push to, edge pull from) is srs, # it's strongly recommend to open the debug_srs_upnode, # when connect to upnode, it will take the debug info, # for example, the id, source id, pid. # please see: https://github.com/ossrs/srs/wiki/v1_CN_SrsLog # default: on debug_srs_upnode on; + + # For origin (mode local) cluster, the co-worker's HTTP APIs. + # This origin will connect to co-workers and communicate with them. + # please read: https://github.com/ossrs/srs/wiki/v3_EN_OriginCluster + coworkers 127.0.0.1:9091 127.0.0.1:9092; } } @@ -1782,3 +1788,28 @@ vhost stream.transcode.srs.com { } } } + +############################################################################################# +# The origin cluster section +############################################################################################# +http_api { + enabled on; + listen 9090; +} +vhost a.origin.cluster.srs.com { + cluster { + mode local; + coworkers 127.0.0.1:9091; + } +} + +http_api { + enabled on; + listen 9091; +} +vhost b.origin.cluster.srs.com { + cluster { + mode local; + coworkers 127.0.0.1:9090; + } +} diff --git a/trunk/conf/origin.cluster.serverA.conf b/trunk/conf/origin.cluster.serverA.conf new file mode 100644 index 0000000000..6013cfc6f5 --- /dev/null +++ b/trunk/conf/origin.cluster.serverA.conf @@ -0,0 +1,19 @@ +# the config for srs origin-origin cluster +# @see https://github.com/ossrs/srs/wiki/v3_EN_OriginCluster +# @see full.conf for detail config. + +listen 19350; +max_connections 1000; +daemon off; +srs_log_tank console; +pid ./objs/origin.cluster.serverA.pid; +http_api { + enabled on; + listen 9090; +} +vhost __defaultVhost__ { + cluster { + mode local; + coworkers 127.0.0.1:9091; + } +} diff --git a/trunk/conf/origin.cluster.serverB.conf b/trunk/conf/origin.cluster.serverB.conf new file mode 100644 index 0000000000..1423fd9ceb --- /dev/null +++ b/trunk/conf/origin.cluster.serverB.conf @@ -0,0 +1,19 @@ +# the config for srs origin-origin cluster +# @see https://github.com/ossrs/srs/wiki/v3_EN_OriginCluster +# @see full.conf for detail config. + +listen 19351; +max_connections 1000; +daemon off; +srs_log_tank console; +pid ./objs/origin.cluster.serverB.pid; +http_api { + enabled on; + listen 9091; +} +vhost __defaultVhost__ { + cluster { + mode local; + coworkers 127.0.0.1:9090; + } +} diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index a13e57fbbb..547a4a792b 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -3726,7 +3726,7 @@ srs_error_t SrsConfig::check_normal_config() } else if (n == "cluster") { for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name; - if (m != "mode" && m != "origin" && m != "token_traverse" && m != "vhost" && m != "debug_srs_upnode") { + if (m != "mode" && m != "origin" && m != "token_traverse" && m != "vhost" && m != "debug_srs_upnode" && m != "coworkers") { return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.cluster.%s of %s", m.c_str(), vhost->arg0().c_str()); } } @@ -5157,6 +5157,33 @@ string SrsConfig::get_vhost_edge_transform_vhost(string vhost) return conf->arg0(); } +vector SrsConfig::get_vhost_coworkers(string vhost) +{ + vector coworkers; + + SrsConfDirective* conf = get_vhost(vhost); + if (!conf) { + return coworkers; + } + + conf = conf->get("cluster"); + if (!conf) { + return coworkers; + } + + conf = conf->get("coworkers"); + for (int i = 0; i < (int)conf->directives.size(); i++) { + SrsConfDirective* option = conf->directives[i]; + if (!option) { + continue; + } + + coworkers.push_back(option->arg0()); + } + + return coworkers; +} + bool SrsConfig::get_security_enabled(string vhost) { static bool DEFAULT = false; diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 307617f42e..348ec489a7 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -190,7 +190,7 @@ class SrsConfDirective virtual SrsConfDirective* copy(); // @param except the name of sub directive. virtual SrsConfDirective* copy(std::string except); - // args +// args public: /** * get the args0,1,2, if user want to get more args, @@ -200,7 +200,7 @@ class SrsConfDirective virtual std::string arg1(); virtual std::string arg2(); virtual std::string arg3(); - // directives +// directives public: /** * get the directive by index. @@ -224,7 +224,7 @@ class SrsConfDirective * remove the v from sub directives, user must free the v. */ virtual void remove(SrsConfDirective* v); - // help utilities +// help utilities public: /** * whether current directive is vhost. @@ -234,7 +234,7 @@ class SrsConfDirective * whether current directive is stream_caster. */ virtual bool is_stream_caster(); - // parse utilities +// parse utilities public: /** * parse config directive from file buffer. @@ -256,7 +256,7 @@ class SrsConfDirective virtual SrsJsonAny* dumps_arg0_to_integer(); virtual SrsJsonAny* dumps_arg0_to_number(); virtual SrsJsonAny* dumps_arg0_to_boolean(); - // private parse. +// private parse. private: /** * the directive parsing type. @@ -297,7 +297,7 @@ class SrsConfDirective */ class SrsConfig { - // user command +// user command private: /** * whether srs is run in dolphin mode. @@ -322,7 +322,7 @@ class SrsConfig * whether show SRS signature and exit. */ bool show_signature; - // global env variables. +// global env variables. private: /** * the user parameters, the argc and argv. @@ -345,7 +345,7 @@ class SrsConfig * the directive root. */ SrsConfDirective* root; - // reload section +// reload section private: /** * the reload subscribers, when reload, callback all handlers. @@ -362,7 +362,7 @@ class SrsConfig virtual bool is_dolphin(); private: virtual void set_config_directive(SrsConfDirective* parent, std::string dir, std::string value); - // reload +// reload public: /** * for reload handler to register itself, @@ -564,7 +564,7 @@ class SrsConfig * get the cli, the main(argc,argv), program start command. */ virtual std::string argv(); - // global section +// global section public: /** * get the directive root, corresponding to the config file. @@ -619,7 +619,7 @@ class SrsConfig virtual std::string get_work_dir(); // whether use asprocess mode. virtual bool get_asprocess(); - // stream_caster section +// stream_caster section public: /** * get all stream_caster in config file. @@ -649,7 +649,7 @@ class SrsConfig * get the max udp port for rtp of stream caster rtsp. */ virtual int get_stream_caster_rtp_port_max(SrsConfDirective* conf); - // kafka section. +// kafka section. public: /** * whether the kafka enabled. @@ -663,7 +663,7 @@ class SrsConfig * get the kafka topic to use for srs. */ virtual std::string get_kafka_topic(); - // vhost specified section +// vhost specified section public: /** * get the vhost directive by vhost name. @@ -826,7 +826,7 @@ class SrsConfig * get the forward directive of vhost. */ virtual SrsConfDirective* get_forwards(std::string vhost); - // http_hooks section +// http_hooks section private: /** * get the http_hooks directive of vhost. @@ -883,7 +883,7 @@ class SrsConfig * @return the on_hls_notify callback directive, the args is the url to callback. */ virtual SrsConfDirective* get_vhost_on_hls_notify(std::string vhost); - // bwct(bandwidth check tool) section +// bwct(bandwidth check tool) section public: /** * whether bw check enabled for vhost. @@ -908,7 +908,7 @@ class SrsConfig * @remark this is used to protect the service bandwidth. */ virtual int get_bw_check_limit_kbps(std::string vhost); - // vhost cluster section +// vhost cluster section public: /** * whether vhost is edge mode. @@ -939,7 +939,12 @@ class SrsConfig * @see https://github.com/ossrs/srs/issues/372 */ virtual std::string get_vhost_edge_transform_vhost(std::string vhost); - // vhost security section + /** + * Get the co-workers of origin cluster. + * @see https://github.com/ossrs/srs/wiki/v3_EN_OriginCluster + */ + virtual std::vector get_vhost_coworkers(std::string vhost); +// vhost security section public: /** * whether the secrity of vhost enabled. @@ -949,7 +954,7 @@ class SrsConfig * get the security rules. */ virtual SrsConfDirective* get_security_rules(std::string vhost); - // vhost transcode section +// vhost transcode section public: /** * get the transcode directive of vhost in specified scope. @@ -1070,7 +1075,7 @@ class SrsConfig * @remark, we will use some variable, for instance, [vhost] to substitude with vhost. */ virtual std::string get_engine_output(SrsConfDirective* conf); - // vhost exec secion +// vhost exec secion private: /** * get the exec directive of vhost. @@ -1111,7 +1116,7 @@ class SrsConfig * get the ingest input url. */ virtual std::string get_ingest_input_url(SrsConfDirective* conf); - // log section +// log section public: /** * whether log to file. @@ -1134,7 +1139,7 @@ class SrsConfig * @remark, /dev/null to disable it. */ virtual std::string get_ffmpeg_log_dir(); - // The MPEG-DASH section. +// The MPEG-DASH section. private: virtual SrsConfDirective* get_dash(std::string vhost); public: @@ -1150,7 +1155,7 @@ class SrsConfig virtual std::string get_dash_path(std::string vhost); // Get the path for DASH MPD, to generate the MPD file. virtual std::string get_dash_mpd_file(std::string vhost); - // hls section +// hls section private: /** * get the hls directive of vhost. @@ -1231,7 +1236,7 @@ class SrsConfig * that is, to read max bytes of the bytes from the callback, or timeout or error. */ virtual int get_vhost_hls_nb_notify(std::string vhost); - // hds section +// hds section private: /** * get the hds directive of vhost. @@ -1255,8 +1260,7 @@ class SrsConfig * a window is a set of hds fragments. */ virtual double get_hds_window(const std::string &vhost); - - // dvr section +// dvr section private: /** * get the dvr directive. @@ -1292,7 +1296,7 @@ class SrsConfig * get the time_jitter algorithm for dvr. */ virtual int get_dvr_time_jitter(std::string vhost); - // http api section +// http api section private: /** * whether http api enabled @@ -1327,7 +1331,7 @@ class SrsConfig * whether allow rpc update. */ virtual bool get_raw_api_allow_update(); - // http stream section +// http stream section private: /** * whether http stream enabled. @@ -1366,7 +1370,7 @@ class SrsConfig * the path on disk for mount root of http vhost. */ virtual std::string get_vhost_http_dir(std::string vhost); - // flv live streaming section +// flv live streaming section public: /** * get whether vhost enabled http flv live stream @@ -1381,7 +1385,7 @@ class SrsConfig * used to generate the flv stream mount path. */ virtual std::string get_vhost_http_remux_mount(std::string vhost); - // http heartbeart section +// http heartbeart section private: /** * get the heartbeat directive. @@ -1408,7 +1412,7 @@ class SrsConfig * whether report with summaries of http api: /api/v1/summaries. */ virtual bool get_heartbeat_summaries(); - // stats section +// stats section private: /** * get the stats directive. diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 0f9ef0b087..8d208fecd7 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -43,7 +43,7 @@ #define RTMP_SIG_SRS_ROLE "cluster" #define RTMP_SIG_SRS_URL "https://github.com/ossrs/srs" #define RTMP_SIG_SRS_LICENSE "The MIT License (MIT)" -#define RTMP_SIG_SRS_COPYRIGHT "Copyright (c) 2013-2017 " RTMP_SIG_SRS_KEY "(" RTMP_SIG_SRS_AUTHROS ")" +#define RTMP_SIG_SRS_COPYRIGHT "Copyright (c) 2013-2018 " RTMP_SIG_SRS_KEY "(" RTMP_SIG_SRS_AUTHROS ")" #define RTMP_SIG_SRS_PRIMARY RTMP_SIG_SRS_KEY "/" VERSION_STABLE_BRANCH #define RTMP_SIG_SRS_HANDSHAKE RTMP_SIG_SRS_KEY "(" RTMP_SIG_SRS_VERSION ")" #define RTMP_SIG_SRS_VERSION SRS_XSTR(VERSION_MAJOR) "." SRS_XSTR(VERSION_MINOR) "." SRS_XSTR(VERSION_REVISION)