3232import org .apache .flink .kubernetes .operator .api .status .JobManagerDeploymentStatus ;
3333import org .apache .flink .kubernetes .operator .api .status .ReconciliationState ;
3434import org .apache .flink .kubernetes .operator .config .KubernetesOperatorConfigOptions ;
35- import org .apache .flink .kubernetes .operator .controller .FlinkResourceContext ;
3635import org .apache .flink .kubernetes .operator .reconciler .TestReconcilerAdapter ;
3736import org .apache .flink .runtime .client .JobStatusMessage ;
3837
4948import java .util .Optional ;
5049import java .util .Set ;
5150import java .util .concurrent .atomic .AtomicInteger ;
52- import java .util .stream .Collectors ;
5351
5452import static org .junit .jupiter .api .Assertions .assertEquals ;
5553import static org .junit .jupiter .api .Assertions .assertFalse ;
@@ -151,8 +149,7 @@ public void testSetOwnerReference() throws Exception {
151149 }
152150
153151 @ Test
154- public void testGetUnmanagedJobs () throws Exception {
155- // Create a session cluster deployment with blocking enabled
152+ public void testGetNonTerminalJobs () throws Exception {
156153 FlinkDeployment deployment = TestUtils .buildSessionCluster ();
157154 deployment
158155 .getSpec ()
@@ -166,7 +163,6 @@ public void testGetUnmanagedJobs() throws Exception {
166163 .getFlinkConfiguration ()
167164 .get (KubernetesOperatorConfigOptions .BLOCK_ON_UNMANAGED_JOBS .key ()));
168165
169- // First, deploy the session cluster to set up proper state
170166 reconciler .reconcile (deployment , flinkService .getContext ());
171167
172168 // Verify deployment is in DEPLOYED state
@@ -181,7 +177,7 @@ public void testGetUnmanagedJobs() throws Exception {
181177 JobID unmanagedTerminatedJobId = new JobID ();
182178 JobID unmanagedRunningJobId2 = new JobID ();
183179
184- // Add a mix of managed and unmanaged jobs to the testing service
180+ // Add jobs to the testing service
185181 flinkService
186182 .listJobs ()
187183 .add (
@@ -253,60 +249,59 @@ public void testGetUnmanagedJobs() throws Exception {
253249 sessionJobs .add (managedSessionJob1 );
254250 sessionJobs .add (managedSessionJob2 );
255251
256- // Test with blocking enabled - should identify running unmanaged jobs correctly
252+ // Test with blocking enabled - should identify all non-terminal jobs
257253 var context = TestUtils .createContextWithReadyFlinkDeployment (kubernetesClient );
258254 var resourceContext = getResourceContext (deployment , context );
259255
260- // Use reflection to access the private getUnmanagedJobs method
261- var sessionReconciler = reconciler .getReconciler ();
262- var getUnmanagedJobsMethod =
263- sessionReconciler
264- .getClass ()
265- .getDeclaredMethod (
266- "getUnmanagedJobs" , FlinkResourceContext .class , Set .class );
267- getUnmanagedJobsMethod .setAccessible (true );
268-
269- @ SuppressWarnings ("unchecked" )
270- var unmanagedJobs =
271- (java .util .Set <String >)
272- getUnmanagedJobsMethod .invoke (
273- sessionReconciler , resourceContext , sessionJobs );
274-
275- // Verify only RUNNING unmanaged jobs are identified - should be 2
276- assertEquals (2 , unmanagedJobs .size (), "Should identify exactly 2 running unmanaged jobs" );
277-
278- // Verify that none of the managed job IDs are in the unmanaged list
279- Set <String > managedJobIds =
280- sessionJobs .stream ()
281- .map (job -> job .getStatus ().getJobStatus ().getJobId ())
282- .collect (Collectors .toSet ());
283-
284- for (String managedJobId : managedJobIds ) {
285- assertFalse (
286- unmanagedJobs .contains (managedJobId ),
287- "Managed job " + managedJobId + " should NOT be in unmanaged jobs" );
288- }
256+ var sessionReconciler = (SessionReconciler ) reconciler .getReconciler ();
257+ Set <JobID > nonTerminalJobs = sessionReconciler .getNonTerminalJobs (resourceContext );
258+
259+ // Verify all non-terminal jobs are identified - should be 4 (2 managed + 2 unmanaged
260+ // running)
261+ assertEquals (4 , nonTerminalJobs .size (), "Should identify exactly 4 non-terminal jobs" );
262+
263+ assertTrue (
264+ nonTerminalJobs .contains (unmanagedRunningJobId1 ),
265+ "Should contain unmanagedRunningJobId1" );
266+ assertTrue (
267+ nonTerminalJobs .contains (unmanagedRunningJobId2 ),
268+ "Should contain unmanagedRunningJobId2" );
289269
290- // Verify managed jobs are properly set up (we should have 2 managed jobs)
291- assertEquals (2 , managedJobIds .size (), "Should have 2 managed jobs configured" );
270+ // Verify terminated job is not included
271+ assertFalse (
272+ nonTerminalJobs .contains (unmanagedTerminatedJobId ),
273+ "Should not contain terminated job" );
274+
275+ // Test scenario with only unmanaged jobs
276+ flinkService
277+ .listJobs ()
278+ .removeIf (
279+ job ->
280+ job .f1 .getJobId ().equals (managedJobId1 )
281+ || job .f1 .getJobId ().equals (managedJobId2 ));
282+
283+ Set <JobID > nonTerminalJobsAfterSessionJobsRemoval =
284+ sessionReconciler .getNonTerminalJobs (resourceContext );
285+
286+ assertEquals (
287+ 2 ,
288+ nonTerminalJobsAfterSessionJobsRemoval .size (),
289+ "Should have 2 non-terminal jobs when sessionjobs are deleted" );
292290
293- // Test scenario with no running unmanaged jobs
291+ // Test scenario with no running jobs
294292 flinkService
295293 .listJobs ()
296294 .removeIf (
297295 job ->
298296 job .f1 .getJobId ().equals (unmanagedRunningJobId1 )
299297 || job .f1 .getJobId ().equals (unmanagedRunningJobId2 ));
300298
301- @ SuppressWarnings ("unchecked" )
302- var unmanagedJobsAfterRemoval =
303- (java .util .Set <String >)
304- getUnmanagedJobsMethod .invoke (
305- sessionReconciler , resourceContext , sessionJobs );
299+ Set <JobID > nonTerminalJobsAfterRemoval =
300+ sessionReconciler .getNonTerminalJobs (resourceContext );
306301
307302 assertEquals (
308303 0 ,
309- unmanagedJobsAfterRemoval .size (),
310- "Should have no unmanaged jobs when only managed and terminated jobs exist" );
304+ nonTerminalJobsAfterRemoval .size (),
305+ "Should have no non-terminal jobs when only terminated jobs exist" );
311306 }
312307}
0 commit comments