|
| 1 | +#include "redis.h" |
| 2 | +#include "dsdc_sunrpc.h" |
| 3 | +#include "sha1.h" |
| 4 | + |
| 5 | +//----------------------------------------------------------------------------- |
| 6 | + |
| 7 | +void* dsdcHeartbeatLoop(void* arg); |
| 8 | +CLIENT* dsdcMasterConnection(int index); |
| 9 | +void dsdcMakekey(dsdc_key_t* out, const char* hostname, int port, int id); |
| 10 | +void dsdcSpawnMasterThread(int index); |
| 11 | + |
| 12 | +//----------------------------------------------------------------------------- |
| 13 | + |
| 14 | +void dsdcInit(void) { |
| 15 | + for (int i = 0; i < server.dsdc_master_count; i++) { |
| 16 | + dsdcSpawnMasterThread(i); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +//----------------------------------------------------------------------------- |
| 21 | + |
| 22 | +void dsdcSpawnMasterThread(int index) { |
| 23 | + pthread_attr_t attr; |
| 24 | + pthread_t thread; |
| 25 | + |
| 26 | + CLIENT* clnt = dsdcMasterConnection(index); |
| 27 | + |
| 28 | + pthread_attr_init(&attr); |
| 29 | + if (pthread_create(&thread,&attr,dsdcHeartbeatLoop,clnt) != 0) { |
| 30 | + redisLog(REDIS_WARNING,"Fatal: Can't initialize DSDC Heartbeat."); |
| 31 | + exit(1); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +//----------------------------------------------------------------------------- |
| 36 | + |
| 37 | +CLIENT* dsdcMasterConnection(int index) { |
| 38 | + char myname[256]; |
| 39 | + char* master_host = server.dsdc_masters[index]; |
| 40 | + int num_keys = server.dsdc_num_keys; |
| 41 | + |
| 42 | + CLIENT* clnt = clnt_create(master_host, DSDC_PROG, DSDC_VERS, "tcp"); |
| 43 | + |
| 44 | + if (clnt) { |
| 45 | + printf("dsdc: connected to master: %s\n", master_host); |
| 46 | + } else { |
| 47 | + clnt_pcreateerror(master_host); |
| 48 | + exit(1); |
| 49 | + } |
| 50 | + |
| 51 | + dsdc_register2_arg_t reg_arg; |
| 52 | + reg_arg.primary = TRUE; |
| 53 | + reg_arg.lock_server = FALSE; |
| 54 | + |
| 55 | + gethostname(myname, 256); |
| 56 | + |
| 57 | + dsdcx_slave2_t slave; |
| 58 | + slave.hostname = myname; |
| 59 | + slave.port = server.port; |
| 60 | + slave.slave_type = DSDC_REDIS_SLAVE; |
| 61 | + |
| 62 | + dsdc_keyset_t keys; |
| 63 | + keys.dsdc_keyset_t_val = zmalloc(sizeof(dsdc_key_t) * num_keys); |
| 64 | + keys.dsdc_keyset_t_len = num_keys; |
| 65 | + |
| 66 | + for (int i = 0; i < num_keys; i++) { |
| 67 | + dsdcMakekey(&keys.dsdc_keyset_t_val[i], myname, server.port, i); |
| 68 | + } |
| 69 | + |
| 70 | + slave.keys = keys; |
| 71 | + reg_arg.slave = slave; |
| 72 | + |
| 73 | + dsdc_res_t* res = dsdc_register2_1(®_arg, clnt); |
| 74 | + if (!(res && *res == DSDC_OK)) { |
| 75 | + clnt_perror(clnt, "dsdc: registration failed"); |
| 76 | + exit(1); |
| 77 | + } |
| 78 | + |
| 79 | + return clnt; |
| 80 | +} |
| 81 | + |
| 82 | +//----------------------------------------------------------------------------- |
| 83 | + |
| 84 | +void* dsdcHeartbeatLoop(void* arg) { |
| 85 | + |
| 86 | + CLIENT* clnt = (CLIENT*) arg; |
| 87 | + |
| 88 | + while (TRUE) { |
| 89 | + dsdc_res_t* res = dsdc_heartbeat_1(NULL, clnt); |
| 90 | + if (!res) { |
| 91 | + printf("dsdc: heartbeat failed\n"); |
| 92 | + } |
| 93 | + |
| 94 | + sleep(1); |
| 95 | + } |
| 96 | + |
| 97 | + return NULL; |
| 98 | +} |
| 99 | + |
| 100 | +//----------------------------------------------------------------------------- |
| 101 | + |
| 102 | +void dsdcMakekey(dsdc_key_t* out, const char* hostname, int port, |
| 103 | + int index) { |
| 104 | + |
| 105 | + char buffer[1024]; |
| 106 | + SHA1_CTX ctx; |
| 107 | + |
| 108 | + int len = snprintf(buffer, 1024, "%s-%d-%d", hostname, port, index); |
| 109 | + |
| 110 | + SHA1Init(&ctx); |
| 111 | + SHA1Update(&ctx, (unsigned char*) buffer, len); |
| 112 | + SHA1Final( (unsigned char*) out, &ctx); |
| 113 | + |
| 114 | +} |
0 commit comments