Line data Source code
1 : #include <signal.h>
2 : #include <thrift/protocol/TBinaryProtocol.h>
3 : #include <thrift/server/TThreadedServer.h>
4 : #include <thrift/transport/TBufferTransports.h>
5 : #include <thrift/transport/TServerSocket.h>
6 :
7 : #include <boost/program_options.hpp>
8 :
9 : #include "../utils.h"
10 : #include "../utils_mongodb.h"
11 : #include "../utils_redis.h"
12 : #include "../utils_thrift.h"
13 : #include "SocialGraphHandler.h"
14 :
15 : extern "C" void __gcov_flush();
16 :
17 : using json = nlohmann::json;
18 : using apache::thrift::protocol::TBinaryProtocolFactory;
19 : using apache::thrift::server::TThreadedServer;
20 : using apache::thrift::transport::TFramedTransportFactory;
21 : using apache::thrift::transport::TServerSocket;
22 : using namespace social_network;
23 :
24 0 : void sigintHandler(int sig) { exit(EXIT_SUCCESS); }
25 :
26 1 : void handle_sigusr1(int signum) {
27 1 : __gcov_flush();
28 0 : }
29 :
30 1 : int main(int argc, char *argv[]) {
31 1 : signal(SIGINT, sigintHandler);
32 1 : signal(SIGUSR1, handle_sigusr1);
33 1 : init_logger();
34 :
35 : // Command line options
36 : namespace po = boost::program_options;
37 1 : po::options_description desc("Options");
38 2 : desc.add_options()("help", "produce help message")(
39 : "redis-cluster",
40 2 : po::value<bool>()->default_value(false)->implicit_value(true),
41 1 : "Enable redis cluster mode");
42 :
43 1 : po::variables_map vm;
44 1 : po::store(po::parse_command_line(argc, argv, desc), vm);
45 1 : po::notify(vm);
46 :
47 1 : if (vm.count("help")) {
48 0 : std::cout << desc << "\n";
49 0 : return 0;
50 : }
51 :
52 1 : bool redis_cluster_flag = false;
53 1 : if (vm.count("redis-cluster")) {
54 1 : if (vm["redis-cluster"].as<bool>()) {
55 0 : redis_cluster_flag = true;
56 : }
57 : }
58 :
59 1 : SetUpTracer("config/jaeger-config.yml", "social-graph-service");
60 :
61 1 : json config_json;
62 1 : if (load_config_file("config/service-config.json", &config_json) != 0) {
63 0 : exit(EXIT_FAILURE);
64 : }
65 :
66 1 : int port = config_json["social-graph-service"]["port"];
67 :
68 1 : int mongodb_conns = config_json["social-graph-mongodb"]["connections"];
69 1 : int mongodb_timeout = config_json["social-graph-mongodb"]["timeout_ms"];
70 :
71 1 : std::string user_addr = config_json["user-service"]["addr"];
72 1 : int user_port = config_json["user-service"]["port"];
73 1 : int user_conns = config_json["user-service"]["connections"];
74 1 : int user_timeout = config_json["user-service"]["timeout_ms"];
75 1 : int user_keepalive = config_json["user-service"]["keepalive_ms"];
76 :
77 1 : int redis_cluster_config_flag = config_json["social-graph-redis"]["use_cluster"];
78 1 : int redis_replica_config_flag = config_json["social-graph-redis"]["use_replica"];
79 : mongoc_client_pool_t *mongodb_client_pool =
80 1 : init_mongodb_client_pool(config_json, "social-graph", mongodb_conns);
81 :
82 1 : if (mongodb_client_pool == nullptr) {
83 0 : return EXIT_FAILURE;
84 : }
85 :
86 1 : if (redis_replica_config_flag && (redis_cluster_config_flag || redis_cluster_flag)) {
87 0 : LOG(error) << "Can't start service when Redis Cluster and Redis Replica are enabled at the same time";
88 0 : exit(EXIT_FAILURE);
89 : }
90 :
91 : ClientPool<ThriftClient<UserServiceClient>> user_client_pool(
92 : "social-graph", user_addr, user_port, 0, user_conns, user_timeout,
93 1 : user_keepalive, config_json);
94 :
95 1 : mongoc_client_t *mongodb_client = mongoc_client_pool_pop(mongodb_client_pool);
96 1 : if (!mongodb_client) {
97 0 : LOG(fatal) << "Failed to pop mongoc client";
98 0 : return EXIT_FAILURE;
99 : }
100 1 : bool r = false;
101 3 : while (!r) {
102 1 : r = CreateIndex(mongodb_client, "social-graph", "user_id", true);
103 1 : if (!r) {
104 0 : LOG(error) << "Failed to create mongodb index, try again";
105 0 : sleep(1);
106 : }
107 : }
108 1 : mongoc_client_pool_push(mongodb_client_pool, mongodb_client);
109 :
110 : std::shared_ptr<TServerSocket> server_socket =
111 1 : get_server_socket(config_json, "0.0.0.0", port);
112 :
113 1 : if (redis_cluster_flag || redis_cluster_config_flag) {
114 : RedisCluster redis_cluster_client_pool =
115 0 : init_redis_cluster_client_pool(config_json, "social-graph");
116 : TThreadedServer server(
117 0 : std::make_shared<SocialGraphServiceProcessor>(
118 0 : std::make_shared<SocialGraphHandler>(mongodb_client_pool,
119 : &redis_cluster_client_pool,
120 : &user_client_pool)),
121 0 : server_socket, std::make_shared<TFramedTransportFactory>(),
122 0 : std::make_shared<TBinaryProtocolFactory>());
123 0 : LOG(info) << "Starting the social-graph-service server with Redis Cluster support...";
124 0 : server.serve();
125 : }
126 :
127 1 : else if (redis_replica_config_flag) {
128 0 : Redis redis_replica_client_pool = init_redis_replica_client_pool(config_json, "redis-replica");
129 0 : Redis redis_primary_client_pool = init_redis_replica_client_pool(config_json, "redis-primary");
130 :
131 : TThreadedServer server(
132 0 : std::make_shared<SocialGraphServiceProcessor>(
133 0 : std::make_shared<SocialGraphHandler>(
134 : mongodb_client_pool, &redis_replica_client_pool, &redis_primary_client_pool, &user_client_pool)),
135 0 : server_socket, std::make_shared<TFramedTransportFactory>(),
136 0 : std::make_shared<TBinaryProtocolFactory>());
137 0 : LOG(info) << "Starting the social-graph-service server with Redis replica support";
138 0 : server.serve();
139 : }
140 :
141 : else {
142 : Redis redis_client_pool =
143 1 : init_redis_client_pool(config_json, "social-graph");
144 : TThreadedServer server(
145 2 : std::make_shared<SocialGraphServiceProcessor>(
146 2 : std::make_shared<SocialGraphHandler>(
147 : mongodb_client_pool, &redis_client_pool, &user_client_pool)),
148 2 : server_socket, std::make_shared<TFramedTransportFactory>(),
149 6 : std::make_shared<TBinaryProtocolFactory>());
150 2 : LOG(info) << "Starting the social-graph-service server ...";
151 1 : server.serve();
152 : }
153 3 : }
|