forked from apache/spark
/
ResourceInformation.scala
104 lines (88 loc) · 3.52 KB
/
ResourceInformation.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* 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.resource
import scala.util.control.NonFatal
import org.json4s.{DefaultFormats, Extraction, Formats, JValue}
import org.json4s.jackson.JsonMethods._
import org.apache.spark.SparkException
import org.apache.spark.annotation.Evolving
import org.apache.spark.util.ArrayImplicits._
/**
* Class to hold information about a type of Resource. A resource could be a GPU, FPGA, etc.
* The array of addresses are resource specific and its up to the user to interpret the address.
*
* One example is GPUs, where the addresses would be the indices of the GPUs
*
* @param name the name of the resource
* @param addresses an array of strings describing the addresses of the resource
*
* @since 3.0.0
*/
@Evolving
class ResourceInformation(
val name: String,
val addresses: Array[String]) extends Serializable {
override def toString: String = s"[name: ${name}, addresses: ${addresses.mkString(",")}]"
override def equals(obj: Any): Boolean = {
obj match {
case that: ResourceInformation =>
that.getClass == this.getClass &&
that.name == name && that.addresses.toSeq == addresses.toSeq
case _ =>
false
}
}
override def hashCode(): Int = Seq(name, addresses.toSeq).hashCode()
// TODO(SPARK-39658): reconsider whether we want to expose a third-party library's
// symbols as part of a public API:
final def toJson(): JValue = ResourceInformationJson(name, addresses.toImmutableArraySeq).toJValue
}
private[spark] object ResourceInformation {
private lazy val exampleJson: String = compact(render(
ResourceInformationJson("gpu", Seq("0", "1")).toJValue))
/**
* Parses a JSON string into a [[ResourceInformation]] instance.
*/
def parseJson(json: String): ResourceInformation = {
implicit val formats: Formats = DefaultFormats
try {
parse(json).extract[ResourceInformationJson].toResourceInformation
} catch {
case NonFatal(e) =>
throw new SparkException(s"Error parsing JSON into ResourceInformation:\n$json\n" +
s"Here is a correct example: $exampleJson.", e)
}
}
def parseJson(json: JValue): ResourceInformation = {
implicit val formats: Formats = DefaultFormats
try {
json.extract[ResourceInformationJson].toResourceInformation
} catch {
case NonFatal(e) =>
throw new SparkException(s"Error parsing JSON into ResourceInformation:\n$json\n", e)
}
}
}
/** A case class to simplify JSON serialization of [[ResourceInformation]]. */
private case class ResourceInformationJson(name: String, addresses: Seq[String]) {
def toJValue: JValue = {
Extraction.decompose(this)(DefaultFormats)
}
def toResourceInformation: ResourceInformation = {
new ResourceInformation(name, addresses.toArray)
}
}