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.List;
23  import java.util.ArrayList;
24  
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.protobuf.generated.ClusterStatusProtos;
27  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
28  import org.apache.hadoop.hbase.util.Strings;
29  
30  /**
31   * This class is used for exporting some of the info from replication metrics
32   */
33  @InterfaceAudience.Private
34  public class ReplicationLoad {
35  
36    // Empty load instance.
37    public static final ReplicationLoad EMPTY_REPLICATIONLOAD = new ReplicationLoad();
38  
39    private List<MetricsSource> sourceMetricsList;
40    private MetricsSink sinkMetrics;
41  
42    private List<ClusterStatusProtos.ReplicationLoadSource> replicationLoadSourceList;
43    private ClusterStatusProtos.ReplicationLoadSink replicationLoadSink;
44  
45    /** default constructor */
46    public ReplicationLoad() {
47      super();
48    }
49  
50    /**
51     * buildReplicationLoad
52     * @param srMetricsList
53     * @param skMetrics
54     */
55  
56    public void buildReplicationLoad(final List<MetricsSource> srMetricsList,
57        final MetricsSink skMetrics) {
58      this.sourceMetricsList = srMetricsList;
59      this.sinkMetrics = skMetrics;
60  
61      // build the SinkLoad
62      ClusterStatusProtos.ReplicationLoadSink.Builder rLoadSinkBuild =
63          ClusterStatusProtos.ReplicationLoadSink.newBuilder();
64      rLoadSinkBuild.setAgeOfLastAppliedOp(sinkMetrics.getAgeOfLastAppliedOp());
65      rLoadSinkBuild.setTimeStampsOfLastAppliedOp(sinkMetrics.getTimeStampOfLastAppliedOp());
66      this.replicationLoadSink = rLoadSinkBuild.build();
67  
68      // build the SourceLoad List
69      this.replicationLoadSourceList = new ArrayList<ClusterStatusProtos.ReplicationLoadSource>();
70      for (MetricsSource sm : this.sourceMetricsList) {
71        long ageOfLastShippedOp = sm.getAgeOfLastShippedOp();
72        int sizeOfLogQueue = sm.getSizeOfLogQueue();
73        long timeStampOfLastShippedOp = sm.getTimeStampOfLastShippedOp();
74        long replicationLag;
75        long timePassedAfterLastShippedOp =
76            EnvironmentEdgeManager.currentTime() - timeStampOfLastShippedOp;
77        if (sizeOfLogQueue != 0) {
78          // err on the large side
79          replicationLag = Math.max(ageOfLastShippedOp, timePassedAfterLastShippedOp);
80        } else if (timePassedAfterLastShippedOp < 2 * ageOfLastShippedOp) {
81          replicationLag = ageOfLastShippedOp; // last shipped happen recently
82        } else {
83          // last shipped may happen last night,
84          // so NO real lag although ageOfLastShippedOp is non-zero
85          replicationLag = 0;
86        }
87  
88        ClusterStatusProtos.ReplicationLoadSource.Builder rLoadSourceBuild =
89            ClusterStatusProtos.ReplicationLoadSource.newBuilder();
90        rLoadSourceBuild.setPeerID(sm.getPeerID());
91        rLoadSourceBuild.setAgeOfLastShippedOp(ageOfLastShippedOp);
92        rLoadSourceBuild.setSizeOfLogQueue(sizeOfLogQueue);
93        rLoadSourceBuild.setTimeStampOfLastShippedOp(timeStampOfLastShippedOp);
94        rLoadSourceBuild.setReplicationLag(replicationLag);
95  
96        this.replicationLoadSourceList.add(rLoadSourceBuild.build());
97      }
98  
99    }
100 
101   /**
102    * sourceToString
103    * @return a string contains sourceReplicationLoad information
104    */
105   public String sourceToString() {
106     if (this.sourceMetricsList == null) return null;
107 
108     StringBuilder sb = new StringBuilder();
109 
110     for (ClusterStatusProtos.ReplicationLoadSource rls : this.replicationLoadSourceList) {
111 
112       sb = Strings.appendKeyValue(sb, "\n           PeerID", rls.getPeerID());
113       sb = Strings.appendKeyValue(sb, "AgeOfLastShippedOp", rls.getAgeOfLastShippedOp());
114       sb = Strings.appendKeyValue(sb, "SizeOfLogQueue", rls.getSizeOfLogQueue());
115       sb =
116           Strings.appendKeyValue(sb, "TimeStampsOfLastShippedOp",
117             (new Date(rls.getTimeStampOfLastShippedOp()).toString()));
118       sb = Strings.appendKeyValue(sb, "Replication Lag", rls.getReplicationLag());
119     }
120 
121     return sb.toString();
122   }
123 
124   /**
125    * sinkToString
126    * @return a string contains sinkReplicationLoad information
127    */
128   public String sinkToString() {
129     if (this.replicationLoadSink == null) return null;
130 
131     StringBuilder sb = new StringBuilder();
132     sb =
133         Strings.appendKeyValue(sb, "AgeOfLastAppliedOp",
134           this.replicationLoadSink.getAgeOfLastAppliedOp());
135     sb =
136         Strings.appendKeyValue(sb, "TimeStampsOfLastAppliedOp",
137           (new Date(this.replicationLoadSink.getTimeStampsOfLastAppliedOp()).toString()));
138 
139     return sb.toString();
140   }
141 
142   public ClusterStatusProtos.ReplicationLoadSink getReplicationLoadSink() {
143     return this.replicationLoadSink;
144   }
145 
146   public List<ClusterStatusProtos.ReplicationLoadSource> getReplicationLoadSourceList() {
147     return this.replicationLoadSourceList;
148   }
149 
150   /**
151    * @see java.lang.Object#toString()
152    */
153   @Override
154   public String toString() {
155     return this.sourceToString() + System.getProperty("line.separator") + this.sinkToString();
156   }
157 
158 }