View Javadoc

1   /**
2    * Copyright 2014 The Apache Software Foundation Licensed to the Apache Software Foundation (ASF)
3    * under one or more contributor license agreements. See the NOTICE file distributed with this work
4    * for additional information regarding copyright ownership. The ASF licenses this file to you under
5    * the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at
7    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in
8    * writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
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   * This class is used for exporting some of the info from replication metrics
25   */
26  @InterfaceAudience.Private
27  public class ReplicationLoad {
28  
29    // Empty load instance.
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    /** default constructor */
39    public ReplicationLoad() {
40      super();
41    }
42  
43    /**
44     * buildReplicationLoad
45     * @param srMetricsList
46     * @param skMetrics
47     */
48  
49    public void buildReplicationLoad(final List<MetricsSource> srMetricsList,
50        final MetricsSink skMetrics) {
51      this.sourceMetricsList = srMetricsList;
52      this.sinkMetrics = skMetrics;
53  
54      // build the SinkLoad
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      // build the SourceLoad List
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          // err on the large side
72          replicationLag = Math.max(ageOfLastShippedOp, timePassedAfterLastShippedOp);
73        } else if (timePassedAfterLastShippedOp < 2 * ageOfLastShippedOp) {
74          replicationLag = ageOfLastShippedOp; // last shipped happen recently
75        } else {
76          // last shipped may happen last night,
77          // so NO real lag although ageOfLastShippedOp is non-zero
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     * sourceToString
96     * @return a string contains sourceReplicationLoad information
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    * sinkToString
119    * @return a string contains sinkReplicationLoad information
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    * @see java.lang.Object#toString()
145    */
146   @Override
147   public String toString() {
148     return this.sourceToString() + System.getProperty("line.separator") + this.sinkToString();
149   }
150 
151 }