Connection getting stale when using mysql jdbc catalog
Apache Iceberg version
1.9.2
Query engine
Trino
Please describe the bug 🐞
Hello Iceberg Community,
We have recently switched jdbc catalog from postgresql to mysql. This meant for us to ensure that trino and kafka connector have the mysql driver on classpath, to be able to connect to it. This initially worked good. But after a while we noticed that we are having exceptions from mysql driver, like this:
Caused by: org.apache.iceberg.jdbc.UncheckedSQLException: Failed to execute exists query: SELECT table_namespace FROM iceberg_tables WHERE catalog_name = ? AND (table_namespace = ? OR table_namespace LIKE ? ESCAPE '!') LIMIT 1
at org.apache.iceberg.jdbc.JdbcUtil.exists(JdbcUtil.java:828)
at org.apache.iceberg.jdbc.JdbcUtil.namespaceExists(JdbcUtil.java:796)
at org.apache.iceberg.jdbc.JdbcCatalog.namespaceExists(JdbcCatalog.java:606)
at io.trino.plugin.iceberg.catalog.jdbc.TrinoJdbcCatalog.namespaceExists(TrinoJdbcCatalog.java:127)
at io.trino.plugin.iceberg.catalog.jdbc.TrinoJdbcCatalog.listNamespaces(TrinoJdbcCatalog.java:273)
at io.trino.plugin.iceberg.catalog.jdbc.TrinoJdbcCatalog.listTables(TrinoJdbcCatalog.java:183)
at io.trino.plugin.iceberg.IcebergMetadata.getRelationTypes(IcebergMetadata.java:953)
at io.trino.plugin.base.classloader.ClassLoaderSafeConnectorMetadata.getRelationTypes(ClassLoaderSafeConnectorMetadata.java:297)
at io.trino.tracing.TracingConnectorMetadata.getRelationTypes(TracingConnectorMetadata.java:269)
at io.trino.metadata.MetadataManager.getRelationTypes(MetadataManager.java:618)
at io.trino.tracing.TracingMetadata.getRelationTypes(TracingMetadata.java:331)
at io.trino.metadata.MetadataListing.doGetRelationTypes(MetadataListing.java:150)
at io.trino.metadata.MetadataListing.getRelationTypes(MetadataListing.java:141)
... 46 more
Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet successfully received from the server was 8,105,461 milliseconds ago. The last packet sent successfully to the server was 8,105,461 milliseconds ago.
After some digging, it turned out, that mysql server is killing idle connections after some time (usually hours (see here), default 8 hours) and than when client tries to reuse the connection, the exceptions is thrown. Postgres, by default, doesn't do this (see here). Now, I've checked how iceberg handels jdbc connections and I've found out here, that each time connection is needed, first one from pool is taken, used and returned back to first position. Default pool size is 2 (here). In practice (at least in our setup) this means that one connection is being reused (almost) all the time (borrowed -> returned, borrowed -> returned and so on), while the second connection is sitting there without being used - but eventually it can get picked up and exception is thrown.
With kafka, we could set the pool size to 1 (using this property) and the problem was solved.
With trino, however, it is not possible to pass this property down to iceberg - they had a ticket to do that, but they added only passing of property retryable_status_codes, so I cannot change the pool size.
My question is - why is pooling done in this way - that (almost) always the one connection is being reused? Why are they not rotated? That would keep them both active in the eyes of mysql server (or any server caring about active/idle connections). Does something speak against rotating them?
Regards, b4sus
Willingness to contribute
- [x] I can contribute a fix for this bug independently
- [ ] I would be willing to contribute a fix for this bug with guidance from the Iceberg community
- [ ] I cannot contribute a fix for this bug at this time
I think it's just simpler to implement. Is it possible to catch such an exception and create a new client?
We are getting this error using trino (rino is using iceberg jar), so we cannot catch anything directly. We have to rerun the query/job which failed and the connection will be recreated.
To implementation - as far as I can see, the change would be simply changing the release method (see here) from clients.addFirst to clients.addLast - that should cause the connections to rotate nicely.
This issue has been automatically marked as stale because it has been open for 180 days with no activity. It will be closed in next 14 days if no further activity occurs. To permanently prevent this issue from being considered stale, add the label 'not-stale', but commenting on the issue is preferred when possible.
This issue has been closed because it has not received any activity in the last 14 days since being marked as 'stale'