Skip to content

Commit

Permalink
Set generation from ckp-logs
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Hannum <mhannum72@gmail.com>
  • Loading branch information
markhannum authored and akshatsikarwar committed Dec 16, 2024
1 parent 86e067e commit cdeef87
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 126 deletions.
11 changes: 6 additions & 5 deletions bdb/phys_rep_lsn.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
logmsg(lvl, "physrep: " __VA_ARGS__); \
} while (0)

int matchable_log_type(int rectype);
int matchable_log_type(DB_ENV *dbenv, int rectype);

extern int gbl_physrep_debug;
int gbl_physrep_exit_on_invalid_logstream = 0;
Expand Down Expand Up @@ -126,7 +126,7 @@ int find_log_timestamp(bdb_state_type *bdb_state, time_t time,
LOGCOPY_32(&rectype, logrec.data);
normalize_rectype(&rectype);

} while (!matchable_log_type(rectype));
} while (!matchable_log_type(bdb_state->dbenv, rectype));

my_time = get_timestamp_from_matchable_record(logrec.data);
if (gbl_physrep_debug) {
Expand Down Expand Up @@ -194,7 +194,7 @@ static int get_next_matchable(DB_LOGC *logc, LOG_INFO *info, int check_current,
LOGCOPY_32(&rectype, logrec->data);
normalize_rectype(&rectype);

if (matchable_log_type(rectype) && in_parent_range(&match_lsn, parent_highest, parent_lowest)) {
if (matchable_log_type(logc->dbenv, rectype) && in_parent_range(&match_lsn, parent_highest, parent_lowest)) {
if (gbl_physrep_debug) {
physrep_logmsg(LOGMSG_USER, "%s: Initial rec {%u:%u} is matchable\n",
__func__, info->file, info->offset);
Expand Down Expand Up @@ -228,7 +228,8 @@ static int get_next_matchable(DB_LOGC *logc, LOG_INFO *info, int check_current,

LOGCOPY_32(&rectype, logrec->data);
normalize_rectype(&rectype);
matchable = (matchable_log_type(rectype) && in_parent_range(&match_lsn, parent_highest, parent_lowest));
matchable =
(matchable_log_type(logc->dbenv, rectype) && in_parent_range(&match_lsn, parent_highest, parent_lowest));
} while (!matchable);

info->file = match_lsn.file;
Expand Down Expand Up @@ -565,7 +566,7 @@ int physrep_bdb_wait_for_seqnum(bdb_state_type *bdb_state, DB_LSN *lsn, void *da

LOGCOPY_32(&rectype, data);
normalize_rectype(&rectype);
if (!matchable_log_type(rectype)) {
if (!matchable_log_type(bdb_state->dbenv, rectype)) {
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion berkdb/env/env_recover.c
Original file line number Diff line number Diff line change
Expand Up @@ -2662,7 +2662,7 @@ __env_find_verify_recover_start(dbenv, lsnp)
do {
LOGCOPY_32(&rectype, rec.data);
normalize_rectype(&rectype);
} while ((!matchable_log_type(rectype) || log_compare(lsnp, &s_lsn) >= 0) &&
} while ((!matchable_log_type(dbenv, rectype) || log_compare(lsnp, &s_lsn) >= 0) &&
(ret = __log_c_get(logc, lsnp, &rec, DB_PREV)) == 0);

if (ret != 0)
Expand Down
95 changes: 61 additions & 34 deletions berkdb/rep/rep_method.c
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,7 @@ __retrieve_logged_generation_commitlsn(dbenv, lsn, gen)
return ret;
}

extern int gbl_debug_election;
/*
* __rep_elect --
* Called after master failure to hold/participate in an election for
Expand Down Expand Up @@ -1260,15 +1261,17 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
*/
if (in_progress) {
*eidp = dbenv->rep_eid;
logmsg(LOGMSG_DEBUG, "%s line %d returning %d master %s egen is %d\n",
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d returning %d master %s egen is %d\n",
__func__, __LINE__, ret, *eidp, *newgen);
return (0);
}
#if 0
fprintf(stderr, "%s:%d broadcasting REP_MASTER_REQ\n",
__FILE__, __LINE__);
#endif
logmsg(LOGMSG_DEBUG, "%s start sending master req\n", __func__);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s start sending master req\n", __func__);
send_master_req(dbenv, __func__, __LINE__);
ret = __rep_wait(dbenv, timeout / 4, eidp, newgen, 0, REP_F_EPHASE1);
switch (ret) {
Expand All @@ -1279,7 +1282,8 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
if (FLD_ISSET(dbenv->verbose, DB_VERB_REPLICATION))
__db_err(dbenv, "Found master %d", *eidp);
#endif
logmsg(LOGMSG_DEBUG, "%s line %d returning %d master %s egen is %d\n",
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d returning %d master %s egen is %d\n",
__func__, __LINE__, ret, *eidp, *newgen);
return (0);
}
Expand Down Expand Up @@ -1308,7 +1312,8 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
MUTEX_LOCK(dbenv, db_rep->rep_mutexp);

/* WAITSTART pushes us past point of no-return */
logmsg(LOGMSG_DEBUG, "%s line %d setting PHASE1 clearing TALLY\n", __func__, __LINE__);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d setting PHASE1 clearing TALLY\n", __func__, __LINE__);
F_SET(rep, REP_F_EPHASE1 | REP_F_WAITSTART | REP_F_NOARCHIVE);
F_CLR(rep, REP_F_TALLY);

Expand All @@ -1329,7 +1334,8 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
/* Tally our own vote */
if (__rep_tally(dbenv, rep, rep->eid, &rep->sites, rep->egen,
rep->tally_off, __func__, __LINE__) != 0) {
logmsg(LOGMSG_DEBUG, "%s line %d rep-tally failed, lockdone\n", __func__, __LINE__);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d rep-tally failed, lockdone\n", __func__, __LINE__);
goto lockdone;
}
__rep_cmp_vote(dbenv, rep, &rep->eid, rep->egen, &lsn, priority,
Expand All @@ -1345,12 +1351,13 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
egen = rep->egen;
committed_gen = rep->committed_gen;
send_vote2 = (rep->sites >= rep->nsites && rep->w_priority != 0);
logmsg(LOGMSG_DEBUG, "%s line %d send_vote2 is %d, rep->sites is %d, rep->nsites is %d\n",
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d send_vote2 is %d, rep->sites is %d, rep->nsites is %d\n",
__func__, __LINE__, send_vote2, rep->sites, rep->nsites);

/* If we have all vote1, change to PHASE2 immediately */
if (send_vote2) {
logmsg(LOGMSG_DEBUG, "%s line %d clearing PHASE1 setting PHASE2\n", __func__, __LINE__);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG, "%s line %d clearing PHASE1 setting PHASE2\n", __func__, __LINE__);
F_SET(rep, REP_F_EPHASE2);
F_CLR(rep, REP_F_EPHASE1);
if (rep->winner == rep->eid) {
Expand All @@ -1363,12 +1370,14 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
Pthread_mutex_unlock(&rep_candidate_lock);

if (use_committed_gen) {
logmsg(LOGMSG_DEBUG, "%s line %d broadcasting REP_GEN_VOTE1 to all with committed-gen=%d gen=%d egen=%d\n",
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d broadcasting REP_GEN_VOTE1 to all with committed-gen=%d gen=%d egen=%d\n",
__func__, __LINE__, committed_gen, rep->gen, egen);
__rep_send_gen_vote(dbenv, &lsn, nsites, priority, tiebreaker,
egen, committed_gen, db_eid_broadcast, REP_GEN_VOTE1);
} else {
logmsg(LOGMSG_DEBUG, "%s line %d broadcasting REP_VOTE1 to all (committed-gen=0) gen=%d egen=%d\n",
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d broadcasting REP_VOTE1 to all (committed-gen=0) gen=%d egen=%d\n",
__func__, __LINE__, rep->gen, egen);
__rep_send_vote(dbenv, &lsn, nsites, priority, tiebreaker, egen,
db_eid_broadcast, REP_VOTE1);
Expand All @@ -1387,15 +1396,18 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
#endif
/* This increments our election gen */
__rep_elect_done(dbenv, rep, 0, __func__, __LINE__);
logmsg(LOGMSG_DEBUG, "%s line %d returning %d master %s egen is %d\n",
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d returning %d master %s egen is %d\n",
__func__, __LINE__, ret, *eidp, *newgen);
return (0);
}
logmsg(LOGMSG_DEBUG, "%s line %d going to phase2 because nomaster\n", __func__, __LINE__);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d going to phase2 because nomaster\n", __func__, __LINE__);
goto phase2;
case DB_ELECTION_GENCHG:
case DB_TIMEOUT:
logmsg(LOGMSG_DEBUG, "%s line %d ret is %d break\n", __func__, __LINE__, ret);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d ret is %d break\n", __func__, __LINE__, ret);
break;
default:
goto err;
Expand All @@ -1420,7 +1432,8 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
__db_err(dbenv, "Egen changed from %lu to %lu",
(u_long)egen, (u_long)rep->egen);
#endif
logmsg(LOGMSG_DEBUG, "%s line %d rep egen changed from %d to %d, restarting\n",
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d rep egen changed from %d to %d, restarting\n",
__func__, __LINE__, egen, rep->egen);
goto restart;
}
Expand All @@ -1430,7 +1443,8 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
if (rep->sites > rep->nsites / 2) {

/* We think we've seen enough to cast a vote. */
logmsg(LOGMSG_DEBUG, "%s line %d have seen enough votes for vote2\n", __func__, __LINE__);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d have seen enough votes for vote2\n", __func__, __LINE__);
send_vote = rep->winner;
/*
* See if we won. This will make sure we
Expand All @@ -1446,7 +1460,8 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
"Counted my vote %d", rep->votes);
#endif
}
logmsg(LOGMSG_DEBUG, "%s line %d setting PHASE2 clearing PHASE1\n", __func__, __LINE__);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d setting PHASE2 clearing PHASE1\n", __func__, __LINE__);
F_SET(rep, REP_F_EPHASE2);
F_CLR(rep, REP_F_EPHASE1);
}
Expand All @@ -1460,7 +1475,8 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
"Not enough votes to elect: received %d of %d",
rep->sites, rep->nsites);
#endif
logmsg(LOGMSG_DEBUG, "%s line %d not enough vote1s, failing\n", __func__, __LINE__);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d not enough vote1s, failing\n", __func__, __LINE__);
ret = DB_REP_UNAVAIL;
goto err;

Expand All @@ -1469,21 +1485,24 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
* We have seen enough vote1's. Now we need to wait
* for all the vote2's.
*/
logmsg(LOGMSG_DEBUG, "%s line %d have seen enough votes to cast vote2!\n", __func__, __LINE__);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d have seen enough votes to cast vote2!\n", __func__, __LINE__);
if (send_vote != rep->eid) {
#ifdef DIAGNOSTIC
if (FLD_ISSET(dbenv->verbose, DB_VERB_REPLICATION) &&
send_vote != rep->eid)
__db_err(dbenv, "Sending vote");
#endif
if (use_committed_gen) {
logmsg(LOGMSG_DEBUG, "%s line %d sending REP_GEN_VOTE2 to %s "
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d sending REP_GEN_VOTE2 to %s "
"with committed-gen=%d gen=%d egen=%d\n", __func__, __LINE__,
send_vote, committed_gen, rep->gen, egen);
__rep_send_gen_vote(dbenv, NULL, 0, 0, 0, egen,
committed_gen, send_vote, REP_GEN_VOTE2);
} else {
logmsg(LOGMSG_DEBUG, "%s line %d sending REP_VOTE2 to %s "
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d sending REP_VOTE2 to %s "
"(committed-gen=0) gen=%d egen=%d\n", __func__, __LINE__,
send_vote, rep->gen, egen);
__rep_send_vote(dbenv, NULL, 0, 0, 0, egen,
Expand All @@ -1502,7 +1521,8 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
case 0:
/* Increment our election gen */
__rep_elect_done(dbenv, rep, 0, __func__, __LINE__);
logmsg(LOGMSG_DEBUG, "%s line %d returning %d master %s egen is %d\n",
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d returning %d master %s egen is %d\n",
__func__, __LINE__, ret, *eidp, *newgen);
return (0);
case DB_TIMEOUT:
Expand All @@ -1522,7 +1542,8 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
if (send_vote == rep->eid && done) {
if (nsites == 1)
__rep_elect_master(dbenv, rep, eidp);
logmsg(LOGMSG_DEBUG, "%s line %d elected master %s current-egen "
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d elected master %s current-egen "
"%d\n", __func__, __LINE__, rep->eid, rep->egen);
ret = 0;
goto lockdone;
Expand All @@ -1540,7 +1561,8 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)
* from elect_init where we were unable to grow_sites. In
* that case we do not want to discard all known election info.
*/
logmsg(LOGMSG_DEBUG, "%s line %d ret is %d\n", __func__, __LINE__, ret);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d ret is %d\n", __func__, __LINE__, ret);
assert(ret == 0 || ret == DB_REP_UNAVAIL);
if (ret == 0 || ret == DB_REP_UNAVAIL) {
__rep_elect_done(dbenv, rep, 0, __func__, __LINE__);
Expand All @@ -1550,7 +1572,8 @@ __rep_elect(dbenv, nsites, priority, timeout, newgen, already_master, eidp)

Pthread_mutex_unlock(&rep_candidate_lock);
MUTEX_UNLOCK(dbenv, db_rep->rep_mutexp);
logmsg(LOGMSG_DEBUG, "%s line %d returning %d master %s egen is %d\n",
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d returning %d master %s egen is %d\n",
__func__, __LINE__, ret, *eidp, *newgen);
return (ret);
}
Expand Down Expand Up @@ -1582,7 +1605,8 @@ __rep_elect_init(dbenv, lsnp, nsites, priority, beginp, otally)

/* If we are already a master; simply broadcast that fact and return. */
if (F_ISSET(rep, REP_F_MASTER)) {
logmsg(LOGMSG_DEBUG, "%s line %d sending REP_NEWMASTER\n",
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d sending REP_NEWMASTER\n",
__func__, __LINE__);
(void)__rep_send_message(dbenv,
db_eid_broadcast, REP_NEWMASTER, lsnp, NULL, 0, NULL);
Expand All @@ -1607,7 +1631,8 @@ __rep_elect_init(dbenv, lsnp, nsites, priority, beginp, otally)
DB_ENV_TEST_RECOVERY(dbenv, DB_TEST_ELECTINIT, ret, NULL);
rep->nsites = nsites;
rep->priority = priority;
logmsg(LOGMSG_DEBUG, "%s line %d setting master_id to %s\n", __func__, __LINE__, db_eid_invalid);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d setting master_id to %s\n", __func__, __LINE__, db_eid_invalid);
rep->master_id = db_eid_invalid;
}
DB_TEST_RECOVERY_LABEL
Expand All @@ -1628,7 +1653,8 @@ __rep_elect_master(dbenv, rep, eidp)
REP *rep;
char **eidp;
{
logmsg(LOGMSG_DEBUG, "%s line %d setting master_id to %s\n", __func__, __LINE__, rep->eid);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_DEBUG,
"%s line %d setting master_id to %s\n", __func__, __LINE__, rep->eid);
rep->master_id = rep->eid;
F_SET(rep, REP_F_MASTERELECT);
if (eidp != NULL)
Expand Down Expand Up @@ -1688,7 +1714,8 @@ __rep_wait(dbenv, timeout, eidp, outegen, inegen, flags)
Pthread_mutex_lock(&gbl_rep_egen_lk);
rc = pthread_cond_timedwait(&gbl_rep_egen_cd, &gbl_rep_egen_lk, &tm);
if (rc && rc != ETIMEDOUT)
logmsg(LOGMSG_ERROR, "Err rc=%d from pthread_cond_timedwait\n", rc);
logmsg(gbl_debug_election ? LOGMSG_USER : LOGMSG_ERROR,
"Err rc=%d from pthread_cond_timedwait\n", rc);

*outegen = rep->egen;
Pthread_mutex_unlock(&gbl_rep_egen_lk);
Expand Down Expand Up @@ -1744,7 +1771,7 @@ __rep_flush(dbenv)

/* treat the end of the log as perm */
(void)__rep_send_message(dbenv,
db_eid_broadcast, REP_LOG, &lsn, &rec, DB_LOG_PERM, NULL);
db_eid_broadcast, REP_LOG, &lsn, &rec, DB_LOG_PERM, NULL);

err: if ((t_ret = __log_c_close(logc)) != 0 && ret == 0)
ret = t_ret;
Expand Down Expand Up @@ -1822,13 +1849,13 @@ extern pthread_mutex_t gbl_durable_lsn_lk;

static int
__rep_deadlocks(dbenv, deadlocks)
DB_ENV *dbenv;
u_int64_t *deadlocks;
DB_ENV *dbenv;
u_int64_t *deadlocks;
{
DB_REP *db_rep = dbenv->rep_handle;
REP *rep = db_rep->region;
*deadlocks = ATOMIC_LOAD64(rep->stat.retry);
return 0;
*deadlocks = ATOMIC_LOAD64(rep->stat.retry);
return 0;
}

/*
Expand Down Expand Up @@ -1859,7 +1886,7 @@ __rep_stat(dbenv, statp, flags)

*statp = NULL;
if ((ret = __db_fchk(dbenv,
"DB_ENV->rep_stat", flags, DB_STAT_CLEAR)) != 0)
"DB_ENV->rep_stat", flags, DB_STAT_CLEAR)) != 0)
return (ret);

/* Allocate a stat struct to return to the user. */
Expand Down Expand Up @@ -1917,7 +1944,7 @@ __rep_stat(dbenv, statp, flags)
queued = rep->stat.st_log_queued;
memset(&rep->stat, 0, sizeof(rep->stat));
rep->stat.st_log_queued = rep->stat.st_log_queued_total =
rep->stat.st_log_queued_max = queued;
rep->stat.st_log_queued_max = queued;
}

if (dolock) {
Expand Down
Loading

0 comments on commit cdeef87

Please sign in to comment.