From 8ba4831d9c72d632929ecb691ec59416137cafc3 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Thu, 11 Aug 2022 21:44:19 +0100 Subject: [PATCH] Implement best effort allocation --- modules/chankeep.py | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/modules/chankeep.py b/modules/chankeep.py index 7f7ce14..89b8e60 100644 --- a/modules/chankeep.py +++ b/modules/chankeep.py @@ -5,7 +5,7 @@ from twisted.internet.threads import deferToThread import main import modules.provision -from utils.logging.debug import debug +from utils.logging.debug import debug, trace from utils.logging.log import error, log, warn @@ -77,22 +77,31 @@ def emptyChanAllocate(net, flist, relay, new): chans_not_covered = toalloc - sum_free # 2148 - 250 = 1898 relays_needed = chans_not_covered / chanfree[1] # 1898 / 250 = 7.592 correction = ceil(relays_needed) - debug( - ( - f"emptyChanAllocate() secondary allocation sum_free:{sum_free} " - f"chans_not_covered:{chans_not_covered} relays_needed:{relays_needed} " - f"correction:{correction}" + if main.config["ChanKeep"]["Provision"]: + debug( + ( + f"emptyChanAllocate() secondary allocation sum_free:{sum_free} " + f"chans_not_covered:{chans_not_covered} relays_needed:{relays_needed} " + f"correction:{correction}" + ) ) - ) - debug( - ( - f"emptyChanAllocate() not enough free channels: toalloc:{toalloc} " - f"free:{chanfree[0]} chanlimit:{chanfree[1]} correction:{correction}" + debug( + ( + f"emptyChanAllocate() not enough free channels: toalloc:{toalloc} " + f"free:{chanfree[0]} chanlimit:{chanfree[1]} correction:{correction}" + ) ) - ) - warn("Ran out of channel spaces, provisioning additional %i relays for %s" % (correction, net)) - modules.provision.provisionMultipleRelays(net, correction) - return False + warn("Ran out of channel spaces, provisioning additional %i relays for %s" % (correction, net)) + modules.provision.provisionMultipleRelays(net, correction) + return False + else: + # We don't have enough spaces and we can't add any. + # Let's do the best we can in the circumstances. + debug(f"emptyChanAllocate() cannot create additional relays for {net}") + debug(f"emptyChanAllocate() {chans_not_covered} channels cannot be covered") + flist = flist[:sum_free] + debug(f"emptyChanAllocate() flist truncated to {sum_free}, length nis now {len(flist)}") + trace(f"emptyChanAllocate() best effort allocation: {flist}") for i in chanfree[0].keys(): for x in range(chanfree[0][i]): if not len(flist):