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
|
2023-02-03 22:07:55 +00:00
|
|
|
import sys
|
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
|
2023-02-14 01:13:00 +00:00
|
|
|
from collections import deque
|
|
|
|
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-02-23 11:55:19 +00:00
|
|
|
def shuffled(lis, shuffle=True):
|
2023-02-15 02:25:52 +00:00
|
|
|
''' Generator yielding list in shuffled order
|
|
|
|
'''
|
2023-01-26 13:29:12 +00:00
|
|
|
# based on https://stackoverflow.com/a/60342323
|
2023-02-23 11:55:19 +00:00
|
|
|
if shuffle:
|
|
|
|
for index in random.sample(range(len(lis)), len(lis)):
|
|
|
|
yield lis[index]
|
|
|
|
else:
|
|
|
|
for v in lis:
|
|
|
|
yield v
|
2023-02-15 02:25:52 +00:00
|
|
|
def shuffledDict(d, shuffle=True):
|
|
|
|
''' Generator yielding dictionary in shuffled order
|
|
|
|
|
|
|
|
Shuffle, except if not (optional parameter useful for experiment setup)
|
|
|
|
'''
|
|
|
|
if shuffle:
|
|
|
|
lis = list(d.items())
|
|
|
|
for index in random.sample(range(len(d)), len(d)):
|
|
|
|
yield lis[index]
|
|
|
|
else:
|
|
|
|
for kv in d.items():
|
|
|
|
yield kv
|
2023-02-14 10:45:55 +00:00
|
|
|
|
2023-02-03 22:07:55 +00:00
|
|
|
def sampleLine(line, limit):
|
|
|
|
""" sample up to 'limit' bits from a bitarray
|
|
|
|
|
|
|
|
Since this is quite expensive, we use a number of heuristics to get it fast.
|
|
|
|
"""
|
|
|
|
if limit == sys.maxsize :
|
|
|
|
return line
|
|
|
|
else:
|
|
|
|
w = line.count(1)
|
|
|
|
if limit >= w :
|
|
|
|
return line
|
|
|
|
else:
|
|
|
|
l = len(line)
|
|
|
|
r = zeros(l)
|
|
|
|
if w < l/10 or limit > l/2 :
|
|
|
|
indices = [ i for i in range(l) if line[i] ]
|
|
|
|
sample = random.sample(indices, limit)
|
|
|
|
for i in sample:
|
|
|
|
r[i] = 1
|
|
|
|
return r
|
|
|
|
else:
|
|
|
|
while limit:
|
|
|
|
i = random.randrange(0, l)
|
|
|
|
if line[i] and not r[i]:
|
|
|
|
r[i] = 1
|
|
|
|
limit -= 1
|
|
|
|
return r
|
|
|
|
|
2023-01-26 13:29:12 +00:00
|
|
|
class NextToSend:
|
|
|
|
def __init__(self, neigh, toSend, id, dim):
|
|
|
|
self.neigh = neigh
|
|
|
|
self.toSend = toSend
|
|
|
|
self.id = id
|
|
|
|
self.dim = dim
|
2023-02-15 14:06:42 +00:00
|
|
|
|
2023-02-15 13:22:55 +00:00
|
|
|
class SegmentToSend:
|
|
|
|
def __init__(self, dim, id, i):
|
|
|
|
self.dim = dim
|
|
|
|
self.id = id
|
|
|
|
self.i = i
|
|
|
|
|
2023-01-25 17:19:18 +00:00
|
|
|
class Neighbor:
|
2023-02-15 14:06:42 +00:00
|
|
|
"""This class implements a node neighbor to monitor sent and received data."""
|
2023-01-25 17:19:18 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It returns the amount of sent and received data."""
|
2023-02-15 02:26:31 +00:00
|
|
|
return "%d:%d/%d, q:%d" % (self.node.ID, self.sent.count(1), self.received.count(1), len(self.sendQueue))
|
2023-01-25 17:19:18 +00:00
|
|
|
|
2023-02-15 02:10:55 +00:00
|
|
|
def __init__(self, v, dim, blockSize):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It initializes the neighbor with the node and sets counters to zero."""
|
2023-01-25 17:19:18 +00:00
|
|
|
self.node = v
|
2023-02-15 02:10:55 +00:00
|
|
|
self.dim = dim # 0:row 1:col
|
2023-01-26 09:08:19 +00:00
|
|
|
self.receiving = zeros(blockSize)
|
2023-01-25 20:36:53 +00:00
|
|
|
self.received = zeros(blockSize)
|
|
|
|
self.sent = zeros(blockSize)
|
2023-02-14 10:52:47 +00:00
|
|
|
self.sendQueue = deque()
|
2023-01-25 17:19:18 +00:00
|
|
|
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-02-15 14:06:42 +00:00
|
|
|
class Validator:
|
|
|
|
"""This class implements a validator/node in the network."""
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-01-25 20:38:21 +00:00
|
|
|
def __repr__(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It returns the validator ID."""
|
2023-01-25 20:38:21 +00:00
|
|
|
return str(self.ID)
|
|
|
|
|
2023-01-23 17:04:54 +00:00
|
|
|
def __init__(self, ID, amIproposer, logger, shape, rows, columns):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It initializes the validator with the logger, shape and assigned rows/columns."""
|
2023-01-23 17:04:54 +00:00
|
|
|
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-02-14 10:52:47 +00:00
|
|
|
self.receivedQueue = deque()
|
2023-02-14 01:13:00 +00:00
|
|
|
self.sendQueue = deque()
|
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)]
|
2023-02-16 16:32:57 +00:00
|
|
|
self.columnIDs = columns[(self.ID*self.shape.chi):(self.ID*self.shape.chi + self.shape.chi)]
|
2023-01-23 17:04:54 +00:00
|
|
|
#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 20:36:53 +00:00
|
|
|
self.rowNeighbors = collections.defaultdict(dict)
|
|
|
|
self.columnNeighbors = collections.defaultdict(dict)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-01-25 23:34:21 +00:00
|
|
|
#statistics
|
|
|
|
self.statsTxInSlot = 0
|
|
|
|
self.statsTxPerSlot = []
|
|
|
|
self.statsRxInSlot = 0
|
|
|
|
self.statsRxPerSlot = []
|
|
|
|
|
2023-01-26 13:29:12 +00:00
|
|
|
# Set uplink bandwidth. In segments (~560 bytes) per timestep (50ms?)
|
|
|
|
# 1 Mbps ~= 1e6 / 20 / 8 / 560 ~= 11
|
|
|
|
# TODO: this should be a parameter
|
2023-02-14 01:09:28 +00:00
|
|
|
self.bwUplink = 110 if not self.amIproposer else 2200 # approx. 10Mbps and 200Mbps
|
2023-01-26 13:29:12 +00:00
|
|
|
|
2023-02-15 02:23:39 +00:00
|
|
|
self.repairOnTheFly = True
|
2023-02-23 10:21:58 +00:00
|
|
|
self.sendLineUntil = (self.shape.blockSize + 1) // 2 # stop sending on a p2p link if at least this amount of samples passed
|
2023-02-14 10:52:47 +00:00
|
|
|
self.perNeighborQueue = False # queue incoming messages to outgoing connections on arrival (as typical GossipSub impl)
|
2023-02-14 10:51:04 +00:00
|
|
|
self.perNodeQueue = False # keep a global queue of incoming messages for later sequential dispatch
|
2023-02-15 02:25:52 +00:00
|
|
|
self.shuffleLines = True # shuffle the order of rows/columns in each iteration while trying to send
|
|
|
|
self.shuffleNeighbors = True # shuffle the order of neighbors when sending the same segment to each neighbor
|
2023-02-15 02:15:00 +00:00
|
|
|
self.dumbRandomScheduler = False # dumb random scheduler
|
2023-02-15 13:22:55 +00:00
|
|
|
self.segmentShuffleScheduler = True # send each segment that's worth sending once in shuffled order, then repeat
|
2023-02-15 13:28:38 +00:00
|
|
|
self.segmentShuffleSchedulerPersist = True # Persist scheduler state between timesteps
|
2023-01-26 13:29:12 +00:00
|
|
|
|
2022-11-30 14:28:27 +00:00
|
|
|
def logIDs(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It logs the assigned rows and columns."""
|
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):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It initializes the block for the proposer."""
|
|
|
|
if self.amIproposer == 1:
|
|
|
|
self.logger.debug("I am a block proposer.", extra=self.format)
|
|
|
|
self.block = Block(self.shape.blockSize)
|
|
|
|
self.block.fill()
|
|
|
|
#self.block.print()
|
|
|
|
else:
|
|
|
|
self.logger.warning("I am not a block proposer."% self.ID)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2022-12-20 10:13:54 +00:00
|
|
|
def broadcastBlock(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""The block proposer broadcasts the block to all validators."""
|
2023-01-13 15:51:27 +00:00
|
|
|
if self.amIproposer == 0:
|
2023-02-15 14:06:42 +00:00
|
|
|
self.logger.warning("I am not a block proposer", extra=self.format)
|
2022-11-30 14:28:27 +00:00
|
|
|
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-26 00:12:13 +00:00
|
|
|
|
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()
|
2022-12-20 10:13:54 +00:00
|
|
|
|
|
|
|
def getColumn(self, index):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It returns a given column."""
|
2022-12-20 10:13:54 +00:00
|
|
|
return self.block.getColumn(index)
|
|
|
|
|
|
|
|
def getRow(self, index):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It returns a given row."""
|
2022-12-20 10:13:54 +00:00
|
|
|
return self.block.getRow(index)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-02-14 01:11:44 +00:00
|
|
|
def receiveSegment(self, rID, cID, src):
|
|
|
|
# register receive so that we are not sending back
|
|
|
|
if rID in self.rowIDs:
|
|
|
|
if src in self.rowNeighbors[rID]:
|
|
|
|
self.rowNeighbors[rID][src].receiving[cID] = 1
|
|
|
|
if cID in self.columnIDs:
|
|
|
|
if src in self.columnNeighbors[cID]:
|
|
|
|
self.columnNeighbors[cID][src].receiving[rID] = 1
|
|
|
|
if not self.receivedBlock.getSegment(rID, cID):
|
2023-02-15 02:26:31 +00:00
|
|
|
self.logger.debug("Recv new: %d->%d: %d,%d", src, self.ID, rID, cID, extra=self.format)
|
2023-02-14 01:11:44 +00:00
|
|
|
self.receivedBlock.setSegment(rID, cID)
|
2023-02-15 13:17:17 +00:00
|
|
|
if self.perNodeQueue or self.perNeighborQueue:
|
|
|
|
self.receivedQueue.append((rID, cID))
|
2023-02-15 02:26:31 +00:00
|
|
|
else:
|
|
|
|
self.logger.debug("Recv DUP: %d->%d: %d,%d", src, self.ID, rID, cID, extra=self.format)
|
2023-02-14 01:11:44 +00:00
|
|
|
# self.statsRxDuplicateInSlot += 1
|
|
|
|
self.statsRxInSlot += 1
|
|
|
|
|
2023-02-15 02:18:00 +00:00
|
|
|
def addToSendQueue(self, rID, cID):
|
|
|
|
if self.perNodeQueue:
|
|
|
|
self.sendQueue.append((rID, cID))
|
|
|
|
|
|
|
|
if self.perNeighborQueue:
|
|
|
|
if rID in self.rowIDs:
|
|
|
|
for neigh in self.rowNeighbors[rID].values():
|
|
|
|
neigh.sendQueue.append(cID)
|
|
|
|
|
|
|
|
if cID in self.columnIDs:
|
|
|
|
for neigh in self.columnNeighbors[cID].values():
|
|
|
|
neigh.sendQueue.append(rID)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2022-12-20 10:13:54 +00:00
|
|
|
def receiveRowsColumns(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It receives rows and columns."""
|
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
|
|
|
|
2022-12-20 10:13:54 +00:00
|
|
|
self.block.merge(self.receivedBlock)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2023-01-26 09:08:19 +00:00
|
|
|
for neighs in self.rowNeighbors.values():
|
|
|
|
for neigh in neighs.values():
|
|
|
|
neigh.received |= neigh.receiving
|
|
|
|
neigh.receiving.setall(0)
|
|
|
|
|
|
|
|
for neighs in self.columnNeighbors.values():
|
|
|
|
for neigh in neighs.values():
|
|
|
|
neigh.received |= neigh.receiving
|
|
|
|
neigh.receiving.setall(0)
|
2023-02-15 14:06:42 +00:00
|
|
|
|
2023-02-14 01:13:00 +00:00
|
|
|
# add newly received segments to the send queue
|
2023-02-15 13:17:17 +00:00
|
|
|
if self.perNodeQueue or self.perNeighborQueue:
|
|
|
|
while self.receivedQueue:
|
|
|
|
(rID, cID) = self.receivedQueue.popleft()
|
|
|
|
self.addToSendQueue(rID, cID)
|
2023-02-14 01:13:00 +00:00
|
|
|
|
2023-01-25 23:34:21 +00:00
|
|
|
def updateStats(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It updates the stats related to sent and received data."""
|
2023-01-25 23:34:21 +00:00
|
|
|
self.logger.debug("Stats: tx %d, rx %d", self.statsTxInSlot, self.statsRxInSlot, extra=self.format)
|
|
|
|
self.statsRxPerSlot.append(self.statsRxInSlot)
|
|
|
|
self.statsTxPerSlot.append(self.statsTxInSlot)
|
|
|
|
self.statsRxInSlot = 0
|
|
|
|
self.statsTxInSlot = 0
|
|
|
|
|
2023-02-14 01:11:44 +00:00
|
|
|
def sendSegmentToNeigh(self, rID, cID, neigh):
|
2023-02-23 10:21:58 +00:00
|
|
|
if (neigh.sent | neigh.received).count(1) >= self.sendLineUntil:
|
|
|
|
return False # sent enough, other side can restore
|
2023-02-15 02:10:55 +00:00
|
|
|
if neigh.dim == 0: #row
|
|
|
|
i = cID
|
|
|
|
else:
|
|
|
|
i = rID
|
2023-02-15 13:19:21 +00:00
|
|
|
if not neigh.sent[i] and not neigh.received[i] :
|
2023-02-15 02:10:55 +00:00
|
|
|
neigh.sent[i] = 1
|
2023-02-14 01:11:44 +00:00
|
|
|
neigh.node.receiveSegment(rID, cID, self.ID)
|
|
|
|
self.statsTxInSlot += 1
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False # received or already sent
|
|
|
|
|
2023-01-26 13:29:12 +00:00
|
|
|
def send(self):
|
|
|
|
""" Send as much as we can in the timeslot, limited by bwUplink
|
|
|
|
"""
|
|
|
|
|
2023-02-15 13:34:24 +00:00
|
|
|
# process node level send queue
|
2023-02-14 01:13:00 +00:00
|
|
|
while self.sendQueue:
|
|
|
|
(rID, cID) = self.sendQueue[0]
|
|
|
|
|
|
|
|
if rID in self.rowIDs:
|
2023-02-15 02:25:52 +00:00
|
|
|
for _, neigh in shuffledDict(self.rowNeighbors[rID], self.shuffleNeighbors):
|
2023-02-14 01:13:00 +00:00
|
|
|
self.sendSegmentToNeigh(rID, cID, neigh)
|
|
|
|
|
|
|
|
if self.statsTxInSlot >= self.bwUplink:
|
|
|
|
return
|
|
|
|
|
|
|
|
if cID in self.columnIDs:
|
2023-02-15 02:25:52 +00:00
|
|
|
for _, neigh in shuffledDict(self.columnNeighbors[cID], self.shuffleNeighbors):
|
2023-02-14 01:13:00 +00:00
|
|
|
self.sendSegmentToNeigh(rID, cID, neigh)
|
|
|
|
|
|
|
|
if self.statsTxInSlot >= self.bwUplink:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.sendQueue.popleft()
|
|
|
|
|
2023-02-15 13:34:24 +00:00
|
|
|
# process neighbor level send queues in shuffled breadth-first order
|
2023-02-14 10:52:47 +00:00
|
|
|
progress = True
|
|
|
|
while (progress):
|
|
|
|
progress = False
|
2023-02-15 02:25:52 +00:00
|
|
|
for rID, neighs in shuffledDict(self.rowNeighbors, self.shuffleLines):
|
|
|
|
for _, neigh in shuffledDict(neighs, self.shuffleNeighbors):
|
2023-02-14 10:52:47 +00:00
|
|
|
if (neigh.sendQueue):
|
|
|
|
self.sendSegmentToNeigh(rID, neigh.sendQueue.popleft(), neigh)
|
|
|
|
progress = True
|
|
|
|
if self.statsTxInSlot >= self.bwUplink:
|
|
|
|
return
|
|
|
|
|
2023-02-15 02:25:52 +00:00
|
|
|
for cID, neighs in shuffledDict(self.columnNeighbors, self.shuffleLines):
|
|
|
|
for _, neigh in shuffledDict(neighs, self.shuffleNeighbors):
|
2023-02-14 10:52:47 +00:00
|
|
|
if (neigh.sendQueue):
|
|
|
|
self.sendSegmentToNeigh(neigh.sendQueue.popleft(), cID, neigh)
|
|
|
|
progress = True
|
|
|
|
if self.statsTxInSlot >= self.bwUplink:
|
|
|
|
return
|
|
|
|
|
2023-02-15 13:22:55 +00:00
|
|
|
# process possible segments to send in shuffled breadth-first order
|
|
|
|
if self.segmentShuffleScheduler:
|
2023-02-15 13:28:38 +00:00
|
|
|
# This scheduler check which owned segments needs sending (at least
|
|
|
|
# one neighbor needing it). Then it sends each segment that's worth sending
|
|
|
|
# once, in shuffled order. This is repeated until bw limit.
|
2023-02-15 13:22:55 +00:00
|
|
|
while True:
|
2023-02-16 08:19:45 +00:00
|
|
|
if hasattr(self, 'segmentShuffleGen') and self.segmentShuffleGen is not None:
|
|
|
|
#self.logger.debug("TX:%d queue:%d", self.statsTxInSlot, len(self.segmentsToSend), extra=self.format)
|
|
|
|
for s in self.segmentShuffleGen:
|
2023-02-15 13:28:38 +00:00
|
|
|
self.logger.debug("%d:%d/%d", s.dim, s.id, s.i, extra=self.format)
|
|
|
|
if s.dim == 0:
|
|
|
|
for _, neigh in shuffledDict(self.rowNeighbors[s.id], self.shuffleNeighbors):
|
|
|
|
self.logger.debug("%d or %d", neigh.sent[s.i], neigh.received[s.i], extra=self.format)
|
|
|
|
if not neigh.sent[s.i] and not neigh.received[s.i]:
|
2023-02-23 10:22:28 +00:00
|
|
|
if self.sendSegmentToNeigh(s.id, s.i, neigh):
|
|
|
|
self.logger.debug("sending to %d", neigh.node.ID, extra=self.format)
|
|
|
|
break
|
2023-02-15 13:28:38 +00:00
|
|
|
else:
|
|
|
|
for _, neigh in shuffledDict(self.columnNeighbors[s.id], self.shuffleNeighbors):
|
|
|
|
self.logger.debug("%d or %d", neigh.sent[s.i], neigh.received[s.i], extra=self.format)
|
|
|
|
if not neigh.sent[s.i] and not neigh.received[s.i]:
|
2023-02-23 10:22:28 +00:00
|
|
|
if self.sendSegmentToNeigh(s.i, s.id, neigh):
|
|
|
|
self.logger.debug("sending to %d", neigh.node.ID, extra=self.format)
|
|
|
|
break
|
2023-02-15 13:28:38 +00:00
|
|
|
|
|
|
|
if self.statsTxInSlot >= self.bwUplink:
|
|
|
|
if not self.segmentShuffleSchedulerPersist:
|
|
|
|
# remove scheduler state before leaving
|
|
|
|
self.segmentsToSend = []
|
2023-02-16 08:19:45 +00:00
|
|
|
self.segmentShuffleGen = None
|
2023-02-15 13:28:38 +00:00
|
|
|
return
|
|
|
|
|
2023-02-15 13:22:55 +00:00
|
|
|
self.segmentsToSend = []
|
|
|
|
for rID, neighs in self.rowNeighbors.items():
|
|
|
|
line = self.getRow(rID)
|
|
|
|
needed = zeros(self.shape.blockSize)
|
|
|
|
for neigh in neighs.values():
|
2023-02-23 10:21:58 +00:00
|
|
|
sentOrReceived = neigh.received | neigh.sent
|
|
|
|
if sentOrReceived.count(1) < self.sendLineUntil:
|
|
|
|
needed |= ~sentOrReceived
|
2023-02-15 13:22:55 +00:00
|
|
|
needed &= line
|
|
|
|
if (needed).any():
|
|
|
|
for i in range(len(needed)):
|
|
|
|
if needed[i]:
|
|
|
|
self.segmentsToSend.append(SegmentToSend(0, rID, i))
|
|
|
|
|
|
|
|
for cID, neighs in self.columnNeighbors.items():
|
|
|
|
line = self.getColumn(cID)
|
|
|
|
needed = zeros(self.shape.blockSize)
|
|
|
|
for neigh in neighs.values():
|
2023-02-23 10:21:58 +00:00
|
|
|
sentOrReceived = neigh.received | neigh.sent
|
|
|
|
if sentOrReceived.count(1) < self.sendLineUntil:
|
|
|
|
needed |= ~sentOrReceived
|
2023-02-15 13:22:55 +00:00
|
|
|
needed &= line
|
|
|
|
if (needed).any():
|
|
|
|
for i in range(len(needed)):
|
|
|
|
if needed[i]:
|
|
|
|
self.segmentsToSend.append(SegmentToSend(1, cID, i))
|
|
|
|
|
2023-02-15 13:28:38 +00:00
|
|
|
if not self.segmentsToSend:
|
2023-02-15 13:22:55 +00:00
|
|
|
break
|
2023-02-16 08:19:45 +00:00
|
|
|
else:
|
2023-02-23 11:55:19 +00:00
|
|
|
self.segmentShuffleGen = shuffled(self.segmentsToSend, self.shuffleLines)
|
2023-02-15 13:22:55 +00:00
|
|
|
|
2023-02-15 02:15:00 +00:00
|
|
|
if self.dumbRandomScheduler:
|
|
|
|
# dumb random scheduler picking segments at random and trying to send it
|
|
|
|
tries = 100
|
|
|
|
t = tries
|
|
|
|
while t:
|
|
|
|
if self.rowIDs:
|
|
|
|
rID = random.choice(self.rowIDs)
|
|
|
|
cID = random.randrange(0, self.shape.blockSize)
|
|
|
|
if self.block.getSegment(rID, cID) :
|
|
|
|
neigh = random.choice(list(self.rowNeighbors[rID].values()))
|
2023-02-15 13:19:21 +00:00
|
|
|
if not neigh.sent[cID] and not neigh.received[cID] :
|
2023-02-15 02:15:00 +00:00
|
|
|
neigh.sent[cID] = 1
|
|
|
|
neigh.node.receiveSegment(rID, cID, self.ID)
|
|
|
|
self.statsTxInSlot += 1
|
|
|
|
t = tries
|
|
|
|
if self.statsTxInSlot >= self.bwUplink:
|
|
|
|
return
|
|
|
|
if self.columnIDs:
|
|
|
|
cID = random.choice(self.columnIDs)
|
|
|
|
rID = random.randrange(0, self.shape.blockSize)
|
|
|
|
if self.block.getSegment(rID, cID) :
|
|
|
|
neigh = random.choice(list(self.columnNeighbors[cID].values()))
|
2023-02-15 13:19:21 +00:00
|
|
|
if not neigh.sent[rID] and not neigh.received[rID] :
|
2023-02-15 02:15:00 +00:00
|
|
|
neigh.sent[rID] = 1
|
|
|
|
neigh.node.receiveSegment(rID, cID, self.ID)
|
|
|
|
self.statsTxInSlot += 1
|
|
|
|
t = tries
|
|
|
|
t -= 1
|
|
|
|
if self.statsTxInSlot >= self.bwUplink:
|
|
|
|
return
|
|
|
|
return
|
2022-11-30 14:28:27 +00:00
|
|
|
|
|
|
|
def logRows(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It logs the rows assigned to the validator."""
|
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):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It logs the columns assigned to the validator."""
|
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):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It restores the rows assigned to the validator, that can be repaired."""
|
2023-02-15 02:23:39 +00:00
|
|
|
if self.repairOnTheFly:
|
|
|
|
for id in self.rowIDs:
|
|
|
|
rep = self.block.repairRow(id)
|
|
|
|
if (rep.any()):
|
|
|
|
# If operation is based on send queues, segments should
|
|
|
|
# be queued after successful repair.
|
|
|
|
for i in range(len(rep)):
|
|
|
|
if rep[i]:
|
|
|
|
self.logger.debug("Rep: %d,%d", id, i, extra=self.format)
|
|
|
|
self.addToSendQueue(id, i)
|
|
|
|
# self.statsRepairInSlot += rep.count(1)
|
2022-11-30 14:28:27 +00:00
|
|
|
|
2022-12-07 14:46:45 +00:00
|
|
|
def restoreColumns(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It restores the columns assigned to the validator, that can be repaired."""
|
2023-02-15 02:23:39 +00:00
|
|
|
if self.repairOnTheFly:
|
|
|
|
for id in self.columnIDs:
|
|
|
|
rep = self.block.repairColumn(id)
|
|
|
|
if (rep.any()):
|
|
|
|
# If operation is based on send queues, segments should
|
|
|
|
# be queued after successful repair.
|
|
|
|
for i in range(len(rep)):
|
|
|
|
if rep[i]:
|
|
|
|
self.logger.debug("Rep: %d,%d", i, id, extra=self.format)
|
|
|
|
self.addToSendQueue(i, id)
|
|
|
|
# self.statsRepairInSlot += rep.count(1)
|
2022-12-20 10:13:54 +00:00
|
|
|
|
|
|
|
def checkStatus(self):
|
2023-02-15 14:06:42 +00:00
|
|
|
"""It checks how many expected/arrived samples are for each assigned row/column."""
|
2022-12-20 10:13:54 +00:00
|
|
|
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)
|