我正在尝试创建一个基本仿真,在该仿真中我生成伪节点,将其连接起来,并使用DHT方法为它们分配随机数量的进程。我以圆形链表的方式连接,我认为这样实现起来会更容易。我正在使用以下功能来实现DHT:
dc是具有哈希表和有关下一个节点的信息的节点类
def closest_dataCenter(dc,dcNext,key):
largestNode = hash_sha1(str(com_count))
dc_id = hash_sha1(str(dc.id))
dcNext_id = hash_sha1(str(dcNext.id))
if(dc_id>dcNext_id):
if(((largestNode)-dcNext_id-key) > (key-dc_id)):
return dc
else:
return dcNext
else:
if((key-dc_id) > (dcNext_id-key)):
return dcNext
else:
return dc
def find_dataCenter(dc,key):
current = dc
current_id = hash_sha1(str(current.id))
current_nextNode = hash_sha1(str(current.nextNode.id))
while(not (current_id<=key<current_nextNode)):
current_id = hash_sha1(str(current.id))
current_nextNode = hash_sha1(str(current.nextNode.id))
print("Key:" + str(key) + " \n")
print("Current id: " + str(current.id) + " Current id hash: " + str(current_id))
print("CurrentNext id: " + str(current.nextNode.id) + " CurrentNext id hash: " + str(current_nextNode))
time.sleep(1)
if(key>current_id>current_nextNode):
break
else:
current = current.nextNode
return closest_dataCenter(current,current.nextNode,key)
def store(start,key,value):
dc = find_dataCenter(start,key)
dc.hash_table[key]=value
def lookup(start,key):
dc = find_dataCenter(start,key)
return dc.hash_table[key]
在我的哈希函数中,我使用sha1哈希,然后在返回十六进制之前将其转换为整数。我在想,我会寄出钥匙和价值。密钥将被散列并找到最接近的节点ID对,然后找到最接近的节点。但是我想哈希不能那样工作,因为当我哈希某些键值时,它们有时找不到自己的位置并永远循环。例如:
Current id: 1 Current id hash: 304942582444936629325699363757435820077590259883
CurrentNext id: 2 CurrentNext id hash: 1246245281287062843477446394631337292330716631216
Key:62051490369831458547456710601409275698631100567
Current id: 2 Current id hash: 1246245281287062843477446394631337292330716631216
CurrentNext id: 3 CurrentNext id hash: 684329801336223661356952546078269889038938702779
Key:62051490369831458547456710601409275698631100567
Current id: 3 Current id hash: 684329801336223661356952546078269889038938702779
CurrentNext id: 4 CurrentNext id hash: 156380102318965990264666286018191900590658905210
Key:62051490369831458547456710601409275698631100567
Current id: 4 Current id hash: 156380102318965990264666286018191900590658905210
CurrentNext id: 5 CurrentNext id hash: 983116577831777608312765670515538102764663892676
Key:62051490369831458547456710601409275698631100567
Current id: 5 Current id hash: 983116577831777608312765670515538102764663892676
CurrentNext id: 6 CurrentNext id hash: 1106827226057151132207397296477425838227048555128
Key:62051490369831458547456710601409275698631100567
Current id: 6 Current id hash: 1106827226057151132207397296477425838227048555128
CurrentNext id: 7 CurrentNext id hash: 823067872317374799613180678352776801958854168538
Key:62051490369831458547456710601409275698631100567
Current id: 7 Current id hash: 823067872317374799613180678352776801958854168538
CurrentNext id: 8 CurrentNext id hash: 1452173985408750203318475117189636062911214042143
Key:62051490369831458547456710601409275698631100567
Current id: 8 Current id hash: 1452173985408750203318475117189636062911214042143
CurrentNext id: 1 CurrentNext id hash: 304942582444936629325699363757435820077590259883
Key:62051490369831458547456710601409275698631100567
我缺少DHT方法逻辑背后的东西吗,我应该编写自己的哈希函数还是不使用哈希?如何找到最接近我的哈希键的节点,如果所有内容都被哈希化,最接近的节点又有什么可能?如果有人可以向我解释DHT方法背后的逻辑,我将很高兴,并且我该如何将其应用于我的问题?预先感谢。
完整代码(如果需要):
import random
import time
import hashlib
com_count = random.randint(5,10)
process_count = random.randint(5,20)
###########################################################################################
class dataCenter:
def __init__(self,id):
self.nextNode = None
self.id=id
self.hash_table={}
class circularLinkedList:
def __init__(self):
self.head = None
def push(self,id):
ptr1 = dataCenter(id)
temp = self.head
ptr1.nextNode = self.head
if self.head is not None:
while(temp.nextNode != self.head):
temp = temp.nextNode
temp.nextNode = ptr1
else:
ptr1.nextNode = ptr1
self.head = ptr1
def printList(self):
temp = self.head
if self.head is not None:
while(True):
print("%d" %(temp.id))
temp = temp.nextNode
if (temp == self.head):
break
def find_next(self,id):
this_dc = self.head
while True:
if(this_dc.id == id):
print("ID: " + str(this_dc.id) + " connected to " + str(this_dc.nextNode.id))
break
elif(this_dc.nextNode == self.head):
return False
this_dc = this_dc.nextNode
def find(self,id):
this_dc = self.head
while True:
if(this_dc.id == id):
return this_dc
break
elif(this_dc.nextNode == self.head):
return False
this_dc = this_dc.nextNode
###########################################################################################
def print_connections_info(clist):
print ("Number of Data Centers: "+str(com_count))
for i in range(com_count+1):
cList.find_next(i)
###########################################################################################
def create_dc(com_count):
for i in range(com_count):
cList.push(com_count-i)
###########################################################################################
def hash_sha1(x):
hash_object = hashlib.sha1()
hash_object.update(bytes(x, encoding='utf-8'))
return int(hash_object.hexdigest(),16)
###########################################################################################
def closest_dataCenter(dc,dcNext,key):
largestNode = hash_sha1(str(com_count))
dc_id = hash_sha1(str(dc.id))
dcNext_id = hash_sha1(str(dcNext.id))
if(dc_id>dcNext_id):
if(((largestNode)-dcNext_id-key) > (key-dc_id)):
return dc
else:
return dcNext
else:
if((key-dc_id) > (dcNext_id-key)):
return dcNext
else:
return dc
###########################################################################################
def find_dataCenter(dc,key):
current = dc
current_id = hash_sha1(str(current.id))
current_nextNode = hash_sha1(str(current.nextNode.id))
while(not (current_id<=key<current_nextNode)):
current_id = hash_sha1(str(current.id))
current_nextNode = hash_sha1(str(current.nextNode.id))
print("Key:" + str(key) + " \n")
print("Current id: " + str(current.id) + " Current id hash: " + str(current_id))
print("CurrentNext id: " + str(current.nextNode.id) + " CurrentNext id hash: " + str(current_nextNode))
time.sleep(1)
if(key>current_id>current_nextNode):
break
else:
current = current.nextNode
return closest_dataCenter(current,current.nextNode,key)
###########################################################################################
def store(start,key,value):
dc = find_dataCenter(start,key)
dc.hash_table[key]=value
###########################################################################################
def lookup(start,key):
dc = find_dataCenter(start,key)
return dc.hash_table[key]
###########################################################################################
def create_process(pc_count):
li = []
for i in range(pc_count):
li.append("Process " + str(i))
return li
###########################################################################################
cList = circularLinkedList()
process_list = create_process(process_count)
create_dc(com_count)
print_connections_info(cList)
for i in range(len(process_list)):
store(cList.find(1),hash_sha1(str(i)),process_list[i])
print(cList.find(1))
print(hash_sha1(str(i)))
print(process_list[i])
print("**********************")
答案 0 :(得分:1)
我解决了更改此行的问题:
if(key>=current.id>current.nextNode.id):
break
这是永远循环的,因为当前的id有时等于key。