Akka流CPU利用率高,创建的线程太多

Akka流CPU利用率高,创建的线程太多,akka,cpu,akka-stream,Akka,Cpu,Akka Stream,我有一个消费者使用akka streams,我们正在使用来自kafka的数据包。线程的数量有几千个,并且一直在增加 尝试进行线程转储,发现有两种线程具有最大计数: 下面是配置文件 akka.kafka.consumer { # Tuning property of scheduled polls. # Controls the interval from one scheduled poll to the next. poll-interval = 250ms # Tuning

我有一个消费者使用akka streams,我们正在使用来自kafka的数据包。线程的数量有几千个,并且一直在增加

尝试进行线程转储,发现有两种线程具有最大计数:

下面是配置文件

akka.kafka.consumer {
  # Tuning property of scheduled polls.
  # Controls the interval from one scheduled poll to the next.
  poll-interval = 250ms

  # Tuning property of the `KafkaConsumer.poll` parameter.
  # Note that non-zero value means that the thread that
  # is executing the stage will be blocked. See also the `wakup-timeout` setting below.
  poll-timeout = 50ms

  # The stage will delay stopping the internal actor to allow processing of
  # messages already in the stream (required for successful committing).
  # Prefer use of `DrainingControl` over a large stop-timeout.
  stop-timeout = 30s

  # Duration to wait for `KafkaConsumer.close` to finish.
  close-timeout = 20s

  # If offset commit requests are not completed within this timeout
  # the returned Future is completed `CommitTimeoutException`.
  # The `Transactional.source` waits this amount of time for the producer to mark messages as not
  # being in flight anymore as well as waiting for messages to drain, when rebalance is triggered.
  commit-timeout = 15s

  # If commits take longer than this time a warning is logged
  commit-time-warning = 1s

  # Not used anymore (since 1.0-RC1)
  # wakeup-timeout = 3s

  # Not used anymore (since 1.0-RC1)
  # max-wakeups = 10

  # If set to a finite duration, the consumer will re-send the last committed offsets periodically
  # for all assigned partitions. See https://issues.apache.org/jira/browse/KAFKA-4682.
  commit-refresh-interval = infinite

  # Not used anymore (since 1.0-RC1)
  # wakeup-debug = true

  # Fully qualified config path which holds the dispatcher configuration
  # to be used by the KafkaConsumerActor. Some blocking may occur.
  use-dispatcher = "akka.default-dispatcher"

  # Properties defined by org.apache.kafka.clients.consumer.ConsumerConfig
  # can be defined in this configuration section.
  kafka-clients {
    # Disable auto-commit by default
    enable.auto.commit = false
  }

  # Time to wait for pending requests when a partition is closed
  wait-close-partition = 500ms

  # Limits the query to Kafka for a topic's position
  position-timeout = 5s

  # When using `AssignmentOffsetsForTimes` subscriptions: timeout for the
  # call to Kafka's API
  offset-for-times-timeout = 5s

  # Timeout for akka.kafka.Metadata requests
  # This value is used instead of Kafka's default from `default.api.timeout.ms`
  # which is 1 minute.
  metadata-request-timeout = 5s

  # Interval for checking that transaction was completed before closing the consumer.
  # Used in the transactional flow for exactly-once-semantics processing.
  eos-draining-check-interval = 30ms
}


#####################################
# Akka Stream Reference Config File #
#####################################

akka {
  stream {

    # Default materializer settings
    materializer {

      # Initial size of buffers used in stream elements
      initial-input-buffer-size = 4
      # Maximum size of buffers used in stream elements
      max-input-buffer-size = 8

      # Fully qualified config path which holds the dispatcher configuration
      # to be used by ActorMaterializer when creating Actors.
      # When this value is left empty, the default-dispatcher will be used.
      dispatcher = "akka.default-dispatcher"

      # Cleanup leaked publishers and subscribers when they are not used within a given
      # deadline
      subscription-timeout {
        # when the subscription timeout is reached one of the following strategies on
        # the "stale" publisher:
        # cancel - cancel it (via `onError` or subscribing to the publisher and
        #          `cancel()`ing the subscription right away
        # warn   - log a warning statement about the stale element (then drop the
        #          reference to it)
        # noop   - do nothing (not recommended)
        mode = cancel

        # time after which a subscriber / publisher is considered stale and eligible
        # for cancelation (see `akka.stream.subscription-timeout.mode`)
        timeout = 5s
      }

      # Enable additional troubleshooting logging at DEBUG log level
      debug-logging = off

      # Maximum number of elements emitted in batch if downstream signals large demand
      output-burst-limit = 1000

      # Enable automatic fusing of all graphs that are run. For short-lived streams
      # this may cause an initial runtime overhead, but most of the time fusing is
      # desirable since it reduces the number of Actors that are created.
      # Deprecated, since Akka 2.5.0, setting does not have any effect.
      auto-fusing = on

      # Those stream elements which have explicit buffers (like mapAsync, mapAsyncUnordered,
      # buffer, flatMapMerge, Source.actorRef, Source.queue, etc.) will preallocate a fixed
      # buffer upon stream materialization if the requested buffer size is less than this
      # configuration parameter. The default is very high because failing early is better
      # than failing under load.
      #
      # Buffers sized larger than this will dynamically grow/shrink and consume more memory
      # per element than the fixed size buffers.
      max-fixed-buffer-size = 1000000000

      # Maximum number of sync messages that actor can process for stream to substream communication.
      # Parameter allows to interrupt synchronous processing to get upsteam/downstream messages.
      # Allows to accelerate message processing that happening withing same actor but keep system responsive.
      sync-processing-limit = 1000

      debug {
        # Enables the fuzzing mode which increases the chance of race conditions
        # by aggressively reordering events and making certain operations more
        # concurrent than usual.
        # This setting is for testing purposes, NEVER enable this in a production
        # environment!
        # To get the best results, try combining this setting with a throughput
        # of 1 on the corresponding dispatchers.
        fuzzing-mode = off
      }
    }

    # Fully qualified config path which holds the dispatcher configuration
    # to be used by ActorMaterializer when creating Actors for IO operations,
    # such as FileSource, FileSink and others.
    blocking-io-dispatcher = "akka.stream.default-blocking-io-dispatcher"

    default-blocking-io-dispatcher {
      type = "Dispatcher"
      executor = "thread-pool-executor"
      throughput = 1

      thread-pool-executor {
        fixed-pool-size = 16
      }
    }
  }

  # configure overrides to ssl-configuration here (to be used by akka-streams, and akka-http – i.e. when serving https connections)
  ssl-config {
    protocol = "TLSv1.2"
  }
}


ssl-config {
  logger = "com.typesafe.sslconfig.akka.util.AkkaLoggerBridge"
}

akka.default-dispatcher {
  type = "Dispatcher"

  executor = "thread-pool-executor"

  thread-pool-executor {
    # minimum number of threads to cap factor-based core number to
    core-pool-size-min = 5
    # No of core threads ... ceil(available processors * factor)
    core-pool-size-factor = 2.0
    # maximum number of threads to cap factor-based number to
    core-pool-size-max = 20
  }
  # Throughput defines the maximum number of messages to be
  # processed per actor before the thread jumps to the next actor.
  # Set to 1 for as fair as possible.
  throughput = 5
}
以下是最大线程数的错误:

一是:

优先级:5-线程ID:0x00007f9414008800-nativeId:0x5575-nativeId(十进制):21877-状态:等待 堆栈跟踪: java.lang.Thread.State:等待(停车) 在sun.misc.Unsafe.park(本机方法) -停车等待(akka.dispatch.ForkJoinExecutor配置程序$AkkaForkJoinPool) 位于akka.dispatch.forkjoin.ForkJoinPool.scan(ForkJoinPool.java:2075) 位于akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) 在akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) 锁定可拥有的同步器:

另一个是:

默认调度程序-1 优先级:5-线程ID:0x00007f947805d800-nativeId:0x638a-nativeId(十进制):25482-状态:TIMED_WAITING 堆栈跟踪: java.lang.Thread.State:定时等待(休眠) 位于java.lang.Thread.sleep(本机方法) 在akka.actor.lightArrayRevolversScheduler.waitNanos(lightArrayRevolversScheduler.scala:87) 在akka.actor.LightArrayRevolverScheduler$$anon$3.nextTick(LightArrayRevolverScheduler.scala:271) 在akka.actor.LightArrayRevolverScheduler$$anon$3.run(LightArrayRevolverScheduler.scala:241) 运行(Thread.java:745) 锁定可拥有的同步器: -没有


CPU利用率将达到800%。是什么导致了如此高的CPU利用率。请告知。

您可以发布代码片段吗?这可能是因为您可能正在运行大量异步任务(futures)。谢谢,找到了解决方案。实际上,默认调度程序和调度程序是在自定义调度程序之外创建的。默认调度程序可以创建不必要的线程,这些线程会导致线程不足并使用所有资源。您可以发布代码片段吗?这可能是因为您可能正在运行大量异步任务(futures)。谢谢,找到了解决方案。实际上,默认调度程序和调度程序是在自定义调度程序之外创建的。默认调度程序可以创建不必要的线程,这些线程会导致线程不足并使用所有资源。