Skip to content

Commit 72823d3

Browse files
committed
CLEANUP: refactored with arcus_register_cache_instance() function.
1 parent bce2526 commit 72823d3

File tree

1 file changed

+23
-37
lines changed

1 file changed

+23
-37
lines changed

arcus_zk.c

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,11 @@ arcus_zk_client_init(zk_info_t *zinfo)
392392
inc_count(-1);
393393
return EX_PROTOCOL;
394394
}
395-
395+
// "recv" timeout is actually the session timeout
396+
// ZK client ping period is recv_timeout / 3.
397+
arcus_conf.logger->log(EXTENSION_LOG_INFO, NULL,
398+
"ZooKeeper client initialized. (ZK session timeout=%d sec)\n",
399+
zoo_recv_timeout(main_zk->zh)/1000);
396400
return 0;
397401
}
398402

@@ -1207,7 +1211,7 @@ static int arcus_check_server_mapping(zhandle_t *zh, const char *root)
12071211
return rc;
12081212
}
12091213

1210-
static int arcus_create_ephemeral_znode(zhandle_t *zh, const char *root)
1214+
static int arcus_create_ephemeral_znode(zhandle_t *zh)
12111215
{
12121216
int rc;
12131217
char zpath[512];
@@ -1254,6 +1258,17 @@ static int arcus_create_ephemeral_znode(zhandle_t *zh, const char *root)
12541258
return 0;
12551259
}
12561260

1261+
static int arcus_register_cache_instance(zhandle_t *zh)
1262+
{
1263+
/* create "/cache_list/{svc}/ip:port-hostname" ephemeral znode */
1264+
if (arcus_create_ephemeral_znode(zh) != 0) {
1265+
arcus_conf.logger->log(EXTENSION_LOG_WARNING, NULL,
1266+
"arcus_create_ephemeral_znode() failed.\n");
1267+
return -1;
1268+
}
1269+
return 0;
1270+
}
1271+
12571272
static char *arcus_get_self_name(void)
12581273
{
12591274
return arcus_conf.mc_ipport;
@@ -1450,7 +1465,6 @@ void arcus_zk_init(char *ensemble_list, int zk_to,
14501465
"Failed to initialize zk client\n");
14511466
arcus_exit(NULL, rc);
14521467
}
1453-
14541468
/* setting main zk */
14551469
main_zk = zinfo;
14561470

@@ -1478,10 +1492,8 @@ void arcus_zk_init(char *ensemble_list, int zk_to,
14781492

14791493
arcus_conf.init = true;
14801494

1481-
/* create "/cache_list/{svc}/ip:port-hostname" ephemeral znode */
1482-
if (arcus_create_ephemeral_znode(main_zk->zh, zk_root) != 0) {
1483-
arcus_conf.logger->log(EXTENSION_LOG_WARNING, NULL,
1484-
"arcus_create_ephemeral_znode() failed.\n");
1495+
/* register cache instance in ZK */
1496+
if (arcus_register_cache_instance(main_zk->zh) != 0) {
14851497
arcus_exit(main_zk->zh, EX_PROTOCOL);
14861498
}
14871499

@@ -1494,11 +1506,6 @@ void arcus_zk_init(char *ensemble_list, int zk_to,
14941506
"Memcached joined Arcus cache cloud for \"%s\" service "
14951507
"(took %ld microsec)\n", arcus_conf.svc, difftime_us);
14961508

1497-
// "recv" timeout is actually the session timeout
1498-
// ZK client ping period is recv_timeout / 3.
1499-
arcus_conf.logger->log(EXTENSION_LOG_INFO, NULL,
1500-
"ZooKeeper session timeout: %d sec\n", zoo_recv_timeout(main_zk->zh)/1000);
1501-
15021509
#ifdef ENABLE_CLUSTER_AWARE
15031510
const char *self_name = arcus_get_self_name();
15041511
arcus_conf.ch = cluster_config_init(self_name,
@@ -1706,33 +1713,21 @@ int arcus_zk_rejoin_ensemble()
17061713

17071714
/* initialize Arcus ZK stats */
17081715
memset(&azk_stat, 0, sizeof(azk_stat));
1709-
17101716
memset(&main_zk->myid, 0, sizeof(clientid_t));
1711-
17121717
assert(main_zk->ensemble_list);
17131718

17141719
gettimeofday(&start_time, 0);
1715-
1716-
ret = arcus_zk_client_init(main_zk);
1717-
if (ret != 0) {
1720+
if (arcus_zk_client_init(main_zk) != 0) {
17181721
arcus_conf.logger->log(EXTENSION_LOG_WARNING, NULL,
17191722
"Failed to initialize zk client.\n");
1720-
break;
1723+
ret = -1; break;
17211724
}
1722-
1723-
/* Use the zk root directory and the service code
1724-
* acquired during the execution of arcus_zk_init().
1725-
*/
1726-
assert(zk_root != NULL && arcus_conf.svc != NULL);
1727-
17281725
/* create "/cache_list/{svc}/ip:port-hostname" ephemeral znode */
1729-
ret = arcus_create_ephemeral_znode(main_zk->zh, zk_root);
1730-
if (ret != 0) {
1726+
if (arcus_create_ephemeral_znode(main_zk->zh) != 0) {
17311727
arcus_conf.logger->log(EXTENSION_LOG_WARNING, NULL,
17321728
"arcus_create_ephemeral_znode() failed.\n");
1733-
break;
1729+
ret = -1; break;
17341730
}
1735-
17361731
gettimeofday(&end_time, 0);
17371732
difftime_us = (end_time.tv_sec*1000000 + end_time.tv_usec) -
17381733
(start_time.tv_sec*1000000 + start_time.tv_usec);
@@ -1742,27 +1737,19 @@ int arcus_zk_rejoin_ensemble()
17421737
"Memcached rejoined Arcus cache cloud for \"%s\" service "
17431738
"(took %ld microsec)\n", arcus_conf.svc, difftime_us);
17441739

1745-
// "recv" timeout is actually the session timeout
1746-
// ZK client ping period is recv_timeout / 3.
1747-
arcus_conf.logger->log(EXTENSION_LOG_INFO, NULL,
1748-
"ZooKeeper session timeout: %d sec\n", zoo_recv_timeout(main_zk->zh)/1000);
1749-
17501740
assert(arcus_conf.ch);
1751-
17521741
struct String_vector strv = { 0, NULL };
17531742
/* 2nd argument, NULL means no watcher */
17541743
if (arcus_read_ZK_children(main_zk->zh, arcus_conf.cluster_path, NULL, &strv) <= 0) {
17551744
arcus_conf.logger->log(EXTENSION_LOG_WARNING, NULL,
17561745
"Failed to read cache list from ZK.\n");
17571746
ret = -1; break;
17581747
}
1759-
17601748
if (update_cluster_config(&strv) != 0) {
17611749
arcus_conf.logger->log(EXTENSION_LOG_WARNING, NULL,
17621750
"Failed to update cluster config.\n");
17631751
ret = -1; break;
17641752
}
1765-
17661753
deallocate_String_vector(&strv);
17671754

17681755
arcus_conf.logger->log(EXTENSION_LOG_WARNING, NULL,
@@ -1785,7 +1772,6 @@ int arcus_zk_rejoin_ensemble()
17851772
zookeeper_close(main_zk->zh);
17861773
main_zk->zh = NULL;
17871774
}
1788-
ret = -1;
17891775
}
17901776
pthread_mutex_unlock(&zk_lock);
17911777

0 commit comments

Comments
 (0)