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