-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SPARK-7436: Fixed instantiation of custom recovery mode factory and a…
…dded tests
- Loading branch information
1 parent
2337ccc
commit 6298313
Showing
3 changed files
with
208 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
core/src/test/scala/org/apache/spark/deploy/master/CustomRecoveryModeFactory.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
// This file is placed in different package to make sure all of these components work well | ||
// when they are outside of org.apache.spark. | ||
package other.supplier | ||
|
||
import scala.collection.mutable | ||
import scala.reflect.ClassTag | ||
|
||
import akka.serialization.Serialization | ||
|
||
import org.apache.spark.SparkConf | ||
import org.apache.spark.deploy.master._ | ||
|
||
class CustomRecoveryModeFactory( | ||
conf: SparkConf, | ||
serialization: Serialization | ||
) extends StandaloneRecoveryModeFactory(conf, serialization) { | ||
|
||
CustomRecoveryModeFactory.instantiationAttempts += 1 | ||
|
||
/** | ||
* PersistenceEngine defines how the persistent data(Information about worker, driver etc..) | ||
* is handled for recovery. | ||
* | ||
*/ | ||
override def createPersistenceEngine(): PersistenceEngine = | ||
new CustomPersistenceEngine(serialization) | ||
|
||
/** | ||
* Create an instance of LeaderAgent that decides who gets elected as master. | ||
*/ | ||
override def createLeaderElectionAgent(master: LeaderElectable): LeaderElectionAgent = | ||
new CustomLeaderElectionAgent(master) | ||
} | ||
|
||
object CustomRecoveryModeFactory { | ||
@volatile var instantiationAttempts = 0 | ||
} | ||
|
||
class CustomPersistenceEngine(serialization: Serialization) extends PersistenceEngine { | ||
val data = mutable.HashMap[String, Array[Byte]]() | ||
|
||
CustomPersistenceEngine.lastInstance = Some(this) | ||
|
||
/** | ||
* Defines how the object is serialized and persisted. Implementation will | ||
* depend on the store used. | ||
*/ | ||
override def persist(name: String, obj: Object): Unit = { | ||
CustomPersistenceEngine.persistAttempts += 1 | ||
serialization.serialize(obj) match { | ||
case util.Success(bytes) => data += name -> bytes | ||
case util.Failure(cause) => throw new RuntimeException(cause) | ||
} | ||
} | ||
|
||
/** | ||
* Defines how the object referred by its name is removed from the store. | ||
*/ | ||
override def unpersist(name: String): Unit = { | ||
CustomPersistenceEngine.unpersistAttempts += 1 | ||
data -= name | ||
} | ||
|
||
/** | ||
* Gives all objects, matching a prefix. This defines how objects are | ||
* read/deserialized back. | ||
*/ | ||
override def read[T: ClassTag](prefix: String): Seq[T] = { | ||
CustomPersistenceEngine.readAttempts += 1 | ||
val clazz = implicitly[ClassTag[T]].runtimeClass.asInstanceOf[Class[T]] | ||
val results = for ((name, bytes) <- data; if name.startsWith(prefix)) | ||
yield serialization.deserialize(bytes, clazz) | ||
|
||
results.find(_.isFailure).foreach { | ||
case util.Failure(cause) => throw new RuntimeException(cause) | ||
} | ||
|
||
results.flatMap(_.toOption).toSeq | ||
} | ||
} | ||
|
||
object CustomPersistenceEngine { | ||
@volatile var persistAttempts = 0 | ||
@volatile var unpersistAttempts = 0 | ||
@volatile var readAttempts = 0 | ||
|
||
@volatile var lastInstance: Option[CustomPersistenceEngine] = None | ||
} | ||
|
||
class CustomLeaderElectionAgent(val masterActor: LeaderElectable) extends LeaderElectionAgent { | ||
masterActor.electedLeader() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters