syhily commented on a change in pull request #17452: URL: https://github.com/apache/flink/pull/17452#discussion_r791011231
########## File path: flink-connectors/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/sink/config/PulsarSinkConfigUtils.java ########## @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.connector.pulsar.sink.config; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.configuration.ConfigOption; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.util.Preconditions; + +import org.apache.flink.shaded.guava30.com.google.common.collect.ImmutableList; +import org.apache.flink.shaded.guava30.com.google.common.collect.ImmutableSet; + +import org.apache.pulsar.client.api.Consumer; +import org.apache.pulsar.client.api.ProducerBuilder; +import org.apache.pulsar.client.api.PulsarClient; +import org.apache.pulsar.client.api.Schema; + +import java.util.List; +import java.util.Set; + +import static java.util.concurrent.TimeUnit.MICROSECONDS; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static org.apache.flink.connector.pulsar.common.config.PulsarConfigUtils.setOptionValue; +import static org.apache.flink.connector.pulsar.common.config.PulsarOptions.PULSAR_ADMIN_URL; +import static org.apache.flink.connector.pulsar.common.config.PulsarOptions.PULSAR_AUTH_PARAMS; +import static org.apache.flink.connector.pulsar.common.config.PulsarOptions.PULSAR_AUTH_PARAM_MAP; +import static org.apache.flink.connector.pulsar.common.config.PulsarOptions.PULSAR_SERVICE_URL; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_BATCHING_ENABLED; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_BATCHING_MAX_MESSAGES; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_BATCHING_MAX_PUBLISH_DELAY_MICROS; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_BLOCK_IF_QUEUE_FULL; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_COMPRESSION_TYPE; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_CRYPTO_FAILURE_ACTION; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_ENABLE_CHUNKING; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_HASHING_SCHEME; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_MAX_PENDING_MESSAGES; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_MAX_PENDING_MESSAGES_ACROSS_PARTITIONS; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_MESSAGE_ROUTING_MODE; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_PRODUCER_NAME; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_SEND_TIMEOUT_MS; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_TOPIC_NAME; + +/** Create source related {@link Consumer} and validate config. */ +@Internal +public final class PulsarSinkConfigUtils { + + private PulsarSinkConfigUtils() { + // No need to create instance. + } + + private static final List<Set<ConfigOption<?>>> CONFLICT_SINK_OPTIONS = + ImmutableList.<Set<ConfigOption<?>>>builder() + .add(ImmutableSet.of(PULSAR_AUTH_PARAMS, PULSAR_AUTH_PARAM_MAP)) + .build(); + + private static final Set<ConfigOption<?>> REQUIRED_SINK_OPTIONS = + ImmutableSet.<ConfigOption<?>>builder() + .add(PULSAR_SERVICE_URL) + .add(PULSAR_ADMIN_URL) + .build(); + + /** + * Helper method for checking client related config options. We would validate: + * + * <ul> + * <li>If user have provided the required client config options. + * <li>If user have provided some conflict options. + * </ul> + */ + public static void checkConfigurations(Configuration configuration) { + REQUIRED_SINK_OPTIONS.forEach( + option -> + Preconditions.checkArgument( + configuration.contains(option), + "Config option %s is not provided for pulsar source.", + option)); + + CONFLICT_SINK_OPTIONS.forEach( + options -> { + long nums = options.stream().filter(configuration::contains).count(); + Preconditions.checkArgument( + nums <= 1, + "Conflict config options %s were provided, we only support one of them for creating pulsar source.", + options); + }); + } + + /** Create a pulsar consumer builder by using the given Configuration. */ + public static <T> ProducerBuilder<T> createProducerBuilder( + PulsarClient client, Schema<T> schema, Configuration configuration) { + ProducerBuilder<T> builder = client.newProducer(schema); + + setOptionValue(configuration, PULSAR_TOPIC_NAME, builder::topic); + setOptionValue(configuration, PULSAR_PRODUCER_NAME, builder::producerName); + setOptionValue( + configuration, + PULSAR_SEND_TIMEOUT_MS, + v -> builder.sendTimeout(v.intValue(), MILLISECONDS)); + setOptionValue(configuration, PULSAR_BLOCK_IF_QUEUE_FULL, builder::blockIfQueueFull); + setOptionValue(configuration, PULSAR_MAX_PENDING_MESSAGES, builder::maxPendingMessages); + setOptionValue( + configuration, + PULSAR_MAX_PENDING_MESSAGES_ACROSS_PARTITIONS, + builder::maxPendingMessagesAcrossPartitions); + setOptionValue(configuration, PULSAR_MESSAGE_ROUTING_MODE, builder::messageRoutingMode); + setOptionValue(configuration, PULSAR_HASHING_SCHEME, builder::hashingScheme); Review comment: Yeah the shared configurations are defined in class `PulsarOptions` ########## File path: flink-connectors/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/sink/config/SinkConfiguration.java ########## @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.connector.pulsar.sink.config; + +import org.apache.flink.annotation.PublicEvolving; +import org.apache.flink.configuration.Configuration; + +import java.io.Serializable; + +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_FAIL_ON_WRITE; +import static org.apache.flink.connector.pulsar.sink.PulsarSinkOptions.PULSAR_TRANSACTION_TIMEOUT_MILLIS; + +/** The configure class for pulsar sink. */ +@PublicEvolving +public class SinkConfiguration implements Serializable { Review comment: I merged this class with `Configuration`. I think we can still keep this class. ########## File path: flink-connectors/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/sink/PulsarSink.java ########## @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.connector.pulsar.sink; + +import org.apache.flink.annotation.PublicEvolving; +import org.apache.flink.api.connector.sink.Committer; +import org.apache.flink.api.connector.sink.GlobalCommitter; +import org.apache.flink.api.connector.sink.Sink; +import org.apache.flink.api.connector.sink.SinkWriter; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.connector.base.DeliveryGuarantee; +import org.apache.flink.connector.pulsar.sink.committer.PulsarCommitter; +import org.apache.flink.connector.pulsar.sink.writer.PulsarWriter; +import org.apache.flink.connector.pulsar.sink.writer.PulsarWriterState; +import org.apache.flink.connector.pulsar.sink.writer.PulsarWriterStateSerializer; +import org.apache.flink.connector.pulsar.sink.writer.selector.PartitionSelector; +import org.apache.flink.connector.pulsar.sink.writer.selector.TopicSelector; +import org.apache.flink.connector.pulsar.sink.writer.serializer.PulsarSerializationSchema; +import org.apache.flink.core.io.SimpleVersionedSerializer; + +import java.io.IOException; +import java.util.List; +import java.util.Optional; + +/** + * a pulsar Sink implement. + * + * @param <IN> record data type. + */ +@PublicEvolving +public class PulsarSink<IN> implements Sink<IN, PulsarSinkCommittable, PulsarWriterState, Void> { + + private final DeliveryGuarantee deliveryGuarantee; + + private final TopicSelector<IN> topicSelector; + private final PulsarSerializationSchema<IN, ?> serializationSchema; + private final PartitionSelector<IN> partitionSelector; + + private final Configuration configuration; + + public PulsarSink( + DeliveryGuarantee deliveryGuarantee, + TopicSelector<IN> topicSelector, + PulsarSerializationSchema<IN, ?> serializationSchema, + PartitionSelector<IN> partitionSelector, + Configuration configuration) { + this.deliveryGuarantee = deliveryGuarantee; + this.topicSelector = topicSelector; + this.serializationSchema = serializationSchema; + this.partitionSelector = partitionSelector; + this.configuration = configuration; + } + + /** + * Get a PulsarSinkBuilder to builder a {@link PulsarSink}. + * + * @return a Pulsar sink builder. + */ + @SuppressWarnings("java:S4977") Review comment: This is used to pass the `SonarLint`. Sonar thought this method shouldn't cover other type annotation. https://jira.sonarsource.com/browse/SONARJAVA-2961 ########## File path: flink-connectors/flink-connector-pulsar/pom.xml ########## @@ -36,12 +36,14 @@ under the License. <packaging>jar</packaging> <properties> - <pulsar.version>2.8.0</pulsar.version> + <pulsar.version>2.9.1</pulsar.version> <!-- Test Libraries --> <protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version> - <commons-lang3.version>3.11</commons-lang3.version> - <grpc.version>1.33.0</grpc.version> + <pulsar-commons-lang3.version>3.11</pulsar-commons-lang3.version> + <pulsar-zookeeper.version>3.6.3</pulsar-zookeeper.version> Review comment: Yep. They are required only for Pulsar Broker. ########## File path: flink-connectors/flink-connector-pulsar/pom.xml ########## @@ -138,23 +140,60 @@ under the License. <version>${pulsar.version}</version> <scope>test</scope> </dependency> + <!-- Pulsar use a newer commons-lang3 in broker. --> <!-- Bump the version only for testing. --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> - <version>${commons-lang3.version}</version> + <version>${pulsar-commons-lang3.version}</version> + <scope>test</scope> + </dependency> + + <!-- Pulsar use a newer zookeeper in broker. --> + <!-- Bump the version only for testing. --> + <dependency> + <groupId>org.apache.zookeeper</groupId> + <artifactId>zookeeper</artifactId> + <version>${pulsar-zookeeper.version}</version> <scope>test</scope> </dependency> <!-- Add Pulsar 2.x as a dependency. --> <!-- Move this to button for avoiding class conflicts with pulsar-broker. --> - <dependency> <groupId>org.apache.pulsar</groupId> <artifactId>pulsar-client-all</artifactId> <version>${pulsar.version}</version> <exclusions> + <exclusion> + <groupId>com.sun.activation</groupId> Review comment: These dependencies are just used for annotation which should be working on the broker side. It's not required on the client side. ########## File path: flink-connectors/flink-connector-pulsar/pom.xml ########## @@ -163,13 +202,22 @@ under the License. </dependency> </dependencies> - <!-- gRPC use version range which don't support by flink ci. --> <dependencyManagement> <dependencies> + <!-- Pulsar use higher gRPC version. --> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-bom</artifactId> - <version>${grpc.version}</version> + <version>${pulsar-grpc.version}</version> + <type>pom</type> + <scope>import</scope> Review comment: Yep. ########## File path: flink-connectors/flink-connector-pulsar/pom.xml ########## @@ -163,13 +202,22 @@ under the License. </dependency> </dependencies> - <!-- gRPC use version range which don't support by flink ci. --> <dependencyManagement> <dependencies> + <!-- Pulsar use higher gRPC version. --> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-bom</artifactId> - <version>${grpc.version}</version> + <version>${pulsar-grpc.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + + <!-- Pulsar use higher netty version. --> + <dependency> + <groupId>io.netty</groupId> + <artifactId>netty-bom</artifactId> + <version>${pulsar-netty.version}</version> Review comment: I have checked the dependencies in `flink-connector-pulsar`, we only use netty in tests. There is no compiled dependency for netty. ########## File path: flink-connectors/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/common/config/PulsarConfigUtils.java ########## @@ -94,6 +100,11 @@ private PulsarConfigUtils() { public static PulsarClient createClient(Configuration configuration) { ClientBuilder builder = PulsarClient.builder(); + // requestTimeoutMs don't have a setter method on ClientBuilder. We have to use low level + // setter method instead. So we put this at the beginning of the builder. + Integer requestTimeoutMs = configuration.get(PULSAR_REQUEST_TIMEOUT_MS); + builder.loadConf(singletonMap("requestTimeoutMs", requestTimeoutMs)); Review comment: This has been added to the document. It was generated by `flink-docs`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org