Skip to content

Commit 3821ab9

Browse files
committed
Fixes based on PR comments.
1 parent 08e5646 commit 3821ab9

File tree

3 files changed

+62
-59
lines changed

3 files changed

+62
-59
lines changed

core/src/main/scala/org/apache/spark/storage/BlockManager.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ private[spark] class BlockManager(
792792
* Get peer block managers in the system.
793793
*/
794794
private def getPeers(forceFetch: Boolean): HashSet[BlockManagerId] = {
795-
val cachedPeersTtl = conf.getInt("spark.storage.cachedPeersTtl", 1000) // milliseconds
795+
val cachedPeersTtl = conf.getInt("spark.storage.cachedPeersTtl", 60 * 1000) // milliseconds
796796
val timeout = System.currentTimeMillis - lastPeerFetchTime > cachedPeersTtl
797797

798798
cachedPeers.synchronized {

core/src/main/scala/org/apache/spark/storage/BlockManagerMasterActor.scala

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -404,17 +404,11 @@ class BlockManagerMasterActor(val isLocal: Boolean, conf: SparkConf, listenerBus
404404

405405
/** Get the list of the peers of the given block manager */
406406
private def getPeers(blockManagerId: BlockManagerId): Seq[BlockManagerId] = {
407-
val blockManagerIds = blockManagerInfo.keySet.filterNot { _.isDriver }.toArray
408-
val selfIndex = blockManagerIds.indexOf(blockManagerId)
409-
if (selfIndex == -1) {
410-
logError("Self index for " + blockManagerId + " not found")
411-
Seq.empty
407+
val blockManagerIds = blockManagerInfo.keySet
408+
if (blockManagerIds.contains(blockManagerId)) {
409+
blockManagerIds.filterNot { _.isDriver }.filterNot { _ == blockManagerId }.toSeq
412410
} else {
413-
// If the blockManagerIds is [ id1 id2 id3 id4 id5 ] and the blockManagerId is id2
414-
// Then this code will return the list [ id3 id4 id5 id1 ]
415-
Array.tabulate[BlockManagerId](blockManagerIds.size - 1) { i =>
416-
blockManagerIds((selfIndex + i + 1) % blockManagerIds.size)
417-
}
411+
Seq.empty
418412
}
419413
}
420414
}

core/src/test/scala/org/apache/spark/storage/BlockManagerSuite.scala

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class BlockManagerSuite extends FunSuite with Matchers with BeforeAndAfter
6363
val securityMgr = new SecurityManager(conf)
6464
val mapOutputTracker = new MapOutputTrackerMaster(conf)
6565
val shuffleManager = new HashShuffleManager(conf)
66+
67+
// List of block manager created during an unit test, so that all of the them can be stopped
68+
// after the unit test.
6669
val allStores = new ArrayBuffer[BlockManager]
6770

6871
// Reuse a serializer across tests to avoid creating a new thread-local buffer on each test
@@ -1241,7 +1244,7 @@ class BlockManagerSuite extends FunSuite with Matchers with BeforeAndAfter
12411244
assert(unrollMemoryAfterB7 === unrollMemoryAfterB4)
12421245
}
12431246

1244-
test("get peers with store addition and removal") {
1247+
test("get peers with addition and removal of block managers") {
12451248
val numStores = 4
12461249
val stores = (1 to numStores - 1).map { i => makeBlockManager(1000, s"store$i") }
12471250
val storeIds = stores.map { _.blockManagerId }.toSet
@@ -1313,11 +1316,17 @@ class BlockManagerSuite extends FunSuite with Matchers with BeforeAndAfter
13131316
testReplication(5, storageLevels)
13141317
}
13151318

1316-
test("block replication with addition and deletion of executors") {
1319+
test("block replication with addition and deletion of block managers") {
13171320
val blockSize = 1000
13181321
val storeSize = 10000
13191322
val initialStores = (1 to 2).map { i => makeBlockManager(storeSize, s"store$i") }
13201323

1324+
/**
1325+
* Function to test whether insert a block with replication achieves the expected replication.
1326+
* Since this function can be call on the same block id repeatedly through an `eventually`,
1327+
* it needs to be ensured that the method leaves block manager + master in the same state as
1328+
* it was before attempting to insert the block.
1329+
*/
13211330
def testPut(blockId: String, storageLevel: StorageLevel, expectedNumLocations: Int) {
13221331
try {
13231332
initialStores(0).putSingle(blockId, new Array[Byte](blockSize), storageLevel)
@@ -1345,7 +1354,7 @@ class BlockManagerSuite extends FunSuite with Matchers with BeforeAndAfter
13451354
}
13461355

13471356
// Remove all but the 1st store, 2x replication should fail
1348-
(initialStores.slice(1, initialStores.size) ++ Seq(newStore1, newStore2)).foreach {
1357+
(initialStores.tail ++ Seq(newStore1, newStore2)).foreach {
13491358
store =>
13501359
master.removeExecutor(store.blockManagerId.executorId)
13511360
store.stop()
@@ -1394,57 +1403,57 @@ class BlockManagerSuite extends FunSuite with Matchers with BeforeAndAfter
13941403
s"master did not have ${storageLevel.replication} locations for $blockId")
13951404

13961405
// Test state of the stores that contain the block
1397-
stores.filter(testStore => blockLocations.contains(testStore.blockManagerId.executorId))
1406+
stores.filter { testStore => blockLocations.contains(testStore.blockManagerId.executorId) }
13981407
.foreach { testStore =>
1399-
val testStoreName = testStore.blockManagerId.executorId
1400-
assert(testStore.getLocal(blockId).isDefined, s"$blockId was not found in $testStoreName")
1401-
assert(master.getLocations(blockId).map(_.executorId).toSet.contains(testStoreName),
1402-
s"master does not have status for ${blockId.name} in $testStoreName")
1403-
1404-
val blockStatus = master.getBlockStatus(blockId)(testStore.blockManagerId)
1405-
1406-
// Assert that block status in the master for this store has expected storage level
1407-
assert(
1408-
blockStatus.storageLevel.useDisk === storageLevel.useDisk &&
1409-
blockStatus.storageLevel.useMemory === storageLevel.useMemory &&
1410-
blockStatus.storageLevel.useOffHeap === storageLevel.useOffHeap &&
1411-
blockStatus.storageLevel.deserialized === storageLevel.deserialized,
1412-
s"master does not know correct storage level for ${blockId.name} in $testStoreName")
1413-
1414-
// Assert that the block status in the master for this store has correct memory usage info
1415-
assert(!blockStatus.storageLevel.useMemory || blockStatus.memSize >= blockSize,
1416-
s"master does not know size of ${blockId.name} stored in memory of $testStoreName")
1417-
1418-
1419-
// If the block is supposed to be in memory, then drop the copy of the block in
1420-
// this store test whether master is updated with zero memory usage this store
1421-
if (storageLevel.useMemory) {
1422-
// Force the block to be dropped by adding a number of dummy blocks
1423-
(1 to 10).foreach { i =>
1424-
testStore.putSingle(s"dummy-block-$i", new Array[Byte](1000), MEMORY_ONLY_SER)
1425-
}
1426-
(1 to 10).foreach { i => testStore.removeBlock(s"dummy-block-$i") }
1408+
val testStoreName = testStore.blockManagerId.executorId
1409+
assert(testStore.getLocal(blockId).isDefined, s"$blockId was not found in $testStoreName")
1410+
assert(master.getLocations(blockId).map(_.executorId).toSet.contains(testStoreName),
1411+
s"master does not have status for ${blockId.name} in $testStoreName")
14271412

1428-
val newBlockStatusOption = master.getBlockStatus(blockId).get(testStore.blockManagerId)
1413+
val blockStatus = master.getBlockStatus(blockId)(testStore.blockManagerId)
14291414

1430-
// Assert that the block status in the master either does not exist (block removed
1431-
// from every store) or has zero memory usage for this store
1415+
// Assert that block status in the master for this store has expected storage level
14321416
assert(
1433-
newBlockStatusOption.isEmpty || newBlockStatusOption.get.memSize === 0,
1434-
s"after dropping, master does not know size of ${blockId.name} " +
1435-
s"stored in memory of $testStoreName"
1436-
)
1437-
}
1417+
blockStatus.storageLevel.useDisk === storageLevel.useDisk &&
1418+
blockStatus.storageLevel.useMemory === storageLevel.useMemory &&
1419+
blockStatus.storageLevel.useOffHeap === storageLevel.useOffHeap &&
1420+
blockStatus.storageLevel.deserialized === storageLevel.deserialized,
1421+
s"master does not know correct storage level for ${blockId.name} in $testStoreName")
1422+
1423+
// Assert that the block status in the master for this store has correct memory usage info
1424+
assert(!blockStatus.storageLevel.useMemory || blockStatus.memSize >= blockSize,
1425+
s"master does not know size of ${blockId.name} stored in memory of $testStoreName")
1426+
1427+
1428+
// If the block is supposed to be in memory, then drop the copy of the block in
1429+
// this store test whether master is updated with zero memory usage this store
1430+
if (storageLevel.useMemory) {
1431+
// Force the block to be dropped by adding a number of dummy blocks
1432+
(1 to 10).foreach { i =>
1433+
testStore.putSingle(s"dummy-block-$i", new Array[Byte](1000), MEMORY_ONLY_SER)
1434+
}
1435+
(1 to 10).foreach { i => testStore.removeBlock(s"dummy-block-$i") }
1436+
1437+
val newBlockStatusOption = master.getBlockStatus(blockId).get(testStore.blockManagerId)
1438+
1439+
// Assert that the block status in the master either does not exist (block removed
1440+
// from every store) or has zero memory usage for this store
1441+
assert(
1442+
newBlockStatusOption.isEmpty || newBlockStatusOption.get.memSize === 0,
1443+
s"after dropping, master does not know size of ${blockId.name} " +
1444+
s"stored in memory of $testStoreName"
1445+
)
1446+
}
14381447

1439-
// If the block is supposed to be in disk (after dropping or otherwise, then
1440-
// test whether master has correct disk usage for this store
1441-
if (storageLevel.useDisk) {
1442-
assert(master.getBlockStatus(blockId)(testStore.blockManagerId).diskSize >= blockSize,
1443-
s"after dropping, master does not know size of ${blockId.name} " +
1444-
s"stored in disk of $testStoreName"
1445-
)
1448+
// If the block is supposed to be in disk (after dropping or otherwise, then
1449+
// test whether master has correct disk usage for this store
1450+
if (storageLevel.useDisk) {
1451+
assert(master.getBlockStatus(blockId)(testStore.blockManagerId).diskSize >= blockSize,
1452+
s"after dropping, master does not know size of ${blockId.name} " +
1453+
s"stored in disk of $testStoreName"
1454+
)
1455+
}
14461456
}
1447-
}
14481457
master.removeBlock(blockId)
14491458
}
14501459
}

0 commit comments

Comments
 (0)