Skip to content

Commit

Permalink
Merge pull request #11345 from stubenhuang/issue-health-11336
Browse files Browse the repository at this point in the history
feat: 健康检查优化 #11336
  • Loading branch information
bkci-bot authored Dec 25, 2024
2 parents 68fbc41 + d6d2bbf commit d8b63da
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import com.tencent.devops.common.db.pojo.TableShardingStrategyEnum
import com.zaxxer.hikari.HikariDataSource
import com.zaxxer.hikari.metrics.micrometer.MicrometerMetricsTrackerFactory
import io.micrometer.core.instrument.MeterRegistry
import java.util.Properties
import javax.sql.DataSource
import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration
Expand All @@ -62,8 +64,6 @@ import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.core.Ordered
import org.springframework.transaction.annotation.EnableTransactionManagement
import java.util.Properties
import javax.sql.DataSource

@Suppress("LongParameterList", "MagicNumber", "ComplexMethod")
@Configuration
Expand Down Expand Up @@ -122,6 +122,9 @@ class BkShardingDataSourceConfiguration {
@Value("\${sharding.tableShardingStrategy.defaultShardingNum:#{5}}")
private val defaultTableShardingNum: Int = 5

@Value("\${spring.datasource.connectionTestQuery:select 1;}")
private lateinit var dataSourceConnectionTestQuery: String

private fun dataSourceMap(
dataSourcePrefixName: String,
dataSourceConfigs: List<DataSourceConfig>,
Expand Down Expand Up @@ -165,6 +168,7 @@ class BkShardingDataSourceConfiguration {
connectionInitSql = datasourceInitSql
leakDetectionThreshold = datasourceLeakDetectionThreshold
metricsTrackerFactory = MicrometerMetricsTrackerFactory(metricsRegistry)
connectionTestQuery = dataSourceConnectionTestQuery
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ package com.tencent.devops.common.db
import com.mysql.cj.jdbc.Driver
import com.tencent.devops.common.db.config.DBBaseConfiguration
import com.zaxxer.hikari.HikariDataSource
import javax.sql.DataSource
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.AutoConfigureBefore
import org.springframework.boot.autoconfigure.AutoConfigureOrder
Expand All @@ -42,7 +43,6 @@ import org.springframework.context.annotation.Primary
import org.springframework.context.annotation.PropertySource
import org.springframework.core.Ordered
import org.springframework.transaction.annotation.EnableTransactionManagement
import javax.sql.DataSource

/**
*
Expand All @@ -58,21 +58,31 @@ class DBAutoConfiguration {

@Value("\${spring.datasource.url:#{null}}")
private val datasourceUrl: String? = null

@Value("\${spring.datasource.username:#{null}}")
private val datasourceUsername: String? = null

@Value("\${spring.datasource.password:#{null}}")
private val datasourcePassword: String? = null

@Value("\${spring.datasource.initSql:#{null}}")
private val datasourceInitSql: String? = null

@Value("\${spring.datasource.leakDetectionThreshold:#{0}}")
private val datasouceLeakDetectionThreshold: Long = 0

@Value("\${spring.datasource.minimumIdle:#{1}}")
private val datasourceMinimumIdle: Int = 1

@Value("\${spring.datasource.maximumPoolSize:#{50}}")
private val datasourceMaximumPoolSize: Int = 50

@Value("\${spring.datasource.idleTimeout:#{60000}}")
private val datasourceIdleTimeout: Long = 60000

@Value("\${spring.datasource.connectionTestQuery:select 1;}")
private lateinit var dataSourceConnectionTestQuery: String

@Bean
@Primary
fun dataSource(): DataSource {
Expand All @@ -90,6 +100,7 @@ class DBAutoConfiguration {
idleTimeout = datasourceIdleTimeout
connectionInitSql = datasourceInitSql
leakDetectionThreshold = datasouceLeakDetectionThreshold
connectionTestQuery = dataSourceConnectionTestQuery
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,5 @@ spring.jmx.enabled=true
management.endpoints.web.exposure.include=*
management.health.elasticsearch.enabled=false
management.endpoint.health.show-details=ALWAYS
management.security.enabled=false
management.endpoints.web.base-path=/management
management.metrics.tags.application=${spring.application.name}
spring.cloud.consul.discovery.health-check-path=/management/health
spring.cloud.consul.discovery.heartbeat.enabled=true
spring.cloud.consul.discovery.query-passing=true
spring.cloud.consul.discovery.preferIpAddress=true
spring.cloud.kubernetes.discovery.all-namespaces=true
management.metrics.tags.application=${spring.application.name}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.tencent.devops.process.actuate

import com.tencent.devops.process.dao.PipelineFavorDao
import org.jooq.DSLContext
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.actuate.health.AbstractHealthIndicator
import org.springframework.boot.actuate.health.Health
import org.springframework.stereotype.Component

@Component
class ProcessHealthIndicator @Autowired constructor(
private val pipelineFavorDao: PipelineFavorDao,
private val dslContext: DSLContext
) : AbstractHealthIndicator() {
@SuppressWarnings("SwallowedException", "TooGenericExceptionCaught")
override fun doHealthCheck(builder: Health.Builder) {
try {
val maxId = pipelineFavorDao.getMaxId(dslContext)
builder.up().withDetail("MaxId", maxId)
} catch (e: Exception) {
logger.error("Get max id failed")
builder.down()
}
}

companion object {
private val logger = LoggerFactory.getLogger(ProcessHealthIndicator::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ package com.tencent.devops.process.dao

import com.tencent.devops.model.process.tables.TPipelineFavor
import com.tencent.devops.model.process.tables.records.TPipelineFavorRecord
import java.time.LocalDateTime
import org.jooq.DSLContext
import org.jooq.Result
import org.jooq.impl.DSL
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
import java.time.LocalDateTime

/**
* 用户收藏流水线
Expand Down Expand Up @@ -138,6 +139,14 @@ class PipelineFavorDao {
}
}

fun getMaxId(
dslContext: DSLContext
): Long {
with(TPipelineFavor.T_PIPELINE_FAVOR) {
return dslContext.select(DSL.max(ID)).from(this).fetchOne(0, Long::class.java)!!
}
}

companion object {
private val logger = LoggerFactory.getLogger(PipelineFavorDao::class.java)
}
Expand Down

0 comments on commit d8b63da

Please sign in to comment.