shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

Firebird Proxy does not enforce a maximum buffered BLOB write size (isc_blobtoobig)

Open rishi-rana opened this issue 2 months ago • 5 comments

Split off from #39052 at makssent's suggestion (https://github.com/apache/shardingsphere/issues/39052#issuecomment-5581713843), since this needs a program-logic change rather than just wiring an existing signal to an error response.

Bug Report

Expected behavior

Once a client-uploaded BLOB (via op_put_segment / op_batch_segments) exceeds some maximum buffered size, further segments for that BLOB should keep failing with isc_blobtoobig instead of being silently accepted.

Actual behavior

FirebirdBlobWrite buffers an in-flight BLOB write into an unbounded ByteArrayOutputStream (proxy/frontend/dialect/firebird/.../command/query/blob/cache/FirebirdBlobWrite.java). There is no size tracking or limit anywhere in the BLOB write path, so a client can buffer an arbitrarily large BLOB into proxy heap memory before it is ever routed to a backend.

Why this needs its own issue

Unlike the other four sub-cases in #39052, this one isn't "surface an error the cache layer already detects" — no such signal exists yet. It needs:

  • A decision on where the limit comes from (a hardcoded constant vs. a configurable proxy property), since Firebird itself has no fixed protocol-level BLOB size ceiling — this would be a ShardingSphere-proxy-specific safety guard against unbounded heap buffering.
  • Once decided, tracking buffered size against that limit in FirebirdBlobWrite/FirebirdBlobWriteCache, and returning isc_blobtoobig (already scaffolded via the #38927 error-handling pattern) from FirebirdPutBlobSegmentCommandExecutor/FirebirdBatchBlobSegmentsCommandExecutor once exceeded, with segments received after the limit continuing to fail rather than being accepted again.

Related

  • #39052 (parent issue, now scoped to the other 4 sub-cases)
  • #38927 (established the Firebird dialect error-handling pattern this would extend)

rishi-rana avatar Sep 08 '26 16:09 rishi-rana

I'd like to work on this issue. @makssent can you please tell me if its going to be a hardcoded value or a configurable property?

bhaskars2904 avatar Sep 23 '26 23:09 bhaskars2904

I'd like to work on this issue. @makssent can you please tell me if its going to be a hardcoded value or a configurable property?

Hi! You can find the expected behavior and the required value in the Firebird source code: https://github.com/FirebirdSQL/firebird

makssent avatar Sep 24 '26 06:09 makssent

You can get a basic understanding of how the server works from the Firebird Wire Protocol documentation.

For the complete and more detailed implementation, you can refer to the Firebird source code.

makssent avatar Sep 24 '26 06:09 makssent

@makssent thank you for providing the links to the Firebird docs and code. I have following analysis for the issue:

Firebird protocol: The firebird protocol states that an op_put_segment or a op_batch_segment are commands that are used to upload the blob data broken into segments by the client. The bytes reserved for the total length of the segment is 2byte (Firebird 3.0 and higher). This implies that the maximum size of the buffer is 2^16-1 i.e. 65535 bytes (64 KB). This is the only limit and it applies to one segment. If the file is larger then there are multiple segments used where the op_batch_segment command provides better performance than op_put_segment command.

Sharding Sphere proxy: The FirebirdPutBlobSegmentCommandExecutor and FirebirdBatchBlobSegmentsCommandExecutor simply appends the segments into the FirebirdBlobWriteCache in-memory buffer without writing them to the Firebird server. This is a completely different mechanism where the segments are held in-memory before writing them to the server. Hence the Firebird wire protocol can't be used as a reference for imposing the limits on the in-memory buffer.

Conclusion : This leaves us with 2 choices - 1) To impose a limit on the in-memory buffer which is dictated by Sharding Sphere (a not so good idea imo) or 2) To not buffer the segments in-memory but write them to the server on the execution of every command the way its being done by the Firebird client and server.

@makssent please review and provide your suggestions on the approach that is right to be taken here.

bhaskars2904 avatar Sep 28 '26 09:09 bhaskars2904

@makssent please review and provide your suggestions on the approach that is right to be taken here.

I think both approaches are possible, and it depends on the scope you want to take here.

If you want to keep the current ShardingSphere implementation and only fix the issue, you can introduce a limit for the in-memory buffer. In that case, however, this should be treated as a ShardingSphere-specific limitation, not as the Firebird 65535-byte protocol limit, because Firebird applies that limit to a single segment, not to the whole BLOB.

If you want to improve the implementation itself and make ShardingSphere Proxy behave closer to Firebird, then the second approach makes more sense: process/write BLOB segments incrementally instead of buffering the whole BLOB in memory.

So I think the choice mainly depends on whether you want to make a small fix within the current implementation or change the BLOB handling implementation itself.

makssent avatar Sep 28 '26 13:09 makssent