DataSourceSyncRecorder.java
6.67 KB
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
/*
* Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software;Designed and Developed mainly by many Chinese
* opensource volunteers. you can redistribute it and/or modify it under the
* terms of the GNU General Public License version 2 only, as published by the
* Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Any questions about this component can be directed to it's project Web address
* https://code.google.com/p/opencloudb/.
*
*/
package io.mycat.statistic;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import io.mycat.config.model.DataHostConfig;
import io.mycat.util.TimeUtil;
/**
* 记录最近3个时段的平均响应时间,默认1,10,30分钟。
*
* @author songwie
*/
public class DataSourceSyncRecorder {
private Map<String, String> records;
private final List<Record> asynRecords;//value,time
private static final Logger LOGGER = LoggerFactory.getLogger("DataSourceSyncRecorder");
private static final long SWAP_TIME = 24 * 60 * 60 * 1000L;
//日期处理
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private int switchType = 2;
public DataSourceSyncRecorder() {
this.records = new HashMap<String, String>() ;
this.asynRecords = new LinkedList<Record>();
}
public String get() {
return records.toString();
}
public void set(Map<String, String> resultResult,int switchType) {
try{
long time = TimeUtil.currentTimeMillis();
this.switchType = switchType;
remove(time);
if (resultResult!=null && !resultResult.isEmpty()) {
this.records = resultResult;
if(switchType==DataHostConfig.SYN_STATUS_SWITCH_DS){ //slave
String sencords = resultResult.get("Seconds_Behind_Master");
long Seconds_Behind_Master = -1;
if(sencords!=null){
Seconds_Behind_Master = Long.parseLong(sencords);
}
this.asynRecords.add(new Record(TimeUtil.currentTimeMillis(),Seconds_Behind_Master));
}
if(switchType==DataHostConfig.CLUSTER_STATUS_SWITCH_DS){//cluster
double wsrep_local_recv_queue_avg = Double.valueOf(resultResult.get("wsrep_local_recv_queue_avg"));
this.asynRecords.add(new Record(TimeUtil.currentTimeMillis(),wsrep_local_recv_queue_avg));
}
return;
}
}catch(Exception e){
LOGGER.error("record DataSourceSyncRecorder error " + e.getMessage());
}
}
/**
* 删除超过统计时间段的数据
*/
private void remove(long time) {
final List<Record> recordsAll = this.asynRecords;
while (recordsAll.size() > 0) {
Record record = recordsAll.get(0);
if (time >= record.time + SWAP_TIME) {
recordsAll.remove(0);
} else {
break;
}
}
}
public int getSwitchType() {
return this.switchType;
}
public void setSwitchType(int switchType) {
this.switchType = switchType;
}
public Map<String, String> getRecords() {
return this.records;
}
public List<Record> getAsynRecords() {
return this.asynRecords;
}
public static SimpleDateFormat getSdf() {
return sdf;
}
/**
* @author mycat
*/
public static class Record {
private Object value;
private long time;
Record(long time, Object value) {
this.time = time;
this.value = value;
}
public Object getValue() {
return this.value;
}
public void setValue(Object value) {
this.value = value;
}
public long getTime() {
return this.time;
}
public void setTime(long time) {
this.time = time;
}
}
}