Skip to content

Commit

Permalink
feat(server): support request-limit ratio conf of runtime resource in…
Browse files Browse the repository at this point in the history
… region (#1702)
  • Loading branch information
maslow authored Nov 21, 2023
1 parent 6cec750 commit 2046b4b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18-slim
FROM node:20-slim

RUN apt-get update
RUN apt-get install -y openssl wget libkrb5-dev
Expand Down
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"prebuild": "rimraf dist",
"build": "nest build",
"start": "nest start",
"dev": "nest start --watch --debug",
"watch": "nest start --watch",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
Expand Down
4 changes: 2 additions & 2 deletions server/src/application/application.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class ApplicationController {
return ResponseUtil.error(`runtime ${dto.runtimeId} not found`)
}

const regionId = new ObjectId(dto.regionId)
const regionId = region._id

// check if trial tier
const isTrialTier = await this.resource.isTrialBundle(dto)
Expand Down Expand Up @@ -143,7 +143,7 @@ export class ApplicationController {

// create application
const appid = await this.application.tryGenerateUniqueAppid()
await this.application.create(user._id, appid, dto, isTrialTier)
await this.application.create(regionId, user._id, appid, dto, isTrialTier)

const app = await this.application.findOne(appid)
return ResponseUtil.ok(app)
Expand Down
28 changes: 22 additions & 6 deletions server/src/application/application.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ import {
} from './entities/application-bundle'
import { GroupService } from 'src/group/group.service'
import { GroupMember } from 'src/group/entities/group-member'
import { RegionService } from 'src/region/region.service'
import { assert } from 'console'
import { Region } from 'src/region/entities/region'

@Injectable()
export class ApplicationService {
private readonly logger = new Logger(ApplicationService.name)

constructor(private readonly groupService: GroupService) {}
constructor(
private readonly groupService: GroupService,
readonly regionService: RegionService,
) {}

/**
* Create application
Expand All @@ -37,6 +43,7 @@ export class ApplicationService {
* - create application
*/
async create(
regionId: ObjectId,
userid: ObjectId,
appid: string,
dto: CreateApplicationDto,
Expand All @@ -45,6 +52,8 @@ export class ApplicationService {
const client = SystemDatabase.client
const db = client.db()
const session = client.startSession()
const region = await this.regionService.findOne(regionId)
assert(region, 'region cannot be empty')

try {
// start transaction
Expand Down Expand Up @@ -72,7 +81,7 @@ export class ApplicationService {
await db.collection<ApplicationBundle>('ApplicationBundle').insertOne(
{
appid,
resource: this.buildBundleResource(dto),
resource: this.buildBundleResource(region, dto),
autoscaling: this.buildAutoscalingConfig(dto),
isTrialTier: isTrialTier,
createdAt: new Date(),
Expand Down Expand Up @@ -323,7 +332,10 @@ export class ApplicationService {
isTrialTier: boolean,
) {
const db = SystemDatabase.db
const resource = this.buildBundleResource(dto)
const region = await this.regionService.findByAppId(appid)
assert(region, 'region cannot be empty')

const resource = this.buildBundleResource(region, dto)
const autoscaling = this.buildAutoscalingConfig(dto)

const res = await db
Expand Down Expand Up @@ -386,9 +398,13 @@ export class ApplicationService {
return prefix + nano()
}

private buildBundleResource(dto: UpdateApplicationBundleDto) {
const requestCPU = Math.floor(dto.cpu * 0.4)
const requestMemory = Math.floor(dto.memory * 0.4)
private buildBundleResource(region: Region, dto: UpdateApplicationBundleDto) {
const bundleConf = region.bundleConf
const cpuRatio = bundleConf?.cpuRequestLimitRatio || 0.1
const memoryRatio = bundleConf?.memoryRequestLimitRatio || 0.5

const requestCPU = Math.floor(dto.cpu * cpuRatio)
const requestMemory = Math.floor(dto.memory * memoryRatio)
const limitCountOfCloudFunction = Math.floor(dto.cpu * 1)

const magicNumber = Math.floor(dto.cpu * 0.03)
Expand Down
6 changes: 5 additions & 1 deletion server/src/initializer/initializer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export class InitializerService {
npmInstallFlags: '',
runtimeAffinity: {},
},
bundleConf: {
cpuRequestLimitRatio: 0.1,
memoryRequestLimitRatio: 0.5,
},
databaseConf: {
driver: 'mongodb',
connectionUri: ServerConfig.DEFAULT_REGION_DATABASE_URL,
Expand Down Expand Up @@ -284,7 +288,7 @@ export class InitializerService {
[ResourceType.StorageCapacity]: { value: 1024 },
[ResourceType.NetworkTraffic]: { value: 0 },
},
enableFreeTier: true,
enableFreeTier: false,
limitCountOfFreeTierPerUser: 20,
createdAt: new Date(),
updatedAt: new Date(),
Expand Down
6 changes: 6 additions & 0 deletions server/src/region/entities/region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export type RegionClusterConf = {
runtimeAffinity: any
}

export type RegionResourceBundleConf = {
cpuRequestLimitRatio: number
memoryRequestLimitRatio: number
}

export type RegionDatabaseConf = {
driver: string
connectionUri: string
Expand Down Expand Up @@ -74,6 +79,7 @@ export class Region {

namespaceConf: RegionNamespaceConf
clusterConf: RegionClusterConf
bundleConf: RegionResourceBundleConf
databaseConf: RegionDatabaseConf
gatewayConf: RegionGatewayConf
storageConf: RegionStorageConf
Expand Down

0 comments on commit 2046b4b

Please sign in to comment.