Skip to content

Commit 383bf72

Browse files
hsaputrarxin
authored andcommitted
Cleanup on Connection, ConnectionManagerId, ConnectionManager classes part 2
Cleanup on Connection, ConnectionManagerId, and ConnectionManager classes part 2 while I was working at the code there to help IDE: 1. Remove unused imports 2. Remove parentheses in method calls that do not have side affect. 3. Add parentheses in method calls that do have side effect or not simple get to object properties. 4. Change if-else check (via isInstanceOf) for Connection class type with Scala expression for consistency and cleanliness. 5. Remove semicolon 6. Remove extra spaces. 7. Remove redundant return for consistency Author: Henry Saputra <[email protected]> Closes #1157 from hsaputra/cleanup_connection_classes_part2 and squashes the following commits: 4be6906 [Henry Saputra] Fix Spark Scala style for line over 100 chars. 85b24f7 [Henry Saputra] Cleanup on Connection and ConnectionManager classes part 2 while I was working at the code there to help IDE: 1. Remove unused imports 2. Remove parentheses in method calls that do not have side affect. 3. Add parentheses in method calls that do have side effect. 4. Change if-else check (via isInstanceOf) for Connection class type with Scala expression for consitency and cleanliness. 5. Remove semicolon 6. Remove extra spaces.
1 parent 21ddd7d commit 383bf72

File tree

3 files changed

+62
-69
lines changed

3 files changed

+62
-69
lines changed

core/src/main/scala/org/apache/spark/network/Connection.scala

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717

1818
package org.apache.spark.network
1919

20-
import org.apache.spark._
21-
import org.apache.spark.SparkSaslServer
22-
23-
import scala.collection.mutable.{HashMap, Queue, ArrayBuffer}
24-
2520
import java.net._
2621
import java.nio._
2722
import java.nio.channels._
@@ -41,7 +36,7 @@ abstract class Connection(val channel: SocketChannel, val selector: Selector,
4136
def this(channel_ : SocketChannel, selector_ : Selector, id_ : ConnectionId) = {
4237
this(channel_, selector_,
4338
ConnectionManagerId.fromSocketAddress(
44-
channel_.socket.getRemoteSocketAddress().asInstanceOf[InetSocketAddress]), id_)
39+
channel_.socket.getRemoteSocketAddress.asInstanceOf[InetSocketAddress]), id_)
4540
}
4641

4742
channel.configureBlocking(false)
@@ -89,7 +84,7 @@ abstract class Connection(val channel: SocketChannel, val selector: Selector,
8984

9085
private def disposeSasl() {
9186
if (sparkSaslServer != null) {
92-
sparkSaslServer.dispose();
87+
sparkSaslServer.dispose()
9388
}
9489

9590
if (sparkSaslClient != null) {
@@ -328,15 +323,13 @@ class SendingConnection(val address: InetSocketAddress, selector_ : Selector,
328323
// Is highly unlikely unless there was an unclean close of socket, etc
329324
registerInterest()
330325
logInfo("Connected to [" + address + "], " + outbox.messages.size + " messages pending")
331-
true
332326
} catch {
333327
case e: Exception => {
334328
logWarning("Error finishing connection to " + address, e)
335329
callOnExceptionCallback(e)
336-
// ignore
337-
return true
338330
}
339331
}
332+
true
340333
}
341334

342335
override def write(): Boolean = {
@@ -546,7 +539,7 @@ private[spark] class ReceivingConnection(
546539
/* println("Filled buffer at " + System.currentTimeMillis) */
547540
val bufferMessage = inbox.getMessageForChunk(currentChunk).get
548541
if (bufferMessage.isCompletelyReceived) {
549-
bufferMessage.flip
542+
bufferMessage.flip()
550543
bufferMessage.finishTime = System.currentTimeMillis
551544
logDebug("Finished receiving [" + bufferMessage + "] from " +
552545
"[" + getRemoteConnectionManagerId() + "] in " + bufferMessage.timeTaken)

core/src/main/scala/org/apache/spark/network/ConnectionManager.scala

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private[spark] class ConnectionManager(port: Int, conf: SparkConf,
249249
def run() {
250250
try {
251251
while(!selectorThread.isInterrupted) {
252-
while (! registerRequests.isEmpty) {
252+
while (!registerRequests.isEmpty) {
253253
val conn: SendingConnection = registerRequests.dequeue()
254254
addListeners(conn)
255255
conn.connect()
@@ -308,7 +308,7 @@ private[spark] class ConnectionManager(port: Int, conf: SparkConf,
308308
// Some keys within the selectors list are invalid/closed. clear them.
309309
val allKeys = selector.keys().iterator()
310310

311-
while (allKeys.hasNext()) {
311+
while (allKeys.hasNext) {
312312
val key = allKeys.next()
313313
try {
314314
if (! key.isValid) {
@@ -341,7 +341,7 @@ private[spark] class ConnectionManager(port: Int, conf: SparkConf,
341341

342342
if (0 != selectedKeysCount) {
343343
val selectedKeys = selector.selectedKeys().iterator()
344-
while (selectedKeys.hasNext()) {
344+
while (selectedKeys.hasNext) {
345345
val key = selectedKeys.next
346346
selectedKeys.remove()
347347
try {
@@ -419,62 +419,63 @@ private[spark] class ConnectionManager(port: Int, conf: SparkConf,
419419
connectionsByKey -= connection.key
420420

421421
try {
422-
if (connection.isInstanceOf[SendingConnection]) {
423-
val sendingConnection = connection.asInstanceOf[SendingConnection]
424-
val sendingConnectionManagerId = sendingConnection.getRemoteConnectionManagerId()
425-
logInfo("Removing SendingConnection to " + sendingConnectionManagerId)
426-
427-
connectionsById -= sendingConnectionManagerId
428-
connectionsAwaitingSasl -= connection.connectionId
422+
connection match {
423+
case sendingConnection: SendingConnection =>
424+
val sendingConnectionManagerId = sendingConnection.getRemoteConnectionManagerId()
425+
logInfo("Removing SendingConnection to " + sendingConnectionManagerId)
426+
427+
connectionsById -= sendingConnectionManagerId
428+
connectionsAwaitingSasl -= connection.connectionId
429+
430+
messageStatuses.synchronized {
431+
messageStatuses.values.filter(_.connectionManagerId == sendingConnectionManagerId)
432+
.foreach(status => {
433+
logInfo("Notifying " + status)
434+
status.synchronized {
435+
status.attempted = true
436+
status.acked = false
437+
status.markDone()
438+
}
439+
})
429440

430-
messageStatuses.synchronized {
431-
messageStatuses
432-
.values.filter(_.connectionManagerId == sendingConnectionManagerId).foreach(status => {
433-
logInfo("Notifying " + status)
434-
status.synchronized {
435-
status.attempted = true
436-
status.acked = false
437-
status.markDone()
438-
}
441+
messageStatuses.retain((i, status) => {
442+
status.connectionManagerId != sendingConnectionManagerId
439443
})
444+
}
445+
case receivingConnection: ReceivingConnection =>
446+
val remoteConnectionManagerId = receivingConnection.getRemoteConnectionManagerId()
447+
logInfo("Removing ReceivingConnection to " + remoteConnectionManagerId)
440448

441-
messageStatuses.retain((i, status) => {
442-
status.connectionManagerId != sendingConnectionManagerId
443-
})
444-
}
445-
} else if (connection.isInstanceOf[ReceivingConnection]) {
446-
val receivingConnection = connection.asInstanceOf[ReceivingConnection]
447-
val remoteConnectionManagerId = receivingConnection.getRemoteConnectionManagerId()
448-
logInfo("Removing ReceivingConnection to " + remoteConnectionManagerId)
449-
450-
val sendingConnectionOpt = connectionsById.get(remoteConnectionManagerId)
451-
if (! sendingConnectionOpt.isDefined) {
452-
logError("Corresponding SendingConnectionManagerId not found")
453-
return
454-
}
449+
val sendingConnectionOpt = connectionsById.get(remoteConnectionManagerId)
450+
if (!sendingConnectionOpt.isDefined) {
451+
logError("Corresponding SendingConnectionManagerId not found")
452+
return
453+
}
455454

456-
val sendingConnection = sendingConnectionOpt.get
457-
connectionsById -= remoteConnectionManagerId
458-
sendingConnection.close()
455+
val sendingConnection = sendingConnectionOpt.get
456+
connectionsById -= remoteConnectionManagerId
457+
sendingConnection.close()
459458

460-
val sendingConnectionManagerId = sendingConnection.getRemoteConnectionManagerId()
459+
val sendingConnectionManagerId = sendingConnection.getRemoteConnectionManagerId()
461460

462-
assert (sendingConnectionManagerId == remoteConnectionManagerId)
461+
assert(sendingConnectionManagerId == remoteConnectionManagerId)
463462

464-
messageStatuses.synchronized {
465-
for (s <- messageStatuses.values if s.connectionManagerId == sendingConnectionManagerId) {
466-
logInfo("Notifying " + s)
467-
s.synchronized {
468-
s.attempted = true
469-
s.acked = false
470-
s.markDone()
463+
messageStatuses.synchronized {
464+
for (s <- messageStatuses.values
465+
if s.connectionManagerId == sendingConnectionManagerId) {
466+
logInfo("Notifying " + s)
467+
s.synchronized {
468+
s.attempted = true
469+
s.acked = false
470+
s.markDone()
471+
}
471472
}
472-
}
473473

474-
messageStatuses.retain((i, status) => {
475-
status.connectionManagerId != sendingConnectionManagerId
476-
})
477-
}
474+
messageStatuses.retain((i, status) => {
475+
status.connectionManagerId != sendingConnectionManagerId
476+
})
477+
}
478+
case _ => logError("Unsupported type of connection.")
478479
}
479480
} finally {
480481
// So that the selection keys can be removed.
@@ -517,13 +518,13 @@ private[spark] class ConnectionManager(port: Int, conf: SparkConf,
517518
logDebug("Client sasl completed for id: " + waitingConn.connectionId)
518519
connectionsAwaitingSasl -= waitingConn.connectionId
519520
waitingConn.getAuthenticated().synchronized {
520-
waitingConn.getAuthenticated().notifyAll();
521+
waitingConn.getAuthenticated().notifyAll()
521522
}
522523
return
523524
} else {
524525
var replyToken : Array[Byte] = null
525526
try {
526-
replyToken = waitingConn.sparkSaslClient.saslResponse(securityMsg.getToken);
527+
replyToken = waitingConn.sparkSaslClient.saslResponse(securityMsg.getToken)
527528
if (waitingConn.isSaslComplete()) {
528529
logDebug("Client sasl completed after evaluate for id: " + waitingConn.connectionId)
529530
connectionsAwaitingSasl -= waitingConn.connectionId
@@ -533,7 +534,7 @@ private[spark] class ConnectionManager(port: Int, conf: SparkConf,
533534
return
534535
}
535536
val securityMsgResp = SecurityMessage.fromResponse(replyToken,
536-
securityMsg.getConnectionId.toString())
537+
securityMsg.getConnectionId.toString)
537538
val message = securityMsgResp.toBufferMessage
538539
if (message == null) throw new Exception("Error creating security message")
539540
sendSecurityMessage(waitingConn.getRemoteConnectionManagerId(), message)
@@ -630,13 +631,13 @@ private[spark] class ConnectionManager(port: Int, conf: SparkConf,
630631
case bufferMessage: BufferMessage => {
631632
if (authEnabled) {
632633
val res = handleAuthentication(connection, bufferMessage)
633-
if (res == true) {
634+
if (res) {
634635
// message was security negotiation so skip the rest
635636
logDebug("After handleAuth result was true, returning")
636637
return
637638
}
638639
}
639-
if (bufferMessage.hasAckId) {
640+
if (bufferMessage.hasAckId()) {
640641
val sentMessageStatus = messageStatuses.synchronized {
641642
messageStatuses.get(bufferMessage.ackId) match {
642643
case Some(status) => {
@@ -646,7 +647,6 @@ private[spark] class ConnectionManager(port: Int, conf: SparkConf,
646647
case None => {
647648
throw new Exception("Could not find reference for received ack message " +
648649
message.id)
649-
null
650650
}
651651
}
652652
}
@@ -668,7 +668,7 @@ private[spark] class ConnectionManager(port: Int, conf: SparkConf,
668668
if (ackMessage.isDefined) {
669669
if (!ackMessage.get.isInstanceOf[BufferMessage]) {
670670
logDebug("Response to " + bufferMessage + " is not a buffer message, it is of type "
671-
+ ackMessage.get.getClass())
671+
+ ackMessage.get.getClass)
672672
} else if (!ackMessage.get.asInstanceOf[BufferMessage].hasAckId) {
673673
logDebug("Response to " + bufferMessage + " does not have ack id set")
674674
ackMessage.get.asInstanceOf[BufferMessage].ackId = bufferMessage.id

core/src/main/scala/org/apache/spark/network/ConnectionManagerId.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ private[spark] case class ConnectionManagerId(host: String, port: Int) {
3232

3333
private[spark] object ConnectionManagerId {
3434
def fromSocketAddress(socketAddress: InetSocketAddress): ConnectionManagerId = {
35-
new ConnectionManagerId(socketAddress.getHostName(), socketAddress.getPort())
35+
new ConnectionManagerId(socketAddress.getHostName, socketAddress.getPort)
3636
}
3737
}

0 commit comments

Comments
 (0)