| 27 | }; |
| 28 | |
| 29 | const redisDbPersistentDataSnapshot *redisDbPersistentData::createSnapshot(uint64_t mvccCheckpoint, bool fOptional) |
| 30 | { |
| 31 | serverAssert(GlobalLocksAcquired()); |
| 32 | serverAssert(m_refCount == 0); // do not call this on a snapshot |
| 33 | |
| 34 | if (performEvictions(true /*fPreSnapshot*/) != C_OK && fOptional) |
| 35 | return nullptr; // can't create snapshot due to OOM |
| 36 | |
| 37 | int levels = 1; |
| 38 | redisDbPersistentDataSnapshot *psnapshot = m_spdbSnapshotHOLDER.get(); |
| 39 | while (psnapshot != nullptr) |
| 40 | { |
| 41 | ++levels; |
| 42 | psnapshot = psnapshot->m_spdbSnapshotHOLDER.get(); |
| 43 | } |
| 44 | |
| 45 | if (m_spdbSnapshotHOLDER != nullptr) |
| 46 | { |
| 47 | // If possible reuse an existing snapshot (we want to minimize nesting) |
| 48 | if (mvccCheckpoint <= m_spdbSnapshotHOLDER->m_mvccCheckpoint) |
| 49 | { |
| 50 | if (!m_spdbSnapshotHOLDER->FStale()) |
| 51 | { |
| 52 | m_spdbSnapshotHOLDER->m_refCount++; |
| 53 | return m_spdbSnapshotHOLDER.get(); |
| 54 | } |
| 55 | serverLog(LL_VERBOSE, "Existing snapshot too old, creating a new one"); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // See if we have too many levels and can bail out of this to reduce load |
| 60 | if (fOptional && (levels >= 6)) |
| 61 | { |
| 62 | serverLog(LL_DEBUG, "Snapshot nesting too deep, abondoning"); |
| 63 | return nullptr; |
| 64 | } |
| 65 | |
| 66 | auto spdb = std::unique_ptr<redisDbPersistentDataSnapshot>(new (MALLOC_LOCAL) redisDbPersistentDataSnapshot()); |
| 67 | |
| 68 | // We can't have async rehash modifying these. Setting the asyncdata list to null |
| 69 | // will cause us to throw away the async work rather than modify the tables in flight |
| 70 | discontinueAsyncRehash(m_pdict); |
| 71 | discontinueAsyncRehash(m_pdictTombstone); |
| 72 | |
| 73 | spdb->m_fAllChanged = false; |
| 74 | spdb->m_fTrackingChanges = 0; |
| 75 | spdb->m_pdict = m_pdict; |
| 76 | spdb->m_pdictTombstone = m_pdictTombstone; |
| 77 | spdb->m_numexpires = m_numexpires; |
| 78 | // Add a fake iterator so the dicts don't rehash (they need to be read only) |
| 79 | dictPauseRehashing(spdb->m_pdict); |
| 80 | dictForceRehash(spdb->m_pdictTombstone); // prevent rehashing by finishing the rehash now |
| 81 | spdb->m_spdbSnapshotHOLDER = std::move(m_spdbSnapshotHOLDER); |
| 82 | if (m_spstorage != nullptr) |
| 83 | spdb->m_spstorage = std::shared_ptr<StorageCache>(const_cast<StorageCache*>(m_spstorage->clone())); |
| 84 | spdb->m_pdbSnapshot = m_pdbSnapshot; |
| 85 | spdb->m_refCount = 1; |
| 86 | spdb->m_mvccCheckpoint = getMvccTstamp(); |
no test coverage detected