kubo39's blog

ただの雑記です。

mysql-nativeで一度に受信できるパケットサイズは16777210バイト

コードを読んでもとくにサイズ上限が規定されていなかったので実験した。

  • 環境(mysql-nativeのバージョン)
$ cat dub.selections.json| grep mysql-native
                "mysql-native": "3.0.0",

以下のようにmax_allowed_packetや受信サイズを調整して試した結果、max_allowed_packetのサイズにかかわらず16777210バイトが上限になるようだ。 というわけでいくらmax allowed packet sizeを16MBより大きくしても効果がないので注意する必要がある。

import mysql.connection;
import mysql.commands;

import std.array : array;
import std.range;

void main()
{
    auto conn = new Connection("host=127.0.0.1;port=3307;db=testdb;user=testuser;pwd=testpassword");
    scope(exit) conn.close();
    auto row = conn.queryRow("SELECT REPEAT('A', 16777210)");

    assert(row[0].get!string == 'A'.repeat.take(16_777_210).array);
    //
    // max_allowed_packet = 16MB,24MB
    //
    // OK: 15_728_640 (15MB)
    // OK: 16_777_210 (16MB - 6)
    // NG: 16_777_211
    // NG: 16_777_216 (16MB)
    //
    // 1MB = 1024 * 1024 = 1048576
    // 15MB = 15 * 1024 * 1024 = 15 * 1048576 = 15_728_640
    // 16MB = 16 * 1024 * 1024 = 16 * 1048576 = 16_777_216
}

1バイトでも超えると以下のようなエラーになる。

$ dub run
Performing "debug" build using /home/kubo39/dlang/dmd-2.094.0/linux/bin64/dmd for x86_64.
taggedalgebraic 0.11.18: target for configuration "library" is up to date.
eventcore 0.9.9: target for configuration "epoll" is up to date.
stdx-allocator 2.77.5: target for configuration "library" is up to date.
vibe-core 1.10.3: target for configuration "epoll" is up to date.
mysql-native 3.0.0: target for configuration "library" is up to date.
large-packet ~master: building configuration "application"...
Linking...
To force a rebuild of up-to-date targets, run again with --force.
Running ./large-packet
core.exception.AssertError@../../../../.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/protocol/packet_helpers.d(366): Assertion failure
----------------
??:? _d_assertp [0x56231b799d9d]
../../../../.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/protocol/packet_helpers.d:366 pure nothrow @nogc @safe ubyte[] mysql.protocol.packet_helpers.consume!().consume(ref ubyte[], ulong) [0x56231b6ac06d]
../../../../.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/protocol/packet_helpers.d:723 mysql.protocol.extra_types.SQLValue mysql.protocol.packet_helpers.consumeIfComplete!().consumeIfComplete(ref ubyte[], mysql.protocol.constants.SQLType, bool, bool, ushort) [0x56231b6bfb61]
../../../../.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/protocol/comms.d:673 void mysql.protocol.comms.ctorRow(mysql.connection.Connection, ref ubyte[], mysql.protocol.packets.ResultSetHeaders, bool, out std.variant.VariantN!(32uL).VariantN[], out bool[], out immutable(char)[][]) [0x56231b6bd57b]
../../../../.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/result.d:55 ref mysql.result.Row mysql.result.Row.__ctor(mysql.connection.Connection, ref ubyte[], mysql.protocol.packets.ResultSetHeaders, bool) [0x56231b6af8b1]
../../../../.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/protocol/comms.d:886 mysql.result.Row mysql.protocol.comms.getNextRow(mysql.connection.Connection) [0x56231b6be519]
../../../../.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/result.d:278 void mysql.result.ResultRange.popFront() [0x56231b6aff8f]
../../../../.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/result.d:233 ref mysql.result.ResultRange mysql.result.ResultRange.__ctor(mysql.connection.Connection, mysql.protocol.packets.ResultSetHeaders, immutable(char)[][]) [0x56231b6afe1c]
../../../../.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/commands.d:384 mysql.result.ResultRange mysql.commands.queryImpl(mysql.commands.ColumnSpecialization[], mysql.connection.Connection, mysql.protocol.comms.ExecQueryImplInfo) [0x56231b6b0884]
../../../../.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/commands.d:506 std.typecons.Nullable!(mysql.result.Row).Nullable mysql.commands.queryRowImpl(mysql.commands.ColumnSpecialization[], mysql.connection.Connection, mysql.protocol.comms.ExecQueryImplInfo) [0x56231b69e340]
../../../../.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql/commands.d:453 std.typecons.Nullable!(mysql.result.Row).Nullable mysql.commands.queryRow(mysql.connection.Connection, const(char[]), mysql.commands.ColumnSpecialization[]) [0x56231b69e2cb]
source/app.d:11 _Dmain [0x56231b69d17c]
Program exited with code 1