Skip to content

Commit 7298f06

Browse files
committed
[SPARK-53814] Use Java List.of instead of Collections.(empty|singleton)List
1 parent 9f5d04e commit 7298f06

File tree

13 files changed

+45
-74
lines changed

13 files changed

+45
-74
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import java.io.IOException;
2626
import java.util.ArrayList;
27-
import java.util.Collections;
2827
import java.util.List;
2928
import java.util.Map;
3029
import java.util.Properties;
@@ -94,15 +93,15 @@ public void handle(HttpExchange exchange) throws IOException {
9493
exchange,
9594
HTTP_OK,
9695
formatMetricsSnapshot(),
97-
Map.of("Content-Type", Collections.singletonList("text/plain;version=0.0.4")));
96+
Map.of("Content-Type", List.of("text/plain;version=0.0.4")));
9897
} else {
9998
HttpServletRequest httpServletRequest = null;
10099
String value = getMetricsSnapshot(httpServletRequest);
101100
sendMessage(
102101
exchange,
103102
HTTP_OK,
104103
String.join("\n", filterNonEmptyRecords(value)),
105-
Map.of("Content-Type", Collections.singletonList("text/plain;version=0.0.4")));
104+
Map.of("Content-Type", List.of("text/plain;version=0.0.4")));
106105
}
107106
}
108107

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import static org.apache.spark.k8s.operator.utils.Utils.commonResourceLabelsStr;
2626

2727
import java.util.ArrayList;
28-
import java.util.Collections;
2928
import java.util.List;
3029

3130
import io.fabric8.kubernetes.api.model.Pod;
@@ -178,20 +177,16 @@ protected List<AppReconcileStep> getReconcileSteps(final SparkApplication app) {
178177
steps.add(
179178
new AppResourceObserveStep(
180179
List.of(new AppDriverStartObserver(), new AppDriverReadyObserver())));
181-
steps.add(
182-
new AppResourceObserveStep(Collections.singletonList(new AppDriverRunningObserver())));
183-
steps.add(
184-
new AppResourceObserveStep(Collections.singletonList(new AppDriverTimeoutObserver())));
180+
steps.add(new AppResourceObserveStep(List.of(new AppDriverRunningObserver())));
181+
steps.add(new AppResourceObserveStep(List.of(new AppDriverTimeoutObserver())));
185182
}
186183
case DriverReady,
187184
InitializedBelowThresholdExecutors,
188185
RunningHealthy,
189186
RunningWithBelowThresholdExecutors -> {
190187
steps.add(new AppRunningStep());
191-
steps.add(
192-
new AppResourceObserveStep(Collections.singletonList(new AppDriverRunningObserver())));
193-
steps.add(
194-
new AppResourceObserveStep(Collections.singletonList(new AppDriverTimeoutObserver())));
188+
steps.add(new AppResourceObserveStep(List.of(new AppDriverRunningObserver())));
189+
steps.add(new AppResourceObserveStep(List.of(new AppDriverTimeoutObserver())));
195190
}
196191
default -> steps.add(new AppUnknownStateStep());
197192
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import static org.apache.spark.k8s.operator.Constants.*;
2323
import static org.apache.spark.k8s.operator.reconciler.ReconcileProgress.completeAndDefaultRequeue;
2424

25-
import java.util.Collections;
25+
import java.util.List;
2626
import java.util.Set;
2727

2828
import io.fabric8.kubernetes.api.model.Pod;
@@ -85,8 +85,7 @@ public ReconcileProgress reconcile(
8585
}
8686
}
8787
if (proposedStateSummary.equals(prevStateSummary)) {
88-
return observeDriver(
89-
context, statusRecorder, Collections.singletonList(new AppDriverRunningObserver()));
88+
return observeDriver(context, statusRecorder, List.of(new AppDriverRunningObserver()));
9089
} else {
9190
ApplicationStatus updatedStatus =
9291
context

spark-operator/src/test/java/org/apache/spark/k8s/operator/metrics/source/KubernetesMetricsInterceptorTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import static org.junit.jupiter.api.Assertions.assertThrows;
2323

2424
import java.util.Arrays;
25-
import java.util.Collections;
2625
import java.util.HashMap;
2726
import java.util.List;
2827
import java.util.Map;
@@ -66,7 +65,7 @@ void cleanUp() {
6665
@Order(1)
6766
void testMetricsEnabled() {
6867
KubernetesMetricsInterceptor metricsInterceptor = new KubernetesMetricsInterceptor();
69-
List<Interceptor> interceptors = Collections.singletonList(metricsInterceptor);
68+
List<Interceptor> interceptors = List.of(metricsInterceptor);
7069
try (KubernetesClient client =
7170
KubernetesClientFactory.buildKubernetesClient(
7271
interceptors, kubernetesClient.getConfiguration())) {
@@ -106,7 +105,7 @@ void testMetricsEnabled() {
106105
@Order(2)
107106
void testWhenKubernetesServerNotWorking() {
108107
KubernetesMetricsInterceptor metricsInterceptor = new KubernetesMetricsInterceptor();
109-
List<Interceptor> interceptors = Collections.singletonList(metricsInterceptor);
108+
List<Interceptor> interceptors = List.of(metricsInterceptor);
110109
try (KubernetesClient client =
111110
KubernetesClientFactory.buildKubernetesClient(
112111
interceptors, kubernetesClient.getConfiguration())) {

spark-operator/src/test/java/org/apache/spark/k8s/operator/probe/HealthProbeTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import static org.mockito.Mockito.when;
2626

2727
import java.util.Arrays;
28-
import java.util.Collections;
2928
import java.util.HashMap;
3029
import java.util.List;
3130
import java.util.Map;
@@ -108,7 +107,7 @@ public boolean isStarted() {
108107

109108
@Test
110109
void testHealthProbeWithInformerHealthWithMultiOperators() {
111-
HealthProbe healthyProbe = new HealthProbe(operators, Collections.emptyList());
110+
HealthProbe healthyProbe = new HealthProbe(operators, List.of());
112111
isRunning.set(true);
113112
assertFalse(
114113
healthyProbe.isHealthy(),
@@ -128,8 +127,7 @@ void testHealthProbeWithInformerHealthWithMultiOperators() {
128127

129128
@Test
130129
void testHealthProbeWithInformerHealthWithSingleOperator() {
131-
HealthProbe healthyProbe =
132-
new HealthProbe(Collections.singletonList(operator), Collections.emptyList());
130+
HealthProbe healthyProbe = new HealthProbe(List.of(operator), List.of());
133131
assertFalse(healthyProbe.isHealthy(), "Health Probe should fail when operator is not running");
134132
isRunning.set(true);
135133
unhealthyEventSources.put(
@@ -143,8 +141,7 @@ void testHealthProbeWithInformerHealthWithSingleOperator() {
143141
@Test
144142
void testHealthProbeWithSentinelHealthWithMultiOperators() {
145143
var sentinelManager = mock(SentinelManager.class);
146-
HealthProbe healthyProbe =
147-
new HealthProbe(operators, Collections.singletonList(sentinelManager));
144+
HealthProbe healthyProbe = new HealthProbe(operators, List.of(sentinelManager));
148145
isRunning.set(true);
149146
isRunning2.set(true);
150147
when(sentinelManager.allSentinelsAreHealthy()).thenReturn(false);

spark-operator/src/test/java/org/apache/spark/k8s/operator/probe/ProbeServiceTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.net.HttpURLConnection;
3232
import java.net.MalformedURLException;
3333
import java.net.URL;
34-
import java.util.Collections;
3534
import java.util.HashMap;
3635
import java.util.List;
3736

@@ -56,9 +55,7 @@ void testHealthProbeEndpointWithStaticProperties() throws Exception {
5655
when(runtimeInfo.unhealthyInformerWrappingEventSourceHealthIndicator())
5756
.thenReturn(new HashMap<>());
5857
when(sentinelManager.allSentinelsAreHealthy()).thenReturn(true);
59-
ProbeService probeService =
60-
new ProbeService(
61-
Collections.singletonList(operator), Collections.singletonList(sentinelManager), null);
58+
ProbeService probeService = new ProbeService(List.of(operator), List.of(sentinelManager), null);
6259
probeService.start();
6360
hitHealthyEndpoint();
6461
probeService.stop();
@@ -83,8 +80,7 @@ void testHealthProbeEndpointWithDynamicProperties() throws Exception {
8380
.thenReturn(new HashMap<>());
8481
when(sentinelManager.allSentinelsAreHealthy()).thenReturn(true);
8582
ProbeService probeService =
86-
new ProbeService(
87-
List.of(operator, operator1), Collections.singletonList(sentinelManager), null);
83+
new ProbeService(List.of(operator, operator1), List.of(sentinelManager), null);
8884
probeService.start();
8985
hitHealthyEndpoint();
9086
probeService.stop();
@@ -110,8 +106,7 @@ void testReadinessProbeEndpointWithDynamicProperties() throws Exception {
110106
.thenReturn(new HashMap<>());
111107
when(operator1.getKubernetesClient()).thenReturn(client);
112108
ProbeService probeService =
113-
new ProbeService(
114-
List.of(operator, operator1), Collections.singletonList(sentinelManager), null);
109+
new ProbeService(List.of(operator, operator1), List.of(sentinelManager), null);
115110
probeService.start();
116111
hitStartedUpEndpoint();
117112
probeService.stop();

spark-operator/src/test/java/org/apache/spark/k8s/operator/reconciler/SparkAppReconcilerTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import static org.mockito.Mockito.mockConstruction;
3030
import static org.mockito.Mockito.when;
3131

32-
import java.util.Collections;
32+
import java.util.List;
3333
import java.util.Optional;
3434

3535
import io.fabric8.kubernetes.api.model.Pod;
@@ -96,8 +96,8 @@ void testCleanupRunningApp() {
9696
when(mock.getClient()).thenReturn(mockClient);
9797
when(mock.getDriverPod()).thenReturn(Optional.of(mockDriver));
9898
when(mock.getDriverPodSpec()).thenReturn(mockDriver);
99-
when(mock.getDriverPreResourcesSpec()).thenReturn(Collections.emptyList());
100-
when(mock.getDriverResourcesSpec()).thenReturn(Collections.emptyList());
99+
when(mock.getDriverPreResourcesSpec()).thenReturn(List.of());
100+
when(mock.getDriverResourcesSpec()).thenReturn(List.of());
101101
});
102102
MockedStatic<ReconcilerUtils> utils = Mockito.mockStatic(ReconcilerUtils.class)) {
103103
// delete running app
@@ -128,8 +128,8 @@ void testCleanupAppTerminatedWithoutReleaseResources() {
128128
when(mock.getClient()).thenReturn(mockClient);
129129
when(mock.getDriverPod()).thenReturn(Optional.of(mockDriver));
130130
when(mock.getDriverPodSpec()).thenReturn(mockDriver);
131-
when(mock.getDriverPreResourcesSpec()).thenReturn(Collections.emptyList());
132-
when(mock.getDriverResourcesSpec()).thenReturn(Collections.emptyList());
131+
when(mock.getDriverPreResourcesSpec()).thenReturn(List.of());
132+
when(mock.getDriverResourcesSpec()).thenReturn(List.of());
133133
});
134134
MockedStatic<ReconcilerUtils> utils = Mockito.mockStatic(ReconcilerUtils.class)) {
135135
// delete app
@@ -160,8 +160,8 @@ void testCleanupAppTerminatedResourceReleased() {
160160
(mock, context) -> {
161161
when(mock.getResource()).thenReturn(app);
162162
when(mock.getClient()).thenReturn(mockClient);
163-
when(mock.getDriverPreResourcesSpec()).thenReturn(Collections.emptyList());
164-
when(mock.getDriverResourcesSpec()).thenReturn(Collections.emptyList());
163+
when(mock.getDriverPreResourcesSpec()).thenReturn(List.of());
164+
when(mock.getDriverResourcesSpec()).thenReturn(List.of());
165165
});
166166
MockedStatic<ReconcilerUtils> utils = Mockito.mockStatic(ReconcilerUtils.class)) {
167167
// delete app

spark-operator/src/test/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/AppCleanUpStepTest.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import java.time.Duration;
3333
import java.time.Instant;
34-
import java.util.Collections;
3534
import java.util.List;
3635
import java.util.Optional;
3736

@@ -170,8 +169,8 @@ void onDemandCleanupForRunningAppExpectDelete() {
170169
when(mockAppContext.getClient()).thenReturn(mockClient);
171170
Pod driverPod = mock(Pod.class);
172171
when(mockAppContext.getDriverPod()).thenReturn(Optional.of(driverPod));
173-
when(mockAppContext.getDriverPreResourcesSpec()).thenReturn(Collections.emptyList());
174-
when(mockAppContext.getDriverResourcesSpec()).thenReturn(Collections.emptyList());
172+
when(mockAppContext.getDriverPreResourcesSpec()).thenReturn(List.of());
173+
when(mockAppContext.getDriverResourcesSpec()).thenReturn(List.of());
175174
when(mockRecorder.persistStatus(eq(mockAppContext), any())).thenReturn(true);
176175
when(mockRecorder.appendNewStateAndPersist(eq(mockAppContext), any())).thenReturn(true);
177176

@@ -260,8 +259,8 @@ void onDemandCleanupForTerminatedAppExpectDelete() {
260259
when(mockAppContext.getClient()).thenReturn(mockClient);
261260
Pod driverPod = mock(Pod.class);
262261
when(mockAppContext.getDriverPod()).thenReturn(Optional.of(driverPod));
263-
when(mockAppContext.getDriverPreResourcesSpec()).thenReturn(Collections.emptyList());
264-
when(mockAppContext.getDriverResourcesSpec()).thenReturn(Collections.emptyList());
262+
when(mockAppContext.getDriverPreResourcesSpec()).thenReturn(List.of());
263+
when(mockAppContext.getDriverResourcesSpec()).thenReturn(List.of());
265264
when(mockRecorder.persistStatus(eq(mockAppContext), any())).thenReturn(true);
266265
when(mockRecorder.appendNewStateAndPersist(eq(mockAppContext), any())).thenReturn(true);
267266

@@ -314,14 +313,12 @@ void cleanupForAppExpectDeleteWithRecompute() {
314313
ConfigMap resource2 = mock(ConfigMap.class);
315314
when(mockAppContext1.getDriverPod()).thenReturn(Optional.of(driverPod));
316315
when(mockAppContext1.getDriverPodSpec()).thenReturn(driverPodSpec);
317-
when(mockAppContext1.getDriverPreResourcesSpec())
318-
.thenReturn(Collections.singletonList(resource1));
319-
when(mockAppContext1.getDriverResourcesSpec()).thenReturn(Collections.singletonList(resource2));
316+
when(mockAppContext1.getDriverPreResourcesSpec()).thenReturn(List.of(resource1));
317+
when(mockAppContext1.getDriverResourcesSpec()).thenReturn(List.of(resource2));
320318
when(mockAppContext2.getDriverPod()).thenReturn(Optional.of(driverPod));
321319
when(mockAppContext2.getDriverPodSpec()).thenReturn(driverPodSpec);
322-
when(mockAppContext2.getDriverPreResourcesSpec())
323-
.thenReturn(Collections.singletonList(resource1));
324-
when(mockAppContext2.getDriverResourcesSpec()).thenReturn(Collections.singletonList(resource2));
320+
when(mockAppContext2.getDriverPreResourcesSpec()).thenReturn(List.of(resource1));
321+
when(mockAppContext2.getDriverResourcesSpec()).thenReturn(List.of(resource2));
325322
when(mockRecorder.persistStatus(any(), any())).thenReturn(true);
326323
when(mockRecorder.appendNewStateAndPersist(any(), any())).thenReturn(true);
327324

spark-operator/src/test/java/org/apache/spark/k8s/operator/reconciler/reconcilesteps/AppInitStepTest.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import static org.mockito.Mockito.verify;
2626
import static org.mockito.Mockito.when;
2727

28-
import java.util.Collections;
2928
import java.util.List;
3029
import java.util.Map;
3130

@@ -103,10 +102,9 @@ void driverResourcesHaveOwnerReferencesToDriver() {
103102
SparkApplication application = new SparkApplication();
104103
application.setMetadata(applicationMetadata);
105104
when(mocksparkAppContext.getResource()).thenReturn(application);
106-
when(mocksparkAppContext.getDriverPreResourcesSpec()).thenReturn(Collections.emptyList());
105+
when(mocksparkAppContext.getDriverPreResourcesSpec()).thenReturn(List.of());
107106
when(mocksparkAppContext.getDriverPodSpec()).thenReturn(driverPodSpec);
108-
when(mocksparkAppContext.getDriverResourcesSpec())
109-
.thenReturn(Collections.singletonList(resourceConfigMapSpec));
107+
when(mocksparkAppContext.getDriverResourcesSpec()).thenReturn(List.of(resourceConfigMapSpec));
110108
when(mocksparkAppContext.getClient()).thenReturn(kubernetesClient);
111109
when(recorder.appendNewStateAndPersist(any(), any())).thenReturn(true);
112110
when(recorder.persistStatus(any(), any())).thenReturn(true);
@@ -137,9 +135,9 @@ void createdPreResourcesPatchedWithOwnerReferencesToDriver() {
137135
application.setMetadata(applicationMetadata);
138136
when(mocksparkAppContext.getResource()).thenReturn(application);
139137
when(mocksparkAppContext.getDriverPreResourcesSpec())
140-
.thenReturn(Collections.singletonList(preResourceConfigMapSpec));
138+
.thenReturn(List.of(preResourceConfigMapSpec));
141139
when(mocksparkAppContext.getDriverPodSpec()).thenReturn(driverPodSpec);
142-
when(mocksparkAppContext.getDriverResourcesSpec()).thenReturn(Collections.emptyList());
140+
when(mocksparkAppContext.getDriverResourcesSpec()).thenReturn(List.of());
143141
when(recorder.appendNewStateAndPersist(any(), any())).thenReturn(true);
144142
when(recorder.persistStatus(any(), any())).thenReturn(true);
145143

@@ -201,10 +199,9 @@ void appInitStepShouldBeIdempotentWhenStatusUpdateFails() {
201199
SparkApplication application = new SparkApplication();
202200
application.setMetadata(applicationMetadata);
203201
when(mocksparkAppContext.getResource()).thenReturn(application);
204-
when(mocksparkAppContext.getDriverPreResourcesSpec()).thenReturn(Collections.emptyList());
202+
when(mocksparkAppContext.getDriverPreResourcesSpec()).thenReturn(List.of());
205203
when(mocksparkAppContext.getDriverPodSpec()).thenReturn(driverPodSpec);
206-
when(mocksparkAppContext.getDriverResourcesSpec())
207-
.thenReturn(Collections.singletonList(resourceConfigMapSpec));
204+
when(mocksparkAppContext.getDriverResourcesSpec()).thenReturn(List.of(resourceConfigMapSpec));
208205
when(mocksparkAppContext.getClient()).thenReturn(kubernetesClient);
209206
when(recorder.appendNewStateAndPersist(any(), any())).thenReturn(false, true);
210207
when(recorder.persistStatus(any(), any())).thenReturn(false, true);

spark-submission-worker/src/main/java/org/apache/spark/k8s/operator/SparkAppResourceSpec.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import java.util.ArrayList;
2323
import java.util.Collection;
24-
import java.util.Collections;
2524
import java.util.List;
2625
import java.util.stream.Collectors;
2726

@@ -170,7 +169,7 @@ private SparkPod addConfigMap(SparkPod pod, Map<String, String> confFilesMap) {
170169
private List<HasMetadata> configureDriverServerIngress(
171170
SparkPod pod, List<DriverServiceIngressSpec> driverServiceIngressList) {
172171
if (driverServiceIngressList == null || driverServiceIngressList.isEmpty()) {
173-
return Collections.emptyList();
172+
return List.of();
174173
}
175174
return driverServiceIngressList.stream()
176175
.map(spec -> DriverServiceIngressUtils.buildIngressService(spec, pod.pod().getMetadata()))

0 commit comments

Comments
 (0)