• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / jetcache / #380

04 Sep 2023 07:27AM CUT coverage: 88.931% (+0.002%) from 88.929%
#380

push

web-flow
for springboot3 remove javax (#813)

* for springboot3 remove javax

* CacheManager destroyMethod

* SpringConfigProvider destroyMethod

26 of 26 new or added lines in 2 files covered. (100.0%)

4684 of 5267 relevant lines covered (88.93%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.33
/jetcache-core/src/main/java/com/alicp/jetcache/support/DefaultMetricsManager.java
1
package com.alicp.jetcache.support;
2

3
import org.slf4j.Logger;
4
import org.slf4j.LoggerFactory;
5

6
import java.time.LocalDateTime;
7
import java.time.ZoneId;
8
import java.util.Arrays;
9
import java.util.List;
10
import java.util.concurrent.CopyOnWriteArrayList;
11
import java.util.concurrent.ScheduledFuture;
12
import java.util.concurrent.TimeUnit;
13
import java.util.concurrent.locks.ReentrantLock;
14
import java.util.function.Consumer;
15
import java.util.stream.Collectors;
16

17
/**
18
 * Created on 2016/10/31.
19
 *
20
 * @author <a href="mailto:areyouok@gmail.com">huangli</a>
21
 */
22
public class DefaultMetricsManager {
23
    private static final Logger logger = LoggerFactory.getLogger(DefaultMetricsManager.class);
1✔
24

25
    protected final CopyOnWriteArrayList<DefaultCacheMonitor> monitorList = new CopyOnWriteArrayList();
1✔
26

27
    private ScheduledFuture<?> future;
28

29
    private final int resetTime;
30
    private final TimeUnit resetTimeUnit;
31
    private final Consumer<StatInfo> metricsCallback;
32
    private final ReentrantLock reentrantLock = new ReentrantLock();
1✔
33

34
    public DefaultMetricsManager(int resetTime, TimeUnit resetTimeUnit, Consumer<StatInfo> metricsCallback) {
1✔
35
        this.resetTime = resetTime;
1✔
36
        this.resetTimeUnit = resetTimeUnit;
1✔
37
        this.metricsCallback = metricsCallback;
1✔
38
    }
1✔
39

40
    public DefaultMetricsManager(int resetTime, TimeUnit resetTimeUnit) {
41
        this(resetTime, resetTimeUnit, false);
×
42
    }
×
43

44
    public DefaultMetricsManager(int resetTime, TimeUnit resetTimeUnit, boolean verboseLog) {
1✔
45
        this.resetTime = resetTime;
1✔
46
        this.resetTimeUnit = resetTimeUnit;
1✔
47
        this.metricsCallback = new StatInfoLogger(verboseLog);
1✔
48
    }
1✔
49

50
    final Runnable cmd = new Runnable() {
1✔
51
        private long time = System.currentTimeMillis();
1✔
52

53
        @Override
54
        public void run() {
55
            try {
56
                List<CacheStat> stats = monitorList.stream().map((m) -> {
1✔
57
                    CacheStat stat = m.getCacheStat();
1✔
58
                    m.resetStat();
1✔
59
                    return stat;
1✔
60
                }).collect(Collectors.toList());
1✔
61

62
                long endTime = System.currentTimeMillis();
1✔
63
                StatInfo statInfo = new StatInfo();
1✔
64
                statInfo.setStartTime(time);
1✔
65
                statInfo.setEndTime(endTime);
1✔
66
                statInfo.setStats(stats);
1✔
67
                time = endTime;
1✔
68

69
                metricsCallback.accept(statInfo);
1✔
70
            } catch (Exception e) {
×
71
                logger.error("jetcache DefaultMetricsManager error", e);
×
72
            }
1✔
73
        }
1✔
74
    };
75

76

77
    public void start() {
78
        reentrantLock.lock();
1✔
79
        try {
80
            if (future != null) {
1✔
81
                return;
×
82
            }
83
            long delay = firstDelay(resetTime, resetTimeUnit);
1✔
84
            future = JetCacheExecutor.defaultExecutor().scheduleAtFixedRate(
1✔
85
                    cmd, delay, resetTimeUnit.toMillis(resetTime), TimeUnit.MILLISECONDS);
1✔
86
            logger.info("cache stat period at " + resetTime + " " + resetTimeUnit);
1✔
87
        }finally {
88
            reentrantLock.unlock();
1✔
89
        }
90
    }
1✔
91

92
    public void stop() {
93
        reentrantLock.lock();
1✔
94
        try {
95
            future.cancel(false);
1✔
96
            logger.info("cache stat canceled");
1✔
97
            future = null;
1✔
98
        }finally {
99
            reentrantLock.unlock();
1✔
100
        }
101
    }
1✔
102

103
    public void add(DefaultCacheMonitor... monitors) {
104
        monitorList.addAll(Arrays.asList(monitors));
1✔
105
    }
1✔
106

107
    public void clear() {
108
        monitorList.clear();
1✔
109
    }
1✔
110

111
    protected static long firstDelay(int resetTime, TimeUnit resetTimeUnit) {
112
        LocalDateTime firstResetTime = computeFirstResetTime(LocalDateTime.now(), resetTime, resetTimeUnit);
1✔
113
        return firstResetTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() - System.currentTimeMillis();
1✔
114
    }
115

116
    protected static LocalDateTime computeFirstResetTime(LocalDateTime baseTime, int time, TimeUnit unit) {
117
        if (unit != TimeUnit.SECONDS && unit != TimeUnit.MINUTES && unit != TimeUnit.HOURS && unit != TimeUnit.DAYS) {
1✔
118
            throw new IllegalArgumentException();
1✔
119
        }
120
        LocalDateTime t = baseTime;
1✔
121
        switch (unit) {
1✔
122
            case DAYS:
123
                t = t.plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
1✔
124
                break;
1✔
125
            case HOURS:
126
                if (24 % time == 0) {
1✔
127
                    t = t.plusHours(time - t.getHour() % time);
1✔
128
                } else {
129
                    t = t.plusHours(1);
1✔
130
                }
131
                t = t.withMinute(0).withSecond(0).withNano(0);
1✔
132
                break;
1✔
133
            case MINUTES:
134
                if (60 % time == 0) {
1✔
135
                    t = t.plusMinutes(time - t.getMinute() % time);
1✔
136
                } else {
137
                    t = t.plusMinutes(1);
1✔
138
                }
139
                t = t.withSecond(0).withNano(0);
1✔
140
                break;
1✔
141
            case SECONDS:
142
                if (60 % time == 0) {
1✔
143
                    t = t.plusSeconds(time - t.getSecond() % time);
1✔
144
                } else {
145
                    t = t.plusSeconds(1);
1✔
146
                }
147
                t = t.withNano(0);
1✔
148
                break;
149
        }
150
        return t;
1✔
151
    }
152

153

154
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc