Repository: skiptests/astraea
Pull request #350 (2022-05-17 02:38:33)

Rename Quota.Action to Quota.Limit
as title


Patch:
diff --git a/app/src/main/java/org/astraea/admin/Builder.java b/app/src/main/java/org/astraea/admin/Builder.java
index 1950b67c..8906b422 100644
--- a/app/src/main/java/org/astraea/admin/Builder.java
+++ b/app/src/main/java/org/astraea/admin/Builder.java
@@ -618,6 +618,7 @@ public class Builder {
 
         @Override
         public void create() {
+          if (connectionRate == Integer.MAX_VALUE) return;
           Utils.handleException(
               () ->
                   admin
@@ -627,7 +628,8 @@ public class Builder {
                                   new ClientQuotaEntity(Map.of(ClientQuotaEntity.IP, ip)),
                                   List.of(
                                       new ClientQuotaAlteration.Op(
-                                          "connection_creation_rate", (double) connectionRate)))))
+                                          Quota.Limit.IP_CONNECTION_RATE.nameOfKafka(),
+                                          (double) connectionRate)))))
                       .all()
                       .get());
         }
@@ -656,9 +658,13 @@ public class Builder {
         public void create() {
           var q = new ArrayList<ClientQuotaAlteration.Op>();
           if (produceRate != Integer.MAX_VALUE)
-            q.add(new ClientQuotaAlteration.Op("producer_byte_rate", (double) produceRate));
+            q.add(
+                new ClientQuotaAlteration.Op(
+                    Quota.Limit.PRODUCER_BYTE_RATE.nameOfKafka(), (double) produceRate));
           if (consumeRate != Integer.MAX_VALUE)
-            q.add(new ClientQuotaAlteration.Op("consumer_byte_rate", (double) consumeRate));
+            q.add(
+                new ClientQuotaAlteration.Op(
+                    Quota.Limit.CONSUMER_BYTE_RATE.nameOfKafka(), (double) consumeRate));
           if (!q.isEmpty())
             Utils.handleException(
                 () ->
diff --git a/app/src/main/java/org/astraea/admin/Quota.java b/app/src/main/java/org/astraea/admin/Quota.java
index 5c4d0584..ae9fb62c 100644
--- a/app/src/main/java/org/astraea/admin/Quota.java
+++ b/app/src/main/java/org/astraea/admin/Quota.java
@@ -22,7 +22,7 @@ public class Quota {
                                         new Quota(
                                             target(stringStringEntry.getKey()),
                                             stringStringEntry.getValue(),
-                                            action(v.getKey()),
+                                            limit(v.getKey()),
                                             v.getValue()))))
         .collect(Collectors.toUnmodifiableList());
   }
@@ -34,8 +34,8 @@ public class Quota {
         .orElseThrow(() -> new RuntimeException("unknown: " + value));
   }
 
-  static Action action(String value) {
-    return Arrays.stream(Action.values())
+  static Limit limit(String value) {
+    return Arrays.stream(Limit.values())
         .filter(p -> p.nameOfKafka.equalsIgnoreCase(value))
         .findFirst()
         .orElseThrow(() -> new RuntimeException("unknown: " + value));
@@ -56,7 +56,7 @@ public class Quota {
     }
   }
 
-  public enum Action {
+  public enum Limit {
     PRODUCER_BYTE_RATE("producer_byte_rate"),
     CONSUMER_BYTE_RATE("consumer_byte_rate"),
     REQUEST_PERCENTAGE("request_percentage"),
@@ -64,7 +64,7 @@ public class Quota {
     IP_CONNECTION_RATE("connection_creation_rate");
     private final String nameOfKafka;
 
-    Action(String nameOfKafka) {
+    Limit(String nameOfKafka) {
       this.nameOfKafka = nameOfKafka;
     }
 
@@ -76,14 +76,14 @@ public class Quota {
   private final Target target;
 
   private final String targetValue;
-  private final Action action;
-  private final double actionValue;
+  private final Limit limit;
+  private final double limitValue;
 
-  public Quota(Target target, String targetValue, Action action, double actionValue) {
+  public Quota(Target target, String targetValue, Limit limit, double limitValue) {
     this.target = target;
     this.targetValue = targetValue;
-    this.action = action;
-    this.actionValue = actionValue;
+    this.limit = limit;
+    this.limitValue = limitValue;
   }
 
   public Target target() {
@@ -94,12 +94,12 @@ public class Quota {
     return targetValue;
   }
 
-  public Action action() {
-    return action;
+  public Limit limit() {
+    return limit;
   }
 
-  public double actionValue() {
-    return actionValue;
+  public double limitValue() {
+    return limitValue;
   }
 
   @Override
@@ -107,15 +107,15 @@ public class Quota {
     if (this == o) return true;
     if (o == null || getClass() != o.getClass()) return false;
     Quota quota = (Quota) o;
-    return Double.compare(quota.actionValue, actionValue) == 0
+    return Double.compare(quota.limitValue, limitValue) == 0
         && target == quota.target
         && Objects.equals(targetValue, quota.targetValue)
-        && action == quota.action;
+        && limit == quota.limit;
   }
 
   @Override
   public int hashCode() {
-    return Objects.hash(target, targetValue, action, actionValue);
+    return Objects.hash(target, targetValue, limit, limitValue);
   }
 
   @Override
@@ -126,10 +126,10 @@ public class Quota {
         + ", targetValue='"
         + targetValue
         + '\''
-        + ", action="
-        + action
-        + ", actionValue="
-        + actionValue
+        + ", limit="
+        + limit
+        + ", limitValue="
+        + limitValue
         + '}';
   }
 }
diff --git a/app/src/main/java/org/astraea/web/QuotaHandler.java b/app/src/main/java/org/astraea/web/QuotaHandler.java
index bcfb6be5..fd7c73f9 100644
--- a/app/src/main/java/org/astraea/web/QuotaHandler.java
+++ b/app/src/main/java/org/astraea/web/QuotaHandler.java
@@ -6,14 +6,18 @@ import java.util.Map;
 import java.util.Optional;
 import java.util.stream.Collectors;
 import org.astraea.admin.Admin;
+import org.astraea.admin.Quota;
 
 public class QuotaHandler implements Handler {
 
-  static final String IP_KEY = "ip";
-  static final String CLIENT_ID_KEY = "client-id";
-  static final String CONNECTION_RATE_KEY = "connection-rate";
-  static final String PRODUCE_RATE_KEY = "produce-rate";
-  static final String CONSUME_RATE_KEY = "consume-rate";
+  static final String IP_KEY = org.astraea.admin.Quota.Target.IP.nameOfKafka();
+  static final String CLIENT_ID_KEY = org.astraea.admin.Quota.Target.CLIENT_ID.nameOfKafka();
+  static final String CONNECTION_RATE_KEY =
+      org.astraea.admin.Quota.Limit.IP_CONNECTION_RATE.nameOfKafka();
+  static final String PRODUCE_RATE_KEY =
+      org.astraea.admin.Quota.Limit.PRODUCER_BYTE_RATE.nameOfKafka();
+  static final String CONSUME_RATE_KEY =
+      org.astraea.admin.Quota.Limit.CONSUMER_BYTE_RATE.nameOfKafka();
 
   private final Admin admin;
 
@@ -37,7 +41,7 @@ public class QuotaHandler implements Handler {
       admin
           .quotaCreator()
           .ip(request.value(IP_KEY))
-          .connectionRate(request.intValue(CONNECTION_RATE_KEY))
+          .connectionRate(request.intValue(CONNECTION_RATE_KEY, Integer.MAX_VALUE))
           .create();
       return new Quotas(admin.quotas(org.astraea.admin.Quota.Target.IP, request.value(IP_KEY)));
     }
@@ -54,26 +58,41 @@ public class QuotaHandler implements Handler {
     return ErrorObject.for404("You must define either " + CLIENT_ID_KEY + " or " + IP_KEY);
   }
 
-  static class Quota implements JsonObject {
-    final String target;
+  static class Target implements JsonObject {
+    final String name;
+    final String value;
+
+    Target(String name, String value) {
+      this.name = name;
+      this.value = value;
+    }
+  }
+
+  static class Limit implements JsonObject {
+    final String name;
+    final double value;
 
-    final String targetValue;
-    final String action;
-    final double actionValue;
+    Limit(String name, double value) {
+      this.name = name;
+      this.value = value;
+    }
+  }
+
+  static class Quota implements JsonObject {
+    final Target target;
+    final Limit limit;
 
     public Quota(org.astraea.admin.Quota quota) {
       this(
           quota.target().nameOfKafka(),
           quota.targetValue(),
-          quota.action().nameOfKafka(),
-          quota.actionValue());
+          quota.limit().nameOfKafka(),
+          quota.limitValue());
     }
 
-    public Quota(String target, String targetValue, String action, double actionValue) {
-      this.target = target;
-      this.targetValue = targetValue;
-      this.action = action;
-      this.actionValue = actionValue;
+    public Quota(String target, String targetValue, String limit, double limitValue) {
+      this.target = new Target(target, targetValue);
+      this.limit = new Limit(limit, limitValue);
     }
   }
 


Test patch:
diff --git a/app/src/test/java/org/astraea/admin/AdminTest.java b/app/src/test/java/org/astraea/admin/AdminTest.java
index cf76715e..cfef8718 100644
--- a/app/src/test/java/org/astraea/admin/AdminTest.java
+++ b/app/src/test/java/org/astraea/admin/AdminTest.java
@@ -428,10 +428,10 @@ public class AdminTest extends RequireBrokerCluster {
                 Set.of(Quota.Target.IP),
                 quotas.stream().map(Quota::target).collect(Collectors.toSet()));
             Assertions.assertEquals(
-                Set.of(Quota.Action.IP_CONNECTION_RATE),
-                quotas.stream().map(Quota::action).collect(Collectors.toSet()));
+                Set.of(Quota.Limit.IP_CONNECTION_RATE),
+                quotas.stream().map(Quota::limit).collect(Collectors.toSet()));
             Assertions.assertEquals("192.168.11.11", quotas.iterator().next().targetValue());
-            Assertions.assertEquals(10, quotas.iterator().next().actionValue());
+            Assertions.assertEquals(10, quotas.iterator().next().limitValue());
           };
 
       // only target
@@ -469,33 +469,33 @@ public class AdminTest extends RequireBrokerCluster {
                 Set.of(Quota.Target.CLIENT_ID),
                 quotas.stream().map(Quota::target).collect(Collectors.toSet()));
             Assertions.assertEquals(
-                Set.of(Quota.Action.PRODUCER_BYTE_RATE, Quota.Action.CONSUMER_BYTE_RATE),
-                quotas.stream().map(Quota::action).collect(Collectors.toSet()));
+                Set.of(Quota.Limit.PRODUCER_BYTE_RATE, Quota.Limit.CONSUMER_BYTE_RATE),
+                quotas.stream().map(Quota::limit).collect(Collectors.toSet()));
             Assertions.assertEquals(
                 10,
                 quotas.stream()
-                    .filter(q -> q.action() == Quota.Action.PRODUCER_BYTE_RATE)
+                    .filter(q -> q.limit() == Quota.Limit.PRODUCER_BYTE_RATE)
                     .findFirst()
                     .get()
-                    .actionValue());
+                    .limitValue());
             Assertions.assertEquals(
                 "my-id",
                 quotas.stream()
-                    .filter(q -> q.action() == Quota.Action.PRODUCER_BYTE_RATE)
+                    .filter(q -> q.limit() == Quota.Limit.PRODUCER_BYTE_RATE)
                     .findFirst()
                     .get()
                     .targetValue());
             Assertions.assertEquals(
                 100,
                 quotas.stream()
-                    .filter(q -> q.action() == Quota.Action.CONSUMER_BYTE_RATE)
+                    .filter(q -> q.limit() == Quota.Limit.CONSUMER_BYTE_RATE)
                     .findFirst()
                     .get()
-                    .actionValue());
+                    .limitValue());
             Assertions.assertEquals(
                 "my-id",
                 quotas.stream()
-                    .filter(q -> q.action() == Quota.Action.CONSUMER_BYTE_RATE)
+                    .filter(q -> q.limit() == Quota.Limit.CONSUMER_BYTE_RATE)
                     .findFirst()
                     .get()
                     .targetValue());
diff --git a/app/src/test/java/org/astraea/admin/QuotaTest.java b/app/src/test/java/org/astraea/admin/QuotaTest.java
index 6ad7ee38..0a1ac33f 100644
--- a/app/src/test/java/org/astraea/admin/QuotaTest.java
+++ b/app/src/test/java/org/astraea/admin/QuotaTest.java
@@ -14,7 +14,7 @@ public class QuotaTest {
 
   @Test
   void testAction() {
-    Arrays.stream(Quota.Action.values())
-        .forEach(t -> Assertions.assertEquals(t, Quota.action(t.nameOfKafka())));
+    Arrays.stream(Quota.Limit.values())
+        .forEach(t -> Assertions.assertEquals(t, Quota.limit(t.nameOfKafka())));
   }
 }
diff --git a/app/src/test/java/org/astraea/web/QuotaHandlerTest.java b/app/src/test/java/org/astraea/web/QuotaHandlerTest.java
index 715ca4e2..3c15b375 100644
--- a/app/src/test/java/org/astraea/web/QuotaHandlerTest.java
+++ b/app/src/test/java/org/astraea/web/QuotaHandlerTest.java
@@ -24,11 +24,11 @@ public class QuotaHandlerTest extends RequireBrokerCluster {
                       Map.of(QuotaHandler.IP_KEY, ip, QuotaHandler.CONNECTION_RATE_KEY, "10"))));
       Assertions.assertEquals(1, result.quotas.size());
       Assertions.assertEquals(
-          Quota.Target.IP.nameOfKafka(), result.quotas.iterator().next().target);
-      Assertions.assertEquals(ip, result.quotas.iterator().next().targetValue);
+          Quota.Target.IP.nameOfKafka(), result.quotas.iterator().next().target.name);
+      Assertions.assertEquals(ip, result.quotas.iterator().next().target.value);
       Assertions.assertEquals(
-          Quota.Action.IP_CONNECTION_RATE.nameOfKafka(), result.quotas.iterator().next().action);
-      Assertions.assertEquals(10, result.quotas.iterator().next().actionValue);
+          Quota.Limit.IP_CONNECTION_RATE.nameOfKafka(), result.quotas.iterator().next().limit.name);
+      Assertions.assertEquals(10, result.quotas.iterator().next().limit.value);
     }
   }