Nice handling of error cases. The first two lines we can simplify it a little bit to

    val loc = parents.count(p => rdd.preferredLocations(p).contains(getPreferredLocation))