Skip to content

mysql: Leaking of prepared statements when using opentelemetry #18975

Open

Description

@binary64

Bug description

We started to experience this error in production:

ServerError { code: 1461, message: "Can't create more than max_prepared_stmt_count statements (current value: 16382)", state: "42000" }

How to reproduce

git clone [email protected]:binary64/prisma-prepared-statements.git
cd prisma-prepared-statements
pnpm i
pnpm test

Expected behavior

The number of mysql prepared statements should remain at 1.

Prisma information

(this info on my repo)

Environment & setup

Node 18

Prisma Version

Latest

Activity

  1. added
    kind/bug
    on Apr 27, 2023
  2. janpio commented on Apr 27, 2023

    @janpio
    Contributor

    What is the influence of OpenTelemetry?
    Why do you expect the number of prepared statements to stay at 1?

  3. binary64 commented on Apr 27, 2023

    @binary64
    Author

    One side-issue in relation to this is the actual limit if you don't specify the statement_cache_size seems to be 700, but the documentation states it should be 100.

  4. binary64 commented on Apr 27, 2023

    @binary64
    Author

    I'm not aware of the intracises of passing comments to a prepared statement in terms of what is a cache hit or not, so assuming that it's not possible to pass the span id to a prepared statement as an argument (and therefore operating the cache at a high hit-ratio), for DX purposes it might have saved my production server from outage by having statement_cache_size somehow default to 0 (disabled) whenever opentelemetry is active.

  5. binary64 commented on Apr 27, 2023

    @binary64
    Author

    The output I was seeing from the mysql general_log was:

    <SNIP>.`users`.`last_login` DESC LIMIT ? OFFSET ? /* traceparent=00-9ac81127dd948e028f37fdab4f14a21d-e130a242c3b57adb-01 */
     412 Execute  SELECT <SNIP>.`users`.`last_login` DESC LIMIT 50 OFFSET 0 /* traceparent=00-9ac81127dd948e028f37fdab4f14a21d-e130a242c3b57adb-01 */
     412 Close stmt
     412 Prepare  SELECT <SNIP>.`users`.`last_login` DESC LIMIT ? OFFSET ? /* traceparent=00-9ac81127dd948e028f37fdab4f14a21d-2e8f4cc71ec166e9-01 */
     412 Execute  SELECT <SNIP>.`users`.`last_login` DESC LIMIT 50 OFFSET 0 /* traceparent=00-9ac81127dd948e028f37fdab4f14a21d-2e8f4cc71ec166e9-01 */
    

    The hash changes every query

  6. jkomyno commented on Apr 28, 2023

    @jkomyno
    Contributor

    Internal: this other issue could shed light on what OP is reporting.

  7. added
    bug/0-unknown
    on Apr 28, 2023
  8. binary64 commented on Apr 28, 2023

    @binary64
    Author

    Upon reflecting this would appear to be an impossible technical task - MySQL doesn't seem to support ? as a placeholder in comments. This makes sense.

    What doesn't make sense is the combination of Prisma defaults along with the sample code for opentelemtry tracing. On a DX level, my suggestions are:

    1. Add a big warning to the opentracing code to state this is not suitable for production without some extra considerations.
    2. A bit hacky I admit.. but Prisma could recognise that @prisma/instrumentation is modifying the SQL query and to adjust the internal fallback default for statement_cache_size to 0. This is because we know for sure that every single query will be unique, busting the whole benefit of cache.

    The only idea I have is could the tracing comment be attached to the CALL statement instead of the PREPARE STATEMENT statement? Then you could strip the comment before adding into the cache. If this somehow works, my PoC would show max "1" stored procedure on the server at all times.

  9. added
    bug/1-unconfirmed
    and removed
    bug/0-unknown
    on Jul 13, 2023
  10. SevInf commented on Aug 4, 2023

    @SevInf
    Contributor

    Still happens on Prisma 5.1.1. Smaller reproduction:

    import { PrismaClient } from '@prisma/client'
    import { registerInstrumentations } from '@opentelemetry/instrumentation'
    import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
    import { PrismaInstrumentation } from '@prisma/instrumentation'
    
    const provider = new NodeTracerProvider()
    
    registerInstrumentations({
      tracerProvider: provider,
      instrumentations: [new PrismaInstrumentation()],
    })
    
    provider.register()
    
    async function main() {
      const prisma = new PrismaClient()
    
      for (let i = 0; i < 10; i++) {
        await prisma.user.findMany()
        console.log(await prisma.$queryRaw`SHOW GLOBAL STATUS LIKE '%prepared_stmt_count%';`)
      }
    }
    
    main()

    Test performs prisma query and outputs total number of prepared queries:

    [ { Variable_name: 'Prepared_stmt_count', Value: '2' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '3' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '4' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '5' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '6' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '7' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '8' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '9' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '10' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '11' } ]

    One can see that number of prepared statements grows with each iterations.
    If we comment out proveder.register() or new PrismaInstrumentation() line, output stays consistent:

    [ { Variable_name: 'Prepared_stmt_count', Value: '2' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '2' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '2' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '2' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '2' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '2' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '2' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '2' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '2' } ]
    [ { Variable_name: 'Prepared_stmt_count', Value: '2' } ]

    Note that it it happens even if tracing preview feature is not enabled.

  11. added
    bug/2-confirmed
    and removed
    bug/1-unconfirmed
    on Aug 4, 2023
  12. changed the title [-]Leaking of prepared statements when using opentelemetry[/-] [+]mysql: Leaking of prepared statements when using opentelemetry[/+] on Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/2-confirmedkind/bugtopic: max_prepared_stmt_counttopic: mysqltopic: tracing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions