2016-12-21 7 views
0

から取得した値は、いくつかのリファクタリングの後、私たちは突然、実行時にこの出来事を見たときに:スカラにStackOverflowError地図

java.lang.StackOverflowError: null 
at scala.collection.MapLike$MappedValues.get(MapLike.scala:249) 
at scala.collection.MapLike$MappedValues.get(MapLike.scala:249) 
at scala.collection.MapLike$MappedValues.get(MapLike.scala:249) 
at scala.collection.MapLike$MappedValues.get(MapLike.scala:249) 
at scala.collection.MapLike$MappedValues.get(MapLike.scala:249) 

我々は同様の問題を発見したが、それらのどれもが、まさにこのトレースがありませんでした:

答えて

3

上記の問題点怠け者はMapLike.mapValuesであり、さらに調査したところ、原因がわかりました。 here

を説明するように

case class EvictableValue(value: String, evictionTime: Instant) 

    val startData = Map(
    "node1" -> Map(
     "test" -> EvictableValue("bar", Instant.now().plusSeconds(1000)) 
    ) 
) 

    // every n seconds we do the below code 
    // here simulated by the fold 
    val newData = (1 to 20000).foldLeft(startData){(data, _) => 
    data.mapValues { value => 
     value.filter(_._2.evictionTime.isBefore(Instant.now())) 
    } 
    } 

    // this stack overflows 
    val result = newData.get("test") 

ソリューションは、ビューを強制的にMap.transform

data.transform { (_, value) => 
    value.filter(_._2.evictionTime.isBefore(Instant.now())) 
} 

かに切り替えることでした:

は、我々は定期的に呼ばれ、このような何かをしているいくつかのクリーンアップコードを持っています

data.mapValues{ value => 
    value.filter(_._2.evictionTime.isBefore(Instant.now())) 
}.view.force