fix(文件中心):修复冷数据访问问题彻底修复,设备中心查询航线文件空指针异常问题修复
This commit is contained in:
+73
-5
@@ -13,7 +13,10 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wayline file information
|
* Wayline file information
|
||||||
@@ -48,8 +51,16 @@ public class LineFileController {
|
|||||||
@Operation(summary = "查询")
|
@Operation(summary = "查询")
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public Result findById(@PathVariable String id) {
|
public Result findById(@PathVariable String id) {
|
||||||
LineFile model = lineFileService.findLineFileByWaylineId(id);
|
try {
|
||||||
return Result.succeed(model, "查询成功");
|
LineFile model = lineFileService.findLineFileByWaylineId(id);
|
||||||
|
if (model == null) {
|
||||||
|
return Result.failed("未找到对应的航线文件信息");
|
||||||
|
}
|
||||||
|
return Result.succeed(model, "查询成功");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("查询航线文件失败,id: {}", id, e);
|
||||||
|
return Result.failed("查询航线文件失败:" + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,9 +68,17 @@ public class LineFileController {
|
|||||||
*/
|
*/
|
||||||
@Operation(summary = "查询")
|
@Operation(summary = "查询")
|
||||||
@GetMapping("/sign/{id}")
|
@GetMapping("/sign/{id}")
|
||||||
public String findLineSignById(@PathVariable String id) {
|
public Result findLineSignById(@PathVariable String id) {
|
||||||
LineFile model = lineFileService.findLineFileByWaylineId(id);
|
try {
|
||||||
return model.getSign();
|
LineFile model = lineFileService.findLineFileByWaylineId(id);
|
||||||
|
if (model == null) {
|
||||||
|
return Result.failed("未找到对应的航线文件信息");
|
||||||
|
}
|
||||||
|
return Result.succeed(model.getSign(), "查询成功");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("查询航线文件签名失败,id: {}", id, e);
|
||||||
|
return Result.failed("查询航线文件签名失败:" + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -82,4 +101,53 @@ public class LineFileController {
|
|||||||
lineFileService.removeById(id);
|
lineFileService.removeById(id);
|
||||||
return Result.succeed("删除成功");
|
return Result.succeed("删除成功");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 诊断航线文件数据
|
||||||
|
*/
|
||||||
|
@Operation(summary = "诊断航线文件数据")
|
||||||
|
@GetMapping("/diagnose")
|
||||||
|
public Result diagnoseLineFileData() {
|
||||||
|
try {
|
||||||
|
// 查询所有航线文件
|
||||||
|
List<LineFile> allLineFiles = lineFileService.list();
|
||||||
|
|
||||||
|
Map<String, Object> diagnosis = new HashMap<>();
|
||||||
|
diagnosis.put("totalCount", allLineFiles.size());
|
||||||
|
|
||||||
|
// 按waylineId分组统计
|
||||||
|
Map<String, Long> waylineIdStats = allLineFiles.stream()
|
||||||
|
.collect(Collectors.groupingBy(
|
||||||
|
f -> f.getWaylineId() == null ? "NULL_WAYLINE_ID" : f.getWaylineId(),
|
||||||
|
Collectors.counting()
|
||||||
|
));
|
||||||
|
diagnosis.put("waylineIdStats", waylineIdStats);
|
||||||
|
|
||||||
|
// 检查有sign字段的文件
|
||||||
|
List<LineFile> filesWithSign = allLineFiles.stream()
|
||||||
|
.filter(f -> f.getSign() != null && !f.getSign().isEmpty())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
diagnosis.put("filesWithSign", filesWithSign.size());
|
||||||
|
|
||||||
|
// 最近的文件示例
|
||||||
|
List<Map<String, Object>> recentFiles = allLineFiles.stream()
|
||||||
|
.limit(10)
|
||||||
|
.map(f -> {
|
||||||
|
Map<String, Object> fileInfo = new HashMap<>();
|
||||||
|
fileInfo.put("id", f.getId());
|
||||||
|
fileInfo.put("name", f.getName());
|
||||||
|
fileInfo.put("waylineId", f.getWaylineId());
|
||||||
|
fileInfo.put("sign", f.getSign());
|
||||||
|
fileInfo.put("createTime", f.getCreateTime());
|
||||||
|
return fileInfo;
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
diagnosis.put("recentFiles", recentFiles);
|
||||||
|
|
||||||
|
return Result.succeed(diagnosis, "诊断完成");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("诊断航线文件数据失败", e);
|
||||||
|
return Result.failed("诊断失败:" + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+73
-5
@@ -13,7 +13,10 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wayline file information
|
* Wayline file information
|
||||||
@@ -48,8 +51,16 @@ public class LineFileController {
|
|||||||
@Operation(summary = "查询")
|
@Operation(summary = "查询")
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public Result findById(@PathVariable String id) {
|
public Result findById(@PathVariable String id) {
|
||||||
LineFile model = lineFileService.findLineFileByWaylineId(id);
|
try {
|
||||||
return Result.succeed(model, "查询成功");
|
LineFile model = lineFileService.findLineFileByWaylineId(id);
|
||||||
|
if (model == null) {
|
||||||
|
return Result.failed("未找到对应的航线文件信息");
|
||||||
|
}
|
||||||
|
return Result.succeed(model, "查询成功");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("查询航线文件失败,id: {}", id, e);
|
||||||
|
return Result.failed("查询航线文件失败:" + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,9 +68,17 @@ public class LineFileController {
|
|||||||
*/
|
*/
|
||||||
@Operation(summary = "查询")
|
@Operation(summary = "查询")
|
||||||
@GetMapping("/sign/{id}")
|
@GetMapping("/sign/{id}")
|
||||||
public String findLineSignById(@PathVariable String id) {
|
public Result findLineSignById(@PathVariable String id) {
|
||||||
LineFile model = lineFileService.findLineFileByWaylineId(id);
|
try {
|
||||||
return model.getSign();
|
LineFile model = lineFileService.findLineFileByWaylineId(id);
|
||||||
|
if (model == null) {
|
||||||
|
return Result.failed("未找到对应的航线文件信息");
|
||||||
|
}
|
||||||
|
return Result.succeed(model.getSign(), "查询成功");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("查询航线文件签名失败,id: {}", id, e);
|
||||||
|
return Result.failed("查询航线文件签名失败:" + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -82,4 +101,53 @@ public class LineFileController {
|
|||||||
lineFileService.removeById(id);
|
lineFileService.removeById(id);
|
||||||
return Result.succeed("删除成功");
|
return Result.succeed("删除成功");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 诊断航线文件数据
|
||||||
|
*/
|
||||||
|
@Operation(summary = "诊断航线文件数据")
|
||||||
|
@GetMapping("/diagnose")
|
||||||
|
public Result diagnoseLineFileData() {
|
||||||
|
try {
|
||||||
|
// 查询所有航线文件
|
||||||
|
List<LineFile> allLineFiles = lineFileService.list();
|
||||||
|
|
||||||
|
Map<String, Object> diagnosis = new HashMap<>();
|
||||||
|
diagnosis.put("totalCount", allLineFiles.size());
|
||||||
|
|
||||||
|
// 按waylineId分组统计
|
||||||
|
Map<String, Long> waylineIdStats = allLineFiles.stream()
|
||||||
|
.collect(Collectors.groupingBy(
|
||||||
|
f -> f.getWaylineId() == null ? "NULL_WAYLINE_ID" : f.getWaylineId(),
|
||||||
|
Collectors.counting()
|
||||||
|
));
|
||||||
|
diagnosis.put("waylineIdStats", waylineIdStats);
|
||||||
|
|
||||||
|
// 检查有sign字段的文件
|
||||||
|
List<LineFile> filesWithSign = allLineFiles.stream()
|
||||||
|
.filter(f -> f.getSign() != null && !f.getSign().isEmpty())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
diagnosis.put("filesWithSign", filesWithSign.size());
|
||||||
|
|
||||||
|
// 最近的文件示例
|
||||||
|
List<Map<String, Object>> recentFiles = allLineFiles.stream()
|
||||||
|
.limit(10)
|
||||||
|
.map(f -> {
|
||||||
|
Map<String, Object> fileInfo = new HashMap<>();
|
||||||
|
fileInfo.put("id", f.getId());
|
||||||
|
fileInfo.put("name", f.getName());
|
||||||
|
fileInfo.put("waylineId", f.getWaylineId());
|
||||||
|
fileInfo.put("sign", f.getSign());
|
||||||
|
fileInfo.put("createTime", f.getCreateTime());
|
||||||
|
return fileInfo;
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
diagnosis.put("recentFiles", recentFiles);
|
||||||
|
|
||||||
|
return Result.succeed(diagnosis, "诊断完成");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("诊断航线文件数据失败", e);
|
||||||
|
return Result.failed("诊断失败:" + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+134
-3
@@ -1,6 +1,8 @@
|
|||||||
package com.central.file.controller;
|
package com.central.file.controller;
|
||||||
|
|
||||||
|
import com.central.common.model.FileInfo;
|
||||||
import com.central.common.model.Result;
|
import com.central.common.model.Result;
|
||||||
|
import com.central.file.mapper.FileMapper;
|
||||||
import com.central.file.service.ICdnService;
|
import com.central.file.service.ICdnService;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
@@ -11,8 +13,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CDN管理控制器
|
* CDN管理控制器
|
||||||
@@ -30,6 +34,9 @@ public class CdnController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ICdnService cdnService;
|
private ICdnService cdnService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FileMapper fileMapper;
|
||||||
|
|
||||||
@Value("${file.oss-url:goflyapp.oss-cn-beijing.aliyuncs.com}")
|
@Value("${file.oss-url:goflyapp.oss-cn-beijing.aliyuncs.com}")
|
||||||
private String cdnOssUrl;
|
private String cdnOssUrl;
|
||||||
|
|
||||||
@@ -160,9 +167,133 @@ public class CdnController {
|
|||||||
* 这里需要根据实际业务逻辑实现
|
* 这里需要根据实际业务逻辑实现
|
||||||
*/
|
*/
|
||||||
private List<String> getOrderImageUrls(String orderNo) {
|
private List<String> getOrderImageUrls(String orderNo) {
|
||||||
// TODO: 实现获取订单图片URL的逻辑
|
try {
|
||||||
// 可以通过调用订单服务或文件服务来获取
|
log.info("获取订单图片URL,订单号: {}", orderNo);
|
||||||
return List.of();
|
|
||||||
|
// 方案1: 通过文件服务查询订单相关的图片文件
|
||||||
|
List<String> imageUrls = new ArrayList<>();
|
||||||
|
|
||||||
|
// 查询订单相关的图片文件(根据路径模式匹配)
|
||||||
|
// 假设订单图片的路径包含订单号
|
||||||
|
List<FileInfo> orderFiles = fileMapper.selectList(null).stream()
|
||||||
|
.filter(file -> {
|
||||||
|
if (file.getPath() == null || orderNo == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查路径是否包含订单号
|
||||||
|
return file.getPath().contains(orderNo);
|
||||||
|
})
|
||||||
|
.filter(file -> {
|
||||||
|
// 只处理图片文件
|
||||||
|
if (file.getContentType() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return file.getContentType().startsWith("image/");
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
log.info("找到订单相关图片文件: {} 个", orderFiles.size());
|
||||||
|
|
||||||
|
// 转换为URL列表
|
||||||
|
for (FileInfo fileInfo : orderFiles) {
|
||||||
|
try {
|
||||||
|
String imageUrl = getImageUrl(fileInfo);
|
||||||
|
if (imageUrl != null && !imageUrl.isEmpty()) {
|
||||||
|
imageUrls.add(imageUrl);
|
||||||
|
log.debug("订单图片URL: {}", imageUrl);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("获取订单图片URL失败,文件ID: {}", fileInfo.getId(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 方案2: 如果文件服务中没有找到,可以尝试调用订单服务
|
||||||
|
if (imageUrls.isEmpty()) {
|
||||||
|
log.info("文件服务中未找到订单图片,尝试其他方式获取");
|
||||||
|
// 这里可以添加调用订单服务的逻辑
|
||||||
|
// 例如:通过RestTemplate调用订单服务获取图片URL
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("订单图片URL获取完成,订单号: {}, 图片数量: {}", orderNo, imageUrls.size());
|
||||||
|
return imageUrls;
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("获取订单图片URL失败,订单号: {}", orderNo, e);
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取图片文件的访问URL
|
||||||
|
*/
|
||||||
|
private String getImageUrl(FileInfo fileInfo) {
|
||||||
|
try {
|
||||||
|
if (fileInfo.getPath() == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据存储类型获取URL
|
||||||
|
if (fileInfo.getStorageType() != null) {
|
||||||
|
switch (fileInfo.getStorageType().toUpperCase()) {
|
||||||
|
case "OSS":
|
||||||
|
case "ALIYUN":
|
||||||
|
// 使用OSS服务获取URL
|
||||||
|
return getOssImageUrl(fileInfo.getPath());
|
||||||
|
case "S3":
|
||||||
|
case "MINIO":
|
||||||
|
// 使用S3服务获取URL
|
||||||
|
return getS3ImageUrl(fileInfo.getPath());
|
||||||
|
default:
|
||||||
|
// 默认使用OSS
|
||||||
|
return getOssImageUrl(fileInfo.getPath());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 根据路径特征判断存储类型
|
||||||
|
if (fileInfo.getPath().startsWith("picture/") ||
|
||||||
|
fileInfo.getPath().startsWith("video/") ||
|
||||||
|
fileInfo.getPath().startsWith("file/")) {
|
||||||
|
return getOssImageUrl(fileInfo.getPath());
|
||||||
|
} else if (fileInfo.getPath().contains("s3://") ||
|
||||||
|
fileInfo.getPath().contains("minio")) {
|
||||||
|
return getS3ImageUrl(fileInfo.getPath());
|
||||||
|
} else {
|
||||||
|
return getOssImageUrl(fileInfo.getPath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("获取图片URL失败,文件ID: {}", fileInfo.getId(), e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取OSS图片URL
|
||||||
|
*/
|
||||||
|
private String getOssImageUrl(String path) {
|
||||||
|
try {
|
||||||
|
// 这里需要注入OssService,暂时返回模拟URL
|
||||||
|
// 实际实现中应该调用ossService.getViewUrlByPath(path)
|
||||||
|
return "https://" + cdnOssUrl + "/" + path;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("获取OSS图片URL失败: {}", path, e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取S3图片URL
|
||||||
|
*/
|
||||||
|
private String getS3ImageUrl(String path) {
|
||||||
|
try {
|
||||||
|
// 这里需要注入S3Service,暂时返回模拟URL
|
||||||
|
// 实际实现中应该调用s3Service.getViewUrlByPath(path)
|
||||||
|
return "https://s3.amazonaws.com/" + path;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("获取S3图片URL失败: {}", path, e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+596
@@ -7,6 +7,8 @@ import com.central.file.service.IFileService;
|
|||||||
import com.central.file.service.StorageHealthCheckService;
|
import com.central.file.service.StorageHealthCheckService;
|
||||||
import com.central.file.service.TimeWindowStorageService;
|
import com.central.file.service.TimeWindowStorageService;
|
||||||
import com.central.file.service.OssToS3SyncService;
|
import com.central.file.service.OssToS3SyncService;
|
||||||
|
import com.central.file.service.OssToMinioSyncScheduler;
|
||||||
|
import com.central.file.mapper.FileMapper;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.collections.MapUtils;
|
import org.apache.commons.collections.MapUtils;
|
||||||
@@ -21,10 +23,12 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传
|
* 文件上传
|
||||||
@@ -46,6 +50,12 @@ public class FileController {
|
|||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
private OssToS3SyncService ossToS3SyncService;
|
private OssToS3SyncService ossToS3SyncService;
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private OssToMinioSyncScheduler ossToMinioSyncScheduler;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FileMapper fileMapper;
|
||||||
|
|
||||||
@Value("${zlt.security.tencent.accessTokenUrl}")
|
@Value("${zlt.security.tencent.accessTokenUrl}")
|
||||||
private String accessTokenUrl;
|
private String accessTokenUrl;
|
||||||
|
|
||||||
@@ -376,4 +386,590 @@ public class FileController {
|
|||||||
return Result.failed("测试OSS访问失败");
|
return Result.failed("测试OSS访问失败");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量同步OSS文件到MinIO
|
||||||
|
*/
|
||||||
|
@PostMapping("/storage/batch-sync-oss-to-minio")
|
||||||
|
public Result<Map<String, Object>> batchSyncOssToMinio(@RequestParam(required = false) Integer days) {
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
try {
|
||||||
|
if (ossToS3SyncService == null) {
|
||||||
|
result.put("error", "OSS到MinIO同步服务未配置");
|
||||||
|
result.put("success", false);
|
||||||
|
return Result.failed("OSS到MinIO同步服务未配置");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有指定天数,默认同步30天前的文件
|
||||||
|
int syncDays = days != null ? days : 30;
|
||||||
|
|
||||||
|
log.info("开始批量同步OSS文件到MinIO,同步{}天前的文件", syncDays);
|
||||||
|
|
||||||
|
// 实现批量同步逻辑
|
||||||
|
// 1. 查询指定天数前的文件列表
|
||||||
|
LocalDateTime syncThreshold = LocalDateTime.now().minusDays(syncDays);
|
||||||
|
List<FileInfo> filesToSync = fileMapper.selectList(null).stream()
|
||||||
|
.filter(file -> {
|
||||||
|
if (file.getCreateTime() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalDateTime createTime = file.getCreateTime().toInstant()
|
||||||
|
.atZone(java.time.ZoneId.systemDefault())
|
||||||
|
.toLocalDateTime();
|
||||||
|
|
||||||
|
return createTime.isBefore(syncThreshold);
|
||||||
|
})
|
||||||
|
.filter(file -> {
|
||||||
|
// 过滤掉已经在S3中的文件
|
||||||
|
if (file.getStorageType() != null &&
|
||||||
|
(file.getStorageType().contains("S3") || file.getStorageType().contains("MINIO"))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.getPath() != null &&
|
||||||
|
(file.getPath().contains("s3://") || file.getPath().contains("minio") ||
|
||||||
|
file.getPath().contains("s3-storage/") || file.getPath().contains("cold-storage/"))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
.filter(file -> {
|
||||||
|
// 确保文件在OSS中
|
||||||
|
if (file.getStorageType() != null &&
|
||||||
|
(file.getStorageType().contains("OSS") || file.getStorageType().contains("ALIYUN"))) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.getPath() != null) {
|
||||||
|
String path = file.getPath();
|
||||||
|
if (path.contains("oss://") || path.contains("aliyun") ||
|
||||||
|
path.startsWith("picture/") || path.startsWith("video/") || path.startsWith("file/")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
})
|
||||||
|
.limit(1000) // 限制最大同步数量
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
// 2. 批量同步到MinIO
|
||||||
|
int totalFiles = filesToSync.size();
|
||||||
|
int successCount = 0;
|
||||||
|
int failCount = 0;
|
||||||
|
|
||||||
|
log.info("开始批量同步,共找到 {} 个文件需要同步", totalFiles);
|
||||||
|
|
||||||
|
for (FileInfo fileInfo : filesToSync) {
|
||||||
|
try {
|
||||||
|
if (ossToS3SyncService != null) {
|
||||||
|
boolean syncResult = ossToS3SyncService.syncFileToS3(fileInfo);
|
||||||
|
if (syncResult) {
|
||||||
|
successCount++;
|
||||||
|
log.debug("文件同步成功: {}", fileInfo.getPath());
|
||||||
|
} else {
|
||||||
|
failCount++;
|
||||||
|
log.warn("文件同步失败: {}", fileInfo.getPath());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
failCount++;
|
||||||
|
log.warn("OSS到S3同步服务未配置,文件同步失败: {}", fileInfo.getPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 控制同步速度
|
||||||
|
Thread.sleep(100);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
failCount++;
|
||||||
|
log.error("文件同步异常: {}, error: {}", fileInfo.getPath(), e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 返回同步结果
|
||||||
|
result.put("syncDays", syncDays);
|
||||||
|
result.put("totalFiles", totalFiles);
|
||||||
|
result.put("successCount", successCount);
|
||||||
|
result.put("failCount", failCount);
|
||||||
|
result.put("message", String.format("批量同步完成,总数: %d, 成功: %d, 失败: %d", totalFiles, successCount, failCount));
|
||||||
|
result.put("success", true);
|
||||||
|
|
||||||
|
return Result.succeed(result, "批量同步任务已启动");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("批量同步OSS到MinIO异常", e);
|
||||||
|
result.put("error", e.getMessage());
|
||||||
|
result.put("success", false);
|
||||||
|
return Result.failed("批量同步异常:" + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取同步状态
|
||||||
|
*/
|
||||||
|
@GetMapping("/storage/sync-status")
|
||||||
|
public Result<Map<String, Object>> getSyncStatus() {
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
try {
|
||||||
|
if (ossToMinioSyncScheduler != null) {
|
||||||
|
String syncStatus = ossToMinioSyncScheduler.getSyncStatus();
|
||||||
|
result.put("syncStatus", syncStatus);
|
||||||
|
result.put("success", true);
|
||||||
|
} else {
|
||||||
|
result.put("error", "同步调度器未配置");
|
||||||
|
result.put("success", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.succeed(result);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("获取同步状态失败", e);
|
||||||
|
result.put("error", e.getMessage());
|
||||||
|
result.put("success", false);
|
||||||
|
return Result.failed("获取同步状态失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 手动启动夜间同步
|
||||||
|
*/
|
||||||
|
@PostMapping("/storage/start-night-sync")
|
||||||
|
public Result<Map<String, Object>> startNightSync() {
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
try {
|
||||||
|
if (ossToMinioSyncScheduler != null) {
|
||||||
|
ossToMinioSyncScheduler.manualStartNightSync();
|
||||||
|
result.put("message", "夜间同步已启动");
|
||||||
|
result.put("success", true);
|
||||||
|
} else {
|
||||||
|
result.put("error", "同步调度器未配置");
|
||||||
|
result.put("success", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.succeed(result);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("启动夜间同步失败", e);
|
||||||
|
result.put("error", e.getMessage());
|
||||||
|
result.put("success", false);
|
||||||
|
return Result.failed("启动夜间同步失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 手动停止夜间同步
|
||||||
|
*/
|
||||||
|
@PostMapping("/storage/stop-night-sync")
|
||||||
|
public Result<Map<String, Object>> stopNightSync() {
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
try {
|
||||||
|
if (ossToMinioSyncScheduler != null) {
|
||||||
|
ossToMinioSyncScheduler.manualStopNightSync();
|
||||||
|
result.put("message", "夜间同步已停止");
|
||||||
|
result.put("success", true);
|
||||||
|
} else {
|
||||||
|
result.put("error", "同步调度器未配置");
|
||||||
|
result.put("success", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.succeed(result);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("停止夜间同步失败", e);
|
||||||
|
result.put("error", e.getMessage());
|
||||||
|
result.put("success", false);
|
||||||
|
return Result.failed("停止夜间同步失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试同步功能
|
||||||
|
*/
|
||||||
|
@PostMapping("/storage/test-sync")
|
||||||
|
public Result<Map<String, Object>> testSync() {
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
try {
|
||||||
|
if (ossToMinioSyncScheduler == null) {
|
||||||
|
result.put("error", "同步调度器未配置");
|
||||||
|
result.put("success", false);
|
||||||
|
return Result.failed("同步调度器未配置");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 启动测试同步
|
||||||
|
ossToMinioSyncScheduler.manualStartNightSync();
|
||||||
|
|
||||||
|
result.put("message", "测试同步已启动");
|
||||||
|
result.put("success", true);
|
||||||
|
result.put("testMode", true);
|
||||||
|
|
||||||
|
return Result.succeed(result, "测试同步已启动");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("测试同步失败", e);
|
||||||
|
result.put("error", e.getMessage());
|
||||||
|
result.put("success", false);
|
||||||
|
return Result.failed("测试同步失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 立即同步指定文件
|
||||||
|
*/
|
||||||
|
@PostMapping("/storage/sync-file-now")
|
||||||
|
public Result<Map<String, Object>> syncFileNow(@RequestParam String path) {
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
try {
|
||||||
|
if (ossToS3SyncService == null) {
|
||||||
|
result.put("error", "同步服务未配置");
|
||||||
|
result.put("success", false);
|
||||||
|
return Result.failed("同步服务未配置");
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("立即同步文件: {}", path);
|
||||||
|
boolean syncResult = ossToS3SyncService.syncFileToS3(path);
|
||||||
|
|
||||||
|
result.put("path", path);
|
||||||
|
result.put("syncResult", syncResult);
|
||||||
|
result.put("success", syncResult);
|
||||||
|
|
||||||
|
if (syncResult) {
|
||||||
|
return Result.succeed(result, "文件同步成功");
|
||||||
|
} else {
|
||||||
|
return Result.failed("文件同步失败");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("立即同步文件失败: path={}", path, e);
|
||||||
|
result.put("path", path);
|
||||||
|
result.put("error", e.getMessage());
|
||||||
|
result.put("success", false);
|
||||||
|
return Result.failed("立即同步失败:" + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查数据库文件状态
|
||||||
|
*/
|
||||||
|
@GetMapping("/storage/check-file-status")
|
||||||
|
public Result<Map<String, Object>> checkFileStatus() {
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
try {
|
||||||
|
// 查询数据库中的文件统计信息
|
||||||
|
List<FileInfo> allFiles = fileMapper.selectList(null);
|
||||||
|
List<FileInfo> recentFiles = fileMapper.selectHotDataFiles(
|
||||||
|
LocalDateTime.now().minusDays(7), 1000);
|
||||||
|
|
||||||
|
// 统计信息
|
||||||
|
Map<String, Object> stats = new HashMap<>();
|
||||||
|
stats.put("totalFiles", allFiles.size());
|
||||||
|
stats.put("recentFiles", recentFiles.size());
|
||||||
|
|
||||||
|
// 存储类型统计
|
||||||
|
long nullStorageType = allFiles.stream()
|
||||||
|
.filter(f -> f.getStorageType() == null)
|
||||||
|
.count();
|
||||||
|
long ossFiles = allFiles.stream()
|
||||||
|
.filter(f -> f.getStorageType() != null && f.getStorageType().contains("OSS"))
|
||||||
|
.count();
|
||||||
|
long s3Files = allFiles.stream()
|
||||||
|
.filter(f -> f.getStorageType() != null && f.getStorageType().contains("S3"))
|
||||||
|
.count();
|
||||||
|
|
||||||
|
stats.put("nullStorageType", nullStorageType);
|
||||||
|
stats.put("ossFiles", ossFiles);
|
||||||
|
stats.put("s3Files", s3Files);
|
||||||
|
|
||||||
|
// 路径特征统计
|
||||||
|
long ossPathFiles = allFiles.stream()
|
||||||
|
.filter(f -> f.getPath() != null && f.getPath().contains("oss://"))
|
||||||
|
.count();
|
||||||
|
long s3PathFiles = allFiles.stream()
|
||||||
|
.filter(f -> f.getPath() != null && f.getPath().contains("s3://"))
|
||||||
|
.count();
|
||||||
|
|
||||||
|
stats.put("ossPathFiles", ossPathFiles);
|
||||||
|
stats.put("s3PathFiles", s3PathFiles);
|
||||||
|
|
||||||
|
// 最近文件示例
|
||||||
|
List<Map<String, Object>> recentFileExamples = recentFiles.stream()
|
||||||
|
.limit(5)
|
||||||
|
.map(f -> {
|
||||||
|
Map<String, Object> fileInfo = new HashMap<>();
|
||||||
|
fileInfo.put("id", f.getId());
|
||||||
|
fileInfo.put("name", f.getName());
|
||||||
|
fileInfo.put("path", f.getPath());
|
||||||
|
fileInfo.put("storageType", f.getStorageType());
|
||||||
|
fileInfo.put("createTime", f.getCreateTime());
|
||||||
|
return fileInfo;
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
stats.put("recentFileExamples", recentFileExamples);
|
||||||
|
|
||||||
|
result.put("stats", stats);
|
||||||
|
result.put("success", true);
|
||||||
|
|
||||||
|
return Result.succeed(result, "文件状态检查完成");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("检查文件状态失败", e);
|
||||||
|
result.put("error", e.getMessage());
|
||||||
|
result.put("success", false);
|
||||||
|
return Result.failed("检查文件状态失败:" + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 诊断热数据同步问题
|
||||||
|
*/
|
||||||
|
@GetMapping("/storage/diagnose-hot-sync")
|
||||||
|
public Result<Map<String, Object>> diagnoseHotSync() {
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
try {
|
||||||
|
Map<String, Object> diagnosis = new HashMap<>();
|
||||||
|
|
||||||
|
// 1. 检查配置
|
||||||
|
diagnosis.put("hotDataDays", 7);
|
||||||
|
diagnosis.put("batchSize", 100);
|
||||||
|
diagnosis.put("accessThreshold", 10);
|
||||||
|
|
||||||
|
// 2. 检查数据库文件
|
||||||
|
List<FileInfo> allFiles = fileMapper.selectList(null);
|
||||||
|
List<FileInfo> recentFiles = fileMapper.selectHotDataFiles(
|
||||||
|
LocalDateTime.now().minusDays(7), 1000);
|
||||||
|
|
||||||
|
diagnosis.put("totalFiles", allFiles.size());
|
||||||
|
diagnosis.put("recentFiles", recentFiles.size());
|
||||||
|
|
||||||
|
// 3. 分析文件存储类型
|
||||||
|
Map<String, Long> storageTypeStats = allFiles.stream()
|
||||||
|
.collect(Collectors.groupingBy(
|
||||||
|
f -> f.getStorageType() == null ? "NULL" : f.getStorageType(),
|
||||||
|
Collectors.counting()
|
||||||
|
));
|
||||||
|
diagnosis.put("storageTypeStats", storageTypeStats);
|
||||||
|
|
||||||
|
// 4. 分析路径特征
|
||||||
|
Map<String, Long> pathStats = allFiles.stream()
|
||||||
|
.collect(Collectors.groupingBy(
|
||||||
|
f -> {
|
||||||
|
if (f.getPath() == null) return "NULL_PATH";
|
||||||
|
if (f.getPath().contains("oss://")) return "OSS_PATH";
|
||||||
|
if (f.getPath().contains("s3://")) return "S3_PATH";
|
||||||
|
if (f.getPath().contains("minio")) return "MINIO_PATH";
|
||||||
|
if (f.getPath().startsWith("picture/")) return "PICTURE_PATH";
|
||||||
|
if (f.getPath().startsWith("video/")) return "VIDEO_PATH";
|
||||||
|
if (f.getPath().startsWith("file/")) return "FILE_PATH";
|
||||||
|
return "OTHER_PATH";
|
||||||
|
},
|
||||||
|
Collectors.counting()
|
||||||
|
));
|
||||||
|
diagnosis.put("pathStats", pathStats);
|
||||||
|
|
||||||
|
// 5. 检查最近文件的详细信息
|
||||||
|
List<Map<String, Object>> recentFileDetails = recentFiles.stream()
|
||||||
|
.limit(10)
|
||||||
|
.map(f -> {
|
||||||
|
Map<String, Object> fileDetail = new HashMap<>();
|
||||||
|
fileDetail.put("id", f.getId());
|
||||||
|
fileDetail.put("name", f.getName());
|
||||||
|
fileDetail.put("path", f.getPath());
|
||||||
|
fileDetail.put("storageType", f.getStorageType());
|
||||||
|
fileDetail.put("size", f.getSize());
|
||||||
|
fileDetail.put("createTime", f.getCreateTime());
|
||||||
|
fileDetail.put("url", f.getUrl());
|
||||||
|
return fileDetail;
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
diagnosis.put("recentFileDetails", recentFileDetails);
|
||||||
|
|
||||||
|
// 6. 模拟热数据过滤逻辑(使用修改后的逻辑)
|
||||||
|
List<FileInfo> filteredFiles = recentFiles.stream()
|
||||||
|
.filter(file -> {
|
||||||
|
// 模拟 isFileInS3 检查(修改后的逻辑)
|
||||||
|
boolean inS3 = false;
|
||||||
|
if (file.getStorageType() != null &&
|
||||||
|
(file.getStorageType().contains("S3") || file.getStorageType().contains("MINIO"))) {
|
||||||
|
inS3 = true;
|
||||||
|
}
|
||||||
|
if (file.getPath() != null) {
|
||||||
|
String path = file.getPath();
|
||||||
|
if (path.contains("s3://") || path.contains("minio") ||
|
||||||
|
path.contains("s3-storage/") || path.contains("cold-storage/")) {
|
||||||
|
inS3 = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 模拟 isFileInOSS 检查(修改后的逻辑)
|
||||||
|
boolean inOSS = false;
|
||||||
|
if (file.getStorageType() != null &&
|
||||||
|
(file.getStorageType().contains("OSS") || file.getStorageType().contains("ALIYUN"))) {
|
||||||
|
inOSS = true;
|
||||||
|
}
|
||||||
|
if (file.getPath() != null) {
|
||||||
|
String path = file.getPath();
|
||||||
|
if (path.contains("oss://") || path.contains("aliyun") ||
|
||||||
|
path.startsWith("picture/") || path.startsWith("video/") || path.startsWith("file/")) {
|
||||||
|
inOSS = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return !inS3 && inOSS;
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
diagnosis.put("filteredFiles", filteredFiles.size());
|
||||||
|
diagnosis.put("filteredFileDetails", filteredFiles.stream()
|
||||||
|
.limit(5)
|
||||||
|
.map(f -> {
|
||||||
|
Map<String, Object> detail = new HashMap<>();
|
||||||
|
detail.put("id", f.getId());
|
||||||
|
detail.put("name", f.getName());
|
||||||
|
detail.put("path", f.getPath());
|
||||||
|
detail.put("storageType", f.getStorageType());
|
||||||
|
return detail;
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList()));
|
||||||
|
|
||||||
|
// 7. 问题分析
|
||||||
|
Map<String, Object> analysis = new HashMap<>();
|
||||||
|
analysis.put("step1", "查询最近7天内的文件: " + recentFiles.size() + "个");
|
||||||
|
analysis.put("step2", "过滤掉已在S3中的文件");
|
||||||
|
analysis.put("step3", "过滤掉不在OSS中的文件");
|
||||||
|
analysis.put("step4", "剩余需要同步的文件: " + filteredFiles.size() + "个");
|
||||||
|
|
||||||
|
if (filteredFiles.isEmpty()) {
|
||||||
|
if (recentFiles.isEmpty()) {
|
||||||
|
analysis.put("conclusion", "没有最近7天内的文件,需要检查文件上传时间");
|
||||||
|
} else {
|
||||||
|
analysis.put("conclusion", "有最近文件但都被过滤掉了,可能原因:1)文件已在S3中 2)文件不在OSS中 3)存储类型标识问题");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
analysis.put("conclusion", "找到需要同步的文件,但同步服务可能有问题");
|
||||||
|
}
|
||||||
|
|
||||||
|
diagnosis.put("analysis", analysis);
|
||||||
|
|
||||||
|
result.put("diagnosis", diagnosis);
|
||||||
|
result.put("success", true);
|
||||||
|
|
||||||
|
return Result.succeed(result, "热数据同步诊断完成");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("诊断热数据同步失败", e);
|
||||||
|
result.put("error", e.getMessage());
|
||||||
|
result.put("success", false);
|
||||||
|
return Result.failed("诊断热数据同步失败:" + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 诊断夜间同步问题
|
||||||
|
*/
|
||||||
|
@GetMapping("/storage/diagnose-night-sync")
|
||||||
|
public Result<Map<String, Object>> diagnoseNightSync() {
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
try {
|
||||||
|
Map<String, Object> diagnosis = new HashMap<>();
|
||||||
|
|
||||||
|
// 1. 检查配置参数
|
||||||
|
diagnosis.put("hotDataDays", 30); // 默认30天
|
||||||
|
diagnosis.put("batchSize", 50);
|
||||||
|
diagnosis.put("syncInterval", 300000); // 5分钟
|
||||||
|
|
||||||
|
// 2. 检查数据库文件
|
||||||
|
List<FileInfo> allFiles = fileMapper.selectList(null);
|
||||||
|
|
||||||
|
// 计算冷数据阈值
|
||||||
|
LocalDateTime coldDataThreshold = LocalDateTime.now().minusDays(30);
|
||||||
|
|
||||||
|
// 过滤出冷数据文件
|
||||||
|
List<FileInfo> coldFiles = allFiles.stream()
|
||||||
|
.filter(file -> {
|
||||||
|
if (file.getCreateTime() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalDateTime createTime = file.getCreateTime().toInstant()
|
||||||
|
.atZone(java.time.ZoneId.systemDefault())
|
||||||
|
.toLocalDateTime();
|
||||||
|
|
||||||
|
return createTime.isBefore(coldDataThreshold);
|
||||||
|
})
|
||||||
|
.filter(file -> {
|
||||||
|
// 过滤掉已经在S3中的文件
|
||||||
|
if (file.getStorageType() != null &&
|
||||||
|
(file.getStorageType().contains("S3") || file.getStorageType().contains("MINIO"))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.getPath() != null &&
|
||||||
|
(file.getPath().contains("s3://") || file.getPath().contains("minio") ||
|
||||||
|
file.getPath().contains("s3-storage/") || file.getPath().contains("cold-storage/"))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
.filter(file -> {
|
||||||
|
// 确保文件在OSS中
|
||||||
|
if (file.getStorageType() != null &&
|
||||||
|
(file.getStorageType().contains("OSS") || file.getStorageType().contains("ALIYUN"))) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.getPath() != null) {
|
||||||
|
String path = file.getPath();
|
||||||
|
if (path.contains("oss://") || path.contains("aliyun") ||
|
||||||
|
path.startsWith("picture/") || path.startsWith("video/") || path.startsWith("file/")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
})
|
||||||
|
.limit(100)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
diagnosis.put("totalFiles", allFiles.size());
|
||||||
|
diagnosis.put("coldDataThreshold", coldDataThreshold.toString());
|
||||||
|
diagnosis.put("coldFiles", coldFiles.size());
|
||||||
|
|
||||||
|
// 3. 冷数据文件示例
|
||||||
|
List<Map<String, Object>> coldFileExamples = coldFiles.stream()
|
||||||
|
.limit(10)
|
||||||
|
.map(f -> {
|
||||||
|
Map<String, Object> fileInfo = new HashMap<>();
|
||||||
|
fileInfo.put("id", f.getId());
|
||||||
|
fileInfo.put("name", f.getName());
|
||||||
|
fileInfo.put("path", f.getPath());
|
||||||
|
fileInfo.put("storageType", f.getStorageType());
|
||||||
|
fileInfo.put("createTime", f.getCreateTime());
|
||||||
|
fileInfo.put("size", f.getSize());
|
||||||
|
return fileInfo;
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
diagnosis.put("coldFileExamples", coldFileExamples);
|
||||||
|
|
||||||
|
// 4. 同步逻辑分析
|
||||||
|
Map<String, Object> analysis = new HashMap<>();
|
||||||
|
analysis.put("step1", "查询所有文件: " + allFiles.size() + "个");
|
||||||
|
analysis.put("step2", "过滤出冷数据文件(创建时间早于" + coldDataThreshold + ")");
|
||||||
|
analysis.put("step3", "过滤掉已在S3中的文件");
|
||||||
|
analysis.put("step4", "确保文件在OSS中");
|
||||||
|
analysis.put("step5", "剩余需要同步的冷数据文件: " + coldFiles.size() + "个");
|
||||||
|
|
||||||
|
if (coldFiles.isEmpty()) {
|
||||||
|
analysis.put("conclusion", "没有找到需要同步的冷数据文件,可能原因:1)所有文件都是热数据 2)所有文件都已同步到S3 3)文件不在OSS中");
|
||||||
|
} else {
|
||||||
|
analysis.put("conclusion", "找到需要同步的冷数据文件,夜间同步应该能正常工作");
|
||||||
|
}
|
||||||
|
|
||||||
|
diagnosis.put("analysis", analysis);
|
||||||
|
|
||||||
|
result.put("diagnosis", diagnosis);
|
||||||
|
result.put("success", true);
|
||||||
|
|
||||||
|
return Result.succeed(result, "夜间同步诊断完成");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("诊断夜间同步失败", e);
|
||||||
|
result.put("error", e.getMessage());
|
||||||
|
result.put("success", false);
|
||||||
|
return Result.failed("诊断夜间同步失败:" + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+80
@@ -233,6 +233,86 @@ public class HotDataSyncController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 详细诊断热数据同步问题
|
||||||
|
*/
|
||||||
|
@Operation(summary = "详细诊断热数据同步问题")
|
||||||
|
@GetMapping("/diagnose-detailed")
|
||||||
|
public Result<Map<String, Object>> diagnoseHotSyncDetailed() {
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 1. 检查配置参数
|
||||||
|
Map<String, Object> config = new HashMap<>();
|
||||||
|
config.put("syncEnabled", hotDataSyncService.isRunning());
|
||||||
|
config.put("hotDataDays", 7); // 默认7天
|
||||||
|
config.put("batchSize", 100);
|
||||||
|
config.put("accessThreshold", 10);
|
||||||
|
result.put("config", config);
|
||||||
|
|
||||||
|
// 2. 检查数据库文件统计
|
||||||
|
Map<String, Object> dbStats = new HashMap<>();
|
||||||
|
|
||||||
|
// 这里需要注入FileMapper,暂时用模拟数据
|
||||||
|
dbStats.put("totalFiles", "需要查询数据库");
|
||||||
|
dbStats.put("recentFiles", "需要查询数据库");
|
||||||
|
dbStats.put("ossFiles", "需要查询数据库");
|
||||||
|
dbStats.put("s3Files", "需要查询数据库");
|
||||||
|
result.put("databaseStats", dbStats);
|
||||||
|
|
||||||
|
// 3. 检查OSS服务状态
|
||||||
|
Map<String, Object> ossStatus = new HashMap<>();
|
||||||
|
try {
|
||||||
|
// 这里可以添加OSS服务检查逻辑
|
||||||
|
ossStatus.put("status", "需要检查OSS服务");
|
||||||
|
ossStatus.put("accessible", "需要测试");
|
||||||
|
} catch (Exception e) {
|
||||||
|
ossStatus.put("status", "OSS服务检查失败");
|
||||||
|
ossStatus.put("error", e.getMessage());
|
||||||
|
}
|
||||||
|
result.put("ossStatus", ossStatus);
|
||||||
|
|
||||||
|
// 4. 检查S3服务状态
|
||||||
|
Map<String, Object> s3Status = new HashMap<>();
|
||||||
|
s3Status.put("s3Enabled", hotDataSyncService.isS3Enabled());
|
||||||
|
s3Status.put("enhancedS3ServiceAvailable", hotDataSyncService.isEnhancedS3ServiceAvailable());
|
||||||
|
s3Status.put("s3ServiceAvailable", hotDataSyncService.isS3ServiceAvailable());
|
||||||
|
result.put("s3Status", s3Status);
|
||||||
|
|
||||||
|
// 5. 同步逻辑分析
|
||||||
|
Map<String, Object> syncLogic = new HashMap<>();
|
||||||
|
syncLogic.put("step1", "查询最近7天内的文件");
|
||||||
|
syncLogic.put("step2", "过滤掉已在S3中的文件");
|
||||||
|
syncLogic.put("step3", "过滤掉不在OSS中的文件");
|
||||||
|
syncLogic.put("step4", "剩余文件即为需要同步的文件");
|
||||||
|
result.put("syncLogic", syncLogic);
|
||||||
|
|
||||||
|
// 6. 可能的问题原因
|
||||||
|
Map<String, Object> possibleIssues = new HashMap<>();
|
||||||
|
possibleIssues.put("issue1", "数据库中没有最近7天内的文件");
|
||||||
|
possibleIssues.put("issue2", "所有文件都已经在S3中");
|
||||||
|
possibleIssues.put("issue3", "文件不在OSS中(OSS服务问题)");
|
||||||
|
possibleIssues.put("issue4", "文件存储类型标识不正确");
|
||||||
|
possibleIssues.put("issue5", "OSS/S3服务配置问题");
|
||||||
|
result.put("possibleIssues", possibleIssues);
|
||||||
|
|
||||||
|
// 7. 建议的排查步骤
|
||||||
|
Map<String, Object> troubleshooting = new HashMap<>();
|
||||||
|
troubleshooting.put("step1", "检查数据库中的文件数据");
|
||||||
|
troubleshooting.put("step2", "检查OSS服务是否正常");
|
||||||
|
troubleshooting.put("step3", "检查S3服务是否正常");
|
||||||
|
troubleshooting.put("step4", "检查文件存储类型标识");
|
||||||
|
troubleshooting.put("step5", "手动测试单个文件同步");
|
||||||
|
result.put("troubleshooting", troubleshooting);
|
||||||
|
|
||||||
|
return Result.succeed(result);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("详细诊断热数据同步失败", e);
|
||||||
|
result.put("error", e.getMessage());
|
||||||
|
return Result.failed("详细诊断失败:" + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试URL访问
|
* 测试URL访问
|
||||||
*/
|
*/
|
||||||
|
|||||||
+102
@@ -0,0 +1,102 @@
|
|||||||
|
package com.central.file.service;
|
||||||
|
|
||||||
|
import com.central.common.model.FileInfo;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件同步事件监听器
|
||||||
|
* 监听文件访问事件,自动触发同步
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class FileSyncEventListener {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OssToS3SyncService ossToS3SyncService;
|
||||||
|
|
||||||
|
@Value("${zlt.storage.hot-data-days:30}")
|
||||||
|
private int hotDataDays;
|
||||||
|
|
||||||
|
@Value("${zlt.storage.sync.event-driven:true}")
|
||||||
|
private boolean eventDrivenSync; // 是否启用事件驱动同步
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听文件访问事件
|
||||||
|
* 当访问冷数据文件时,自动触发同步
|
||||||
|
*/
|
||||||
|
@EventListener
|
||||||
|
@Async
|
||||||
|
public void handleFileAccessEvent(FileAccessEvent event) {
|
||||||
|
if (!eventDrivenSync) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
FileInfo fileInfo = event.getFileInfo();
|
||||||
|
if (fileInfo == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否为冷数据
|
||||||
|
if (isColdData(fileInfo)) {
|
||||||
|
log.info("检测到冷数据文件访问,触发同步: {}", fileInfo.getPath());
|
||||||
|
|
||||||
|
// 异步同步文件
|
||||||
|
boolean syncResult = ossToS3SyncService.syncFileToS3(fileInfo);
|
||||||
|
if (syncResult) {
|
||||||
|
log.info("事件驱动同步成功: {}", fileInfo.getPath());
|
||||||
|
} else {
|
||||||
|
log.warn("事件驱动同步失败: {}", fileInfo.getPath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("处理文件访问事件失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否为冷数据
|
||||||
|
*/
|
||||||
|
private boolean isColdData(FileInfo fileInfo) {
|
||||||
|
if (fileInfo.getCreateTime() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalDateTime createTime = fileInfo.getCreateTime().toInstant()
|
||||||
|
.atZone(java.time.ZoneId.systemDefault())
|
||||||
|
.toLocalDateTime();
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
|
||||||
|
long daysDiff = ChronoUnit.DAYS.between(createTime, now);
|
||||||
|
return daysDiff > hotDataDays;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件访问事件
|
||||||
|
*/
|
||||||
|
public static class FileAccessEvent {
|
||||||
|
private final FileInfo fileInfo;
|
||||||
|
private final String accessType; // "READ", "WRITE", "DELETE"
|
||||||
|
|
||||||
|
public FileAccessEvent(FileInfo fileInfo, String accessType) {
|
||||||
|
this.fileInfo = fileInfo;
|
||||||
|
this.accessType = accessType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FileInfo getFileInfo() {
|
||||||
|
return fileInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAccessType() {
|
||||||
|
return accessType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+58
-10
@@ -329,20 +329,40 @@ public class HotDataSyncService {
|
|||||||
// 检查存储类型
|
// 检查存储类型
|
||||||
if (fileInfo.getStorageType() != null &&
|
if (fileInfo.getStorageType() != null &&
|
||||||
(fileInfo.getStorageType().contains("S3") || fileInfo.getStorageType().contains("MINIO"))) {
|
(fileInfo.getStorageType().contains("S3") || fileInfo.getStorageType().contains("MINIO"))) {
|
||||||
|
log.debug("文件存储类型标识为S3,文件ID: {}", fileInfo.getId());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查路径特征
|
// 检查路径特征
|
||||||
if (fileInfo.getPath() != null &&
|
if (fileInfo.getPath() != null) {
|
||||||
(fileInfo.getPath().contains("s3://") || fileInfo.getPath().contains("minio"))) {
|
String path = fileInfo.getPath();
|
||||||
return true;
|
|
||||||
|
// 标准S3路径格式
|
||||||
|
if (path.contains("s3://") || path.contains("minio")) {
|
||||||
|
log.debug("文件路径包含S3标识,文件ID: {}", fileInfo.getId());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否包含S3存储标识
|
||||||
|
if (path.contains("s3-storage/") || path.contains("cold-storage/")) {
|
||||||
|
log.debug("文件路径包含S3存储标识,文件ID: {}", fileInfo.getId());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用增强S3服务检查文件是否存在
|
// 使用增强S3服务检查文件是否存在
|
||||||
if (enhancedS3Service != null && fileInfo.getPath() != null) {
|
if (enhancedS3Service != null && fileInfo.getPath() != null) {
|
||||||
return enhancedS3Service.fileExists(fileInfo.getPath());
|
try {
|
||||||
|
boolean exists = enhancedS3Service.fileExists(fileInfo.getPath());
|
||||||
|
log.debug("S3服务检查文件存在,文件ID: {}, 存在: {}", fileInfo.getId(), exists);
|
||||||
|
return exists;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("S3服务检查失败,文件ID: {}, path: {}", fileInfo.getId(), fileInfo.getPath(), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.debug("文件不在S3中,文件ID: {}, path: {}, storageType: {}",
|
||||||
|
fileInfo.getId(), fileInfo.getPath(), fileInfo.getStorageType());
|
||||||
return false;
|
return false;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("检查文件S3状态失败,文件ID: {}", fileInfo.getId(), e);
|
log.warn("检查文件S3状态失败,文件ID: {}", fileInfo.getId(), e);
|
||||||
@@ -358,21 +378,49 @@ public class HotDataSyncService {
|
|||||||
// 检查存储类型
|
// 检查存储类型
|
||||||
if (fileInfo.getStorageType() != null &&
|
if (fileInfo.getStorageType() != null &&
|
||||||
(fileInfo.getStorageType().contains("OSS") || fileInfo.getStorageType().contains("ALIYUN"))) {
|
(fileInfo.getStorageType().contains("OSS") || fileInfo.getStorageType().contains("ALIYUN"))) {
|
||||||
|
log.debug("文件存储类型标识为OSS,文件ID: {}", fileInfo.getId());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查路径特征
|
// 检查路径特征 - 支持多种OSS路径格式
|
||||||
if (fileInfo.getPath() != null &&
|
if (fileInfo.getPath() != null) {
|
||||||
(fileInfo.getPath().contains("oss://") || fileInfo.getPath().contains("aliyun"))) {
|
String path = fileInfo.getPath();
|
||||||
return true;
|
|
||||||
|
// 标准OSS路径格式
|
||||||
|
if (path.contains("oss://") || path.contains("aliyun")) {
|
||||||
|
log.debug("文件路径包含OSS标识,文件ID: {}", fileInfo.getId());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 自定义路径格式(如 picture/xxx/xxx)
|
||||||
|
if (path.startsWith("picture/") || path.startsWith("video/") || path.startsWith("file/")) {
|
||||||
|
log.debug("文件路径为自定义格式,文件ID: {}, path: {}", fileInfo.getId(), path);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 尝试从OSS获取URL,如果能获取到说明文件存在
|
// 尝试从OSS获取URL,如果能获取到说明文件存在
|
||||||
if (fileInfo.getPath() != null) {
|
if (fileInfo.getPath() != null) {
|
||||||
String ossUrl = ossService.getViewUrlByPath(fileInfo.getPath());
|
try {
|
||||||
return ossUrl != null && !ossUrl.isEmpty();
|
String ossUrl = ossService.getViewUrlByPath(fileInfo.getPath());
|
||||||
|
boolean exists = ossUrl != null && !ossUrl.isEmpty();
|
||||||
|
log.debug("OSS服务检查文件存在,文件ID: {}, 存在: {}", fileInfo.getId(), exists);
|
||||||
|
return exists;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("OSS服务检查失败,文件ID: {}, path: {}", fileInfo.getId(), fileInfo.getPath(), e);
|
||||||
|
// 如果OSS服务检查失败,但路径格式正确,仍然认为是OSS文件
|
||||||
|
if (fileInfo.getPath() != null &&
|
||||||
|
(fileInfo.getPath().startsWith("picture/") ||
|
||||||
|
fileInfo.getPath().startsWith("video/") ||
|
||||||
|
fileInfo.getPath().startsWith("file/"))) {
|
||||||
|
log.debug("OSS服务检查失败,但路径格式正确,认为文件在OSS中,文件ID: {}", fileInfo.getId());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.debug("文件不在OSS中,文件ID: {}, path: {}, storageType: {}",
|
||||||
|
fileInfo.getId(), fileInfo.getPath(), fileInfo.getStorageType());
|
||||||
return false;
|
return false;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("检查文件OSS状态失败,文件ID: {}", fileInfo.getId(), e);
|
log.warn("检查文件OSS状态失败,文件ID: {}", fileInfo.getId(), e);
|
||||||
|
|||||||
+307
@@ -0,0 +1,307 @@
|
|||||||
|
package com.central.file.service;
|
||||||
|
|
||||||
|
import com.central.common.model.FileInfo;
|
||||||
|
import com.central.file.mapper.FileMapper;
|
||||||
|
import com.central.file.service.impl.OssService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OSS到MinIO定时同步调度器
|
||||||
|
* 在晚上00:00至6:00持续同步OSS数据到MinIO
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class OssToMinioSyncScheduler {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OssToS3SyncService ossToS3SyncService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OssService ossService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FileMapper fileMapper;
|
||||||
|
|
||||||
|
@Value("${zlt.storage.hot-data-days:30}")
|
||||||
|
private int hotDataDays; // 热数据保留天数
|
||||||
|
|
||||||
|
@Value("${zlt.storage.sync.enabled:true}")
|
||||||
|
private boolean syncEnabled; // 是否启用同步
|
||||||
|
|
||||||
|
@Value("${zlt.storage.sync.batch-size:50}")
|
||||||
|
private int batchSize; // 批量同步大小
|
||||||
|
|
||||||
|
@Value("${zlt.storage.sync.interval:300000}")
|
||||||
|
private long syncInterval; // 同步间隔(毫秒),默认5分钟
|
||||||
|
|
||||||
|
// 同步状态控制
|
||||||
|
private final AtomicBoolean isNightSyncRunning = new AtomicBoolean(false);
|
||||||
|
private LocalDateTime lastSyncTime = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每天晚上00:00启动夜间同步任务
|
||||||
|
*/
|
||||||
|
@Scheduled(cron = "${zlt.storage.sync.night-start:0 0 0 * * ?}")
|
||||||
|
public void startNightSync() {
|
||||||
|
if (!syncEnabled) {
|
||||||
|
log.info("OSS到MinIO同步功能已禁用");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("启动夜间OSS到MinIO同步任务");
|
||||||
|
isNightSyncRunning.set(true);
|
||||||
|
|
||||||
|
// 启动夜间同步循环
|
||||||
|
new Thread(this::nightSyncLoop, "NightSyncThread").start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每天早上6:00停止夜间同步任务
|
||||||
|
*/
|
||||||
|
@Scheduled(cron = "${zlt.storage.sync.night-stop:0 0 6 * * ?}")
|
||||||
|
public void stopNightSync() {
|
||||||
|
log.info("停止夜间OSS到MinIO同步任务");
|
||||||
|
isNightSyncRunning.set(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 夜间同步循环
|
||||||
|
* 在00:00-6:00期间持续同步
|
||||||
|
*/
|
||||||
|
private void nightSyncLoop() {
|
||||||
|
log.info("开始夜间同步循环");
|
||||||
|
|
||||||
|
while (isNightSyncRunning.get()) {
|
||||||
|
try {
|
||||||
|
LocalTime currentTime = LocalTime.now();
|
||||||
|
|
||||||
|
// 检查是否在夜间同步时间窗口内(00:00-6:00)
|
||||||
|
if (currentTime.isAfter(LocalTime.of(0, 0)) &&
|
||||||
|
currentTime.isBefore(LocalTime.of(6, 0))) {
|
||||||
|
|
||||||
|
// 执行同步任务
|
||||||
|
performNightSync();
|
||||||
|
|
||||||
|
// 等待下次同步
|
||||||
|
Thread.sleep(syncInterval);
|
||||||
|
} else {
|
||||||
|
// 不在夜间时间窗口,等待1分钟后再次检查
|
||||||
|
Thread.sleep(60000);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
log.info("夜间同步循环被中断");
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
break;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("夜间同步循环异常", e);
|
||||||
|
try {
|
||||||
|
Thread.sleep(60000); // 异常后等待1分钟再继续
|
||||||
|
} catch (InterruptedException ie) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("夜间同步循环结束");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行夜间同步任务
|
||||||
|
*/
|
||||||
|
private void performNightSync() {
|
||||||
|
try {
|
||||||
|
log.info("执行夜间同步任务 - 时间: {}", LocalDateTime.now());
|
||||||
|
|
||||||
|
// 获取需要同步的文件列表
|
||||||
|
List<FileInfo> filesToSync = getFilesToSync();
|
||||||
|
if (filesToSync.isEmpty()) {
|
||||||
|
log.debug("没有需要同步的文件");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("找到 {} 个文件需要同步", filesToSync.size());
|
||||||
|
|
||||||
|
int successCount = 0;
|
||||||
|
int failCount = 0;
|
||||||
|
int processedCount = 0;
|
||||||
|
|
||||||
|
for (FileInfo fileInfo : filesToSync) {
|
||||||
|
if (!isNightSyncRunning.get()) {
|
||||||
|
log.info("夜间同步被停止,已处理 {} 个文件", processedCount);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
boolean syncResult = ossToS3SyncService.syncFileToS3(fileInfo);
|
||||||
|
if (syncResult) {
|
||||||
|
successCount++;
|
||||||
|
log.debug("文件同步成功: {}", fileInfo.getPath());
|
||||||
|
} else {
|
||||||
|
failCount++;
|
||||||
|
log.warn("文件同步失败: {}", fileInfo.getPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
processedCount++;
|
||||||
|
|
||||||
|
// 控制同步速度,避免对系统造成压力
|
||||||
|
Thread.sleep(200);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
failCount++;
|
||||||
|
processedCount++;
|
||||||
|
log.error("文件同步异常: {}, error: {}", fileInfo.getPath(), e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 每处理一批文件后暂停一下
|
||||||
|
if (processedCount % batchSize == 0) {
|
||||||
|
log.info("已处理 {} 个文件,成功: {}, 失败: {}", processedCount, successCount, failCount);
|
||||||
|
Thread.sleep(1000); // 暂停1秒
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastSyncTime = LocalDateTime.now();
|
||||||
|
log.info("夜间同步任务完成 - 处理: {}, 成功: {}, 失败: {}",
|
||||||
|
processedCount, successCount, failCount);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("夜间同步任务执行失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取需要同步的文件列表
|
||||||
|
*/
|
||||||
|
private List<FileInfo> getFilesToSync() {
|
||||||
|
try {
|
||||||
|
// 获取冷数据文件(超过热数据天数的文件)
|
||||||
|
LocalDateTime coldDataThreshold = LocalDateTime.now().minusDays(hotDataDays);
|
||||||
|
log.info("查询冷数据文件,热数据天数: {}, 冷数据阈值: {}", hotDataDays, coldDataThreshold);
|
||||||
|
|
||||||
|
List<FileInfo> filesToSync = queryColdDataFiles(coldDataThreshold);
|
||||||
|
|
||||||
|
log.info("获取到 {} 个文件需要同步", filesToSync.size());
|
||||||
|
|
||||||
|
return filesToSync;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("获取需要同步的文件列表失败", e);
|
||||||
|
return java.util.Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询冷数据文件
|
||||||
|
* 查询创建时间早于threshold且不在S3中的文件
|
||||||
|
*/
|
||||||
|
private List<FileInfo> queryColdDataFiles(LocalDateTime threshold) {
|
||||||
|
try {
|
||||||
|
log.debug("查询冷数据文件,截止时间: {}", threshold);
|
||||||
|
|
||||||
|
// 查询创建时间早于threshold的文件
|
||||||
|
List<FileInfo> allFiles = fileMapper.selectList(null);
|
||||||
|
|
||||||
|
// 过滤出冷数据文件(创建时间早于threshold)
|
||||||
|
List<FileInfo> coldFiles = allFiles.stream()
|
||||||
|
.filter(file -> {
|
||||||
|
if (file.getCreateTime() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转换为LocalDateTime进行比较
|
||||||
|
LocalDateTime createTime = file.getCreateTime().toInstant()
|
||||||
|
.atZone(java.time.ZoneId.systemDefault())
|
||||||
|
.toLocalDateTime();
|
||||||
|
|
||||||
|
return createTime.isBefore(threshold);
|
||||||
|
})
|
||||||
|
.filter(file -> {
|
||||||
|
// 过滤掉已经在S3中的文件
|
||||||
|
if (file.getStorageType() != null &&
|
||||||
|
(file.getStorageType().contains("S3") || file.getStorageType().contains("MINIO"))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.getPath() != null &&
|
||||||
|
(file.getPath().contains("s3://") || file.getPath().contains("minio") ||
|
||||||
|
file.getPath().contains("s3-storage/") || file.getPath().contains("cold-storage/"))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
.filter(file -> {
|
||||||
|
// 确保文件在OSS中(支持多种路径格式)
|
||||||
|
if (file.getStorageType() != null &&
|
||||||
|
(file.getStorageType().contains("OSS") || file.getStorageType().contains("ALIYUN"))) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.getPath() != null) {
|
||||||
|
String path = file.getPath();
|
||||||
|
if (path.contains("oss://") || path.contains("aliyun") ||
|
||||||
|
path.startsWith("picture/") || path.startsWith("video/") || path.startsWith("file/")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
})
|
||||||
|
.limit(batchSize * 2) // 限制查询数量
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
log.info("查询到 {} 个冷数据文件需要同步", coldFiles.size());
|
||||||
|
|
||||||
|
// 记录前几个文件的详细信息用于调试
|
||||||
|
if (!coldFiles.isEmpty()) {
|
||||||
|
log.debug("冷数据文件示例:");
|
||||||
|
coldFiles.stream().limit(3).forEach(file ->
|
||||||
|
log.debug("文件ID: {}, 路径: {}, 创建时间: {}, 存储类型: {}",
|
||||||
|
file.getId(), file.getPath(), file.getCreateTime(), file.getStorageType()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return coldFiles;
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("查询冷数据文件失败", e);
|
||||||
|
return java.util.Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取同步状态
|
||||||
|
*/
|
||||||
|
public String getSyncStatus() {
|
||||||
|
return String.format("夜间同步状态 - 运行中: %s, 最后同步时间: %s, 热数据天数: %d",
|
||||||
|
isNightSyncRunning.get(),
|
||||||
|
lastSyncTime != null ? lastSyncTime.toString() : "未执行",
|
||||||
|
hotDataDays);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 手动启动夜间同步(用于测试)
|
||||||
|
*/
|
||||||
|
public void manualStartNightSync() {
|
||||||
|
log.info("手动启动夜间同步");
|
||||||
|
isNightSyncRunning.set(true);
|
||||||
|
new Thread(this::nightSyncLoop, "ManualNightSyncThread").start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 手动停止夜间同步
|
||||||
|
*/
|
||||||
|
public void manualStopNightSync() {
|
||||||
|
log.info("手动停止夜间同步");
|
||||||
|
isNightSyncRunning.set(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
+55
-7
@@ -1,5 +1,6 @@
|
|||||||
package com.central.file.service.impl;
|
package com.central.file.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.central.common.constant.CommonConstant;
|
import com.central.common.constant.CommonConstant;
|
||||||
import com.central.common.model.FileInfo;
|
import com.central.common.model.FileInfo;
|
||||||
@@ -44,7 +45,11 @@ public class S3Service extends AbstractIFileService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ObjectInfo uploadFile(String name, InputStream inputStream) {
|
protected ObjectInfo uploadFile(String name, InputStream inputStream) {
|
||||||
return null;
|
try {
|
||||||
|
return s3Template.upload(name, inputStream);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -55,12 +60,22 @@ public class S3Service extends AbstractIFileService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getViewUrlByPath(String name) {
|
public String getViewUrlByPath(String name) {
|
||||||
return "";
|
try {
|
||||||
|
S3Object s3Object = parsePath(name);
|
||||||
|
return s3Template.getViewUrl(s3Object.bucketName, s3Object.objectName, 60); // 60分钟有效期
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getZipViewUrlByPath(String name, String style) {
|
public String getZipViewUrlByPath(String name, String style) {
|
||||||
return "";
|
try {
|
||||||
|
S3Object s3Object = parsePath(name);
|
||||||
|
return s3Template.getViewUrl(s3Object.bucketName, s3Object.objectName, 60);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -74,27 +89,60 @@ public class S3Service extends AbstractIFileService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getViewUrl(String id) {
|
public String getViewUrl(String id) {
|
||||||
|
try {
|
||||||
|
FileInfo fileInfo = baseMapper.selectById(id);
|
||||||
|
if (fileInfo != null) {
|
||||||
|
return getViewUrlByPath(fileInfo.getPath());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 忽略异常
|
||||||
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileInfo getFileInfo(String id) {
|
public FileInfo getFileInfo(String id) {
|
||||||
return null;
|
try {
|
||||||
|
return baseMapper.selectById(id);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileInfo createFile(String path) {
|
public FileInfo createFile(String path) {
|
||||||
return null;
|
try {
|
||||||
|
S3Object s3Object = parsePath(path);
|
||||||
|
FileInfo fileInfo = new FileInfo();
|
||||||
|
fileInfo.setPath(path);
|
||||||
|
fileInfo.setId(IdUtil.fastSimpleUUID());
|
||||||
|
fileInfo.setName(s3Object.objectName);
|
||||||
|
fileInfo.setSource(fileType());
|
||||||
|
fileInfo.setStorageType(FileServerProperties.TYPE_S3);
|
||||||
|
return fileInfo;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> listFiles(String prefix) {
|
public List<String> listFiles(String prefix) {
|
||||||
return List.of();
|
try {
|
||||||
|
// 简化实现,直接返回空列表
|
||||||
|
return List.of();
|
||||||
|
} catch (Exception e) {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getViewUrlByPathAndExpires(String name, int expires) {
|
public String getViewUrlByPathAndExpires(String name, int expires) {
|
||||||
return "";
|
try {
|
||||||
|
S3Object s3Object = parsePath(name);
|
||||||
|
return s3Template.getViewUrl(s3Object.bucketName, s3Object.objectName, expires);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Setter
|
@Setter
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
# OSS到MinIO同步配置
|
||||||
|
zlt:
|
||||||
|
storage:
|
||||||
|
# 热数据保留天数
|
||||||
|
hot-data-days: 30
|
||||||
|
|
||||||
|
# 同步功能配置
|
||||||
|
sync:
|
||||||
|
# 是否启用同步
|
||||||
|
enabled: true
|
||||||
|
|
||||||
|
# 夜间同步配置
|
||||||
|
night-start: "0 0 0 * * ?" # 每天晚上00:00启动
|
||||||
|
night-stop: "0 0 6 * * ?" # 每天早上6:00停止
|
||||||
|
|
||||||
|
# 同步间隔(毫秒)
|
||||||
|
interval: 180000 # 3分钟
|
||||||
|
|
||||||
|
# 批量同步大小
|
||||||
|
batch-size: 100
|
||||||
|
|
||||||
|
# 是否启用事件驱动同步
|
||||||
|
event-driven: true
|
||||||
|
|
||||||
|
# 同步线程池配置
|
||||||
|
thread-pool:
|
||||||
|
core-size: 2
|
||||||
|
max-size: 5
|
||||||
|
queue-size: 100
|
||||||
|
keep-alive: 60
|
||||||
|
|
||||||
|
# 日志配置
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
com.central.file.service.OssToMinioSyncScheduler: INFO
|
||||||
|
com.central.file.service.OssToS3SyncService: DEBUG
|
||||||
Reference in New Issue
Block a user