#include "dbms_pipe_buffer.hpp" extern "C" { #include "utils/elog.h" #include "utils/memutils.h" #include "varatt.h" } static constexpr Size DBMS_PIPE_LOCAL_BUFFER_LIMIT = 32766; static constexpr Size DBMS_PIPE_GENERIC_ITEM_OVERHEAD = 3; static constexpr Size DBMS_PIPE_VARCHAR2_ITEM_OVERHEAD = 6; struct DbmsPipeBufferItem { DbmsPipeBufferItem *next; int32 type_code; Size data_bytes; bool is_null; char data[FLEXIBLE_ARRAY_MEMBER]; }; struct DbmsPipeOutgoingBuffer { MemoryContext context; DbmsPipeBufferItem *head; DbmsPipeBufferItem *tail; Size accounted_bytes; }; enum class DbmsPipeReadState { FRESH, NO_RECEIVED_MESSAGE, RECEIVED_MESSAGE, INVALID }; struct DbmsPipeIncomingBuffer { MemoryContext context; DbmsPipeBufferItem *head; DbmsPipeBufferItem *tail; DbmsPipeBufferItem *cursor; bool has_retained_message; DbmsPipeReadState state; }; /* Внутренний формат передачи typed items между backend-ами RuDB. */ struct DbmsPipeSerializedItemHeader { int32 type_code; uint32 data_bytes; uint8 is_null; uint8 reserved[3]; }; static DbmsPipeOutgoingBuffer dbms_pipe_outgoing_buffer = {}; static DbmsPipeIncomingBuffer dbms_pipe_incoming_buffer = { nullptr, nullptr, nullptr, nullptr, false, DbmsPipeReadState::FRESH }; static void dbms_pipe_buffer_ensure_outgoing_context() { if (dbms_pipe_outgoing_buffer.context == nullptr) dbms_pipe_outgoing_buffer.context = AllocSetContextCreate(TopMemoryContext, "DBMS_PIPE outgoing buffer", ALLOCSET_SMALL_SIZES); } static void dbms_pipe_buffer_check_capacity(Size item_bytes) { if (item_bytes > DBMS_PIPE_LOCAL_BUFFER_LIMIT || dbms_pipe_outgoing_buffer.accounted_bytes > DBMS_PIPE_LOCAL_BUFFER_LIMIT - item_bytes) ereport(ERROR, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), errmsg("ORA-06558: buffer in dbms_pipe package is full. " "No more items allowed"))); } static DbmsPipeBufferItem * dbms_pipe_buffer_allocate_item(MemoryContext context, DbmsPipeItemType type_code, const void *data, Size data_bytes, bool is_null) { const Size allocation_size = offsetof(DbmsPipeBufferItem, data) + data_bytes; auto *const item = static_cast( MemoryContextAlloc(context, allocation_size)); item->next = nullptr; item->type_code = type_code; item->data_bytes = data_bytes; item->is_null = is_null; if (!is_null && data_bytes > 0) memcpy(item->data, data, data_bytes); return item; } static void dbms_pipe_buffer_append(DbmsPipeItemType type_code, const void *data, Size data_bytes, bool is_null, Size accounted_bytes) { dbms_pipe_buffer_check_capacity(accounted_bytes); dbms_pipe_buffer_ensure_outgoing_context(); DbmsPipeBufferItem *const packed = dbms_pipe_buffer_allocate_item(dbms_pipe_outgoing_buffer.context, type_code, data, data_bytes, is_null); if (dbms_pipe_outgoing_buffer.tail == nullptr) dbms_pipe_outgoing_buffer.head = packed; else dbms_pipe_outgoing_buffer.tail->next = packed; dbms_pipe_outgoing_buffer.tail = packed; dbms_pipe_outgoing_buffer.accounted_bytes += accounted_bytes; if (dbms_pipe_incoming_buffer.state != DbmsPipeReadState::RECEIVED_MESSAGE) dbms_pipe_incoming_buffer.state = DbmsPipeReadState::NO_RECEIVED_MESSAGE; } static void dbms_pipe_buffer_pack_varlena(DbmsPipeItemType type_code, const varlena *item, bool is_null, Size item_overhead) { const Size logical_bytes = is_null ? 0 : VARSIZE_ANY_EXHDR(item); const Size stored_bytes = is_null ? 0 : VARSIZE_ANY(item); dbms_pipe_buffer_append(type_code, item, stored_bytes, is_null, logical_bytes + item_overhead); } static void dbms_pipe_buffer_clear_outgoing() { if (dbms_pipe_outgoing_buffer.context != nullptr) MemoryContextReset(dbms_pipe_outgoing_buffer.context); dbms_pipe_outgoing_buffer.head = nullptr; dbms_pipe_outgoing_buffer.tail = nullptr; dbms_pipe_outgoing_buffer.accounted_bytes = 0; } static void dbms_pipe_buffer_clear_incoming() { if (dbms_pipe_incoming_buffer.context != nullptr) MemoryContextReset(dbms_pipe_incoming_buffer.context); dbms_pipe_incoming_buffer.head = nullptr; dbms_pipe_incoming_buffer.tail = nullptr; dbms_pipe_incoming_buffer.cursor = nullptr; } static void dbms_pipe_buffer_raise_empty() { ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("ORA-06556: the pipe is empty, cannot fulfill the " "unpack_message request"))); } void dbms_pipe_buffer_pack_varchar2(const text *item, bool is_null) { dbms_pipe_buffer_pack_varlena(DBMS_PIPE_ITEM_VARCHAR2, item, is_null, DBMS_PIPE_VARCHAR2_ITEM_OVERHEAD); } void dbms_pipe_buffer_pack_number(Numeric item, bool is_null) { dbms_pipe_buffer_pack_varlena(DBMS_PIPE_ITEM_NUMBER, reinterpret_cast(item), is_null, DBMS_PIPE_GENERIC_ITEM_OVERHEAD); } void dbms_pipe_buffer_pack_date(Timestamp item, bool is_null) { dbms_pipe_buffer_append(DBMS_PIPE_ITEM_DATE, is_null ? nullptr : &item, is_null ? 0 : sizeof(item), is_null, (is_null ? 0 : sizeof(item)) + DBMS_PIPE_GENERIC_ITEM_OVERHEAD); } void dbms_pipe_buffer_pack_raw(const bytea *item, bool is_null) { dbms_pipe_buffer_pack_varlena(DBMS_PIPE_ITEM_RAW, item, is_null, DBMS_PIPE_GENERIC_ITEM_OVERHEAD); } void dbms_pipe_buffer_pack_rowid(const text *item, bool is_null) { dbms_pipe_buffer_pack_varlena(DBMS_PIPE_ITEM_ROWID, item, is_null, DBMS_PIPE_GENERIC_ITEM_OVERHEAD); } DbmsPipeSerializedMessage dbms_pipe_buffer_serialize_outgoing() { DbmsPipeSerializedMessage message = { nullptr, 0, dbms_pipe_outgoing_buffer.accounted_bytes }; for (DbmsPipeBufferItem *item = dbms_pipe_outgoing_buffer.head; item != nullptr; item = item->next) { const Size item_bytes = sizeof(DbmsPipeSerializedItemHeader) + item->data_bytes; if (message.data_bytes > MaxAllocSize - item_bytes) ereport(ERROR, (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), errmsg("DBMS_PIPE serialized message is too large"))); message.data_bytes += item_bytes; } if (message.data_bytes == 0) return message; message.data = static_cast(palloc(message.data_bytes)); Size offset = 0; for (DbmsPipeBufferItem *item = dbms_pipe_outgoing_buffer.head; item != nullptr; item = item->next) { DbmsPipeSerializedItemHeader header = {}; header.type_code = item->type_code; header.data_bytes = static_cast(item->data_bytes); header.is_null = item->is_null ? 1 : 0; memcpy(message.data + offset, &header, sizeof(header)); offset += sizeof(header); if (item->data_bytes > 0) { memcpy(message.data + offset, item->data, item->data_bytes); offset += item->data_bytes; } } Assert(offset == message.data_bytes); return message; } void dbms_pipe_buffer_send_succeeded() { dbms_pipe_buffer_clear_outgoing(); if (dbms_pipe_incoming_buffer.state != DbmsPipeReadState::RECEIVED_MESSAGE) dbms_pipe_incoming_buffer.state = DbmsPipeReadState::NO_RECEIVED_MESSAGE; } void dbms_pipe_buffer_receive_succeeded(const char *data, Size data_bytes) { /* * Сначала полностью разбираем сообщение в новом контексте. * Ошибка не должна разрушать сохранённое принятое сообщение. */ MemoryContext new_context = AllocSetContextCreate(TopMemoryContext, "DBMS_PIPE incoming buffer", ALLOCSET_SMALL_SIZES); DbmsPipeBufferItem *head = nullptr; DbmsPipeBufferItem *tail = nullptr; PG_TRY(); { Size offset = 0; while (offset < data_bytes) { if (data_bytes - offset < sizeof(DbmsPipeSerializedItemHeader)) ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED), errmsg("corrupt DBMS_PIPE message header"))); DbmsPipeSerializedItemHeader header; memcpy(&header, data + offset, sizeof(header)); offset += sizeof(header); if (header.data_bytes > data_bytes - offset) ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED), errmsg("corrupt DBMS_PIPE message payload"))); const auto type_code = static_cast(header.type_code); DbmsPipeBufferItem *const item = dbms_pipe_buffer_allocate_item(new_context, type_code, data + offset, header.data_bytes, header.is_null != 0); if (tail == nullptr) head = item; else tail->next = item; tail = item; offset += header.data_bytes; } } PG_CATCH(); { MemoryContextDelete(new_context); PG_RE_THROW(); } PG_END_TRY(); if (dbms_pipe_incoming_buffer.context != nullptr) MemoryContextDelete(dbms_pipe_incoming_buffer.context); dbms_pipe_incoming_buffer.context = new_context; dbms_pipe_incoming_buffer.head = head; dbms_pipe_incoming_buffer.tail = tail; dbms_pipe_incoming_buffer.cursor = head; dbms_pipe_incoming_buffer.has_retained_message = true; dbms_pipe_incoming_buffer.state = DbmsPipeReadState::RECEIVED_MESSAGE; } void dbms_pipe_buffer_receive_failed() { dbms_pipe_incoming_buffer.state = DbmsPipeReadState::INVALID; } void dbms_pipe_buffer_purge_read_state() { /* * PURGE не затрагивает исходящий packed payload, но делает прежнее * received message недоступным для NEXT_ITEM_TYPE/UNPACK/RESET replay. */ dbms_pipe_buffer_clear_incoming(); dbms_pipe_incoming_buffer.has_retained_message = false; dbms_pipe_incoming_buffer.state = DbmsPipeReadState::INVALID; } int32 dbms_pipe_buffer_next_item_type() { switch (dbms_pipe_incoming_buffer.state) { case DbmsPipeReadState::RECEIVED_MESSAGE: if (dbms_pipe_incoming_buffer.cursor == nullptr) return DBMS_PIPE_ITEM_END; return dbms_pipe_incoming_buffer.cursor->type_code; case DbmsPipeReadState::NO_RECEIVED_MESSAGE: return DBMS_PIPE_ITEM_NO_RECEIVED_MESSAGE; case DbmsPipeReadState::FRESH: case DbmsPipeReadState::INVALID: dbms_pipe_buffer_raise_empty(); } pg_unreachable(); } DbmsPipeUnpackedItem dbms_pipe_buffer_unpack(DbmsPipeItemType expected_type) { if (dbms_pipe_incoming_buffer.state != DbmsPipeReadState::RECEIVED_MESSAGE || dbms_pipe_incoming_buffer.cursor == nullptr) dbms_pipe_buffer_raise_empty(); DbmsPipeBufferItem *const item = dbms_pipe_incoming_buffer.cursor; if (item->type_code != expected_type) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("ORA-06559: wrong datatype requested, %d, " "actual datatype is %d", static_cast(expected_type), item->type_code))); dbms_pipe_incoming_buffer.cursor = item->next; return { item->is_null ? nullptr : item->data, item->data_bytes, item->is_null }; } void dbms_pipe_buffer_reset() { dbms_pipe_buffer_clear_outgoing(); if (dbms_pipe_incoming_buffer.has_retained_message) { dbms_pipe_incoming_buffer.cursor = dbms_pipe_incoming_buffer.head; dbms_pipe_incoming_buffer.state = DbmsPipeReadState::RECEIVED_MESSAGE; } else dbms_pipe_incoming_buffer.state = DbmsPipeReadState::NO_RECEIVED_MESSAGE; }