Skip to content

Commit 16a3c27

Browse files
{180685083} feat(reqlog): post schema change activity
Statreqs tell us what the database is busy doing. If any schema changes are running, that is significant and worth reporting in statreqs. Schema change stats are logged in a dedicated section that looks like this: ```text 11/29 02:55:00: SCHEMA CHANGE STATS 11/29 02:55:00: table 't' records converted 269805 diff 169469 rate 84734 r/s adds 0 updates 0 deletes 77 ``` Signed-off-by: Shoumyo Chakravorti <[email protected]>
1 parent 3e01d98 commit 16a3c27

File tree

4 files changed

+122
-10
lines changed

4 files changed

+122
-10
lines changed

db/comdb2.c

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ void berk_memp_sync_alarm_ms(int);
141141
#include <net_appsock.h>
142142
#include "sc_csc2.h"
143143
#include "sc_util.h"
144+
#include "sc_global.h"
144145
#include "reverse_conn.h"
145146
#include "alias.h"
146147
#include "str_util.h" /* QUOTE */
@@ -4626,8 +4627,6 @@ int throttle_lim = 10000;
46264627
int cpu_throttle_threshold = 100000;
46274628

46284629
double gbl_cpupercent;
4629-
#include <sc_util.h>
4630-
46314630

46324631
static inline void log_tbl_item(int64_t curr, int64_t *prev, const char *(*type_to_str)(int), int type, char *string,
46334632
int *hdr_p, struct reqlogger *statlogger, dbtable *tbl, int first)
@@ -4729,6 +4728,8 @@ void *statthd(void *p)
47294728
int have_scon_stats = 0;
47304729
int64_t rw_evicts;
47314730

4731+
int last_num_sc = 0;
4732+
struct running_sc_info *last_schema_changes = NULL;
47324733

47334734
thrman_register(THRTYPE_GENERIC);
47344735
thread_started("statthd");
@@ -4902,7 +4903,9 @@ void *statthd(void *p)
49024903
}
49034904

49044905
int aa_include_updates = bdb_attr_get(thedb->bdb_attr, BDB_ATTR_AA_COUNT_UPD);
4906+
49054907
rdlock_schema_lk();
4908+
49064909
for (ii = 0; ii < dbenv->num_dbs; ++ii) {
49074910
dbtable *tbl = dbenv->dbs[ii];
49084911
int hdr = 0;
@@ -4949,6 +4952,50 @@ void *statthd(void *p)
49494952
log_tbl_item(tbl->deadlock_count, &tbl->saved_deadlock_count, NULL, 0, "deadlock count", &hdr,
49504953
statlogger, tbl, 0);
49514954
}
4955+
4956+
int num_sc = 0;
4957+
struct running_sc_info *schema_changes = NULL;
4958+
4959+
if (list_running_schema_changes(&schema_changes, &num_sc) == 0) {
4960+
if (num_sc > 0) {
4961+
/* log header */
4962+
reqlog_logf(statlogger, REQL_INFO, "SCHEMA CHANGE STATS\n");
4963+
}
4964+
4965+
for (ii = 0; ii < num_sc; ii++) {
4966+
struct running_sc_info sc = schema_changes[ii];
4967+
struct running_sc_info *last_sc = NULL;
4968+
4969+
for (jj = 0; jj < last_num_sc; jj++) {
4970+
if (strcmp(last_schema_changes[jj].table_name, sc.table_name) == 0) {
4971+
last_sc = &last_schema_changes[jj];
4972+
break;
4973+
}
4974+
}
4975+
4976+
uint64_t last_nrecs = last_sc ? last_sc->nrecs : 0;
4977+
uint32_t last_adds = last_sc ? last_sc->adds : 0;
4978+
uint32_t last_updates = last_sc ? last_sc->updates : 0;
4979+
uint32_t last_deletes = last_sc ? last_sc->deletes : 0;
4980+
4981+
uint64_t diff_nrecs = sc.nrecs - last_nrecs;
4982+
4983+
reqlog_logf(
4984+
statlogger, REQL_INFO,
4985+
" table '%s' records converted %ld diff %ld rate %ld r/s adds %d updates %d deletes %d\n",
4986+
sc.table_name, //
4987+
sc.nrecs, diff_nrecs, diff_nrecs / thresh, //
4988+
sc.adds - last_adds, //
4989+
sc.updates - last_updates, //
4990+
sc.deletes - last_deletes);
4991+
}
4992+
4993+
free(last_schema_changes);
4994+
4995+
last_num_sc = num_sc;
4996+
last_schema_changes = schema_changes;
4997+
}
4998+
49524999
unlock_schema_lk();
49535000

49545001
pstats = bdb_get_process_stats();

schemachange/sc_global.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,49 @@ void sc_alter_latency(int counter)
697697
}
698698
}
699699
}
700+
701+
int list_running_schema_changes(struct running_sc_info **info, int *num_running_sc)
702+
{
703+
unsigned int bucket;
704+
void *entry;
705+
sc_table_t *sc_table = NULL;
706+
*num_running_sc = 0;
707+
708+
Pthread_mutex_lock(&schema_change_in_progress_mutex);
709+
710+
if (!sc_tables) {
711+
Pthread_mutex_unlock(&schema_change_in_progress_mutex);
712+
return 1;
713+
}
714+
715+
/* preallocate outparam */
716+
int unused;
717+
int num_entries = 0;
718+
hash_info(sc_tables, &num_entries, &unused, &unused, &unused, &unused, &unused, &unused);
719+
*info = realloc(*info, sizeof(struct running_sc_info) * num_entries);
720+
721+
for (sc_table = hash_first(sc_tables, &entry, &bucket); sc_table;
722+
sc_table = hash_next(sc_tables, &entry, &bucket)) {
723+
724+
/* get_dbtable_by_name looks at thedb->db_hash, so caller must
725+
* hold the schema lock */
726+
struct dbtable *db = get_dbtable_by_name(sc_table->tablename);
727+
728+
Pthread_rwlock_rdlock(&db->sc_live_lk);
729+
730+
if (db->doing_conversion || db->doing_upgrade) {
731+
struct running_sc_info *sc_info = &((*info)[*num_running_sc]);
732+
sc_info->table_name = strdup(sc_table->tablename);
733+
sc_info->nrecs = db->sc_nrecs;
734+
sc_info->adds = db->sc_adds;
735+
sc_info->updates = db->sc_updates;
736+
sc_info->deletes = db->sc_deletes;
737+
++(*num_running_sc);
738+
}
739+
740+
Pthread_rwlock_unlock(&db->sc_live_lk);
741+
}
742+
743+
Pthread_mutex_unlock(&schema_change_in_progress_mutex);
744+
return 0;
745+
}

schemachange/sc_global.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#ifndef INCLUDE_SC_GLOBAL_H
1818
#define INCLUDE_SC_GLOBAL_H
1919

20+
#include <stdint.h>
21+
22+
#include "schemachange.h"
23+
2024
extern pthread_mutex_t schema_change_in_progress_mutex;
2125
extern pthread_mutex_t fastinit_in_progress_mutex;
2226
extern pthread_mutex_t schema_change_sbuf2_lock;
@@ -60,6 +64,14 @@ extern int rep_sync_save;
6064
extern int log_sync_save;
6165
extern int log_sync_time_save;
6266

67+
struct running_sc_info {
68+
const char *table_name;
69+
uint64_t nrecs; /* num records converted (does not include `adds`) */
70+
uint32_t adds; /* num added in front of sc cursor */
71+
uint32_t updates; /* num updated behind sc cursor */
72+
uint32_t deletes; /* num deleted behind sc cursor */
73+
};
74+
6375
int is_dta_being_rebuilt(struct scplan *plan);
6476
const char *get_sc_to_name(const char *);
6577
void wait_for_sc_to_stop(const char *operation, const char *func, int line);
@@ -87,5 +99,8 @@ struct schema_change_type *preempt_ongoing_alter(char *table, int action);
8799
void clear_ongoing_alter();
88100
int get_stopsc(const char *func, int line);
89101
void sc_alter_latency(int counter);
102+
/* List all running schema changes and their progress. Caller must acquire schema
103+
* lock in read mode. */
104+
int list_running_schema_changes(struct running_sc_info **info, int *num_running_sc);
90105

91106
#endif

schemachange/sc_records.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,20 @@ static inline int print_aggregate_sc_stat(struct convert_record_data *data,
140140
/* totals across all threads */
141141
if (data->scanmode != SCAN_PARALLEL) return 1;
142142

143-
long long total_nrecs_diff =
144-
data->from->sc_nrecs - data->from->sc_prev_nrecs;
143+
uint64_t total_nrecs_diff = data->from->sc_nrecs - data->from->sc_prev_nrecs;
145144
data->from->sc_prev_nrecs = data->from->sc_nrecs;
145+
146+
/* actual progress made after accounting for updates and deletes that
147+
* happened behind the schema change cursor (by definition, all adds happen
148+
* in front of the schema change cursor) */
149+
uint64_t actual_nrecs = data->from->sc_nrecs - (data->from->sc_updates + data->from->sc_deletes);
150+
146151
sc_printf(data->s,
147-
"[%s] progress TOTAL %lld +%lld actual "
148-
"progress total %lld rate %lld r/s\n",
149-
data->from->tablename, data->from->sc_nrecs, total_nrecs_diff,
150-
data->from->sc_nrecs -
151-
(data->from->sc_adds + data->from->sc_updates),
152-
total_nrecs_diff / sc_report_freq);
152+
"[%s] progress TOTAL %lld +%lld rate %lld r/s "
153+
"actual progress total %lld\n",
154+
data->from->tablename, data->from->sc_nrecs, total_nrecs_diff, total_nrecs_diff / sc_report_freq,
155+
actual_nrecs);
156+
153157
return 1;
154158
}
155159

0 commit comments

Comments
 (0)