Skip to content

Commit 034cefc

Browse files
committed
[SPARK-53820] Introduce o.a.s.k8s.operator.utils.StringUtils
### What changes were proposed in this pull request? This PR aims to introduce `org.apache.spark.k8s.operator.utils.StringUtils`. ### Why are the changes needed? To reduce the dependency on `commons-lang3`. **BEFORE** ``` $ git grep org.apache.commons.lang3 | grep -v test | grep -v checkstyle | wc -l 11 ``` **AFTER** ``` $ git grep org.apache.commons.lang3 | grep -v test | grep -v checkstyle | wc -l 0 ``` ### Does this PR introduce _any_ user-facing change? No behavior change. ### How was this patch tested? Pass the CIs. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #370 from dongjoon-hyun/SPARK-53820. Authored-by: Dongjoon Hyun <[email protected]> Signed-off-by: Dongjoon Hyun <[email protected]>
1 parent 88601b7 commit 034cefc

File tree

12 files changed

+84
-14
lines changed

12 files changed

+84
-14
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.spark.k8s.operator.utils;
21+
22+
import java.io.PrintWriter;
23+
import java.io.StringWriter;
24+
25+
/** Utility class for string operations. */
26+
public final class StringUtils {
27+
private StringUtils() {}
28+
29+
public static boolean isBlank(final String str) {
30+
return str == null || str.isBlank();
31+
}
32+
33+
public static boolean isNotBlank(final String str) {
34+
return !isBlank(str);
35+
}
36+
37+
public static boolean isEmpty(final String str) {
38+
return str == null || str.length() == 0;
39+
}
40+
41+
public static boolean isNotEmpty(final String str) {
42+
return !isEmpty(str);
43+
}
44+
45+
public static int ordinalIndexOf(final String str, final String substr, final int ordinal) {
46+
if (str == null || substr == null || ordinal <= 0) {
47+
return -1;
48+
}
49+
if (substr.isEmpty()) {
50+
return 0;
51+
}
52+
53+
int index = -1;
54+
for (int i = 0; i < ordinal; i++) {
55+
index = str.indexOf(substr, index + 1);
56+
if (index == -1) {
57+
return -1;
58+
}
59+
}
60+
return index;
61+
}
62+
63+
public static String getStackTrace(Throwable throwable) {
64+
if (throwable == null) {
65+
return "";
66+
}
67+
68+
StringWriter sw = new StringWriter();
69+
PrintWriter pw = new PrintWriter(sw);
70+
throwable.printStackTrace(pw);
71+
return sw.toString();
72+
}
73+
}

spark-operator/src/main/java/org/apache/spark/k8s/operator/config/ConfigOption.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import lombok.RequiredArgsConstructor;
2828
import lombok.ToString;
2929
import lombok.extern.slf4j.Slf4j;
30-
import org.apache.commons.lang3.StringUtils;
3130

3231
import org.apache.spark.k8s.operator.utils.ModelUtils;
32+
import org.apache.spark.k8s.operator.utils.StringUtils;
3333

3434
/**
3535
* Config options for Spark Operator. Supports primitive and serialized JSON.

spark-operator/src/main/java/org/apache/spark/k8s/operator/config/SparkOperatorConfManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
import java.util.Properties;
2828

2929
import lombok.extern.slf4j.Slf4j;
30-
import org.apache.commons.lang3.StringUtils;
30+
31+
import org.apache.spark.k8s.operator.utils.StringUtils;
3132

3233
/**
3334
* Loads ConfigOption from properties file. In addition, loads hot properties override from config

spark-operator/src/main/java/org/apache/spark/k8s/operator/metrics/MetricsSystemFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
import java.util.Map;
2525
import java.util.Properties;
2626

27-
import org.apache.commons.lang3.StringUtils;
28-
2927
import org.apache.spark.k8s.operator.config.SparkOperatorConfManager;
28+
import org.apache.spark.k8s.operator.utils.StringUtils;
3029

3130
/** Factory for MetricsSystem. */
3231
public final class MetricsSystemFactory {

spark-operator/src/main/java/org/apache/spark/k8s/operator/metrics/PrometheusPullModelHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
import jakarta.servlet.http.HttpServletRequest;
4141
import lombok.Getter;
4242
import lombok.extern.slf4j.Slf4j;
43-
import org.apache.commons.lang3.StringUtils;
4443

4544
import org.apache.spark.k8s.operator.config.SparkOperatorConf;
45+
import org.apache.spark.k8s.operator.utils.StringUtils;
4646
import org.apache.spark.metrics.sink.PrometheusServlet;
4747

4848
/** Serves as simple Prometheus sink (pull model), presenting metrics snapshot as HttpHandler. */

spark-operator/src/main/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/AppCleanUpStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import io.fabric8.kubernetes.client.KubernetesClient;
3434
import lombok.NoArgsConstructor;
3535
import lombok.extern.slf4j.Slf4j;
36-
import org.apache.commons.lang3.StringUtils;
3736

3837
import org.apache.spark.k8s.operator.SparkApplication;
3938
import org.apache.spark.k8s.operator.config.SparkOperatorConf;
@@ -48,6 +47,7 @@
4847
import org.apache.spark.k8s.operator.utils.ReconcilerUtils;
4948
import org.apache.spark.k8s.operator.utils.SparkAppStatusRecorder;
5049
import org.apache.spark.k8s.operator.utils.SparkAppStatusUtils;
50+
import org.apache.spark.k8s.operator.utils.StringUtils;
5151

5252
/**
5353
* Cleanup all secondary resources when application is deleted, or at the end of each attempt.

spark-operator/src/main/java/org/apache/spark/k8s/operator/utils/ClassLoadingUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.Set;
2626

2727
import lombok.extern.slf4j.Slf4j;
28-
import org.apache.commons.lang3.StringUtils;
2928

3029
import org.apache.spark.k8s.operator.listeners.BaseStatusListener;
3130

spark-operator/src/main/java/org/apache/spark/k8s/operator/utils/SparkExceptionUtils.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import static java.net.HttpURLConnection.HTTP_CONFLICT;
2323

2424
import io.fabric8.kubernetes.client.KubernetesClientException;
25-
import org.apache.commons.lang3.StringUtils;
26-
import org.apache.commons.lang3.exception.ExceptionUtils;
2725

2826
/** Utility class for Spark exceptions. */
2927
public final class SparkExceptionUtils {
@@ -51,6 +49,6 @@ public static boolean isConflictForExistingResource(KubernetesClientException e)
5149
* @return A string containing the stack trace of the exception.
5250
*/
5351
public static String buildGeneralErrorMessage(Exception e) {
54-
return ExceptionUtils.getStackTrace(e);
52+
return StringUtils.getStackTrace(e);
5553
}
5654
}

spark-operator/src/main/java/org/apache/spark/k8s/operator/utils/Utils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import io.fabric8.kubernetes.api.model.HasMetadata;
4242
import io.javaoperatorsdk.operator.processing.event.ResourceID;
4343
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper;
44-
import org.apache.commons.lang3.StringUtils;
4544

4645
import org.apache.spark.k8s.operator.Constants;
4746
import org.apache.spark.k8s.operator.SparkApplication;

spark-operator/src/test/java/org/apache/spark/k8s/operator/config/SparkOperatorConfManagerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
import java.io.IOException;
2323
import java.util.Collections;
2424

25-
import org.apache.commons.lang3.StringUtils;
2625
import org.junit.jupiter.api.Assertions;
2726
import org.junit.jupiter.api.Test;
2827

28+
import org.apache.spark.k8s.operator.utils.StringUtils;
29+
2930
class SparkOperatorConfManagerTest {
3031
@Test
3132
void testLoadPropertiesFromInitFile() throws IOException {

0 commit comments

Comments
 (0)