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 "../utils.h"
8 : #include "../utils_memcached.h"
9 : #include "../utils_mongodb.h"
10 : #include "../utils_thrift.h"
11 : #include "UserHandler.h"
12 :
13 : extern "C" void __gcov_flush();
14 :
15 : using apache::thrift::protocol::TBinaryProtocolFactory;
16 : using apache::thrift::server::TThreadedServer;
17 : using apache::thrift::transport::TFramedTransportFactory;
18 : using apache::thrift::transport::TServerSocket;
19 : using namespace social_network;
20 :
21 0 : void sigintHandler(int sig) { exit(EXIT_SUCCESS); }
22 :
23 1 : void handle_sigusr1(int signum) {
24 1 : __gcov_flush();
25 0 : }
26 :
27 1 : int main(int argc, char *argv[]) {
28 1 : signal(SIGINT, sigintHandler);
29 1 : signal(SIGUSR1, handle_sigusr1);
30 1 : init_logger();
31 :
32 1 : SetUpTracer("config/jaeger-config.yml", "user-service");
33 :
34 1 : json config_json;
35 1 : if (load_config_file("config/service-config.json", &config_json) != 0) {
36 0 : exit(EXIT_FAILURE);
37 : }
38 :
39 1 : std::string secret = config_json["secret"];
40 :
41 1 : int port = config_json["user-service"]["port"];
42 :
43 1 : std::string social_graph_addr = config_json["social-graph-service"]["addr"];
44 1 : int social_graph_port = config_json["social-graph-service"]["port"];
45 1 : int social_graph_conns = config_json["social-graph-service"]["connections"];
46 1 : int social_graph_timeout = config_json["social-graph-service"]["timeout_ms"];
47 : int social_graph_keepalive =
48 1 : config_json["social-graph-service"]["keepalive_ms"];
49 :
50 1 : int mongodb_conns = config_json["user-mongodb"]["connections"];
51 1 : int mongodb_timeout = config_json["user-mongodb"]["timeout_ms"];
52 :
53 1 : int memcached_conns = config_json["user-memcached"]["connections"];
54 1 : int memcached_timeout = config_json["user-memcached"]["timeout_ms"];
55 :
56 : memcached_pool_st *memcached_client_pool =
57 1 : init_memcached_client_pool(config_json, "user", 32, memcached_conns);
58 : mongoc_client_pool_t *mongodb_client_pool =
59 1 : init_mongodb_client_pool(config_json, "user", mongodb_conns);
60 :
61 1 : if (memcached_client_pool == nullptr || mongodb_client_pool == nullptr) {
62 0 : return EXIT_FAILURE;
63 : }
64 :
65 1 : std::string netif = config_json["user-service"]["netif"];
66 1 : std::string machine_id = GetMachineId(netif);
67 1 : if (machine_id == "") {
68 0 : exit(EXIT_FAILURE);
69 : }
70 2 : LOG(info) << "machine_id = " << machine_id;
71 :
72 1 : std::mutex thread_lock;
73 :
74 : ClientPool<ThriftClient<SocialGraphServiceClient>> social_graph_client_pool(
75 : "social-graph", social_graph_addr, social_graph_port, 0,
76 1 : social_graph_conns, social_graph_timeout, social_graph_keepalive, config_json);
77 :
78 1 : mongoc_client_t *mongodb_client = mongoc_client_pool_pop(mongodb_client_pool);
79 1 : if (!mongodb_client) {
80 0 : LOG(fatal) << "Failed to pop mongoc client";
81 0 : return EXIT_FAILURE;
82 : }
83 1 : bool r = false;
84 3 : while (!r) {
85 1 : r = CreateIndex(mongodb_client, "user", "user_id", true);
86 1 : if (!r) {
87 0 : LOG(error) << "Failed to create mongodb index, try again";
88 0 : sleep(1);
89 : }
90 : }
91 1 : mongoc_client_pool_push(mongodb_client_pool, mongodb_client);
92 1 : std::shared_ptr<TServerSocket> server_socket = get_server_socket(config_json, "0.0.0.0", port);
93 :
94 : TThreadedServer server(
95 2 : std::make_shared<UserServiceProcessor>(std::make_shared<UserHandler>(
96 : &thread_lock, machine_id, secret, memcached_client_pool,
97 : mongodb_client_pool, &social_graph_client_pool)),
98 : server_socket,
99 2 : std::make_shared<TFramedTransportFactory>(),
100 6 : std::make_shared<TBinaryProtocolFactory>());
101 2 : LOG(info) << "Starting the user-service server ...";
102 1 : server.serve();
103 3 : }
|