View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * This class is used for exporting some of the info from replication metrics
34   */
35  @InterfaceAudience.Private
36  public class ReplicationLoad {
37  
38    // Empty load instance.
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    /** default constructor */
48    public ReplicationLoad() {
49      super();
50    }
51  
52    /**
53     * buildReplicationLoad
54     * @param srMetricsList
55     * @param skMetrics
56     */
57  
58    public void buildReplicationLoad(final List<MetricsSource> srMetricsList,
59        final MetricsSink skMetrics) {
60      this.sourceMetricsList = srMetricsList;
61      this.sinkMetrics = skMetrics;
62  
63      // build the SinkLoad
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      // build the SourceLoad List
71      Map<String, ClusterStatusProtos.ReplicationLoadSource> replicationLoadSourceMap =
72          new HashMap<String, ClusterStatusProtos.ReplicationLoadSource>();
73      for (MetricsSource sm : this.sourceMetricsList) {
74        // Get the actual peer id
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          // err on the large side
87          replicationLag = Math.max(ageOfLastShippedOp, timePassedAfterLastShippedOp);
88        } else if (timePassedAfterLastShippedOp < 2 * ageOfLastShippedOp) {
89          replicationLag = ageOfLastShippedOp; // last shipped happen recently
90        } else {
91          // last shipped may happen last night,
92          // so NO real lag although ageOfLastShippedOp is non-zero
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    * sourceToString
121    * @return a string contains sourceReplicationLoad information
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    * sinkToString
144    * @return a string contains sinkReplicationLoad information
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    * @see java.lang.Object#toString()
170    */
171   @Override
172   public String toString() {
173     return this.sourceToString() + System.getProperty("line.separator") + this.sinkToString();
174   }
175 
176 }