2121
2222import org .apache .commons .lang3 .tuple .Triple ;
2323import org .apache .hadoop .ozone .om .OMMetadataManager ;
24+ import org .apache .hadoop .ozone .om .exceptions .OMException ;
2425import org .apache .ratis .server .protocol .TermIndex ;
2526import org .apache .hadoop .hdds .utils .db .cache .CacheKey ;
2627import org .apache .hadoop .hdds .utils .db .cache .CacheValue ;
@@ -110,18 +111,18 @@ public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, TermIn
110111 // snapshotTableKey is nothing but /volumeName/bucketName/snapshotName.
111112 // Once all the locks are acquired, it performs the three steps mentioned above and
112113 // release all the locks after that.
113- Set <String > lockSet = new HashSet <>();
114+ Set <Triple < String , String , String >> lockSet = new HashSet <>(4 );
114115 try {
115- acquireLock (lockSet , snapTableKey , omMetadataManager );
116- SnapshotInfo fromSnapshot = omMetadataManager .getSnapshotInfoTable ().get (snapTableKey );
117-
118- if (fromSnapshot == null ) {
116+ if (omMetadataManager .getSnapshotInfoTable ().get (snapTableKey ) == null ) {
119117 // Snapshot may have been purged in the previous iteration of SnapshotDeletingService.
120118 LOG .warn ("The snapshot {} is not longer in snapshot table, It maybe removed in the previous " +
121119 "Snapshot purge request." , snapTableKey );
122120 continue ;
123121 }
124122
123+ acquireLock (lockSet , snapTableKey , omMetadataManager );
124+ SnapshotInfo fromSnapshot = omMetadataManager .getSnapshotInfoTable ().get (snapTableKey );
125+
125126 SnapshotInfo nextSnapshot =
126127 SnapshotUtils .getNextActiveSnapshot (fromSnapshot , snapshotChainManager , omSnapshotManager );
127128
@@ -139,8 +140,9 @@ public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, TermIn
139140 omMetadataManager .getSnapshotInfoTable ()
140141 .addCacheEntry (new CacheKey <>(fromSnapshot .getTableKey ()), CacheValue .get (trxnLogIndex ));
141142 } finally {
142- for (String lockString : lockSet ) {
143- releaseLock (lockString , omMetadataManager );
143+ for (Triple <String , String , String > lockKey : lockSet ) {
144+ omMetadataManager .getLock ()
145+ .releaseWriteLock (SNAPSHOT_LOCK , lockKey .getLeft (), lockKey .getMiddle (), lockKey .getRight ());
144146 }
145147 }
146148 }
@@ -156,23 +158,25 @@ public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, TermIn
156158 return omClientResponse ;
157159 }
158160
159- private void acquireLock (Set <String > lockSet , String snapshotTableKey ,
161+ private void acquireLock (Set <Triple < String , String , String > > lockSet , String snapshotTableKey ,
160162 OMMetadataManager omMetadataManager ) throws IOException {
161- if (!lockSet .contains (snapshotTableKey )) {
162- Triple <String , String , String > triple = SnapshotUtils .getSnapshotTableKeyFromString (snapshotTableKey );
163+ SnapshotInfo snapshotInfo = omMetadataManager .getSnapshotInfoTable ().get (snapshotTableKey );
164+
165+ // It should not be the case that lock is required for non-existing snapshot.
166+ if (snapshotInfo == null ) {
167+ LOG .error ("Snapshot: '{}' doesn't not exist in snapshot table." , snapshotTableKey );
168+ throw new OMException ("Snapshot: '{" + snapshotTableKey + "}' doesn't not exist in snapshot table." ,
169+ OMException .ResultCodes .FILE_NOT_FOUND );
170+ }
171+ Triple <String , String , String > lockKey = Triple .of (snapshotInfo .getVolumeName (), snapshotInfo .getBucketName (),
172+ snapshotInfo .getName ());
173+ if (!lockSet .contains (lockKey )) {
163174 mergeOmLockDetails (omMetadataManager .getLock ()
164- .acquireWriteLock (SNAPSHOT_LOCK , triple .getLeft (), triple .getMiddle (), triple .getRight ()));
165- lockSet .add (snapshotTableKey );
175+ .acquireWriteLock (SNAPSHOT_LOCK , lockKey .getLeft (), lockKey .getMiddle (), lockKey .getRight ()));
176+ lockSet .add (lockKey );
166177 }
167178 }
168179
169- private void releaseLock (String snapshotTableKey ,
170- OMMetadataManager omMetadataManager ) throws IOException {
171- Triple <String , String , String > triple = SnapshotUtils .getSnapshotTableKeyFromString (snapshotTableKey );
172- omMetadataManager .getLock ()
173- .releaseWriteLock (SNAPSHOT_LOCK , triple .getLeft (), triple .getMiddle (), triple .getRight ());
174- }
175-
176180 private void updateSnapshotInfoAndCache (SnapshotInfo snapInfo ,
177181 OmMetadataManagerImpl omMetadataManager , long trxnLogIndex ,
178182 Map <String , SnapshotInfo > updatedSnapInfos ) throws IOException {
@@ -199,7 +203,7 @@ private void updateSnapshotInfoAndCache(SnapshotInfo snapInfo,
199203 * update in DB.
200204 */
201205 private void updateSnapshotChainAndCache (
202- Set <String > lockSet ,
206+ Set <Triple < String , String , String > > lockSet ,
203207 OmMetadataManagerImpl metadataManager ,
204208 SnapshotInfo snapInfo ,
205209 long trxnLogIndex ,
@@ -238,20 +242,20 @@ private void updateSnapshotChainAndCache(
238242 acquireLock (lockSet , nextPathSnapshotKey , metadataManager );
239243 }
240244
241- String nestGlobalSnapshotKey = null ;
245+ String nextGlobalSnapshotKey = null ;
242246 if (hasNextGlobalSnapshot ) {
243247 UUID nextGlobalSnapshotId = snapshotChainManager .nextGlobalSnapshot (snapInfo .getSnapshotId ());
244- nestGlobalSnapshotKey = snapshotChainManager .getTableKey (nextGlobalSnapshotId );
248+ nextGlobalSnapshotKey = snapshotChainManager .getTableKey (nextGlobalSnapshotId );
245249
246250 // Acquire lock from the snapshot
247- acquireLock (lockSet , nestGlobalSnapshotKey , metadataManager );
251+ acquireLock (lockSet , nextGlobalSnapshotKey , metadataManager );
248252 }
249253
250254 SnapshotInfo nextPathSnapInfo =
251255 nextPathSnapshotKey != null ? metadataManager .getSnapshotInfoTable ().get (nextPathSnapshotKey ) : null ;
252256
253257 SnapshotInfo nextGlobalSnapInfo =
254- nestGlobalSnapshotKey != null ? metadataManager .getSnapshotInfoTable ().get (nestGlobalSnapshotKey ) : null ;
258+ nextGlobalSnapshotKey != null ? metadataManager .getSnapshotInfoTable ().get (nextGlobalSnapshotKey ) : null ;
255259
256260 // Updates next path snapshot's previous snapshot ID
257261 if (nextPathSnapInfo != null ) {
0 commit comments