1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.replication.regionserver;
20
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.ArrayList;
25 import java.util.Map;
26
27 import org.apache.hadoop.hbase.classification.InterfaceAudience;
28 import org.apache.hadoop.hbase.protobuf.generated.ClusterStatusProtos;
29 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
30 import org.apache.hadoop.hbase.util.Strings;
31
32
33
34
35 @InterfaceAudience.Private
36 public class ReplicationLoad {
37
38
39 public static final ReplicationLoad EMPTY_REPLICATIONLOAD = new ReplicationLoad();
40
41 private List<MetricsSource> sourceMetricsList;
42 private MetricsSink sinkMetrics;
43
44 private List<ClusterStatusProtos.ReplicationLoadSource> replicationLoadSourceList;
45 private ClusterStatusProtos.ReplicationLoadSink replicationLoadSink;
46
47
48 public ReplicationLoad() {
49 super();
50 }
51
52
53
54
55
56
57
58 public void buildReplicationLoad(final List<MetricsSource> srMetricsList,
59 final MetricsSink skMetrics) {
60 this.sourceMetricsList = srMetricsList;
61 this.sinkMetrics = skMetrics;
62
63
64 ClusterStatusProtos.ReplicationLoadSink.Builder rLoadSinkBuild =
65 ClusterStatusProtos.ReplicationLoadSink.newBuilder();
66 rLoadSinkBuild.setAgeOfLastAppliedOp(sinkMetrics.getAgeOfLastAppliedOp());
67 rLoadSinkBuild.setTimeStampsOfLastAppliedOp(sinkMetrics.getTimeStampOfLastAppliedOp());
68 this.replicationLoadSink = rLoadSinkBuild.build();
69
70
71 Map<String, ClusterStatusProtos.ReplicationLoadSource> replicationLoadSourceMap =
72 new HashMap<String, ClusterStatusProtos.ReplicationLoadSource>();
73 for (MetricsSource sm : this.sourceMetricsList) {
74
75 String peerId = sm.getPeerID();
76 String[] parts = peerId.split("-", 2);
77 peerId = parts.length != 1 ? parts[0] : peerId;
78
79 long ageOfLastShippedOp = sm.getAgeOfLastShippedOp();
80 int sizeOfLogQueue = sm.getSizeOfLogQueue();
81 long timeStampOfLastShippedOp = sm.getTimeStampOfLastShippedOp();
82 long replicationLag;
83 long timePassedAfterLastShippedOp =
84 EnvironmentEdgeManager.currentTime() - timeStampOfLastShippedOp;
85 if (sizeOfLogQueue != 0) {
86
87 replicationLag = Math.max(ageOfLastShippedOp, timePassedAfterLastShippedOp);
88 } else if (timePassedAfterLastShippedOp < 2 * ageOfLastShippedOp) {
89 replicationLag = ageOfLastShippedOp;
90 } else {
91
92
93 replicationLag = 0;
94 }
95
96 ClusterStatusProtos.ReplicationLoadSource rLoadSource = replicationLoadSourceMap.get(peerId);
97 if (rLoadSource != null) {
98 ageOfLastShippedOp = Math.max(rLoadSource.getAgeOfLastShippedOp(), ageOfLastShippedOp);
99 sizeOfLogQueue += rLoadSource.getSizeOfLogQueue();
100 timeStampOfLastShippedOp = Math.min(rLoadSource.getTimeStampOfLastShippedOp(),
101 timeStampOfLastShippedOp);
102 replicationLag = Math.max(rLoadSource.getReplicationLag(), replicationLag);
103 }
104
105 ClusterStatusProtos.ReplicationLoadSource.Builder rLoadSourceBuild =
106 ClusterStatusProtos.ReplicationLoadSource.newBuilder();
107 rLoadSourceBuild.setPeerID(peerId);
108 rLoadSourceBuild.setAgeOfLastShippedOp(ageOfLastShippedOp);
109 rLoadSourceBuild.setSizeOfLogQueue(sizeOfLogQueue);
110 rLoadSourceBuild.setTimeStampOfLastShippedOp(timeStampOfLastShippedOp);
111 rLoadSourceBuild.setReplicationLag(replicationLag);
112
113 replicationLoadSourceMap.put(peerId, rLoadSourceBuild.build());
114 }
115 this.replicationLoadSourceList = new ArrayList<ClusterStatusProtos.ReplicationLoadSource>(
116 replicationLoadSourceMap.values());
117 }
118
119
120
121
122
123 public String sourceToString() {
124 if (this.sourceMetricsList == null) return null;
125
126 StringBuilder sb = new StringBuilder();
127
128 for (ClusterStatusProtos.ReplicationLoadSource rls : this.replicationLoadSourceList) {
129
130 sb = Strings.appendKeyValue(sb, "\n PeerID", rls.getPeerID());
131 sb = Strings.appendKeyValue(sb, "AgeOfLastShippedOp", rls.getAgeOfLastShippedOp());
132 sb = Strings.appendKeyValue(sb, "SizeOfLogQueue", rls.getSizeOfLogQueue());
133 sb =
134 Strings.appendKeyValue(sb, "TimeStampsOfLastShippedOp",
135 (new Date(rls.getTimeStampOfLastShippedOp()).toString()));
136 sb = Strings.appendKeyValue(sb, "Replication Lag", rls.getReplicationLag());
137 }
138
139 return sb.toString();
140 }
141
142
143
144
145
146 public String sinkToString() {
147 if (this.replicationLoadSink == null) return null;
148
149 StringBuilder sb = new StringBuilder();
150 sb =
151 Strings.appendKeyValue(sb, "AgeOfLastAppliedOp",
152 this.replicationLoadSink.getAgeOfLastAppliedOp());
153 sb =
154 Strings.appendKeyValue(sb, "TimeStampsOfLastAppliedOp",
155 (new Date(this.replicationLoadSink.getTimeStampsOfLastAppliedOp()).toString()));
156
157 return sb.toString();
158 }
159
160 public ClusterStatusProtos.ReplicationLoadSink getReplicationLoadSink() {
161 return this.replicationLoadSink;
162 }
163
164 public List<ClusterStatusProtos.ReplicationLoadSource> getReplicationLoadSourceList() {
165 return this.replicationLoadSourceList;
166 }
167
168
169
170
171 @Override
172 public String toString() {
173 return this.sourceToString() + System.getProperty("line.separator") + this.sinkToString();
174 }
175
176 }