You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by gi...@apache.org on 2026/09/01 12:01:40 UTC

(seatunnel) branch gh-readonly-queue/dev/pr-11738-e171b290516c0be548165d7a5cd1937d6bcbd08f created (now 00d5a2ab3b)

This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a change to branch gh-readonly-queue/dev/pr-11738-e171b290516c0be548165d7a5cd1937d6bcbd08f
in repository https://gitbox.apache.org/repos/asf/seatunnel.git


      at 00d5a2ab3b [Improve][Connector-V2][Prometheus] Migrate batch size validation to OptionRule (#11738)

This branch includes the following new commits:

     new 00d5a2ab3b [Improve][Connector-V2][Prometheus] Migrate batch size validation to OptionRule (#11738)

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(seatunnel) 01/01: [Improve][Connector-V2][Prometheus] Migrate batch size validation to OptionRule (#11738)

Posted by gi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/dev/pr-11738-e171b290516c0be548165d7a5cd1937d6bcbd08f
in repository https://gitbox.apache.org/repos/asf/seatunnel.git

commit 00d5a2ab3bcf47ff39a6cdaafdfc1d566bc01508
Author: Goutam Adwant <86...@users.noreply.github.com>
AuthorDate: Tue Sep 1 12:01:16 2026 +0000

    [Improve][Connector-V2][Prometheus] Migrate batch size validation to OptionRule (#11738)
    
    Signed-off-by: goutamadwant <wo...@gmail.com>
---
 docs/en/connectors/sink/Prometheus.md              |  2 +-
 docs/zh/connectors/sink/Prometheus.md              |  2 +-
 .../prometheus/config/PrometheusSinkConfig.java    | 13 +-----
 .../prometheus/config/PrometheusSinkOptions.java   |  3 +-
 .../prometheus/sink/PrometheusSinkFactory.java     |  5 ++-
 .../prometheus/PrometheusFactoryTest.java          | 50 ++++++++++++++++++++++
 .../prometheus/PrometheusSinkConfigTest.java       | 13 ------
 7 files changed, 59 insertions(+), 29 deletions(-)

diff --git a/docs/en/connectors/sink/Prometheus.md b/docs/en/connectors/sink/Prometheus.md
index 0980f63203..d80a0e8766 100644
--- a/docs/en/connectors/sink/Prometheus.md
+++ b/docs/en/connectors/sink/Prometheus.md
@@ -53,7 +53,7 @@ downloaded from Maven Central.
 | retry                       | Int    | No       | -       | Maximum retry times when the HTTP request throws an `IOException`. |
 | retry_backoff_multiplier_ms | Int    | No       | 100     | Retry backoff multiplier in milliseconds. |
 | retry_backoff_max_ms        | Int    | No       | 10000   | Maximum retry backoff in milliseconds. |
-| batch_size                  | Int    | No       | 1024    | Maximum number of rows buffered before writing to Prometheus. |
+| batch_size                  | Int    | No       | 1024    | Positive number of rows buffered before writing to Prometheus. |
 | multi_table_sink_replica    | Int    | No       | 1       | Writer replica count for each table in a multi-table sink job. |
 | common-options              | Config | No       | -       | Sink plugin common parameters. See [Sink Common Options](../common-options/sink-common-options.md). |
 
diff --git a/docs/zh/connectors/sink/Prometheus.md b/docs/zh/connectors/sink/Prometheus.md
index b528d9cf7b..76f5ae6955 100644
--- a/docs/zh/connectors/sink/Prometheus.md
+++ b/docs/zh/connectors/sink/Prometheus.md
@@ -50,7 +50,7 @@ Prometheus 数据接收器把上游数据写入 Prometheus remote write API。
 | retry                       | Int    | 否       | -      | HTTP 请求出现 `IOException` 时的最大重试次数。 |
 | retry_backoff_multiplier_ms | Int    | 否       | 100    | 重试退避时间倍数,单位毫秒。 |
 | retry_backoff_max_ms        | Int    | 否       | 10000  | 最大重试退避时间,单位毫秒。 |
-| batch_size                  | Int    | 否       | 1024   | 写入 Prometheus 前最多缓存的行数。 |
+| batch_size                  | Int    | 否       | 1024   | 写入 Prometheus 前缓存的行数,必须大于 0。 |
 | multi_table_sink_replica    | Int    | 否       | 1      | 多表写入时,每张表使用的写入器副本数。 |
 | common-options              | Config | 否       | -      | 接收器插件通用参数,详情请参考[接收器通用选项](../common-options/sink-common-options.md)。 |
 
diff --git a/seatunnel-connectors-v2/connector-prometheus/src/main/java/org/apache/seatunnel/connectors/seatunnel/prometheus/config/PrometheusSinkConfig.java b/seatunnel-connectors-v2/connector-prometheus/src/main/java/org/apache/seatunnel/connectors/seatunnel/prometheus/config/PrometheusSinkConfig.java
index f944ffc9fb..1c2b6a0517 100644
--- a/seatunnel-connectors-v2/connector-prometheus/src/main/java/org/apache/seatunnel/connectors/seatunnel/prometheus/config/PrometheusSinkConfig.java
+++ b/seatunnel-connectors-v2/connector-prometheus/src/main/java/org/apache/seatunnel/connectors/seatunnel/prometheus/config/PrometheusSinkConfig.java
@@ -23,8 +23,6 @@ import lombok.Getter;
 import lombok.Setter;
 import lombok.ToString;
 
-import static org.apache.seatunnel.shade.com.google.common.base.Preconditions.checkArgument;
-
 @Setter
 @Getter
 @ToString
@@ -49,16 +47,7 @@ public class PrometheusSinkConfig extends HttpConfig {
         if (pluginConfig.getOptional(PrometheusSinkOptions.KEY_TIMESTAMP).isPresent()) {
             sinkConfig.setKeyTimestamp(pluginConfig.get(PrometheusSinkOptions.KEY_TIMESTAMP));
         }
-        // Use get() (not getOptional().isPresent()) so the option's declared default is applied
-        // when batch_size is not configured; getOptional() does not fall back to the default, which
-        // would leave batchSize at 0 and disable the size-based flush trigger.
-        int batchSize = checkIntArgument(pluginConfig.get(PrometheusSinkOptions.BATCH_SIZE));
-        sinkConfig.setBatchSize(batchSize);
+        sinkConfig.setBatchSize(pluginConfig.get(PrometheusSinkOptions.BATCH_SIZE));
         return sinkConfig;
     }
-
-    private static int checkIntArgument(int args) {
-        checkArgument(args > 0);
-        return args;
-    }
 }
diff --git a/seatunnel-connectors-v2/connector-prometheus/src/main/java/org/apache/seatunnel/connectors/seatunnel/prometheus/config/PrometheusSinkOptions.java b/seatunnel-connectors-v2/connector-prometheus/src/main/java/org/apache/seatunnel/connectors/seatunnel/prometheus/config/PrometheusSinkOptions.java
index 427994f765..4798e6c21d 100644
--- a/seatunnel-connectors-v2/connector-prometheus/src/main/java/org/apache/seatunnel/connectors/seatunnel/prometheus/config/PrometheusSinkOptions.java
+++ b/seatunnel-connectors-v2/connector-prometheus/src/main/java/org/apache/seatunnel/connectors/seatunnel/prometheus/config/PrometheusSinkOptions.java
@@ -41,5 +41,6 @@ public class PrometheusSinkOptions extends HttpCommonOptions {
             Options.key("batch_size")
                     .intType()
                     .defaultValue(DEFAULT_BATCH_SIZE)
-                    .withDescription("the batch size writer to prometheus");
+                    .withDescription(
+                            "the positive number of rows buffered before writing to prometheus");
 }
diff --git a/seatunnel-connectors-v2/connector-prometheus/src/main/java/org/apache/seatunnel/connectors/seatunnel/prometheus/sink/PrometheusSinkFactory.java b/seatunnel-connectors-v2/connector-prometheus/src/main/java/org/apache/seatunnel/connectors/seatunnel/prometheus/sink/PrometheusSinkFactory.java
index 879c6a2862..8aaf5edb16 100644
--- a/seatunnel-connectors-v2/connector-prometheus/src/main/java/org/apache/seatunnel/connectors/seatunnel/prometheus/sink/PrometheusSinkFactory.java
+++ b/seatunnel-connectors-v2/connector-prometheus/src/main/java/org/apache/seatunnel/connectors/seatunnel/prometheus/sink/PrometheusSinkFactory.java
@@ -17,6 +17,7 @@
 package org.apache.seatunnel.connectors.seatunnel.prometheus.sink;
 
 import org.apache.seatunnel.api.configuration.ReadonlyConfig;
+import org.apache.seatunnel.api.configuration.util.Conditions;
 import org.apache.seatunnel.api.configuration.util.OptionRule;
 import org.apache.seatunnel.api.options.SinkConnectorCommonOptions;
 import org.apache.seatunnel.api.table.catalog.CatalogTable;
@@ -52,7 +53,9 @@ public class PrometheusSinkFactory extends HttpSinkFactory {
                 .optional(PrometheusSinkOptions.RETRY)
                 .optional(PrometheusSinkOptions.RETRY_BACKOFF_MULTIPLIER_MS)
                 .optional(PrometheusSinkOptions.RETRY_BACKOFF_MAX_MS)
-                .optional(PrometheusSinkOptions.BATCH_SIZE)
+                .optional(
+                        PrometheusSinkOptions.BATCH_SIZE,
+                        Conditions.greaterThan(PrometheusSinkOptions.BATCH_SIZE, 0))
                 .optional(SinkConnectorCommonOptions.MULTI_TABLE_SINK_REPLICA)
                 .build();
     }
diff --git a/seatunnel-connectors-v2/connector-prometheus/src/test/java/org/apache/seatunnel/connectors/seatunnel/prometheus/PrometheusFactoryTest.java b/seatunnel-connectors-v2/connector-prometheus/src/test/java/org/apache/seatunnel/connectors/seatunnel/prometheus/PrometheusFactoryTest.java
index 782b08ed01..6f7af34c88 100644
--- a/seatunnel-connectors-v2/connector-prometheus/src/test/java/org/apache/seatunnel/connectors/seatunnel/prometheus/PrometheusFactoryTest.java
+++ b/seatunnel-connectors-v2/connector-prometheus/src/test/java/org/apache/seatunnel/connectors/seatunnel/prometheus/PrometheusFactoryTest.java
@@ -17,12 +17,19 @@
 
 package org.apache.seatunnel.connectors.seatunnel.prometheus;
 
+import org.apache.seatunnel.api.configuration.ReadonlyConfig;
+import org.apache.seatunnel.api.configuration.util.ConfigValidator;
+import org.apache.seatunnel.api.configuration.util.OptionValidationException;
+import org.apache.seatunnel.connectors.seatunnel.prometheus.config.PrometheusSinkOptions;
 import org.apache.seatunnel.connectors.seatunnel.prometheus.sink.PrometheusSinkFactory;
 import org.apache.seatunnel.connectors.seatunnel.prometheus.source.PrometheusSourceFactory;
 
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
+import java.util.HashMap;
+import java.util.Map;
+
 public class PrometheusFactoryTest {
 
     @Test
@@ -30,4 +37,47 @@ public class PrometheusFactoryTest {
         Assertions.assertNotNull((new PrometheusSourceFactory()).optionRule());
         Assertions.assertNotNull((new PrometheusSinkFactory()).optionRule());
     }
+
+    @Test
+    void testPositiveBatchSize() {
+        Assertions.assertDoesNotThrow(() -> validateBatchSize(1));
+    }
+
+    @Test
+    void testBatchSizeIsOptional() {
+        Assertions.assertDoesNotThrow(() -> validate(sinkConfig()));
+    }
+
+    @Test
+    void testNonPositiveBatchSizeFails() {
+        assertInvalidBatchSize(0);
+        assertInvalidBatchSize(-1);
+    }
+
+    private void assertInvalidBatchSize(int batchSize) {
+        OptionValidationException exception =
+                Assertions.assertThrows(
+                        OptionValidationException.class, () -> validateBatchSize(batchSize));
+        Assertions.assertTrue(
+                exception.getMessage().contains(PrometheusSinkOptions.BATCH_SIZE.key()));
+    }
+
+    private void validateBatchSize(int batchSize) {
+        Map<String, Object> config = sinkConfig();
+        config.put(PrometheusSinkOptions.BATCH_SIZE.key(), batchSize);
+        validate(config);
+    }
+
+    private Map<String, Object> sinkConfig() {
+        Map<String, Object> config = new HashMap<>();
+        config.put(PrometheusSinkOptions.URL.key(), "http://localhost:9090");
+        config.put(PrometheusSinkOptions.KEY_LABEL.key(), "label");
+        config.put(PrometheusSinkOptions.KEY_VALUE.key(), "value");
+        return config;
+    }
+
+    private void validate(Map<String, Object> config) {
+        ConfigValidator.of(ReadonlyConfig.fromMap(config))
+                .validate(new PrometheusSinkFactory().optionRule());
+    }
 }
diff --git a/seatunnel-connectors-v2/connector-prometheus/src/test/java/org/apache/seatunnel/connectors/seatunnel/prometheus/PrometheusSinkConfigTest.java b/seatunnel-connectors-v2/connector-prometheus/src/test/java/org/apache/seatunnel/connectors/seatunnel/prometheus/PrometheusSinkConfigTest.java
index 5d47be0c51..473a760d05 100644
--- a/seatunnel-connectors-v2/connector-prometheus/src/test/java/org/apache/seatunnel/connectors/seatunnel/prometheus/PrometheusSinkConfigTest.java
+++ b/seatunnel-connectors-v2/connector-prometheus/src/test/java/org/apache/seatunnel/connectors/seatunnel/prometheus/PrometheusSinkConfigTest.java
@@ -54,17 +54,4 @@ class PrometheusSinkConfigTest {
 
         Assertions.assertEquals(5, config.getBatchSize());
     }
-
-    @Test
-    void shouldRejectNonPositiveBatchSize() {
-        Map<String, Object> map = new HashMap<>();
-        map.put("url", "http://localhost:9090/api/v1/write");
-        map.put("key_label", "c_map");
-        map.put("key_value", "c_double");
-        map.put("batch_size", 0);
-
-        Assertions.assertThrows(
-                Exception.class,
-                () -> PrometheusSinkConfig.loadConfig(ReadonlyConfig.fromMap(map)));
-    }
 }