Skip to content

Commit

Permalink
base: ensure that a SQL instance ID is not set to conflicting value
Browse files Browse the repository at this point in the history
Prior to this patch, a SQL instance ID container could be
assigned successively different values. This would make it possible
for erroneous code to mistakenly initialize the instance ID twice,
without any evidence that something was amiss.

This patch fixes this by using the same prevention logic that we
already use for node IDs.

Release note: None
  • Loading branch information
knz committed Nov 10, 2021
1 parent 75257ac commit c5a4716
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pkg/base/node_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,27 @@ func NewSQLIDContainer(sqlInstanceID SQLInstanceID, nodeID *NodeIDContainer) *SQ
// SetSQLInstanceID sets the SQL instance ID. It returns an error if
// we attempt to set an instance ID when the nodeID has already been
// initialized.
func (c *SQLIDContainer) SetSQLInstanceID(sqlInstanceID SQLInstanceID) error {
func (c *SQLIDContainer) SetSQLInstanceID(ctx context.Context, sqlInstanceID SQLInstanceID) error {
if _, ok := c.OptionalNodeID(); ok {
return errors.New("attempting to initialize instance ID when node ID is set")
}

// Use the same logic to set the instance ID as for the node ID.
//
// TODO(knz): All this could be advantageously simplified if we agreed
// to use the same type for NodeIDContainer and SQLIDContainer.
if sqlInstanceID <= 0 {
log.Fatalf(ctx, "trying to set invalid SQLInstanceID: %d", sqlInstanceID)
}
oldVal := atomic.SwapInt32((*int32)(&c.sqlInstanceID), int32(sqlInstanceID))
if oldVal == 0 {
if log.V(2) {
log.Infof(ctx, "SQLInstanceID set to %d", sqlInstanceID)
}
} else if oldVal != int32(sqlInstanceID) {
log.Fatalf(ctx, "different SQLInstanceIDs set: %d, then %d", oldVal, sqlInstanceID)
}

c.sqlInstanceID = sqlInstanceID
c.str.Store(strconv.Itoa(int(sqlInstanceID)))
return nil
Expand Down

0 comments on commit c5a4716

Please sign in to comment.