schedule same nodes with different weight

This commit is contained in:
streetycat
2023-09-18 07:58:08 +00:00
parent b38aae81e2
commit f2a25b1b99
2 changed files with 18 additions and 0 deletions
+15
View File
@@ -1,4 +1,5 @@
from abc import ABC, abstractmethod
import random
from typing import Optional
import logging
import asyncio
@@ -59,9 +60,23 @@ class ComputeKernel:
asyncio.create_task(_run_task_loop())
def _schedule(self, task) -> ComputeNode:
# find all the node which supports this task
support_nodes = []
total_weights = 0
for node in self.compute_nodes.values():
if node.is_support(task) is True:
support_nodes.append({
"pos": total_weights,
"node": node
})
total_weights += node.weight()
# hit a random node with weight
hit_pos = random.randint(0, total_weights - 1)
for i in range(min(len(support_nodes) - 1, hit_pos), -1, -1):
if support_nodes[i]["pos"] <= hit_pos:
return node
logger.warning(
f"task {task.display()} is not support by any compute node")
return None
+3
View File
@@ -35,6 +35,9 @@ class ComputeNode(ABC):
def is_local(self) -> bool:
pass
def weight(self) -> int:
return 1
def is_trusted(self) -> bool:
return True