Uploaded image for project: 'S2Graph'
  1. S2Graph
  2. S2GRAPH-257

SafeUpdateCache#withCache method incorrect behavior

    XMLWordPrintableJSON

Details

    • Bug
    • Status: To Do
    • Major
    • Resolution: Unresolved
    • 0.1.0, 0.2.0
    • None
    • s2core
    • None

    Description

      The SafeUpdateCache Class needs to change the value in another thread when requesting the value from the cache instance.

      With the above operation, the current thread should not be blocked.

      However, due to the bug in the line below, the lazy value is evaluated unintentionally, causing the forground thread to block.

       

      https://github.com/apache/incubator-s2graph/blob/07a5af39a5a52aad2e61c34d685fe00a6bb8e2cc/s2core/src/main/scala/org/apache/s2graph/core/utils/SafeUpdateCache.scala#L158

       

       

      def withCache[T <: AnyRef](key: String,
                                   broadcast: Boolean,
                                   cacheTTLInSecs: Option[Int] = None,
                                   onEvict: AnyRef => Unit = defaultOnEvict)(op: => T): T = {
      ...
      ...
              if (running) cachedVal
              else {
                val value = op // Bug on this line: value must be declared as lazy val
                Future(value)(executionContext) onComplete {
                  case Failure(ex) =>
                    put(key, cachedVal, false)
                    logger.error(s"withCache update failed: $cacheKey", ex)
                  case Success(newValue) =>
      
                    put(key, newValue, broadcast = (broadcast && newValue != cachedVal))
                    onEvict(cachedVal)
      
                    cachedVal match {
                      case None =>
                      case _ => logger.info(s"withCache update success: $cacheKey")
                    }
                }
      
                cachedVal
              }
            }
          }
        }
      

       

       

       

      Attachments

        Activity

          People

            daewon Daewon Jeong
            daewon Daewon Jeong
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: