LCOV - code coverage report
Current view: top level - src/HomeTimelineService - HomeTimelineService.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 47 70 67.1 %
Date: 2025-11-04 02:46:59 Functions: 4 5 80.0 %

          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 "../ClientPool.h"
      10             : #include "../logger.h"
      11             : #include "../tracing.h"
      12             : #include "../utils.h"
      13             : #include "../utils_redis.h"
      14             : #include "../utils_thrift.h"
      15             : #include "HomeTimelineHandler.h"
      16             : 
      17             : extern "C" void __gcov_flush();
      18             : 
      19             : using apache::thrift::protocol::TBinaryProtocolFactory;
      20             : using apache::thrift::server::TThreadedServer;
      21             : using apache::thrift::transport::TFramedTransportFactory;
      22             : using apache::thrift::transport::TServerSocket;
      23             : using namespace social_network;
      24             : 
      25           0 : void sigintHandler(int sig) { exit(EXIT_SUCCESS); }
      26             : 
      27           1 : void handle_sigusr1(int signum) {
      28           1 :   __gcov_flush();
      29           0 : }
      30             : 
      31           1 : int main(int argc, char *argv[]) {
      32           1 :   signal(SIGINT, sigintHandler);
      33           1 :   signal(SIGUSR1, handle_sigusr1);
      34           1 :   init_logger();
      35             : 
      36             :   // Command line options
      37             :   namespace po = boost::program_options;
      38           1 :   po::options_description desc("Options");
      39           2 :   desc.add_options()("help", "produce help message")(
      40             :       "redis-cluster",
      41           2 :       po::value<bool>()->default_value(false)->implicit_value(true),
      42           1 :       "Enable redis cluster mode");
      43             : 
      44           1 :   po::variables_map vm;
      45           1 :   po::store(po::parse_command_line(argc, argv, desc), vm);
      46           1 :   po::notify(vm);
      47             : 
      48           1 :   if (vm.count("help")) {
      49           0 :     std::cout << desc << "\n";
      50           0 :     return 0;
      51             :   }
      52             : 
      53           1 :   bool redis_cluster_flag = false;
      54           1 :   if (vm.count("redis-cluster")) {
      55           1 :     if (vm["redis-cluster"].as<bool>()) {
      56           0 :       redis_cluster_flag = true;
      57             :     }
      58             :   }
      59             : 
      60           1 :   SetUpTracer("config/jaeger-config.yml", "home-timeline-service");
      61             : 
      62           1 :   json config_json;
      63           1 :   if (load_config_file("config/service-config.json", &config_json) != 0) {
      64           0 :     exit(EXIT_FAILURE);
      65             :   }
      66             : 
      67           1 :   int port = config_json["home-timeline-service"]["port"];
      68           1 :   int redis_cluster_config_flag = config_json["home-timeline-redis"]["use_cluster"];
      69             : 
      70           1 :   int redis_replica_config_flag = config_json["home-timeline-redis"]["use_replica"];
      71             : 
      72           1 :   int post_storage_port = config_json["post-storage-service"]["port"];
      73           1 :   std::string post_storage_addr = config_json["post-storage-service"]["addr"];
      74           1 :   int post_storage_conns = config_json["post-storage-service"]["connections"];
      75           1 :   int post_storage_timeout = config_json["post-storage-service"]["timeout_ms"];
      76             :   int post_storage_keepalive =
      77           1 :       config_json["post-storage-service"]["keepalive_ms"];
      78             : 
      79           1 :   int social_graph_port = config_json["social-graph-service"]["port"];
      80           1 :   std::string social_graph_addr = config_json["social-graph-service"]["addr"];
      81           1 :   int social_graph_conns = config_json["social-graph-service"]["connections"];
      82           1 :   int social_graph_timeout = config_json["social-graph-service"]["timeout_ms"];
      83             :   int social_graph_keepalive =
      84           1 :       config_json["social-graph-service"]["keepalive_ms"];
      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<PostStorageServiceClient>> post_storage_client_pool(
      92             :       "post-storage-client", post_storage_addr, post_storage_port, 0,
      93             :       post_storage_conns, post_storage_timeout, post_storage_keepalive,
      94           1 :       config_json);
      95             : 
      96             :   ClientPool<ThriftClient<SocialGraphServiceClient>> social_graph_client_pool(
      97             :       "social-graph-client", social_graph_addr, social_graph_port, 0,
      98             :       social_graph_conns, social_graph_timeout, social_graph_keepalive,
      99           1 :       config_json);
     100             : 
     101             :   std::shared_ptr<TServerSocket> server_socket =
     102           1 :       get_server_socket(config_json, "0.0.0.0", port);
     103             : 
     104             : 
     105           1 :   if (redis_replica_config_flag) {
     106           0 :           Redis redis_replica_client_pool = init_redis_replica_client_pool(config_json, "redis-replica");
     107           0 :           Redis redis_primary_client_pool = init_redis_replica_client_pool(config_json, "redis-primary");
     108             : 
     109             :           TThreadedServer server(
     110           0 :               std::make_shared<HomeTimelineServiceProcessor>(
     111           0 :                   std::make_shared<HomeTimelineHandler>(&redis_replica_client_pool,
     112             :                       &redis_primary_client_pool,
     113             :                       &post_storage_client_pool,
     114             :                       &social_graph_client_pool)),
     115           0 :               server_socket, std::make_shared<TFramedTransportFactory>(),
     116           0 :               std::make_shared<TBinaryProtocolFactory>());
     117             : 
     118           0 :           LOG(info) << "Starting the home-timeline-service server with replicated Redis support...";
     119           0 :           server.serve();
     120             : 
     121             :       
     122             :   }
     123             : 
     124           1 :   else if (redis_cluster_flag || redis_cluster_config_flag) {
     125             :     RedisCluster redis_cluster_client_pool =
     126           0 :         init_redis_cluster_client_pool(config_json, "home-timeline");
     127             :     TThreadedServer server(
     128           0 :         std::make_shared<HomeTimelineServiceProcessor>(
     129           0 :             std::make_shared<HomeTimelineHandler>(&redis_cluster_client_pool,
     130             :                                                   &post_storage_client_pool,
     131             :                                                   &social_graph_client_pool)),
     132           0 :         server_socket, std::make_shared<TFramedTransportFactory>(),
     133           0 :         std::make_shared<TBinaryProtocolFactory>());
     134             : 
     135           0 :     LOG(info) << "Starting the home-timeline-service server with Redis Cluster support...";
     136           0 :     server.serve();
     137             :   } else {
     138             :     Redis redis_client_pool =
     139           1 :         init_redis_client_pool(config_json, "home-timeline");
     140             :     TThreadedServer server(
     141           2 :         std::make_shared<HomeTimelineServiceProcessor>(
     142           2 :             std::make_shared<HomeTimelineHandler>(&redis_client_pool,
     143             :                                                   &post_storage_client_pool,
     144             :                                                   &social_graph_client_pool)),
     145           2 :         server_socket, std::make_shared<TFramedTransportFactory>(),
     146           6 :         std::make_shared<TBinaryProtocolFactory>());
     147             : 
     148           2 :     LOG(info) << "Starting the home-timeline-service server...";
     149           1 :     server.serve();
     150             :   }
     151           3 : }

Generated by: LCOV version 1.12