-
Notifications
You must be signed in to change notification settings - Fork 138
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
CBG-3849 Remove inline cas handling for xattr config persistence #6850
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,14 +51,12 @@ type XattrBootstrapPersistence struct { | |
|
||
const cfgXattrKey = "_sync" | ||
const cfgXattrConfigPath = cfgXattrKey + ".config" | ||
const cfgXattrCasPath = cfgXattrKey + ".cas" | ||
const cfgXattrBody = `{"cfgVersion": 1}` | ||
|
||
func (xbp *XattrBootstrapPersistence) insertConfig(c *gocb.Collection, key string, value interface{}) (cas uint64, err error) { | ||
|
||
mutateOps := []gocb.MutateInSpec{ | ||
gocb.UpsertSpec(cfgXattrConfigPath, value, UpsertSpecXattr), | ||
gocb.UpsertSpec(cfgXattrCasPath, gocb.MutationMacroCAS, UpsertSpecXattr), | ||
gocb.ReplaceSpec("", json.RawMessage(cfgXattrBody), nil), | ||
} | ||
options := &gocb.MutateInOptions{ | ||
|
@@ -91,7 +89,7 @@ func (xbp *XattrBootstrapPersistence) touchConfigRollback(c *gocb.Collection, ke | |
return result.Cas(), nil | ||
} | ||
|
||
// loadRawConfig returns the config and document cas (not cfgCas). Does not restore deleted documents, | ||
// loadRawConfig returns the config and document cas. Does not restore deleted documents, | ||
// to avoid cas collisions with concurrent updates | ||
func (xbp *XattrBootstrapPersistence) loadRawConfig(ctx context.Context, c *gocb.Collection, key string) ([]byte, gocb.Cas, error) { | ||
|
||
|
@@ -150,9 +148,9 @@ func (xbp *XattrBootstrapPersistence) removeRawConfig(c *gocb.Collection, key st | |
} | ||
|
||
func (xbp *XattrBootstrapPersistence) replaceRawConfig(c *gocb.Collection, key string, value []byte, cas gocb.Cas) (gocb.Cas, error) { | ||
|
||
mutateOps := []gocb.MutateInSpec{ | ||
gocb.UpsertSpec(cfgXattrConfigPath, bytesToRawMessage(value), UpsertSpecXattr), | ||
gocb.UpsertSpec(cfgXattrCasPath, gocb.MutationMacroCAS, UpsertSpecXattr), | ||
} | ||
options := &gocb.MutateInOptions{ | ||
StoreSemantic: gocb.StoreSemanticsReplace, | ||
|
@@ -171,7 +169,6 @@ func (xbp *XattrBootstrapPersistence) loadConfig(ctx context.Context, c *gocb.Co | |
|
||
ops := []gocb.LookupInSpec{ | ||
gocb.GetSpec(cfgXattrConfigPath, GetSpecXattr), | ||
gocb.GetSpec(cfgXattrCasPath, GetSpecXattr), | ||
gocb.GetSpec("", &gocb.GetSpecOptions{}), | ||
} | ||
lookupOpts := &gocb.LookupInOptions{ | ||
|
@@ -187,26 +184,19 @@ func (xbp *XattrBootstrapPersistence) loadConfig(ctx context.Context, c *gocb.Co | |
DebugfCtx(ctx, KeyCRUD, "No xattr config found for key=%s, path=%s: %v", key, cfgXattrConfigPath, xattrContErr) | ||
return 0, ErrNotFound | ||
} | ||
|
||
// cas | ||
var strCas string | ||
xattrCasErr := res.ContentAt(1, &strCas) | ||
if xattrCasErr != nil { | ||
DebugfCtx(ctx, KeyCRUD, "No xattr cas found for key=%s, path=%s: %v", key, cfgXattrCasPath, xattrContErr) | ||
return 0, ErrNotFound | ||
} | ||
cfgCas := HexCasToUint64(strCas) | ||
casOut := res.Cas() | ||
|
||
// deleted document check - if deleted, restore | ||
var body map[string]interface{} | ||
bodyErr := res.ContentAt(2, &body) | ||
bodyErr := res.ContentAt(1, &body) | ||
if bodyErr != nil { | ||
restoreErr := xbp.restoreDocumentBody(c, key, valuePtr, strCas) | ||
var restoreErr error | ||
casOut, restoreErr = xbp.restoreDocumentBody(c, key, valuePtr, res.Cas()) | ||
if restoreErr != nil { | ||
WarnfCtx(ctx, "Error attempting to restore unexpected deletion of config: %v", restoreErr) | ||
} | ||
} | ||
return cfgCas, nil | ||
return uint64(casOut), nil | ||
} else if errors.Is(lookupErr, gocbcore.ErrDocumentNotFound) { | ||
DebugfCtx(ctx, KeyCRUD, "No config document found for key=%s", key) | ||
return 0, ErrNotFound | ||
|
@@ -215,24 +205,23 @@ func (xbp *XattrBootstrapPersistence) loadConfig(ctx context.Context, c *gocb.Co | |
} | ||
} | ||
|
||
// Restore a deleted document's body. Rewrites metadata, but preserves previous cfgCas | ||
func (xbp *XattrBootstrapPersistence) restoreDocumentBody(c *gocb.Collection, key string, value interface{}, cfgCas string) error { | ||
// Restore a deleted document's body. Rewrites metadata | ||
func (xbp *XattrBootstrapPersistence) restoreDocumentBody(c *gocb.Collection, key string, value interface{}, cas gocb.Cas) (casOut gocb.Cas, err error) { | ||
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 aren't using a cas check here because there aren't cas checks on insert. I think you can remove We are sure that there is no document existing because on |
||
mutateOps := []gocb.MutateInSpec{ | ||
gocb.UpsertSpec(cfgXattrConfigPath, value, UpsertSpecXattr), | ||
gocb.UpsertSpec(cfgXattrCasPath, cfgCas, UpsertSpecXattr), | ||
gocb.ReplaceSpec("", json.RawMessage(cfgXattrBody), nil), | ||
} | ||
options := &gocb.MutateInOptions{ | ||
StoreSemantic: gocb.StoreSemanticsInsert, | ||
} | ||
_, mutateErr := c.MutateIn(key, mutateOps, options) | ||
result, mutateErr := c.MutateIn(key, mutateOps, options) | ||
if isKVError(mutateErr, memd.StatusKeyExists) { | ||
return ErrAlreadyExists | ||
return 0, ErrAlreadyExists | ||
} | ||
if mutateErr != nil { | ||
return mutateErr | ||
return 0, mutateErr | ||
} | ||
return nil | ||
return result.Cas(), nil | ||
} | ||
|
||
// Document Body persistence stores config in the document body. | ||
|
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
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.
If there's an error here, I think
casOut
will be updated to 0, which might not be correct? The tombstone document will have a cas.OTOH, the cas isn't super useful
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 can also see how if there's no document, then it should have a zero cas. I'm not sure what to do here.
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 you're correct - if the restore fails we should return the cas of the current version of the document (the tombstone), since we're returning the tombstone's xattr.