2022-11-30 14:28:27 +00:00
|
|
|
#!/bin/python3
|
|
|
|
|
|
|
|
import random
|
2022-12-20 10:13:54 +00:00
|
|
|
import collections
|
2022-12-20 10:25:44 +00:00
|
|
|
import logging
|
2022-11-30 14:28:27 +00:00
|
|
|
from DAS.block import *
|
2022-12-07 14:25:48 +00:00
|
|
|
from bitarray import bitarray
|
|
|
|
from bitarray.util import zeros
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-01-25 17:19:18 +00:00
|
|
|
class Neighbor:
|
|
|
|
|
|
|
|
def __repr__(self):
|
2023-01-25 20:38:21 +00:00
|
|
|
return "%d:%d/%d" % (self.node.ID, self.sent.count(1), self.received.count(1))
|
2023-01-25 17:19:18 +00:00
|
|
|
|
2023-01-25 20:36:53 +00:00
|
|
|
def __init__(self, v, blockSize):
|
2023-01-25 17:19:18 +00:00
|
|
|
self.node = v
|
2023-01-25 20:36:53 +00:00
|
|
|
self.received = zeros(blockSize)
|
|
|
|
self.sent = zeros(blockSize)
|
2023-01-25 17:19:18 +00:00
|
|
|
|
2022-11-30 14:28:27 +00:00
|
|
|
class Validator:
|
|
|
|
|
|
|
|
ID = 0
|
2023-01-13 15:51:27 +00:00
|
|
|
amIproposer = 0
|
2023-01-23 17:04:54 +00:00
|
|
|
shape = []
|
2022-11-30 14:28:27 +00:00
|
|
|
format = {}
|
|
|
|
logger = []
|
|
|
|
|
2023-01-25 20:38:21 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return str(self.ID)
|
|
|
|
|
2023-01-23 17:04:54 +00:00
|
|
|
def __init__(self, ID, amIproposer, logger, shape, rows, columns):
|
|
|
|
self.shape = shape
|
2022-11-30 14:28:27 +00:00
|
|
|
FORMAT = "%(levelname)s : %(entity)s : %(message)s"
|
|
|
|
self.ID = ID
|
|
|
|
self.format = {"entity": "Val "+str(self.ID)}
|
2023-01-23 17:04:54 +00:00
|
|
|
self.block = Block(self.shape.blockSize)
|
|
|
|
self.receivedBlock = Block(self.shape.blockSize)
|
2023-01-13 15:51:27 +00:00
|
|
|
self.amIproposer = amIproposer
|
2022-11-30 14:28:27 +00:00
|
|
|
self.logger = logger
|
2023-01-23 17:04:54 +00:00
|
|
|
if self.shape.chi < 1:
|
2022-11-30 14:28:27 +00:00
|
|
|
self.logger.error("Chi has to be greater than 0", extra=self.format)
|
2023-01-23 17:04:54 +00:00
|
|
|
elif self.shape.chi > self.shape.blockSize:
|
2022-11-30 14:28:27 +00:00
|
|
|
self.logger.error("Chi has to be smaller than %d" % blockSize, extra=self.format)
|
|
|
|
else:
|
2023-01-13 15:51:27 +00:00
|
|
|
if amIproposer:
|
2023-01-23 17:04:54 +00:00
|
|
|
self.rowIDs = range(shape.blockSize)
|
|
|
|
self.columnIDs = range(shape.blockSize)
|
2022-12-20 10:13:54 +00:00
|
|
|
else:
|
2023-01-23 17:04:54 +00:00
|
|
|
self.rowIDs = rows[(self.ID*self.shape.chi):(self.ID*self.shape.chi + self.shape.chi)]
|
|
|
|
self.columnIDs = rows[(self.ID*self.shape.chi):(self.ID*self.shape.chi + self.shape.chi)]
|
|
|
|
#if shape.deterministic:
|
|
|
|
# random.seed(self.ID)
|
|
|
|
#self.rowIDs = random.sample(range(self.shape.blockSize), self.shape.chi)
|
|
|
|
#self.columnIDs = random.sample(range(self.shape.blockSize), self.shape.chi)
|
2023-01-25 15:28:42 +00:00
|
|
|
self.changedRow = {id:False for id in self.rowIDs}
|
|
|
|
self.changedColumn = {id:False for id in self.columnIDs}
|
2023-01-25 20:36:53 +00:00
|
|
|
self.rowNeighbors = collections.defaultdict(dict)
|
|
|
|
self.columnNeighbors = collections.defaultdict(dict)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
|
|
|
def logIDs(self):
|
2023-01-13 15:51:27 +00:00
|
|
|
if self.amIproposer == 1:
|
2022-11-30 14:28:27 +00:00
|
|
|
self.logger.warning("I am a block proposer."% self.ID)
|
|
|
|
else:
|
|
|
|
self.logger.debug("Selected rows: "+str(self.rowIDs), extra=self.format)
|
|
|
|
self.logger.debug("Selected columns: "+str(self.columnIDs), extra=self.format)
|
|
|
|
|
|
|
|
def initBlock(self):
|
|
|
|
self.logger.debug("I am a block proposer.", extra=self.format)
|
2023-01-23 17:04:54 +00:00
|
|
|
self.block = Block(self.shape.blockSize)
|
2022-11-30 14:28:27 +00:00
|
|
|
self.block.fill()
|
|
|
|
#self.block.print()
|
|
|
|
|
2022-12-20 10:13:54 +00:00
|
|
|
def broadcastBlock(self):
|
2023-01-13 15:51:27 +00:00
|
|
|
if self.amIproposer == 0:
|
2022-11-30 14:28:27 +00:00
|
|
|
self.logger.error("I am NOT a block proposer", extra=self.format)
|
|
|
|
else:
|
|
|
|
self.logger.debug("Broadcasting my block...", extra=self.format)
|
2023-01-23 17:04:54 +00:00
|
|
|
order = [i for i in range(self.shape.blockSize * self.shape.blockSize)]
|
2022-11-30 14:28:27 +00:00
|
|
|
random.shuffle(order)
|
|
|
|
while(order):
|
|
|
|
i = order.pop()
|
2023-01-23 17:04:54 +00:00
|
|
|
if (random.randint(0,99) >= self.shape.failureRate):
|
2022-12-20 10:13:54 +00:00
|
|
|
self.block.data[i] = 1
|
|
|
|
else:
|
|
|
|
self.block.data[i] = 0
|
2023-01-11 16:20:19 +00:00
|
|
|
nbFailures = self.block.data.count(0)
|
2023-01-23 17:04:54 +00:00
|
|
|
measuredFailureRate = nbFailures * 100 / (self.shape.blockSize * self.shape.blockSize)
|
2023-01-16 21:43:52 +00:00
|
|
|
self.logger.debug("Number of failures: %d (%0.02f %%)", nbFailures, measuredFailureRate, extra=self.format)
|
2022-11-30 14:28:27 +00:00
|
|
|
#broadcasted.print()
|
2023-01-23 17:04:54 +00:00
|
|
|
for id in range(self.shape.blockSize):
|
2022-12-20 11:45:35 +00:00
|
|
|
self.sendColumn(id)
|
2023-01-23 17:04:54 +00:00
|
|
|
for id in range(self.shape.blockSize):
|
2022-12-20 11:45:35 +00:00
|
|
|
self.sendRow(id)
|
2022-12-20 10:13:54 +00:00
|
|
|
|
|
|
|
def getColumn(self, index):
|
|
|
|
return self.block.getColumn(index)
|
|
|
|
|
|
|
|
def getRow(self, index):
|
|
|
|
return self.block.getRow(index)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-01-25 20:36:53 +00:00
|
|
|
def receiveColumn(self, id, column, src):
|
2022-12-20 10:13:54 +00:00
|
|
|
if id in self.columnIDs:
|
2023-01-25 20:36:53 +00:00
|
|
|
# register receive so that we are not sending back
|
|
|
|
self.columnNeighbors[id][src].received |= column
|
2022-12-20 10:13:54 +00:00
|
|
|
self.receivedBlock.mergeColumn(id, column)
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
2023-01-25 20:36:53 +00:00
|
|
|
def receiveRow(self, id, row, src):
|
2022-12-20 10:13:54 +00:00
|
|
|
if id in self.rowIDs:
|
2023-01-25 20:36:53 +00:00
|
|
|
# register receive so that we are not sending back
|
|
|
|
self.rowNeighbors[id][src].received |= row
|
2022-12-20 10:13:54 +00:00
|
|
|
self.receivedBlock.mergeRow(id, row)
|
|
|
|
else:
|
|
|
|
pass
|
2022-11-30 14:28:27 +00:00
|
|
|
|
|
|
|
|
2022-12-20 10:13:54 +00:00
|
|
|
def receiveRowsColumns(self):
|
2023-01-13 15:51:27 +00:00
|
|
|
if self.amIproposer == 1:
|
2022-11-30 14:28:27 +00:00
|
|
|
self.logger.error("I am a block proposer", extra=self.format)
|
|
|
|
else:
|
|
|
|
self.logger.debug("Receiving the data...", extra=self.format)
|
2022-12-20 10:13:54 +00:00
|
|
|
#self.logger.debug("%s -> %s", self.block.data, self.receivedBlock.data, extra=self.format)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-01-25 15:28:42 +00:00
|
|
|
self.changedRow = { id:
|
|
|
|
self.getRow(id) != self.receivedBlock.getRow(id)
|
|
|
|
for id in self.rowIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
self.changedColumn = { id:
|
|
|
|
self.getColumn(id) != self.receivedBlock.getColumn(id)
|
|
|
|
for id in self.columnIDs
|
|
|
|
}
|
|
|
|
|
2022-12-20 10:13:54 +00:00
|
|
|
self.block.merge(self.receivedBlock)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2022-12-20 11:45:35 +00:00
|
|
|
def sendColumn(self, columnID):
|
2022-12-20 10:13:54 +00:00
|
|
|
line = self.getColumn(columnID)
|
|
|
|
if line.any():
|
|
|
|
self.logger.debug("col %d -> %s", columnID, self.columnNeighbors[columnID] , extra=self.format)
|
2023-01-25 20:36:53 +00:00
|
|
|
for n in self.columnNeighbors[columnID].values():
|
|
|
|
|
|
|
|
# if there is anything new to send, send it
|
|
|
|
toSend = line & ~n.sent & ~n.received
|
|
|
|
if (toSend).any():
|
|
|
|
n.sent |= toSend;
|
|
|
|
n.node.receiveColumn(columnID, toSend, self.ID)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2022-12-20 11:45:35 +00:00
|
|
|
def sendRow(self, rowID):
|
2022-12-20 10:13:54 +00:00
|
|
|
line = self.getRow(rowID)
|
|
|
|
if line.any():
|
|
|
|
self.logger.debug("row %d -> %s", rowID, self.rowNeighbors[rowID], extra=self.format)
|
2023-01-25 20:36:53 +00:00
|
|
|
for n in self.rowNeighbors[rowID].values():
|
|
|
|
|
|
|
|
# if there is anything new to send, send it
|
|
|
|
toSend = line & ~n.sent & ~n.received
|
|
|
|
if (toSend).any():
|
|
|
|
n.sent |= toSend;
|
|
|
|
n.node.receiveRow(rowID, toSend, self.ID)
|
2022-12-20 10:13:54 +00:00
|
|
|
|
|
|
|
def sendRows(self):
|
2023-01-13 15:51:27 +00:00
|
|
|
if self.amIproposer == 1:
|
2022-11-30 14:28:27 +00:00
|
|
|
self.logger.error("I am a block proposer", extra=self.format)
|
|
|
|
else:
|
|
|
|
self.logger.debug("Sending restored rows...", extra=self.format)
|
2022-12-20 11:45:35 +00:00
|
|
|
for r in self.rowIDs:
|
2023-01-25 15:28:42 +00:00
|
|
|
if self.changedRow[r]:
|
|
|
|
self.sendRow(r)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2022-12-20 10:13:54 +00:00
|
|
|
def sendColumns(self):
|
2023-01-13 15:51:27 +00:00
|
|
|
if self.amIproposer == 1:
|
2022-11-30 14:28:27 +00:00
|
|
|
self.logger.error("I am a block proposer", extra=self.format)
|
|
|
|
else:
|
|
|
|
self.logger.debug("Sending restored columns...", extra=self.format)
|
2022-12-20 11:45:35 +00:00
|
|
|
for c in self.columnIDs:
|
2023-01-25 15:28:42 +00:00
|
|
|
if self.changedColumn[c]:
|
|
|
|
self.sendColumn(c)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
|
|
|
def logRows(self):
|
2022-12-20 10:25:44 +00:00
|
|
|
if self.logger.isEnabledFor(logging.DEBUG):
|
|
|
|
for id in self.rowIDs:
|
|
|
|
self.logger.debug("Row %d: %s", id, self.getRow(id), extra=self.format)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
|
|
|
def logColumns(self):
|
2022-12-20 10:25:44 +00:00
|
|
|
if self.logger.isEnabledFor(logging.DEBUG):
|
|
|
|
for id in self.columnIDs:
|
|
|
|
self.logger.debug("Column %d: %s", id, self.getColumn(id), extra=self.format)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2022-12-07 14:46:45 +00:00
|
|
|
def restoreRows(self):
|
2022-12-20 10:13:54 +00:00
|
|
|
for id in self.rowIDs:
|
|
|
|
self.block.repairRow(id)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2022-12-07 14:46:45 +00:00
|
|
|
def restoreColumns(self):
|
2022-12-20 10:13:54 +00:00
|
|
|
for id in self.columnIDs:
|
|
|
|
self.block.repairColumn(id)
|
|
|
|
|
|
|
|
def checkStatus(self):
|
|
|
|
arrived = 0
|
|
|
|
expected = 0
|
|
|
|
for id in self.columnIDs:
|
|
|
|
line = self.getColumn(id)
|
|
|
|
arrived += line.count(1)
|
|
|
|
expected += len(line)
|
|
|
|
for id in self.rowIDs:
|
|
|
|
line = self.getRow(id)
|
|
|
|
arrived += line.count(1)
|
|
|
|
expected += len(line)
|
|
|
|
self.logger.debug("status: %d / %d", arrived, expected, extra=self.format)
|
|
|
|
|
|
|
|
return (arrived, expected)
|