-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_commands.py
152 lines (101 loc) · 3.99 KB
/
redis_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import redis
import json
import threading
class Log(object):
def __init__(self, my_id, my_ip, view, majority, host='localhost', port=6379, server_limit = 100, ):
self.r = redis.Redis(host=host, port=port, db=0, decode_responses=True)
self.r.set("last_used","-1")
self.r.set("server_limit", str(server_limit))
self.my_id = my_id
self.my_ip = my_ip
self.r.hset("-1", mapping = view)
self.r.set("majority", str(majority))
self.r.set("leader", my_ip)
def get_lock(self, location):
getting_lock = True
while getting_lock:
if self.r.setnx(str(location) + ".lock", "1") == 1:
getting_lock = False
# def get_min_proposal(self, location)
def release_lock(self, location):
self.r.delete(str(location) + ".lock")
def get_next_loc(self):
self.get_lock("-1")
last_used = int(self.r.get("last_used"))
next_free = last_used + 1
while self.r.exists(str(next_free)) and not (self.r.hexists(str(next_free), "value") == "N"):
next_free += 1
self.r.set("last_used", str(next_free))
self.release_lock("-1")
return next_free
def update_leader(self, view):
self.get_lock("leader_elect")
for x in view:
self.r.hset("-1", x, view[x])
new_leader = (None, -1)
for x in self.r.hkeys("-1"):
if int(self.r.hget("-1", x)) > new_leader[1]:
new_leader = (x, int(self.r.hget("-1", x)))
self.r.set("leader", new_leader[0])
self.release_lock("leader_elect")
def prepare_loc(self, location):
self.get_lock(location)
returnal = False
if self.r.exists(str(location)) == 0:
maps = {"base_proposal": "0", "min_proposal": "-1", "accepted_val": "N", "accepted_proposal": "-1", "already_accepted": "-1"}
self.r.hset(str(location), mapping=maps)
returnal = True
self.release_lock(location)
return returnal
def get_proposal(self, location):
self.get_lock(location)
server_limit = int(self.r.get("server_limit"))
base = int(self.r.hget(str(location), "base_proposal"))
base += 1
self.r.hset(str(location), "base_proposal", str(base))
self.release_lock(location)
return base * server_limit + int(self.my_id)
# def base_prop_update(self, location, new):
# self.get_lock(location)
# base = int(self.r.hget(str(location), "base_proposal"))
# if base < new:
# self.r.hset(str(location), "base_proposal", str(new))
# self.release_lock(location)
def update_value(self, location, proposal, value):
self.get_lock(location)
min_proposal = int(self.r.hget(str(location), "min_proposal"))
if proposal == min_proposal:
self.r.hset(str(location), "accepted_val", str(value))
self.r.hset(str(location), "accepted_proposal", str(proposal))
self.r.hset(str(location), "already_accepted", str(True))
self.release_lock(location)
return True
else:
self.release_lock(location)
return False
if __name__== "__main__":
# print("wtf")
temp = Log(my_id=2, server_limit=100)
temp.r.flushall()
temp = Log(my_id=2, server_limit=100)
# temp.r.flushall()
# print("wtf2")
# print(temp.get_next_loc())
# temp.r.flushall()
def conc(redis):
loc = redis.get_next_loc()
print(loc)
redis.prepare_loc(loc)
threads = []
for x in range(10):
thread = threading.Thread(target=conc, args=(temp,))
thread.start()
threads.append(thread)
for x in threads:
x.join()
temp.update_value(0,-1,"oogabooga")
print(temp.get_proposal(0))
for x in temp.r.scan_iter(_type="HASH"):
print(temp.r.hgetall(x))
# print(temp.r.hget("1", "accepted_val"))
# Continue work at accept