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

alibaba / jetcache / #405

16 Apr 2024 05:58AM UTC coverage: 0.0% (-88.9%) from 88.866%
#405

push

areyouok
add encoding to fix coverage report

0 of 5353 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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 huangli
21
 */
22
public class DefaultMetricsManager {
23
    private static final Logger logger = LoggerFactory.getLogger(DefaultMetricsManager.class);
×
24

25
    protected final CopyOnWriteArrayList<DefaultCacheMonitor> monitorList = new CopyOnWriteArrayList();
×
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();
×
33

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

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

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

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

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

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

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

76

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

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

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

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

111
    protected static long firstDelay(int resetTime, TimeUnit resetTimeUnit) {
112
        LocalDateTime firstResetTime = computeFirstResetTime(LocalDateTime.now(), resetTime, resetTimeUnit);
×
113
        return firstResetTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() - System.currentTimeMillis();
×
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) {
×
118
            throw new IllegalArgumentException();
×
119
        }
120
        LocalDateTime t = baseTime;
×
121
        switch (unit) {
×
122
            case DAYS:
123
                t = t.plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
×
124
                break;
×
125
            case HOURS:
126
                if (24 % time == 0) {
×
127
                    t = t.plusHours(time - t.getHour() % time);
×
128
                } else {
129
                    t = t.plusHours(1);
×
130
                }
131
                t = t.withMinute(0).withSecond(0).withNano(0);
×
132
                break;
×
133
            case MINUTES:
134
                if (60 % time == 0) {
×
135
                    t = t.plusMinutes(time - t.getMinute() % time);
×
136
                } else {
137
                    t = t.plusMinutes(1);
×
138
                }
139
                t = t.withSecond(0).withNano(0);
×
140
                break;
×
141
            case SECONDS:
142
                if (60 % time == 0) {
×
143
                    t = t.plusSeconds(time - t.getSecond() % time);
×
144
                } else {
145
                    t = t.plusSeconds(1);
×
146
                }
147
                t = t.withNano(0);
×
148
                break;
149
        }
150
        return t;
×
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