From: Ronnie sahlberg Date: Sun, 15 Apr 2007 04:39:23 +0000 (+1000) Subject: add examples for volker on how to do fetchlock/storeunlock X-Git-Tag: tevent-0.9.20~348^2~2926^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=01c12982a65e33ebcef45f14d7b50f60c95ef492;p=thirdparty%2Fsamba.git add examples for volker on how to do fetchlock/storeunlock note that the actual locking/unl;ocking does not still work (This used to be ctdb commit 45505520a69a2fbbb1e3a015b54d2133924f46a4) --- diff --git a/ctdb/direct/ctdbd.c b/ctdb/direct/ctdbd.c index 97c349c7eee..be687640908 100644 --- a/ctdb/direct/ctdbd.c +++ b/ctdb/direct/ctdbd.c @@ -43,6 +43,7 @@ static void block_signal(int signum) int main(int argc, const char *argv[]) { struct ctdb_context *ctdb; + struct ctdb_db_context *ctdb_db; const char *nlist = NULL; const char *transport = "tcp"; const char *myaddress = NULL; @@ -126,6 +127,13 @@ int main(int argc, const char *argv[]) exit(1); } + /* attach to a specific database */ + ctdb_db = ctdb_attach(ctdb, "test.tdb", TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0666); + if (!ctdb_db) { + printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb)); + exit(1); + } + /* start the protocol running */ ret = ctdb_start(ctdb); diff --git a/ctdb/direct/ctdbd_test.c b/ctdb/direct/ctdbd_test.c index a24d29a92b0..019cdad30dd 100644 --- a/ctdb/direct/ctdbd_test.c +++ b/ctdb/direct/ctdbd_test.c @@ -169,11 +169,130 @@ int receive_a_message(int fd, struct ctdb_req_message **preply) return 0; } +/* + hash function for mapping data to a VNN - taken from tdb +*/ +uint32_t ctdb_hash(const TDB_DATA *key) +{ + uint32_t value; /* Used to compute the hash value. */ + uint32_t i; /* Used to cycle through random values. */ + + /* Set the initial value from the key size. */ + for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++) + value = (value + (key->dptr[i] << (i*5 % 24))); + + 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); + + + /* 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) +{ + struct ctdb_req_store_unlock *req; + struct ctdb_reply_store_unlock *rep; + uint32_t length; + int len, cnt, tot; + + len = offsetof(struct ctdb_req_store_unlock, data) + key.dsize + data.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.reqid = 1; + req->db_id = db_id; + req->keylen = key.dsize; + req->datalen = data.dsize; + memcpy(&req->data[0], key.dptr, key.dsize); + memcpy(&req->data[key.dsize], data.dptr, data.dsize); + + cnt=write(fd, req, len); + + + /* 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("store unlock reply: state:%d\n",rep->state); +} + int main(int argc, const char *argv[]) { int fd, pid, 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]; /* open the socket to talk to the local ctdb daemon */ fd=ux_socket_connect(CTDB_SOCKET); @@ -203,13 +322,42 @@ int main(int argc, const char *argv[]) /* send a message to ourself */ dstvnn=vnn; dstpid=pid; - message.dptr="Test message"; - message.dsize=strlen(message.dptr)+1; + message.dptr=discard_const("Test message"); + message.dsize=strlen((const char *)message.dptr)+1; + printf("sending test message [%s] to ourself\n", message.dptr); send_a_message(fd, vnn, dstvnn, dstpid, message); - receive_a_message(fd, &reply); - /* wait for the message to come back */ + receive_a_message(fd, &reply); + printf("received message: [%s]\n",&reply->data[0]); + + /* create the db id for "test.tdb" */ + dbname.dptr = discard_const("test.tdb"); + dbname.dsize = strlen((const char *)(dbname.dptr)); + db_id = ctdb_hash(&dbname); + printf("the has for the database id is 0x%08x\n",db_id); + printf("\n"); + + /* send a fetch lock */ + 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); + printf("\n"); return 0;