001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to you under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.hadoop.hbase.quotas; 018 019import java.io.IOException; 020import java.util.ArrayList; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024import java.util.Objects; 025import java.util.Map.Entry; 026 027import org.apache.hadoop.hbase.DoNotRetryIOException; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.quotas.QuotaSettingsFactory.QuotaGlobalsSettingsBypass; 030import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 031import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos; 032import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.Quotas; 033import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.SpaceQuota; 034import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.Throttle; 035import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.TimedQuota; 036import org.apache.yetus.audience.InterfaceAudience; 037 038/** 039 * Implementation of {@link GlobalQuotaSettings} to hide the Protobuf messages we use internally. 040 */ 041@InterfaceAudience.Private 042public class GlobalQuotaSettingsImpl extends GlobalQuotaSettings { 043 044 private final QuotaProtos.Throttle throttleProto; 045 private final Boolean bypassGlobals; 046 private final QuotaProtos.SpaceQuota spaceProto; 047 048 protected GlobalQuotaSettingsImpl( 049 String username, TableName tableName, String namespace, QuotaProtos.Quotas quotas) { 050 this(username, tableName, namespace, 051 (quotas != null && quotas.hasThrottle() ? quotas.getThrottle() : null), 052 (quotas != null && quotas.hasBypassGlobals() ? quotas.getBypassGlobals() : null), 053 (quotas != null && quotas.hasSpace() ? quotas.getSpace() : null)); 054 } 055 056 protected GlobalQuotaSettingsImpl( 057 String userName, TableName tableName, String namespace, QuotaProtos.Throttle throttleProto, 058 Boolean bypassGlobals, QuotaProtos.SpaceQuota spaceProto) { 059 super(userName, tableName, namespace); 060 this.throttleProto = throttleProto; 061 this.bypassGlobals = bypassGlobals; 062 this.spaceProto = spaceProto; 063 } 064 065 @Override 066 public List<QuotaSettings> getQuotaSettings() { 067 // Very similar to QuotaSettingsFactory 068 List<QuotaSettings> settings = new ArrayList<>(); 069 if (throttleProto != null) { 070 settings.addAll(QuotaSettingsFactory.fromThrottle( 071 getUserName(), getTableName(), getNamespace(), throttleProto)); 072 } 073 if (bypassGlobals != null && bypassGlobals.booleanValue()) { 074 settings.add(new QuotaGlobalsSettingsBypass( 075 getUserName(), getTableName(), getNamespace(), true)); 076 } 077 if (spaceProto != null) { 078 settings.add(QuotaSettingsFactory.fromSpace(getTableName(), getNamespace(), spaceProto)); 079 } 080 return settings; 081 } 082 083 protected QuotaProtos.Throttle getThrottleProto() { 084 return this.throttleProto; 085 } 086 087 protected Boolean getBypassGlobals() { 088 return this.bypassGlobals; 089 } 090 091 protected QuotaProtos.SpaceQuota getSpaceProto() { 092 return this.spaceProto; 093 } 094 095 /** 096 * Constructs a new {@link Quotas} message from {@code this}. 097 */ 098 protected Quotas toQuotas() { 099 QuotaProtos.Quotas.Builder builder = QuotaProtos.Quotas.newBuilder(); 100 if (getThrottleProto() != null) { 101 builder.setThrottle(getThrottleProto()); 102 } 103 if (getBypassGlobals() != null) { 104 builder.setBypassGlobals(getBypassGlobals()); 105 } 106 if (getSpaceProto() != null) { 107 builder.setSpace(getSpaceProto()); 108 } 109 return builder.build(); 110 } 111 112 @Override 113 protected GlobalQuotaSettingsImpl merge(QuotaSettings other) throws IOException { 114 // Validate the quota subject 115 validateQuotaTarget(other); 116 117 // Propagate the Throttle 118 QuotaProtos.Throttle.Builder throttleBuilder = (throttleProto == null 119 ? null : throttleProto.toBuilder()); 120 if (other instanceof ThrottleSettings) { 121 if (throttleBuilder == null) { 122 throttleBuilder = QuotaProtos.Throttle.newBuilder(); 123 } 124 ThrottleSettings otherThrottle = (ThrottleSettings) other; 125 126 if (otherThrottle.proto.hasType()) { 127 QuotaProtos.ThrottleRequest otherProto = otherThrottle.proto; 128 if (otherProto.hasTimedQuota()) { 129 if (otherProto.hasTimedQuota()) { 130 validateTimedQuota(otherProto.getTimedQuota()); 131 } 132 133 switch (otherProto.getType()) { 134 case REQUEST_NUMBER: 135 if (otherProto.hasTimedQuota()) { 136 throttleBuilder.setReqNum(otherProto.getTimedQuota()); 137 } else { 138 throttleBuilder.clearReqNum(); 139 } 140 break; 141 case REQUEST_SIZE: 142 if (otherProto.hasTimedQuota()) { 143 throttleBuilder.setReqSize(otherProto.getTimedQuota()); 144 } else { 145 throttleBuilder.clearReqSize(); 146 } 147 break; 148 case WRITE_NUMBER: 149 if (otherProto.hasTimedQuota()) { 150 throttleBuilder.setWriteNum(otherProto.getTimedQuota()); 151 } else { 152 throttleBuilder.clearWriteNum(); 153 } 154 break; 155 case WRITE_SIZE: 156 if (otherProto.hasTimedQuota()) { 157 throttleBuilder.setWriteSize(otherProto.getTimedQuota()); 158 } else { 159 throttleBuilder.clearWriteSize(); 160 } 161 break; 162 case READ_NUMBER: 163 if (otherProto.hasTimedQuota()) { 164 throttleBuilder.setReadNum(otherProto.getTimedQuota()); 165 } else { 166 throttleBuilder.clearReqNum(); 167 } 168 break; 169 case READ_SIZE: 170 if (otherProto.hasTimedQuota()) { 171 throttleBuilder.setReadSize(otherProto.getTimedQuota()); 172 } else { 173 throttleBuilder.clearReadSize(); 174 } 175 break; 176 } 177 } else { 178 clearThrottleBuilder(throttleBuilder); 179 } 180 } else { 181 clearThrottleBuilder(throttleBuilder); 182 } 183 } 184 185 // Propagate the space quota portion 186 QuotaProtos.SpaceQuota.Builder spaceBuilder = (spaceProto == null 187 ? null : spaceProto.toBuilder()); 188 if (other instanceof SpaceLimitSettings) { 189 if (spaceBuilder == null) { 190 spaceBuilder = QuotaProtos.SpaceQuota.newBuilder(); 191 } 192 SpaceLimitSettings settingsToMerge = (SpaceLimitSettings) other; 193 194 QuotaProtos.SpaceLimitRequest spaceRequest = settingsToMerge.getProto(); 195 196 // The message contained the expect SpaceQuota object 197 if (spaceRequest.hasQuota()) { 198 SpaceQuota quotaToMerge = spaceRequest.getQuota(); 199 // Validate that the two settings are for the same target. 200 // SpaceQuotas either apply to a table or a namespace (no user spacequota). 201 if (!Objects.equals(getTableName(), settingsToMerge.getTableName()) 202 && !Objects.equals(getNamespace(), settingsToMerge.getNamespace())) { 203 throw new IllegalArgumentException( 204 "Cannot merge " + settingsToMerge + " into " + this); 205 } 206 207 if (quotaToMerge.getRemove()) { 208 // Update the builder to propagate the removal 209 spaceBuilder.setRemove(true).clearSoftLimit().clearViolationPolicy(); 210 } else { 211 // Add the new settings to the existing settings 212 spaceBuilder.mergeFrom(quotaToMerge); 213 } 214 } 215 } 216 217 Boolean bypassGlobals = this.bypassGlobals; 218 if (other instanceof QuotaGlobalsSettingsBypass) { 219 bypassGlobals = ((QuotaGlobalsSettingsBypass) other).getBypass(); 220 } 221 222 if (throttleBuilder == null && 223 (spaceBuilder == null || (spaceBuilder.hasRemove() && spaceBuilder.getRemove())) 224 && bypassGlobals == null) { 225 return null; 226 } 227 228 return new GlobalQuotaSettingsImpl( 229 getUserName(), getTableName(), getNamespace(), 230 (throttleBuilder == null ? null : throttleBuilder.build()), bypassGlobals, 231 (spaceBuilder == null ? null : spaceBuilder.build())); 232 } 233 234 private void validateTimedQuota(final TimedQuota timedQuota) throws IOException { 235 if (timedQuota.getSoftLimit() < 1) { 236 throw new DoNotRetryIOException(new UnsupportedOperationException( 237 "The throttle limit must be greater then 0, got " + timedQuota.getSoftLimit())); 238 } 239 } 240 241 @Override 242 public String toString() { 243 StringBuilder builder = new StringBuilder(); 244 builder.append("GlobalQuota: "); 245 if (throttleProto != null) { 246 Map<ThrottleType,TimedQuota> throttleQuotas = buildThrottleQuotas(throttleProto); 247 builder.append(" { TYPE => THROTTLE "); 248 for (Entry<ThrottleType,TimedQuota> entry : throttleQuotas.entrySet()) { 249 final ThrottleType type = entry.getKey(); 250 final TimedQuota timedQuota = entry.getValue(); 251 builder.append("{THROTTLE_TYPE => ").append(type.name()).append(", LIMIT => "); 252 if (timedQuota.hasSoftLimit()) { 253 switch (type) { 254 case REQUEST_NUMBER: 255 case WRITE_NUMBER: 256 case READ_NUMBER: 257 builder.append(String.format("%dreq", timedQuota.getSoftLimit())); 258 break; 259 case REQUEST_SIZE: 260 case WRITE_SIZE: 261 case READ_SIZE: 262 builder.append(sizeToString(timedQuota.getSoftLimit())); 263 break; 264 } 265 } else if (timedQuota.hasShare()) { 266 builder.append(String.format("%.2f%%", timedQuota.getShare())); 267 } 268 builder.append('/'); 269 builder.append(timeToString(ProtobufUtil.toTimeUnit(timedQuota.getTimeUnit()))); 270 if (timedQuota.hasScope()) { 271 builder.append(", SCOPE => "); 272 builder.append(timedQuota.getScope().toString()); 273 } 274 } 275 builder.append( "} } "); 276 } else { 277 builder.append(" {} "); 278 } 279 if (bypassGlobals != null) { 280 builder.append(" { GLOBAL_BYPASS => " + bypassGlobals + " } "); 281 } 282 if (spaceProto != null) { 283 builder.append(" { TYPE => SPACE"); 284 if (getTableName() != null) { 285 builder.append(", TABLE => ").append(getTableName()); 286 } 287 if (getNamespace() != null) { 288 builder.append(", NAMESPACE => ").append(getNamespace()); 289 } 290 if (spaceProto.getRemove()) { 291 builder.append(", REMOVE => ").append(spaceProto.getRemove()); 292 } else { 293 builder.append(", LIMIT => ").append(spaceProto.getSoftLimit()); 294 builder.append(", VIOLATION_POLICY => ").append(spaceProto.getViolationPolicy()); 295 } 296 builder.append(" } "); 297 } 298 return builder.toString(); 299 } 300 301 private Map<ThrottleType,TimedQuota> buildThrottleQuotas(Throttle proto) { 302 HashMap<ThrottleType,TimedQuota> quotas = new HashMap<>(); 303 if (proto.hasReadNum()) { 304 quotas.put(ThrottleType.READ_NUMBER, proto.getReadNum()); 305 } 306 if (proto.hasReadSize()) { 307 quotas.put(ThrottleType.READ_SIZE, proto.getReadSize()); 308 } 309 if (proto.hasReqNum()) { 310 quotas.put(ThrottleType.REQUEST_NUMBER, proto.getReqNum()); 311 } 312 if (proto.hasReqSize()) { 313 quotas.put(ThrottleType.REQUEST_SIZE, proto.getReqSize()); 314 } 315 if (proto.hasWriteNum()) { 316 quotas.put(ThrottleType.WRITE_NUMBER, proto.getWriteNum()); 317 } 318 if (proto.hasWriteSize()) { 319 quotas.put(ThrottleType.WRITE_SIZE, proto.getWriteSize()); 320 } 321 return quotas; 322 } 323 324 private void clearThrottleBuilder(QuotaProtos.Throttle.Builder builder) { 325 builder.clearReadNum(); 326 builder.clearReadSize(); 327 builder.clearReqNum(); 328 builder.clearReqSize(); 329 builder.clearWriteNum(); 330 builder.clearWriteSize(); 331 } 332}