Adding configuration class

This commit is contained in:
leobago 2023-01-13 16:51:27 +01:00
parent d119d53693
commit e8e82d7460
7 changed files with 92 additions and 72 deletions

View File

@ -1 +1,2 @@
from DAS.simulator import * from DAS.simulator import *
from DAS.configuration import *

View File

@ -9,8 +9,8 @@ class Block:
blockSize = 0 blockSize = 0
data = bitarray() data = bitarray()
def __init__(self, size): def __init__(self, blockSize):
self.blockSize = size self.blockSize = blockSize
self.data = zeros(self.blockSize*self.blockSize) self.data = zeros(self.blockSize*self.blockSize)
def fill(self): def fill(self):

30
DAS/configuration.py Normal file
View File

@ -0,0 +1,30 @@
#!/bin/python3
class Configuration:
blockSize = 0
failureRateStep = 0
maxTries = 0
numberValidators = 0
chi = 0
failureRate = 0
deterministic = 0
def __init__(self, blockSize, failureRateStep, maxTries, numberValidators, chi, failureRate, deterministic):
if numberValidators < (blockSize*4):
print("ERROR: The number of validators cannot be lower than the block size * 4")
exit(1)
if chi < 1:
print("Chi has to be greater than 0")
exit(1)
if chi > blockSize:
print("Chi has to be smaller than %d" % blockSize)
exit(1)
self.blockSize = blockSize
self.failureRateStep = failureRateStep
self.maxTries = maxTries
self.numberValidators = numberValidators
self.chi = chi
self.failureRate = failureRate
self.deterministic = deterministic

View File

@ -5,40 +5,40 @@ from DAS.block import *
class Observer: class Observer:
block = [] block = []
blockSize = 0
rows = [] rows = []
columns = [] columns = []
goldenData = [] goldenData = []
broadcasted = [] broadcasted = []
config = []
logger = [] logger = []
def __init__(self, blockSize, logger): def __init__(self, logger, config):
self.config = config
self.format = {"entity": "Observer"} self.format = {"entity": "Observer"}
self.blockSize = blockSize
self.logger = logger self.logger = logger
def reset(self): def reset(self):
self.block = [0] * self.blockSize * self.blockSize self.block = [0] * self.config.blockSize * self.config.blockSize
self.goldenData = [0] * self.blockSize * self.blockSize self.goldenData = [0] * self.config.blockSize * self.config.blockSize
self.rows = [0] * self.blockSize self.rows = [0] * self.config.blockSize
self.columns = [0] * self.blockSize self.columns = [0] * self.config.blockSize
self.broadcasted = Block(self.blockSize) self.broadcasted = Block(self.config.blockSize)
def checkRowsColumns(self, validators): def checkRowsColumns(self, validators):
for val in validators: for val in validators:
if val.proposer == 0: if val.amIproposer == 0:
for r in val.rowIDs: for r in val.rowIDs:
self.rows[r] += 1 self.rows[r] += 1
for c in val.columnIDs: for c in val.columnIDs:
self.columns[c] += 1 self.columns[c] += 1
for i in range(self.blockSize): for i in range(self.config.blockSize):
self.logger.debug("Row/Column %d have %d and %d validators assigned." % (i, self.rows[i], self.columns[i]), extra=self.format) self.logger.debug("Row/Column %d have %d and %d validators assigned." % (i, self.rows[i], self.columns[i]), extra=self.format)
if self.rows[i] == 0 or self.columns[i] == 0: if self.rows[i] == 0 or self.columns[i] == 0:
self.logger.warning("There is a row/column that has not been assigned", extra=self.format) self.logger.warning("There is a row/column that has not been assigned", extra=self.format)
def setGoldenData(self, block): def setGoldenData(self, block):
for i in range(self.blockSize*self.blockSize): for i in range(self.config.blockSize*self.config.blockSize):
self.goldenData[i] = block.data[i] self.goldenData[i] = block.data[i]
def checkBroadcasted(self): def checkBroadcasted(self):
@ -54,7 +54,7 @@ class Observer:
arrived = 0 arrived = 0
expected = 0 expected = 0
for val in validators: for val in validators:
if val.proposer == 0: if val.amIproposer == 0:
(a, e) = val.checkStatus() (a, e) = val.checkStatus()
arrived += a arrived += a
expected += e expected += e

View File

@ -9,32 +9,26 @@ from DAS.validator import *
class Simulator: class Simulator:
chi = 8
blockSize = 256
numberValidators = 8192
failureRate = 0
proposerID = 0 proposerID = 0
logLevel = logging.INFO logLevel = logging.INFO
deterministic = 0
validators = [] validators = []
glob = [] glob = []
config = []
logger = [] logger = []
format = {} format = {}
steps = 0
def __init__(self, failureRate): def __init__(self, config):
self.failureRate = failureRate self.config = config
self.format = {"entity": "Simulator"} self.format = {"entity": "Simulator"}
self.steps = 0
def initValidators(self): def initValidators(self):
if not self.deterministic: if not self.config.deterministic:
random.seed(datetime.now()) random.seed(datetime.now())
self.glob = Observer(self.blockSize, self.logger) self.glob = Observer(self.logger, self.config)
self.glob.reset() self.glob.reset()
self.validators = [] self.validators = []
for i in range(self.numberValidators): for i in range(self.config.numberValidators):
val = Validator(i, self.chi, self.blockSize, int(not i!=0), self.failureRate, self.deterministic, self.logger) val = Validator(i, int(not i!=0), self.logger, self.config)
if i == self.proposerID: if i == self.proposerID:
val.initBlock() val.initBlock()
self.glob.setGoldenData(val.block) self.glob.setGoldenData(val.block)
@ -43,15 +37,15 @@ class Simulator:
self.validators.append(val) self.validators.append(val)
def initNetwork(self, d=6): def initNetwork(self, d=6):
rowChannels = [[] for i in range(self.blockSize)] rowChannels = [[] for i in range(self.config.blockSize)]
columnChannels = [[] for i in range(self.blockSize)] columnChannels = [[] for i in range(self.config.blockSize)]
for v in self.validators: for v in self.validators:
for id in v.rowIDs: for id in v.rowIDs:
rowChannels[id].append(v) rowChannels[id].append(v)
for id in v.columnIDs: for id in v.columnIDs:
columnChannels[id].append(v) columnChannels[id].append(v)
for id in range(self.blockSize): for id in range(self.config.blockSize):
G = nx.random_regular_graph(d, len(rowChannels[id])) G = nx.random_regular_graph(d, len(rowChannels[id]))
if not nx.is_connected(G): if not nx.is_connected(G):
self.logger.error("graph not connected for row %d !" % id, extra=self.format) self.logger.error("graph not connected for row %d !" % id, extra=self.format)
@ -86,12 +80,12 @@ class Simulator:
self.validators[self.proposerID].broadcastBlock() self.validators[self.proposerID].broadcastBlock()
arrived, expected = self.glob.checkStatus(self.validators) arrived, expected = self.glob.checkStatus(self.validators)
missingSamples = expected - arrived missingSamples = expected - arrived
self.steps = 0 steps = 0
while(missingSamples > 0): while(missingSamples > 0):
oldMissingSamples = missingSamples oldMissingSamples = missingSamples
for i in range(1,self.numberValidators): for i in range(1,self.config.numberValidators):
self.validators[i].receiveRowsColumns() self.validators[i].receiveRowsColumns()
for i in range(1,self.numberValidators): for i in range(1,self.config.numberValidators):
self.validators[i].restoreRows() self.validators[i].restoreRows()
self.validators[i].restoreColumns() self.validators[i].restoreColumns()
self.validators[i].sendRows() self.validators[i].sendRows()
@ -102,16 +96,16 @@ class Simulator:
arrived, expected = self.glob.checkStatus(self.validators) arrived, expected = self.glob.checkStatus(self.validators)
missingSamples = expected - arrived missingSamples = expected - arrived
missingRate = missingSamples*100/expected missingRate = missingSamples*100/expected
self.logger.info("step %d, missing %d of %d (%0.02f %%)" % (self.steps, missingSamples, expected, missingRate), extra=self.format) self.logger.info("step %d, missing %d of %d (%0.02f %%)" % (steps, missingSamples, expected, missingRate), extra=self.format)
if missingSamples == oldMissingSamples: if missingSamples == oldMissingSamples:
break break
elif missingSamples == 0: elif missingSamples == 0:
break break
else: else:
self.steps += 1 steps += 1
if missingSamples == 0: if missingSamples == 0:
self.logger.debug("The entire block is available at step %d, with failure rate %d !" % (self.steps, self.failureRate), extra=self.format) self.logger.debug("The entire block is available at step %d, with failure rate %d !" % (steps, self.failureRate), extra=self.format)
return 0 return 0
else: else:
self.logger.debug("The block cannot be recovered, failure rate %d!" % self.failureRate, extra=self.format) self.logger.debug("The block cannot be recovered, failure rate %d!" % self.failureRate, extra=self.format)

View File

@ -10,44 +10,40 @@ from bitarray.util import zeros
class Validator: class Validator:
ID = 0 ID = 0
chi = 0 amIproposer = 0
config = []
format = {} format = {}
blocksize = 0
proposer = 0
failureRate = 0
logger = [] logger = []
def __init__(self, ID, chi, blockSize, proposer, failureRate, deterministic, logger): def __init__(self, ID, amIproposer, logger, config):
self.config = config
FORMAT = "%(levelname)s : %(entity)s : %(message)s" FORMAT = "%(levelname)s : %(entity)s : %(message)s"
self.ID = ID self.ID = ID
self.format = {"entity": "Val "+str(self.ID)} self.format = {"entity": "Val "+str(self.ID)}
self.blockSize = blockSize self.block = Block(self.config.blockSize)
self.block = Block(blockSize) self.receivedBlock = Block(self.config.blockSize)
self.receivedBlock = Block(blockSize) self.amIproposer = amIproposer
self.proposer = proposer
self.failureRate = failureRate
self.logger = logger self.logger = logger
if chi < 1: if self.config.chi < 1:
self.logger.error("Chi has to be greater than 0", extra=self.format) self.logger.error("Chi has to be greater than 0", extra=self.format)
elif chi > blockSize: elif self.config.chi > self.config.blockSize:
self.logger.error("Chi has to be smaller than %d" % blockSize, extra=self.format) self.logger.error("Chi has to be smaller than %d" % blockSize, extra=self.format)
else: else:
self.chi = chi if amIproposer:
if proposer: self.rowIDs = range(config.blockSize)
self.rowIDs = range(blockSize) self.columnIDs = range(config.blockSize)
self.columnIDs = range(blockSize)
else: else:
self.rowIDs = [] self.rowIDs = []
self.columnIDs = [] self.columnIDs = []
if deterministic: if config.deterministic:
random.seed(self.ID) random.seed(self.ID)
self.rowIDs = random.sample(range(self.blockSize), self.chi) self.rowIDs = random.sample(range(self.config.blockSize), self.config.chi)
self.columnIDs = random.sample(range(self.blockSize), self.chi) self.columnIDs = random.sample(range(self.config.blockSize), self.config.chi)
self.rowNeighbors = collections.defaultdict(list) self.rowNeighbors = collections.defaultdict(list)
self.columnNeighbors = collections.defaultdict(list) self.columnNeighbors = collections.defaultdict(list)
def logIDs(self): def logIDs(self):
if self.proposer == 1: if self.amIproposer == 1:
self.logger.warning("I am a block proposer."% self.ID) self.logger.warning("I am a block proposer."% self.ID)
else: else:
self.logger.debug("Selected rows: "+str(self.rowIDs), extra=self.format) self.logger.debug("Selected rows: "+str(self.rowIDs), extra=self.format)
@ -55,30 +51,30 @@ class Validator:
def initBlock(self): def initBlock(self):
self.logger.debug("I am a block proposer.", extra=self.format) self.logger.debug("I am a block proposer.", extra=self.format)
self.block = Block(self.blockSize) self.block = Block(self.config.blockSize)
self.block.fill() self.block.fill()
#self.block.print() #self.block.print()
def broadcastBlock(self): def broadcastBlock(self):
if self.proposer == 0: if self.amIproposer == 0:
self.logger.error("I am NOT a block proposer", extra=self.format) self.logger.error("I am NOT a block proposer", extra=self.format)
else: else:
self.logger.debug("Broadcasting my block...", extra=self.format) self.logger.debug("Broadcasting my block...", extra=self.format)
order = [i for i in range(self.blockSize * self.blockSize)] order = [i for i in range(self.config.blockSize * self.config.blockSize)]
random.shuffle(order) random.shuffle(order)
while(order): while(order):
i = order.pop() i = order.pop()
if (random.randint(0,99) >= self.failureRate): if (random.randint(0,99) >= self.config.failureRate):
self.block.data[i] = 1 self.block.data[i] = 1
else: else:
self.block.data[i] = 0 self.block.data[i] = 0
nbFailures = self.block.data.count(0) nbFailures = self.block.data.count(0)
measuredFailureRate = nbFailures * 100 / (self.blockSize * self.blockSize) measuredFailureRate = nbFailures * 100 / (self.config.blockSize * self.config.blockSize)
self.logger.info("Number of failures: %d (%0.02f %%)", nbFailures, measuredFailureRate, extra=self.format) self.logger.info("Number of failures: %d (%0.02f %%)", nbFailures, measuredFailureRate, extra=self.format)
#broadcasted.print() #broadcasted.print()
for id in range(self.blockSize): for id in range(self.config.blockSize):
self.sendColumn(id) self.sendColumn(id)
for id in range(self.blockSize): for id in range(self.config.blockSize):
self.sendRow(id) self.sendRow(id)
def getColumn(self, index): def getColumn(self, index):
@ -101,7 +97,7 @@ class Validator:
def receiveRowsColumns(self): def receiveRowsColumns(self):
if self.proposer == 1: if self.amIproposer == 1:
self.logger.error("I am a block proposer", extra=self.format) self.logger.error("I am a block proposer", extra=self.format)
else: else:
self.logger.debug("Receiving the data...", extra=self.format) self.logger.debug("Receiving the data...", extra=self.format)
@ -124,7 +120,7 @@ class Validator:
n.receiveRow(rowID, line) n.receiveRow(rowID, line)
def sendRows(self): def sendRows(self):
if self.proposer == 1: if self.amIproposer == 1:
self.logger.error("I am a block proposer", extra=self.format) self.logger.error("I am a block proposer", extra=self.format)
else: else:
self.logger.debug("Sending restored rows...", extra=self.format) self.logger.debug("Sending restored rows...", extra=self.format)
@ -132,7 +128,7 @@ class Validator:
self.sendRow(r) self.sendRow(r)
def sendColumns(self): def sendColumns(self):
if self.proposer == 1: if self.amIproposer == 1:
self.logger.error("I am a block proposer", extra=self.format) self.logger.error("I am a block proposer", extra=self.format)
else: else:
self.logger.debug("Sending restored columns...", extra=self.format) self.logger.debug("Sending restored columns...", extra=self.format)

View File

@ -5,27 +5,26 @@ from DAS import *
def study(): def study():
sim = Simulator(0) config = Configuration(64, 20, 10, 256, 8, 0, 0)
sim = Simulator(config)
sim.initLogger() sim.initLogger()
maxTries = 10
step = 20
frRange = [] frRange = []
resultRange = [] resultRange = []
simCnt = 0 simCnt = 0
sim.logger.info("Starting simulations:", extra=sim.format) sim.logger.info("Starting simulations:", extra=sim.format)
start = time.time() start = time.time()
for fr in range(0, 100, step): for fr in range(0, 100, config.failureRateStep):
if fr % 10 == 0: if fr % 10 == 0:
sim.logger.info("Failure rate %d %% ..." % fr, extra=sim.format) sim.logger.info("Failure rate %d %% ..." % fr, extra=sim.format)
sim.resetFailureRate(fr) sim.resetFailureRate(fr)
result = 0 result = 0
for i in range(maxTries): for i in range(config.maxTries):
sim.initValidators() sim.initValidators()
sim.initNetwork() sim.initNetwork()
result += sim.run() result += sim.run()
simCnt += 1 simCnt += 1
frRange.append(fr) frRange.append(fr)
resultRange.append((maxTries-result)*100/maxTries) resultRange.append((config.maxTries-result)*100/config.maxTries)
end = time.time() end = time.time()
sim.logger.info("A total of %d simulations ran in %d seconds" % (simCnt, end-start), extra=sim.format) sim.logger.info("A total of %d simulations ran in %d seconds" % (simCnt, end-start), extra=sim.format)
for i in range(len(frRange)): for i in range(len(frRange)):