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 "PostStorageHandler.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 : static memcached_pool_st* memcached_client_pool;
22 : static mongoc_client_pool_t* mongodb_client_pool;
23 :
24 0 : void sigintHandler(int sig) {
25 0 : if (memcached_client_pool != nullptr) {
26 0 : memcached_pool_destroy(memcached_client_pool);
27 : }
28 0 : if (mongodb_client_pool != nullptr) {
29 0 : mongoc_client_pool_destroy(mongodb_client_pool);
30 : }
31 0 : exit(EXIT_SUCCESS);
32 : }
33 :
34 1 : void handle_sigusr1(int signum) {
35 1 : __gcov_flush();
36 0 : }
37 :
38 1 : int main(int argc, char* argv[]) {
39 1 : signal(SIGINT, sigintHandler);
40 1 : signal(SIGUSR1, handle_sigusr1);
41 1 : init_logger();
42 1 : SetUpTracer("config/jaeger-config.yml", "post-storage-service");
43 :
44 1 : json config_json;
45 1 : if (load_config_file("config/service-config.json", &config_json) != 0) {
46 0 : exit(EXIT_FAILURE);
47 : }
48 :
49 1 : int port = config_json["post-storage-service"]["port"];
50 :
51 1 : int mongodb_conns = config_json["post-storage-mongodb"]["connections"];
52 1 : int mongodb_timeout = config_json["post-storage-mongodb"]["timeout_ms"];
53 :
54 1 : int memcached_conns = config_json["post-storage-memcached"]["connections"];
55 1 : int memcached_timeout = config_json["post-storage-memcached"]["timeout_ms"];
56 :
57 2 : memcached_client_pool = init_memcached_client_pool(
58 1 : config_json, "post-storage", 32, memcached_conns);
59 : mongodb_client_pool =
60 1 : init_mongodb_client_pool(config_json, "post-storage", mongodb_conns);
61 1 : if (memcached_client_pool == nullptr || mongodb_client_pool == nullptr) {
62 0 : return EXIT_FAILURE;
63 : }
64 :
65 1 : mongoc_client_t* mongodb_client = mongoc_client_pool_pop(mongodb_client_pool);
66 1 : if (!mongodb_client) {
67 0 : LOG(fatal) << "Failed to pop mongoc client";
68 0 : return EXIT_FAILURE;
69 : }
70 1 : bool r = false;
71 3 : while (!r) {
72 1 : r = CreateIndex(mongodb_client, "post", "post_id", true);
73 1 : if (!r) {
74 0 : LOG(error) << "Failed to create mongodb index, try again";
75 0 : sleep(1);
76 : }
77 : }
78 1 : mongoc_client_pool_push(mongodb_client_pool, mongodb_client);
79 1 : std::shared_ptr<TServerSocket> server_socket = get_server_socket(config_json, "0.0.0.0", port);
80 :
81 2 : TThreadedServer server(std::make_shared<PostStorageServiceProcessor>(
82 2 : std::make_shared<PostStorageHandler>(
83 : memcached_client_pool, mongodb_client_pool)),
84 : server_socket,
85 2 : std::make_shared<TFramedTransportFactory>(),
86 6 : std::make_shared<TBinaryProtocolFactory>());
87 :
88 2 : LOG(info) << "Starting the post-storage-service server...";
89 1 : server.serve();
90 3 : }
|