Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE3
/
balancer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
2abf5919
authored
Nov 22, 2023
by
Karsa Zoltán István
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bf balancer
parent
c5840072
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
3 deletions
+23
-3
balancer/util.py
+3
-3
main.py
+7
-0
sredis/sredis.py
+13
-0
No files found.
balancer/util.py
View file @
2abf5919
...
@@ -25,7 +25,7 @@ def proxy_datacenters(
...
@@ -25,7 +25,7 @@ def proxy_datacenters(
balancer_fun
=
rr_get
,
balancer_fun
=
rr_get
,
datacenter
=
None
,
datacenter
=
None
,
):
):
vm_ram
=
4096
vm_ram
=
2048
cpu_core
=
1
cpu_core
=
1
if
"ram_size"
in
body
:
if
"ram_size"
in
body
:
vm_ram
=
body
[
"ram_size"
]
vm_ram
=
body
[
"ram_size"
]
...
@@ -50,7 +50,7 @@ def proxy_datacenters(
...
@@ -50,7 +50,7 @@ def proxy_datacenters(
)
)
if
int
(
t_resp
.
status_code
/
100
)
!=
2
:
if
int
(
t_resp
.
status_code
/
100
)
!=
2
:
raise
HTTPException
(
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
:
if
not
t_resp
.
content
:
return
ORJSONResponse
({
"status"
:
"ok"
},
status_code
=
t_resp
.
status_code
)
return
ORJSONResponse
({
"status"
:
"ok"
},
status_code
=
t_resp
.
status_code
)
...
@@ -63,7 +63,7 @@ def proxy_datacenters(
...
@@ -63,7 +63,7 @@ def proxy_datacenters(
response
=
ORJSONResponse
(
obj
,
status_code
=
t_resp
.
status_code
)
response
=
ORJSONResponse
(
obj
,
status_code
=
t_resp
.
status_code
)
return
response
return
response
except
Exception
:
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
(
def
create_vm_from_template
(
...
...
main.py
View file @
2abf5919
...
@@ -3,6 +3,7 @@ import os
...
@@ -3,6 +3,7 @@ import os
from
typing
import
List
from
typing
import
List
from
balancer.util
import
delete_rp
,
get_vm_from_rp
,
proxy_datacenters
,
create_rp
from
balancer.util
import
delete_rp
,
get_vm_from_rp
,
proxy_datacenters
,
create_rp
from
sredis.sredis
import
(
from
sredis.sredis
import
(
bf_get
,
check_user
,
check_user
,
create_puser
,
create_puser
,
add_datacenter
,
add_datacenter
,
...
@@ -87,6 +88,12 @@ def proxy_post_rr(
...
@@ -87,6 +88,12 @@ def proxy_post_rr(
):
):
return
proxy_datacenters
(
server_path
,
username
,
body
=
body
,
method
=
"POST"
)
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}"
)
@app.post
(
"/lb/ff/{server_path:path}"
)
def
proxy_post_ff
(
def
proxy_post_ff
(
...
...
sredis/sredis.py
View file @
2abf5919
...
@@ -8,6 +8,7 @@ from core.models import User, DataCenterSchema
...
@@ -8,6 +8,7 @@ from core.models import User, DataCenterSchema
from
core.auth
import
hash_pass
from
core.auth
import
hash_pass
from
fastapi
import
HTTPException
from
fastapi
import
HTTPException
import
random
import
random
import
operator
import
logging
import
logging
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
...
@@ -104,6 +105,12 @@ def rr_get(vm_ram, cpu_core):
...
@@ -104,6 +105,12 @@ def rr_get(vm_ram, cpu_core):
return
rr
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
):
def
wr_get
(
centers
:
DataCenterSchema
):
cnt
=
int
(
r
.
get
(
"datacenters_cnt"
))
cnt
=
int
(
r
.
get
(
"datacenters_cnt"
))
indexis
=
range
(
1
,
cnt
+
1
)
indexis
=
range
(
1
,
cnt
+
1
)
...
@@ -117,6 +124,12 @@ def available_ram():
...
@@ -117,6 +124,12 @@ def available_ram():
valid_dcs
[
dc
.
datacenter_name
]
=
dc
.
mem_max
-
dc
.
mem_used
valid_dcs
[
dc
.
datacenter_name
]
=
dc
.
mem_max
-
dc
.
mem_used
return
valid_dcs
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
):
def
set_token
(
username
:
str
,
datacenter
:
str
,
token
:
str
):
r
.
hset
(
f
"tokens:{username}"
,
datacenter
,
token
)
r
.
hset
(
f
"tokens:{username}"
,
datacenter
,
token
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment