Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): enhance database health checks #11

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
DedicatedDatabaseState,
} from '../entities/dedicated-database'
import { DedicatedDatabaseService } from './dedicated-database.service'
import { TASK_LOCK_INIT_TIME } from 'src/constants'
import { ServerConfig, TASK_LOCK_INIT_TIME } from 'src/constants'
import { Injectable, Logger } from '@nestjs/common'
import { RegionService } from 'src/region/region.service'
import { ClusterService } from 'src/region/cluster/cluster.service'
Expand All @@ -25,6 +25,10 @@ export class DedicatedDatabaseTaskService {

@Cron(CronExpression.EVERY_SECOND)
async tick() {
if (ServerConfig.DISABLED_INSTANCE_TASK) {
return
}

this.handleDeletingPhase().catch((err) => {
this.logger.error(err)
})
Expand Down Expand Up @@ -95,9 +99,12 @@ export class DedicatedDatabaseTaskService {
return
}

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

manifest = await this.dbService.getDeployManifest(region, user, appid)
const unavailable = manifest?.status?.phase !== 'Running'
if (unavailable) {

if (unavailable && !connectionOk) {
await this.relock(appid, waitingTime)
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common'
import { Injectable, Logger } from '@nestjs/common'
import { Region } from 'src/region/entities/region'
import {
DedicatedDatabase,
Expand Down Expand Up @@ -36,6 +36,8 @@ const p_exec = promisify(exec)

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

constructor(
private readonly cluster: ClusterService,
private readonly mongoService: MongoService,
Expand Down Expand Up @@ -288,7 +290,7 @@ export class DedicatedDatabaseService {
`mongodump --uri='${connectionUri}' --gzip --archive=${filePath}`,
)
} catch (error) {
console.error(`failed to export db ${appid}`, error)
this.logger.error(`failed to export db ${appid}`, error)
throw error
} finally {
await SystemDatabase.db
Expand Down Expand Up @@ -387,6 +389,40 @@ export class DedicatedDatabaseService {
}
}

async databaseConnectionIsOk(appid: string): Promise<boolean> {
try {
const { client } = await this.findAndConnect(appid)
const admin = client.db('admin')
const replSetStatus = await admin.command({ replSetGetStatus: 1 })
const members = replSetStatus.members
const replicaSetOk: boolean = replSetStatus.ok === 1

const healthyMembers = members.filter(
(member: any) => member.health === 1,
)

const primary = healthyMembers.find(
(member: any) => member.stateStr === 'PRIMARY',
)

const majorityCount = Math.ceil(members.length / 2)

const isClusterHealthy =
replicaSetOk && primary && healthyMembers.length >= majorityCount

if (isClusterHealthy) {
return true
}

return false
} catch (error) {
this.logger.verbose(
`dedicatedDatabase health check failed ${appid}\n${error.message}`,
)
return false
}
}

async checkDatabaseSyncLimit(uid: ObjectId) {
const count = await SystemDatabase.db
.collection<DatabaseSyncRecord>('DatabaseSyncRecord')
Expand Down
Loading