You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by "FelixGV (via GitHub)" <gi...@apache.org> on 2023/02/21 16:02:44 UTC

[GitHub] [pinot] FelixGV commented on a diff in pull request #10308: Add the support of Apache Pulsar for StreamGithubEventsCommand

FelixGV commented on code in PR #10308:
URL: https://github.com/apache/pinot/pull/10308#discussion_r1113260200


##########
pinot-plugins/pinot-stream-ingestion/pinot-pulsar/src/main/java/org/apache/pinot/plugin/stream/pulsar/server/PulsarDataProducer.java:
##########
@@ -0,0 +1,95 @@
+/**
+ * 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.pinot.plugin.stream.pulsar.server;
+
+import com.google.common.base.Preconditions;
+import java.util.Base64;
+import java.util.Properties;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.spi.stream.StreamDataProducer;
+import org.apache.pulsar.client.api.AuthenticationFactory;
+import org.apache.pulsar.client.api.ClientBuilder;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.PulsarClientException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class PulsarDataProducer implements StreamDataProducer {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(PulsarDataProducer.class);
+
+  public static final String BROKER_SERVICE_URL = "brokerServiceUrl";
+  public static final String TOKEN = "token";
+
+  private PulsarClient _pulsarClient;
+
+  public PulsarDataProducer() {
+  }
+
+  @Override
+  public void init(Properties props) {
+    String brokerServiceUrl = props.getProperty(BROKER_SERVICE_URL);
+    Preconditions.checkNotNull(brokerServiceUrl, "broker service url must be configured.");
+
+    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(brokerServiceUrl);
+
+    String token = props.getProperty(TOKEN);
+    if (StringUtils.isNotEmpty(token)) {
+      try {
+        _pulsarClient = clientBuilder.authentication(AuthenticationFactory.token(token)).build();
+      } catch (PulsarClientException e) {
+        throw new IllegalArgumentException("Failed to create pulsar client");
+      }
+    }
+  }
+
+  @Override
+  public void produce(String topic, byte[] payload) {
+    try {
+      Producer<byte[]> producer = _pulsarClient.newProducer().topic(topic).create();
+      producer.send(payload);
+      producer.close();
+    } catch (PulsarClientException e) {
+      LOGGER.error("Failed to produce message for topic: " + topic, e);
+    }
+  }
+
+  @Override
+  public void produce(String topic, byte[] key, byte[] payload) {
+    try {
+      Producer<byte[]> producer = _pulsarClient.newProducer().topic(topic).create();
+      producer.newMessage().key(Base64.getEncoder().encodeToString(key)).value(payload).send();
+      producer.send(payload);

Review Comment:
   I'm not super familiar with the Pulsar API so maybe this is as intended, but won't this code here produce twice, once with a key and a value, and a second time with just a value?



##########
pinot-plugins/pinot-stream-ingestion/pinot-pulsar/src/main/java/org/apache/pinot/plugin/stream/pulsar/server/PulsarDataProducer.java:
##########
@@ -0,0 +1,95 @@
+/**
+ * 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.pinot.plugin.stream.pulsar.server;
+
+import com.google.common.base.Preconditions;
+import java.util.Base64;
+import java.util.Properties;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.spi.stream.StreamDataProducer;
+import org.apache.pulsar.client.api.AuthenticationFactory;
+import org.apache.pulsar.client.api.ClientBuilder;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.PulsarClientException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class PulsarDataProducer implements StreamDataProducer {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(PulsarDataProducer.class);
+
+  public static final String BROKER_SERVICE_URL = "brokerServiceUrl";
+  public static final String TOKEN = "token";
+
+  private PulsarClient _pulsarClient;
+
+  public PulsarDataProducer() {
+  }
+
+  @Override
+  public void init(Properties props) {
+    String brokerServiceUrl = props.getProperty(BROKER_SERVICE_URL);
+    Preconditions.checkNotNull(brokerServiceUrl, "broker service url must be configured.");
+
+    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(brokerServiceUrl);
+
+    String token = props.getProperty(TOKEN);
+    if (StringUtils.isNotEmpty(token)) {
+      try {
+        _pulsarClient = clientBuilder.authentication(AuthenticationFactory.token(token)).build();
+      } catch (PulsarClientException e) {
+        throw new IllegalArgumentException("Failed to create pulsar client");
+      }
+    }
+  }
+
+  @Override
+  public void produce(String topic, byte[] payload) {
+    try {
+      Producer<byte[]> producer = _pulsarClient.newProducer().topic(topic).create();

Review Comment:
   If we do create/close clients here (as opposed to reusing them), should we use `try-with-resources`? The current code seems like it could leak clients if they throw during `send()`.



-- 
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: commits-unsubscribe@pinot.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org