Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io.getquill.quat.Quat memory leak #2735

Closed
coolcrazycool opened this issue Apr 24, 2023 · 2 comments
Closed

io.getquill.quat.Quat memory leak #2735

coolcrazycool opened this issue Apr 24, 2023 · 2 comments

Comments

@coolcrazycool
Copy link

Version: (4.6.0)
Module: (quill-zio, quill-jdbc-zio)
Database: (postgresql)

Actual behavior

Trying to create backend with Quill.
But when I done it, get memory leaks. My G1 Old Gen heap zone goes up.
When I get memory dump, I see so many LinkedHashMap. Around 7millions for 5000 API requests:
Pasted Graphic

Inside of this objects I have io.getquill.quat.Quat objects implementations. For all of them

My Code Samples

import io.getquill._
import zio.{ULayer, ZIO, ZLayer}

import java.sql.SQLException
import java.util.UUID
import javax.sql.DataSource

// Entity

val sourceSchema: Quoted[EntityQuery[SourceEntity]] = quote {
    querySchema[SourceEntity](""""datalineage"."Source"""")
}

// Repository

class SourceRepository {
  import QuillContext._

  def getOrInsert(
      sourceTypeId: Int,
      sourceData: Source,
      clusterId: Int
  ): ZIO[DataSource, Throwable, SourceEntity] =
    run(findByParameters(data = Some(sourceData), clusterId = Some(clusterId)))
      .map(_.headOption)
      .flatMap {
        case Some(source) => ZIO.succeed(source)
        case None         => insert(sourceTypeId, sourceData, clusterId)
      }

  def findByParameters(
      sourceId: Option[Long] = Option.empty[Long],
      data: Option[Source] = Option.empty[Source],
      time: Option[Long] = Option.empty[Long],
      clusterId: Option[Int] = Option.empty[Int]
  ): Quoted[EntityQuery[SourceEntity]] = {
    val filterData = data.map(_.asJson.noSpaces)
    sourceSchema.filter(s =>
      lift(sourceId).map(_ == s.sourceId).forall(t => t) &&
        lift(filterData).map(_ == s.data).forall(t => t) &&
        lift(time).forall(_ >= s.addTime) &&
        lift(clusterId).forall(_ == s.clusterId)
    )
  }

  def insert(
      sourceTypeId: Int,
      sourceData: Source,
      clusterId: Int
  ): ZIO[DataSource, SQLException, SourceEntity] =
    run(
      sourceSchema
        .insert(
          _.sourceTypeId -> lift(sourceTypeId),
          _.data         -> lift(sourceData.asJson.noSpaces),
          _.clusterId    -> lift(clusterId)
        )
        .returning(r => SourceEntity(r.sourceId, r.sourceTypeId, r.data, r.clusterId, r.addTime))
    )
}


// Service


  def get(
      time: Option[Long],
      cluster: Option[String]
  ): Quoted[Query[(Long, SourceEntity, SourceTypeEntity, ClusterEntity)]] =
    quote(for {
      source <- sourceRepository
        .findByParameters(time = time)
      cluster <- clusterRepository
        .get(cluster)
        .join(cluster => source.clusterId == cluster.clusterId)
      sourceType <- sourceTypeRepository
        .get()
        .join(sourceType => sourceType.sourceTypeId == source.sourceTypeId)
    } yield (source.sourceId, source, sourceType, cluster))

  def findBySourceParameters(
      time: Option[Long] = Option.empty[Long],
      cluster: Option[String] = Option.empty[String],
      tp: Option[String] = Option.empty[String]
  ): IO[Throwable, Seq[ReadSource]] =
    (for {
      source <- transaction(
        run(get(time, cluster))
      )
      filteredSource <- ZIO.filter(source)(s => ZIO.attempt(tp.forall(s._3.sourceTypeName == _)))
      mappedSource <- ZIO.foreach(filteredSource) {
        case (_, source, tp, _) =>
          ZIO.attempt(
            ReadSource(
              tp.sourceTypeName,
              source.decodedData
            )
          )
      }

    } yield mappedSource).provideEnvironment(ZEnvironment(dataSource))

@getquill/maintainers

@wb14123
Copy link
Contributor

wb14123 commented Oct 1, 2023

This should be fixed by #2878

@guizmaii
Copy link
Member

guizmaii commented Oct 2, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants