001/**
002 * Copyright The Apache Software Foundation
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package org.apache.hadoop.hbase;
022
023import java.util.HashMap;
024import java.util.Map;
025
026import org.apache.hadoop.hbase.util.Strings;
027import org.apache.yetus.audience.InterfaceAudience;
028
029import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
030
031import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos;
032
033@InterfaceAudience.Private
034public final class UserMetricsBuilder {
035
036  public static UserMetrics toUserMetrics(ClusterStatusProtos.UserLoad userLoad) {
037    UserMetricsBuilder builder = UserMetricsBuilder.newBuilder(userLoad.getUserName().getBytes());
038    userLoad.getClientMetricsList().stream().map(
039      clientMetrics -> new ClientMetricsImpl(clientMetrics.getHostName(),
040            clientMetrics.getReadRequestsCount(), clientMetrics.getWriteRequestsCount(),
041            clientMetrics.getFilteredRequestsCount())).forEach(builder::addClientMetris);
042    return builder.build();
043  }
044
045  public static ClusterStatusProtos.UserLoad toUserMetrics(UserMetrics userMetrics) {
046    ClusterStatusProtos.UserLoad.Builder builder =
047        ClusterStatusProtos.UserLoad.newBuilder().setUserName(userMetrics.getNameAsString());
048    userMetrics.getClientMetrics().values().stream().map(
049      clientMetrics -> ClusterStatusProtos.ClientMetrics.newBuilder()
050            .setHostName(clientMetrics.getHostName())
051            .setWriteRequestsCount(clientMetrics.getWriteRequestsCount())
052            .setReadRequestsCount(clientMetrics.getReadRequestsCount())
053            .setFilteredRequestsCount(clientMetrics.getFilteredReadRequestsCount()).build())
054        .forEach(builder::addClientMetrics);
055    return builder.build();
056  }
057
058  public static UserMetricsBuilder newBuilder(byte[] name) {
059    return new UserMetricsBuilder(name);
060  }
061
062
063  private final byte[] name;
064  private Map<String, UserMetrics.ClientMetrics> clientMetricsMap = new HashMap<>();
065  private UserMetricsBuilder(byte[] name) {
066    this.name = name;
067  }
068
069  public UserMetricsBuilder addClientMetris(UserMetrics.ClientMetrics clientMetrics) {
070    clientMetricsMap.put(clientMetrics.getHostName(), clientMetrics);
071    return this;
072  }
073
074  public UserMetrics build() {
075    return new UserMetricsImpl(name, clientMetricsMap);
076  }
077
078  public static class ClientMetricsImpl implements UserMetrics.ClientMetrics {
079    private final long filteredReadRequestsCount;
080    private final String hostName;
081    private final long readRequestCount;
082    private final long writeRequestCount;
083
084    public ClientMetricsImpl(String hostName, long readRequest, long writeRequest,
085        long filteredReadRequestsCount) {
086      this.hostName = hostName;
087      this.readRequestCount = readRequest;
088      this.writeRequestCount = writeRequest;
089      this.filteredReadRequestsCount = filteredReadRequestsCount;
090    }
091
092    @Override public String getHostName() {
093      return hostName;
094    }
095
096    @Override public long getReadRequestsCount() {
097      return readRequestCount;
098    }
099
100    @Override public long getWriteRequestsCount() {
101      return writeRequestCount;
102    }
103
104    @Override public long getFilteredReadRequestsCount() {
105      return filteredReadRequestsCount;
106    }
107  }
108
109  private static class UserMetricsImpl implements UserMetrics {
110    private final byte[] name;
111    private final Map<String, ClientMetrics> clientMetricsMap;
112
113    UserMetricsImpl(byte[] name, Map<String, ClientMetrics> clientMetricsMap) {
114      this.name = Preconditions.checkNotNull(name);
115      this.clientMetricsMap = clientMetricsMap;
116    }
117
118    @Override public byte[] getUserName() {
119      return name;
120    }
121
122    @Override public long getReadRequestCount() {
123      return clientMetricsMap.values().stream().map(c -> c.getReadRequestsCount())
124          .reduce(0L, Long::sum);
125    }
126
127    @Override public long getWriteRequestCount() {
128      return clientMetricsMap.values().stream().map(c -> c.getWriteRequestsCount())
129          .reduce(0L, Long::sum);
130    }
131
132    @Override public Map<String, ClientMetrics> getClientMetrics() {
133      return this.clientMetricsMap;
134    }
135
136    @Override public long getFilteredReadRequests() {
137      return clientMetricsMap.values().stream().map(c -> c.getFilteredReadRequestsCount())
138          .reduce(0L, Long::sum);
139    }
140
141    @Override
142    public String toString() {
143      StringBuilder sb = Strings
144          .appendKeyValue(new StringBuilder(), "readRequestCount", this.getReadRequestCount());
145      Strings.appendKeyValue(sb, "writeRequestCount", this.getWriteRequestCount());
146      Strings.appendKeyValue(sb, "filteredReadRequestCount", this.getFilteredReadRequests());
147      return sb.toString();
148    }
149  }
150
151}