ctdb-tests: Implement srvid_handler for dispatching messages master
authorAmitay Isaacs <amitay@gmail.com>
Tue, 5 May 2020 06:53:39 +0000 (16:53 +1000)
committerAmitay Isaacs <amitay@gmail.com>
Tue, 5 May 2020 06:53:39 +0000 (16:53 +1000)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
ctdb/tests/src/fake_ctdbd.c

index 146b7da344c62fd41da11e21ba67d23a0fcc81cf..4ef2f617f2c100e9022d19ae508703db1b72abe7 100644 (file)
@@ -1998,9 +1998,75 @@ done:
        client_send_control(req, header, &reply);
 }
 
+static void srvid_handler_done(struct tevent_req *subreq);
+
 static void srvid_handler(uint64_t srvid, TDB_DATA data, void *private_data)
 {
-       printf("Received a message for SRVID 0x%"PRIx64"\n", srvid);
+       struct client_state *state = talloc_get_type_abort(
+               private_data, struct client_state);
+       struct ctdbd_context *ctdb = state->ctdb;
+       struct tevent_req *subreq;
+       struct ctdb_req_header request_header;
+       struct ctdb_req_message_data message;
+       uint8_t *buf;
+       size_t datalen, buflen;
+       int ret;
+
+       request_header = (struct ctdb_req_header) {
+               .ctdb_magic = CTDB_MAGIC,
+               .ctdb_version = CTDB_PROTOCOL,
+               .generation = ctdb->vnn_map->generation,
+               .operation = CTDB_REQ_MESSAGE,
+               .destnode = state->pnn,
+               .srcnode = state->pnn,
+               .reqid = 0,
+       };
+
+       message = (struct ctdb_req_message_data) {
+               .srvid = srvid,
+               .data = data,
+       };
+
+       datalen = ctdb_req_message_data_len(&request_header, &message);
+       ret = ctdb_allocate_pkt(state, datalen, &buf, &buflen);
+       if (ret != 0) {
+               return;
+       }
+
+       ret = ctdb_req_message_data_push(&request_header,
+                                        &message,
+                                        buf,
+                                        &buflen);
+       if (ret != 0) {
+               talloc_free(buf);
+               return;
+       }
+
+       subreq = comm_write_send(state, state->ev, state->comm, buf, buflen);
+       if (subreq == NULL) {
+               talloc_free(buf);
+               return;
+       }
+       tevent_req_set_callback(subreq, srvid_handler_done, state);
+
+       talloc_steal(subreq, buf);
+}
+
+static void srvid_handler_done(struct tevent_req *subreq)
+{
+       struct client_state *state = tevent_req_data(
+               subreq, struct client_state);
+       int ret;
+       bool ok;
+
+       ok = comm_write_recv(subreq, &ret);
+       TALLOC_FREE(subreq);
+       if (!ok) {
+               DEBUG(DEBUG_ERR,
+                     ("Failed to dispatch message to client pid=%u, ret=%d\n",
+                      state->pid,
+                      ret));
+       }
 }
 
 static void control_register_srvid(TALLOC_CTX *mem_ctx,