Search code examples
hashuser-defined-functionsaerospikenosql

What kind of hash function does Aerospike use for UDF modules?


Aerospike allows to list already loaded UDF modules. Here is an aql-example (taken from help-portal):

SHOW MODULES

The result contains hash field:

aql> show modules
+---------------------------+-------+------------------------+
| module                    | type  | hash                   |
+---------------------------+-------+------------------------+
| "example1.lua"            | "lua" | "033671e05067888fce09" |
| "example2.lua"            | "lua" | "07b42082cca8e73a96b2" |
+---------------------------+-------+------------------------+
2 rows in set (0.000 secs)

My question (1): I have the example1.lua file (sources of the UDF module), how to calculate (and check) hash for it?

My hypotheses:

  1. hash depends on source file - TRUE
  2. hash depends on timestamp of loading - failed
  3. hash depends on filename - TRUE
  4. md5sum on source file - failed (obviously, see hypothesis 3)
  5. sha1sum on pure source file - failed (see hypothesis 3)

Actually, I want to check the version for already registered module, and upgrade that module if necessary. As mentioned in the documentation:

the hash value of the file. Most users will not find the hash value useful, but some may use it to verify the version or instance of a UDF on the server.

So, the alternate question (2) is: how to check the version of a registered UDF module?


Solution

  • Its a 20-byte sha1 hash of the below 3 fields represented as a json document. The json document is string representation without any newline separator or EOF character.

    "content64" : base64 encoding of whole file
    "type" : LUA
    "name" : filename
    

    Example :

    {"content64": "ZnVuY3Rpb24gcHJpbnRfbWV0YShyZWMpCgoJaWYgbm90IGFlcm9zcGlrZTpleGlzdHMocmVjKSB0aGVuCgkJcmV0dXJuICJub3JlYyIKCWVuZAoKCWluZm8ocmVjb3JkLnR0bChyZWMpKQoJaW5mbyhyZWNvcmQubGFzdF91cGRhdGVfdGltZShyZWMpKQoJcmV0dXJuIHJlY29yZC5sYXN0X3VwZGF0ZV90aW1lKHJlYykKZW5kCg==", "type": "LUA", "name": "lut.lua"}
    

    Cross-checking : (Ignore the '' escape character added for the sake of echo command)

    $ echo -n {\"content64\": \"ZnVuY3Rpb24gcHJpbnRfbWV0YShyZWMpCgoJaWYgbm90IGFlcm9zcGlrZTpleGlzdHMocmVjKSB0aGVuCgkJcmV0dXJuICJub3JlYyIKCWVuZAoKCWluZm8ocmVjb3JkLnR0bChyZWMpKQoJaW5mbyhyZWNvcmQubGFzdF91cGRhdGVfdGltZShyZWMpKQoJcmV0dXJuIHJlY29yZC5sYXN0X3VwZGF0ZV90aW1lKHJlYykKZW5kCg==\", \"type\": \"LUA\", \"name\": \"lut.lua\"} | sha1sum 
    998354a59337b229e2dd777a3288e8e8f33568a5  -
    
    $ asinfo -v "udf-list"
    filename=lut.lua,hash=998354a59337b229e2dd777a3288e8e8f33568a5,type=LUA;