From 29519c71443bf78f035b9593b105e017350cf64c Mon Sep 17 00:00:00 2001
From: Alexander Turenko <alexander.turenko@tarantool.org>
Date: Mon, 10 Jun 2024 19:53:44 +0300
Subject: [PATCH] config: use vshard-ee if available

The new closed-source `vshard-ee` module was recently introduced. It is
based on the open-source `vshard` module and, as far as I know, provides
the same API.

Let's configure `vshard-ee` in the same way as `vshard` if sharding is
enabled in tarantool's configuration.

Prefer `vshard-ee` if both are available.

Fixes https://github.com/tarantool/tarantool-ee/issues/815

@TarantoolBot document
Title: config: vshard-ee is now supported

The declarative configuration supports `vshard-ee` in addition to
`vshard` since Tarantool 3.1.1 and 3.2+.

`vshard` is mentioned in the documentation at least [here][1]. All such
places should be updated to mention both `vshard-ee` and `vshard`.

[1]: https://www.tarantool.io/en/doc/latest/reference/configuration/configuration_reference/#sharding
---
 .../unreleased/config-support-vshard-ee.md    |  4 +++
 src/box/lua/config/applier/credentials.lua    |  6 ++--
 src/box/lua/config/applier/sharding.lua       |  8 +++--
 .../upgrade/2.11.0/vshard/storage-gen.lua     |  5 ++-
 test/config-luatest/vshard_test.lua           | 34 ++++++++++++-------
 test/config-luatest/vshard_upgrade_test.lua   |  7 ++--
 6 files changed, 44 insertions(+), 20 deletions(-)
 create mode 100644 changelogs/unreleased/config-support-vshard-ee.md

diff --git a/changelogs/unreleased/config-support-vshard-ee.md b/changelogs/unreleased/config-support-vshard-ee.md
new file mode 100644
index 0000000000..8c5b12e208
--- /dev/null
+++ b/changelogs/unreleased/config-support-vshard-ee.md
@@ -0,0 +1,4 @@
+## bugfix/config
+
+* Support for `vshard-ee` in addition to `vshard` in the sharding configuration
+  logic (ghe-815).
diff --git a/src/box/lua/config/applier/credentials.lua b/src/box/lua/config/applier/credentials.lua
index 70547c073f..36028ef703 100644
--- a/src/box/lua/config/applier/credentials.lua
+++ b/src/box/lua/config/applier/credentials.lua
@@ -1,5 +1,6 @@
 local expression = require('internal.config.utils.expression')
 local log = require('internal.config.utils.log')
+local loaders = require('internal.loaders')
 local digest = require('digest')
 local fiber = require('fiber')
 
@@ -318,9 +319,10 @@ local function sharding_role(configdata)
     -- The error will be thrown later, in sharding.lua. Here we are simply
     -- trying to avoid the "module not found" error.
     --
-    local ok, vshard = pcall(require, 'vshard')
+    local ok, vshard = pcall(loaders.require_first, 'vshard-ee', 'vshard')
     if ok and expression.eval('v >= 0.1.25', {v = vshard.consts.VERSION}) then
-        local vexports = require('vshard.storage.exports')
+        local vexports = loaders.require_first('vshard-ee.storage.exports',
+            'vshard.storage.exports')
         local exports = vexports.compile(vexports.log[#vexports.log])
         for name in pairs(exports.funcs) do
             table.insert(funcs, name)
diff --git a/src/box/lua/config/applier/sharding.lua b/src/box/lua/config/applier/sharding.lua
index 2869f77ef9..db8ede0c6c 100644
--- a/src/box/lua/config/applier/sharding.lua
+++ b/src/box/lua/config/applier/sharding.lua
@@ -1,5 +1,6 @@
 local expression = require('internal.config.utils.expression')
 local log = require('internal.config.utils.log')
+local loaders = require('internal.loaders')
 _G.vshard = nil
 
 -- Watcher which will create all the necessary functions.
@@ -12,9 +13,9 @@ local function apply(config)
         return
     end
     -- Make sure vshard is available and its version is not too old.
-    local ok, vshard = pcall(require, 'vshard')
+    local ok, vshard = pcall(loaders.require_first, 'vshard-ee', 'vshard')
     if not ok then
-        error('The vshard module is not available', 0)
+        error('The vshard-ee/vshard module is not available', 0)
     end
     if expression.eval('v < 0.1.25', {v = vshard.consts.VERSION}) then
         error('The vshard module is too old: the minimum supported version ' ..
@@ -36,7 +37,8 @@ local function apply(config)
         -- Start a watcher which will create all the necessary functions.
         if watcher == nil then
             local function deploy_funcs()
-                local vexports = require('vshard.storage.exports')
+                local vexports = loaders.require_first(
+                    'vshard-ee.storage.exports', 'vshard.storage.exports')
                 local exports = vexports.compile(vexports.log[#vexports.log])
                 vexports.deploy_funcs(exports)
             end
diff --git a/test/box-luatest/upgrade/2.11.0/vshard/storage-gen.lua b/test/box-luatest/upgrade/2.11.0/vshard/storage-gen.lua
index b9d075378f..2498b0b27a 100644
--- a/test/box-luatest/upgrade/2.11.0/vshard/storage-gen.lua
+++ b/test/box-luatest/upgrade/2.11.0/vshard/storage-gen.lua
@@ -45,7 +45,10 @@ local names = {
 }
 
 -- Start the database with sharding
-local vshard = require('vshard')
+local ok, vshard = pcall(require, 'vshard-ee')
+if not ok then
+    vshard = require('vshard')
+end
 vshard.storage.cfg(cfg, names[NAME])
 
 box.once("testapp", function()
diff --git a/test/config-luatest/vshard_test.lua b/test/config-luatest/vshard_test.lua
index de37b5ffd6..7abd2a62c4 100644
--- a/test/config-luatest/vshard_test.lua
+++ b/test/config-luatest/vshard_test.lua
@@ -7,10 +7,17 @@ local helpers = require('test.config-luatest.helpers')
 
 local g = helpers.group()
 
-local has_vshard = pcall(require, 'vshard')
+local has_vshard = pcall(require, 'vshard-ee')
+if not has_vshard then
+    has_vshard = pcall(require, 'vshard')
+end
+
+local function skip_if_no_vshard()
+    t.skip_if(not has_vshard, 'Module "vshard-ee/vshard" is not available')
+end
 
 g.test_fixed_masters = function(g)
-    t.skip_if(not has_vshard, 'Module "vshard" is not available')
+    skip_if_no_vshard()
     local dir = treegen.prepare_directory(g, {}, {})
     local config = [[
     credentials:
@@ -258,7 +265,7 @@ g.test_fixed_masters = function(g)
 end
 
 g.test_rebalancer_role = function(g)
-    t.skip_if(not has_vshard, 'Module "vshard" is not available')
+    skip_if_no_vshard()
     local dir = treegen.prepare_directory(g, {}, {})
     local config = [[
     credentials:
@@ -422,7 +429,7 @@ g.test_rebalancer_role = function(g)
 end
 
 g.test_too_many_rebalancers = function(g)
-    t.skip_if(not has_vshard, 'Module "vshard" is not available')
+    skip_if_no_vshard()
     local dir = treegen.prepare_directory(g, {}, {})
     local config = [[
     credentials:
@@ -470,7 +477,7 @@ end
 -- credentials sharding role.
 --
 g.test_no_sharding_credentials_role_success = function(g)
-    t.skip_if(not has_vshard, 'Module "vshard" is not available')
+    skip_if_no_vshard()
     helpers.success_case(g, {
         options = {
             ['credentials.users.storage.roles'] = {'sharding'},
@@ -494,7 +501,7 @@ g.test_no_sharding_credentials_role_success = function(g)
 end
 
 g.test_no_sharding_credentials_role_error = function(g)
-    t.skip_if(not has_vshard, 'Module "vshard" is not available')
+    skip_if_no_vshard()
     local dir = treegen.prepare_directory(g, {}, {})
     local config = [[
     credentials:
@@ -536,7 +543,7 @@ end
 -- credentials, its credential sharding role is not checked.
 --
 g.test_no_storage_user = function(g)
-    t.skip_if(not has_vshard, 'Module "vshard" is not available')
+    skip_if_no_vshard()
     helpers.success_case(g, {
         options = {
             ['sharding.roles'] = {'storage', 'router'},
@@ -551,7 +558,7 @@ end
 
 -- Make sure that the credential sharding role has all necessary credentials.
 g.test_sharding_credentials_role = function(g)
-    t.skip_if(not has_vshard, 'Module "vshard" is not available')
+    skip_if_no_vshard()
     local dir = treegen.prepare_directory(g, {}, {})
     local config = [[
     credentials:
@@ -594,7 +601,10 @@ g.test_sharding_credentials_role = function(g)
         local role = box.space._user.index.name:get('sharding')
         local repl_id = box.space._user.index.name:get('replication').id
         t.assert(role ~= nil)
-        local vexports = require('vshard.storage.exports')
+        local ok, vexports = pcall(require, 'vshard-ee.storage.exports')
+        if not ok then
+            vexports = require('vshard.storage.exports')
+        end
         local exports = vexports.compile(vexports.log[#vexports.log])
 
         -- The role should have the necessary privileges.
@@ -618,7 +628,7 @@ g.test_sharding_credentials_role = function(g)
 end
 
 g.test_no_suitable_uri = function(g)
-    t.skip_if(not has_vshard, 'Module "vshard" is not available')
+    skip_if_no_vshard()
     local dir = treegen.prepare_directory(g, {}, {})
     local config = [[
     credentials:
@@ -656,7 +666,7 @@ end
 -- Make sure that rebalancing will be disabled if rebalancer_mode == 'off', even
 -- if rebalancer sharding role is assigned.
 g.test_rebalancer_mode = function(g)
-    t.skip_if(not has_vshard, 'Module "vshard" is not available')
+    skip_if_no_vshard()
     local dir = treegen.prepare_directory(g, {}, {})
     local config = [[
     credentials:
@@ -708,7 +718,7 @@ end
 -- Make sure the shrading.weight configuration parameter is set
 -- correctly in vshard.
 g.test_weight = function(g)
-    t.skip_if(not has_vshard, 'Module "vshard" is not available')
+    skip_if_no_vshard()
     local dir = treegen.prepare_directory(g, {}, {})
     local config = [[
     credentials:
diff --git a/test/config-luatest/vshard_upgrade_test.lua b/test/config-luatest/vshard_upgrade_test.lua
index f5538c7a1a..2a6620e8dd 100644
--- a/test/config-luatest/vshard_upgrade_test.lua
+++ b/test/config-luatest/vshard_upgrade_test.lua
@@ -7,7 +7,10 @@ local fio = require('fio')
 
 local g = t.group()
 
-local has_vshard = pcall(require, 'vshard')
+local has_vshard = pcall(require, 'vshard-ee')
+if not has_vshard then
+    has_vshard = pcall(require, 'vshard')
+end
 
 --
 -- Test, that Tarantool upgrade with vshard happens without downtime:
@@ -20,7 +23,7 @@ local has_vshard = pcall(require, 'vshard')
 --     6. Vshard works after all names are applied.
 --
 g.before_all(function(g)
-    t.skip_if(not has_vshard, 'Module "vshard" is not available')
+    t.skip_if(not has_vshard, 'Module "vshard-ee/vshard" is not available')
     treegen.init(g)
     local uuids = {
         ['rs-001'] = 'cbf06940-0790-498b-948d-042b62cf3d29',
-- 
GitLab