#
Administrator
17 小时以前 3d00a1bef761c34adee410454eb0ede54f882751
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package com.zy.core.task;
 
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zy.asrs.entity.DeviceDataLog;
import com.zy.asrs.service.DeviceDataLogService;
import com.zy.common.utils.RedisUtil;
import com.zy.core.enums.RedisKeyType;
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.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Stream;
import java.util.stream.Collectors;
 
@Slf4j
@Component
public class DeviceLogScheduler {
 
    private static final int BASE_BATCH_SIZE = 100;
    private static final int BACKLOG_SCAN_LIMIT = 2000;
    private static final int MAX_BATCH_SIZE = 1000;
 
    @Value("${deviceLogStorage.type}")
    private String storageType;
    @Value("${deviceLogStorage.loggingPath}")
    private String loggingPath;
    @Value("${deviceLogStorage.expireDays}")
    private Integer expireDays;
    @Autowired
    private DeviceDataLogService deviceDataLogService;
    @Autowired
    private RedisUtil redisUtil;
 
    private static final ReentrantLock FILE_OP_LOCK = new ReentrantLock();
 
    @Scheduled(cron = "0/3 * * * * ? ")
    public void delDeviceLog() {
        if ("mysql".equals(storageType)) {
            deviceDataLogService.clearLog(expireDays == null ? 1 : expireDays);
        }else if ("file".equals(storageType)) {
            if (!FILE_OP_LOCK.tryLock()) {
                return;
            }
            try {
                clearFileLog(expireDays == null ? 1 : expireDays);
            } finally {
                FILE_OP_LOCK.unlock();
            }
        }else {
            log.error("未定义的存储类型:{}", storageType);
        }
    }
 
    @Scheduled(cron = "0/3 * * * * ? ")
    public void execute() {
        Set<String> scannedKeys = redisUtil.scanKeys(RedisKeyType.DEVICE_LOG_KEY.key, BACKLOG_SCAN_LIMIT);
        if (scannedKeys == null || scannedKeys.isEmpty()) {
            return;
        }
        Set<String> keys = selectBatchKeys(scannedKeys);
        List<Object> values = redisUtil.multiGet(keys);
        List<DeviceDataLog> list = new ArrayList<>();
        for (Object object : values) {
            if (object instanceof DeviceDataLog) {
                list.add((DeviceDataLog) object);
            }
        }
        if (!list.isEmpty()) {
            if ("mysql".equals(storageType)) {
                mysqlSave(keys, list);
            }else if ("file".equals(storageType)) {
                if (!FILE_OP_LOCK.tryLock()) {
                    return;
                }
                try {
                    fileSave(keys, list);
                } finally {
                    FILE_OP_LOCK.unlock();
                }
            }else {
                log.error("未定义的存储类型:{}", storageType);
            }
        }
    }
 
    private Set<String> selectBatchKeys(Set<String> scannedKeys) {
        int backlog = scannedKeys.size();
        int batchSize = resolveBatchSize(backlog);
        if (backlog <= batchSize) {
            return scannedKeys;
        }
        LinkedHashSet<String> selected = new LinkedHashSet<>();
        for (String key : scannedKeys) {
            selected.add(key);
            if (selected.size() >= batchSize) {
                break;
            }
        }
        return selected;
    }
 
    private int resolveBatchSize(int backlog) {
        if (backlog <= BASE_BATCH_SIZE) {
            return backlog;
        }
        int adaptive = Math.max(BASE_BATCH_SIZE, backlog / 2);
        int rounded = ((adaptive + BASE_BATCH_SIZE - 1) / BASE_BATCH_SIZE) * BASE_BATCH_SIZE;
        return Math.min(MAX_BATCH_SIZE, rounded);
    }
 
    private void mysqlSave(Set<String> keys, List<DeviceDataLog> list) {
        if (deviceDataLogService.saveBatch(list)) {
            redisUtil.del(keys.toArray(new String[0]));
        }
    }
 
    private void fileSave(Set<String> keys, List<DeviceDataLog> list) {
        try {
            Path baseDir = Paths.get(loggingPath);
            Files.createDirectories(baseDir);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Map<String, Map<String, List<DeviceDataLog>>> group = new HashMap<>();
            for (DeviceDataLog logItem : list) {
                String typeName = logItem.getType();
                String datePart = sdf.format(logItem.getCreateTime() == null ? new Date() : logItem.getCreateTime());
                String prefix = typeName + "_" + String.valueOf(logItem.getDeviceNo()) + "_" + datePart + "_";
                group.computeIfAbsent(datePart, k -> new HashMap<>())
                        .computeIfAbsent(prefix, k -> new ArrayList<>())
                        .add(logItem);
            }
            for (Map.Entry<String, Map<String, List<DeviceDataLog>>> dateEntry : group.entrySet()) {
                Path dayDir = baseDir.resolve(dateEntry.getKey());
                Files.createDirectories(dayDir);
                for (Map.Entry<String, List<DeviceDataLog>> entry : dateEntry.getValue().entrySet()) {
                    String prefix = entry.getKey();
                    List<DeviceDataLog> logs = entry.getValue();
                    logs.sort(Comparator.comparing(DeviceDataLog::getCreateTime, Comparator.nullsLast(Date::compareTo)));
                    int index = findStartIndex(dayDir, prefix);
                    Path current = dayDir.resolve(prefix + index + ".log");
                    if (!Files.exists(current)) {
                        Files.createFile(current);
                    }
                    long size = Files.size(current);
                    long max = 1024L * 1024L;
                    for (DeviceDataLog d : logs) {
                        String json = JSON.toJSONStringWithDateFormat(d, "yyyy-MM-dd HH:mm:ss.SSS", SerializerFeature.WriteDateUseDateFormat);
                        byte[] line = (json + System.lineSeparator()).getBytes(StandardCharsets.UTF_8);
                        if (size + line.length > max) {
                            index++;
                            current = dayDir.resolve(prefix + index + ".log");
                            if (!Files.exists(current)) {
                                Files.createFile(current);
                            }
                            size = 0;
                        }
                        Files.write(current, line, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
                        size += line.length;
                    }
                }
            }
            redisUtil.del(keys.toArray(new String[0]));
        } catch (Exception e) {
            log.error("设备日志文件存储失败", e);
        }
    }
 
    private int findStartIndex(Path baseDir, String prefix) throws Exception {
        List<Path> matched;
        try (Stream<Path> stream = Files.list(baseDir)) {
            matched = stream
                    .filter(p -> {
                        String n = p.getFileName().toString();
                        return n.startsWith(prefix) && n.endsWith(".log");
                    })
                    .collect(Collectors.toList());
        }
        int maxIdx = 0;
        for (Path p : matched) {
            String name = p.getFileName().toString();
            String suf = name.substring(prefix.length());
            if (!suf.isEmpty()) {
                try {
                    int val = Integer.parseInt(suf.replace(".log", ""));
                    if (val > maxIdx) {
                        maxIdx = val;
                    }
                } catch (NumberFormatException ignored) {}
            }
        }
        int candidate = maxIdx == 0 ? 1 : maxIdx;
        Path path = baseDir.resolve(prefix + candidate + ".log");
        if (Files.exists(path)) {
            long size = Files.size(path);
            if (size >= 1024L * 1024L) {
                return candidate + 1;
            }
        }
        return candidate;
    }
 
    private void clearFileLog(int days) {
        try {
            Path baseDir = Paths.get(loggingPath);
            if (!Files.exists(baseDir)) {
                return;
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            long cutoff = System.currentTimeMillis() - (long) days * 24 * 60 * 60 * 1000;
            List<Path> dirs;
            try (Stream<Path> stream = Files.list(baseDir)) {
                dirs = stream.filter(Files::isDirectory).collect(Collectors.toList());
            }
            for (Path dir : dirs) {
                String name = dir.getFileName().toString();
                if (name.length() == 8 && name.chars().allMatch(Character::isDigit)) {
                    Date d = sdf.parse(name);
                    if (d.getTime() < cutoff) {
                        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
                            @Override
                            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                                try {
                                    Files.deleteIfExists(file);
                                } catch (Exception ignored) {}
                                return FileVisitResult.CONTINUE;
                            }
 
                            @Override
                            public FileVisitResult postVisitDirectory(Path dir, java.io.IOException exc) {
                                try {
                                    Files.deleteIfExists(dir);
                                } catch (Exception ignored) {}
                                return FileVisitResult.CONTINUE;
                            }
                        });
                    }
                }
            }
        } catch (Exception e) {
            log.error("设备日志文件清理失败", e);
        }
    }
 
}