@Override public int hashCode() { return Objects.hash(getTableName(), getNamespace(), proto); }
@Override protected QuotaSettings merge(QuotaSettings newSettings) { if (newSettings instanceof SpaceLimitSettings) { SpaceLimitSettings settingsToMerge = (SpaceLimitSettings) newSettings; // The message contained the expect SpaceQuota object if (settingsToMerge.proto.hasQuota()) { SpaceQuota quotaToMerge = settingsToMerge.proto.getQuota(); if (quotaToMerge.getRemove()) { return settingsToMerge; } else { // Validate that the two settings are for the same target. // 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 " + newSettings + " into " + this); } // Create a builder from the old settings SpaceQuota.Builder mergedBuilder = this.proto.getQuota().toBuilder(); // Build a new SpaceQuotas object from merging in the new settings return new SpaceLimitSettings( getTableName(), getNamespace(), buildProtoFromQuota(mergedBuilder.mergeFrom(quotaToMerge).build())); } } // else, we don't know what to do, so return the original object } return this; }
SpaceLimitSettings(TableName tableName, long sizeLimit, SpaceViolationPolicy violationPolicy) { super(null, Objects.requireNonNull(tableName), null, null); validateSizeLimit(sizeLimit); proto = buildProtoAddQuota(sizeLimit, Objects.requireNonNull(violationPolicy)); }
static QuotaSettings fromSpace(TableName table, String namespace, SpaceQuota protoQuota) { if (protoQuota == null) { return null; } if ((table == null && namespace == null) || (table != null && namespace != null)) { throw new IllegalArgumentException( "Can only construct SpaceLimitSettings for a table or namespace."); } if (table != null) { if (protoQuota.getRemove()) { return new SpaceLimitSettings(table); } return SpaceLimitSettings.fromSpaceQuota(table, protoQuota); } else { if (protoQuota.getRemove()) { return new SpaceLimitSettings(namespace); } // namespace must be non-null return SpaceLimitSettings.fromSpaceQuota(namespace, protoQuota); } }
assertFalse("Should not have multiple space quotas", seenSpace); SpaceLimitSettings spaceLimit = (SpaceLimitSettings) setting; assertEquals(tn, spaceLimit.getTableName()); assertNull("Username should be null", spaceLimit.getUserName()); assertNull("Namespace should be null", spaceLimit.getNamespace()); assertNull("RegionServer should be null", spaceLimit.getRegionServer()); assertTrue("SpaceLimitSettings should have a SpaceQuota", spaceLimit.getProto().hasQuota()); assertEquals(spaceQuota, spaceLimit.getProto().getQuota()); seenSpace = true; } else {
/** * Creates a {@link QuotaSettings} object to remove the FileSystem space quota for the given * namespace. * * @param namespace The namespace to remove the quota on. * @return A {@link QuotaSettings} object. */ public static QuotaSettings removeNamespaceSpaceLimit(String namespace) { return new SpaceLimitSettings(namespace); } }
QuotaProtos.SpaceLimitRequest spaceRequest = settingsToMerge.getProto(); if (!Objects.equals(getTableName(), settingsToMerge.getTableName()) && !Objects.equals(getNamespace(), settingsToMerge.getNamespace())) { throw new IllegalArgumentException( "Cannot merge " + settingsToMerge + " into " + this);
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("TYPE => SPACE"); if (getTableName() != null) { sb.append(", TABLE => ").append(getTableName()); } if (getNamespace() != null) { sb.append(", NAMESPACE => ").append(getNamespace()); } if (proto.getQuota().getRemove()) { sb.append(", REMOVE => ").append(proto.getQuota().getRemove()); } else { sb.append(", LIMIT => ").append(sizeToString(proto.getQuota().getSoftLimit())); sb.append(", VIOLATION_POLICY => ").append(proto.getQuota().getViolationPolicy()); } return sb.toString(); }
/** * Constructs a {@link SpaceLimitSettings} from the provided protobuf message and tablename. * * @param tableName The target tablename for the limit. * @param proto The protobuf representation. * @return A QuotaSettings. */ static SpaceLimitSettings fromSpaceQuota( final TableName tableName, final QuotaProtos.SpaceQuota proto) { validateProtoArguments(proto); return new SpaceLimitSettings(tableName, proto.getSoftLimit(), ProtobufUtil.toViolationPolicy(proto.getViolationPolicy())); }
@Test public void testTableQuota() { final TableName tableName = TableName.valueOf("foo"); final long sizeLimit = 1024 * 1024; final SpaceViolationPolicy policy = SpaceViolationPolicy.NO_WRITES; SpaceLimitSettings settings = new SpaceLimitSettings(tableName, sizeLimit, policy); SetQuotaRequest proto = QuotaSettings.buildSetQuotaRequestProto(settings); assertFalse("User should be missing", proto.hasUserName()); assertFalse("Namespace should be missing", proto.hasNamespace()); assertEquals(ProtobufUtil.toProtoTableName(tableName), proto.getTableName()); SpaceLimitRequest spaceLimitReq = proto.getSpaceLimit(); assertNotNull("SpaceLimitRequest was null", spaceLimitReq); SpaceQuota spaceQuota = spaceLimitReq.getQuota(); assertNotNull("SpaceQuota was null", spaceQuota); assertEquals(sizeLimit, spaceQuota.getSoftLimit()); assertEquals(ProtobufUtil.toProtoViolationPolicy(policy), spaceQuota.getViolationPolicy()); assertEquals(QuotaType.SPACE, settings.getQuotaType()); SpaceLimitSettings copy = new SpaceLimitSettings(tableName, sizeLimit, policy); assertEquals(settings, copy); assertEquals(settings.hashCode(), copy.hashCode()); }
private void assertSpaceQuota( long sizeLimit, SpaceViolationPolicy violationPolicy, QuotaSettings actualSettings) { assertTrue("The actual QuotaSettings was not an instance of " + SpaceLimitSettings.class + " but of " + actualSettings.getClass(), actualSettings instanceof SpaceLimitSettings); SpaceLimitRequest spaceLimitRequest = ((SpaceLimitSettings) actualSettings).getProto(); assertEquals(sizeLimit, spaceLimitRequest.getQuota().getSoftLimit()); assertEquals(violationPolicy, ProtobufUtil.toViolationPolicy(spaceLimitRequest.getQuota().getViolationPolicy())); }
/** * Constructs a {@code SpaceLimitSettings} to remove a space quota on the given {@code namespace}. */ SpaceLimitSettings(String namespace) { super(null, null, Objects.requireNonNull(namespace), null); proto = buildProtoRemoveQuota(); }
SpaceLimitSettings(String namespace, long sizeLimit, SpaceViolationPolicy violationPolicy) { super(null, null, Objects.requireNonNull(namespace)); if (sizeLimit < 0L) { throw new IllegalArgumentException("Size limit must be a non-negative value."); } proto = buildProtoAddQuota(sizeLimit, Objects.requireNonNull(violationPolicy)); }
/** * Builds a {@link SpaceQuota} protobuf object given the arguments. * * @param sizeLimit The size limit of the quota. * @param violationPolicy The action to take when the quota is exceeded. * @return The protobuf SpaceQuota representation. */ private SpaceLimitRequest buildProtoAddQuota( long sizeLimit, SpaceViolationPolicy violationPolicy) { return buildProtoFromQuota(SpaceQuota.newBuilder() .setSoftLimit(sizeLimit) .setViolationPolicy(ProtobufUtil.toProtoViolationPolicy(violationPolicy)) .build()); }
assertFalse("Should not have multiple space quotas", seenSpace); SpaceLimitSettings spaceLimit = (SpaceLimitSettings) setting; assertEquals(tn, spaceLimit.getTableName()); assertNull("Username should be null", spaceLimit.getUserName()); assertNull("Namespace should be null", spaceLimit.getNamespace()); assertTrue("SpaceLimitSettings should have a SpaceQuota", spaceLimit.getProto().hasQuota()); assertEquals(spaceQuota, spaceLimit.getProto().getQuota()); seenSpace = true; } else {
/** * Creates a {@link QuotaSettings} object to remove the FileSystem space quota for the given * table. * * @param tableName The name of the table to remove the quota for. * @return A {@link QuotaSettings} object. */ public static QuotaSettings removeTableSpaceLimit(TableName tableName) { return new SpaceLimitSettings(tableName); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("TYPE => SPACE"); if (getTableName() != null) { sb.append(", TABLE => ").append(getTableName()); } if (getNamespace() != null) { sb.append(", NAMESPACE => ").append(getNamespace()); } if (proto.getQuota().getRemove()) { sb.append(", REMOVE => ").append(proto.getQuota().getRemove()); } else { sb.append(", LIMIT => ").append(sizeToString(proto.getQuota().getSoftLimit())); sb.append(", VIOLATION_POLICY => ").append(proto.getQuota().getViolationPolicy()); } return sb.toString(); }
/** * Constructs a {@link SpaceLimitSettings} from the provided protobuf message and namespace. * * @param namespace The target namespace for the limit. * @param proto The protobuf representation. * @return A QuotaSettings. */ static SpaceLimitSettings fromSpaceQuota( final String namespace, final QuotaProtos.SpaceQuota proto) { validateProtoArguments(proto); return new SpaceLimitSettings(namespace, proto.getSoftLimit(), ProtobufUtil.toViolationPolicy(proto.getViolationPolicy())); }
@Test public void testNamespaceQuota() { final String namespace = "foo_ns"; final long sizeLimit = 1024 * 1024; final SpaceViolationPolicy policy = SpaceViolationPolicy.NO_WRITES; SpaceLimitSettings settings = new SpaceLimitSettings(namespace, sizeLimit, policy); SetQuotaRequest proto = QuotaSettings.buildSetQuotaRequestProto(settings); assertFalse("User should be missing", proto.hasUserName()); assertFalse("TableName should be missing", proto.hasTableName()); assertEquals(namespace, proto.getNamespace()); SpaceLimitRequest spaceLimitReq = proto.getSpaceLimit(); assertNotNull("SpaceLimitRequest was null", spaceLimitReq); SpaceQuota spaceQuota = spaceLimitReq.getQuota(); assertNotNull("SpaceQuota was null", spaceQuota); assertEquals(sizeLimit, spaceQuota.getSoftLimit()); assertEquals(ProtobufUtil.toProtoViolationPolicy(policy), spaceQuota.getViolationPolicy()); assertEquals(QuotaType.SPACE, settings.getQuotaType()); SpaceLimitSettings copy = new SpaceLimitSettings(namespace, sizeLimit, policy); assertEquals(settings, copy); assertEquals(settings.hashCode(), copy.hashCode()); }
static QuotaSettings fromSpace(TableName table, String namespace, SpaceQuota protoQuota) { if (protoQuota == null) { return null; } if ((table == null && namespace == null) || (table != null && namespace != null)) { throw new IllegalArgumentException( "Can only construct SpaceLimitSettings for a table or namespace."); } if (table != null) { if (protoQuota.getRemove()) { return new SpaceLimitSettings(table); } return SpaceLimitSettings.fromSpaceQuota(table, protoQuota); } else { if (protoQuota.getRemove()) { return new SpaceLimitSettings(namespace); } // namespace must be non-null return SpaceLimitSettings.fromSpaceQuota(namespace, protoQuota); } }