|
@@ -0,0 +1,79 @@
|
|
|
|
|
+package com.tzld.videoVector.listener;
|
|
|
|
|
+
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
|
+import org.springframework.boot.context.event.ApplicationReadyEvent;
|
|
|
|
|
+import org.springframework.context.event.EventListener;
|
|
|
|
|
+import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import javax.sql.DataSource;
|
|
|
|
|
+import java.sql.Connection;
|
|
|
|
|
+import java.sql.ResultSet;
|
|
|
|
|
+import java.sql.Statement;
|
|
|
|
|
+
|
|
|
|
|
+@Component
|
|
|
|
|
+public class StartupWarmupListener {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(StartupWarmupListener.class);
|
|
|
|
|
+
|
|
|
|
|
+ private final DataSource videoVectorDataSource;
|
|
|
|
|
+ private final DataSource pgVectorDataSource;
|
|
|
|
|
+ private final RedisConnectionFactory redisConnectionFactory;
|
|
|
|
|
+
|
|
|
|
|
+ public StartupWarmupListener(
|
|
|
|
|
+ @Qualifier("videoVectorDataSource") DataSource videoVectorDataSource,
|
|
|
|
|
+ @Qualifier("pgVectorDataSource") DataSource pgVectorDataSource,
|
|
|
|
|
+ RedisConnectionFactory redisConnectionFactory) {
|
|
|
|
|
+ this.videoVectorDataSource = videoVectorDataSource;
|
|
|
|
|
+ this.pgVectorDataSource = pgVectorDataSource;
|
|
|
|
|
+ this.redisConnectionFactory = redisConnectionFactory;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @EventListener(ApplicationReadyEvent.class)
|
|
|
|
|
+ public void onReady() {
|
|
|
|
|
+ log.info("warming up connection pools...");
|
|
|
|
|
+ warmUpDB(videoVectorDataSource, "mysql");
|
|
|
|
|
+ warmUpPGVector(pgVectorDataSource);
|
|
|
|
|
+ warmUpRedis();
|
|
|
|
|
+ log.info("connection pools warmed up");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void warmUpDB(DataSource ds, String name) {
|
|
|
|
|
+ try (Connection conn = ds.getConnection();
|
|
|
|
|
+ Statement stmt = conn.createStatement()) {
|
|
|
|
|
+ stmt.execute("SELECT 1");
|
|
|
|
|
+ log.info("{} connection pool ready", name);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("{} warmup failed: {}", name, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void warmUpPGVector(DataSource ds) {
|
|
|
|
|
+ try (Connection conn = ds.getConnection();
|
|
|
|
|
+ Statement stmt = conn.createStatement()) {
|
|
|
|
|
+ stmt.execute("SELECT 1");
|
|
|
|
|
+ // 验证 connection-init-sql 是否生效
|
|
|
|
|
+ ResultSet rs = stmt.executeQuery("SHOW hnsw.ef_search");
|
|
|
|
|
+ if (rs.next()) {
|
|
|
|
|
+ log.info("pgvector connection pool ready, hnsw.ef_search = {}", rs.getString(1));
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("pgvector warmup failed: {}", e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void warmUpRedis() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Object result = redisConnectionFactory.getConnection().execute("PING");
|
|
|
|
|
+ if ("PONG".equals(result)) {
|
|
|
|
|
+ log.info("redis connection pool ready");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.warn("redis PING unexpected response: {}", result);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("redis warmup failed: {}", e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|