|
| 1 | +# User metadata update rework |
| 2 | +## Overview and Motivation |
| 3 | +`ms-users` service uses Redis pipeline based javascript script for updating user metadata records. |
| 4 | +For audience tracking features, there were some logic changes in `utils/updateMetadata.js`, but this brought |
| 5 | +some problems with parallel update requests: Some changes to the user's audience list could be lost. |
| 6 | +It's possible to use `dlock` semaphores to block parallel updates: https://github.com/AVVS/distributed-callback-queue#semaphore, but |
| 7 | +a better solution is to use Redis LUA script, this guarantees that data is updated and all audiences stay in place. |
| 8 | + |
| 9 | +## utils/updateMetadata.js |
| 10 | +Almost all logic presented in this file will be removed and ported into LUA Script. |
| 11 | +This Function will check the consistency of the provided `opts`. If `opts.metadata` and `opts.audiences` are objects, |
| 12 | +they will be transformed to array. Count of meta operations and audiences checked to equal each other. |
| 13 | + |
| 14 | +After commands execution result returned from the script, decoded from JSON string, all command results that having length == 1 |
| 15 | +converted from an array to value from array[0]. |
| 16 | + |
| 17 | +## script/updateMetadata.lua |
| 18 | +Script repeats all logic including custom scripts. |
| 19 | +### Script parameters: |
| 20 | +1. KEYS[1] - "{ms-users}{userId}!metadata!{audience}"-for proper cluster node handling, and used as metadata template |
| 21 | +2. KEYS[2] - Audiences hashset/keyTemplate [Described here](user_audience_tracking.md) |
| 22 | +2. ARGV[1] UserId |
| 23 | +3. ARGV[2] JSON encoded opts parameter opts.{script, metadata, audiences} |
| 24 | + |
| 25 | +### if Scripts provided |
| 26 | +All scripts evaluated before any execution. This protects us from inconsistent data changes. |
| 27 | + |
| 28 | +### Audience list update |
| 29 | +For proper audience list handling, script get's current list of user's audiences from hset `KEYS[1].userid`, unions them with |
| 30 | +`opts.audiences` and generates full list metadata keys. Iterates over them, to form list of curent user audiences, |
| 31 | +and saves data back into HSET `KEYS[1].userid`. |
| 32 | + |
| 33 | +### Depending on metadata or script set: |
| 34 | +If `opt.metadata` set: |
| 35 | + * Script starts iterating audiences. |
| 36 | + * On each audience, creates metadata key from provided template |
| 37 | + * Iterates over provided in `opt.metadata` operations, based on index of `opts.audiences`. |
| 38 | + ```javascript |
| 39 | + const opts = { |
| 40 | + audiences: ['first', 'second'], |
| 41 | + metadata: [{ |
| 42 | + // first audience commands, |
| 43 | + }, { |
| 44 | + // second audience commands |
| 45 | + }] |
| 46 | + } |
| 47 | + ``` |
| 48 | + Commands executed in order: `audiences[0]` => `metadata[0]`,`audiences[1]` => `metadata[1]`, |
| 49 | + |
| 50 | +If `opt.script` set: |
| 51 | + * Script starts iterating audiences and creates metadata keys from provided template |
| 52 | + * Iterates over `opt.script`: |
| 53 | + * EVAL's script from `script.lua` and executes with params generated from: metadata keys(generated in previous step) |
| 54 | + and passed `script.argv` |
| 55 | + * If script evaluation fails, script returns redis.error witch description. |
| 56 | +
|
| 57 | +When operations/scripts processed, the script forms json object like |
| 58 | +```javascript |
| 59 | +const metaResponse = [ |
| 60 | + //forEach audience |
| 61 | + { |
| 62 | + '$incr': { |
| 63 | + field: 'result', // result returned from HINCRBY command |
| 64 | + }, |
| 65 | + '$remove': intCount, // count of deleted fields |
| 66 | + '$set': "OK", // or cmd hset result. |
| 67 | + } |
| 68 | +]; |
| 69 | +
|
| 70 | +const scriptResponse = { |
| 71 | + 'scriptName': [ |
| 72 | + // values returned from script |
| 73 | + ] |
| 74 | +}; |
| 75 | +``` |
| 76 | +and returns as encoded json string. |
| 77 | +
|
0 commit comments