func (wr *Wrangler) applySchemaShardSimple(statusArray []*TabletStatus, preflight *myproto.SchemaChangeResult, masterTabletAlias topo.TabletAlias, change string, force bool) (*myproto.SchemaChangeResult, error) { // check all tablets have the same schema as the master's // BeforeSchema. If not, we shouldn't proceed log.Infof("Checking schema on all tablets") for _, status := range statusArray { diffs := myproto.DiffSchemaToArray("master", preflight.BeforeSchema, status.ti.Alias.String(), status.beforeSchema) if len(diffs) > 0 { if force { log.Warningf("Tablet %v has inconsistent schema, ignoring: %v", status.ti.Alias, strings.Join(diffs, "\n")) } else { return nil, fmt.Errorf("Tablet %v has inconsistent schema: %v", status.ti.Alias, strings.Join(diffs, "\n")) } } } // we're good, just send to the master log.Infof("Applying schema change to master in simple mode") sc := &myproto.SchemaChange{Sql: change, Force: force, AllowReplication: true, BeforeSchema: preflight.BeforeSchema, AfterSchema: preflight.AfterSchema} return wr.ApplySchema(masterTabletAlias, sc) }
// ApplySchemaChange will apply the schema change to the given database. func (mysqld *Mysqld) ApplySchemaChange(dbName string, change *proto.SchemaChange) (*proto.SchemaChangeResult, error) { // check current schema matches beforeSchema, err := mysqld.GetSchema(dbName, nil, nil, false) if err != nil { return nil, err } if change.BeforeSchema != nil { schemaDiffs := proto.DiffSchemaToArray("actual", beforeSchema, "expected", change.BeforeSchema) if len(schemaDiffs) > 0 { for _, msg := range schemaDiffs { log.Warningf("BeforeSchema differs: %v", msg) } // let's see if the schema was already applied if change.AfterSchema != nil { schemaDiffs = proto.DiffSchemaToArray("actual", beforeSchema, "expected", change.AfterSchema) if len(schemaDiffs) == 0 { // no diff between the schema we expect // after the change and the current // schema, we already applied it return &proto.SchemaChangeResult{BeforeSchema: beforeSchema, AfterSchema: beforeSchema}, nil } } if change.Force { log.Warningf("BeforeSchema differs, applying anyway") } else { return nil, fmt.Errorf("BeforeSchema differs") } } } sql := change.Sql if !change.AllowReplication { sql = "SET sql_log_bin = 0;\n" + sql } // add a 'use XXX' in front of the SQL sql = "USE " + dbName + ";\n" + sql // execute the schema change using an external mysql process // (to benefit from the extra commands in mysql cli) if err = mysqld.ExecuteMysqlCommand(sql); err != nil { return nil, err } // get AfterSchema afterSchema, err := mysqld.GetSchema(dbName, nil, nil, false) if err != nil { return nil, err } // compare to the provided AfterSchema if change.AfterSchema != nil { schemaDiffs := proto.DiffSchemaToArray("actual", afterSchema, "expected", change.AfterSchema) if len(schemaDiffs) > 0 { for _, msg := range schemaDiffs { log.Warningf("AfterSchema differs: %v", msg) } if change.Force { log.Warningf("AfterSchema differs, not reporting error") } else { return nil, fmt.Errorf("AfterSchema differs") } } } return &proto.SchemaChangeResult{BeforeSchema: beforeSchema, AfterSchema: afterSchema}, nil }
func (wr *Wrangler) applySchemaKeyspace(keyspace string, change string, simple, force bool) (*myproto.SchemaChangeResult, error) { shards, err := wr.ts.GetShardNames(keyspace) if err != nil { return nil, err } // corner cases if len(shards) == 0 { return nil, fmt.Errorf("No shards in keyspace %v", keyspace) } if len(shards) == 1 { log.Infof("Only one shard in keyspace %v, using ApplySchemaShard", keyspace) return wr.ApplySchemaShard(keyspace, shards[0], change, topo.TabletAlias{}, simple, force) } // Get schema on all shard masters in parallel log.Infof("Getting schema on all shards") beforeSchemas := make([]*myproto.SchemaDefinition, len(shards)) shardInfos := make([]*topo.ShardInfo, len(shards)) wg := sync.WaitGroup{} mu := sync.Mutex{} getErrs := make([]string, 0, 5) for i, shard := range shards { wg.Add(1) go func(i int, shard string) { var err error defer func() { if err != nil { mu.Lock() getErrs = append(getErrs, err.Error()) mu.Unlock() } wg.Done() }() shardInfos[i], err = wr.ts.GetShard(keyspace, shard) if err != nil { return } beforeSchemas[i], err = wr.GetSchema(shardInfos[i].MasterAlias, nil, nil, false) }(i, shard) } wg.Wait() if len(getErrs) > 0 { return nil, fmt.Errorf("Error(s) getting schema: %v", strings.Join(getErrs, ", ")) } // check they all match, or use the force flag log.Infof("Checking starting schemas match on all shards") for i, beforeSchema := range beforeSchemas { if i == 0 { continue } diffs := myproto.DiffSchemaToArray("shard 0", beforeSchemas[0], fmt.Sprintf("shard %v", i), beforeSchema) if len(diffs) > 0 { if force { log.Warningf("Shard %v has inconsistent schema, ignoring: %v", i, strings.Join(diffs, "\n")) } else { return nil, fmt.Errorf("Shard %v has inconsistent schema: %v", i, strings.Join(diffs, "\n")) } } } // preflight on shard 0 master, to get baseline // this assumes shard 0 master doesn't have the schema upgrade applied // if it does, we'll have to fix the slaves and other shards manually. log.Infof("Running Preflight on Shard 0 Master") preflight, err := wr.PreflightSchema(shardInfos[0].MasterAlias, change) if err != nil { return nil, err } // for each shard, apply the change log.Infof("Applying change on all shards") var applyErr error for i, shard := range shards { wg.Add(1) go func(i int, shard string) { defer wg.Done() _, err := wr.lockAndApplySchemaShard(shardInfos[i], preflight, keyspace, shard, shardInfos[i].MasterAlias, change, topo.TabletAlias{}, simple, force) if err != nil { mu.Lock() applyErr = err mu.Unlock() return } }(i, shard) } wg.Wait() if applyErr != nil { return nil, applyErr } return &myproto.SchemaChangeResult{BeforeSchema: preflight.BeforeSchema, AfterSchema: preflight.AfterSchema}, nil }
func (wr *Wrangler) applySchemaShardComplex(statusArray []*TabletStatus, shardInfo *topo.ShardInfo, preflight *myproto.SchemaChangeResult, masterTabletAlias topo.TabletAlias, change string, newParentTabletAlias topo.TabletAlias, force bool) (*myproto.SchemaChangeResult, error) { // apply the schema change to all replica / slave tablets for _, status := range statusArray { // if already applied, we skip this guy diffs := myproto.DiffSchemaToArray("after", preflight.AfterSchema, status.ti.Alias.String(), status.beforeSchema) if len(diffs) == 0 { log.Infof("Tablet %v already has the AfterSchema, skipping", status.ti.Alias) continue } // make sure the before schema matches diffs = myproto.DiffSchemaToArray("master", preflight.BeforeSchema, status.ti.Alias.String(), status.beforeSchema) if len(diffs) > 0 { if force { log.Warningf("Tablet %v has inconsistent schema, ignoring: %v", status.ti.Alias, strings.Join(diffs, "\n")) } else { return nil, fmt.Errorf("Tablet %v has inconsistent schema: %v", status.ti.Alias, strings.Join(diffs, "\n")) } } // take this guy out of the serving graph if necessary ti, err := wr.ts.GetTablet(status.ti.Alias) if err != nil { return nil, err } typeChangeRequired := ti.Tablet.IsInServingGraph() if typeChangeRequired { // note we want to update the serving graph there err = wr.changeTypeInternal(ti.Alias, topo.TYPE_SCHEMA_UPGRADE) if err != nil { return nil, err } } // apply the schema change log.Infof("Applying schema change to slave %v in complex mode", status.ti.Alias) sc := &myproto.SchemaChange{Sql: change, Force: force, AllowReplication: false, BeforeSchema: preflight.BeforeSchema, AfterSchema: preflight.AfterSchema} _, err = wr.ApplySchema(status.ti.Alias, sc) if err != nil { return nil, err } // put this guy back into the serving graph if typeChangeRequired { err = wr.changeTypeInternal(ti.Alias, ti.Tablet.Type) if err != nil { return nil, err } } } // if newParentTabletAlias is passed in, use that as the new master if !newParentTabletAlias.IsZero() { log.Infof("Reparenting with new master set to %v", newParentTabletAlias) tabletMap, err := topo.GetTabletMapForShard(context.TODO(), wr.ts, shardInfo.Keyspace(), shardInfo.ShardName()) if err != nil { return nil, err } slaveTabletMap, masterTabletMap := topotools.SortedTabletMap(tabletMap) newMasterTablet, err := wr.ts.GetTablet(newParentTabletAlias) if err != nil { return nil, err } // Create reusable Reparent event with available info ev := &events.Reparent{ ShardInfo: *shardInfo, NewMaster: *newMasterTablet.Tablet, } if oldMasterTablet, ok := tabletMap[shardInfo.MasterAlias]; ok { ev.OldMaster = *oldMasterTablet.Tablet } err = wr.reparentShardGraceful(ev, shardInfo, slaveTabletMap, masterTabletMap, newMasterTablet /*leaveMasterReadOnly*/, false) if err != nil { return nil, err } // Here we would apply the schema change to the old // master, but after a reparent it's in Scrap state, // so no need to. When/if reparent leaves the // original master in a different state (like replica // or rdonly), then we should apply the schema there // too. log.Infof("Skipping schema change on old master %v in complex mode, it's been Scrapped", masterTabletAlias) } return &myproto.SchemaChangeResult{BeforeSchema: preflight.BeforeSchema, AfterSchema: preflight.AfterSchema}, nil }