Skip to content

Commit

Permalink
Change to only store a prefix of the datastore's unique ID in the zed…
Browse files Browse the repository at this point in the history
…token

Results in smaller tokens but given that datastore IDs are generated, should still minimize the chances of a conflict
  • Loading branch information
josephschorr committed Jul 11, 2024
1 parent 33c21c5 commit a526fc5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 82 deletions.
123 changes: 62 additions & 61 deletions pkg/proto/impl/v1/impl.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/proto/impl/v1/impl.pb.validate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions pkg/proto/impl/v1/impl_vtproto.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions pkg/zedtoken/zedtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const (
StatusMismatchedDatastoreID
)

const uniqueIDPrefixLength = 8

// MustNewFromRevisionForTesting generates an encoded zedtoken from an integral revision.
func MustNewFromRevisionForTesting(revision datastore.Revision) *v1.ZedToken {
encoded, err := newFromRevision(revision, legacyEmptyDatastoreID)
Expand All @@ -66,11 +68,16 @@ func NewFromRevision(ctx context.Context, revision datastore.Revision, ds datast
}

func newFromRevision(revision datastore.Revision, datastoreUniqueID string) (*v1.ZedToken, error) {
datastoreUniqueIDPrefix := datastoreUniqueID
if len(datastoreUniqueIDPrefix) > uniqueIDPrefixLength {
datastoreUniqueIDPrefix = datastoreUniqueIDPrefix[0:uniqueIDPrefixLength]
}

toEncode := &zedtoken.DecodedZedToken{
VersionOneof: &zedtoken.DecodedZedToken_V1{
V1: &zedtoken.DecodedZedToken_V1ZedToken{
Revision: revision.String(),
DatastoreUniqueId: datastoreUniqueID,
Revision: revision.String(),
DatastoreUniqueIdPrefix: datastoreUniqueIDPrefix,
},
},
}
Expand Down Expand Up @@ -132,7 +139,7 @@ func DecodeRevision(encoded *v1.ZedToken, ds revisionDecoder) (datastore.Revisio
return datastore.NoRevision, StatusUnknown, fmt.Errorf(errDecodeError, err)
}

if ver.V1.DatastoreUniqueId == legacyEmptyDatastoreID {
if ver.V1.DatastoreUniqueIdPrefix == legacyEmptyDatastoreID {
return parsed, StatusLegacyEmptyDatastoreID, nil
}

Expand All @@ -141,7 +148,12 @@ func DecodeRevision(encoded *v1.ZedToken, ds revisionDecoder) (datastore.Revisio
return datastore.NoRevision, StatusUnknown, fmt.Errorf(errDecodeError, err)
}

if ver.V1.DatastoreUniqueId != datastoreUniqueID {
datastoreUniqueIDPrefix := datastoreUniqueID
if len(datastoreUniqueIDPrefix) > uniqueIDPrefixLength {
datastoreUniqueIDPrefix = datastoreUniqueIDPrefix[0:uniqueIDPrefixLength]
}

if ver.V1.DatastoreUniqueIdPrefix != datastoreUniqueIDPrefix {
return parsed, StatusMismatchedDatastoreID, nil
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/zedtoken/zedtoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ var decodeTests = []struct {
},
{
format: "V1 ZedToken with matching datastore unique ID",
token: "GhIKAjQyEgxzb21ldW5pcXVlaWQ=",
token: "Gg4KAjQyEghzb21ldW5pcQ==",
datastoreUniqueID: "someuniqueid",
expectedRevision: revisions.NewForTransactionID(42),
expectedStatus: StatusValid,
expectError: false,
},
{
format: "V1 ZedToken with mismatched datastore unique ID",
token: "GhIKAjQyEgxzb21ldW5pcXVlaWQ=",
token: "Gg4KAjQyEghzb21ldW5pcQ==",
datastoreUniqueID: "anotheruniqueid",
expectedRevision: revisions.NewForTransactionID(42),
expectedStatus: StatusMismatchedDatastoreID,
Expand Down Expand Up @@ -225,7 +225,7 @@ var hlcDecodeTests = []struct {
},
{
format: "V1 ZedToken with matching datastore unique ID",
token: "GkYKHjE2OTM1NDA5NDAzNzMwNDU3MjcuMDAwMDAwMDAwMRIkNjM0OWFhZjItMzdjZC00N2I5LTg0ZTgtZmU1ZmE2ZTJkZWFk",
token: "GioKHjE2OTM1NDA5NDAzNzMwNDU3MjcuMDAwMDAwMDAwMRIINjM0OWFhZjI=",
datastoreUniqueID: "6349aaf2-37cd-47b9-84e8-fe5fa6e2dead",
expectedStatus: StatusValid,
expectedRevision: (func() datastore.Revision {
Expand All @@ -243,7 +243,7 @@ var hlcDecodeTests = []struct {
},
{
format: "V1 ZedToken with mismatched datastore unique ID",
token: "GkYKHjE2OTM1NDA5NDAzNzMwNDU3MjcuMDAwMDAwMDAwMRIkNjM0OWFhZjItMzdjZC00N2I5LTg0ZTgtZmU1ZmE2ZTJkZWFk",
token: "GioKHjE2OTM1NDA5NDAzNzMwNDU3MjcuMDAwMDAwMDAwMRIINjM0OWFhZjI=",
datastoreUniqueID: "arrrg-6349aaf2-37cd-47b9-84e8-fe5fa6e2dead",
expectedStatus: StatusMismatchedDatastoreID,
expectedRevision: (func() datastore.Revision {
Expand Down
6 changes: 3 additions & 3 deletions proto/internal/impl/v1/impl.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ message DecodedZedToken {
message V1ZedToken {
string revision = 1;

// datastore_unique_id is the unique ID for the datastore. Will be empty for legacy
// tokens.
string datastore_unique_id = 2;
// datastore_unique_id_prefix is a prefix of the unique ID for the datastore that created
// this token. Will be empty for legacy tokens.
string datastore_unique_id_prefix = 2;
}
oneof version_oneof {
V1Zookie deprecated_v1_zookie = 2;
Expand Down

0 comments on commit a526fc5

Please sign in to comment.