forked from apache/zeppelin
/
SparkVersion.java
111 lines (89 loc) · 3.74 KB
/
SparkVersion.java
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
105
106
107
108
109
110
111
/*
* 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.zeppelin.spark;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Provide reading comparing capability of spark version returned from SparkContext.version()
*/
public class SparkVersion {
private static final Logger logger = LoggerFactory.getLogger(SparkVersion.class);
public static final SparkVersion SPARK_1_6_0 = SparkVersion.fromVersionString("1.6.0");
public static final SparkVersion SPARK_2_0_0 = SparkVersion.fromVersionString("2.0.0");
public static final SparkVersion SPARK_2_3_0 = SparkVersion.fromVersionString("2.3.0");
public static final SparkVersion SPARK_2_3_1 = SparkVersion.fromVersionString("2.3.1");
public static final SparkVersion SPARK_2_4_0 = SparkVersion.fromVersionString("2.4.0");
public static final SparkVersion SPARK_3_0_0 = SparkVersion.fromVersionString("3.0.0");
public static final SparkVersion MIN_SUPPORTED_VERSION = SPARK_1_6_0;
public static final SparkVersion UNSUPPORTED_FUTURE_VERSION = SPARK_3_0_0;
private int version;
private String versionString;
SparkVersion(String versionString) {
this.versionString = versionString;
try {
int pos = versionString.indexOf('-');
String numberPart = versionString;
if (pos > 0) {
numberPart = versionString.substring(0, pos);
}
String versions[] = numberPart.split("\\.");
int major = Integer.parseInt(versions[0]);
int minor = Integer.parseInt(versions[1]);
int patch = Integer.parseInt(versions[2]);
// version is always 5 digits. (e.g. 2.0.0 -> 20000, 1.6.2 -> 10602)
version = Integer.parseInt(String.format("%d%02d%02d", major, minor, patch));
} catch (Exception e) {
logger.error("Can not recognize Spark version " + versionString +
". Assume it's a future release", e);
// assume it is future release
version = 99999;
}
}
public int toNumber() {
return version;
}
public String toString() {
return versionString;
}
public boolean isUnsupportedVersion() {
return olderThan(MIN_SUPPORTED_VERSION) || newerThanEquals(UNSUPPORTED_FUTURE_VERSION);
}
public static SparkVersion fromVersionString(String versionString) {
return new SparkVersion(versionString);
}
public boolean isSpark2() {
return this.newerThanEquals(SPARK_2_0_0);
}
public boolean isSecretSocketSupported() {
return this.newerThanEquals(SPARK_2_3_1);
}
public boolean equals(Object versionToCompare) {
return version == ((SparkVersion) versionToCompare).version;
}
public boolean newerThan(SparkVersion versionToCompare) {
return version > versionToCompare.version;
}
public boolean newerThanEquals(SparkVersion versionToCompare) {
return version >= versionToCompare.version;
}
public boolean olderThan(SparkVersion versionToCompare) {
return version < versionToCompare.version;
}
public boolean olderThanEquals(SparkVersion versionToCompare) {
return version <= versionToCompare.version;
}
}