From 4970c7e9a65b54a73988c14b4c2ccd58480b5281 Mon Sep 17 00:00:00 2001 From: colm Date: Fri, 23 Dec 2016 11:45:17 -0500 Subject: [PATCH] FS-9775: Exclude newly created nodes from find --- libs/libks/src/dht/ks_dht_bucket.c | 14 ++++++++++---- libs/libks/test/testbuckets.c | 20 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/libs/libks/src/dht/ks_dht_bucket.c b/libs/libks/src/dht/ks_dht_bucket.c index 32e2183e5e..5e6378627c 100644 --- a/libs/libks/src/dht/ks_dht_bucket.c +++ b/libs/libks/src/dht/ks_dht_bucket.c @@ -377,6 +377,11 @@ KS_DECLARE(ks_status_t) ks_dhtrt_delete_node(ks_dhtrt_routetable_t *table, ks_dh ks_rwl_read_unlock(internal->lock); /* release write lock */ /* at this point no subsequent find/query will return the node */ + if (s == KS_STATUS_FAIL) { + ks_log(KS_LOG_DEBUG, "Delete node: node not found\n"); + return KS_STATUS_FAIL; /* cannot delete what we cannot find */ + } + ks_dhtrt_queue_node_fordelete(table, node); return s; } @@ -856,11 +861,13 @@ uint8_t ks_dhtrt_findclosest_locked_nodes(ks_dhtrt_routetable_t *table, ks_dhtrt KS_DECLARE(ks_status_t) ks_dhtrt_release_node(ks_dht_node_t* node) { + assert(node); return ks_rwl_read_unlock(node->reflock); } KS_DECLARE(ks_status_t) ks_dhtrt_sharelock_node(ks_dht_node_t* node) { + assert(node); return ks_rwl_read_lock(node->reflock); } @@ -1257,9 +1264,7 @@ void ks_dhtrt_split_bucket(ks_dhtrt_bucket_header_t *original, if (ks_dhtrt_ismasked(source->entries[rix].id, left->mask)) { /* move it to the left */ - memcpy(dest->entries[lix].id, source->entries[rix].id, KS_DHT_NODEID_SIZE); - dest->entries[lix].gptr = source->entries[rix].gptr; - dest->entries[lix].inuse = 1; + memcpy(&dest->entries[lix], &source->entries[rix], sizeof(ks_dhtrt_bucket_entry_t)); ++lix; ++dest->count; @@ -1371,7 +1376,8 @@ ks_dht_node_t *ks_dhtrt_find_nodeid(ks_dhtrt_bucket_t *bucket, ks_dhtrt_nodeid_t bucket->entries[ix].inuse ); } #endif - if ( bucket->entries[ix].inuse == 1 && + if ( bucket->entries[ix].inuse == 1 && + bucket->entries[ix].flags == DHTPEER_ACTIVE && (!memcmp(id, bucket->entries[ix].id, KS_DHT_NODEID_SIZE)) ) { return bucket->entries[ix].gptr; } diff --git a/libs/libks/test/testbuckets.c b/libs/libks/test/testbuckets.c index 421371be38..b744ee78e9 100644 --- a/libs/libks/test/testbuckets.c +++ b/libs/libks/test/testbuckets.c @@ -52,11 +52,19 @@ void test01() } peer = ks_dhtrt_find_node(rt, homeid); - if (peer == 0) { - printf("*** ks_dhtrt_find_node test01 failed \n"); fflush(stdout); + if (peer != 0) { + printf("*** ks_dhtrt_find_node test01 failed. find should fail\n"); fflush(stdout); exit(102); } + ks_dhtrt_touch_node(rt, homeid); + + peer = ks_dhtrt_find_node(rt, homeid); + if (peer == 0) { + printf("*** ks_dhtrt_find_node test01 failed. find should succeed\n"); fflush(stdout); + exit(102); + } + status = ks_dhtrt_create_node(rt, homeid, KS_DHT_LOCAL, ip, port, &peer1); if (status == KS_STATUS_FAIL) { printf("**** ks_dhtrt_create_node test01 did allow duplicate createnodes!!\n"); @@ -325,6 +333,7 @@ void test05() unsigned short port = 7001; ks_dhtrt_create_node(rt, nodeid, KS_DHT_REMOTE, ipv4, port, &peer); + ks_dhtrt_touch_node(rt, nodeid); peer1 = ks_dhtrt_find_node(rt, nodeid); printf("test05 - first find compelete\n"); fflush(stdout); @@ -341,6 +350,10 @@ void test05() s = ks_dhtrt_release_node(peer2); if (s == KS_STATUS_FAIL) printf("release 1 failed\n"); + s = ks_dhtrt_release_node(peer2); + if (s == KS_STATUS_FAIL) printf("release 1 failed\n"); + + printf("* **testbuckets - test05 finished\n\n\n"); fflush(stdout); return; @@ -440,6 +453,7 @@ void test07() for(int i0=0, i1=0; i0<150; ++i0, ++i1) { if (i0%20 == 0) { g_nodeid2.id[0]>>=1; + ks_dhtrt_dump(rt, 7); } else { ++ g_nodeid2.id[19]; @@ -449,6 +463,8 @@ void test07() ks_dhtrt_release_node(peer); } + ks_dhtrt_dump(rt, 7); + memset(g_nodeid2.id, 0xef, KS_DHT_NODEID_SIZE); for (int i0=0, i1=0; i0<150; ++i0, ++i1) { if (i0%20 == 0) {