forked from lei.chen/myxce
214 lines
5.2 KiB
C
214 lines
5.2 KiB
C
#include "config.h"
|
|
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "cluster.h"
|
|
#include "xce/cluster_def.h"
|
|
#include "xce/errnox.h"
|
|
|
|
static int validate_host(cluster_t* c, const char* host, const char* key)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
static int handle_client_join(cluster_t* c, endpoint_t* ep,
|
|
xce_request_t* request)
|
|
{
|
|
xce_reply_t reply;
|
|
|
|
init_reply_by_req(&reply, request);
|
|
|
|
reply.status = 0;
|
|
reply.data_which = RequestReply_data_nodata;
|
|
|
|
xce_reply(ep, c->myhost, &reply);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int handle_worker_join(cluster_t* c, endpoint_t* ep,
|
|
xce_request_t* request)
|
|
{
|
|
int idx;
|
|
ep_private_t* priv = (ep_private_t*)ep->data;
|
|
xce_reply_t reply;
|
|
xce_peerinfo_t* from = request->from;
|
|
int rc = 0;
|
|
xce_joindata_t* joinmsg = NULL;
|
|
|
|
TRACE("%s worker %s joining...", __func__, from->name);
|
|
|
|
init_reply_by_req(&reply, request);
|
|
|
|
if (!validate_host(c, from->name, from->key)) {
|
|
ERROR("%s reject join of host %s", __func__, from->name);
|
|
rc = -EPERM;
|
|
|
|
reply.status = rc;
|
|
reply.data_which = RequestReply_data_nodata;
|
|
|
|
xce_reply(ep, c->myhost, &reply);
|
|
return rc;
|
|
}
|
|
|
|
joinmsg = request->body.joinmsg;
|
|
|
|
xce_run_hooks(&(c->hooksys), XCE_CONTROLLER_HOOK_WORKER, HOOK_BEFORE_EVENT,
|
|
HOOK_DETAIL_WORKER_JOIN, c, from->name, 0);
|
|
idx = add_worker(c, ep, from->name, joinmsg->data.attrs);
|
|
|
|
reply.status = idx;
|
|
reply.data_which = RequestReply_data_nodata;
|
|
xce_reply(ep, c->myhost, &reply);
|
|
|
|
xce_run_hooks(&(c->hooksys), XCE_CONTROLLER_HOOK_WORKER, HOOK_AFTER_EVENT,
|
|
HOOK_DETAIL_WORKER_JOIN, c, from->name, idx);
|
|
if (idx < 0) {
|
|
ERROR("worker %s join failed, rc %d", from->name, idx);
|
|
|
|
return idx;
|
|
}
|
|
|
|
priv->u.idx = idx;
|
|
on_worker_join(c, ep);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int handle_scheduler_join(cluster_t* c, endpoint_t* ep,
|
|
xce_request_t* request)
|
|
{
|
|
xce_reply_t reply;
|
|
ep_private_t* priv = (ep_private_t*)ep->data;
|
|
int rc = 0;
|
|
|
|
TRACE("%s scheduler join, ep %p", __func__, ep);
|
|
|
|
init_reply_by_req(&reply, request);
|
|
|
|
c->sched.ep = ep;
|
|
priv->u.idx = 0;
|
|
|
|
rc = on_scheduler_join(c);
|
|
|
|
reply.status = rc;
|
|
reply.data_which = RequestReply_data_nodata;
|
|
xce_reply(ep, c->myhost, &reply);
|
|
|
|
return rc;
|
|
}
|
|
|
|
static int handle_controller_join(cluster_t* c, endpoint_t* ep,
|
|
xce_request_t* request)
|
|
{
|
|
xce_reply_t reply;
|
|
int rc = -ENOSYS;
|
|
|
|
ERROR("we still have not support join of controller");
|
|
|
|
init_reply_by_req(&reply, request);
|
|
reply.status = rc;
|
|
reply.data_which = RequestReply_data_nodata;
|
|
|
|
xce_reply(ep, c->myhost, &reply);
|
|
|
|
return rc;
|
|
}
|
|
|
|
static int handle_apiserver_join(cluster_t* c, endpoint_t* ep,
|
|
xce_request_t* request)
|
|
{
|
|
xce_reply_t reply;
|
|
xce_peerinfo_t* from = request->from;
|
|
int rc = 0;
|
|
|
|
TRACE("%s api server join, ep %p", __func__, ep);
|
|
|
|
init_reply_by_req(&reply, request);
|
|
|
|
rc = add_apiserver(c, from->name, ep);
|
|
if (rc >= 0) {
|
|
ep_private_t* priv = (ep_private_t*)ep->data;
|
|
|
|
priv->u.idx = rc;
|
|
on_apiserver_join(c, ep);
|
|
}
|
|
|
|
reply.status = rc;
|
|
reply.data_which = RequestReply_data_nodata;
|
|
xce_reply(ep, c->myhost, &reply);
|
|
|
|
if (rc < 0) {
|
|
ERROR("api server %s join failed, rc %d", from->name, rc);
|
|
return rc;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int handle_join_(cluster_t* c, endpoint_t* ep, xce_request_t* request)
|
|
{
|
|
ep_private_t* priv;
|
|
int rc = -EINVAL;
|
|
int role;
|
|
xce_reply_t reply;
|
|
xce_peerinfo_t* from = request->from;
|
|
|
|
init_reply_by_req(&reply, request);
|
|
|
|
priv = (ep_private_t*)ep->data;
|
|
if (priv == NULL) {
|
|
rc = -E_XCE_INTERNAL;
|
|
|
|
ERROR("priv data of endpoint should not be NULL");
|
|
|
|
reply.status = rc;
|
|
reply.data_which = RequestReply_data_nodata;
|
|
xce_reply(ep, c->myhost, &reply);
|
|
|
|
return rc;
|
|
}
|
|
|
|
role = request->body.joinmsg->role;
|
|
priv->role = role;
|
|
|
|
TRACE("%s role %d join from %s", __func__, role, from->name);
|
|
|
|
switch (role) {
|
|
case HostRole_client:
|
|
rc = handle_client_join(c, ep, request);
|
|
break;
|
|
case HostRole_worker:
|
|
rc = handle_worker_join(c, ep, request);
|
|
break;
|
|
case HostRole_scheduler:
|
|
rc = handle_scheduler_join(c, ep, request);
|
|
break;
|
|
case HostRole_controller:
|
|
rc = handle_controller_join(c, ep, request);
|
|
break;
|
|
case HostRole_apiserver:
|
|
rc = handle_apiserver_join(c, ep, request);
|
|
break;
|
|
default:
|
|
rc = -E_XCE_BAD_ROLE;
|
|
ERROR("unknown role %d from host %s", role, from->name);
|
|
|
|
reply.status = rc;
|
|
reply.data_which = RequestReply_data_nodata;
|
|
xce_reply(ep, c->myhost, &reply);
|
|
break;
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
int handle_join(cluster_t* c, endpoint_t* ep, xce_request_t* request)
|
|
{
|
|
int rc = 0;
|
|
|
|
rc = handle_join_(c, ep, request);
|
|
|
|
return rc;
|
|
}
|