1

Let's say we have Tarantool Cartridge based service, which stores posts made by users. When user makes a new post, it is inserted to corresponding space. Simultaneously into sharded-queue tube notify_friends is added task for notifying user friends about new post.

Question is about creation of tube notify_friends. Initially I planned to do that in init() method of the service role, but it causes error, because tube creation modifies clusterwide-config and it is being changed when init() runs. I could try creating tube at first task add request, but not sure if it's the best approach.

Denis
  • 566
  • 1
  • 5
  • 8

2 Answers2

1

There are 2 ways I'd go with it:

  1. Create the tube on the first request as you propose. Nothing bad will happen.
  2. If you want to do it in advance - create a fiber in the init function that will try to create the tube after 10 seconds since the initialization, if the tube doesn't exist. You can figure out all instances that have sharded_queue storage, and run the fiber only on the first one (sort alphabetically by instance URI).
  • Could you explain more about second part of second option? – Denis Sep 28 '20 at 06:57
  • 1. How do I get all instances that have sharded_queue storage? Use `cartridge. rpc_get_candidates()` or `cartridge.lua-api.get-topology()` or something else? 2. How to run fiber on first one? Check if current instance is first one in list? – Denis Sep 28 '20 at 07:03
  • 1
    Yes. You can get a list of instances with a specific role using rpc_get_candidates (https://www.tarantool.io/en/doc/latest/book/cartridge/cartridge_api/modules/cartridge/#rpc-get-candidates-role-name-opts), and then sort them by URI to find out who is first. Then get your uri with membership.myself().uri and compare what you get from rpc_get_candidates with this value to figure out if you're first. –  Sep 28 '20 at 07:23
1

You can put it to "default config" of your app.

Check it here: How to implement default config section for a custom Tarantool Cartridge role?

Dmitry Sharonov
  • 471
  • 2
  • 12