Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import java.nio._
import java.nio.channels._
import java.nio.channels.spi._
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.{LinkedBlockingDeque, ThreadPoolExecutor, TimeUnit}
import java.util.concurrent.{Executors, LinkedBlockingDeque, ThreadPoolExecutor, TimeUnit}
import java.util.{Timer, TimerTask}

import scala.collection.mutable.{ArrayBuffer, HashMap, HashSet, SynchronizedMap, SynchronizedQueue}
Expand Down Expand Up @@ -77,7 +77,8 @@ private[nio] class ConnectionManager(
}

private val selector = SelectorProvider.provider.openSelector()
private val ackTimeoutMonitor = new Timer("AckTimeoutMonitor", true)
private val ackTimeoutMonitor = Executors.newScheduledThreadPool(2,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason for the magic number 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid some task block the whole scheduler. ScheduledExecutorService executes the task in its own thread pool, instead of fork a new thread for the task execution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked the codes:

    public ScheduledThreadPoolExecutor(int corePoolSize,
                                       ThreadFactory threadFactory) {
        super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
              new DelayedWorkQueue(), threadFactory);
    }

2 is the min thread number, not the max thread number.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

corePoolSize the number of threads to keep in the pool, even if they are idle

Here I think 1 is enough.

Utils.namedThreadFactory("AckTimeoutMonitor"))

private val ackTimeout = conf.getInt("spark.core.connection.ack.wait.timeout", 60)

Expand Down Expand Up @@ -899,10 +900,12 @@ private[nio] class ConnectionManager(
: Future[Message] = {
val promise = Promise[Message]()

val timeoutTask = new TimerTask {
// to avoid reference to the whole message body
val messageId: Int = message.id
val timeoutTask = new Runnable {
override def run(): Unit = {
messageStatuses.synchronized {
messageStatuses.remove(message.id).foreach ( s => {
messageStatuses.remove(messageId).foreach ( s => {
val e = new IOException("sendMessageReliably failed because ack " +
s"was not received within $ackTimeout sec")
if (!promise.tryFailure(e)) {
Expand All @@ -913,8 +916,9 @@ private[nio] class ConnectionManager(
}
}

val timeoutTaskFuture = ackTimeoutMonitor.schedule(timeoutTask, ackTimeout, TimeUnit.SECONDS)
val status = new MessageStatus(message, connectionManagerId, s => {
timeoutTask.cancel()
timeoutTaskFuture.cancel(true)
s match {
case scala.util.Failure(e) =>
// Indicates a failure where we either never sent or never got ACK'd
Expand Down Expand Up @@ -943,7 +947,6 @@ private[nio] class ConnectionManager(
messageStatuses += ((message.id, status))
}

ackTimeoutMonitor.schedule(timeoutTask, ackTimeout * 1000)
sendMessage(connectionManagerId, message)
promise.future
}
Expand All @@ -953,7 +956,7 @@ private[nio] class ConnectionManager(
}

def stop() {
ackTimeoutMonitor.cancel()
ackTimeoutMonitor.shutdownNow()
selectorThread.interrupt()
selectorThread.join()
selector.close()
Expand Down