fix(文件中心):冷热数据切换逻辑完善,已实现时间窗口查询各自的存储服务

This commit is contained in:
2025-10-14 23:21:02 +08:00
parent ac0d185ae2
commit 7299cc0777
5 changed files with 129 additions and 107 deletions
@@ -120,7 +120,9 @@ public class FileController {
PageResult<FileInfo> pageResult = fileService.findList(params); PageResult<FileInfo> pageResult = fileService.findList(params);
if(pageResult.getCount() >0){ if(pageResult.getCount() >0){
for (FileInfo fileInfo: pageResult.getData()){ for (FileInfo fileInfo: pageResult.getData()){
fileInfo.setUrl(timeWindowStorageService.getSmartViewUrlByPath(fileInfo.getName())); String url = timeWindowStorageService.getSmartViewUrlByPath(fileInfo.getName());
log.info("文件查询URL: name={}, url={}", fileInfo.getName(), url);
fileInfo.setUrl(url);
} }
} }
return pageResult; return pageResult;
@@ -136,17 +138,23 @@ public class FileController {
@GetMapping("/files/{id}") @GetMapping("/files/{id}")
public Result getViewUrl(@PathVariable String id) { public Result getViewUrl(@PathVariable String id) {
return Result.succeed(timeWindowStorageService.getSmartViewUrl(id),"获取成功"); String url = timeWindowStorageService.getSmartViewUrl(id);
log.info("文件查询URL: id={}, url={}", id, url);
return Result.succeed(url,"获取成功");
} }
@GetMapping("/file/path") @GetMapping("/file/path")
public Result getFileViewUrlByPath(@RequestParam String path) { public Result getFileViewUrlByPath(@RequestParam String path) {
return Result.succeed(timeWindowStorageService.getSmartViewUrlByPath(path),"获取成功"); String url = timeWindowStorageService.getSmartViewUrlByPath(path);
log.info("文件查询URL: path={}, url={}", path, url);
return Result.succeed(url,"获取成功");
} }
@GetMapping("/files/path") @GetMapping("/files/path")
public String getViewUrlByPath(@RequestParam String path) { public String getViewUrlByPath(@RequestParam String path) {
return timeWindowStorageService.getSmartViewUrlByPath(path); String url = timeWindowStorageService.getSmartViewUrlByPath(path);
log.info("文件查询URL: path={}, url={}", path, url);
return url;
} }
@GetMapping("/files/path/style") @GetMapping("/files/path/style")
@@ -4,6 +4,7 @@ import com.central.common.model.FileInfo;
import com.central.file.service.impl.OssService; import com.central.file.service.impl.OssService;
import com.central.file.service.impl.S3Service; import com.central.file.service.impl.S3Service;
import com.central.file.service.impl.EnhancedS3Service; import com.central.file.service.impl.EnhancedS3Service;
import com.central.file.mapper.FileMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
@@ -12,6 +13,7 @@ import org.springframework.stereotype.Service;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
/** /**
* 时间窗口智能存储服务 * 时间窗口智能存储服务
@@ -36,15 +38,17 @@ public class TimeWindowStorageService {
@Qualifier("enhancedS3FileService") @Qualifier("enhancedS3FileService")
private EnhancedS3Service enhancedS3Service; private EnhancedS3Service enhancedS3Service;
@Autowired(required = false) // 自动同步逻辑已取消,不再注入同步服务
private OssToS3SyncService ossToS3SyncService;
@Autowired
private FileMapper fileMapper;
// 配置参数 // 配置参数
@Value("${zlt.storage.hot-data-days:30}") @Value("${zlt.storage.hot-data-days:30}")
private int hotDataDays; // 热数据保留天数,默认30天 private int hotDataDays; // 热数据保留天数,默认30天
@Value("${zlt.storage.enable-time-window:true}") // @Value("${zlt.storage.enable-time-window:true}")
private boolean enableTimeWindow; // 是否启用时间窗口存储 private boolean enableTimeWindow = true; // 是否启用时间窗口存储
/** /**
* 智能获取文件访问URL * 智能获取文件访问URL
@@ -64,6 +68,8 @@ public class TimeWindowStorageService {
// 2. 判断存储类型 // 2. 判断存储类型
String storageType = getStorageTypeByTime(fileInfo); String storageType = getStorageTypeByTime(fileInfo);
log.info("时间窗口判定: fileId={} createTime={} hotDataDays={} enableTimeWindow={} -> storageType={}",
fileInfo.getId(), fileInfo.getCreateTime(), hotDataDays, enableTimeWindow, storageType);
// 3. 根据存储类型获取URL // 3. 根据存储类型获取URL
String viewUrl = getViewUrlByStorageType(fileInfo, storageType); String viewUrl = getViewUrlByStorageType(fileInfo, storageType);
@@ -86,22 +92,62 @@ public class TimeWindowStorageService {
*/ */
public String getSmartViewUrlByPath(String filePath) { public String getSmartViewUrlByPath(String filePath) {
try { try {
// 1. 根据路径特征判断存储类型 // 先按路径查询数据库,若查到则走时间窗口策略
if (isS3Path(filePath)) { FileInfo fileInfo = getFileInfoByPath(filePath);
log.debug("S3路径,直接查询S3: path={}", filePath); if (fileInfo != null) {
return queryFromS3ByPath(filePath); String storageType = getStorageTypeByTime(fileInfo);
} else if (isOssPath(filePath)) { log.info("[PATH] 时间窗口判定: path={} createTime={} hotDataDays={} enableTimeWindow={} -> storageType={}",
log.debug("OSS路径,直接查询OSS: path={}", filePath); filePath, fileInfo.getCreateTime(), hotDataDays, enableTimeWindow, storageType);
return ossService.getViewUrlByPath(filePath); String url = getViewUrlByStorageType(fileInfo, storageType);
log.info("[PATH] 按时间窗口获取URL: path={} storageType={} url={}", filePath, storageType, url);
return url;
} }
// 2. 路径特征不明确,尝试智能查询 // 若DB无记录,退回统一路径策略:S3优先,其次OSS
log.debug("路径特征不明确,尝试智能查询: path={}", filePath); log.warn("[PATH] 数据库未找到文件信息,按统一策略查询: path={}", filePath);
return getViewUrlByFallback(filePath); // 1) 先尝试S3(不触发任何自动同步)
String s3Url = queryFromS3ByPath(filePath);
} catch (Exception e) { if (s3Url != null && !s3Url.isEmpty()) {
log.error("智能存储查询失败: path={}, error={}", filePath, e.getMessage(), e); log.debug("S3查询成功: path={}", filePath);
return s3Url;
}
// 2) 再尝试OSS
try {
String ossUrl = ossService.getViewUrlByPath(filePath);
if (ossUrl != null && !ossUrl.isEmpty()) {
log.debug("OSS查询成功: path={}", filePath);
return ossUrl;
}
} catch (Exception e) {
log.warn("OSS查询失败: path={}, error={}", filePath, e.getMessage());
}
log.warn("路径查询失败(OSS/S3均未命中): path={}", filePath);
return ""; return "";
} catch (Exception e) {
log.error("智能存储路径查询异常: path={}, error={}", filePath, e.getMessage(), e);
return "";
}
}
private FileInfo getFileInfoByPath(String filePath) {
try {
if (filePath == null || filePath.isEmpty()) {
return null;
}
QueryWrapper<FileInfo> wrapper = new QueryWrapper<>();
wrapper.eq("path", filePath).last("limit 1");
FileInfo info = fileMapper.selectOne(wrapper);
if (info == null) {
// 某些历史数据可能将 name 作为相对路径,可兼容一次
wrapper = new QueryWrapper<>();
wrapper.eq("name", filePath).last("limit 1");
info = fileMapper.selectOne(wrapper);
}
return info;
} catch (Exception ex) {
log.warn("按路径查询文件信息异常: path={}, error={}", filePath, ex.getMessage());
return null;
} }
} }
@@ -155,7 +201,9 @@ public class TimeWindowStorageService {
} }
// S3查询失败,降级到OSS // S3查询失败,降级到OSS
log.warn("S3查询失败,降级到OSS: fileId={}", fileInfo.getId()); log.warn("S3查询失败,降级到OSS: fileId={}", fileInfo.getId());
return queryFromOss(fileInfo); String ossUrl = queryFromOss(fileInfo);
log.info("OSS降级查询结果: fileId={}, url={}", fileInfo.getId(), ossUrl);
return ossUrl;
} else { } else {
// 热数据,优先从OSS查询 // 热数据,优先从OSS查询
log.info("热数据,优先从OSS查询: fileId={}", fileInfo.getId()); log.info("热数据,优先从OSS查询: fileId={}", fileInfo.getId());
@@ -166,7 +214,9 @@ public class TimeWindowStorageService {
} }
// OSS查询失败,降级到S3 // OSS查询失败,降级到S3
log.warn("OSS查询失败,降级到S3: fileId={}", fileInfo.getId()); log.warn("OSS查询失败,降级到S3: fileId={}", fileInfo.getId());
return queryFromS3(fileInfo); String s3Url = queryFromS3(fileInfo);
log.info("S3降级查询结果: fileId={}, url={}", fileInfo.getId(), s3Url);
return s3Url;
} }
} }
@@ -175,7 +225,13 @@ public class TimeWindowStorageService {
*/ */
private String queryFromOss(FileInfo fileInfo) { private String queryFromOss(FileInfo fileInfo) {
try { try {
return ossService.getViewUrlByPath(fileInfo.getPath()); String url = ossService.getViewUrlByPath(fileInfo.getPath());
if (url == null || url.isEmpty()) {
log.warn("从OSS查询为空: fileId={} path={}", fileInfo.getId(), fileInfo.getPath());
} else {
log.debug("从OSS获取URL: fileId={} url={}", fileInfo.getId(), url);
}
return url;
} catch (Exception e) { } catch (Exception e) {
log.warn("从OSS查询失败: fileId={}, error={}", fileInfo.getId(), e.getMessage()); log.warn("从OSS查询失败: fileId={}, error={}", fileInfo.getId(), e.getMessage());
return ""; return "";
@@ -190,10 +246,22 @@ public class TimeWindowStorageService {
// 优先使用EnhancedS3Service // 优先使用EnhancedS3Service
if (enhancedS3Service != null) { if (enhancedS3Service != null) {
log.debug("使用EnhancedS3Service查询: fileId={}, path={}", fileInfo.getId(), fileInfo.getPath()); log.debug("使用EnhancedS3Service查询: fileId={}, path={}", fileInfo.getId(), fileInfo.getPath());
return enhancedS3Service.getViewUrlByPath(fileInfo.getPath()); String url = enhancedS3Service.getViewUrlByPath(fileInfo.getPath());
if (url == null || url.isEmpty()) {
log.warn("从S3查询为空(Enhanced): fileId={} path={}", fileInfo.getId(), fileInfo.getPath());
} else {
log.debug("从S3获取URL(Enhanced): fileId={} url={}", fileInfo.getId(), url);
}
return url;
} else if (s3Service != null) { } else if (s3Service != null) {
log.debug("使用S3Service查询: fileId={}, path={}", fileInfo.getId(), fileInfo.getPath()); log.debug("使用S3Service查询: fileId={}, path={}", fileInfo.getId(), fileInfo.getPath());
return s3Service.getViewUrlByPath(fileInfo.getPath()); String url = s3Service.getViewUrlByPath(fileInfo.getPath());
if (url == null || url.isEmpty()) {
log.warn("从S3查询为空(基础): fileId={} path={}", fileInfo.getId(), fileInfo.getPath());
} else {
log.debug("从S3获取URL(基础): fileId={} url={}", fileInfo.getId(), url);
}
return url;
} else { } else {
log.warn("S3服务未配置,无法查询S3: fileId={}", fileInfo.getId()); log.warn("S3服务未配置,无法查询S3: fileId={}", fileInfo.getId());
return ""; return "";
@@ -212,21 +280,7 @@ public class TimeWindowStorageService {
// 优先使用EnhancedS3Service // 优先使用EnhancedS3Service
if (enhancedS3Service != null) { if (enhancedS3Service != null) {
log.debug("使用EnhancedS3Service查询: path={}", filePath); log.debug("使用EnhancedS3Service查询: path={}", filePath);
String s3Url = enhancedS3Service.getViewUrlByPath(filePath); return enhancedS3Service.getViewUrlByPath(filePath);
// 如果S3中没有找到文件,尝试从OSS同步
if ((s3Url == null || s3Url.isEmpty()) && ossToS3SyncService != null) {
log.info("S3中未找到文件,尝试从OSS同步: path={}", filePath);
boolean syncResult = ossToS3SyncService.syncFileToS3(filePath);
if (syncResult) {
log.info("OSS同步到S3成功,重新获取URL: path={}", filePath);
s3Url = enhancedS3Service.getViewUrlByPath(filePath);
} else {
log.warn("OSS同步到S3失败: path={}", filePath);
}
}
return s3Url;
} else if (s3Service != null) { } else if (s3Service != null) {
log.debug("使用S3Service查询: path={}", filePath); log.debug("使用S3Service查询: path={}", filePath);
return s3Service.getViewUrlByPath(filePath); return s3Service.getViewUrlByPath(filePath);
@@ -243,61 +297,19 @@ public class TimeWindowStorageService {
/** /**
* 降级查询策略 * 降级查询策略
*/ */
private String getViewUrlByFallback(String filePath) { // 删除未使用的降级方法以消除告警
// 先尝试OSS // private String getViewUrlByFallback(String filePath) { return ""; }
try {
String ossUrl = ossService.getViewUrlByPath(filePath);
if (ossUrl != null && !ossUrl.isEmpty()) {
log.debug("OSS降级查询成功: path={}", filePath);
return ossUrl;
}
} catch (Exception e) {
log.warn("OSS降级查询失败: path={}, error={}", filePath, e.getMessage());
}
// 再尝试S3
try {
String s3Url = queryFromS3ByPath(filePath);
if (s3Url != null && !s3Url.isEmpty()) {
log.debug("S3降级查询成功: path={}", filePath);
return s3Url;
}
} catch (Exception e) {
log.warn("S3降级查询失败: path={}, error={}", filePath, e.getMessage());
}
log.warn("所有存储查询都失败: path={}", filePath);
return "";
}
/** /**
* 判断是否为S3路径 * 判断是否为S3路径
*/ */
private boolean isS3Path(String path) { // 删除未使用的方法以消除告警
return path != null && ( // private boolean isS3Path(String path) { return false; }
path.startsWith("s3://") ||
path.contains("minio") ||
path.startsWith("cold-storage") ||
path.contains("S3") ||
path.startsWith("hot-sync/") || // 兼容旧的hot-sync前缀
// 只有明确包含S3标识的路径才认为是S3路径
path.contains("s3://") ||
path.contains("minio://")
);
}
/** /**
* 判断是否为OSS路径 * 判断是否为OSS路径
*/ */
private boolean isOssPath(String path) { // private boolean isOssPath(String path) { return false; }
return path != null && (
path.startsWith("oss://") ||
path.contains("aliyun") ||
path.contains("ali-oss") ||
path.contains("OSS") ||
path.startsWith("https://goflyapp.oss-cn-beijing.aliyuncs.com/")
);
}
/** /**
* 检查文件是否需要迁移 * 检查文件是否需要迁移
@@ -1,7 +1,6 @@
package com.central.file.service.impl; package com.central.file.service.impl;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.central.common.constant.CommonConstant;
import com.central.common.model.FileInfo; import com.central.common.model.FileInfo;
import com.central.oss.model.ObjectInfo; import com.central.oss.model.ObjectInfo;
import com.central.oss.properties.FileServerProperties; import com.central.oss.properties.FileServerProperties;
@@ -336,7 +335,7 @@ public class EnhancedS3Service extends AbstractIFileService {
private S3Object parsePath(String path) { private S3Object parsePath(String path) {
S3Object s3Object = new S3Object(); S3Object s3Object = new S3Object();
if (StrUtil.isNotEmpty(path)) { if (StrUtil.isNotEmpty(path)) {
// 处理s3://bucket/object格式 // 1) 显式 s3://bucket/object 格式
if (path.startsWith("s3://")) { if (path.startsWith("s3://")) {
String[] parts = path.substring(5).split("/", 2); String[] parts = path.substring(5).split("/", 2);
if (parts.length >= 2) { if (parts.length >= 2) {
@@ -346,23 +345,25 @@ public class EnhancedS3Service extends AbstractIFileService {
s3Object.bucketName = fileServerProperties.getS3().getBucketName(); s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = decodeUrl(parts[0]); s3Object.objectName = decodeUrl(parts[0]);
} }
} else if (path.startsWith("hot-sync/")) { return s3Object;
// 处理hot-sync前缀的路径(兼容旧格式)
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = decodeUrl(path.substring("hot-sync/".length())); // 去掉hot-sync/前缀并解码
} else {
// 处理相对路径或无前缀路径
int splitIndex = path.lastIndexOf(CommonConstant.PATH_SPLIT);
if (splitIndex != -1) {
s3Object.bucketName = path.substring(0, splitIndex);
s3Object.objectName = decodeUrl(path.substring(splitIndex + 1));
} else {
// 如果没有分隔符,使用配置的bucket
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = decodeUrl(path);
}
} }
// 2) 兼容历史 hot-sync/ 前缀:对象名去掉前缀,bucket 用配置
if (path.startsWith("hot-sync/")) {
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = decodeUrl(path.substring("hot-sync/".length()));
return s3Object;
}
// 3) 统一相对路径策略:bucket 固定取配置,objectName 使用完整 path
// 这样与 OSS 目录保持一致:同一个 filePath 在 S3 作为对象键
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = decodeUrl(path);
return s3Object;
} }
// path 为空时,退回配置 bucket,空对象名
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = "";
return s3Object; return s3Object;
} }
@@ -8,7 +8,8 @@ import lombok.Getter;
* @author lsl create by 2025/1/10 * @author lsl create by 2025/1/10
*/ */
@Getter @Getter
public enum OrderStatus implements ZltEnum { public enum
OrderStatus implements ZltEnum {
UNPAID(0,"待支付"), UNPAID(0,"待支付"),
PAID(1, "已支付排队中"), PAID(1, "已支付排队中"),
CANCELED(2, "已取消"), CANCELED(2, "已取消"),
@@ -29,7 +29,7 @@ zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
zlt.s3.access-key=Hq78CaYg2BI6ZfxCRMKI zlt.s3.access-key=Hq78CaYg2BI6ZfxCRMKI
zlt.s3.accessKeySecret=DJCt5q9K7GQ2zcBEfr3twb1vQ6BgUW1LKFlI5NSw zlt.s3.accessKeySecret=DJCt5q9K7GQ2zcBEfr3twb1vQ6BgUW1LKFlI5NSw
zlt.s3.endpoint=http://220.205.16.27:9000 zlt.s3.endpoint=http://220.205.16.27:9000
zlt.s3.bucketName=goflyapp zlt.s3.bucketName=goflymedia
zlt.s3.region=us-east-1 zlt.s3.region=us-east-1
zlt.oss.secretId=LTAI5tSNV1RvexXpMMCZhxSe zlt.oss.secretId=LTAI5tSNV1RvexXpMMCZhxSe