CLEANUP: refactored the arcus_zk_set_ensemble() funtion.
@@ -1588,9 +1588,14 @@ void arcus_zk_destroy(void)
int arcus_zk_set_ensemble(char *ensemble_list)
{
int ret = 0;
+
pthread_mutex_lock(&zk_lock);
- if (main_zk->zh) {
do {
+ if (main_zk->zh == NULL) {
+ arcus_conf.logger->log(EXTENSION_LOG_WARNING, NULL,
+ "Failed to change the ZooKeeper ensemble list. Invalid ZK handle.\n");
+ ret = -1; break;
+ }
char *copy = strdup(ensemble_list);
if (!copy) {
/* Should not happen unless the system is really short of memory. */
@@ -1609,17 +1614,10 @@ int arcus_zk_set_ensemble(char *ensemble_list)
arcus_conf.logger->log(EXTENSION_LOG_WARNING, NULL,
"Successfully changed the ZooKeeper ensemble list. list=%s\n",
ensemble_list);
- /* main_zk->ensemble_list is not used after init.
- * Nothing uses it. So it is okay to just replace the pointer.
- */
- free(main_zk->ensemble_list);
+ char *old_ensemble = main_zk->ensemble_list;
main_zk->ensemble_list = copy;
+ free(old_ensemble);
} while(0);
- } else {
- arcus_conf.logger->log(EXTENSION_LOG_WARNING, NULL,
- "Failed to change the ZooKeeper ensemble list. Invalid ZK handle.\n");
- ret = -1;
- }
pthread_mutex_unlock(&zk_lock);
return ret;
}
