Skip to content

Commit caca1e1

Browse files
Gumixlocker
authored andcommitted
box: move arrow_ipc.[hc] from src/box/ to src/lib/core/
Arrow format serialization is actually box-independent and suits better for the `core` directory. However the ClientError can not be set outside of the `box`, that's why the EncodeError and DecodeError were introduced. Follow-up tarantool#10508 NO_DOC=refactoring NO_CHANGELOG=refactoring
1 parent 835fadd commit caca1e1

File tree

12 files changed

+105
-44
lines changed

12 files changed

+105
-44
lines changed

src/box/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ add_custom_target(box_generate_lua_sources
134134
set_property(DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${lua_sources})
135135

136136
include_directories(${ZSTD_INCLUDE_DIRS})
137-
include_directories(${NANOARROW_INCLUDE_DIRS})
138137
include_directories(${PROJECT_BINARY_DIR}/src/box/sql)
139138
include_directories(${PROJECT_BINARY_DIR}/src/box)
140139
include_directories(${EXTRA_CORE_INCLUDE_DIRS})
@@ -283,7 +282,6 @@ set(box_sources
283282
decimal.c
284283
read_view.c
285284
mp_box_ctx.c
286-
arrow_ipc.c
287285
${sql_sources}
288286
${lua_sources}
289287
lua/init.c

src/box/errcode.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,7 @@ struct errcode_record {
433433
_(ER_READ_VIEW_CLOSED, 286, "The read view is closed") \
434434
_(ER_WAL_QUEUE_FULL, 287, "The WAL queue is full") \
435435
_(ER_INVALID_VCLOCK, 288, "Invalid vclock", "value", STRING) \
436-
_(ER_ARROW_IPC_ENCODE, 289, "Failed to encode Arrow IPC data", "method", STRING, "details", STRING) \
437-
_(ER_ARROW_IPC_DECODE, 290, "Failed to decode Arrow IPC data", "method", STRING, "details", STRING) \
438-
_(ER_SYNC_QUEUE_FULL, 291, "The synchronous transaction queue is full") \
436+
_(ER_SYNC_QUEUE_FULL, 289, "The synchronous transaction queue is full") \
439437
TEST_ERROR_CODES(_) /** This one should be last. */
440438

441439
/*

src/box/mp_error.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ error_build(struct mp_error *mp_error)
259259
err = new SSLError();
260260
} else if (strcmp(mp_error->type, "FileFormatError") == 0) {
261261
err = new FileFormatError();
262+
} else if (strcmp(mp_error->type, "EncodeError") == 0) {
263+
err = new EncodeError();
264+
} else if (strcmp(mp_error->type, "DecodeError") == 0) {
265+
err = new DecodeError();
262266
} else {
263267
err = new ClientError();
264268
}

src/lib/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ set(core_sources
2828
random.c
2929
trigger.cc
3030
port.c
31+
arrow_ipc.c
3132
decimal.c
3233
mp_decimal.c
3334
cord_buf.c
@@ -67,6 +68,7 @@ else()
6768
endif()
6869

6970
include_directories(${OPENSSL_INCLUDE_DIR}
71+
${NANOARROW_INCLUDE_DIRS}
7072
${EXTRA_CORE_INCLUDE_DIRS})
7173

7274
if (TARGET_OS_NETBSD)

src/box/arrow_ipc.c renamed to src/lib/core/arrow_ipc.c

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "arrow_ipc.h"
77

88
#include "diag.h"
9-
#include "error.h"
109
#include "small/region.h"
1110
#include "nanoarrow/nanoarrow_ipc.h"
1211

@@ -23,25 +22,22 @@ arrow_ipc_encode(struct ArrowArray *array, struct ArrowSchema *schema,
2322
struct ArrowArrayView array_view;
2423
rc = ArrowArrayViewInitFromSchema(&array_view, schema, &error);
2524
if (rc != NANOARROW_OK) {
26-
diag_set(ClientError, ER_ARROW_IPC_ENCODE,
27-
"ArrowArrayViewInitFromSchema", error.message);
25+
diag_set(EncodeError, "Arrow", error.message);
2826
return -1;
2927
}
3028

3129
/* Set buffer sizes and data pointers from an array. */
3230
rc = ArrowArrayViewSetArray(&array_view, array, &error);
3331
if (rc != NANOARROW_OK) {
34-
diag_set(ClientError, ER_ARROW_IPC_ENCODE,
35-
"ArrowArrayViewSetArray", error.message);
32+
diag_set(EncodeError, "Arrow", error.message);
3633
goto error1;
3734
}
3835

3936
/* All bytes written to the stream will be appended to the buffer. */
4037
struct ArrowIpcOutputStream stream;
4138
rc = ArrowIpcOutputStreamInitBuffer(&stream, &buffer);
4239
if (rc != NANOARROW_OK) {
43-
diag_set(ClientError, ER_ARROW_IPC_ENCODE,
44-
"ArrowIpcOutputStreamInitBuffer", NULL);
40+
diag_set(EncodeError, "Arrow", tt_strerror(rc));
4541
goto error1;
4642
}
4743

@@ -52,23 +48,20 @@ arrow_ipc_encode(struct ArrowArray *array, struct ArrowSchema *schema,
5248
struct ArrowIpcWriter writer;
5349
rc = ArrowIpcWriterInit(&writer, &stream);
5450
if (rc != NANOARROW_OK) {
55-
diag_set(ClientError, ER_ARROW_IPC_ENCODE, "ArrowIpcWriterInit",
56-
NULL);
51+
diag_set(EncodeError, "Arrow", tt_strerror(rc));
5752
stream.release(&stream);
5853
goto error1;
5954
}
6055

6156
rc = ArrowIpcWriterWriteSchema(&writer, schema, &error);
6257
if (rc != NANOARROW_OK) {
63-
diag_set(ClientError, ER_ARROW_IPC_ENCODE,
64-
"ArrowIpcWriterWriteSchema", error.message);
58+
diag_set(EncodeError, "Arrow", error.message);
6559
goto error2;
6660
}
6761

6862
rc = ArrowIpcWriterWriteArrayView(&writer, &array_view, &error);
6963
if (rc != NANOARROW_OK) {
70-
diag_set(ClientError, ER_ARROW_IPC_ENCODE,
71-
"ArrowIpcWriterWriteArrayView", error.message);
64+
diag_set(EncodeError, "Arrow", error.message);
7265
goto error2;
7366
}
7467

@@ -99,8 +92,7 @@ arrow_ipc_decode(struct ArrowArray *array, struct ArrowSchema *schema,
9992
{
10093
ssize_t size = data_end - data;
10194
if (size <= 0) {
102-
diag_set(ClientError, ER_ARROW_IPC_DECODE, NULL,
103-
"Unexpected data size");
95+
diag_set(DecodeError, "Arrow", "unexpected data size");
10496
return -1;
10597
}
10698

@@ -111,8 +103,7 @@ arrow_ipc_decode(struct ArrowArray *array, struct ArrowSchema *schema,
111103

112104
rc = ArrowBufferAppend(&buffer, data, size);
113105
if (rc != NANOARROW_OK) {
114-
diag_set(ClientError, ER_ARROW_IPC_DECODE, "ArrowBufferAppend",
115-
NULL);
106+
diag_set(DecodeError, "Arrow", tt_strerror(rc));
116107
ArrowBufferReset(&buffer);
117108
return -1;
118109
}
@@ -124,8 +115,7 @@ arrow_ipc_decode(struct ArrowArray *array, struct ArrowSchema *schema,
124115
struct ArrowIpcInputStream input_stream;
125116
rc = ArrowIpcInputStreamInitBuffer(&input_stream, &buffer);
126117
if (rc != NANOARROW_OK) {
127-
diag_set(ClientError, ER_ARROW_IPC_DECODE,
128-
"ArrowIpcInputStreamInitBuffer", NULL);
118+
diag_set(DecodeError, "Arrow", tt_strerror(rc));
129119
ArrowBufferReset(&buffer);
130120
return -1;
131121
}
@@ -137,23 +127,20 @@ arrow_ipc_decode(struct ArrowArray *array, struct ArrowSchema *schema,
137127
struct ArrowArrayStream array_stream;
138128
rc = ArrowIpcArrayStreamReaderInit(&array_stream, &input_stream, NULL);
139129
if (rc != NANOARROW_OK) {
140-
diag_set(ClientError, ER_ARROW_IPC_DECODE,
141-
"ArrowIpcArrayStreamReaderInit", NULL);
130+
diag_set(DecodeError, "Arrow", tt_strerror(rc));
142131
input_stream.release(&input_stream);
143132
return -1;
144133
}
145134

146135
rc = ArrowArrayStreamGetSchema(&array_stream, schema, &error);
147136
if (rc != NANOARROW_OK) {
148-
diag_set(ClientError, ER_ARROW_IPC_DECODE,
149-
"ArrowArrayStreamGetSchema", error.message);
137+
diag_set(DecodeError, "Arrow", error.message);
150138
goto error;
151139
}
152140

153141
rc = ArrowArrayStreamGetNext(&array_stream, array, &error);
154142
if (rc != NANOARROW_OK) {
155-
diag_set(ClientError, ER_ARROW_IPC_DECODE,
156-
"ArrowArrayStreamGetNext", error.message);
143+
diag_set(DecodeError, "Arrow", error.message);
157144
schema->release(schema);
158145
goto error;
159146
}
File renamed without changes.

src/lib/core/diag.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,12 @@ struct error *
439439
BuildCryptoError(const char *file, unsigned line, const char *format, ...);
440440
struct error *
441441
BuildRaftError(const char *file, unsigned line, const char *format, ...);
442+
struct error *
443+
BuildEncodeError(const char *file, unsigned line, const char *format,
444+
const char *details);
445+
struct error *
446+
BuildDecodeError(const char *file, unsigned line, const char *format,
447+
const char *details);
442448

443449
/**
444450
* Allocate and create new FileFormatError. In case of OOM return OutOfMemory.

src/lib/core/exception.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,32 @@ FileFormatError::FileFormatError(const char *file, unsigned line,
296296
va_end(ap);
297297
}
298298

299+
const struct type_info type_EncodeError =
300+
make_type("EncodeError", &type_Exception);
301+
302+
EncodeError::EncodeError(const char *file, unsigned line, const char *format,
303+
...)
304+
: Exception(&type_EncodeError, file, line)
305+
{
306+
va_list ap;
307+
va_start(ap, format);
308+
error_vformat_msg(this, format, ap);
309+
va_end(ap);
310+
}
311+
312+
const struct type_info type_DecodeError =
313+
make_type("DecodeError", &type_Exception);
314+
315+
DecodeError::DecodeError(const char *file, unsigned line, const char *format,
316+
...)
317+
: Exception(&type_DecodeError, file, line)
318+
{
319+
va_list ap;
320+
va_start(ap, format);
321+
error_vformat_msg(this, format, ap);
322+
va_end(ap);
323+
}
324+
299325
struct error *
300326
BuildOutOfMemory(const char *file, unsigned line,
301327
size_t amount, const char *allocator,
@@ -425,3 +451,25 @@ BuildFileFormatError(const char *file, unsigned line, const char *format, ...)
425451
va_end(ap);
426452
return e;
427453
}
454+
455+
struct error *
456+
BuildEncodeError(const char *file, unsigned line, const char *format,
457+
const char *details)
458+
{
459+
EncodeError *e = new EncodeError(file, line, "%s encode error: %s",
460+
format, details);
461+
error_payload_set_str(&e->payload, "format", format);
462+
error_payload_set_str(&e->payload, "details", details);
463+
return e;
464+
}
465+
466+
struct error *
467+
BuildDecodeError(const char *file, unsigned line, const char *format,
468+
const char *details)
469+
{
470+
DecodeError *e = new DecodeError(file, line, "%s decode error: %s",
471+
format, details);
472+
error_payload_set_str(&e->payload, "format", format);
473+
error_payload_set_str(&e->payload, "details", details);
474+
return e;
475+
}

src/lib/core/exception.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ extern const struct type_info type_RaftError;
5757
extern const struct type_info type_FileFormatError;
5858
/* type_info for FiberSliceIsExceeded exception */
5959
extern const struct type_info type_FiberSliceIsExceeded;
60+
/* type_info for EncodeError exception */
61+
extern const struct type_info type_EncodeError;
62+
/* type_info for DecodeError exception */
63+
extern const struct type_info type_DecodeError;
6064

6165
const char *
6266
exception_get_string(struct error *e, const struct method_info *method);
@@ -271,6 +275,28 @@ class FileFormatError: public Exception {
271275
virtual void raise() { throw this; }
272276
};
273277

278+
class EncodeError: public Exception {
279+
public:
280+
EncodeError(const char *file, unsigned line, const char *format, ...);
281+
282+
EncodeError()
283+
: Exception(&type_EncodeError, NULL, 0)
284+
{
285+
}
286+
virtual void raise() { throw this; }
287+
};
288+
289+
class DecodeError: public Exception {
290+
public:
291+
DecodeError(const char *file, unsigned line, const char *format, ...);
292+
293+
DecodeError()
294+
: Exception(&type_DecodeError, NULL, 0)
295+
{
296+
}
297+
virtual void raise() { throw this; }
298+
};
299+
274300
#define tnt_error(class, ...) ({ \
275301
say_debug("%s at %s:%i", #class, __FILE__, __LINE__); \
276302
class *e = new class(__FILE__, __LINE__, ##__VA_ARGS__); \

test/box-luatest/gh_10508_iproto_insert_arrow_test.lua

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ g.test_iproto_insert_arrow_invalid = function(cg)
8888
-- size: 0}}
8989
r = _G.iproto_insert_arrow('8210cd020036c70008')
9090
t.assert_equals(r[box.iproto.key.ERROR_24],
91-
'Failed to decode Arrow IPC data')
92-
t.assert_equals(r[box.iproto.key.ERROR][0][1][6].details,
93-
'Unexpected data size')
91+
'Arrow decode error: unexpected data size')
9492
-- <MP_MAP> {
9593
-- IPROTO_SPACE_ID: 512,
9694
-- IPROTO_ARROW: <MP_EXT> {
@@ -99,10 +97,7 @@ g.test_iproto_insert_arrow_invalid = function(cg)
9997
-- data: [0xde, 0xad, 0xbe, 0xef]}}
10098
r = _G.iproto_insert_arrow('8210cd020036c70408deadbeef')
10199
t.assert_equals(r[box.iproto.key.ERROR_24],
102-
'Failed to decode Arrow IPC data')
103-
t.assert_equals(r[box.iproto.key.ERROR][0][1][6].method,
104-
'ArrowArrayStreamGetSchema')
105-
t.assert_equals(r[box.iproto.key.ERROR][0][1][6].details,
100+
'Arrow decode error: ' ..
106101
'Expected at least 8 bytes in remainder of stream')
107102

108103
-- Correct Schema, but Array is missing.
@@ -112,10 +107,7 @@ g.test_iproto_insert_arrow_invalid = function(cg)
112107
000004000000f0ffffff4000000001000000610000000600080004000c0010000400
113108
080009000c000c000c0000000400000008000a000c00040006000800ffffffff]])
114109
t.assert_equals(r[box.iproto.key.ERROR_24],
115-
'Failed to decode Arrow IPC data')
116-
t.assert_equals(r[box.iproto.key.ERROR][0][1][6].method,
117-
'ArrowArrayStreamGetNext')
118-
t.assert_equals(r[box.iproto.key.ERROR][0][1][6].details,
110+
'Arrow decode error: ' ..
119111
'Expected at least 8 bytes in remainder of stream')
120112

121113
-- A valid request, but memtx does not support arrow format.

0 commit comments

Comments
 (0)