TLDR : Is there any known race condition issues/co...
# troubleshooting
b
TLDR : Is there any known race condition issues/concurrency bugs in flatMap ? I am noticing an odd behavior in flink code ( using 1.16 version) :
Copy code
class ExtractDeviceLogLineFlatMap extends FlatMapFunction[String, (String, String)] {
    private val LOG = LoggerFactory.getLogger(getClass.getName)
    override def flatMap(line: String, out: Collector[(String, String)]): Unit = {
      val mapper = new ObjectMapper()
      mapper.registerModule(DefaultScalaModule)
      val data = mapper.readValue(line, classOf[Data])
      if (data == null) {
        LOG.error("Null data")
        out.collect(("", ""))
        return
      }
      val stsFormatted = getFormattedSts(data.sts)
      val logCreateTime = LocalDateTime.parse(stsFormatted, java.time.format.DateTimeFormatter.ofPattern("yyyyMMddHHmmss")).toEpochSecond(java.time.ZoneOffset.UTC)
      LOG.info("Got sts : " + data.sts + "Formatted sts : " + stsFormatted + "logCreateTime : " + logCreateTime)
      ...

val PAYLOAD_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"
val STS_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss")
def getFormattedSts(stsString: String): String = {
      var stsDate: Date = null
      try {
        stsDate = new SimpleDateFormat(PAYLOAD_TIME_FORMAT).parse(stsString)
      } catch {
        case e: Exception =>
          logger.error(s"failed to parse upload time: ${stsString} with error: ", e)
          System.exit(1)
          throw e
      }
      STS_FORMAT.format(stsDate)
    }
  }
I use this flatMap in code as:
Copy code
val body: DataStream[(String, String)] =
    dataStream.flatMap(new ExtractDeviceLogLineFlatMap)
      .filter(!_._2.isEmpty)
The function getFormattedSts is returning wrong values, only sometimes. Specifically I tracked down to an instance where it returned the previously computed value when function was last called. the logs below: There was one entry with sts : 2024-03-18T115630Z but flink gave 1710763071 instead of 1710762990
Copy code
2024-03-18 18:55:14,533 INFO  com.roku.dea.StreamFromGcs$ExtractDeviceLogLineFlatMap       [] - Got sts : 2024-03-18T11:56:30ZFormatted sts : 20240318115630logCreateTime : 1710762990
2024-03-18 18:55:14,533 INFO  com.roku.dea.StreamFromGcs$ExtractDeviceLogLineFlatMap       [] - Got sts : 2024-03-18T11:57:51ZFormatted sts : 20240318115751logCreateTime : 1710763071
2024-03-18 18:55:14,534 INFO  com.roku.dea.StreamFromGcs$ExtractDeviceLogLineFlatMap       [] - Got sts : 2024-03-18T11:57:51ZFormatted sts : 20240318115751logCreateTime : 1710763071
2024-03-18 18:55:14,534 INFO  com.roku.dea.StreamFromGcs$ExtractDeviceLogLineFlatMap       [] - Got sts : 2024-03-18T11:56:30ZFormatted sts : 20240318115751logCreateTime : 1710763071
2024-03-18 18:55:14,534 INFO  com.roku.dea.StreamFromGcs$ExtractDeviceLogLineFlatMap       [] - Got sts : 2024-03-18T11:57:51ZFormatted sts : 20240318115751logCreateTime : 1710763071
In the logs, you can see in the second last one, it says Got sts : 2024-03-18T115630ZFormatted sts : 20240318115751logCreateTime : 1710763071 Clearly, it gave the formattedSts value as what was computed in the previous call. (you can see it did correctly in first log line, and returned different result in the 4th line for the same input).
Figured out that this was a problem with SimpleDateFormat class. Looks like that class is not thread-safe inherently. Instead using
LocalDateTime.parse
helped me fix this problem