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