Skip to content

Commit

Permalink
sql: move maybeIncrementVersion to sqlbase
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
vivekmenezes committed May 16, 2019
1 parent 9ecfb8b commit b77a323
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
30 changes: 1 addition & 29 deletions pkg/sql/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,34 +329,6 @@ func (s LeaseStore) WaitForOneVersion(
return tableDesc.Version, nil
}

func maybeIncrementVersion(
ctx context.Context, desc *sqlbase.MutableTableDescriptor, txn *client.Txn,
) error {
// Already incremented, no-op.
if desc.Version == desc.ClusterVersion.Version+1 {
return nil
}
desc.Version++
// We need to set ModificationTime to the transaction's commit
// timestamp. Using CommitTimestamp() guarantees that the
// transaction will commit at the CommitTimestamp().
//
// TODO(vivek): Stop needing to do this by deprecating the
// ModificationTime. A Descriptor modification time can be
// the mvcc timestamp of the descriptor. This requires moving the
// schema change lease out of the descriptor making the
// descriptor truly immutable at a version.
// Also recognize that the leases are released before the transaction
// is committed through a call to TableCollection.releaseLeases(),
// so updating this policy will also need to consider not doing
// that.
modTime := txn.CommitTimestamp()
desc.ModificationTime = modTime
log.Infof(ctx, "publish: descID=%d (%s) version=%d mtime=%s",
desc.ID, desc.Name, desc.Version, modTime.GoTime())
return nil
}

var errDidntUpdateDescriptor = errors.New("didn't update the table descriptor")

// Publish updates a table descriptor. It also maintains the invariant that
Expand Down Expand Up @@ -416,7 +388,7 @@ func (s LeaseStore) Publish(
tableDesc.Version, version)
}

if err := maybeIncrementVersion(ctx, tableDesc, txn); err != nil {
if err := tableDesc.MaybeIncrementVersion(ctx, txn); err != nil {
return err
}
if err := tableDesc.ValidateTable(s.execCfg.Settings); err != nil {
Expand Down
29 changes: 29 additions & 0 deletions pkg/sql/sqlbase/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,35 @@ func (desc *MutableTableDescriptor) allocateColumnFamilyIDs(columnNames map[stri
}
}

// MaybeIncrementVersion increments the version of a descriptor if necessary.
func (desc *MutableTableDescriptor) MaybeIncrementVersion(
ctx context.Context, txn *client.Txn,
) error {
// Already incremented, no-op.
if desc.Version == desc.ClusterVersion.Version+1 {
return nil
}
desc.Version++
// We need to set ModificationTime to the transaction's commit
// timestamp. Using CommitTimestamp() guarantees that the
// transaction will commit at the CommitTimestamp().
//
// TODO(vivek): Stop needing to do this by deprecating the
// ModificationTime. A Descriptor modification time can be
// the mvcc timestamp of the descriptor. This requires moving the
// schema change lease out of the descriptor making the
// descriptor truly immutable at a version.
// Also recognize that the leases are released before the transaction
// is committed through a call to TableCollection.releaseLeases(),
// so updating this policy will also need to consider not doing
// that.
modTime := txn.CommitTimestamp()
desc.ModificationTime = modTime
log.Infof(ctx, "publish: descID=%d (%s) version=%d mtime=%s",
desc.ID, desc.Name, desc.Version, modTime.GoTime())
return nil
}

// Validate validates that the table descriptor is well formed. Checks include
// both single table and cross table invariants.
func (desc *TableDescriptor) Validate(
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ func (p *planner) writeTableDescToBatch(
}
} else {
// Only increment the table descriptor version once in this transaction.
if err := maybeIncrementVersion(ctx, tableDesc, p.txn); err != nil {
if err := tableDesc.MaybeIncrementVersion(ctx, p.txn); err != nil {
return err
}

Expand Down

0 comments on commit b77a323

Please sign in to comment.