Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int getMasterAddr(char *pntIpStr, char *pntPortStr)
- {
- // char ip[512];
- // char port[10];
- char masterName[512];
- redisContext *redisSentinelContext = NULL, *redisMasterContext = NULL;
- redisReply *reply, **pElement;
- int element;
- int nSent;
- struct timeval tv;
- int masterFound;
- static int esistePrioritario = 0;
- static int primaChiamata = 1;
- static SENTINELLOCAL sentinelList[NUM_OF_SENTINEL];
- DBMANAGERINFO dbfileInfo;
- /*
- * recupera valore property da file dbconf.properties
- * il settaggio dei sentinel a non prioritari avviene solo alla prima
- * chiamata della routine
- */
- if (getInfoFromFile(PROPERTY_FILE, &dbfileInfo) == -1)
- return -1;
- if (getMasterName(&dbfileInfo, masterName) == -1)
- return -1;
- if (getSentinelInfo(LOCAL_SENTINEL, &dbfileInfo, sentinelList[0].ip, &(sentinelList[0].port)) == -1)
- return -1;
- if (getSentinelInfo(MASTER_SENTINEL, &dbfileInfo, sentinelList[1].ip, &(sentinelList[1].port)) == -1)
- return -1;
- if (getSentinelInfo(SLAVE_SENTINEL, &dbfileInfo, sentinelList[2].ip, &(sentinelList[2].port)) == -1)
- return -1;
- /*
- * Parto impostando come sentinel prioritario quello locale
- */
- if (primaChiamata) {
- sentinelList[0].prioritario = 1;
- esistePrioritario = 1;
- sentinelList[1].prioritario = 0;
- sentinelList[2].prioritario = 0;
- primaChiamata = 0;
- }
- /*
- * chiede ai sentinel quale sia il master (se rispondono in modo corretto
- * il loro stato prioritario e' posto a 1 altrimenti viene settato a 0).
- */
- tv.tv_sec = 0;
- tv.tv_usec = TIMEOUT_CONNECTION_IN_MS;
- masterFound = 0;
- #ifdef CONNECTION_DEBUG
- fprintf(f, "getMasterAddr ---------------------------------------------------------------------\n");
- fflush(f);
- #endif
- for (nSent = 0; nSent < NUM_OF_SENTINEL; nSent++) {
- if (!sentinelList[nSent].prioritario && esistePrioritario) {
- #ifdef CONNECTION_DEBUG
- fprintf(f, "getMasterAddr Sentinel n. %d (%s:%d) non prioritario. Passo al successivo.\n", nSent,
- sentinelList[nSent].ip, sentinelList[nSent].port);
- fflush(f);
- #endif
- continue;
- }
- #ifdef CONNECTION_DEBUG
- fprintf(f, "getMasterAddr ---------------------------------------------------------------------\n");
- fprintf(f, "getMasterAddr Connessione a sentinel n. %d (%s:%d) \n", nSent, sentinelList[nSent].ip,
- sentinelList[nSent].port);
- fflush(f);
- #endif
- redisSentinelContext = redisConnectWithTimeout(sentinelList[nSent].ip, sentinelList[nSent].port, tv);
- if (redisSentinelContext->err) {
- #ifdef CONNECTION_DEBUG
- fprintf(f, "getMasterAddr Errore connessione sentinel n. %d (%s:%d): %s\n", nSent, sentinelList[nSent].ip,
- sentinelList[nSent].port, redisSentinelContext->errstr);
- fflush(f);
- #endif
- sentinelList[nSent].prioritario = 0;
- esistePrioritario = 0;
- /*
- if(redisSentinelContext != NULL)
- redisFree(redisSentinelContext);
- */
- continue;
- } else {
- #ifdef CONNECTION_DEBUG
- fprintf(f, "getMasterAddr Connessione riuscita\n");
- fflush(f);
- #endif
- reply = redisCommand(redisSentinelContext, "sentinel get-master-addr-by-name %s ", masterName);
- if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {
- printf("getMasterAddr Errore exec cmd sentinel get-master-addr-by-name %s", masterName);
- sentinelList[nSent].prioritario = 0;
- esistePrioritario = 0;
- /*
- if(reply != NULL)
- freeReplyObject(reply);
- if(redisSentinelContext != NULL)
- redisFree(redisSentinelContext);
- */
- continue;
- } else if (reply->type == REDIS_REPLY_ARRAY) {
- pElement = reply->element;
- for (element = 0; element < (int)reply->elements; element++) {
- if (element == 0)
- strcpy(pntIpStr, (*pElement)->str);
- else if (element == 1)
- strcpy(pntPortStr, (*pElement)->str);
- pElement++;
- }
- if (reply != NULL)
- freeReplyObject(reply);
- if (redisSentinelContext != NULL)
- redisFree(redisSentinelContext);
- /*
- * ottenute dal sentinel le coordinate del master prova a collegarsi a quest'ultimo
- * per verificare che sia realmente master
- */
- #ifdef CONNECTION_DEBUG
- fprintf(f, "getMasterAddr Sentinel n. %d ha indicato master ip = %s port = %d\n", nSent, pntIpStr,
- atoi(pntPortStr));
- fflush(f);
- #endif
- redisMasterContext = redisConnectWithTimeout(pntIpStr, atoi(pntPortStr), tv);
- if (redisSentinelContext->err) {
- printf("getMasterAddr Errore connessione master %s: %s\n", masterName,
- redisSentinelContext->errstr);
- sentinelList[nSent].prioritario = 0;
- esistePrioritario = 0;
- /*
- if(reply != NULL)
- freeReplyObject(reply);
- if(redisMasterContext != NULL)
- redisFree(redisMasterContext);
- */
- continue;
- } else {
- #ifdef CONNECTION_DEBUG
- fprintf(f, "getMasterAddr Connessione riuscita\n");
- fflush(f);
- #endif
- reply = redisCommand(redisMasterContext, "role");
- if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {
- printf("getMasterAddr Errore exec cmd role verso master %s ", masterName);
- sentinelList[nSent].prioritario = 0;
- esistePrioritario = 0;
- /*
- if(reply != NULL)
- freeReplyObject(reply);
- if(redisMasterContext != NULL)
- redisFree(redisMasterContext);
- */
- continue;
- } else if (reply->type == REDIS_REPLY_ARRAY) {
- pElement = reply->element;
- if (strcmp((*pElement)->str, "master") == 0) {
- #ifdef CONNECTION_DEBUG
- fprintf(f, "getMasterAddr Sentinel n. %d ha indicato un master corretto\n", nSent);
- fflush(f);
- #endif
- sentinelList[nSent].prioritario = 1;
- esistePrioritario = 1;
- if (reply != NULL)
- freeReplyObject(reply);
- masterFound = 1;
- break;
- } else {
- printf("getMasterAddr Sentinel n. %d ha indicato un master errato\n", nSent);
- sentinelList[nSent].prioritario = 0;
- esistePrioritario = 0;
- if (reply != NULL)
- freeReplyObject(reply);
- if (redisMasterContext != NULL)
- redisFree(redisMasterContext);
- continue;
- }
- }
- }
- }
- }
- }
- #ifdef CONNECTION_DEBUG_2
- fprintf(f, "getMasterAddr ---------------------------------------------------------------------\n");
- fprintf(f, "getMasterAddr Stato attuale dei sentinel\n");
- fprintf(f, "getMasterAddr esistePrioritario = %d\n", esistePrioritario);
- fprintf(f, "getMasterAddr primaChiamata = %d\n", primaChiamata);
- for (nSent = 0; nSent < NUM_OF_SENTINEL; nSent++) {
- fprintf(f, "getMasterAddr ---------------------------------------------------------------------\n");
- fprintf(f, "getMasterAddr Sentinel n.%d\n", nSent);
- fprintf(f, "getMasterAddr ip = %s\n", sentinelList[nSent].ip);
- fprintf(f, "getMasterAddr port = %d\n", sentinelList[nSent].port);
- fprintf(f, "getMasterAddr prioritario = %d\n", sentinelList[nSent].prioritario);
- }
- fflush(f);
- #endif
- if (masterFound) {
- #ifdef GETMASTER_DEBUG
- printf("getMasterAddr: Coordinate master (%s:%d) \n", pntIpStr, atoi(pntPortStr));
- #endif
- redisFree(redisMasterContext);
- return (0);
- } else
- return (-1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement