@@ -591,13 +591,14 @@ int do_alter_table(struct ireq *iq, struct schema_change_type *s,
591591
592592 /* set sc_genids, 0 them if we are starting a new schema change, or
593593 * restore them to their previous values if we are resuming */
594- if (init_sc_genids (newdb , s )) {
594+ /* if (init_sc_genids(newdb, s)) {
595595 sc_errf(s, "failed initilizing sc_genids\n");
596596 delete_temp_table(iq, newdb);
597597 change_schemas_recover(s->tablename);
598598 decrement_sc_yet_to_resume_counter();
599599 return -1;
600- }
600+ }*/
601+ // todo: detect if this is alter or merge and init genids accordingly.
601602
602603 Pthread_rwlock_wrlock (& db -> sc_live_lk );
603604 db -> sc_from = s -> db = db ;
@@ -654,7 +655,10 @@ int do_alter_table(struct ireq *iq, struct schema_change_type *s,
654655 changed == SC_CONSTRAINT_CHANGE ) {
655656 if (!s -> live )
656657 gbl_readonly_sc = 1 ;
657- rc = convert_all_records (db , newdb , newdb -> sc_genids , s );
658+
659+ unsigned long long sc_genids [MAXDTASTRIPE ];
660+ memset (sc_genids , 0 , MAXDTASTRIPE );
661+ rc = convert_all_records (db , newdb , sc_genids , s );
658662 if (rc == 1 ) rc = 0 ;
659663 } else
660664 rc = 0 ;
@@ -775,13 +779,6 @@ static int do_merge_table(struct ireq *iq, struct schema_change_type *s,
775779 return -1 ;
776780 }
777781
778- /* set sc_genids, 0 them if we are starting a new schema change, or
779- * restore them to their previous values if we are resuming */
780- if (init_sc_genids (newdb , s )) {
781- sc_client_error (s , "Failed to initialize sc_genids" );
782- return -1 ;
783- }
784-
785782 Pthread_rwlock_wrlock (& db -> sc_live_lk );
786783 db -> sc_from = s -> db = db ;
787784 db -> sc_to = s -> newdb = newdb ;
@@ -794,6 +791,9 @@ static int do_merge_table(struct ireq *iq, struct schema_change_type *s,
794791 assert (db -> sc_from == db && s -> db == db );
795792 assert (db -> sc_to == newdb && s -> newdb == newdb );
796793 assert (db -> doing_conversion == 1 );
794+ if (s -> resume && IS_ALTERTABLE (s )) {
795+ decrement_sc_yet_to_resume_counter ();
796+ }
797797 MEMORY_SYNC ;
798798
799799 if (get_stopsc (__func__ , __LINE__ )) {
@@ -813,8 +813,10 @@ static int do_merge_table(struct ireq *iq, struct schema_change_type *s,
813813 struct schema * tag = find_tag_schema (newdb , ".NEW..ONDISK" );
814814 if (!tag ) {
815815 struct errstat err = {0 };
816+ Pthread_mutex_lock (& csc2_subsystem_mtx );
816817 rc =
817818 populate_db_with_alt_schema (thedb , newdb , newdb -> csc2_schema , & err );
819+ Pthread_mutex_unlock (& csc2_subsystem_mtx );
818820 if (rc ) {
819821 logmsg (LOGMSG_ERROR , "%s\ncsc2: \"%s\"\n" , err .errstr ,
820822 newdb -> csc2_schema );
@@ -825,9 +827,12 @@ static int do_merge_table(struct ireq *iq, struct schema_change_type *s,
825827
826828 add_ongoing_alter (s );
827829
830+ unsigned long long sc_genids [MAXDTASTRIPE ];
831+ memset (sc_genids , 0 , MAXDTASTRIPE );
832+
828833 /* skip converting records for fastinit and planned schema change
829834 * that doesn't require rebuilding anything. */
830- rc = convert_all_records (db , newdb , newdb -> sc_genids , s );
835+ rc = convert_all_records (db , newdb , sc_genids , s );
831836 if (rc == 1 ) rc = 0 ;
832837
833838 remove_ongoing_alter (s );
@@ -866,7 +871,7 @@ static int do_merge_table(struct ireq *iq, struct schema_change_type *s,
866871
867872 for (i = 0 ; i < gbl_dtastripe ; i ++ ) {
868873 sc_errf (s , " > [%s] stripe %2d was at 0x%016llx\n" , s -> tablename ,
869- i , newdb -> sc_genids [i ]);
874+ i , sc_genids [i ]);
870875 }
871876
872877 while (s -> logical_livesc ) {
@@ -885,6 +890,14 @@ static int do_merge_table(struct ireq *iq, struct schema_change_type *s,
885890 /* handle renaming sqlite_stat1 entries for idx */
886891 check_for_idx_rename (s -> newdb , s -> db );
887892
893+ // All shards point to the same newdb.
894+ //
895+ // By setting it to NULL here, we ensure that
896+ // all shards except for the first shard (which does not run
897+ // do_merge_table) have newdb set to NULL.
898+ // This avoids double-freeing it.
899+ if (rc == SC_MASTER_DOWNGRADE ) { s -> newdb = NULL ; }
900+
888901 return SC_OK ;
889902}
890903
0 commit comments