blob: 38fb7c667a9e705ba2cd2065e57a2af2d5207b61 (
plain)
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
|
package org.onap.msb.apiroute.wrapper.consulextend.expose;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
import org.onap.msb.apiroute.wrapper.util.ServiceFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.ImmutableList;
import com.orbitz.consul.model.ConsulResponse;
import com.orbitz.consul.model.health.HealthCheck;
public class ServiceModifyIndexFilter implements WatchTask.Filter<List<ServiceHealth>> {
private final AtomicReference<ImmutableList<ServiceHealth>> lastResponse =
new AtomicReference<ImmutableList<ServiceHealth>>(ImmutableList.<ServiceHealth>of());
private final static Logger LOGGER = LoggerFactory.getLogger(ServiceModifyIndexFilter.class);
@Override
public boolean filter(ConsulResponse<List<ServiceHealth>> object) {
// TODO Auto-generated method stub
List<ServiceHealth> newList=object.getResponse();
if(realFilter(newList)){
lastResponse.set(ImmutableList.copyOf(newList));
return true;
}
return false;
}
private boolean realFilter(List<ServiceHealth> newList) {
// 1)判断list的size,不等则改变
if (newList.size() != lastResponse.get().size()) {
// 第一次不打印
if (lastResponse.get().size() != 0) {
LOGGER.info(newList.get(0).getService().getService()
+ " instance count is different.new_count:" + newList.size() + " old_count:"
+ lastResponse.get().size());
}
return true;
}
// 2)循环服务实例判断服务内容和健康检查是否改变
for (ServiceHealth newData : newList) {
ServiceHealth sameIdOldData = findSameIdInOldList(newData);
// 若在oldlist中不存在,则改变
if (sameIdOldData == null) {
LOGGER.info(newData.getService().getId()
+ " is a new service instance.the createindex:"
+ newData.getService().getCreateIndex()
+ " the modifyIndex:"
+ newData.getService().getModifyIndex());
return true;
}
// 若在oldlist中存在,则比较ModifyIndex的值和健康检查状态.不等则改变
if(!compareService(newData,sameIdOldData)){
LOGGER.info(newData.getService().getId() +" instance is change because of modifyIndex or health check" );
return true;
}
}
return false;
}
private boolean compareService(ServiceHealth oldData,ServiceHealth newData) {
return compareServiceInfo(oldData.getService(),newData.getService()) &&
compareServiceHealthStatus(oldData.getChecks(),newData.getChecks());
}
private boolean compareServiceInfo(Service oldServiceInfo, Service newServiceInfo) {
if (oldServiceInfo.getModifyIndex() != newServiceInfo.getModifyIndex()) {
LOGGER.info(newServiceInfo.getId() + " new_modifyIndex:"
+ newServiceInfo.getModifyIndex() + " old_modifyIndex:"
+ oldServiceInfo.getModifyIndex());
return false;
}
return true;
}
private boolean compareServiceHealthStatus(List<HealthCheck> oldData, List<HealthCheck> newData) {
boolean oldHealthCheck=ServiceFilter.getInstance().isFilterHealthCheck(oldData);
boolean newHealthCheck=ServiceFilter.getInstance().isFilterHealthCheck(newData);
return oldHealthCheck==newHealthCheck;
}
private ServiceHealth findSameIdInOldList(ServiceHealth newData) {
for (ServiceHealth oldData : lastResponse.get()) {
if (oldData.getService().getId().equals(newData.getService().getId())) {
return oldData;
}
}
return null;
}
public boolean resetModifyIndex() {
// clear last response
lastResponse.set(ImmutableList.<ServiceHealth>of());
return true;
}
}
|