From 0a8043d15e97b922b96215ffb747965a26e9e4f0 Mon Sep 17 00:00:00 2001
From: Ilya Verbin <iverbin@tarantool.org>
Date: Wed, 18 Oct 2023 15:15:19 +0300
Subject: [PATCH] box: improve error message raised on hash index replace
 failure

Old: "Failed to allocate 2147483648 bytes in hash_table for key"
New: "Failed to allocate 16384 bytes in hash_table for key"

ERRINJ_INDEX_ALLOC cannot be used to test this error, because it fails
earlier, so ERRINJ_HASH_INDEX_REPLACE is introduced.

Follow-up #3594

NO_DOC=minor
NO_CHANGELOG=minor
---
 src/box/memtx_hash.cc          |  6 ++----
 src/lib/core/errinj.h          |  1 +
 test/box/errinj.result         |  1 +
 test/box/errinj_index.result   | 12 ++++++++++++
 test/box/errinj_index.test.lua |  5 +++++
 5 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/box/memtx_hash.cc b/src/box/memtx_hash.cc
index bc02721a8b..b80c05c212 100644
--- a/src/box/memtx_hash.cc
+++ b/src/box/memtx_hash.cc
@@ -371,15 +371,13 @@ memtx_hash_index_replace(struct index *base, struct tuple *old_tuple,
 		if (pos == light_index_end)
 			pos = light_index_insert(hash_table, h, new_tuple);
 
-		ERROR_INJECT(ERRINJ_INDEX_ALLOC,
-		{
+		ERROR_INJECT(ERRINJ_HASH_INDEX_REPLACE, {
 			light_index_delete(hash_table, pos);
 			pos = light_index_end;
 		});
 
 		if (pos == light_index_end) {
-			diag_set(OutOfMemory,
-				 (ssize_t)light_index_count(hash_table),
+			diag_set(OutOfMemory, MEMTX_EXTENT_SIZE,
 				 "hash_table", "key");
 			return -1;
 		}
diff --git a/src/lib/core/errinj.h b/src/lib/core/errinj.h
index 085a74479c..7d878efe45 100644
--- a/src/lib/core/errinj.h
+++ b/src/lib/core/errinj.h
@@ -94,6 +94,7 @@ struct errinj {
 	_(ERRINJ_INDEX_ALLOC, ERRINJ_BOOL, {.bparam = false}) \
 	_(ERRINJ_INDEX_RESERVE, ERRINJ_BOOL, {.bparam = false})\
 	_(ERRINJ_INDEX_ITERATOR_NEW, ERRINJ_BOOL, {.bparam = false}) \
+	_(ERRINJ_HASH_INDEX_REPLACE, ERRINJ_BOOL, {.bparam = false}) \
 	_(ERRINJ_IPROTO_CFG_LISTEN, ERRINJ_INT, {.iparam = 0}) \
 	_(ERRINJ_IPROTO_DISABLE_ID, ERRINJ_BOOL, {.bparam = false}) \
 	_(ERRINJ_IPROTO_DISABLE_WATCH, ERRINJ_BOOL, {.bparam = false}) \
diff --git a/test/box/errinj.result b/test/box/errinj.result
index 53ea6b353c..0f47135996 100644
--- a/test/box/errinj.result
+++ b/test/box/errinj.result
@@ -63,6 +63,7 @@ evals
   - ERRINJ_FIBER_MPROTECT: -1
   - ERRINJ_FLIGHTREC_LOG_DELAY: 0
   - ERRINJ_FLIGHTREC_RECREATE_RENAME: false
+  - ERRINJ_HASH_INDEX_REPLACE: false
   - ERRINJ_HTTPC_EXECUTE: false
   - ERRINJ_HTTP_RESPONSE_ADD_WAIT: false
   - ERRINJ_INDEX_ALLOC: false
diff --git a/test/box/errinj_index.result b/test/box/errinj_index.result
index f3943dccac..ec1f7209ad 100644
--- a/test/box/errinj_index.result
+++ b/test/box/errinj_index.result
@@ -489,6 +489,18 @@ res
   - [5009, 5009]
   - [5010, 5010]
 ...
+errinj.set("ERRINJ_HASH_INDEX_REPLACE", true)
+---
+- ok
+...
+s:replace{3594, 3594}
+---
+- error: Failed to allocate 16384 bytes in hash_table for key
+...
+errinj.set("ERRINJ_HASH_INDEX_REPLACE", false)
+---
+- ok
+...
 s:drop()
 ---
 ...
diff --git a/test/box/errinj_index.test.lua b/test/box/errinj_index.test.lua
index 6068965ee2..f6ab00757e 100644
--- a/test/box/errinj_index.test.lua
+++ b/test/box/errinj_index.test.lua
@@ -121,6 +121,11 @@ res
 res = {}
 for i = 5001,5010 do table.insert(res, (s:get{i})) end
 res
+
+errinj.set("ERRINJ_HASH_INDEX_REPLACE", true)
+s:replace{3594, 3594}
+errinj.set("ERRINJ_HASH_INDEX_REPLACE", false)
+
 s:drop()
 
 errinj = nil
-- 
GitLab