Line data Source code
1 : /*
2 : * 64-bit Unique Id Generator
3 : *
4 : * ------------------------------------------------------------------------
5 : * |0| 11 bit machine ID | 40-bit timestamp | 12-bit counter |
6 : * ------------------------------------------------------------------------
7 : *
8 : * 11-bit machine Id code by hasing the MAC address
9 : * 40-bit UNIX timestamp in millisecond precision with custom epoch
10 : * 12 bit counter which increases monotonically on single process
11 : *
12 : */
13 :
14 : #include <signal.h>
15 : #include <thrift/protocol/TBinaryProtocol.h>
16 : #include <thrift/server/TThreadedServer.h>
17 : #include <thrift/transport/TBufferTransports.h>
18 : #include <thrift/transport/TServerSocket.h>
19 :
20 : #include "../utils.h"
21 : #include "../utils_thrift.h"
22 : #include "UniqueIdHandler.h"
23 :
24 : extern "C" void __gcov_flush();
25 :
26 : using apache::thrift::protocol::TBinaryProtocolFactory;
27 : using apache::thrift::server::TThreadedServer;
28 : using apache::thrift::transport::TFramedTransportFactory;
29 : using apache::thrift::transport::TServerSocket;
30 : using namespace social_network;
31 :
32 0 : void sigintHandler(int sig) { exit(EXIT_SUCCESS); }
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", "unique-id-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["unique-id-service"]["port"];
50 1 : std::string netif = config_json["unique-id-service"]["netif"];
51 :
52 1 : std::string machine_id = GetMachineId(netif);
53 1 : if (machine_id == "") {
54 0 : exit(EXIT_FAILURE);
55 : }
56 2 : LOG(info) << "machine_id = " << machine_id;
57 :
58 1 : std::mutex thread_lock;
59 1 : std::shared_ptr<TServerSocket> server_socket = get_server_socket(config_json, "0.0.0.0", port);
60 : TThreadedServer server(
61 2 : std::make_shared<UniqueIdServiceProcessor>(
62 2 : std::make_shared<UniqueIdHandler>(&thread_lock, machine_id)),
63 : server_socket,
64 2 : std::make_shared<TFramedTransportFactory>(),
65 6 : std::make_shared<TBinaryProtocolFactory>());
66 :
67 2 : LOG(info) << "Starting the unique-id-service server ...";
68 1 : server.serve();
69 3 : }
|