-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.py
211 lines (184 loc) · 5.81 KB
/
Config.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
Due on Saturday, 12/03/2022
Author: Wonkwon Lee, Young Il Kim
"""
from enum import Enum
class Transaction(object):
"""
Transaction object that stores the transaction id, timestamp, and a flag to indicate read-only.
Args:
id (str): Transaction ID
ts (int): Timestamp of when the transaction began
is_ro (bool): Whether the transaction is read-only
"""
def __init__(self, id: str, ts: int, is_ro: bool):
"""
Constructor to initialize a transaction object.
"""
self.id = id # Transaction ID
self.ts = ts # Timestamp of when the transaction began
self.is_ro = is_ro # Flag to indicate whether the transaction is read-only
self.is_aborted = False # will abort if deadlock
self.visited_sites = [] # sites visited by this transaction
class Operation(object):
"""
Operation object that stores the operation type, transaction id, variable id, and value.
Args:
op (str): Operation type (R/W)
t_id (str): Transaction ID
v_id (int): Variable ID
val (int): Value to write (if write operation)
"""
def __init__(self, op: str, t_id: str, v_id: int, val: int=None):
"""
Constructor to initialize an operation object.
"""
self.op = op # Operation type (R/W)
self.t_id = t_id # Transaction ID
self.v_id = v_id # Variable ID
self.val = val # Value to write (if write operation)
class Variable(object):
"""
Variable object that stores the variable id, list of committed values, and flags to indicate readable and replicated
Args:
v_id (int): Variable ID
val (int): Initial value of the variable
replicated (bool): Flag to indicate whether the variable is replicated
"""
def __init__(self, v_id: int, val: int, replicated: bool):
"""
Constructor to initialize a variable object.
"""
self.v_id = v_id # Variable ID
self.val_list = [val] # List of stored committed values
self.readable = True # Flag to indicate whether the variable is readable
self.replicated = replicated # Flag to indicate whether the variable is replicated
self.fail = False # Flag to indicate whether the variable is failed
self.temp = None # Temporary value written by a transaction holding W-lock
class LockType(Enum):
"""
Enum for lock type.
"""
READ = 1
WRITE = 2
class TransactionType(Enum):
"""
Enum for transaction type.
"""
RO = 1
RW = 2
class TransactionStatus(Enum):
"""
Enum for transaction status.
"""
ACTIVE = 1
BLOCKED = 2
COMMITED = 3
ABORTED = 4
class DataType(Enum):
"""
Enum for replicated data type.
"""
REPLICATED = 0
NONREPLICATED = 1
class AbortType(Enum):
"""
Enum for abort type.
"""
DEADLOCK = 1
SITE_FAILURE = 2
NO_DATA_FOR_READ_ONLY = 3
class OperationType(Enum):
"""
Enum for operation type.
"""
BEGIN = 1
BEGINRO = 2
WRITE = 3
READ = 4
FAIL = 5
RECOVER = 6
DUMP = 7
END = 8
class Commit(object):
"""
Commit object that stores the value and timestamp of the commit operation.
Args:
val (int): Value to commit
ts (int): Timestamp of when the commit operation was executed
"""
def __init__(self, val, ts):
"""
Constructor to initialize a commit object.
"""
self.val = val
self.ts = ts
class Output(object):
"""
Output object that stores flag to indicate operation succeed and value of the operation.
Args:
succeed (bool): Flag to indicate whether the operation was successful
val (int): Value of the operation (if write operation)
"""
def __init__(self, succeed, val=None):
"""
Constructor to initialize an output object.
"""
self.succeed = succeed
self.val = val
class Temp(object):
"""
Temp object that stores the temporary value and transaction id of the write operation.
Args:
val (int): Temporary value written by a transaction holding the write lock
t_id (str): Transaction ID of the transaction holding the write lock
"""
def __init__(self, val, t_id):
"""
Constructor to initialize a temp object.
"""
self.val = val
self.t_id = t_id
class RLock(object):
"""
Read lock is a shared lock, so multiple transactions can share the same read lock.
Args:
t_id (int): Transaction ID
v_id (int): Variable ID
"""
def __init__(self, t_id: int, v_id: int):
"""
Constructor to initialize a read lock object.
"""
self.t_table = {t_id}
self.v_id = v_id
self.type = LockType.READ
class WLock(object):
"""
Write lock is an exclusive lock, so only one transaction can hold the write lock.
Args:
t_id (int): Transaction ID
v_id (int): Variable ID
"""
def __init__(self, t_id: int, v_id: int):
"""
Constructor to initialize a write lock object.
"""
self.t_id = t_id
self.v_id = v_id
self.type = LockType.WRITE
class QLock(object):
"""
Queue lock stores the transaction waiting for the lock.
Args:
t_id (int): Transaction ID
v_id (int): Variable ID
lock_type (LockType): Lock type (READ/WRITE)
"""
def __init__(self, t_id: int, v_id: int, lock_type: LockType):
"""
Constructor to initialize a lock queue object.
"""
self.t_id = t_id
self.v_id = v_id
self.type = lock_type