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