46
46
static int analyze_thread_memory = 1048576 ;
47
47
#endif
48
48
49
+ int gbl_debug_sleep_on_analyze = 0 ;
50
+
49
51
extern void reset_aa_counter (char * tblname );
50
52
51
53
/* global is-running flag */
@@ -590,11 +592,11 @@ static int local_replicate_write_analyze(char *table)
590
592
591
593
/* get tbl sampling threshold, if NOT -1 set it
592
594
*/
593
- static void get_sampling_threshold (char * table , long long * sampling_thresh )
595
+ static void get_sampling_threshold (tran_type * tran , char * table , long long * sampling_thresh )
594
596
{
595
597
int bdberr = 0 ;
596
598
long long threshold = 0 ;
597
- bdb_get_analyzethreshold_table (NULL , table , & threshold , & bdberr );
599
+ bdb_get_analyzethreshold_table (tran , table , & threshold , & bdberr );
598
600
599
601
#ifdef DEBUG
600
602
printf ("retrieving from llmeta saved threshold for table '%s': %lld\n" ,
@@ -759,20 +761,49 @@ static int analyze_rename_table_for_backup_stats(char *table, struct sqlclntstat
759
761
760
762
int analyze_regular_table (table_descriptor_t * td , struct sqlclntstate * clnt , char * zErrTab , size_t nErrTab ) {
761
763
int rc = 0 ;
764
+ int have_schema_lk = 0 ;
762
765
int sampled_table = 0 ;
763
- struct dbtable * table = get_dbtable_by_name (td -> table );
764
- rc = analyze_rename_table_for_backup_stats (td -> table , clnt , zErrTab );
766
+ tran_type * tran = NULL ;
767
+
768
+ rdlock_schema_lk ();
769
+ have_schema_lk = 1 ;
770
+
771
+ struct dbtable * const table = get_dbtable_by_name (td -> table );
772
+ if (!table ) {
773
+ logmsg (LOGMSG_ERROR , "%s: Cannot find table %s\n" , __func__ , td -> table );
774
+ rc = 1 ;
775
+ goto err ;
776
+ }
765
777
766
- if (rc )
778
+ tran = get_read_only_tran ();
779
+ if (!tran ) {
780
+ logmsg (LOGMSG_ERROR , "%s: Could not get tran\n" , __func__ );
781
+ rc = 1 ;
767
782
goto err ;
783
+ }
768
784
769
- /* grab the size of the table */
770
- int64_t totsiz = calc_table_size (table , 1 );
785
+ rc = bdb_lock_tablename_read (table -> handle , table -> tablename , tran );
786
+ if (rc ) {
787
+ logmsg (LOGMSG_ERROR , "%s: Failed to acquire read lock on %s\n" , __func__ ,
788
+ table -> tablename );
789
+ rc = 1 ;
790
+ goto err ;
791
+ }
792
+
793
+ unlock_schema_lk ();
794
+ have_schema_lk = 0 ;
795
+
796
+ rc = analyze_rename_table_for_backup_stats (td -> table , clnt , zErrTab );
797
+ if (rc ) {
798
+ logmsg (LOGMSG_ERROR , "analyze_rename_table_for_backup_stats failed with rc %d\n" , rc );
799
+ goto err ;
800
+ }
801
+
802
+ const int64_t totsiz = calc_table_size (table , 1 );
771
803
772
804
if (sampled_tables_enabled )
773
- get_sampling_threshold (td -> table , & sampling_threshold );
805
+ get_sampling_threshold (tran , td -> table , & sampling_threshold );
774
806
775
- /* sample if enabled */
776
807
if (sampled_tables_enabled ) {
777
808
if (totsiz <= sampling_threshold ) {
778
809
td -> scale = 100 ;
@@ -787,8 +818,19 @@ int analyze_regular_table(table_descriptor_t *td, struct sqlclntstate *clnt, cha
787
818
}
788
819
}
789
820
821
+ rc = put_read_only_tran (tran );
822
+ tran = NULL ;
823
+ if (rc ) {
824
+ logmsg (LOGMSG_ERROR , "%s: Failed to put tran rc(%d)\n" , __func__ , rc );
825
+ goto err ;
826
+ }
827
+
790
828
clnt -> is_analyze = 1 ;
791
829
830
+ if (gbl_debug_sleep_on_analyze ) {
831
+ sleep (gbl_debug_sleep_on_analyze );
832
+ }
833
+
792
834
/* run analyze as sql query */
793
835
char * sql = sqlite3_mprintf ("analyzesqlite main.\"%w\"" , td -> table );
794
836
assert (sql != NULL );
@@ -803,6 +845,12 @@ int analyze_regular_table(table_descriptor_t *td, struct sqlclntstate *clnt, cha
803
845
if (debug_switch_test_delay_analyze_commit ())
804
846
sleep (10 );
805
847
err :
848
+ if (have_schema_lk ) {
849
+ unlock_schema_lk ();
850
+ }
851
+ if (tran ) {
852
+ put_read_only_tran (tran );
853
+ }
806
854
if (sampled_table ) {
807
855
cleanup_sampled_indicies (clnt );
808
856
}
0 commit comments