2222import java .util .Arrays ;
2323import java .util .Collection ;
2424import java .util .Collections ;
25+ import java .util .Iterator ;
2526import java .util .List ;
2627import java .util .Map ;
2728import java .util .UUID ;
2829import java .util .concurrent .CountDownLatch ;
29- import java .util .concurrent .atomic .AtomicReference ;
30+ import java .util .concurrent .atomic .AtomicInteger ;
3031import java .util .stream .Collectors ;
3132
3233import org .apache .ignite .Ignite ;
3536import org .apache .ignite .compute .ComputeJob ;
3637import org .apache .ignite .compute .ComputeJobResult ;
3738import org .apache .ignite .compute .ComputeTaskAdapter ;
39+ import org .apache .ignite .compute .ComputeTaskFuture ;
3840import org .apache .ignite .configuration .IgniteConfiguration ;
3941import org .apache .ignite .internal .IgniteEx ;
4042import org .apache .ignite .internal .client .thin .ServicesTest ;
43+ import org .apache .ignite .internal .managers .systemview .GridSystemViewManager ;
4144import org .apache .ignite .internal .processors .security .impl .TestSecurityData ;
4245import org .apache .ignite .internal .processors .security .impl .TestSecurityPluginProvider ;
4346import org .apache .ignite .internal .util .typedef .F ;
47+ import org .apache .ignite .internal .util .typedef .X ;
48+ import org .apache .ignite .lang .IgniteFutureCancelledException ;
4449import org .apache .ignite .lang .IgniteProductVersion ;
4550import org .apache .ignite .plugin .security .SecurityPermission ;
4651import org .apache .ignite .plugin .security .SecurityPermissionSet ;
4752import org .apache .ignite .plugin .security .SecurityPermissionSetBuilder ;
4853import org .apache .ignite .services .ServiceConfiguration ;
4954import org .apache .ignite .services .ServiceDescriptor ;
50- import org .apache .ignite .spi .systemview .view .ComputeJobView ;
51- import org .apache .ignite .spi .systemview .view .SystemView ;
55+ import org .apache .ignite .spi .systemview .view .ComputeTaskView ;
5256import org .apache .ignite .util .GridCommandHandlerAbstractTest ;
5357import org .jetbrains .annotations .NotNull ;
5458import org .jetbrains .annotations .Nullable ;
5559import org .junit .Test ;
5660import org .junit .runners .Parameterized ;
5761
5862import static java .util .Arrays .asList ;
63+ import static java .util .concurrent .TimeUnit .MILLISECONDS ;
5964import static org .apache .ignite .internal .IgniteNodeAttributes .ATTR_BUILD_VER ;
6065import static org .apache .ignite .internal .commandline .ArgumentParser .CMD_PASSWORD ;
6166import static org .apache .ignite .internal .commandline .ArgumentParser .CMD_USER ;
6267import static org .apache .ignite .internal .commandline .CommandHandler .EXIT_CODE_OK ;
6368import static org .apache .ignite .internal .commandline .CommandHandler .EXIT_CODE_UNEXPECTED_ERROR ;
6469import static org .apache .ignite .internal .processors .job .GridJobProcessor .JOBS_VIEW ;
70+ import static org .apache .ignite .internal .processors .task .GridTaskProcessor .TASKS_VIEW ;
6571import static org .apache .ignite .internal .util .IgniteUtils .resolveIgnitePath ;
66- import static org .apache .ignite .plugin .security .SecurityPermission .ADMIN_OPS ;
6772import static org .apache .ignite .plugin .security .SecurityPermission .ADMIN_ROLLING_UPGRADE ;
6873import static org .apache .ignite .plugin .security .SecurityPermission .CACHE_CREATE ;
6974import static org .apache .ignite .plugin .security .SecurityPermission .CACHE_DESTROY ;
@@ -87,9 +92,6 @@ public class SecurityCommandHandlerPermissionsTest extends GridCommandHandlerAbs
8792 /** */
8893 public static final String DEFAULT_PWD = "pwd" ;
8994
90- /** */
91- public static CountDownLatch computeLatch ;
92-
9395 /** */
9496 @ Parameterized .Parameters (name = "cmdHnd={0}" )
9597 public static List <String > commandHandlers () {
@@ -103,6 +105,8 @@ public static List<String> commandHandlers() {
103105 persistenceEnable (false );
104106
105107 injectTestSystemOut ();
108+
109+ TestJob .CANCELLED_JOB_CNT .set (0 );
106110 }
107111
108112 /** {@inheritDoc} */
@@ -176,22 +180,22 @@ public void testRollingUpgrade() throws Exception {
176180
177181 List <String > cmdArgs = asList ("--rolling-upgrade" , "enable" , targetVerStr );
178182
179- assertEquals (EXIT_CODE_UNEXPECTED_ERROR , execute ( enrichWithConnectionArguments ( cmdArgs , TEST_NO_PERMISSIONS_LOGIN ) ));
183+ assertEquals (EXIT_CODE_UNEXPECTED_ERROR , executeOnBehalf ( cmdArgs , TEST_NO_PERMISSIONS_LOGIN ));
180184
181185 assertFalse (ign .context ().rollingUpgrade ().enabled ());
182186
183- assertEquals (EXIT_CODE_OK , execute ( enrichWithConnectionArguments ( cmdArgs , TEST_LOGIN ) ));
187+ assertEquals (EXIT_CODE_OK , executeOnBehalf ( cmdArgs , TEST_LOGIN ));
184188
185189 assertTrue (ign .context ().rollingUpgrade ().enabled ());
186190 assertEquals (IgniteProductVersion .fromString (targetVerStr ), ign .context ().rollingUpgrade ().versions ().get2 ());
187191
188192 cmdArgs = asList ("--rolling-upgrade" , "disable" );
189193
190- assertEquals (EXIT_CODE_UNEXPECTED_ERROR , execute ( enrichWithConnectionArguments ( cmdArgs , TEST_NO_PERMISSIONS_LOGIN ) ));
194+ assertEquals (EXIT_CODE_UNEXPECTED_ERROR , executeOnBehalf ( cmdArgs , TEST_NO_PERMISSIONS_LOGIN ));
191195
192196 assertTrue (ign .context ().rollingUpgrade ().enabled ());
193197
194- assertEquals (EXIT_CODE_OK , execute ( enrichWithConnectionArguments ( cmdArgs , TEST_LOGIN ) ));
198+ assertEquals (EXIT_CODE_OK , executeOnBehalf ( cmdArgs , TEST_LOGIN ));
195199
196200 assertFalse (ign .context ().rollingUpgrade ().enabled ());
197201 }
@@ -219,10 +223,10 @@ public void testServiceCancel() throws Exception {
219223
220224 Collection <ServiceDescriptor > svcs = ignite .services ().serviceDescriptors ();
221225
222- assertEquals (EXIT_CODE_UNEXPECTED_ERROR , execute ( enrichWithConnectionArguments ( cmdArgs , TEST_NO_PERMISSIONS_LOGIN ) ));
226+ assertEquals (EXIT_CODE_UNEXPECTED_ERROR , executeOnBehalf ( cmdArgs , TEST_NO_PERMISSIONS_LOGIN ));
223227 assertEquals (1 , svcs .size ());
224228
225- assertEquals (EXIT_CODE_OK , execute ( enrichWithConnectionArguments ( cmdArgs , TEST_LOGIN ) ));
229+ assertEquals (EXIT_CODE_OK , executeOnBehalf ( cmdArgs , TEST_LOGIN ));
226230
227231 svcs = ignite .services ().serviceDescriptors ();
228232 assertEquals (0 , svcs .size ());
@@ -233,42 +237,50 @@ public void testServiceCancel() throws Exception {
233237 public void testTaskCancel () throws Exception {
234238 IgniteEx ignite = startGrid (
235239 0 ,
236- userData (TEST_NO_PERMISSIONS_LOGIN , taskPermission (TestTask .class .getName ())),
237- userData (TEST_LOGIN , taskPermission (TestTask .class .getName (), TASK_CANCEL ))
240+ userData (TEST_NO_PERMISSIONS_LOGIN , NO_PERMISSIONS ),
241+ userData (TEST_LOGIN , SecurityPermissionSetBuilder .create ()
242+ .defaultAllowAll (false )
243+ .appendTaskPermissions (TestTask .class .getName (), TASK_CANCEL )
244+ .build ())
238245 );
239246
240- computeLatch = new CountDownLatch ( 1 );
247+ ComputeTaskFuture < ComputeJobResult > fut = ignite . compute (). executeAsync ( new TestTask (), null );
241248
242- ignite .compute ().executeAsync ( new TestTask (), null );
249+ GridSystemViewManager viewMgr = ignite .context ().systemView ( );
243250
244- try {
245- AtomicReference <ComputeJobView > jobViewHolder = new AtomicReference <>();
251+ assertTrue (waitForCondition (() -> viewMgr .view (TASKS_VIEW ).size () >= 1 , getTestTimeout ()));
252+ assertTrue (waitForCondition (() -> viewMgr .view (JOBS_VIEW ).size () >= 1 , getTestTimeout ()));
253+
254+ Iterator <ComputeTaskView > iter = viewMgr .<ComputeTaskView >view (TASKS_VIEW ).iterator ();
255+
256+ String sesId = iter .next ().sessionId ().toString ();
257+
258+ assertFalse (iter .hasNext ());
246259
247- boolean res = waitForCondition (() -> {
248- SystemView <ComputeJobView > jobs = ignite .context ().systemView ().view (JOBS_VIEW );
260+ Collection <String > cmdArgs = asList ("--kill" , "compute" , sesId );
249261
250- if (jobs .size () >= 1 ) {
251- assertEquals (1 , jobs .size ());
252- jobViewHolder .set (jobs .iterator ().next ());
253- return true ;
254- }
262+ assertEquals (EXIT_CODE_UNEXPECTED_ERROR , executeOnBehalf (cmdArgs , TEST_NO_PERMISSIONS_LOGIN ));
255263
256- return false ;
257- }, getTestTimeout ());
264+ assertFalse (fut .isDone ());
258265
259- assertTrue ( res );
266+ assertEquals ( 0 , TestJob . CANCELLED_JOB_CNT . get () );
260267
261- String sesId = jobViewHolder . get (). sessionId (). toString ( );
262- Collection < String > cmdArgs = asList ( "--kill" , "compute" , sesId );
268+ assertTrue ( waitForCondition (() -> viewMgr . view ( TASKS_VIEW ). size () >= 1 , getTestTimeout ()) );
269+ assertTrue ( waitForCondition (() -> viewMgr . view ( JOBS_VIEW ). size () >= 1 , getTestTimeout ()) );
263270
264- assertEquals (EXIT_CODE_UNEXPECTED_ERROR ,
265- execute (enrichWithConnectionArguments (cmdArgs , TEST_NO_PERMISSIONS_LOGIN )));
271+ assertEquals (EXIT_CODE_OK , executeOnBehalf (cmdArgs , TEST_LOGIN ));
266272
267- assertEquals (EXIT_CODE_OK , execute (enrichWithConnectionArguments (cmdArgs , TEST_LOGIN )));
273+ try {
274+ assertTrue (fut .get (getTestTimeout (), MILLISECONDS ).isCancelled ());
268275 }
269- finally {
270- computeLatch . countDown ( );
276+ catch ( Exception e ) {
277+ assertTrue ( X . hasCause ( e , IgniteFutureCancelledException . class ) );
271278 }
279+
280+ assertTrue (waitForCondition (() -> viewMgr .view (TASKS_VIEW ).size () == 0 , getTestTimeout ()));
281+ assertTrue (waitForCondition (() -> viewMgr .view (JOBS_VIEW ).size () == 0 , getTestTimeout ()));
282+
283+ assertEquals (1 , TestJob .CANCELLED_JOB_CNT .get ());
272284 }
273285
274286 /** */
@@ -292,13 +304,13 @@ private void checkCommandPermissions(Collection<String> cmdArgs, SecurityPermiss
292304
293305 ignite .createCache (DEFAULT_CACHE_NAME );
294306
295- assertEquals (EXIT_CODE_UNEXPECTED_ERROR , execute ( enrichWithConnectionArguments ( cmdArgs , TEST_NO_PERMISSIONS_LOGIN ) ));
307+ assertEquals (EXIT_CODE_UNEXPECTED_ERROR , executeOnBehalf ( cmdArgs , TEST_NO_PERMISSIONS_LOGIN ));
296308
297- // We are losing command failure cause for --cache clear commnad . See IGNITE-21023 for more details.
309+ // We are losing command failure cause for --cache clear command . See IGNITE-21023 for more details.
298310 if (!cmdArgs .containsAll (Arrays .asList ("--cache" , "clear" )))
299311 assertTrue (testOut .toString ().contains ("Authorization failed" ));
300312
301- assertEquals (EXIT_CODE_OK , execute ( enrichWithConnectionArguments ( cmdArgs , TEST_LOGIN ) ));
313+ assertEquals (EXIT_CODE_OK , executeOnBehalf ( cmdArgs , TEST_LOGIN ));
302314 }
303315
304316 /** */
@@ -331,15 +343,6 @@ private SecurityPermissionSet servicePermission(String name, SecurityPermission.
331343 .build ();
332344 }
333345
334- /** */
335- private SecurityPermissionSet taskPermission (String name , SecurityPermission ... perms ) {
336- return SecurityPermissionSetBuilder .create ()
337- .defaultAllowAll (false )
338- .appendSystemPermissions (ADMIN_OPS )
339- .appendTaskPermissions (name , perms )
340- .build ();
341- }
342-
343346 /** */
344347 private TestSecurityData userData (String login , SecurityPermissionSet perms ) {
345348 return new TestSecurityData (
@@ -351,41 +354,52 @@ private TestSecurityData userData(String login, SecurityPermissionSet perms) {
351354 }
352355
353356 /** */
354- private static class TestTask extends ComputeTaskAdapter <Object , Object > {
357+ private int executeOnBehalf (Collection <String > cmdArgs , String login ) {
358+ return execute (enrichWithConnectionArguments (cmdArgs , login ));
359+ }
360+
361+ /** */
362+ private static class TestTask extends ComputeTaskAdapter <Object , ComputeJobResult > {
355363 /** {@inheritDoc} */
356364 @ Override public @ NotNull Map <? extends ComputeJob , ClusterNode > map (
357365 List <ClusterNode > subgrid ,
358366 @ Nullable Object arg
359367 ) throws IgniteException {
360- return subgrid .stream ().filter (g -> !g .isClient ()).collect (Collectors .toMap (ignored -> job (), srv -> srv ));
368+ return subgrid .stream ().filter (g -> !g .isClient ()).collect (Collectors .toMap (ignored -> new TestJob (), srv -> srv ));
361369 }
362370
363371 /** {@inheritDoc} */
364- @ Override public @ Nullable Object reduce (List <ComputeJobResult > results ) throws IgniteException {
365- return null ;
366- }
372+ @ Override public @ Nullable ComputeJobResult reduce (List <ComputeJobResult > results ) throws IgniteException {
373+ assertTrue (results .size () == 1 );
367374
368- /** */
369- protected ComputeJob job () {
370- return new TestJob ();
375+ return results .get (0 );
371376 }
372377 }
373378
374379 /** */
375380 private static class TestJob implements ComputeJob {
381+ /** */
382+ private final CountDownLatch jobBlockedLatch = new CountDownLatch (1 );
383+
384+ /** */
385+ private static final AtomicInteger CANCELLED_JOB_CNT = new AtomicInteger ();
386+
376387 /** {@inheritDoc} */
377388 @ Override public void cancel () {
378- //No-op
389+ jobBlockedLatch .countDown ();
390+
391+ CANCELLED_JOB_CNT .incrementAndGet ();
379392 }
380393
381394 /** {@inheritDoc} */
382- @ Override public Object execute () {
395+ @ Override public Object execute () throws IgniteException {
383396 try {
384- computeLatch .await ();
397+ assertTrue ( jobBlockedLatch .await (5_000 , MILLISECONDS ) );
385398 }
386- catch (Exception e ) {
399+ catch (InterruptedException e ) {
387400 throw new IgniteException (e );
388401 }
402+
389403 return null ;
390404 }
391405 }
0 commit comments