Commit 2abf5919 by Karsa Zoltán István

bf balancer

parent c5840072
......@@ -25,7 +25,7 @@ def proxy_datacenters(
balancer_fun=rr_get,
datacenter=None,
):
vm_ram = 4096
vm_ram = 2048
cpu_core = 1
if "ram_size" in body:
vm_ram = body["ram_size"]
......@@ -50,7 +50,7 @@ def proxy_datacenters(
)
if int(t_resp.status_code / 100) != 2:
raise HTTPException(
status_code=t_resp.status_code, detail="Remote server error"
status_code=t_resp.status_code, detail=f"Remote server error ({server})"
)
if not t_resp.content:
return ORJSONResponse({"status": "ok"}, status_code=t_resp.status_code)
......@@ -63,7 +63,7 @@ def proxy_datacenters(
response = ORJSONResponse(obj, status_code=t_resp.status_code)
return response
except Exception:
raise HTTPException(status_code=500, detail="Remote server error")
raise HTTPException(status_code=500, detail=f"Remote server error ({server})")
def create_vm_from_template(
......
......@@ -3,6 +3,7 @@ import os
from typing import List
from balancer.util import delete_rp, get_vm_from_rp, proxy_datacenters, create_rp
from sredis.sredis import (
bf_get,
check_user,
create_puser,
add_datacenter,
......@@ -87,6 +88,12 @@ def proxy_post_rr(
):
return proxy_datacenters(server_path, username, body=body, method="POST")
@app.post("/lb/bf/{server_path:path}")
def proxy_post_rr(
server_path: str = "/", username=Depends(get_current_user), body=Body()
):
return proxy_datacenters(server_path, username, body=body, method="POST", balancer_fun=bf_get)
@app.post("/lb/ff/{server_path:path}")
def proxy_post_ff(
......
......@@ -8,6 +8,7 @@ from core.models import User, DataCenterSchema
from core.auth import hash_pass
from fastapi import HTTPException
import random
import operator
import logging
logger = logging.getLogger(__name__)
......@@ -104,6 +105,12 @@ def rr_get(vm_ram, cpu_core):
return rr
def bf_get(vm_ram, cpu_core):
dc_ram = utilization_ram(vm_ram)
dc = min(dc_ram.items(), key=operator.itemgetter(1))[0]
return dc
def wr_get(centers: DataCenterSchema):
cnt = int(r.get("datacenters_cnt"))
indexis = range(1, cnt + 1)
......@@ -117,6 +124,12 @@ def available_ram():
valid_dcs[dc.datacenter_name] = dc.mem_max - dc.mem_used
return valid_dcs
def utilization_ram(size: int):
dcs = DataCenterResource.find().all()
util_dcs = {}
for dc in dcs:
util_dcs[dc.datacenter_name] = (dc.mem_max - dc.mem_used - size) / dc.mem_max
return util_dcs
def set_token(username: str, datacenter: str, token: str):
r.hset(f"tokens:{username}", datacenter, token)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment