SSC (Scudata SPL Cloud) Global Variable and Lock

 

Global variable

In SSC environment, sometimes information needs to be shared between computing tasks (QVM scripts) on multiple QVMs. In this case, SSC global variable (simply written as global variable in the following part) is useful. Managed by QVS, the global variable is available to every script run on each QVM applied through QVS.

Qenv function is used to assign value to the global variable: Qenv("gv1",1) #gv1 is the variable name

Qenv(“gv1”) The function is also used to obtain the global variable: Qenv("gv1")

Unlike an ordinary SPL global variable, related data will be transmitted between QVS and QVM once the SSC global variable is accessed. Since the network transmission is much slower than memory access, it is not suitable for the SSC global variable to store a large amount of data. It only fits for storing a small volume of data, like status and arguments.

For example, an application on SSC uses the configuration file to store environment variables and the root directory containing the application. The data file paths the application involves are dynamic and need to be pieced together using the root directory. The global variable helps to do this job conveniently.

The configuration file (json file on S3):

[{"app_root":"bucket1114/root1","datetime_format":"……"}]

At the application initiation phase or when changing the root directory dynamically, set the application’s root directory as global variable:

A B
1 =Qfile("bucket1114/app.json") json file
2 =json(A1.read()) Parse
3 =Qenv("app_root",A2.app_root) Assign value to the global variable

When performing computations in the application, we can use the global variable to assemble the actual dynamic path to access the corresponding data file.

A B
1 =realPath=concat(Qenv("app_root","/","sales.txt")) Get the global variable
2 =Qfile(realPath) Data file
3 =A2.import@t() Parse
4 =A3.select(Amount>arg1 && Amount<=arg2) Compute

Lock

Read/write lock is needed for modifying the SSC global variable. Without the lock, modification may fail or wrong result could be obtained. For example, we want to find how many times (m) a QVM script is executed (Suppose there are two concurrencies and the initial value of m is 0). If the global variable isn’t locked, m+1 in both tasks is 0+1=1 and the final result is 1, which is wrong obviously.

With the lock, m+1 in the two tasks is forcibly executed in order. The locked one is executed first, resources are released, and then the other m+1 is executed. Now m=1 and m+1=2, and we get the result correctly.

QVM script:

A B C
1 if (Qlock("lock1"),2) Lock the variable; lock name is lock1
2 >Qenv("m",Qenv("m")+1) Add 1 to the access count
3 =Qlock@u("lock1") Unlock
4 Else
5 return “lock failed” Lock failed
6

Qlock function is used to lock the variable; both parameters must be present – the first is the lock name and the second is timeout (unit: seconds). The function returns true when locking succeeds; and false when locking fails due to timeout caused by the reason that the lock in another script being not released or other reasons. To unlock the variable, add @u option in Qlock function. Different from an ordinary SPL lock, use of SSC lock involves data transmission between QVM and QVS and delay exists. The timeout value thus should not be too small.