/** * 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); }
/** * 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); } }
/** * Creates a {@link QuotaSettings} object to limit the FileSystem space usage for the given * namespace to the given size in bytes. When the space usage is exceeded by all tables in the * namespace, the provided {@link SpaceViolationPolicy} is enacted on all tables in the namespace. * * @param namespace The namespace on which the quota should be applied. * @param sizeLimit The limit of the namespace's size in bytes. * @param violationPolicy The action to take when the the quota is exceeded. * @return An {@link QuotaSettings} object. */ public static QuotaSettings limitNamespaceSpace( final String namespace, long sizeLimit, final SpaceViolationPolicy violationPolicy) { return new SpaceLimitSettings(namespace, sizeLimit, violationPolicy); }
/** * Creates a {@link QuotaSettings} object to limit the FileSystem space usage for the given table * to the given size in bytes. When the space usage is exceeded by the table, the provided * {@link SpaceViolationPolicy} is enacted on the table. * * @param tableName The name of the table on which the quota should be applied. * @param sizeLimit The limit of a table's size in bytes. * @param violationPolicy The action to take when the quota is exceeded. * @return An {@link QuotaSettings} object. */ public static QuotaSettings limitTableSpace( final TableName tableName, long sizeLimit, final SpaceViolationPolicy violationPolicy) { return new SpaceLimitSettings(tableName, sizeLimit, 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); } }
@Test(expected = NullPointerException.class) public void testNullTableName() { TableName tn = null; new SpaceLimitSettings(tn, 1, SpaceViolationPolicy.NO_INSERTS); }
@Test(expected = NullPointerException.class) public void testNullNamespace() { String ns = null; new SpaceLimitSettings(ns, 1, SpaceViolationPolicy.NO_INSERTS); }
@Test(expected = IllegalArgumentException.class) public void testInvalidTableQuotaSizeLimit() { new SpaceLimitSettings(TableName.valueOf("foo"), -1, SpaceViolationPolicy.NO_INSERTS); }
@Test(expected = NullPointerException.class) public void testNullTableViolationPolicy() { new SpaceLimitSettings(TableName.valueOf("foo"), 1, null); }
@Test(expected = IllegalArgumentException.class) public void testInvalidNamespaceQuotaSizeLimit() { new SpaceLimitSettings("foo_ns", -1, SpaceViolationPolicy.NO_INSERTS); }
@Test(expected = NullPointerException.class) public void testNullNamespaceViolationPolicy() { new SpaceLimitSettings("foo_ns", 1, null); }
/** * 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())); }
/** * 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())); }
@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; }
/** * 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); } }
@Test public void testMergeSpace() throws IOException { TableName tn = TableName.valueOf("foo"); QuotaProtos.Quotas quota = QuotaProtos.Quotas.newBuilder() .setSpace(SPACE_QUOTA).build(); GlobalQuotaSettingsImpl settings = new GlobalQuotaSettingsImpl(null, tn, null, null, quota); // Switch the violation policy to DISABLE GlobalQuotaSettingsImpl merged = settings.merge( new SpaceLimitSettings(tn, SPACE_QUOTA.getSoftLimit(), SpaceViolationPolicy.DISABLE)); QuotaProtos.SpaceQuota mergedSpaceQuota = merged.getSpaceProto(); assertEquals(SPACE_QUOTA.getSoftLimit(), mergedSpaceQuota.getSoftLimit()); assertEquals( QuotaProtos.SpaceViolationPolicy.DISABLE, mergedSpaceQuota.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()); }
@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()); }
@Test(expected = NullPointerException.class) public void testNullTableName() { TableName tn = null; new SpaceLimitSettings(tn, 1, SpaceViolationPolicy.NO_INSERTS); }
GlobalQuotaSettingsImpl finalQuota = merged.merge(new SpaceLimitSettings( ns, SPACE_QUOTA.getSoftLimit(), SpaceViolationPolicy.NO_WRITES_COMPACTIONS));