thomaeschen opened a new issue #12778:
URL: https://github.com/apache/pulsar/issues/12778


   **Describe the bug**
   Develop a Pulsar function which contains the library 
**"org.apache.pulsar.client.impl.schema.JSONSchema"** in it. The pulsar 
function could be executed successfully under the **_localrun_** mode, but the 
system will show the error message "**java.lang.NoClassDefFoundError: 
org/apache/pulsar/client/impl/schema/JSONSchema"** while under the **_cluster 
mode_**.
   
   
   
   **To Reproduce**
   Steps to reproduce the behavior:
   1. Prepare the pulsar function, object class and pom.xml;
    
   Function:
   ```
   package holystone.pulsar;
   
   import org.apache.pulsar.functions.api.Context;
   import org.apache.pulsar.functions.api.Function;
   import org.apache.pulsar.client.api.PulsarClient;
   import org.apache.pulsar.client.api.PulsarClientException;
   import org.apache.pulsar.client.impl.schema.JSONSchema;
   import org.apache.pulsar.client.api.Producer;
   import models.DeliveryNote;
   
      public class DeliverynoteDispatchingFucntion implements 
Function<DeliveryNote, String> {
           
           String sd10_warehouse  = "persistent://demo/pulsar_function/dn-sd10";
           String hh10_warehouse  = "persistent://demo/pulsar_function/dn-hh10";
           String error                   = 
"persistent://demo/pulsar_function/error";
           String final_warehouse = ""; 
           
   
        public String process(DeliveryNote deliverynote, Context context) 
throws PulsarClientException {        
                   
                    String warehouse =  deliverynote.getWarehouse(); 
                
                switch(warehouse) {
                case "sd10":
                        final_warehouse = sd10_warehouse;
                        break;
                case "hh10":
                        final_warehouse = hh10_warehouse;
                        break;
                default:
                        final_warehouse = error;
                        break;
                }               
        
                PulsarClient pulsarClient = PulsarClient.builder()
                                           
.serviceUrl("pulsar://192.168.60.53:6650")
                                           .build();
                        
                    Producer<DeliveryNote> producer = 
                                
pulsarClient.newProducer(JSONSchema.of(DeliveryNote.class))
                                        .topic(final_warehouse)
                                        .create(); 
        
                    
producer.newMessage(JSONSchema.of(DeliveryNote.class)).value(deliverynote).send();
                    producer.flush();
                    producer.close();
                    pulsarClient.close();
                    
                    
                    String json_delivery_note = deliverynote.toString();
                    
                    return json_delivery_note ;
   
   
            }
        
        }
   ```
   The DeliveryNote class is as follow
   
   ```
   package models;
   
   public class DeliveryNote   {
   
        private String deliveryno;
        private String materialno;
        private int    quantity;
        private String warehouse;
        
        public String getDeliveryno() {
                return deliveryno;
        }
        public void setDeliveryno(String deliveryno) {
                this.deliveryno = deliveryno;
        }
        public String getMaterialno() {
                return materialno;
        }
        public void setMaterialno(String materialno) {
                this.materialno = materialno;
        }
        public int getQuantity() {
                return quantity;
        }
        public void setQuantity(int quantity) {
                this.quantity = quantity;
        }
        public String getWarehouse() {
                return warehouse;
        }
        public void setWarehouse(String warehouse) {
                this.warehouse = warehouse;
        }
   
   }
   
   ```
   
   
   
   The POM.xml is as follow 
   
   ```
   <?xml version="1.0" encoding="UTF-8"?>
   
   <project xmlns="http://maven.apache.org/POM/4.0.0";
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
        <modelVersion>4.0.0</modelVersion>
   
        <groupId>com.holystone.pulsar</groupId>
        <artifactId>DeliveryNoteDispatchingFunction</artifactId>
        <version>1.0</version>
   
        <properties>
                <pulsar.version>2.8.1</pulsar.version>
                
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
   
        <dependencies>
                <dependency>
                        <groupId>org.apache.pulsar</groupId>
                        <artifactId>pulsar-client-original</artifactId>
                        <version>${pulsar.version}</version>
                </dependency>
   
                <!-- 
https://mvnrepository.com/artifact/org.apache.pulsar/pulsar-functions-api -->
                <dependency>
                        <groupId>org.apache.pulsar</groupId>
                        <artifactId>pulsar-functions-api</artifactId>
                        <version>${pulsar.version}</version>
                </dependency>
   
        </dependencies>
   
        <build>
                <!-- <defaultGoal>clean compile assembly:single</defaultGoal> 
-->
                <pluginManagement>
                        <plugins>
                                <plugin>
                                        
<artifactId>maven-clean-plugin</artifactId>
                                        <version>3.1.0</version>
                                </plugin>
                                <plugin>
                                        
<artifactId>maven-resources-plugin</artifactId>
                                        <version>3.0.2</version>
                                </plugin>
                                <plugin>
                                        
<artifactId>maven-compiler-plugin</artifactId>
                                        <version>3.8.0</version>
                                        <configuration>
                                                <release>8</release>
                                        </configuration>
                                </plugin>
                                <plugin>
                                        
<artifactId>maven-surefire-plugin</artifactId>
                                        <version>2.22.1</version>
                                </plugin>
                                <plugin>
                                        
<artifactId>maven-jar-plugin</artifactId>
                                        <version>3.0.2</version>
                                </plugin>
                                <plugin>
                                        
<artifactId>maven-install-plugin</artifactId>
                                        <version>2.5.2</version>
                                </plugin>
                                <plugin>
                                        
<artifactId>maven-deploy-plugin</artifactId>
                                        <version>2.8.2</version>
                                </plugin>
   
                                <plugin>
                                        
<artifactId>maven-site-plugin</artifactId>
                                        <version>3.7.1</version>
                                </plugin>
                                <plugin>
                                        
<artifactId>maven-project-info-reports-plugin</artifactId>
                                        <version>3.0.0</version>
                                </plugin>
                                <plugin>
                                        
<artifactId>maven-assembly-plugin</artifactId>
                                        <configuration>
                                                
<finalName>DeliveryNoteDispatching</finalName>
                                                
<appendAssemblyId>false</appendAssemblyId>
                                                <archive>
                                                        <manifest>
                                                                
<mainClass>holystone.pulsar.DeliverynoteDispatchingFunction</mainClass>
                                                        </manifest>
                                                </archive>
                                                <descriptorRefs>
                                                        
<descriptorRef>jar-with-dependencies</descriptorRef>
                                                </descriptorRefs>
                                        </configuration>
                                        <executions>
                                                <execution>
                                                        <id>make-assembly</id>
                                                        <phase>package</phase>
                                                        <goals>
                                                                
<goal>assembly</goal>
                                                        </goals>
                                                </execution>
                                        </executions>
                                </plugin>
                        </plugins>
                </pluginManagement>
        </build>
   </project>
   
   ```
   
   
   
   
   [Function.txt](https://github.com/apache/pulsar/files/7528878/Function.txt)
   [Class.txt](https://github.com/apache/pulsar/files/7528898/Class.txt)
   [pom.txt](https://github.com/apache/pulsar/files/7528892/pom.txt)
   
   2. Package the developed function into the jar file  
(DeliveryNoteDispatchingFunction-1.0.jar)
   
   3. Copy the jar file to the $PULSAR_HOME/functions
   
   4. Execute the command : 
   ```
   bin/pulsar-admin functions create --classname 
holystone.pulsar.DeliverynoteDispatchingFucntion --jar 
functions/DeliveryNoteDispatchingFunction-1.0.jar --inputs 
persistent://demo/pulsar_function/delivery_note --tenant demo --namespace 
pulsar_function --name dispatch --output persistent://demo/pulsar_function/debug
   ```
   
   5.  Get the status by executing the command : 
   ```
   bin/pulsar-admin functions getstatus --tenant demo --namespace 
pulsar_function --name dispatch.
   ```
   the information is 
   ```
   {
     "numInstances" : 1,
     "numRunning" : 0,
     "instances" : [ {
       "instanceId" : 0,
       "status" : {
         "running" : false,
         "error" : "UNAVAILABLE: io exception",
         "numRestarts" : 0,
         "numReceived" : 0,
         "numSuccessfullyProcessed" : 0,
         "numUserExceptions" : 0,
         "latestUserExceptions" : [ ],
         "numSystemExceptions" : 0,
         "latestSystemExceptions" : [ ],
         "averageLatency" : 0.0,
         "lastInvocationTime" : 0,
         "workerId" : "tomcs"
       }
     } ]
   }
   ```
   6. Get the error from the  logs of the function and the error is shown as 
follow : 
   ```
   java.lang.NoClassDefFoundError: 
org/apache/pulsar/client/impl/schema/JSONSchema
           at 
holystone.pulsar.DeliverynoteDispatchingFucntion.process(DeliverynoteDispatchingFucntion.java:48)
 ~[DeliveryNoteDispatchingFunction-1.0.jar:?]
           at 
holystone.pulsar.DeliverynoteDispatchingFucntion.process(DeliverynoteDispatchingFucntion.java:1)
 ~[DeliveryNoteDispatchingFunction-1.0.jar:?]
           at 
org.apache.pulsar.functions.instance.JavaInstance.handleMessage(JavaInstance.java:95)
 ~[org.apache.pulsar-pulsar-functions-instance-2.8.1.jar:?]
           at 
org.apache.pulsar.functions.instance.JavaInstanceRunnable.run(JavaInstanceRunnable.java:271)
 [org.apache.pulsar-pulsar-functions-instance-2.8.1.jar:?]
           at java.lang.Thread.run(Thread.java:748) [?:1.8.0_292]
   Caused by: java.lang.ClassNotFoundException: 
org.apache.pulsar.client.impl.schema.JSONSchema
           at java.net.URLClassLoader.findClass(URLClassLoader.java:382) 
~[?:1.8.0_292]
           at java.lang.ClassLoader.loadClass(ClassLoader.java:418) 
~[?:1.8.0_292]
           at java.lang.ClassLoader.loadClass(ClassLoader.java:351) 
~[?:1.8.0_292]
           ... 5 more
   ```
   
   
   **Expected behavior**
   The Pulsar function could be executed in the cluster mode (The function 
could be executed successfully under the localrun mode)
   
   **Screenshots**
   If applicable, add screenshots to help explain your problem.
   
   **Desktop (please complete the following information):**
    Pulsar 2.8.1
    Linux : VERSION="18.04.5 LTS (Bionic Beaver)"
    Java version:  openjdk version "1.8.0_292"
   
   **Additional context**
   Add any other context about the problem here.
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to