提交 bfe17f31 authored 作者: Eliot Gable's avatar Eliot Gable

More additions, bug fixes, and change to atomic operations to replace many rwlocks.

上级 8255607b
......@@ -537,6 +537,8 @@ switch_status_t config_hb(hac_hb_t** hb, switch_xml_t root)
cur->lport = atoi(port);
cur->ttl = ttl;
cur->next = NULL;
switch_thread_rwlock_create(&cur->recv_lock, config.pool);
switch_thread_rwlock_create(&cur->send_lock, config.pool);
return SWITCH_STATUS_SUCCESS;
fail:
return SWITCH_STATUS_FALSE;
......@@ -611,9 +613,7 @@ switch_status_t config_general(switch_xml_t root)
value = switch_xml_attr(x_list, "value");
if(!switch_strlen_zero(value)) {
config.general.nodeid = value;
switch_thread_rwlock_wrlock(hac_globals.lock);
hac_globals.nodeid = (switch_strlen_zero(value) ? -1 : atoi(value));
switch_thread_rwlock_unlock(hac_globals.lock);
switch_atomic_set(&hac_globals.nodeid, (switch_strlen_zero(value) ? -1 : atoi(value)));
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: Missing nodeid value in general section.\n");
goto fail;
......@@ -623,31 +623,12 @@ switch_status_t config_general(switch_xml_t root)
goto fail;
}
if((x_list = switch_xml_child(root, "master-slave-ratio"))) {
const char* value = NULL;
value = switch_xml_attr(x_list, "value");
if(!switch_strlen_zero(value)) {
config.general.msr = value;
switch_thread_rwlock_wrlock(hac_globals.lock);
hac_globals.msr = (switch_strlen_zero(value) ? "4:1" : value);
switch_thread_rwlock_unlock(hac_globals.lock);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: Missing master-slave-ratio value in general section.\n");
goto fail;
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: Missing master-slave-ratio element in general section.\n");
goto fail;
}
if((x_list = switch_xml_child(root, "timer-a"))) {
const char* value = NULL;
value = switch_xml_attr(x_list, "value");
if(!switch_strlen_zero(value)) {
config.general.timer_a = value;
switch_thread_rwlock_wrlock(hac_globals.lock);
hac_globals.timer_a_dur = (switch_strlen_zero(value) ? 60 : atoi(value));
switch_thread_rwlock_unlock(hac_globals.lock);
switch_atomic_set(&hac_globals.timer_a_dur, (switch_strlen_zero(value) ? 60 : atoi(value)));
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: Missing timer-a value in general section.\n");
goto fail;
......@@ -662,9 +643,7 @@ switch_status_t config_general(switch_xml_t root)
value = switch_xml_attr(x_list, "value");
if(!switch_strlen_zero(value)) {
config.general.timer_b = value;
switch_thread_rwlock_wrlock(hac_globals.lock);
hac_globals.timer_b_dur = (switch_strlen_zero(value) ? 60 : atoi(value));
switch_thread_rwlock_unlock(hac_globals.lock);
switch_atomic_set(&hac_globals.timer_b_dur, (switch_strlen_zero(value) ? 60 : atoi(value)));
}
}
......@@ -673,17 +652,13 @@ switch_status_t config_general(switch_xml_t root)
value = switch_xml_attr(x_list, "value");
if(!switch_strlen_zero(value)) {
config.general.failure_threshold = value;
switch_thread_rwlock_wrlock(hac_globals.lock);
hac_globals.failure_threshold = (switch_strlen_zero(value) ? 5000 : atoi(value));
switch_thread_rwlock_unlock(hac_globals.lock);
switch_atomic_set(&hac_globals.failure_threshold, (switch_strlen_zero(value) ? 5000 : atoi(value)));
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "HAC: Missing failure-threshold value in general section; defaulted to 5000ms.\n");
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "HAC: Missing failure-threshold element in general section; defaulting to 5000ms.\n");
switch_thread_rwlock_wrlock(hac_globals.lock);
hac_globals.failure_threshold = 5000;
switch_thread_rwlock_unlock(hac_globals.lock);
switch_atomic_set(&hac_globals.failure_threshold, 5000);
}
config.general.heartbeat = switch_core_alloc(config.pool, sizeof(hac_hb_t));
......@@ -692,9 +667,9 @@ switch_status_t config_general(switch_xml_t root)
if(config_hb(&cur, x_list) != SWITCH_STATUS_SUCCESS) {
goto fail;
}
switch_thread_rwlock_wrlock(hac_globals.lock);
switch_thread_rwlock_wrlock(hac_globals.hb_lock);
hac_globals.hb = config.general.heartbeat;
switch_thread_rwlock_unlock(hac_globals.lock);
switch_thread_rwlock_unlock(hac_globals.hb_lock);
}
if(switch_strlen_zero(config.general.heartbeat->address)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "HAC: Missing heartbeat element in general section.\n");
......@@ -743,9 +718,7 @@ switch_status_t do_config(switch_bool_t reload)
}
switch_xml_free(xml);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "HAC: Configuration loaded.\n");
switch_thread_rwlock_wrlock(hac_globals.lock);
hac_globals.state = HAC_PEER_STARTING;
switch_thread_rwlock_unlock(hac_globals.lock);
switch_atomic_set(&hac_globals.state, HAC_PEER_STARTING);
return SWITCH_STATUS_SUCCESS;
failconfig:
......
......@@ -23,16 +23,22 @@
extern unsigned char MAGIC[];
/*static char *MARKER = "1"; */
typedef enum {
HAC_PEER_UNKNOWN = 0,
HAC_PEER_STARTING = 1,
HAC_PEER_MASTER = 2,
HAC_PEER_SLAVE = 3,
HAC_PEER_FAILED_MASTER = 4,
HAC_PEER_FAILED_SLAVE = 5,
HAC_PEER_STOPPING_MASTER = 6,
HAC_PEER_STOPPING_SLAVE = 7
} hac_peer_state_t;
/* Atomic state type so we don't need rwlocks */
#define HAC_PEER_UNKNOWN 0
#define HAC_PEER_STARTING 1
#define HAC_PEER_MASTER 2
#define HAC_PEER_SLAVE 3
#define HAC_PEER_FAILED_MASTER 4
#define HAC_PEER_FAILED_SLAVE 5
#define HAC_PEER_STOPPING_MASTER 6
#define HAC_PEER_STOPPING_SLAVE 7
typedef switch_atomic_t hac_peer_state_t;
/* Atomic state type so we don't need rwlocks */
#define HAC_LOOPING 0
#define HAC_SHUTDOWN_RUNTIME 1
#define HAC_UNLOAD 2
typedef switch_atomic_t hac_state_t;
struct s_hac_rule {
const char* name;
......@@ -157,6 +163,8 @@ struct s_hac_hb {
switch_socket_t *udp_sock;
struct timeval last_hb;
struct s_hac_hb* next;
switch_thread_rwlock_t *recv_lock;
switch_thread_rwlock_t *send_lock;
};
typedef struct s_hac_hb hac_hb_t;
......@@ -193,18 +201,14 @@ struct s_hac_config {
};
typedef struct s_hac_config hac_config_t;
typedef enum {
HAC_LOOPING = 0,
HAC_SHUTDOWN_RUNTIME = 1,
HAC_UNLOAD = 2
} hac_state_t;
extern hac_config_t config;
struct s_hac_event {
switch_event_t *event;
switch_bool_t processed;
switch_atomic_t refcount;
const char *uuid;
struct timeval arrived;
};
typedef struct s_hac_event hac_event_t;
......@@ -227,31 +231,36 @@ struct s_hac_peer {
struct timeval last_seen;
hac_peer_state_t state;
uint64_t missed_beats;
switch_atomic_t refcount;
switch_thread_rwlock_t *lock;
};
typedef struct s_hac_peer hac_peer_t;
struct s_global {
switch_memory_pool_t* pool;
char hostname[256];
uint64_t host_hash;
switch_atomic_t host_hash;
switch_memory_pool_t* event_pool;
switch_hash_t *event_hash;
switch_hash_t *peer_hash;
hac_peer_state_t state;
hac_state_t mod_state;
hac_hb_t* hb;
int nodeid;
uint64_t rand_val;
const char* msr;
int req_masters;
int masters;
int slaves;
int timer_a_dur;
int timer_b_dur;
switch_atomic_t nodeid;
switch_atomic_t rand_val;
switch_atomic_t req_masters;
switch_atomic_t masters;
switch_atomic_t slaves;
switch_atomic_t timer_a_dur;
switch_atomic_t timer_b_dur;
struct timeval timer_a;
struct timeval timer_b;
int failure_threshold;
switch_atomic_t failure_threshold;
switch_atomic_t max_latency;
switch_thread_rwlock_t *lock;
switch_thread_rwlock_t *hb_lock;
switch_thread_rwlock_t *event_hash_lock;
switch_thread_rwlock_t *peer_hash_lock;
};
extern struct s_global hac_globals;
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论