|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +// This file is placed in different package to make sure all of these components work well |
| 19 | +// when they are outside of org.apache.spark. |
| 20 | +package other.supplier |
| 21 | + |
| 22 | +import scala.collection.mutable |
| 23 | +import scala.reflect.ClassTag |
| 24 | + |
| 25 | +import akka.serialization.Serialization |
| 26 | + |
| 27 | +import org.apache.spark.SparkConf |
| 28 | +import org.apache.spark.deploy.master._ |
| 29 | + |
| 30 | +class CustomRecoveryModeFactory( |
| 31 | + conf: SparkConf, |
| 32 | + serialization: Serialization |
| 33 | +) extends StandaloneRecoveryModeFactory(conf, serialization) { |
| 34 | + |
| 35 | + CustomRecoveryModeFactory.instantiationAttempts += 1 |
| 36 | + |
| 37 | + /** |
| 38 | + * PersistenceEngine defines how the persistent data(Information about worker, driver etc..) |
| 39 | + * is handled for recovery. |
| 40 | + * |
| 41 | + */ |
| 42 | + override def createPersistenceEngine(): PersistenceEngine = |
| 43 | + new CustomPersistenceEngine(serialization) |
| 44 | + |
| 45 | + /** |
| 46 | + * Create an instance of LeaderAgent that decides who gets elected as master. |
| 47 | + */ |
| 48 | + override def createLeaderElectionAgent(master: LeaderElectable): LeaderElectionAgent = |
| 49 | + new CustomLeaderElectionAgent(master) |
| 50 | +} |
| 51 | + |
| 52 | +object CustomRecoveryModeFactory { |
| 53 | + @volatile var instantiationAttempts = 0 |
| 54 | +} |
| 55 | + |
| 56 | +class CustomPersistenceEngine(serialization: Serialization) extends PersistenceEngine { |
| 57 | + val data = mutable.HashMap[String, Array[Byte]]() |
| 58 | + |
| 59 | + CustomPersistenceEngine.lastInstance = Some(this) |
| 60 | + |
| 61 | + /** |
| 62 | + * Defines how the object is serialized and persisted. Implementation will |
| 63 | + * depend on the store used. |
| 64 | + */ |
| 65 | + override def persist(name: String, obj: Object): Unit = { |
| 66 | + CustomPersistenceEngine.persistAttempts += 1 |
| 67 | + serialization.serialize(obj) match { |
| 68 | + case util.Success(bytes) => data += name -> bytes |
| 69 | + case util.Failure(cause) => throw new RuntimeException(cause) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Defines how the object referred by its name is removed from the store. |
| 75 | + */ |
| 76 | + override def unpersist(name: String): Unit = { |
| 77 | + CustomPersistenceEngine.unpersistAttempts += 1 |
| 78 | + data -= name |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Gives all objects, matching a prefix. This defines how objects are |
| 83 | + * read/deserialized back. |
| 84 | + */ |
| 85 | + override def read[T: ClassTag](prefix: String): Seq[T] = { |
| 86 | + CustomPersistenceEngine.readAttempts += 1 |
| 87 | + val clazz = implicitly[ClassTag[T]].runtimeClass.asInstanceOf[Class[T]] |
| 88 | + val results = for ((name, bytes) <- data; if name.startsWith(prefix)) |
| 89 | + yield serialization.deserialize(bytes, clazz) |
| 90 | + |
| 91 | + results.find(_.isFailure).foreach { |
| 92 | + case util.Failure(cause) => throw new RuntimeException(cause) |
| 93 | + } |
| 94 | + |
| 95 | + results.flatMap(_.toOption).toSeq |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +object CustomPersistenceEngine { |
| 100 | + @volatile var persistAttempts = 0 |
| 101 | + @volatile var unpersistAttempts = 0 |
| 102 | + @volatile var readAttempts = 0 |
| 103 | + |
| 104 | + @volatile var lastInstance: Option[CustomPersistenceEngine] = None |
| 105 | +} |
| 106 | + |
| 107 | +class CustomLeaderElectionAgent(val masterActor: LeaderElectable) extends LeaderElectionAgent { |
| 108 | + masterActor.electedLeader() |
| 109 | +} |
| 110 | + |
0 commit comments