📄 ctdb_call.c
字号:
/* ctdb_call protocol code Copyright (C) Andrew Tridgell 2006 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.*//* see http://wiki.samba.org/index.php/Samba_%26_Clustering for protocol design and packet details*/#include "includes.h"#include "lib/events/events.h"#include "lib/tdb/include/tdb.h"#include "lib/util/dlinklist.h"#include "system/network.h"#include "system/filesys.h"#include "../include/ctdb_private.h"/* find the ctdb_db from a db index */ struct ctdb_db_context *find_ctdb_db(struct ctdb_context *ctdb, uint32_t id){ struct ctdb_db_context *ctdb_db; for (ctdb_db=ctdb->db_list; ctdb_db; ctdb_db=ctdb_db->next) { if (ctdb_db->db_id == id) { break; } } return ctdb_db;}/* a varient of input packet that can be used in lock requeue*/static void ctdb_call_input_pkt(void *p, struct ctdb_req_header *hdr){ struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context); ctdb_input_pkt(ctdb, hdr);}/* send an error reply*/static void ctdb_send_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr, uint32_t status, const char *fmt, ...) PRINTF_ATTRIBUTE(4,5);static void ctdb_send_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr, uint32_t status, const char *fmt, ...){ va_list ap; struct ctdb_reply_error *r; char *msg; int msglen, len; va_start(ap, fmt); msg = talloc_vasprintf(ctdb, fmt, ap); if (msg == NULL) { ctdb_fatal(ctdb, "Unable to allocate error in ctdb_send_error\n"); } va_end(ap); msglen = strlen(msg)+1; len = offsetof(struct ctdb_reply_error, msg); r = ctdb_transport_allocate(ctdb, msg, CTDB_REPLY_ERROR, len + msglen, struct ctdb_reply_error); CTDB_NO_MEMORY_FATAL(ctdb, r); r->hdr.destnode = hdr->srcnode; r->hdr.reqid = hdr->reqid; r->status = status; r->msglen = msglen; memcpy(&r->msg[0], msg, msglen); ctdb_queue_packet(ctdb, &r->hdr); talloc_free(msg);}/* send a redirect reply*/static void ctdb_call_send_redirect(struct ctdb_context *ctdb, TDB_DATA key, struct ctdb_req_call *c, struct ctdb_ltdb_header *header){ uint32_t lmaster = ctdb_lmaster(ctdb, &key); if (ctdb->vnn == lmaster) { c->hdr.destnode = header->dmaster; } else if ((c->hopcount % ctdb->tunable.max_redirect_count) == 0) { c->hdr.destnode = lmaster; } else { c->hdr.destnode = header->dmaster; } c->hopcount++; ctdb_queue_packet(ctdb, &c->hdr);}/* send a dmaster reply caller must have the chainlock before calling this routine. Caller must be the lmaster*/static void ctdb_send_dmaster_reply(struct ctdb_db_context *ctdb_db, struct ctdb_ltdb_header *header, TDB_DATA key, TDB_DATA data, uint32_t new_dmaster, uint32_t reqid){ struct ctdb_context *ctdb = ctdb_db->ctdb; struct ctdb_reply_dmaster *r; int ret, len; TALLOC_CTX *tmp_ctx; if (ctdb->vnn != ctdb_lmaster(ctdb, &key)) { DEBUG(0,(__location__ " Caller is not lmaster!\n")); return; } header->dmaster = new_dmaster; ret = ctdb_ltdb_store(ctdb_db, key, header, data); if (ret != 0) { ctdb_fatal(ctdb, "ctdb_req_dmaster unable to update dmaster"); return; } /* put the packet on a temporary context, allowing us to safely free it below even if ctdb_reply_dmaster() has freed it already */ tmp_ctx = talloc_new(ctdb); /* send the CTDB_REPLY_DMASTER */ len = offsetof(struct ctdb_reply_dmaster, data) + key.dsize + data.dsize; r = ctdb_transport_allocate(ctdb, tmp_ctx, CTDB_REPLY_DMASTER, len, struct ctdb_reply_dmaster); CTDB_NO_MEMORY_FATAL(ctdb, r); r->hdr.destnode = new_dmaster; r->hdr.reqid = reqid; r->rsn = header->rsn; r->keylen = key.dsize; r->datalen = data.dsize; r->db_id = ctdb_db->db_id; memcpy(&r->data[0], key.dptr, key.dsize); memcpy(&r->data[key.dsize], data.dptr, data.dsize); ctdb_queue_packet(ctdb, &r->hdr); talloc_free(tmp_ctx);}/* send a dmaster request (give another node the dmaster for a record) This is always sent to the lmaster, which ensures that the lmaster always knows who the dmaster is. The lmaster will then send a CTDB_REPLY_DMASTER to the new dmaster*/static void ctdb_call_send_dmaster(struct ctdb_db_context *ctdb_db, struct ctdb_req_call *c, struct ctdb_ltdb_header *header, TDB_DATA *key, TDB_DATA *data){ struct ctdb_req_dmaster *r; struct ctdb_context *ctdb = ctdb_db->ctdb; int len; uint32_t lmaster = ctdb_lmaster(ctdb, key); if (lmaster == ctdb->vnn) { ctdb_send_dmaster_reply(ctdb_db, header, *key, *data, c->hdr.srcnode, c->hdr.reqid); return; } len = offsetof(struct ctdb_req_dmaster, data) + key->dsize + data->dsize; r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_DMASTER, len, struct ctdb_req_dmaster); CTDB_NO_MEMORY_FATAL(ctdb, r); r->hdr.destnode = lmaster; r->hdr.reqid = c->hdr.reqid; r->db_id = c->db_id; r->rsn = header->rsn; r->dmaster = c->hdr.srcnode; r->keylen = key->dsize; r->datalen = data->dsize; memcpy(&r->data[0], key->dptr, key->dsize); memcpy(&r->data[key->dsize], data->dptr, data->dsize); header->dmaster = c->hdr.srcnode; if (ctdb_ltdb_store(ctdb_db, *key, header, *data) != 0) { ctdb_fatal(ctdb, "Failed to store record in ctdb_call_send_dmaster"); } ctdb_queue_packet(ctdb, &r->hdr); talloc_free(r);}/* called when a CTDB_REPLY_DMASTER packet comes in, or when the lmaster gets a CTDB_REQUEST_DMASTER for itself. We become the dmaster. must be called with the chainlock held. This function releases the chainlock*/static void ctdb_become_dmaster(struct ctdb_db_context *ctdb_db, uint32_t reqid, TDB_DATA key, TDB_DATA data, uint64_t rsn){ struct ctdb_call_state *state; struct ctdb_context *ctdb = ctdb_db->ctdb; struct ctdb_ltdb_header header; DEBUG(2,("vnn %u dmaster response %08x\n", ctdb->vnn, ctdb_hash(&key))); ZERO_STRUCT(header); header.rsn = rsn + 1; header.dmaster = ctdb->vnn; if (ctdb_ltdb_store(ctdb_db, key, &header, data) != 0) { ctdb_fatal(ctdb, "ctdb_reply_dmaster store failed\n"); ctdb_ltdb_unlock(ctdb_db, key); return; } state = ctdb_reqid_find(ctdb, reqid, struct ctdb_call_state); if (state == NULL) { DEBUG(0,("vnn %u Invalid reqid %u in ctdb_become_dmaster\n", ctdb->vnn, reqid)); ctdb_ltdb_unlock(ctdb_db, key); return; } if (reqid != state->reqid) { /* we found a record but it was the wrong one */ DEBUG(0, ("Dropped orphan in ctdb_become_dmaster with reqid:%u\n",reqid)); ctdb_ltdb_unlock(ctdb_db, key); return; } ctdb_call_local(ctdb_db, &state->call, &header, state, &data, ctdb->vnn); ctdb_ltdb_unlock(ctdb_db, state->call.key); state->state = CTDB_CALL_DONE; if (state->async.fn) { state->async.fn(state); }}/* called when a CTDB_REQ_DMASTER packet comes in this comes into the lmaster for a record when the current dmaster wants to give up the dmaster role and give it to someone else*/void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr){ struct ctdb_req_dmaster *c = (struct ctdb_req_dmaster *)hdr; TDB_DATA key, data, data2; struct ctdb_ltdb_header header; struct ctdb_db_context *ctdb_db; int ret; key.dptr = c->data; key.dsize = c->keylen; data.dptr = c->data + c->keylen; data.dsize = c->datalen; ctdb_db = find_ctdb_db(ctdb, c->db_id); if (!ctdb_db) { ctdb_send_error(ctdb, hdr, -1, "Unknown database in request. db_id==0x%08x", c->db_id); return; } /* fetch the current record */ ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header, hdr, &data2, ctdb_call_input_pkt, ctdb, False); if (ret == -1) { ctdb_fatal(ctdb, "ctdb_req_dmaster failed to fetch record"); return; } if (ret == -2) { DEBUG(2,(__location__ " deferring ctdb_request_dmaster\n")); return; } if (ctdb_lmaster(ctdb, &key) != ctdb->vnn) { DEBUG(0,("vnn %u dmaster request to non-lmaster lmaster=%u gen=%u curgen=%u\n", ctdb->vnn, ctdb_lmaster(ctdb, &key), hdr->generation, ctdb->vnn_map->generation)); ctdb_fatal(ctdb, "ctdb_req_dmaster to non-lmaster"); } DEBUG(2,("vnn %u dmaster request on %08x for %u from %u\n", ctdb->vnn, ctdb_hash(&key), c->dmaster, c->hdr.srcnode)); /* its a protocol error if the sending node is not the current dmaster */ if (header.dmaster != hdr->srcnode) { DEBUG(0,("vnn %u dmaster request non-master %u dmaster=%u key %08x dbid 0x%08x gen=%u curgen=%u\n", ctdb->vnn, hdr->srcnode, header.dmaster, ctdb_hash(&key), ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation)); ctdb_fatal(ctdb, "ctdb_req_dmaster from non-master"); return; } /* use the rsn from the sending node */ header.rsn = c->rsn; /* check if the new dmaster is the lmaster, in which case we skip the dmaster reply */ if (c->dmaster == ctdb->vnn) { ctdb_become_dmaster(ctdb_db, hdr->reqid, key, data, c->rsn); } else { ctdb_send_dmaster_reply(ctdb_db, &header, key, data, c->dmaster, hdr->reqid); ctdb_ltdb_unlock(ctdb_db, key); }}/* called when a CTDB_REQ_CALL packet comes in*/void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr){ struct ctdb_req_call *c = (struct ctdb_req_call *)hdr; TDB_DATA data; struct ctdb_reply_call *r; int ret, len; struct ctdb_ltdb_header header; struct ctdb_call call; struct ctdb_db_context *ctdb_db; ctdb_db = find_ctdb_db(ctdb, c->db_id); if (!ctdb_db) { ctdb_send_error(ctdb, hdr, -1, "Unknown database in request. db_id==0x%08x", c->db_id); return; } call.call_id = c->callid; call.key.dptr = c->data; call.key.dsize = c->keylen; call.call_data.dptr = c->data + c->keylen;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -