make sure we don't have any namespace collision problems with config variables
[sahlberg/ctdb.git] / direct / ctdbd_test.c
index 019cdad30dd022bfbae5dbb7b13ff9d78225deb7..410603ad107afad4bd5a59d1c7d63548fc994f8d 100644 (file)
@@ -3,19 +3,19 @@
 
    Copyright (C) Andrew Tridgell  2006
 
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
-
-   This library is distributed in the hope that it will be useful,
+   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 2 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
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   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, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
 #include "includes.h"
@@ -51,21 +51,6 @@ static int ux_socket_connect(const char *name)
        return fd;
 }
 
-void register_pid_with_daemon(int fd, int pid)
-{
-       struct ctdb_req_register r;
-
-       bzero(&r, sizeof(r));
-       r.hdr.length       = sizeof(r);
-       r.hdr.ctdb_magic   = CTDB_MAGIC;
-       r.hdr.ctdb_version = CTDB_VERSION;
-       r.hdr.operation    = CTDB_REQ_REGISTER;
-       r.srvid            = pid;
-
-       /* XXX must deal with partial writes here */
-       write(fd, &r, sizeof(r));
-}
-
 /* send a command to the cluster to wait until all nodes are connected
    and the cluster is fully operational
  */
@@ -80,6 +65,7 @@ int wait_for_cluster(int fd)
        req.hdr.length       = sizeof(req);
        req.hdr.ctdb_magic   = CTDB_MAGIC;
        req.hdr.ctdb_version = CTDB_VERSION;
+       req.hdr.generation   = 1;
        req.hdr.operation    = CTDB_REQ_CONNECT_WAIT;
 
        /* XXX must deal with partial writes here */
@@ -119,6 +105,7 @@ int send_a_message(int fd, int ourvnn, int vnn, int pid, TDB_DATA data)
        r.hdr.length     = len;
        r.hdr.ctdb_magic = CTDB_MAGIC;
        r.hdr.ctdb_version = CTDB_VERSION;
+       r.hdr.generation = 1;
        r.hdr.operation  = CTDB_REQ_MESSAGE;
        r.hdr.destnode   = vnn;
        r.hdr.srcnode    = ourvnn;
@@ -184,78 +171,33 @@ uint32_t ctdb_hash(const TDB_DATA *key)
        return (1103515243 * value + 12345);  
 }
 
-void fetch_lock(int fd, uint32_t db_id, TDB_DATA key)
-{
-       struct ctdb_req_fetch_lock *req;
-       struct ctdb_reply_fetch_lock *rep;
-       uint32_t length;
-       int len, cnt, tot;
-
-       len = offsetof(struct ctdb_req_fetch_lock, key) + key.dsize;
-       req = malloc(len);
-
-       req->hdr.length      = len;
-       req->hdr.ctdb_magic  = CTDB_MAGIC;
-       req->hdr.ctdb_version = CTDB_VERSION;
-       req->hdr.operation   = CTDB_REQ_FETCH_LOCK;
-       req->hdr.reqid       = 1;
-       req->db_id           = db_id;
-       req->keylen          = key.dsize;
-       memcpy(&req->key[0], key.dptr, key.dsize);
-
-       cnt=write(fd, req, len);
+/* ask the daemon to migrate a record over so that the local node is the dmaster   the client must not have the record locked when performing this call.
 
-
-       /* wait fot the reply */
-       /* read the 4 bytes of length for the pdu */
-       cnt=0;
-       tot=4;
-       while(cnt!=tot){
-               int numread;
-               numread=read(fd, ((char *)&length)+cnt, tot-cnt);
-               if(numread>0){
-                       cnt+=numread;
-               }
-       }
-       /* read the rest of the pdu */
-       rep = malloc(length);
-       tot=length;
-       while(cnt!=tot){
-               int numread;
-               numread=read(fd, ((char *)rep)+cnt, tot-cnt);
-               if(numread>0){
-                       cnt+=numread;
-               }
-       }
-       printf("fetch lock reply: state:%d datalen:%d\n",rep->state,rep->datalen);
-       if(!rep->datalen){
-               printf("no data\n");
-       } else {
-               printf("data:[%s]\n",rep->data);
-       }
-
-}
-
-void store_unlock(int fd, uint32_t db_id, TDB_DATA key, TDB_DATA data)
+   when the daemon has responded   this node should be the dmaster (unless it has migrated off again)
+ */
+void fetch_record(int fd, uint32_t db_id, TDB_DATA key)
 {
-       struct ctdb_req_store_unlock *req;
-       struct ctdb_reply_store_unlock *rep;
+       struct ctdb_req_call *req;
+       struct ctdb_reply_call *rep;
        uint32_t length;
        int len, cnt, tot;
 
-       len = offsetof(struct ctdb_req_store_unlock, data) + key.dsize + data.dsize;
+       len = offsetof(struct ctdb_req_call, data) + key.dsize;
        req = malloc(len);
 
        req->hdr.length      = len;
        req->hdr.ctdb_magic  = CTDB_MAGIC;
        req->hdr.ctdb_version = CTDB_VERSION;
-       req->hdr.operation   = CTDB_REQ_STORE_UNLOCK;
+       req->hdr.generation  = 1;
+       req->hdr.operation   = CTDB_REQ_CALL;
        req->hdr.reqid       = 1;
+
+       req->flags           = CTDB_IMMEDIATE_MIGRATION;
        req->db_id           = db_id;
+       req->callid          = CTDB_NULL_FUNC;
        req->keylen          = key.dsize;
-       req->datalen         = data.dsize;
+       req->calldatalen     = 0;
        memcpy(&req->data[0], key.dptr, key.dsize);
-       memcpy(&req->data[key.dsize], data.dptr, data.dsize);
 
        cnt=write(fd, req, len);
 
@@ -281,18 +223,17 @@ void store_unlock(int fd, uint32_t db_id, TDB_DATA key, TDB_DATA data)
                        cnt+=numread;
                }
        }
-       printf("store unlock reply: state:%d\n",rep->state);
+       printf("fetch record reply: operation:%d state:%d\n",rep->hdr.operation,rep->status);
 }
 
 int main(int argc, const char *argv[])
 {
-       int fd, pid, vnn, dstvnn, dstpid;
+       int fd, pid=0, vnn, dstvnn, dstpid;
        TDB_DATA message;
        struct ctdb_req_message *reply;
        TDB_DATA dbname;
        uint32_t db_id;
-       TDB_DATA key, data;
-       char str[256];
+       TDB_DATA key;
 
        /* open the socket to talk to the local ctdb daemon */
        fd=ux_socket_connect(CTDB_SOCKET);
@@ -302,13 +243,6 @@ int main(int argc, const char *argv[])
        }
 
 
-       /* register our local server id with the daemon so that it knows
-          where to send messages addressed to our local pid.
-        */
-       pid=getpid();
-       register_pid_with_daemon(fd, pid);
-
-
        /* do a connect wait to ensure that all nodes in the cluster are up 
           and operational.
           this also tells us the vnn of the local cluster.
@@ -338,25 +272,12 @@ int main(int argc, const char *argv[])
        printf("the has for the database id is 0x%08x\n",db_id);
        printf("\n");
 
-       /* send a fetch lock */
+       /* send a request to migrate a record to the local node */
        key.dptr=discard_const("TestKey");
        key.dsize=strlen((const char *)(key.dptr));
        printf("fetch the test key:[%s]\n",key.dptr);
-       fetch_lock(fd, db_id, key);
-       printf("\n");
-
 
-       /* send a store unlock */
-       sprintf(str,"TestData_%d",getpid());
-       data.dptr=discard_const(str);
-       data.dsize=strlen((const char *)(data.dptr));
-       printf("store new data==[%s] for this record\n",data.dptr);
-       store_unlock(fd, db_id, key, data);
-       printf("\n");
-
-       /* send a fetch lock */
-       printf("fetch the test key:[%s]\n",key.dptr);
-       fetch_lock(fd, db_id, key);
+       fetch_record(fd, db_id, key);
        printf("\n");