Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Problem

SAP HANA XS JS does not provide a direct way to do the parallel processing like threading in Java. Assume the scenario like you want to execute a long running stored procedure or logic in XS code in parallel. Currently SAP HANA XS JS does not provide this capability. Here we try to overcome this limitation by leveraging the SAP HANA XS Job Scheduling feature.

Pre requisites

  • You need to have latest SAP HANA system installed. Preferably SP 8 or higher.
  • SAP HANA user needs to have the role – “sap.hana.xs.admin.roles::JobAdministrator”
  • Scheduler is enabled. Double click the system from the System view in SAP HANA Studio. Go to configuration tab -> xsengine.ini -> add section named “scheduler” -> add value “enabled” = “true”.
  • SAP HANA workspace configured.

Solution

To address this issue we will be creating a XS Job .There will be separate schedules created in this XS Job for each of the thread request. Once the execution is completed the scheduling instance will be deleted programmatically. SAP HANA provides API to add or delete schedule programmatically. We will be exploring this feature from SAP HANA to achieve the parallel processing. Get the POC code from Github. Note that this project is done as just a POC.

We shall discuss about this in detail now

  1. Import the attached project into the SAP HANA Studio
  2. Share the project to the workspace root package and activate it.
  3. Once the project is activated, this will create a schema named XS_THREAD. We need to grant “SELECT”,”UPDATE”,”INSERT”  privileges to this schema for the SAP HANA user which you are using. For this follow below steps
  4. Open the user from “Security” ->”Users”
  5. Go to the table “Object Privileges”
  6. Click on “+” button and search for XS_THREAD schema
  7. Select on this schema and check the “SELECT”,”UPDATE”,”INSERT”   privileges.
  8. Click on the activate “>” button on the top right corner.
    1. We need to activate the scheduling job as part of this project. For this follow the below steps
    2. Login to the SAP HANA XS Admin - http://<HOST _NAME>:80<INSTANCE>/sap/hana/xs/admin/
    3. Go the package “thread.factory” and select the XS Job named “THREAD_SCHEDULE.xsjob”
    4. Provide the SAP HANA User name which has the role “sap.hana.xs.admin.roles::JobAdministrator” for the “User”
    5. Check the “Active” check box
    6. Save.
  9. This project has three main files to discuss
    1. ThreadFactory.xsjslib : This has the external API to create the thread instances. Here we need to pass the thread name and the thread function details like package, xsjslib file name, function name , parameter details etc.. . This will update the meta data info about this thread into the "XS_THREAD"."thread.factory::THREADS" table and generate a thread id. This thread ID will be used for the future reference in the program like to get the back metadata information. After this it will create a XS Job schedule with one second interval (which is the best immediate time we can chose in the SAP HANA XS Job schedule) and thread id as the parameter to the XS Job schedule.
    2. ThreadHelper.xsjslib: This file contains all the helper functions to create /delete the schedule and managing the thread metadata table.
    3. Thread.xsjs :When each job is executed the framework will call this xsjs file with thread id. Here we get the thread meta data information from the "XS_THREAD"."thread.factory::THREADS" table and call the appropriate function with parameters. Once the thread function is executed successfully the schedule will be deleted.

Usage

ThreadId = $.thread.factory.ThreadFactory.create(< Thread name> ,<package name,<file name>,<function name>,<parameters in JSON Object>);

Assume that you need to execute a function named “callme” defined in ‘sap.test.MyLogic.xsjslib’ file. Follow below syntax for the same,

$.thread.factory.ThreadFactory.create("thread 1 ","sap.test"," MyLogic "," callme ",{ });

To understand this better this project contains a test program “thread.test” -> threadTest.xsjs. This will execute two thread instance for the thread.test:threadlib::execute() method , which will insert 100 entries into the "XS_THREAD"."thread.test::TEST" table.

In above example we executed a stored procedure. This could be used in situation where you need to execute some batch process like some logic in XS code  in parallel. This can be extended to maintain some demon process to handle some background actions also.

Limitations

  • Since we are depending on the XS scheduling feature the program execution can start with a delay of one second only, immediate start is not available.
  • Here we simulated the multithreading as part of application logic, there will be a risk of handling system crashes.
5 Comments