From a30a75bd3fd03779863600f6b379a4894ef87cbc Mon Sep 17 00:00:00 2001 From: kanoi Date: Fri, 18 Mar 2016 18:51:51 +1100 Subject: [PATCH] ckdb - load only one day of shares - or specify a begin workinfo with -b --- src/ckdb.c | 24 +++++++++++++++++++++++- src/ckdb.h | 5 ++++- src/ckdb_dbio.c | 46 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/ckdb.c b/src/ckdb.c index d8decf1c..75085444 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -436,6 +436,7 @@ K_STORE *shares_hi_store; double diff_percent = DIFF_VAL(DIFF_PERCENT_DEFAULT); double share_min_sdiff = 0; +int64_t shares_begin = -1; // SHAREERRORS shareerrors.id.json={...} K_TREE *shareerrors_root; @@ -1728,6 +1729,9 @@ static bool setup_data() mutex_init(&wq_waitlock); cond_init(&wq_waitcond); + LOGWARNING("%sSequence processing is %s", + ignore_seq ? "ALERT: " : "", + ignore_seq ? "Off" : "On"); LOGWARNING("%sStartup payout generation state is %s", genpayout_auto ? "" : "WARNING: ", genpayout_auto ? "On" : "Off"); @@ -2684,6 +2688,12 @@ static enum cmd_values process_seq(MSGLINE *msgline) if (ignore_seq) return ckdb_cmds[msgline->which_cmds].cmd_val; + /* If non-seqall data was in a CCL reload file, + * it can't be processed by update_seq(), so don't */ + if (msgline->n_seqall == 0 && msgline->n_seqstt == 0 && + msgline->n_seqpid == 0) + return ckdb_cmds[msgline->which_cmds].cmd_val; + dupall = update_seq(SEQ_ALL, msgline->n_seqall, msgline->n_seqstt, msgline->n_seqpid, SEQALL, &(msgline->now), &(msgline->cd), msgline->code, @@ -5827,6 +5837,8 @@ static void check_restore_dir(char *name) static struct option long_options[] = { // script to call when alerts happen { "alert", required_argument, 0, 'a' }, + // workinfo to start shares_fill() default is 1 day + { "shares-begin", required_argument, 0, 'b' }, { "config", required_argument, 0, 'c' }, { "dbname", required_argument, 0, 'd' }, { "minsdiff", required_argument, 0, 'D' }, @@ -5888,7 +5900,7 @@ int main(int argc, char **argv) memset(&ckp, 0, sizeof(ckp)); ckp.loglevel = LOG_NOTICE; - while ((c = getopt_long(argc, argv, "a:c:d:D:ghi:I:kl:mM:n:p:P:r:R:s:S:t:u:U:vw:yY:", long_options, &i)) != -1) { + while ((c = getopt_long(argc, argv, "a:c:d:D:ghi:Ikl:mM:n:p:P:r:R:s:S:t:u:U:vw:yY:", long_options, &i)) != -1) { switch(c) { case 'a': len = strlen(optarg); @@ -5898,6 +5910,16 @@ int main(int argc, char **argv) (int)len, MAX_ALERT_CMD); ckdb_alert_cmd = strdup(optarg); break; + case 'b': + { + int64_t beg = atoll(optarg); + if (beg < 0) { + quit(1, "Invalid shares begin " + "%"PRId64" - must be >= 0", + beg); + } + shares_begin = beg; + } case 'c': ckp.config = strdup(optarg); break; diff --git a/src/ckdb.h b/src/ckdb.h index 52aa3a04..0d831a6b 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -51,7 +51,7 @@ #define DB_VLOCK "1" #define DB_VERSION "1.0.5" -#define CKDB_VERSION DB_VERSION"-1.986" +#define CKDB_VERSION DB_VERSION"-1.987" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -1723,6 +1723,9 @@ extern double diff_percent; * This is set only via the runtime parameter -D or --minsdiff */ extern double share_min_sdiff; +// workinfoid to start loading shares, unset = shares_fill() decides +extern int64_t shares_begin; + // SHAREERRORS shareerrors.id.json={...} typedef struct shareerrors { int64_t workinfoid; diff --git a/src/ckdb_dbio.c b/src/ckdb_dbio.c index c7747561..80117809 100644 --- a/src/ckdb_dbio.c +++ b/src/ckdb_dbio.c @@ -3858,16 +3858,51 @@ bool shares_fill(PGconn *conn) { ExecStatusType rescode; PGresult *res; - K_ITEM *item = NULL; + K_TREE_CTX ctx[1]; + K_ITEM *item = NULL, *wi_item; + WORKINFO *workinfo = NULL; SHARES *row; int n, t, i; char *field; char *sel = NULL; - int fields = 14; + char *params[1]; + int fields = 14, par = 0; bool ok = false; + int64_t workinfoid; + tv_t old; LOGDEBUG("%s(): select", __func__); + if (shares_begin >= 0) + workinfoid = shares_begin; + else { + /* Workinfo is already loaded + * CKDB doesn't currently use shares_db in processing, + * but make sure we have enough to avoid loading duplicates + * 1 day should be more than enough for normal running, + * however, if more than 1 day is needed, + * use -b to set the shares_begin workinfoid */ + setnow(&old); + old.tv_sec -= 60 * 60 * 24; // 1 day + K_RLOCK(workinfo_free); + wi_item = last_in_ktree(workinfo_root, ctx); + while (wi_item) { + DATA_WORKINFO(workinfo, wi_item); + if (!tv_newer(&old, &(workinfo->createdate))) + break; + wi_item = prev_in_ktree(ctx); + } + if (wi_item) + workinfoid = workinfo->workinfoid; + else { + // none old enough, so just load from them all + workinfoid = 0; + } + K_RUNLOCK(workinfo_free); + } + + LOGWARNING("%s(): loading from workinfoid>=%"PRId64, __func__, workinfoid); + printf(TICK_PREFIX"sh 0\r"); fflush(stdout); @@ -3875,7 +3910,10 @@ bool shares_fill(PGconn *conn) "workinfoid,userid,workername,clientid,enonce1,nonce2,nonce," "diff,sdiff,errn,error,secondaryuserid,ntime,minsdiff" HISTORYDATECONTROL - " from shares"; + " from shares where workinfoid>=$1"; + par = 0; + params[par++] = bigint_to_buf(workinfoid, NULL, 0); + PARCHK(par, params); res = PQexec(conn, "Begin", CKPQ_READ); rescode = PQresultStatus(res); @@ -3893,7 +3931,7 @@ bool shares_fill(PGconn *conn) goto flail; } - res = PQexec(conn, sel, CKPQ_READ); + res = PQexecParams(conn, sel, par, NULL, (const char **)params, NULL, NULL, 0, CKPQ_READ); rescode = PQresultStatus(res); PQclear(res); if (!PGOK(rescode)) {