Advertisement
Guest User

Example

a guest
Aug 11th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int getMasterAddr(char *pntIpStr, char *pntPortStr)
  2. {
  3.     //    char ip[512];
  4.     //    char port[10];
  5.     char masterName[512];
  6.  
  7.     redisContext *redisSentinelContext = NULL, *redisMasterContext = NULL;
  8.     redisReply *reply, **pElement;
  9.     int element;
  10.     int nSent;
  11.     struct timeval tv;
  12.     int masterFound;
  13.     static int esistePrioritario = 0;
  14.     static int primaChiamata = 1;
  15.     static SENTINELLOCAL sentinelList[NUM_OF_SENTINEL];
  16.     DBMANAGERINFO dbfileInfo;
  17.  
  18.     /*
  19.      * recupera valore property da file dbconf.properties
  20.      * il settaggio dei sentinel a non prioritari avviene solo alla prima
  21.      * chiamata della routine
  22.      */
  23.  
  24.     if (getInfoFromFile(PROPERTY_FILE, &dbfileInfo) == -1)
  25.        return -1;
  26.     if (getMasterName(&dbfileInfo, masterName) == -1)
  27.        return -1;
  28.     if (getSentinelInfo(LOCAL_SENTINEL, &dbfileInfo, sentinelList[0].ip, &(sentinelList[0].port)) == -1)
  29.        return -1;
  30.  
  31.     if (getSentinelInfo(MASTER_SENTINEL, &dbfileInfo, sentinelList[1].ip, &(sentinelList[1].port)) == -1)
  32.        return -1;
  33.     if (getSentinelInfo(SLAVE_SENTINEL, &dbfileInfo, sentinelList[2].ip, &(sentinelList[2].port)) == -1)
  34.        return -1;
  35.  
  36.     /*
  37.      * Parto impostando come sentinel prioritario quello locale
  38.      */
  39.  
  40.     if (primaChiamata) {
  41.         sentinelList[0].prioritario = 1;
  42.         esistePrioritario = 1;
  43.         sentinelList[1].prioritario = 0;
  44.         sentinelList[2].prioritario = 0;
  45.         primaChiamata = 0;
  46.     }
  47.  
  48.     /*
  49.      * chiede ai sentinel quale sia il master (se rispondono in modo corretto
  50.      * il loro stato prioritario e' posto a 1 altrimenti viene settato a 0).
  51.      */
  52.  
  53.     tv.tv_sec = 0;
  54.     tv.tv_usec = TIMEOUT_CONNECTION_IN_MS;
  55.     masterFound = 0;
  56. #ifdef CONNECTION_DEBUG
  57.     fprintf(f, "getMasterAddr ---------------------------------------------------------------------\n");
  58.     fflush(f);
  59. #endif
  60.     for (nSent = 0; nSent < NUM_OF_SENTINEL; nSent++) {
  61.        if (!sentinelList[nSent].prioritario && esistePrioritario) {
  62. #ifdef CONNECTION_DEBUG
  63.            fprintf(f, "getMasterAddr Sentinel n. %d (%s:%d) non prioritario. Passo al successivo.\n", nSent,
  64.                    sentinelList[nSent].ip, sentinelList[nSent].port);
  65.            fflush(f);
  66. #endif
  67.            continue;
  68.        }
  69. #ifdef CONNECTION_DEBUG
  70.        fprintf(f, "getMasterAddr ---------------------------------------------------------------------\n");
  71.        fprintf(f, "getMasterAddr Connessione a sentinel n. %d (%s:%d) \n", nSent, sentinelList[nSent].ip,
  72.                sentinelList[nSent].port);
  73.        fflush(f);
  74. #endif
  75.        redisSentinelContext = redisConnectWithTimeout(sentinelList[nSent].ip, sentinelList[nSent].port, tv);
  76.  
  77.        if (redisSentinelContext->err) {
  78. #ifdef CONNECTION_DEBUG
  79.             fprintf(f, "getMasterAddr Errore connessione sentinel n. %d (%s:%d): %s\n", nSent, sentinelList[nSent].ip,
  80.                     sentinelList[nSent].port, redisSentinelContext->errstr);
  81.             fflush(f);
  82. #endif
  83.             sentinelList[nSent].prioritario = 0;
  84.             esistePrioritario = 0;
  85.             /*
  86.             if(redisSentinelContext != NULL)
  87.                     redisFree(redisSentinelContext);
  88.             */
  89.             continue;
  90.         } else {
  91. #ifdef CONNECTION_DEBUG
  92.             fprintf(f, "getMasterAddr Connessione riuscita\n");
  93.             fflush(f);
  94. #endif
  95.             reply = redisCommand(redisSentinelContext, "sentinel get-master-addr-by-name %s ", masterName);
  96.  
  97.             if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {
  98.                 printf("getMasterAddr Errore exec cmd sentinel get-master-addr-by-name %s", masterName);
  99.                 sentinelList[nSent].prioritario = 0;
  100.                 esistePrioritario = 0;
  101.                 /*
  102.                 if(reply != NULL)
  103.                         freeReplyObject(reply);
  104.  if(redisSentinelContext != NULL)
  105.                         redisFree(redisSentinelContext);
  106.                 */
  107.                 continue;
  108.             } else if (reply->type == REDIS_REPLY_ARRAY) {
  109.                 pElement = reply->element;
  110.                 for (element = 0; element < (int)reply->elements; element++) {
  111.                     if (element == 0)
  112.                         strcpy(pntIpStr, (*pElement)->str);
  113.                     else if (element == 1)
  114.                         strcpy(pntPortStr, (*pElement)->str);
  115.  
  116.                     pElement++;
  117.                 }
  118.  
  119.                 if (reply != NULL)
  120.                     freeReplyObject(reply);
  121.                 if (redisSentinelContext != NULL)
  122.                     redisFree(redisSentinelContext);
  123.  
  124. /*
  125. * ottenute dal sentinel le coordinate del master prova a collegarsi a quest'ultimo
  126. * per verificare che sia realmente master
  127. */
  128. #ifdef CONNECTION_DEBUG
  129.                 fprintf(f, "getMasterAddr Sentinel n. %d ha indicato master ip = %s port = %d\n", nSent, pntIpStr,
  130.                         atoi(pntPortStr));
  131.                 fflush(f);
  132.  
  133. #endif
  134.                 redisMasterContext = redisConnectWithTimeout(pntIpStr, atoi(pntPortStr), tv);
  135.                 if (redisSentinelContext->err) {
  136.                     printf("getMasterAddr Errore connessione master %s: %s\n", masterName,
  137.                            redisSentinelContext->errstr);
  138.                     sentinelList[nSent].prioritario = 0;
  139.                     esistePrioritario = 0;
  140.  
  141.                     /*
  142.                                        if(reply != NULL)
  143.                                                freeReplyObject(reply);
  144.                                        if(redisMasterContext != NULL)
  145.                                                redisFree(redisMasterContext);
  146.                                        */
  147.                     continue;
  148.                 } else {
  149. #ifdef CONNECTION_DEBUG
  150.                     fprintf(f, "getMasterAddr Connessione riuscita\n");
  151.                     fflush(f);
  152. #endif
  153.                     reply = redisCommand(redisMasterContext, "role");
  154.  
  155.                     if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {
  156.                         printf("getMasterAddr Errore exec cmd role verso master %s ", masterName);
  157.                         sentinelList[nSent].prioritario = 0;
  158.                         esistePrioritario = 0;
  159.                         /*
  160.                         if(reply != NULL)
  161.                                 freeReplyObject(reply);
  162.                         if(redisMasterContext != NULL)
  163.                                 redisFree(redisMasterContext);
  164.                         */
  165.                         continue;
  166.                     } else if (reply->type == REDIS_REPLY_ARRAY) {
  167.                         pElement = reply->element;
  168.                         if (strcmp((*pElement)->str, "master") == 0) {
  169. #ifdef CONNECTION_DEBUG
  170.                             fprintf(f, "getMasterAddr Sentinel n. %d ha indicato un master corretto\n", nSent);
  171.                             fflush(f);
  172. #endif
  173.                             sentinelList[nSent].prioritario = 1;
  174.                             esistePrioritario = 1;
  175.                             if (reply != NULL)
  176.                                 freeReplyObject(reply);
  177.                             masterFound = 1;
  178.                             break;
  179.                         } else {
  180.                             printf("getMasterAddr Sentinel n. %d ha indicato un master errato\n", nSent);
  181.                             sentinelList[nSent].prioritario = 0;
  182.                             esistePrioritario = 0;
  183.                             if (reply != NULL)
  184.                                 freeReplyObject(reply);
  185.                             if (redisMasterContext != NULL)
  186.                                 redisFree(redisMasterContext);
  187.                             continue;
  188.                         }
  189.                     }
  190.                 }
  191.             }
  192.         }
  193.     }
  194. #ifdef CONNECTION_DEBUG_2
  195.     fprintf(f, "getMasterAddr ---------------------------------------------------------------------\n");
  196.     fprintf(f, "getMasterAddr Stato attuale dei sentinel\n");
  197.     fprintf(f, "getMasterAddr esistePrioritario = %d\n", esistePrioritario);
  198.     fprintf(f, "getMasterAddr primaChiamata     = %d\n", primaChiamata);
  199.  
  200.     for (nSent = 0; nSent < NUM_OF_SENTINEL; nSent++) {
  201.        fprintf(f, "getMasterAddr ---------------------------------------------------------------------\n");
  202.        fprintf(f, "getMasterAddr Sentinel n.%d\n", nSent);
  203.        fprintf(f, "getMasterAddr ip          = %s\n", sentinelList[nSent].ip);
  204.        fprintf(f, "getMasterAddr port        = %d\n", sentinelList[nSent].port);
  205.        fprintf(f, "getMasterAddr prioritario = %d\n", sentinelList[nSent].prioritario);
  206.    }
  207.    fflush(f);
  208.  
  209. #endif
  210.    if (masterFound) {
  211. #ifdef GETMASTER_DEBUG
  212.        printf("getMasterAddr: Coordinate master (%s:%d) \n", pntIpStr, atoi(pntPortStr));
  213.  
  214. #endif
  215.        redisFree(redisMasterContext);
  216.        return (0);
  217.    } else
  218.        return (-1);
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement