Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove global variable from notify_client #154

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions src/attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ int do_attr_command(struct booth_config *conf, cmd_request_t cmd)
if (rv < 0)
goto out_close;

rv = tpt->send(site, &cl.attr_msg, sendmsglen(&cl.attr_msg));
if (rv < 0)
rv = tpt->send(conf, site, &cl.attr_msg, sendmsglen(&cl.attr_msg));
if (rv < 0) {
goto out_close;
}

msg = malloc(MAX_MSG_LEN);
if (!msg) {
Expand Down Expand Up @@ -346,7 +347,8 @@ append_attr(gpointer key, gpointer value, gpointer user_data)
}


static cmd_result_t attr_get(struct ticket_config *tk, int fd, struct boothc_attr_msg *msg)
static cmd_result_t attr_get(struct booth_config *conf, struct ticket_config *tk,
int fd, struct boothc_attr_msg *msg)
{
cmd_result_t rv = RLT_SUCCESS;
struct boothc_hdr_msg hdr;
Expand All @@ -357,12 +359,15 @@ static cmd_result_t attr_get(struct ticket_config *tk, int fd, struct boothc_att
* lookup attr
* send value
*/
if (!tk->attr)
if (!tk->attr) {
return RLT_NO_SUCH_ATTR;
}

a = (struct geo_attr *)g_hash_table_lookup(tk->attr, msg->attr.name);
if (!a)
if (!a) {
return RLT_NO_SUCH_ATTR;
}

attr_val = g_string_new(NULL);
if (!attr_val) {
log_error("out of memory");
Expand All @@ -371,14 +376,20 @@ static cmd_result_t attr_get(struct ticket_config *tk, int fd, struct boothc_att
g_string_printf(attr_val, "%s\n", a->val);
init_header(&hdr.header, ATTR_GET, 0, 0, RLT_SUCCESS, 0,
sizeof(hdr) + attr_val->len);
if (send_header_plus(fd, &hdr, attr_val->str, attr_val->len))

if (send_header_plus(conf, fd, &hdr, attr_val->str, attr_val->len)) {
rv = RLT_SYNC_FAIL;
if (attr_val)
}

if (attr_val) {
g_string_free(attr_val, TRUE);
}

return rv;
}

static cmd_result_t attr_list(struct ticket_config *tk, int fd, struct boothc_attr_msg *msg)
static cmd_result_t attr_list(struct booth_config *conf, struct ticket_config *tk,
int fd, struct boothc_attr_msg *msg)
{
GString *data;
cmd_result_t rv;
Expand All @@ -399,10 +410,12 @@ static cmd_result_t attr_list(struct ticket_config *tk, int fd, struct boothc_at

init_header(&hdr.header, ATTR_LIST, 0, 0, RLT_SUCCESS, 0,
sizeof(hdr) + data->len);
rv = send_header_plus(fd, &hdr, data->str, data->len);
rv = send_header_plus(conf, fd, &hdr, data->str, data->len);

if (data)
if (data) {
g_string_free(data, TRUE);
}

return rv;
}

Expand All @@ -426,14 +439,18 @@ int process_attr_request(struct booth_config *conf, struct client *req_client,

switch (cmd) {
case ATTR_LIST:
rv = attr_list(tk, req_client->fd, msg);
if (rv)
rv = attr_list(conf, tk, req_client->fd, msg);
if (rv) {
goto reply_now;
}

return 1;
case ATTR_GET:
rv = attr_get(tk, req_client->fd, msg);
if (rv)
rv = attr_get(conf, tk, req_client->fd, msg);
if (rv) {
goto reply_now;
}

return 1;
case ATTR_SET:
rv = attr_set(tk, msg);
Expand All @@ -445,7 +462,7 @@ int process_attr_request(struct booth_config *conf, struct client *req_client,

reply_now:
init_header(&hdr.header, CL_RESULT, 0, 0, rv, 0, sizeof(hdr));
send_header_plus(req_client->fd, &hdr, NULL, 0);
send_header_plus(conf, req_client->fd, &hdr, NULL, 0);
return 1;
}

Expand Down
10 changes: 9 additions & 1 deletion src/booth.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,15 @@ int client_add(int fd, const struct booth_transport *tpt,
int find_client_by_fd(int fd);
void safe_copy(char *dest, char *value, size_t buflen, const char *description);
int update_authkey(void);
void list_peers(int fd);

/**
* @internal
* Response to "get all servers we know about"
*
* @param[in,out] conf config object to refer to
* @param[in] fd file descriptor of the socket to respond to
*/
void list_peers(struct booth_config *conf, int fd);


struct command_line {
Expand Down
17 changes: 10 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,23 @@ static int format_peers(char **pdata, unsigned int *len)
}


void list_peers(int fd)
void list_peers(struct booth_config *conf, int fd)
{
char *data;
unsigned int olen;
struct boothc_hdr_msg hdr;

if (format_peers(&data, &olen) < 0)
if (format_peers(&data, &olen) < 0) {
goto out;
}

init_header(&hdr.header, CL_LIST, 0, 0, RLT_SUCCESS, 0, sizeof(hdr) + olen);
(void)send_header_plus(fd, &hdr, data, olen);
send_header_plus(conf, fd, &hdr, data, olen);

out:
if (data)
if (data) {
free(data);
}
}

/* trim trailing spaces if the key is ascii
Expand Down Expand Up @@ -719,7 +721,7 @@ static int query_get_string_answer(cmd_request_t cmd)
if (rv < 0)
goto out_close;

rv = tpt->send(site, request, msg_size);
rv = tpt->send(booth_conf, site, request, msg_size);
if (rv < 0)
goto out_close;

Expand Down Expand Up @@ -820,9 +822,10 @@ static int do_command(cmd_request_t cmd)
if (rv < 0)
goto out_close;

rv = tpt->send(site, &cl.msg, sendmsglen(&cl.msg));
if (rv < 0)
rv = tpt->send(booth_conf, site, &cl.msg, sendmsglen(&cl.msg));
if (rv < 0) {
goto out_close;
}

read_more:
rv = tpt->recv_auth(site, &reply, sizeof(reply));
Expand Down
26 changes: 14 additions & 12 deletions src/manual.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
* and the current node doesn't have to wait for any responses
* from other sites.
*/
int manual_selection(struct ticket_config *tk,
struct booth_site *preference, int update_term, cmd_reason_t reason)
int manual_selection(struct booth_config *conf, struct ticket_config *tk,
struct booth_site *preference, int update_term,
cmd_reason_t reason)
{
if (local->type != SITE)
if (local->type != SITE) {
return 0;
}

tk_log_debug("starting manual selection (caused by %s %s)",
state_to_string(reason),
Expand All @@ -56,7 +58,7 @@ int manual_selection(struct ticket_config *tk,
save_committed_tkt(tk);

// Inform others about the new leader
ticket_broadcast(tk, OP_HEARTBEAT, OP_ACK, RLT_SUCCESS, 0);
ticket_broadcast(conf, tk, OP_HEARTBEAT, OP_ACK, RLT_SUCCESS, 0);
tk->ticket_updated = 0;

return 0;
Expand All @@ -66,35 +68,35 @@ int manual_selection(struct ticket_config *tk,
* revoked from another site, which this site doesn't
* consider as a leader.
*/
int process_REVOKE_for_manual_ticket (
struct ticket_config *tk,
struct booth_site *sender,
struct boothc_ticket_msg *msg)
int process_REVOKE_for_manual_ticket(struct booth_config *conf,
struct ticket_config *tk,
struct booth_site *sender,
struct boothc_ticket_msg *msg)
{
int rv;

// For manual tickets, we may end up having two leaders.
// If one of them is revoked, it will send information
// to all members of the GEO cluster.

// We may find ourselves here if this particular site
// has not been following the leader which had been revoked
// (and which had sent this message).

// We send the ACK, to satisfy the requestor.
rv = send_msg(OP_ACK, tk, sender, msg);
rv = send_msg(conf, OP_ACK, tk, sender, msg);

// Mark this ticket as not granted to the sender anymore.
mark_ticket_as_revoked(tk, sender);

if (tk->state == ST_LEADER) {
tk_log_warn("%s wants to revoke ticket, "
"but this site is itself a leader",
site_string(sender));

// Because another leader is presumably stepping down,
// let's notify other sites that now we are the only leader.
ticket_broadcast(tk, OP_HEARTBEAT, OP_ACK, RLT_SUCCESS, 0);
ticket_broadcast(conf, tk, OP_HEARTBEAT, OP_ACK, RLT_SUCCESS, 0);
} else {
tk_log_warn("%s wants to revoke ticket, "
"but this site is not following it",
Expand Down
13 changes: 7 additions & 6 deletions src/manual.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@

struct ticket_config;

int manual_selection(struct ticket_config *tk,
struct booth_site *new_leader, int update_term, cmd_reason_t reason);
int manual_selection(struct booth_config *conf, struct ticket_config *tk,
struct booth_site *new_leader, int update_term,
cmd_reason_t reason);

int process_REVOKE_for_manual_ticket (
struct ticket_config *tk,
struct booth_site *sender,
struct boothc_ticket_msg *msg);
int process_REVOKE_for_manual_ticket(struct booth_config *conf,
struct ticket_config *tk,
struct booth_site *sender,
struct boothc_ticket_msg *msg);


#endif /* _MANUAL_H */
Loading