optakt / flow-dps

Flow Data Provisioning Service
Apache License 2.0
29 stars 13 forks source link

Rework storage layer #523

Closed Ullaakut closed 2 years ago

Ullaakut commented 2 years ago

Goal of this PR

Fixes https://github.com/optakt/flow-dps/issues/522

This PR, along with https://github.com/optakt/golang-lru/pull/1 redesigns the storage system of the trie improvements:

In the store, we run the following logic in our eviction handler:

    s.cache, err = lru.NewWithEvict(config.CacheSize, func(k, v interface{}, used int) bool {
        hash, ok := k.(hash.Hash)
        if !ok {
            logger.Fatal().Interface("got", k).Msg("unexpected key format")
        }

        // If the entry is dirty and the cache is not full, abort eviction.
        s.isDirtyMu.RLock()
        if s.isDirty[hash] && used < s.cacheSize {
            s.isDirtyMu.RUnlock()
            return false
        }
        s.isDirtyMu.RUnlock()

        // If the cache is full of dirty entries, push the current commit to disk and evict the entry.
        if s.cacheFullAndDirty(used) {
            s.forceCommit()
        }

        // If the current entry is clean, we can evict it. Otherwise, abort the eviction.
        return !s.isDirty[hash]
    })

Note: This PR includes changes that add error returns to the Insert method, which will conflict with the changes that @awfm9 will bring later on. It should be easy to solve those conflicts however: we just take the code from Max and add error handling in the only remaining case where we create a leaf.

Checklist

Does the PR require tests to be added or updated?

This PR probably requires adding more tests, yes.

Does the PR require documentation to be added or updated?

No

Misc

Ullaakut commented 2 years ago

Merging this in the meantime, but @awfm9 if you want to change the way we handle https://github.com/optakt/flow-dps/pull/523#discussion_r787636038 please LMK and we can do it in another PR. I don't want to block this one for 10 more days though so I'll merge it now to be able to move fwd on the trie improvements.