我要评分
获取效率
正确性
完整性
易理解

Operator Pushdown Development

  1. Use the @HafTarget annotation to mark the functions that need to be pushed down.

    As described below, the @HafTarget annotation indicates that the function needs to be pushed down to an offload node.

    package com.haf.test.testapp;
    
    import com.huawei.haf.annotations.HafTarget;
    
    public class TestAppMain {
    
        private String ip;
    
        private int val;
    
        public TestAppMain(String ip) {
            val = 0;
            this.ip = ip;
        }
    
        @HafTarget(target = "127.0.0.1")
        public void jarOffloadVoidTest() {
            System.out.println("void test");
        }
    
        @HafTarget(target = "ip", to = {"a", "b"}, app = "add", exception = Exception.class)
        public int jarOffloadAddTest(int a, int b) {
            System.out.println("call add func");
            return a + b;
        }
    
        @HafTarget(target = "ip", to = {"a", "b"}, from = "val", timeout = "300", runInHostIfFailed = false, exception = Exception.class, app = "add")
        public int jarOffload(int a, int b) throws Exception {
            System.out.println("");
            val++;
            return a + b;
        }
    
        public static void main(String[] args) {
            String addIp;
            int numA = 0;
            int numB = 0;
            try {
                addIp = args[0];
                numA = Integer.valueOf(args[1]).intValue();
                numB = Integer.valueOf(args[2]).intValue();
            } catch (Exception e) {
                System.out.println(e.fillInStackTrace());
                return;
            }
            TestAppMain testAppMain = new TestAppMain(addIp);
            System.out.println(testAppMain.jarOffloadAddTest(numA, numB));
            testAppMain.jarOffloadVoidTest();
            try {
                testAppMain.jarOffload(numA, numB);
            } catch (Exception e) {
                System.out.println(e.fillInStackTrace());
                return;
            }
        }
    }
    Table 1 Fields supported by the @HafTarget annotation

    Field

    Description

    Required

    target

    IP address of the offload node to which the function will be pushed down.

    The target field can be an IP address or a member variable name of the string type. Ensure that the member variable name is a valid IP address.

    ×

    to

    Parameter name of a pushed function.

    ×

    from

    Name of the variable to be returned after the function is executed.

    ×

    timeout

    Timeout interval for pushing down the function. The default value is 1800s. The timeout field can be a digital string or a member variable name of the int type. Ensure that the member variable name is a valid integer.

    ×

    runInHostIfFailed

    Indicates whether to run the function on the host node if the pushdown fails. The default value is true; that is, if the pushdown fails, the function is run on the host node. If runInHostIfFailed is set to false, the function is not run on the host node when the pushdown fails, and an exception is thrown. You need to use exception to specify the type of the exception to be thrown.

    ×

    exception

    Type of the exception thrown when the function fails to be pushed down. If runInHostIfFailed is set to false, an exception is thrown.

    app

    Name of the current application. For the same JAR package project, if multiple functions need to be pushed down using the @HafTarget annotation, only one annotation is required to specify the application name.

    ×

  2. Configure the POM file and pack the project into a JAR package.
    As described below, maven-assembly-plugin is used to pack all dependencies of the project.
    <build>
     <plugins>
      <plugin>
       <artifactId>maven-assembly-plugin</artifactId>
       <version>3.3.0</version>
       <configuration>
        <archive>
         <manifest>
          <mainClass>com.haf.test.testapp.TestAppMain</mainClass>
         </manifest>
        </archive>
        <descriptorRefs>
         <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
       </configuration>
       <executions>
        <execution>
         <id>make-assembly</id>
         <phase>package</phase>
         <goals>
          <goal>single</goal>
         </goals>
        </execution>
       </executions>
      </plugin>
     </plugins>
    </build>
  3. Develop a serializer.

    During function pushdown, Fast Serialization (FST) is used to serialize the parameters specified by this and to of the current object. If there are types of objects that cannot be directly serialized by FST, write a serializer based on FST and use the @HafSerializer annotation to specify the serializer.

    As described below, if the Page class is a serialized object that is not supported by FST:

    public class Page {
        private String name;
        private int age;
        public Page(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public int getAge() {
            return age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Page{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

    To create an FST serializer for the Page class, you only need to inherit the FSTBasicObjectSerializer class and rewrite the writeObject and instantiate method.

    public class PageSerializer extends FSTBasicObjectSerializer {
    
        @Override
        public void writeObject(FSTObjectOutput out,
                                Object toWrite,
                                FSTClazzInfo clzInfo,
                                FSTClazzInfo.FSTFieldInfo referencedBy,
                                int streamPosition) throws IOException {
            if (toWrite instanceof Page) {
                Page page = (Page) toWrite;
                out.writeInt(page.getAge());
                out.writeStringUTF(page.getName());
            }
        }
    
        @Override
        public Object instantiate(Class objectClass,
                                  FSTObjectInput in,
                                  FSTClazzInfo serializationInfo,
                                  FSTClazzInfo.FSTFieldInfo referencee,
                                  int streamPosition) throws Exception {
            int age = in.readInt();
            String name = in.readStringUTF();
            return new Page(name, age);
        }
    }

    For a serializer that is ready, use the @HafSerializer annotation to use the serializer. See the following information:

    @HafSerializer(clazz = Page.class, serializer = PageSerializer.class)
    @HafTarget(target = "127.0.0.1")
    public Page getPage() {
     Page page = new Page("test", 0);
     return page;
    }

    The @HafSerializer annotation contains two fields:

    • clazz: indicates the class that uses the serializer.
    • serializer: indicates the customized serializer used.
  4. Compile the project.