2727import java .io .IOException ;
2828import java .util .Iterator ;
2929import java .util .Map ;
30+ import java .util .UUID ;
3031import java .util .concurrent .ConcurrentHashMap ;
3132
3233import static org .apache .hadoop .ozone .om .exceptions .OMException .ResultCodes .FILE_NOT_FOUND ;
@@ -39,26 +40,25 @@ public class SnapshotCache {
3940 static final Logger LOG = LoggerFactory .getLogger (SnapshotCache .class );
4041
4142 // Snapshot cache internal hash map.
42- // Key: DB snapshot table key
43+ // Key: SnapshotId
4344 // Value: OmSnapshot instance, each holds a DB instance handle inside
4445 // TODO: [SNAPSHOT] Consider wrapping SoftReference<> around IOmMetadataReader
45- private final ConcurrentHashMap <String , ReferenceCounted <OmSnapshot >> dbMap ;
46+ private final ConcurrentHashMap <UUID , ReferenceCounted <OmSnapshot >> dbMap ;
47+
48+ private final CacheLoader <UUID , OmSnapshot > cacheLoader ;
4649
47- private final CacheLoader <String , OmSnapshot > cacheLoader ;
4850 // Soft-limit of the total number of snapshot DB instances allowed to be
4951 // opened on the OM.
5052 private final int cacheSizeLimit ;
5153
52- public SnapshotCache (
53- CacheLoader <String , OmSnapshot > cacheLoader ,
54- int cacheSizeLimit ) {
54+ public SnapshotCache (CacheLoader <UUID , OmSnapshot > cacheLoader , int cacheSizeLimit ) {
5555 this .dbMap = new ConcurrentHashMap <>();
5656 this .cacheLoader = cacheLoader ;
5757 this .cacheSizeLimit = cacheSizeLimit ;
5858 }
5959
6060 @ VisibleForTesting
61- ConcurrentHashMap <String , ReferenceCounted <OmSnapshot >> getDbMap () {
61+ ConcurrentHashMap <UUID , ReferenceCounted <OmSnapshot >> getDbMap () {
6262 return dbMap ;
6363 }
6464
@@ -71,17 +71,17 @@ public int size() {
7171
7272 /**
7373 * Immediately invalidate an entry.
74- * @param key DB snapshot table key
74+ * @param key SnapshotId
7575 */
76- public void invalidate (String key ) throws IOException {
76+ public void invalidate (UUID key ) throws IOException {
7777 dbMap .compute (key , (k , v ) -> {
7878 if (v == null ) {
79- LOG .warn ("Key : '{}' does not exist in cache." , k );
79+ LOG .warn ("SnapshotId : '{}' does not exist in snapshot cache." , k );
8080 } else {
8181 try {
8282 v .get ().close ();
8383 } catch (IOException e ) {
84- throw new IllegalStateException ("Failed to close snapshot : " + key , e );
84+ throw new IllegalStateException ("Failed to close snapshotId : " + key , e );
8585 }
8686 }
8787 return null ;
@@ -92,11 +92,10 @@ public void invalidate(String key) throws IOException {
9292 * Immediately invalidate all entries and close their DB instances in cache.
9393 */
9494 public void invalidateAll () {
95- Iterator <Map .Entry <String , ReferenceCounted <OmSnapshot >>>
96- it = dbMap .entrySet ().iterator ();
95+ Iterator <Map .Entry <UUID , ReferenceCounted <OmSnapshot >>> it = dbMap .entrySet ().iterator ();
9796
9897 while (it .hasNext ()) {
99- Map .Entry <String , ReferenceCounted <OmSnapshot >> entry = it .next ();
98+ Map .Entry <UUID , ReferenceCounted <OmSnapshot >> entry = it .next ();
10099 OmSnapshot omSnapshot = entry .getValue ().get ();
101100 try {
102101 // TODO: If wrapped with SoftReference<>, omSnapshot could be null?
@@ -114,19 +113,18 @@ public void invalidateAll() {
114113 */
115114 public enum Reason {
116115 FS_API_READ ,
117- SNAPDIFF_READ ,
116+ SNAP_DIFF_READ ,
118117 DEEP_CLEAN_WRITE ,
119118 GARBAGE_COLLECTION_WRITE
120119 }
121120
122121 /**
123122 * Get or load OmSnapshot. Shall be close()d after use.
124123 * TODO: [SNAPSHOT] Can add reason enum to param list later.
125- * @param key snapshot table key
124+ * @param key SnapshotId
126125 * @return an OmSnapshot instance, or null on error
127126 */
128- public ReferenceCounted <OmSnapshot > get (String key )
129- throws IOException {
127+ public ReferenceCounted <OmSnapshot > get (UUID key ) throws IOException {
130128 // Warn if actual cache size exceeds the soft limit already.
131129 if (size () > cacheSizeLimit ) {
132130 LOG .warn ("Snapshot cache size ({}) exceeds configured soft-limit ({})." ,
@@ -137,9 +135,9 @@ public ReferenceCounted<OmSnapshot> get(String key)
137135 ReferenceCounted <OmSnapshot > rcOmSnapshot =
138136 dbMap .compute (key , (k , v ) -> {
139137 if (v == null ) {
140- LOG .info ("Loading snapshot. Table key: {} " , k );
138+ LOG .info ("Loading SnapshotId: '{}' " , k );
141139 try {
142- v = new ReferenceCounted <>(cacheLoader .load (k ), false , this );
140+ v = new ReferenceCounted <>(cacheLoader .load (key ), false , this );
143141 } catch (OMException omEx ) {
144142 // Return null if the snapshot is no longer active
145143 if (!omEx .getResult ().equals (FILE_NOT_FOUND )) {
@@ -163,8 +161,7 @@ public ReferenceCounted<OmSnapshot> get(String key)
163161 if (rcOmSnapshot == null ) {
164162 // The only exception that would fall through the loader logic above
165163 // is OMException with FILE_NOT_FOUND.
166- throw new OMException ("Snapshot table key '" + key + "' not found, "
167- + "or the snapshot is no longer active" ,
164+ throw new OMException ("SnapshotId: '" + key + "' not found, or the snapshot is no longer active." ,
168165 OMException .ResultCodes .FILE_NOT_FOUND );
169166 }
170167
@@ -179,12 +176,12 @@ public ReferenceCounted<OmSnapshot> get(String key)
179176
180177 /**
181178 * Release the reference count on the OmSnapshot instance.
182- * @param key snapshot table key
179+ * @param key SnapshotId
183180 */
184- public void release (String key ) {
181+ public void release (UUID key ) {
185182 dbMap .compute (key , (k , v ) -> {
186183 if (v == null ) {
187- throw new IllegalArgumentException ("Key '" + key + "' does not exist in cache." );
184+ throw new IllegalArgumentException ("SnapshotId '" + key + "' does not exist in cache." );
188185 } else {
189186 v .decrementRefCount ();
190187 }
@@ -196,15 +193,6 @@ public void release(String key) {
196193 cleanup ();
197194 }
198195
199- /**
200- * Alternatively, can release with OmSnapshot instance directly.
201- * @param omSnapshot OmSnapshot
202- */
203- public void release (OmSnapshot omSnapshot ) {
204- final String snapshotTableKey = omSnapshot .getSnapshotTableKey ();
205- release (snapshotTableKey );
206- }
207-
208196 /**
209197 * Wrapper for cleanupInternal() that is synchronized to prevent multiple
210198 * threads from interleaving into the cleanup method.
@@ -221,24 +209,23 @@ private synchronized void cleanup() {
221209 * TODO: [SNAPSHOT] Add new ozone debug CLI command to trigger this directly.
222210 */
223211 private void cleanupInternal () {
224- for (Map .Entry <String , ReferenceCounted <OmSnapshot >> entry : dbMap .entrySet ()) {
212+ for (Map .Entry <UUID , ReferenceCounted <OmSnapshot >> entry : dbMap .entrySet ()) {
225213 dbMap .compute (entry .getKey (), (k , v ) -> {
226214 if (v == null ) {
227- throw new IllegalStateException ("Key '" + k + "' does not exist in cache. The RocksDB " +
215+ throw new IllegalStateException ("SnapshotId '" + k + "' does not exist in cache. The RocksDB " +
228216 "instance of the Snapshot may not be closed properly." );
229217 }
230218
231219 if (v .getTotalRefCount () > 0 ) {
232- LOG .debug ("Snapshot {} is still being referenced ({}), skipping its clean up" ,
233- k , v .getTotalRefCount ());
220+ LOG .debug ("SnapshotId {} is still being referenced ({}), skipping its clean up." , k , v .getTotalRefCount ());
234221 return v ;
235222 } else {
236- LOG .debug ("Closing Snapshot {}. It is not being referenced anymore." , k );
223+ LOG .debug ("Closing SnapshotId {}. It is not being referenced anymore." , k );
237224 // Close the instance, which also closes its DB handle.
238225 try {
239226 v .get ().close ();
240227 } catch (IOException ex ) {
241- throw new IllegalStateException ("Error while closing snapshot DB" , ex );
228+ throw new IllegalStateException ("Error while closing snapshot DB. " , ex );
242229 }
243230 return null ;
244231 }
0 commit comments