-
Notifications
You must be signed in to change notification settings - Fork 5
Feature/tiered storage lru policy #960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
HoJacob
wants to merge
26
commits into
Seagate:feature/tiered_storage
Choose a base branch
from
HoJacob:feature/tiered_storage-lru_policy
base: feature/tiered_storage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
9cef74d
Implemented initial design of ReleaseFile and corresponding tests
d51bfe8
Initial ReadInBuffer component
d805f91
Added initial ReadInBuffer tests
432d54d
Initial implementation of DeleteFile, no tests yet
628feaa
Initial LRU skeleton
2ccb80f
Initial LRU Functions
63b276c
LRU eviction initial implementation
ca744a6
LRU worker logic added
8e31b0c
Initial LRU Code w Workers/Eviction logic
ed56450
LRU capacity design fix
ea8b42a
Fixed deadlock bugs in channels
3ea1864
Initial test file setup
4b15841
Bug fixes and initial tests of lru eviction policy ready for pr draft
449e766
Merge remote-tracking branch 'upstream/feature/tiered_storage' into f…
3f3a1b8
Handeled Dirty File State Scenario, ReleaseFile bugs, added Tests for…
74cce48
Changed all instances of normal map to sync.map
adef71e
Changed all instances of normal map to sync.map in TEST FILE NOW
e9dcb70
LRU Concurrency Design Implementation
13c608c
Fixed infinite loop logic and StopPolicy, old tests now work also tes…
8ac166b
Initial wiring of LRU Policy
d6c9a30
Wired LRU Policy with Tiered Storage, Implemented Tests against funct…
7a58ff6
Fixed DeleteFile and added in logic for edge case in LRU Policy
776d9b3
Tests for DeleteFile, still working on LRU Test
5a99492
Potential Fix for deadlock in Enqueue and Dequeue, manually unlocked …
37507f9
Something might be wrong with my vsCode that commit might not have wo…
ed6d5ed
Initial implementation of Sync and Flush, they are identical
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,371 @@ | ||
| package tiered_storage | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "sync" | ||
| "sync/atomic" | ||
| "time" | ||
|
|
||
| "github.com/Seagate/cloudfuse/common" | ||
| "github.com/Seagate/cloudfuse/common/log" | ||
| ) | ||
|
|
||
| type lruNode struct { | ||
| prev *lruNode | ||
| next *lruNode | ||
| name string | ||
| } | ||
|
|
||
| //upload 50 files and then check | ||
|
|
||
| type lruQueue struct { | ||
| mu sync.Mutex | ||
|
|
||
| nodeMap sync.Map | ||
|
|
||
| wg sync.WaitGroup // tracks capacityChecker only | ||
| workerWg sync.WaitGroup // tracks worker goroutines | ||
| numWorkers int | ||
| activeWorkers int32 | ||
|
|
||
| head *lruNode | ||
| tail *lruNode | ||
|
|
||
| uploadChan chan string | ||
| doneChan chan struct{} | ||
| hallPassChan chan bool | ||
|
|
||
| cachePath string | ||
| maxCacheSize float64 | ||
| totalUploadedSize int64 | ||
|
|
||
| threshold float64 | ||
| targetRatio float64 | ||
|
|
||
| tickerUnit time.Duration | ||
|
|
||
| fileLocks *common.LockMap // uses object name (common.JoinUnixFilepath) | ||
|
|
||
| //Functions to wire later into tiered_storage package | ||
| //upload function from tiered storage WIRE THIS LATER in tiered storage because we are using a function from there | ||
| uploadandCleanFn func(name string) error | ||
|
|
||
| // //delete locally | ||
| // localPath := filepath.Join(c.tmpPath, options.Name) | ||
| // c.mu.Lock() | ||
| // delete(c.fileMap, options.Name) | ||
| // c.mu.Unlock() | ||
| // os.Remove(localPath) | ||
|
|
||
| } | ||
|
|
||
| func (q *lruQueue) StartPolicy() error { | ||
| if q.uploadandCleanFn == nil { | ||
| return fmt.Errorf("lruQueue: upload function not set") | ||
| } | ||
| if q.numWorkers <= 0 { | ||
| return fmt.Errorf("lruQueue: numWorkers must be > 0") | ||
| } | ||
| //initialize queue | ||
| q.head = nil | ||
| q.tail = nil | ||
| //channels | ||
| q.doneChan = make(chan struct{}) | ||
| q.hallPassChan = make(chan bool, 1) | ||
| q.hallPassChan <- true | ||
|
|
||
| //go routines | ||
| q.wg.Add(1) | ||
| go q.capacityChecker() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (q *lruQueue) StopPolicy() error { | ||
| close(q.doneChan) | ||
| // Wait for capacityChecker to exit — its deferred close(uploadChan) fires here, | ||
| // signalling workers that no more jobs are coming. | ||
| q.wg.Wait() | ||
| return nil | ||
| } | ||
|
|
||
| func (q *lruQueue) Touch(name string) { | ||
| q.Enqueue(name) | ||
| } | ||
|
|
||
| func (q *lruQueue) Enqueue(name string) { | ||
| //Maybe have a duplicate , that touches essentially | ||
|
|
||
| //lock earlier | ||
| q.mu.Lock() | ||
| defer q.mu.Unlock() | ||
|
|
||
| //search for new node first | ||
| val, found := q.nodeMap.Load(name) | ||
| if found { | ||
| // Node already exists, no need to create a new one, just touch | ||
| node := val.(*lruNode) | ||
| q.extractNode(node) | ||
| q.setHead(node) | ||
| } else { | ||
| // Node does not exist need to create a new one and put to top | ||
| newNode := &lruNode{name: name} | ||
| // if list is empty, set tail pointer to newNode | ||
| if q.tail == nil { | ||
| q.tail = newNode | ||
| } | ||
| q.nodeMap.Store(name, newNode) | ||
| q.setHead(newNode) | ||
| } | ||
| } | ||
|
|
||
| func (q *lruQueue) Dequeue(name string) { | ||
| log.Trace("lruPolicy::removeNode : %s", name) | ||
|
|
||
| q.mu.Lock() | ||
| defer q.mu.Unlock() | ||
|
|
||
| val, found := q.nodeMap.LoadAndDelete(name) | ||
| if !found || val == nil { | ||
| return | ||
| } | ||
|
|
||
| node := val.(*lruNode) | ||
|
|
||
| q.extractNode(node) | ||
| } | ||
|
|
||
| func (q *lruQueue) setHead(node *lruNode) { | ||
| // insert node at the head | ||
| node.prev = nil | ||
| node.next = q.head | ||
| if q.head != nil { | ||
| q.head.prev = node | ||
| } | ||
| q.head = node | ||
| } | ||
|
|
||
| func (q *lruQueue) extractNode(node *lruNode) { | ||
| // remove the node from its position in the list | ||
|
|
||
| // head case | ||
| if node == q.head { | ||
| q.head = node.next | ||
| } | ||
| //tail case | ||
| if node == q.tail { | ||
| q.tail = node.prev | ||
| } | ||
|
|
||
| if node.next != nil { | ||
| node.next.prev = node.prev | ||
| } | ||
| if node.prev != nil { | ||
| node.prev.next = node.next | ||
| } | ||
| node.prev = nil | ||
| node.next = nil | ||
| } | ||
|
|
||
| func (q *lruQueue) capacityChecker() { | ||
| defer q.wg.Done() | ||
| //defer close(q.uploadChan) | ||
|
|
||
| ticker := time.NewTicker(q.tickerUnit) | ||
| defer ticker.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ticker.C: | ||
| // eviction | ||
| select { | ||
| case <-q.hallPassChan: | ||
|
|
||
| //1. Check if we need eviction | ||
| curSize, err := common.GetUsage(q.cachePath) | ||
| if err != nil { | ||
| log.Err("lruPolicy::capacityChecker : failed to get usage: %v", err) | ||
| q.hallPassChan <- true | ||
| continue | ||
| } | ||
| if curSize/q.maxCacheSize <= q.threshold { | ||
| q.hallPassChan <- true | ||
| break | ||
| } | ||
| //targetRatio should always be less than thresholdRatio | ||
|
|
||
| //2. Find difference to evict down to target ratio | ||
| difference := curSize - q.maxCacheSize*q.targetRatio | ||
| curEvictedSpace := 0 | ||
| actualEvictedSpace := 0 | ||
| atomic.StoreInt64(&q.totalUploadedSize, 0) | ||
| evicFail := false | ||
|
|
||
| //3. LRU Eviction to match difference | ||
| for actualEvictedSpace < int(difference) && !evicFail { | ||
| // Start nomination from what's already been confirmed uploaded, | ||
| // so we only nominate enough new files to cover the remaining gap. | ||
| curEvictedSpace = actualEvictedSpace | ||
|
|
||
| //initialize channel | ||
| q.uploadChan = make(chan string, 1000) | ||
|
|
||
| //start workers here | ||
| q.workerWg.Add(q.numWorkers) | ||
| for i := 0; i < q.numWorkers; i++ { | ||
| go q.worker() | ||
| } | ||
|
|
||
| //populate upload channel for workers | ||
| for curEvictedSpace < int(difference) { | ||
| nodeSize, evicted := q.eviction() | ||
| if !evicted { | ||
| evicFail = true | ||
| break | ||
| } | ||
| curEvictedSpace += int(nodeSize) | ||
| } | ||
|
|
||
| //close upload chan and wait for workers to process all remaining jobs | ||
| close(q.uploadChan) | ||
| q.workerWg.Wait() | ||
|
|
||
| //update the actual evicted space here | ||
| actualEvictedSpace = int(q.totalUploadedSize) | ||
|
|
||
| //if done is closed we need to check shutdown to prevent infinite loop | ||
| select { | ||
| case <-q.doneChan: | ||
| return | ||
| default: | ||
| } | ||
|
|
||
| //we can also have a case here if no files were evicted then we can break on this tick if we so choose | ||
|
|
||
| } | ||
| //give hall pass back when actual evicted space is satisfied | ||
| q.hallPassChan <- true | ||
|
|
||
| case <-q.doneChan: | ||
| return | ||
|
|
||
| //default return? | ||
| default: | ||
| } | ||
| case <-q.doneChan: | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (q *lruQueue) eviction() (int64, bool) { | ||
| q.mu.Lock() | ||
| nodeToEvict := q.tail | ||
| if nodeToEvict == nil { | ||
| q.mu.Unlock() | ||
| return 0, false | ||
| } | ||
|
|
||
| //1. loop through and find the first applicable node | ||
| for nodeToEvict != nil { | ||
| prevNode := nodeToEvict.prev | ||
|
|
||
| flock := q.fileLocks.Get(nodeToEvict.name) | ||
| flock.RLock() | ||
| handleCount := flock.Count() | ||
| flock.RUnlock() | ||
|
|
||
| if handleCount == 0 { | ||
| break | ||
| } | ||
|
|
||
| //node has open handles touch node | ||
| q.extractNode(nodeToEvict) | ||
| q.setHead(nodeToEvict) | ||
| nodeToEvict = prevNode | ||
| } | ||
|
|
||
| //all files are in use | ||
| if nodeToEvict == nil { | ||
| q.mu.Unlock() | ||
| return 0, false | ||
| } | ||
| name := nodeToEvict.name | ||
|
|
||
| //2. Remove file from queue so not accidentally chosen again | ||
| q.extractNode(nodeToEvict) | ||
| q.nodeMap.Delete(name) | ||
|
|
||
| q.mu.Unlock() | ||
|
|
||
| //3. Get the node size that we evict | ||
| localPath := filepath.Join(q.cachePath, name) | ||
|
|
||
| fileInfo, err := os.Stat(localPath) | ||
| if err != nil { | ||
| log.Err("lruPolicy::capacityChecker : failed to stat file: %v", err) | ||
| return 0, false | ||
| } | ||
| nodeSize := fileInfo.Size() | ||
|
|
||
| //4. Send node to channel to be uploaded by workers | ||
| select { | ||
| case q.uploadChan <- name: | ||
| case <-q.doneChan: | ||
| return 0, false | ||
| } | ||
|
|
||
| return nodeSize, true | ||
| } | ||
|
|
||
| func (q *lruQueue) worker() { | ||
| defer q.workerWg.Done() | ||
| for fileName := range q.uploadChan { | ||
|
|
||
| //1. Get handle count and file size | ||
| flock := q.fileLocks.Get(fileName) | ||
| flock.Lock() | ||
| handleCount := flock.Count() | ||
|
|
||
| localPath := filepath.Join(q.cachePath, fileName) | ||
| fileInfo, err := os.Stat(localPath) | ||
| if err != nil { | ||
| log.Warn( | ||
| "lruPolicy::worker : file %s no longer exists or stat failed, skipping: %v", | ||
| fileName, | ||
| err, | ||
| ) | ||
| flock.Unlock() | ||
| continue | ||
| } | ||
|
|
||
| fileSize := fileInfo.Size() | ||
|
|
||
| //2. Check if file eligible to upload | ||
| if handleCount == 0 { | ||
| err := q.uploadandCleanFn(fileName) | ||
| flock.Unlock() | ||
| //handle when file doesn't exist during upload we do not requeue otherwise we do enqueue | ||
| if err != nil { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok this existence error check might not be entirely useful because we lock the file prior to upload and unlock after, so deletion during upload shouldn't happen, because deleteFile waits on a file lock as well. Just a heads up in case you want to delete this code! |
||
| if os.IsNotExist(err) { | ||
| log.Warn( | ||
| "lruPolicy::worker : file %s was deleted during upload, skipping", | ||
| fileName, | ||
| ) | ||
| } else { | ||
| log.Err("lruPolicy::worker : failed to upload file %s: %v", fileName, err) | ||
| //if upload fails we have to put the file back to the queue and map to retry later | ||
| q.Touch(fileName) | ||
| } | ||
| } else { | ||
| atomic.AddInt64(&q.totalUploadedSize, fileSize) | ||
| } | ||
| //file is in use skip upload touch file back to top of queue | ||
| } else { | ||
| flock.Unlock() | ||
| q.Touch(fileName) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good!
Later on, when we're looking at concurrency, let's make sure this code can't run more than once at a time (if the ticker is faster than eviction, we don't begin another eviction loop in parallel).