Skip to content
Closed
Show file tree
Hide file tree
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 @@ -17,6 +17,8 @@

package org.apache.spark.ui.scope

import java.util.Objects

import scala.collection.mutable
import scala.collection.mutable.{ListBuffer, StringBuilder}

Expand Down Expand Up @@ -72,6 +74,22 @@ private[ui] class RDDOperationCluster(val id: String, private var _name: String)
def getCachedNodes: Seq[RDDOperationNode] = {
_childNodes.filter(_.cached) ++ _childClusters.flatMap(_.getCachedNodes)
}

def canEqual(other: Any): Boolean = other.isInstanceOf[RDDOperationCluster]

override def equals(other: Any): Boolean = other match {
case that: RDDOperationCluster =>
(that canEqual this) &&
Copy link
Member

Choose a reason for hiding this comment

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

Do we need canEqual since that is already known to be a RDDOperationCluster?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This check ensures that equality is symmetric if subclasses have a different definition of equals. As I understand scala best practices, it should be there if the class can be extended. Right now this isn't extended, but I thought it may be in the future so I included it. I'm fine either way.

Copy link
Member

Choose a reason for hiding this comment

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

OK this is another way or saying "this.getClass == that.getClass", essentially? I think it is OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's similar, but it's calling a method on the other object to see if it would reject equality. There's a good explanation of canEqual on StackOverflow.

Copy link
Contributor

Choose a reason for hiding this comment

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

The only benefit you get with canEqual over this.getClass == that.getClass is that it's a little bit less strict.
See https://www.artima.com/lejava/articles/equality.html (look for "getClass" in the text to avoid reading the whole thing)

_childClusters == that._childClusters &&
id == that.id &&
_name == that._name
case _ => false
}

override def hashCode(): Int = {
val state = Seq(_childClusters, id, _name)
state.map(Objects.hashCode).foldLeft(0)((a, b) => 31 * a + b)
}
}

private[ui] object RDDOperationGraph extends Logging {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.ui.scope

import org.apache.spark.SparkFunSuite

class RDDOperationGraphSuite extends SparkFunSuite {
test("Test simple cluster equals") {
// create a 2-cluster chain with a child
val c1 = new RDDOperationCluster("1", "Bender")
val c2 = new RDDOperationCluster("2", "Hal")
c1.attachChildCluster(c2)
c1.attachChildNode(new RDDOperationNode(3, "Marvin", false, "collect!"))

// create an equal cluster, but without the child node
val c1copy = new RDDOperationCluster("1", "Bender")
val c2copy = new RDDOperationCluster("2", "Hal")
c1copy.attachChildCluster(c2copy)

assert(c1 == c1copy)
}
}