-
Notifications
You must be signed in to change notification settings - Fork 13
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
feat(idpool): idpool feature for generating id's #400
Open
venkyvsp
wants to merge
4
commits into
opiproject:main
Choose a base branch
from
mardim91:opi_idpool_main_pr
base: main
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.
+180
−36
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
81b09ac
feat(idpool): idpool feature for generating id's
venkyvsp 3a9ec4f
feat(idpool): fixed routing table id busy error handling
venkyvsp a5b9763
feat(idpool): fixed routing table id busy error handling and reference
venkyvsp 64597cf
feat(idpool): fixed review comments
venkyvsp 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 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
This file contains 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,147 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright (c) 2022-2023 Intel Corporation, or its subsidiaries. | ||
// Copyright (C) 2023 Nordix Foundation. | ||
|
||
// Package utils has some utility functions and interfaces | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"reflect" | ||
) | ||
|
||
// IDPool structure | ||
/* Helper class for uniquely assigning IDs from a specified integer set (e.g. a | ||
# range) to keys. IDs are assigned (or read) with GetID(key) and returned back | ||
# into the pool with ReleaseID(key). The IDPool remembers a once-assigned ID | ||
# for keys so that the same ID is assigned for a key. Only when the pool runs | ||
# out of unassigned keys, it will recycle released ids and assign them to new | ||
# keys. | ||
# Optionally, the IDPool supports reference tracking for key/ID pairs. Clients | ||
# can provide a unique reference when fetching and releasing an ID for a key | ||
# to support multiple independent clients. | ||
# The pool will only release the ID for the key, when the last client has the | ||
# released the ID with its reference. When a reference is specified in GetID() | ||
# and ReleaseID() the IDPool returns the current number of reference for the | ||
# ID so that a caller knows when an ID was newly assigned (ref_count 1) or | ||
# finally released (ref_count 0). | ||
*/ | ||
type IDPool struct { | ||
name string // Name of pool | ||
unusedIDs []uint32 // Yet unused IDs in pool Available ids | ||
idsInUse map[interface{}]uint32 // Mapping key: id for currently assigned ids | ||
idsForReuse map[interface{}]uint32 // Mapping key: id for previously assigned ids | ||
refs map[uint32]map[interface{}]bool | ||
size int // Size of the pool | ||
} | ||
|
||
// IDPoolInit initialize mod ptr pool | ||
func IDPoolInit(name string, min uint32, max uint32) IDPool { | ||
var pool IDPool | ||
pool.name = name | ||
var index int | ||
pool.unusedIDs = make([]uint32, (max-min)+1) | ||
if min > 0 { | ||
for value := max; value >= min; value-- { | ||
pool.unusedIDs[index] = value | ||
index++ | ||
} | ||
} else { | ||
return IDPool{} | ||
} | ||
pool.size = len(pool.unusedIDs) | ||
pool.idsInUse = make(map[interface{}]uint32) | ||
pool.idsForReuse = make(map[interface{}]uint32) | ||
pool.refs = make(map[uint32]map[interface{}]bool) | ||
return pool | ||
} | ||
|
||
// GetPoolStatus get status of a pool | ||
func (ip *IDPool) GetPoolStatus() string { | ||
str := fmt.Sprintf("name=%s\n Inuse=%+v\n Refs=%+v\n Forreuse=%+v\n Unused=%+v\n ", ip.name, ip.idsInUse, ip.refs, ip.idsForReuse, ip.unusedIDs) | ||
return str | ||
} | ||
|
||
func (ip *IDPool) assignid(key interface{}) uint32 { | ||
// Check if there was an id assigned for that key earlier | ||
var id uint32 | ||
ok := ip.idsForReuse[key] | ||
if ok != 0 { | ||
// Re-use the old id | ||
delete(ip.idsForReuse, key) | ||
} else { | ||
if len(ip.unusedIDs) != 0 { | ||
// Pick an unused id | ||
id = ip.unusedIDs[len(ip.unusedIDs)-1] | ||
ip.unusedIDs = ip.unusedIDs[0 : len(ip.unusedIDs)-1] | ||
} else { | ||
if len(ip.idsForReuse) != 0 { | ||
// Pick one of the ids earlier used for another key | ||
for oldKey := range ip.idsForReuse { | ||
delete(ip.idsForReuse, oldKey) | ||
break | ||
} | ||
} else { | ||
log.Printf("IDPool: Failed to allocate id for %+v. No free ids in pool.", key) | ||
return 0 | ||
} | ||
} | ||
} | ||
// Store the assigned id, if any | ||
if id != 0 { | ||
ip.idsInUse[key] = id | ||
} | ||
return id | ||
} | ||
|
||
// GetID get the mod ptr id from pool | ||
func (ip *IDPool) GetID(key interface{}, ref interface{}) (uint32, uint32) { | ||
var id uint32 | ||
ok := ip.idsInUse[key] | ||
if ok == 0 { | ||
id = ip.assignid(key) | ||
if id == 0 { | ||
return 0, 0 | ||
} | ||
} else { | ||
id = ok | ||
} | ||
if ref != nil { | ||
log.Printf("IDPool: GetID Assigning key : %+v , id %+v for ref %v", id, key, ref) | ||
if reflect.ValueOf(ip.refs[id]).IsZero() { | ||
ip.refs[id] = make(map[interface{}]bool, 0) | ||
} | ||
ip.refs[id][ref] = true | ||
return id, uint32(len(ip.refs[id])) | ||
} | ||
log.Printf("IDPool: GetID Assigning id %v for key %v and ref %v", id, key, ref) | ||
return id, uint32(0) | ||
} | ||
|
||
// ReleaseID get the reference id | ||
func (ip *IDPool) ReleaseID(key interface{}, ref interface{}) (uint32, uint32) { | ||
log.Printf("IDPool:ReleaseID Releasing id for key %v", key) | ||
ok := ip.idsInUse[key] | ||
if ok == 0 { | ||
log.Printf("No id to release for key %v", key) | ||
return 0, 0 | ||
} | ||
id := ok | ||
refSet := ip.refs[id] | ||
if !reflect.ValueOf(refSet).IsZero() && !reflect.ValueOf(ref).IsZero() { | ||
delete(refSet, ref) | ||
} | ||
if len(refSet) == 0 { | ||
delete(ip.idsInUse, key) | ||
delete(ip.refs, id) | ||
ip.idsForReuse[key] = id | ||
log.Printf("IDPool:ReleaseID Id %v has been released", id) | ||
} else { | ||
log.Printf("IDPool:ReleaseID Keep id:%+v remaining references %+v", id, len(refSet)) | ||
} | ||
if ref != nil { | ||
return id, uint32(len(refSet)) | ||
} | ||
return id, uint32(0) | ||
} |
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.
IDPoolInit
can just returnerror
if failed to initializeThere 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.
added bool return for the initialization status