-
Notifications
You must be signed in to change notification settings - Fork 68
remove id caches #1283
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
Open
HayimShaul
wants to merge
20
commits into
main
Choose a base branch
from
1227-removing-one-show-ids-from-cache
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.
+49
−85
Open
remove id caches #1283
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
5989c62
don't cache ids
56582bf
revert changes
4ddbd8e
reverting
38f34a6
remove one insertion to cache
bca01a1
remove
1db4f15
remove
9acc27c
remove
b9421e6
remove
44c7e9c
remove
955e0a8
cleanup
7c14a70
use NoCache
41bf7d0
use nocache
e7953c9
no nocache
0ec61f0
refactor getSigner
b97e8ef
typo
263a1fc
exclude x509 ids from cache
c67f2cb
remove RegisterSigner
5e62fcd
remove unused var
5b68206
remove unused var
c90031b
cleanup
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
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 |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ import ( | |
| "github.com/hyperledger-labs/fabric-token-sdk/token/driver" | ||
| idriver "github.com/hyperledger-labs/fabric-token-sdk/token/services/identity/driver" | ||
| "github.com/hyperledger-labs/fabric-token-sdk/token/services/logging" | ||
| cache2 "github.com/hyperledger-labs/fabric-token-sdk/token/services/utils/cache" | ||
| "go.uber.org/zap/zapcore" | ||
| ) | ||
|
|
||
|
|
@@ -73,7 +74,6 @@ type Provider struct { | |
|
|
||
| isMeCache cache[bool] | ||
| signers cache[*SignerEntry] | ||
| verifiers cache[*VerifierEntry] | ||
| } | ||
|
|
||
| // NewProvider creates a new identity provider implementing the driver.IdentityProvider interface. | ||
|
|
@@ -91,9 +91,8 @@ func NewProvider( | |
| enrollmentIDUnmarshaler: enrollmentIDUnmarshaler, | ||
| deserializer: deserializer, | ||
| storage: storage, | ||
| isMeCache: secondcache.NewTyped[bool](5000), | ||
| signers: secondcache.NewTyped[*SignerEntry](5000), | ||
| verifiers: secondcache.NewTyped[*VerifierEntry](5000), | ||
| isMeCache: cache2.NewNoCache[bool](), | ||
| signers: secondcache.NewTyped[*SignerEntry](50), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -113,20 +112,6 @@ func (p *Provider) RegisterIdentityDescriptor(ctx context.Context, identityDescr | |
| return nil | ||
| } | ||
|
|
||
| func (p *Provider) RegisterVerifier(ctx context.Context, identity driver.Identity, v driver.Verifier) error { | ||
| if v == nil { | ||
| return errors.New("invalid verifier, expected a valid instance") | ||
| } | ||
| idHash := identity.UniqueID() | ||
| entry := &VerifierEntry{Verifier: v} | ||
| if p.Logger.IsEnabledFor(zapcore.DebugLevel) { | ||
| entry.DebugStack = debug.Stack() | ||
| } | ||
| p.verifiers.Add(idHash, entry) | ||
| p.Logger.DebugfContext(ctx, "register verifier to [%s]:[%s]", idHash, logging.Identifier(v)) | ||
| return nil | ||
| } | ||
|
|
||
| func (p *Provider) RegisterAuditInfo(ctx context.Context, identity driver.Identity, info []byte) error { | ||
| return p.storage.StoreIdentityData(ctx, identity, info, nil, nil) | ||
| } | ||
|
|
@@ -261,55 +246,64 @@ func (p *Provider) areMe(ctx context.Context, identities ...driver.Identity) []s | |
| } | ||
|
|
||
| func (p *Provider) getSigner(ctx context.Context, identity driver.Identity, idHash string) (driver.Signer, error) { | ||
| // check again the cache | ||
| entry, ok := p.signers.Get(idHash) | ||
| if ok { | ||
| signer, _, err := p.getSignerAndCache(ctx, identity, idHash, true) | ||
| return signer, err | ||
| } | ||
|
|
||
| func (p *Provider) getSignerAndCache(ctx context.Context, identity driver.Identity, idHash string, shouldCache bool) (driver.Signer, bool, error) { | ||
| // check cache | ||
| if entry, ok := p.signers.Get(idHash); ok { | ||
| p.Logger.DebugfContext(ctx, "signer for [%s] found", idHash) | ||
| return entry.Signer, nil | ||
| return entry.Signer, false, nil | ||
| } | ||
|
|
||
| p.Logger.DebugfContext(ctx, "signer for [%s] not found, try to deserialize", idHash) | ||
| // ask the deserializer | ||
| signer, err := p.deserializeSigner(ctx, identity) | ||
| if err != nil { | ||
| return nil, errors.Wrapf(err, "failed deserializing identity for signer [%s]", identity) | ||
| } | ||
| entry = &SignerEntry{Signer: signer} | ||
| if p.Logger.IsEnabledFor(zapcore.DebugLevel) { | ||
| entry.DebugStack = debug.Stack() | ||
| } | ||
| p.signers.Add(idHash, entry) | ||
| if err := p.storage.StoreSignerInfo(ctx, identity, nil); err != nil { | ||
| return nil, errors.Wrap(err, "failed to store entry in storage for the passed signer") | ||
| } | ||
| return entry.Signer, nil | ||
| } | ||
| p.Logger.DebugfContext(ctx, "signer for [%s] not found, attempting to deserialize", idHash) | ||
|
|
||
| func (p *Provider) deserializeSigner(ctx context.Context, identity driver.Identity) (driver.Signer, error) { | ||
| // check that we have a deserializer | ||
| if p.deserializer == nil { | ||
| return nil, errors.Errorf("cannot find signer for [%s], no deserializer set", identity) | ||
| return nil, false, errors.Errorf("cannot find signer for [%s], no deserializer set", identity) | ||
| } | ||
| var err error | ||
|
|
||
| // try direct deserialization | ||
| signer, err := p.deserializer.DeserializeSigner(ctx, identity) | ||
| if err == nil { | ||
| return signer, nil | ||
| } | ||
| if err != nil { | ||
| // second chance: try a TypedIdentity | ||
| typed, err2 := UnmarshalTypedIdentity(identity) | ||
| if err2 != nil { | ||
| // neither deserializable nor a typed wrapper | ||
| return nil, false, errors.Wrapf( | ||
| err2, | ||
| "failed to unmarshal typed identity for [%s] and failed deserialization [%s]", | ||
| identity.String(), err, | ||
| ) | ||
| } | ||
|
|
||
| // give it a second chance | ||
| if typed.Type == "x509" { | ||
| shouldCache = false | ||
| } | ||
|
|
||
| // is the identity wrapped in TypedIdentity? | ||
| ro, err2 := UnmarshalTypedIdentity(identity) | ||
| if err2 != nil { | ||
| // No | ||
| return nil, errors.Wrapf(err2, "failed to unmarshal raw owner for identity [%s] and failed deserialization [%s]", identity.String(), err) | ||
| // recursively resolve the inner identity | ||
| signer, shouldCache, err = p.getSignerAndCache(ctx, typed.Identity, typed.Identity.UniqueID(), shouldCache) | ||
| if err != nil { | ||
| return nil, false, errors.Wrapf(err, "failed getting signer for identity [%s]", typed.Identity) | ||
| } | ||
| } | ||
|
|
||
| // yes, check ro.Identity | ||
| signer, err = p.getSigner(ctx, ro.Identity, ro.Identity.UniqueID()) | ||
| if err != nil { | ||
| return nil, errors.Wrapf(err, "failed getting signer for identity [%s]", ro.Identity) | ||
| // Cache the signer for the current idHash | ||
| if shouldCache { | ||
| entry := &SignerEntry{Signer: signer} | ||
| if p.Logger.IsEnabledFor(zapcore.DebugLevel) { | ||
| entry.DebugStack = debug.Stack() | ||
| } | ||
| p.signers.Add(idHash, entry) | ||
| } | ||
| return signer, nil | ||
|
|
||
| // Persist signer info for the current identity | ||
| if err := p.storage.StoreSignerInfo(ctx, identity, nil); err != nil { | ||
| return nil, false, errors.Wrap(err, "failed to store entry in storage for the passed signer") | ||
| } | ||
|
|
||
| return signer, shouldCache, nil | ||
| } | ||
|
|
||
| func (p *Provider) updateCaches(descriptor *idriver.IdentityDescriptor, alias driver.Identity) { | ||
|
|
@@ -331,15 +325,4 @@ func (p *Provider) updateCaches(descriptor *idriver.IdentityDescriptor, alias dr | |
| p.signers.Add(aliasID, entry) | ||
| } | ||
| } | ||
|
Contributor
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. we are still caching any signer no matter the identity type. This will generate even more contention on the signers cache given its new size (5) |
||
| // verifiers | ||
| if descriptor.Verifier != nil { | ||
| entry := &VerifierEntry{Verifier: descriptor.Verifier} | ||
| if p.Logger.IsEnabledFor(zapcore.DebugLevel) { | ||
| entry.DebugStack = debug.Stack() | ||
| } | ||
| p.verifiers.Add(id, entry) | ||
| if setAlias { | ||
| p.verifiers.Add(aliasID, entry) | ||
| } | ||
| } | ||
| } | ||
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
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
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.
I think we said to cache only the identity whose type is x509, no?
If we still cache the signers and the cache is only of size 5 there will be even more contention, no?
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.
actually we said to have the first PR use NoCache and a 2nd PR avoid caching x509.
The system doesn't work with signers being NoCache, so I limited the cache to 5 instead.
but sure. I added to this PR avoiding to insert x509 into the cache and increased the signer cache to 50.