3939import org .apache .ignite .internal .util .future .GridFutureAdapter ;
4040import org .apache .ignite .internal .util .tostring .GridToStringExclude ;
4141import org .apache .ignite .internal .util .tostring .GridToStringInclude ;
42+ import org .apache .ignite .internal .util .typedef .F ;
4243import org .apache .ignite .internal .util .typedef .T2 ;
4344import org .apache .ignite .internal .util .typedef .internal .S ;
4445import org .apache .ignite .internal .util .typedef .internal .U ;
@@ -55,7 +56,7 @@ public class TxDeadlockDetection {
5556 public static final int DFLT_TX_DEADLOCK_DETECTION_TIMEOUT = 60000 ;
5657
5758 /** Deadlock detection maximum iterations. */
58- private static int deadLockTimeout =
59+ private static final int DEAD_LOCK_TIMEOUT =
5960 getInteger (IGNITE_TX_DEADLOCK_DETECTION_TIMEOUT , DFLT_TX_DEADLOCK_DETECTION_TIMEOUT );
6061
6162 /** Sequence. */
@@ -80,7 +81,7 @@ public TxDeadlockDetection(GridCacheSharedContext<?, ?> cctx) {
8081 *
8182 * @param tx Target tx.
8283 * @param keys Keys.
83- * @return {@link TxDeadlock} if found, otherwise - {@code null} .
84+ * @return {@link TxDeadlockFuture} future .
8485 */
8586 TxDeadlockFuture detectDeadlock (IgniteInternalTx tx , Set <IgniteTxKey > keys ) {
8687 GridCacheVersion txId = tx .nearXidVersion ();
@@ -101,7 +102,7 @@ TxDeadlockFuture detectDeadlock(IgniteInternalTx tx, Set<IgniteTxKey> keys) {
101102 * @param wfg Wait-for-graph.
102103 * @param txId Tx ID - start vertex for cycle search in graph.
103104 */
104- static List <GridCacheVersion > findCycle (Map <GridCacheVersion , Set <GridCacheVersion >> wfg , GridCacheVersion txId ) {
105+ static @ Nullable List <GridCacheVersion > findCycle (Map <GridCacheVersion , Set <GridCacheVersion >> wfg , GridCacheVersion txId ) {
105106 if (wfg == null || wfg .isEmpty ())
106107 return null ;
107108
@@ -181,7 +182,7 @@ static class TxDeadlockFuture extends GridFutureAdapter<TxDeadlock> {
181182
182183 /** Pending keys. */
183184 @ GridToStringInclude
184- private Map <UUID , Set <IgniteTxKey >> pendingKeys = new HashMap <>();
185+ private final Map <UUID , Set <IgniteTxKey >> pendingKeys = new HashMap <>();
185186
186187 /** Nodes queue. */
187188 @ GridToStringInclude
@@ -233,7 +234,7 @@ private TxDeadlockFuture(GridCacheSharedContext cctx,
233234 this .topVer = topVer ;
234235 this .keys = keys ;
235236
236- if (deadLockTimeout > 0 ) {
237+ if (DEAD_LOCK_TIMEOUT > 0 ) {
237238 timeoutObj = new DeadlockTimeoutObject ();
238239
239240 cctx .time ().addTimeoutObject (timeoutObj );
@@ -322,7 +323,7 @@ private void detect(TxLocksResponse res) {
322323 * Maps tx keys on nodes. Key can be mapped on some node if this node is primary for given key or
323324 * node is near for transaction that holds or requests lock for key.
324325 *
325- * Key will not be be mapped to node if both key and node are already handled.
326+ * Key will not be mapped to node if both key and node are already handled.
326327 *
327328 * @param txKeys Tx keys.
328329 * @param txLocks Tx locks.
@@ -348,10 +349,7 @@ private void mapTxKeys(@Nullable Set<IgniteTxKey> txKeys, Map<IgniteTxKey, List<
348349 // Process this node earlier than other in order to optimize amount of requests.
349350 preferredNodes .add (nodeId );
350351
351- Set <IgniteTxKey > mappedKeys = pendingKeys .get (nodeId );
352-
353- if (mappedKeys == null )
354- pendingKeys .put (nodeId , mappedKeys = new HashSet <>());
352+ Set <IgniteTxKey > mappedKeys = pendingKeys .computeIfAbsent (nodeId , k -> new HashSet <>());
355353
356354 mappedKeys .add (txKey );
357355 }
@@ -363,10 +361,7 @@ private void mapTxKeys(@Nullable Set<IgniteTxKey> txKeys, Map<IgniteTxKey, List<
363361 else
364362 nodesQueue .addLast (nearNodeId );
365363
366- Set <IgniteTxKey > mappedKeys = pendingKeys .get (nearNodeId );
367-
368- if (mappedKeys == null )
369- pendingKeys .put (nearNodeId , mappedKeys = new HashSet <>());
364+ Set <IgniteTxKey > mappedKeys = pendingKeys .computeIfAbsent (nearNodeId , k -> new HashSet <>());
370365
371366 mappedKeys .add (txKey );
372367 }
@@ -387,10 +382,7 @@ private void mapTxKeys(@Nullable Set<IgniteTxKey> txKeys, Map<IgniteTxKey, List<
387382
388383 nodesQueue .addLast (nodeId );
389384
390- Set <IgniteTxKey > mappedKeys = pendingKeys .get (nodeId );
391-
392- if (mappedKeys == null )
393- pendingKeys .put (nodeId , mappedKeys = new HashSet <>());
385+ Set <IgniteTxKey > mappedKeys = pendingKeys .computeIfAbsent (nodeId , k -> new HashSet <>());
394386
395387 mappedKeys .add (txKey );
396388 }
@@ -417,15 +409,15 @@ private UUID primary(IgniteTxKey txKey) {
417409 private void merge (TxLocksResponse res ) {
418410 Map <IgniteTxKey , List <TxLock >> txLocks = res .txLocks ();
419411
420- if (txLocks == null || txLocks .isEmpty ())
412+ if (F .isEmpty (txLocks ))
421413 return ;
422414
423415 for (Map .Entry <IgniteTxKey , List <TxLock >> e : txLocks .entrySet ()) {
424416 IgniteTxKey txKey = e .getKey ();
425417
426418 List <TxLock > lockList = e .getValue ();
427419
428- if (lockList != null && ! lockList .isEmpty ()) {
420+ if (! F .isEmpty (lockList )) {
429421 for (TxLock lock : lockList ) {
430422 if (lock .owner () || lock .candiate ()) {
431423 if (txs .get (lock .txId ()) == null )
@@ -435,18 +427,12 @@ private void merge(TxLocksResponse res) {
435427 if (lock .owner ()) {
436428 GridCacheVersion txId = lock .txId ();
437429
438- Set <IgniteTxKey > keys = txLockedKeys .get (txId );
439-
440- if (keys == null )
441- txLockedKeys .put (txId , keys = new HashSet <>());
430+ Set <IgniteTxKey > keys = txLockedKeys .computeIfAbsent (txId , k -> new HashSet <>());
442431
443432 keys .add (txKey );
444433 }
445434 else if (lock .candiate ()) {
446- Set <GridCacheVersion > txs = txRequestedKeys .get (txKey );
447-
448- if (txs == null )
449- txRequestedKeys .put (txKey , txs = new HashSet <>());
435+ Set <GridCacheVersion > txs = txRequestedKeys .computeIfAbsent (txKey , k -> new HashSet <>());
450436
451437 txs .add (lock .txId ());
452438 }
@@ -473,10 +459,7 @@ private void updateWaitForGraph(Map<IgniteTxKey, List<TxLock>> txLocks) {
473459 txOwner = lock .txId ();
474460
475461 if (keys .contains (e .getKey ()) && !txId .equals (lock .txId ())) {
476- Set <GridCacheVersion > waitingTxs = wfg .get (txId );
477-
478- if (waitingTxs == null )
479- wfg .put (txId , waitingTxs = new HashSet <>());
462+ Set <GridCacheVersion > waitingTxs = wfg .computeIfAbsent (txId , k -> new HashSet <>());
480463
481464 waitingTxs .add (lock .txId ());
482465 }
@@ -487,10 +470,7 @@ private void updateWaitForGraph(Map<IgniteTxKey, List<TxLock>> txLocks) {
487470 if (lock .candiate () || lock .owner ()) {
488471 GridCacheVersion txId0 = lock .txId ();
489472
490- Set <GridCacheVersion > waitForTxs = wfg .get (txId0 );
491-
492- if (waitForTxs == null )
493- wfg .put (txId0 , waitForTxs = new HashSet <>());
473+ Set <GridCacheVersion > waitForTxs = wfg .computeIfAbsent (txId0 , k -> new HashSet <>());
494474
495475 waitForTxs .add (txOwner );
496476 }
@@ -562,7 +542,7 @@ private class DeadlockTimeoutObject extends GridTimeoutObjectAdapter {
562542 * Default constructor.
563543 */
564544 DeadlockTimeoutObject () {
565- super (deadLockTimeout );
545+ super (DEAD_LOCK_TIMEOUT );
566546 }
567547
568548 /** {@inheritDoc} */
@@ -571,7 +551,7 @@ private class DeadlockTimeoutObject extends GridTimeoutObjectAdapter {
571551
572552 IgniteLogger log = cctx .kernalContext ().log (this .getClass ());
573553
574- U .warn (log , "Deadlock detection was timed out [timeout=" + deadLockTimeout + ", fut=" + this + ']' );
554+ U .warn (log , "Deadlock detection was timed out [timeout=" + DEAD_LOCK_TIMEOUT + ", fut=" + this + ']' );
575555
576556 onDone ();
577557 }
0 commit comments