use of org.apache.hadoop.hbase.quotas.QuotaSettingsFactory.QuotaGlobalsSettingsBypass in project hbase by apache.
the class QuotaSettings method buildFromProto.
/**
* Converts the protocol buffer request into a QuotaSetting POJO. Arbitrarily
* enforces that the request only contain one "limit", despite the message
* allowing multiple. The public API does not allow such use of the message.
*
* @param request The protocol buffer request.
* @return A {@link QuotaSettings} POJO.
*/
@InterfaceAudience.Private
public static QuotaSettings buildFromProto(SetQuotaRequest request) {
String username = null;
if (request.hasUserName()) {
username = request.getUserName();
}
TableName tableName = null;
if (request.hasTableName()) {
tableName = ProtobufUtil.toTableName(request.getTableName());
}
String namespace = null;
if (request.hasNamespace()) {
namespace = request.getNamespace();
}
String regionServer = null;
if (request.hasRegionServer()) {
regionServer = request.getRegionServer();
}
if (request.hasBypassGlobals()) {
// Make sure we don't have either of the two below limits also included
if (request.hasSpaceLimit() || request.hasThrottle()) {
throw new IllegalStateException("SetQuotaRequest has multiple limits: " + TextFormat.shortDebugString(request));
}
return new QuotaGlobalsSettingsBypass(username, tableName, namespace, regionServer, request.getBypassGlobals());
} else if (request.hasSpaceLimit()) {
// Make sure we don't have the below limit as well
if (request.hasThrottle()) {
throw new IllegalStateException("SetQuotaRequests has multiple limits: " + TextFormat.shortDebugString(request));
}
// Sanity check on the pb received.
if (!request.getSpaceLimit().hasQuota()) {
throw new IllegalArgumentException("SpaceLimitRequest is missing the expected SpaceQuota.");
}
return QuotaSettingsFactory.fromSpace(tableName, namespace, request.getSpaceLimit().getQuota());
} else if (request.hasThrottle()) {
return new ThrottleSettings(username, tableName, namespace, regionServer, request.getThrottle());
} else {
throw new IllegalStateException("Unhandled SetRequestRequest state");
}
}
use of org.apache.hadoop.hbase.quotas.QuotaSettingsFactory.QuotaGlobalsSettingsBypass in project hbase by apache.
the class GlobalQuotaSettingsImpl method merge.
@Override
protected GlobalQuotaSettingsImpl merge(QuotaSettings other) throws IOException {
// Validate the quota subject
validateQuotaTarget(other);
// Propagate the Throttle
QuotaProtos.Throttle.Builder throttleBuilder = throttleProto == null ? null : throttleProto.toBuilder();
if (other instanceof ThrottleSettings) {
ThrottleSettings otherThrottle = (ThrottleSettings) other;
if (!otherThrottle.proto.hasType() || !otherThrottle.proto.hasTimedQuota()) {
// It means it's a remove request
// To prevent the "empty" row in QuotaTableUtil.QUOTA_TABLE_NAME
QuotaProtos.ThrottleRequest otherProto = otherThrottle.proto;
if (throttleBuilder != null && !otherThrottle.proto.hasTimedQuota() && otherThrottle.proto.hasType()) {
switch(otherProto.getType()) {
case REQUEST_NUMBER:
throttleBuilder.clearReqNum();
break;
case REQUEST_SIZE:
throttleBuilder.clearReqSize();
break;
case WRITE_NUMBER:
throttleBuilder.clearWriteNum();
break;
case WRITE_SIZE:
throttleBuilder.clearWriteSize();
break;
case READ_NUMBER:
throttleBuilder.clearReadNum();
break;
case READ_SIZE:
throttleBuilder.clearReadSize();
break;
case REQUEST_CAPACITY_UNIT:
throttleBuilder.clearReqCapacityUnit();
break;
case READ_CAPACITY_UNIT:
throttleBuilder.clearReadCapacityUnit();
break;
case WRITE_CAPACITY_UNIT:
throttleBuilder.clearWriteCapacityUnit();
break;
default:
}
boolean hasThrottle = false;
for (QuotaProtos.ThrottleType quotaType : QuotaProtos.ThrottleType.values()) {
hasThrottle = hasThrottle(quotaType, throttleBuilder);
if (hasThrottle) {
break;
}
}
if (!hasThrottle) {
throttleBuilder = null;
}
} else {
throttleBuilder = null;
}
} else {
QuotaProtos.ThrottleRequest otherProto = otherThrottle.proto;
validateTimedQuota(otherProto.getTimedQuota());
if (throttleBuilder == null) {
throttleBuilder = QuotaProtos.Throttle.newBuilder();
}
switch(otherProto.getType()) {
case REQUEST_NUMBER:
throttleBuilder.setReqNum(otherProto.getTimedQuota());
break;
case REQUEST_SIZE:
throttleBuilder.setReqSize(otherProto.getTimedQuota());
break;
case WRITE_NUMBER:
throttleBuilder.setWriteNum(otherProto.getTimedQuota());
break;
case WRITE_SIZE:
throttleBuilder.setWriteSize(otherProto.getTimedQuota());
break;
case READ_NUMBER:
throttleBuilder.setReadNum(otherProto.getTimedQuota());
break;
case READ_SIZE:
throttleBuilder.setReadSize(otherProto.getTimedQuota());
break;
case REQUEST_CAPACITY_UNIT:
throttleBuilder.setReqCapacityUnit(otherProto.getTimedQuota());
break;
case READ_CAPACITY_UNIT:
throttleBuilder.setReadCapacityUnit(otherProto.getTimedQuota());
break;
case WRITE_CAPACITY_UNIT:
throttleBuilder.setWriteCapacityUnit(otherProto.getTimedQuota());
break;
default:
}
}
}
// Propagate the space quota portion
QuotaProtos.SpaceQuota.Builder spaceBuilder = (spaceProto == null ? null : spaceProto.toBuilder());
if (other instanceof SpaceLimitSettings) {
if (spaceBuilder == null) {
spaceBuilder = QuotaProtos.SpaceQuota.newBuilder();
}
SpaceLimitSettings settingsToMerge = (SpaceLimitSettings) other;
QuotaProtos.SpaceLimitRequest spaceRequest = settingsToMerge.getProto();
// The message contained the expect SpaceQuota object
if (spaceRequest.hasQuota()) {
SpaceQuota quotaToMerge = spaceRequest.getQuota();
// SpaceQuotas either apply to a table or a namespace (no user spacequota).
if (!Objects.equals(getTableName(), settingsToMerge.getTableName()) && !Objects.equals(getNamespace(), settingsToMerge.getNamespace())) {
throw new IllegalArgumentException("Cannot merge " + settingsToMerge + " into " + this);
}
if (quotaToMerge.getRemove()) {
// It means it's a remove request
// Update the builder to propagate the removal
spaceBuilder.setRemove(true).clearSoftLimit().clearViolationPolicy();
} else {
// Add the new settings to the existing settings
spaceBuilder.mergeFrom(quotaToMerge);
}
}
}
boolean removeSpaceBuilder = (spaceBuilder == null) || (spaceBuilder.hasRemove() && spaceBuilder.getRemove());
Boolean bypassGlobals = this.bypassGlobals;
if (other instanceof QuotaGlobalsSettingsBypass) {
bypassGlobals = ((QuotaGlobalsSettingsBypass) other).getBypass();
}
if (throttleBuilder == null && removeSpaceBuilder && bypassGlobals == null) {
return null;
}
return new GlobalQuotaSettingsImpl(getUserName(), getTableName(), getNamespace(), getRegionServer(), (throttleBuilder == null ? null : throttleBuilder.build()), bypassGlobals, (removeSpaceBuilder ? null : spaceBuilder.build()));
}
use of org.apache.hadoop.hbase.quotas.QuotaSettingsFactory.QuotaGlobalsSettingsBypass in project hbase by apache.
the class TestQuotaGlobalsSettingsBypass method testMerge.
@Test
public void testMerge() throws IOException {
QuotaGlobalsSettingsBypass orig = new QuotaGlobalsSettingsBypass("joe", null, null, null, true);
assertFalse(orig.merge(new QuotaGlobalsSettingsBypass("joe", null, null, null, false)).getBypass());
}
use of org.apache.hadoop.hbase.quotas.QuotaSettingsFactory.QuotaGlobalsSettingsBypass in project hbase by apache.
the class TestQuotaGlobalsSettingsBypass method testInvalidMerges.
@Test
public void testInvalidMerges() throws IOException {
QuotaGlobalsSettingsBypass userBypass = new QuotaGlobalsSettingsBypass("joe", null, null, null, true);
QuotaGlobalsSettingsBypass tableBypass = new QuotaGlobalsSettingsBypass(null, TableName.valueOf("table"), null, null, true);
QuotaGlobalsSettingsBypass namespaceBypass = new QuotaGlobalsSettingsBypass(null, null, "ns", null, true);
QuotaGlobalsSettingsBypass regionServerBypass = new QuotaGlobalsSettingsBypass(null, null, null, "all", true);
QuotaGlobalsSettingsBypass userOnTableBypass = new QuotaGlobalsSettingsBypass("joe", TableName.valueOf("table"), null, null, true);
QuotaGlobalsSettingsBypass userOnNamespaceBypass = new QuotaGlobalsSettingsBypass("joe", null, "ns", null, true);
QuotaGlobalsSettingsBypass userOnRegionServerBypass = new QuotaGlobalsSettingsBypass("joe", null, null, "all", true);
assertTrue(userBypass.merge(userBypass).getBypass());
expectFailure(userBypass, new QuotaGlobalsSettingsBypass("frank", null, null, null, false));
expectFailure(userBypass, tableBypass);
expectFailure(userBypass, namespaceBypass);
expectFailure(userBypass, regionServerBypass);
expectFailure(userBypass, userOnTableBypass);
expectFailure(userBypass, userOnNamespaceBypass);
expectFailure(userBypass, userOnRegionServerBypass);
assertTrue(tableBypass.merge(tableBypass).getBypass());
expectFailure(tableBypass, userBypass);
expectFailure(tableBypass, new QuotaGlobalsSettingsBypass(null, TableName.valueOf("foo"), null, null, false));
expectFailure(tableBypass, namespaceBypass);
expectFailure(tableBypass, regionServerBypass);
expectFailure(tableBypass, userOnTableBypass);
expectFailure(tableBypass, userOnNamespaceBypass);
expectFailure(tableBypass, userOnRegionServerBypass);
assertTrue(namespaceBypass.merge(namespaceBypass).getBypass());
expectFailure(namespaceBypass, userBypass);
expectFailure(namespaceBypass, tableBypass);
expectFailure(namespaceBypass, regionServerBypass);
expectFailure(namespaceBypass, new QuotaGlobalsSettingsBypass(null, null, "sn", null, false));
expectFailure(namespaceBypass, userOnTableBypass);
expectFailure(namespaceBypass, userOnNamespaceBypass);
expectFailure(namespaceBypass, userOnNamespaceBypass);
assertTrue(regionServerBypass.merge(regionServerBypass).getBypass());
expectFailure(regionServerBypass, userBypass);
expectFailure(regionServerBypass, tableBypass);
expectFailure(regionServerBypass, namespaceBypass);
expectFailure(regionServerBypass, new QuotaGlobalsSettingsBypass(null, null, null, "rs", false));
expectFailure(regionServerBypass, userOnTableBypass);
expectFailure(regionServerBypass, userOnNamespaceBypass);
expectFailure(regionServerBypass, userOnRegionServerBypass);
assertTrue(userOnTableBypass.merge(userOnTableBypass).getBypass());
expectFailure(userOnTableBypass, userBypass);
expectFailure(userOnTableBypass, tableBypass);
expectFailure(userOnTableBypass, namespaceBypass);
expectFailure(userOnTableBypass, regionServerBypass);
// Incorrect user
expectFailure(userOnTableBypass, new QuotaGlobalsSettingsBypass("frank", TableName.valueOf("foo"), null, null, false));
// Incorrect tablename
expectFailure(userOnTableBypass, new QuotaGlobalsSettingsBypass("joe", TableName.valueOf("bar"), null, null, false));
expectFailure(userOnTableBypass, userOnNamespaceBypass);
expectFailure(userOnTableBypass, userOnRegionServerBypass);
assertTrue(userOnNamespaceBypass.merge(userOnNamespaceBypass).getBypass());
expectFailure(userOnNamespaceBypass, userBypass);
expectFailure(userOnNamespaceBypass, tableBypass);
expectFailure(userOnNamespaceBypass, namespaceBypass);
expectFailure(userOnNamespaceBypass, regionServerBypass);
expectFailure(userOnNamespaceBypass, userOnTableBypass);
expectFailure(userOnNamespaceBypass, new QuotaGlobalsSettingsBypass("frank", null, "ns", null, false));
expectFailure(userOnNamespaceBypass, new QuotaGlobalsSettingsBypass("joe", null, "sn", null, false));
expectFailure(userOnNamespaceBypass, userOnRegionServerBypass);
assertTrue(userOnRegionServerBypass.merge(userOnRegionServerBypass).getBypass());
expectFailure(userOnRegionServerBypass, userBypass);
expectFailure(userOnRegionServerBypass, tableBypass);
expectFailure(userOnRegionServerBypass, namespaceBypass);
expectFailure(userOnRegionServerBypass, regionServerBypass);
expectFailure(userOnRegionServerBypass, userOnTableBypass);
expectFailure(userOnRegionServerBypass, userOnNamespaceBypass);
expectFailure(userOnRegionServerBypass, new QuotaGlobalsSettingsBypass("frank", null, null, "all", false));
expectFailure(userOnRegionServerBypass, new QuotaGlobalsSettingsBypass("joe", null, null, "rs", false));
}
Aggregations