|
| 1 | +#!/usr/bin/env tarantool |
| 2 | + |
| 3 | +local fiber = require('fiber') |
| 4 | +local netbox = require('net.box') |
| 5 | +local os = require('os') |
| 6 | +local queue = require('queue') |
| 7 | +local tap = require('tap') |
| 8 | +local tnt = require('t.tnt') |
| 9 | + |
| 10 | +local tube |
| 11 | +local test = tap.test('lost a session id after yield') |
| 12 | + |
| 13 | +-- The test cases are in check_result(). |
| 14 | +test:plan(2) |
| 15 | + |
| 16 | +-- Verify that _queue_taken space is empty. |
| 17 | +local function check_result() |
| 18 | + if tube == nil then |
| 19 | + os.exit(1) |
| 20 | + end |
| 21 | + |
| 22 | + -- tube:drop() is most simple way to check that _queue_taken |
| 23 | + -- is empty. It give an error if it is not so. |
| 24 | + local ok, res = pcall(tube.drop, tube) |
| 25 | + test:is(ok, true, 'drop empty queue') |
| 26 | + test:is(res, true, 'tube:drop() result is true') |
| 27 | + |
| 28 | + tnt.finish() |
| 29 | + os.exit(test:check() and 0 or 1) |
| 30 | +end |
| 31 | + |
| 32 | +-- Yield in queue's on_disconnect trigger (which handles a client |
| 33 | +-- disconnection) may lead to a situation when _queue_taken |
| 34 | +-- temporary space is not cleaned and becomes inconsistent with |
| 35 | +-- 'status' field in <tube_name> space. This appears only on |
| 36 | +-- tarantool versions affected by gh-4627. |
| 37 | +-- |
| 38 | +-- See https://github.com/tarantool/queue/issues/103 |
| 39 | +-- See https://github.com/tarantool/tarantool/issues/4627 |
| 40 | +local function test_lost_session_id_after_yield() |
| 41 | + -- We must check the results of a test after |
| 42 | + -- the queue._on_consumer_disconnect trigger |
| 43 | + -- has been done. |
| 44 | + -- |
| 45 | + -- Triggers are run in LIFO order. |
| 46 | + box.session.on_disconnect(check_result) |
| 47 | + |
| 48 | + local listen = 'localhost:1918' |
| 49 | + tnt.cfg{listen = listen} |
| 50 | + |
| 51 | + local driver = 'fifottl' |
| 52 | + tube = queue.create_tube('test_tube', driver, {if_not_exists = true}) |
| 53 | + |
| 54 | + rawset(_G, 'queue', require('queue')) |
| 55 | + tube:grant('guest', {call = true}) |
| 56 | + |
| 57 | + -- We need at least two tasks to trigger box.session.id() |
| 58 | + -- call after a yield in the queue._on_consumer_disconnect |
| 59 | + -- trigger (in the version of queue before the fix). See |
| 60 | + -- more below. |
| 61 | + queue.tube.test_tube:put('1') |
| 62 | + queue.tube.test_tube:put('2') |
| 63 | + local connection = netbox.connect(listen) |
| 64 | + connection:call('queue.tube.test_tube:take') |
| 65 | + connection:call('queue.tube.test_tube:take') |
| 66 | + |
| 67 | + -- After disconnection of a client the _on_consumer_disconnect |
| 68 | + -- trigger is run. It changes 'status' field for tuples in |
| 69 | + -- <tube_name> space in a loop and removes the corresponding |
| 70 | + -- tuples from _queue_taken space. The version before the fix |
| 71 | + -- operates in this way: |
| 72 | + -- |
| 73 | + -- | <_on_consumer_disconnect> |
| 74 | + -- | for task in tasks of the client: |
| 75 | + -- | call <task:release> |
| 76 | + -- | |
| 77 | + -- | <task:release> |
| 78 | + -- | delete _queue_taken tuple using box.session.id() |
| 79 | + -- | update <tube_name> space using task_id -- !! yield |
| 80 | + -- |
| 81 | + -- So the deletion from _queue_taken may be unable to delete |
| 82 | + -- right tuples for second and following tasks, because |
| 83 | + -- box.session.id() may give a garbage. |
| 84 | + connection:close() |
| 85 | + |
| 86 | + -- Wait for check_result() trigger, which will ensure that |
| 87 | + -- _queue_taken space is cleaned and will exit successfully |
| 88 | + -- in the case (or exit abnormally otherwise). |
| 89 | + fiber.sleep(5) |
| 90 | + |
| 91 | + -- Wrong session id may lead to 'Task was not taken in the |
| 92 | + -- session' error in the _on_consumer_disconnect and so the |
| 93 | + -- second on_disconnect trigger (check_result) will not be |
| 94 | + -- fired. |
| 95 | + os.exit(1) |
| 96 | +end |
| 97 | + |
| 98 | +test_lost_session_id_after_yield() |
0 commit comments