Akka Stream TCP + Akka Stream Kafka制作人不停止不发布消息而不是错误输出

时间:2016-12-14 05:56:58

标签: scala akka akka-stream akka-kafka

我有以下信息流:

Source(IndexedSeq(ByteString.empty))
.via(
  Tcp().outgoingConnection(bsAddress, bsPort)
    .via(Framing.delimiter(ByteString("\n"), 256, allowTruncation = true))
    .map(_.utf8String)
)
.map(m => new ProducerRecord[Array[Byte], String](kafkaTopic, m))
.runWith(
  Producer.plainSink(
    ProducerSettings(system, new ByteArraySerializer, new StringSerializer)
      .withBootstrapServers(s"${kafkaAddress}:${kafkaPort}")
  )
).onComplete {
    case Success(Done) => printAndByeBye("Stream ends successfully")
    case Failure(ex) => printAndByeBye("Stream ends with an error: " + ex.toString)
  }

它可以正常工作一段时间,我可以使用填充在Kafka主题上的消息。但有时候,显然是在一个随机的间隔,没有更多的消息发布,这个代码没有记录任何错误(printAndByeBye将打印传递的消息并终止actor系统。)重新启动应用程序后,消息继续流动。

关于如何知道这里发生了什么的任何想法?

编辑:我把Kamon放在上面,我可以看到以下行为:

Mailbox Size per Actor

Time in Mailbox per Actor

Processing Time per Actor

看起来有些东西在没有通知流停止的情况下停止了,但我不知道如何使其显式并停止流。

3 个答案:

答案 0 :(得分:0)

我建议创建具有监督属性的流来处理TCP连接中可能的异常,如:

val flow = 
    Tcp().outgoingConnection("", 12)
          .via(Framing.delimiter(ByteString("\n"), 256, allowTruncation = true))
          .map(_.utf8String).withAttributes(ActorAttributes.supervisionStrategy {
      case ex: Throwable =>
        println("Error ocurred: " + ex)
        Supervision.Resume
     }

Source(IndexedSeq(ByteString.empty))
.via(flow)
.map(m => new ProducerRecord[Array[Byte], String](kafkaTopic, m))
.runWith(
  Producer.plainSink(
    ProducerSettings(system, new ByteArraySerializer, new StringSerializer)
      .withBootstrapServers(s"${kafkaAddress}:${kafkaPort}")
  )
).onComplete {
    case Success(Done) => printAndByeBye("Stream ends successfully")
    case Failure(ex) => printAndByeBye("Stream ends with an error: " + ex.toString)
  }

如果流有任何错误,则流停止。使用此配置,您将查看流是否已抛出任何异常。

答案 1 :(得分:0)

如果一切都变得沉默, 可以降低到某个地方的背压。 尝试并选择性地更换具有非背压感知阶段的背压感知阶段,并检查问题是否仍然存在。 在您的情况下,有两种可能的背压源:

1)TCP连接

你可以尝试将无限的ByteString来源附加到Kafka,做一些类似的事情:

Source.cycle(() => List(???).iterator)
.map(m => new ProducerRecord[Array[Byte], String](kafkaTopic, m))
.runWith(
  Producer.plainSink(
    ProducerSettings(system, new ByteArraySerializer, new StringSerializer)
      .withBootstrapServers(s"${kafkaAddress}:${kafkaPort}")
  )
).onComplete {
    case Success(Done) => printAndByeBye("Stream ends successfully")
    case Failure(ex) => printAndByeBye("Stream ends with an error: " + ex.toString)
  }

2)卡夫卡水槽

将其替换为一些记录

Source(IndexedSeq(ByteString.empty))
.via(
  Tcp().outgoingConnection(bsAddress, bsPort)
    .via(Framing.delimiter(ByteString("\n"), 256, allowTruncation = true))
    .map(_.utf8String)
)
.map(m => new ProducerRecord[Array[Byte], String](kafkaTopic, m))
.runForeach(println)
.onComplete {
    case Success(Done) => printAndByeBye("Stream ends successfully")
    case Failure(ex) => printAndByeBye("Stream ends with an error: " + ex.toString)
  }

你能在2个案例中只有一个看到问题吗?同时?没有?

答案 2 :(得分:0)

流没有失败,但是TCP流空闲,因为设备发布数据在一段时间后停止发送数据而不丢弃连接。 而不是使用更简单的:

TCP().outgoingConnection(bsAddress, bsPort)

我最终使用:

def outgoingConnection(
remoteAddress:  InetSocketAddress,
localAddress:   Option[InetSocketAddress]           = None,
options:        immutable.Traversable[SocketOption] = Nil,
halfClose:      Boolean                             = true,
connectTimeout: Duration                            = Duration.Inf,
idleTimeout:    Duration                            = Duration.Inf): Flow[ByteString, ByteString, Future[OutgoingConnection]] = ???

所以

Tcp().outgoingConnection(bsAddress, bsPort)

成了

val connectTimeout: Duration = 1 second
val idleTimeout: Duration = 2 second
Tcp().outgoingConnection(
    remoteAddress = InetSocketAddress.createUnresolved(bsAddress, bsPort),
    connectTimeout = connectTimeout,
    idleTimeout = idleTimeout
  )

通过通知idleTimeout,跟随开始失败,可以重新启动另一个流程。