Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
HUAHUAI23 committed Jul 19, 2024
1 parent 6870fb8 commit 8cd2150
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 172 deletions.
13 changes: 10 additions & 3 deletions server/src/application/application.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,16 @@ export class ApplicationController {
@Body() dto: UpdateApplicationBundleDto,
@InjectApplication() app: ApplicationWithRelations,
) {
// todo
// 如果 restarting 不允许变更配置
// 仅允许 started 和 stopped 变更配置
// only running and stopped application can update bundle
if (
app.phase !== ApplicationPhase.Started &&
app.phase !== ApplicationPhase.Stopped
) {
return ResponseUtil.error(
'The application is not running or stopped, can not update bundle',
)
}

const error = dto.autoscaling.validate()
if (error) {
return ResponseUtil.error(error)
Expand Down
2 changes: 2 additions & 0 deletions server/src/application/dto/update-application.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
IsIn,
IsInt,
IsNotEmpty,
IsOptional,
IsString,
Length,
ValidateNested,
Expand Down Expand Up @@ -58,6 +59,7 @@ export class UpdateApplicationStateDto {
description: 'Flag for runtime only operations',
type: Boolean,
})
@IsOptional()
@IsBoolean()
onlyRuntimeFlag?: boolean
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ export class DedicatedDatabaseTaskService {
const user = await this.clusterService.getUserByAppid(appid)
let manifest = await this.dbService.getDeployManifest(region, user, appid)

const waitingTime = Date.now() - data.updatedAt.getTime()

if (!manifest || manifest.spec.componentSpecs[0].replicas === 0) {
await this.dbService.applyDeployManifest(region, user, appid)
await this.relock(appid, waitingTime)
return
}

const waitingTime = Date.now() - data.updatedAt.getTime()

const connectionOk = await this.dbService.databaseConnectionIsOk(appid)

manifest = await this.dbService.getDeployManifest(region, user, appid)
Expand Down Expand Up @@ -302,11 +304,31 @@ export class DedicatedDatabaseTaskService {
const region = await this.regionService.findByAppId(appid)
const user = await this.clusterService.getUserByAppid(appid)

const isDeployManifestChanged =
await this.dbService.isDeployManifestChanged(region, user, appid)

if (isDeployManifestChanged) {
await this.dbService.applyDeployManifest(region, user, appid)
await db.collection<DedicatedDatabase>('DedicatedDatabase').updateOne(
{
appid: appid,
},
{
$set: {
state: DedicatedDatabaseState.Running,
phase: DedicatedDatabasePhase.Starting,
lockedAt: TASK_LOCK_INIT_TIME,
updatedAt: new Date(),
},
},
)
}

const OpsRequestManifest =
await this.dbService.getKubeBlockOpsRequestManifest(user, appid)
await this.dbService.getKubeBlockOpsRequestManifest(region, user, appid)

if (!OpsRequestManifest) {
await this.dbService.applyKubeBlockOpsRequestManifest(user, appid)
await this.dbService.applyKubeBlockOpsRequestManifest(region, user, appid)
await this.relock(appid, waitingTime)
return
}
Expand All @@ -317,9 +339,7 @@ export class DedicatedDatabaseTaskService {
appid,
)

const manifestExists = ddbDeployManifest && OpsRequestManifest

if (!manifestExists) {
if (!ddbDeployManifest) {
await this.relock(appid, waitingTime)
return
}
Expand All @@ -329,7 +349,7 @@ export class DedicatedDatabaseTaskService {
OpsRequestManifest?.status?.phase === 'Succeed'

if (isRestartSuccessful) {
await this.dbService.deleteKubeBlockOpsManifest(user, appid)
await this.dbService.deleteKubeBlockOpsManifest(region, user, appid)
await db.collection<DedicatedDatabase>('DedicatedDatabase').updateOne(
{
appid: appid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
DatabaseSyncRecord,
DatabaseSyncState,
} from '../entities/database-sync-record'
import { extractNumber } from 'src/utils/number'

const getDedicatedDatabaseName = (appid: string) => `sealaf-${appid}`
const p_exec = promisify(exec)
Expand Down Expand Up @@ -160,21 +161,75 @@ export class DedicatedDatabaseService {
return manifest
}

async applyKubeBlockOpsRequestManifest(user: User, appid: string) {
const manifest = this.makeKubeBlockOpsRequestManifest(appid, user)
async isDeployManifestChanged(
region: Region,
user: User,
appid: string,
): Promise<boolean> {
const ddbDeployManifest = await this.getDeployManifest(region, user, appid)
const replicas = Number(ddbDeployManifest.spec.componentSpecs[0].replicas)

const limitCPU = extractNumber(
ddbDeployManifest.spec.componentSpecs[0].resources?.limits?.cpu,
)
const limitMemory = extractNumber(
ddbDeployManifest.spec.componentSpecs[0].resources?.limits?.memory,
)
const capacity = extractNumber(
ddbDeployManifest.spec.componentSpecs[0]?.volumeClaimTemplates[0]?.spec
?.resources?.requests?.storage,
)

const spec = await this.getDedicatedDatabaseSpec(appid)

const isLimitCpuMatch =
spec.limitCPU === limitCPU || spec.limitCPU / 1000 === limitCPU
const isLimitMemoryMatch =
spec.limitMemory === limitMemory ||
spec.limitMemory / 1024 === limitMemory
const isCapacityMatch =
spec.capacity === capacity || spec.capacity / 1024 === capacity
const isReplicasMatch = spec.replicas === replicas

return !(
isLimitCpuMatch &&
isLimitMemoryMatch &&
isReplicasMatch &&
isCapacityMatch
)
}

async applyKubeBlockOpsRequestManifest(
region: Region,
user: User,
appid: string,
) {
const manifest = this.makeKubeBlockOpsRequestManifest(region, user, appid)
const res = await this.cluster.applyYamlString(manifest, user.namespace)
return res
}

async deleteKubeBlockOpsManifest(user: User, appid: string) {
const manifest = await this.getKubeBlockOpsRequestManifest(user, appid)
async deleteKubeBlockOpsManifest(region: Region, user: User, appid: string) {
const manifest = await this.getKubeBlockOpsRequestManifest(
region,
user,
appid,
)
const res = await this.cluster.deleteCustomObject(manifest)
return res
}

async getKubeBlockOpsRequestManifest(user: User, appid: string) {
async getKubeBlockOpsRequestManifest(
region: Region,
user: User,
appid: string,
) {
const api = this.cluster.makeObjectApi()
const emptyManifest = this.makeKubeBlockOpsRequestManifest(appid, user)
const emptyManifest = this.makeKubeBlockOpsRequestManifest(
region,
user,
appid,
)
const specs = loadAllYaml(emptyManifest)
assert(
specs && specs.length > 0,
Expand All @@ -189,21 +244,11 @@ export class DedicatedDatabaseService {
}
}

makeKubeBlockOpsRequestManifest(appid: string, user: User) {
const template = `
apiVersion: apps.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: <%- name %>
namespace: <%- namespace %>
spec:
clusterRef: <%- clusterName %>
type: Restart
restart:
- componentName: mongodb
`
makeKubeBlockOpsRequestManifest(region: Region, user: User, appid: string) {
const clusterName = getDedicatedDatabaseName(appid)
const namespace = user.namespace

const template = region.deployManifest.databaseOpsRequest
const tmpl = _.template(template)

const manifest = tmpl({
Expand Down
10 changes: 10 additions & 0 deletions server/src/initializer/deploy-manifest/databaseOpsRequest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: apps.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: <%- name %>
namespace: <%- namespace %>
spec:
clusterRef: <%- clusterName %>
type: Restart
restart:
- componentName: mongodb
22 changes: 19 additions & 3 deletions server/src/instance/instance-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export class InstanceTaskService {
},
)

// if databse operation success but runtime failed
await db
.collection<DedicatedDatabase>('DedicatedDatabase')
.findOneAndUpdate(
Expand All @@ -143,10 +144,16 @@ export class InstanceTaskService {
const appid = app.appid

const ddb = await this.dedicatedDatabaseService.findOne(appid)

if (!ddb) {
await this.relock(appid, waitingTime)
return
}

if (ddb) {
if (
ddb.state !== DedicatedDatabaseState.Running &&
ddb.phase !== DedicatedDatabasePhase.Started
ddb.phase !== DedicatedDatabasePhase.Started ||
ddb.state !== DedicatedDatabaseState.Running
) {
await this.relock(appid, waitingTime)
return
Expand Down Expand Up @@ -296,7 +303,16 @@ export class InstanceTaskService {
}

const ddb = await this.dedicatedDatabaseService.findOne(appid)
if (ddb && ddb.phase !== DedicatedDatabasePhase.Stopped) {

if (!ddb) {
await this.relock(appid, waitingTime)
return
}

if (
ddb.phase !== DedicatedDatabasePhase.Stopped ||
ddb.state !== DedicatedDatabaseState.Stopped
) {
await this.relock(appid, waitingTime)
return
}
Expand Down
5 changes: 5 additions & 0 deletions server/src/utils/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ export function PriceDiv(price1: number | string, price2: number | string) {
const price2Num = Number(price2)
return PriceRound(price1Num / price2Num)
}

export function extractNumber(value: string): number {
const match = value.match(/\d+/)
return match ? parseInt(match[0], 10) : null
}
2 changes: 1 addition & 1 deletion web/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
"Balance": "Balance",
"Pause": "Pause",
"PauseTips": "Are you sure you want to pause this application?",
"RestartTips": "Are you sure you want to restart this application?",
"RestartTips": "Restart the database together",
"CommonSetting": "Common Settings",
"EditorFont": "Editor Font",
"FontSize": "Font Size",
Expand Down
2 changes: 1 addition & 1 deletion web/public/locales/zh-CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
"Balance": "余额",
"Pause": "暂停应用",
"PauseTips": "确定要暂停此应用吗?",
"RestartTips": "确定要重启此应用吗?",
"RestartTips": "同时重启数据库",
"CommonSetting": "常用设置",
"EditorFont": "编辑器字体",
"FontSize": "字体大小",
Expand Down
2 changes: 1 addition & 1 deletion web/public/locales/zh/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
"Balance": "余额",
"Pause": "暂停应用",
"PauseTips": "确定要暂停此应用吗?",
"RestartTips": "确定要重启此应用吗?",
"RestartTips": "同时重启数据库",
"CommonSetting": "常用设置",
"EditorFont": "编辑器字体",
"FontSize": "字体大小",
Expand Down
Loading

0 comments on commit 8cd2150

Please sign in to comment.