@@ -1292,8 +1292,6 @@ int bplog_schemachange_run(struct ireq *iq, uuid_t uuid, void *pscs)
12921292 iq -> sc = sc ;
12931293 iq -> sc -> iq = iq ;
12941294 rc = osql_process_schemachange (sc , uuid );
1295- /* remove this from session, cleanup will be done by bp writer */
1296- listc_rfl (scs , sc );
12971295 if (rc )
12981296 break ;
12991297 }
@@ -1306,13 +1304,19 @@ int bplog_schemachange_run(struct ireq *iq, uuid_t uuid, void *pscs)
13061304}
13071305
13081306/* wait for all schema changes to finish */
1309- int bplog_schemachange_wait (struct ireq * iq , int rc )
1307+ int bplog_schemachange_wait (struct ireq * iq , void * pscs , int rc )
13101308{
1311- struct schema_change_type * sc ;
1309+ LISTC_T (struct schema_change_type ) * scs = pscs ;
1310+ struct schema_change_type * sc , * tmp ;
1311+
1312+ /* unlink sclists from iq->sorese */
1313+ LISTC_FOR_EACH_SAFE (scs , sc , tmp , scs_lnk )
1314+ {
1315+ listc_rfl (scs , sc );
1316+ }
13121317
13131318 iq -> sc = sc = iq -> sc_pending ;
13141319 iq -> sc_pending = NULL ;
1315-
13161320 while (sc != NULL ) {
13171321 Pthread_mutex_lock (& sc -> mtx );
13181322 sc -> nothrevent = 1 ;
@@ -1430,7 +1434,7 @@ int bplog_schemachange(struct ireq *iq)
14301434
14311435 rc = bplog_schemachange_run (iq , iq -> sorese -> uuid , & iq -> sorese -> scs );
14321436
1433- return bplog_schemachange_wait (iq , rc );
1437+ return bplog_schemachange_wait (iq , & iq -> sorese -> scs , rc );
14341438}
14351439
14361440int get_schema_change_txns (struct ireq * iq , tran_type * * logi ,
@@ -1457,16 +1461,13 @@ int get_schema_change_txns(struct ireq *iq, tran_type **logi,
14571461 return 0 ;
14581462}
14591463
1460- void * resume_sc_multiddl_txn_finalize (void * p )
1464+ int resume_sc_multiddl_txn_finalize (struct ireq * iq )
14611465{
1462- comdb2_name_thread (__func__ );
1463- bdb_thread_event (thedb -> bdb_env , BDBTHR_EVENT_START_RDWR );
1464-
1465- struct ireq * iq = (struct ireq * )p ;
14661466 struct schema_change_type * sc ;
14671467 tran_type * parent_trans = NULL ;
14681468 int error = 0 ;
14691469 uuid_t uuid ;
1470+ int rc = 0 ;
14701471
14711472 comdb2uuidcpy (uuid , iq -> sc_pending -> uuid );
14721473
@@ -1500,16 +1501,14 @@ void *resume_sc_multiddl_txn_finalize(void *p)
15001501 if (error ) {
15011502 logmsg (LOGMSG_ERROR , "%s: Aborting schema change because of errors\n" ,
15021503 __func__ );
1504+ rc = ERR_SC ;
15031505 goto abort_sc ;
15041506 }
15051507
1506- int rc ;
15071508 if ((rc = get_schema_change_txns (iq , & iq -> sc_logical_tran , & parent_trans ,
15081509 & iq -> sc_tran ))) {
1509- logmsg (LOGMSG_ERROR ,
1510- "%s:%d failed to start schema change transaction\n" , __func__ ,
1511- - rc );
1512- rc = -1 ;
1510+ logmsg (LOGMSG_ERROR , "%s:%d failed to start schema change transaction\n" , __func__ , rc );
1511+ rc = ERR_SC ;
15131512 goto abort_sc ;
15141513 }
15151514
@@ -1538,9 +1537,8 @@ void *resume_sc_multiddl_txn_finalize(void *p)
15381537 iq -> sc_logical_tran = NULL ;
15391538
15401539 osql_postcommit_handle (iq );
1541- bdb_thread_event (thedb -> bdb_env , BDBTHR_EVENT_DONE_RDWR );
15421540
1543- return NULL ;
1541+ return 0 ;
15441542
15451543abort_sc :
15461544 logmsg (LOGMSG_ERROR , "%s: aborting schema change\n" , __func__ );
@@ -1561,7 +1559,42 @@ void *resume_sc_multiddl_txn_finalize(void *p)
15611559 }
15621560
15631561 osql_postabort_handle (iq );
1564- bdb_thread_event (thedb -> bdb_env , BDBTHR_EVENT_DONE_RDWR );
1562+
1563+ return rc ;
1564+ }
1565+
1566+ void * resume_sc_multiddl_txn_finalize_thd (void * arg )
1567+ {
1568+ struct ireq * iq = (struct ireq * )arg ;
1569+ struct schema_change_type * sc ;
1570+
1571+ comdb2_name_thread (__func__ );
1572+ backend_thread_event (thedb , COMDB2_THR_EVENT_START_RDWR );
1573+
1574+ if (!(sc = iq -> scs )) {
1575+ logmsg (LOGMSG_ERROR , "%s NULL sc list?\n" , __func__ );
1576+ backend_thread_event (thedb , COMDB2_THR_EVENT_DONE_RDWR );
1577+ return NULL ;
1578+ }
1579+
1580+ /* the schemas are async resumed; wait here for them
1581+ * to finish
1582+ */
1583+ while (sc ) {
1584+ /* wait for this scs to finish */
1585+ wait_for_schema_change_start (sc , ASYNC_SC_END );
1586+ sc = sc -> scs_lnk .next ;
1587+ }
1588+
1589+ /* at this point, the schema changes have ran and are linked in sc_pending;
1590+ * run the regular finalize routine
1591+ */
1592+ int rc = resume_sc_multiddl_txn_finalize (iq );
1593+ if (rc ) {
1594+ logmsg (LOGMSG_ERROR , "%s failed to finalize rc %d\n" , __func__ , rc );
1595+ }
1596+
1597+ backend_thread_event (thedb , COMDB2_THR_EVENT_DONE_RDWR );
15651598
15661599 return NULL ;
15671600}
@@ -1608,22 +1641,23 @@ int resume_sc_multiddl_txn(sc_list_t *scl)
16081641 struct ireq * iq = calloc (1 , sizeof (struct ireq ));
16091642 init_fake_ireq (thedb , iq );
16101643
1611- /* this starts schema changeas ;
1644+ /* this starts schema changes ;
16121645 * the alters have to register themselves inline,
1613- * but the rest of the execution is done in parallel
1614- * waiting is done by a separate thread that will finalize
1615- * the schema change
1616- * NOTE: schema changes will be queued in iq->sc_pending
1646+ * but the rest of the execution is done in a parallel thread,
1647+ * which is also responsible for finalizing the transaction
16171648 *
16181649 */
16191650 rc = bplog_schemachange_run (iq , scl -> uuid , & scs );
16201651 if (rc ) {
1652+ /* TODO: free the scs */
16211653 free (iq );
16221654 return -1 ;
16231655 }
16241656
1657+ /* prepare the wait for scs */
1658+ iq -> scs = scs .top ;
1659+
16251660 pthread_t tid ;
1626- Pthread_create (& tid , & gbl_pthread_attr_detached ,
1627- resume_sc_multiddl_txn_finalize , iq );
1661+ Pthread_create (& tid , & gbl_pthread_attr_detached , resume_sc_multiddl_txn_finalize_thd , iq );
16281662 return 0 ;
16291663}
0 commit comments