Browse Source

ckdb - wrap most PQ functions and simplify

master
kanoi 8 years ago
parent
commit
ac6aff7918
  1. 68
      src/ckdb.c
  2. 28
      src/ckdb.h
  3. 91
      src/ckdb_cmd.c
  4. 6
      src/ckdb_data.c
  5. 1051
      src/ckdb_dbio.c

68
src/ckdb.c

@ -1716,9 +1716,11 @@ PGconn *dbconnect()
*/
static bool getdata1()
{
PGconn *conn = dbconnect();
PGconn *conn = NULL;
bool ok = true;
CKPQConn(&conn);
if (!(ok = check_db_version(conn)))
goto matane;
if (!(ok = optioncontrol_fill(conn)))
@ -1731,7 +1733,7 @@ static bool getdata1()
matane:
PQfinish(conn);
CKPQFinish(&conn);
return ok;
}
@ -1740,19 +1742,25 @@ matane:
*/
static bool getdata2()
{
PGconn *conn = dbconnect();
bool ok = blocks_fill(conn);
PGconn *conn = NULL;
bool ok;
CKPQConn(&conn);
ok = blocks_fill(conn);
PQfinish(conn);
CKPQFinish(&conn);
return ok;
}
static bool getdata3()
{
PGconn *conn = dbconnect();
PGconn *conn = NULL;
bool ok = true;
CKPQConn(&conn);
if (!key_update && !confirm_sharesummary) {
if (!(ok = paymentaddresses_fill(conn)) || everyone_die)
goto sukamudai;
@ -1762,12 +1770,12 @@ static bool getdata3()
if (!(ok = miningpayouts_fill(conn)) || everyone_die)
goto sukamudai;
}
PQfinish(conn);
conn = dbconnect();
CKPQFinish(&conn);
CKPQConn(&conn);
if (!(ok = workinfo_fill(conn)) || everyone_die)
goto sukamudai;
PQfinish(conn);
conn = dbconnect();
CKPQFinish(&conn);
CKPQConn(&conn);
if (!(ok = marks_fill(conn)) || everyone_die)
goto sukamudai;
/* must be after workinfo */
@ -1778,14 +1786,14 @@ static bool getdata3()
if (!(ok = payouts_fill(conn)) || everyone_die)
goto sukamudai;
}
PQfinish(conn);
conn = dbconnect();
CKPQFinish(&conn);
CKPQConn(&conn);
if (!key_update) {
if (!(ok = markersummary_fill(conn)) || everyone_die)
goto sukamudai;
}
PQfinish(conn);
conn = dbconnect();
CKPQFinish(&conn);
CKPQConn(&conn);
if (!key_update) {
if (!(ok = shares_fill(conn)) || everyone_die)
goto sukamudai;
@ -1795,7 +1803,7 @@ static bool getdata3()
sukamudai:
PQfinish(conn);
CKPQFinish(&conn);
return ok;
}
@ -6153,7 +6161,7 @@ static void *listener_all(void *arg)
else
clistener_using_data = true;
conn = dbconnect();
CKPQConn(&conn);
now = time(NULL);
while (!everyone_die) {
@ -6250,8 +6258,8 @@ static void *listener_all(void *arg)
// Don't keep a connection for more than ~10s
if ((time(NULL) - now) > 10) {
PQfinish(conn);
conn = dbconnect();
CKPQFinish(&conn);
CKPQConn(&conn);
now = time(NULL);
}
@ -6294,9 +6302,7 @@ static void *listener_all(void *arg)
}
}
}
if (conn)
PQfinish(conn);
CKPQFinish(&conn);
if (mythread != 0)
LOGNOTICE("%s() %s exiting", __func__, buf);
@ -7202,7 +7208,7 @@ static void *process_reload(__maybe_unused void *arg)
when_add.tv_sec = RELOAD_QUEUE_SLEEP_MS / 1000;
when_add.tv_nsec = (RELOAD_QUEUE_SLEEP_MS % 1000) * 1000000;
conn = dbconnect();
CKPQConn(&conn);
now = time(NULL);
while (!everyone_die) {
@ -7306,8 +7312,8 @@ static void *process_reload(__maybe_unused void *arg)
// Don't keep a connection for more than ~10s ... of processing
if ((time(NULL) - now) > 10) {
PQfinish(conn);
conn = dbconnect();
CKPQFinish(&conn);
CKPQConn(&conn);
now = time(NULL);
}
@ -7322,9 +7328,7 @@ static void *process_reload(__maybe_unused void *arg)
tick();
}
if (conn)
PQfinish(conn);
CKPQFinish(&conn);
if (mythread == 0) {
for (i = 1; i < THREAD_LIMIT; i++) {
@ -7859,7 +7863,7 @@ static void *pqproc(void *arg)
when_add.tv_nsec = (CMD_QUEUE_SLEEP_MS % 1000) * 1000000;
now = time(NULL);
conn = dbconnect();
CKPQConn(&conn);
wqgot = 0;
// Override checking until pool0 is complete
@ -7972,8 +7976,8 @@ static void *pqproc(void *arg)
/* Don't keep a connection for more than ~10s or ~10000 items
* but always have a connection open */
if ((time(NULL) - now) > 10 || wqgot > 10000) {
PQfinish(conn);
conn = dbconnect();
CKPQFinish(&conn);
CKPQConn(&conn);
now = time(NULL);
wqgot = 0;
}
@ -8043,9 +8047,7 @@ static void *pqproc(void *arg)
mutex_unlock(&wq_pool_waitlock);
}
}
if (conn)
PQfinish(conn);
CKPQFinish(&conn);
if (mythread == 0) {
for (i = 1; i < THREAD_LIMIT; i++) {

28
src/ckdb.h

@ -58,7 +58,7 @@
#define DB_VLOCK "1"
#define DB_VERSION "1.0.7"
#define CKDB_VERSION DB_VERSION"-2.518"
#define CKDB_VERSION DB_VERSION"-2.600"
#define WHERE_FFL " - from %s %s() line %d"
#define WHERE_FFL_HERE __FILE__, __func__, __LINE__
@ -3614,12 +3614,12 @@ extern void userinfo_block(BLOCKS *blocks, enum info_type isnew, int delta);
#define CKPQ_READ true
#define CKPQ_WRITE false
#define CKPQexec(_conn, _qry, _isread) _CKPQexec(_conn, _qry, _isread, WHERE_FFL_HERE)
extern PGresult *_CKPQexec(PGconn *conn, const char *qry, bool isread, WHERE_FFL_ARGS);
#define CKPQexecParams(_conn, _qry, _p1, _p2, _p3, _p4, _p5, _p6, _isread) \
_CKPQexecParams(_conn, _qry, _p1, _p2, _p3, _p4, _p5, _p6, \
#define CKPQExec(_conn, _qry, _isread) _CKPQExec(_conn, _qry, _isread, WHERE_FFL_HERE)
extern PGresult *_CKPQExec(PGconn *conn, const char *qry, bool isread, WHERE_FFL_ARGS);
#define CKPQExecParams(_conn, _qry, _p1, _p2, _p3, _p4, _p5, _p6, _isread) \
_CKPQExecParams(_conn, _qry, _p1, _p2, _p3, _p4, _p5, _p6, \
_isread, WHERE_FFL_HERE)
extern PGresult *_CKPQexecParams(PGconn *conn, const char *qry,
extern PGresult *_CKPQExecParams(PGconn *conn, const char *qry,
int nParams,
const Oid *paramTypes,
const char *const * paramValues,
@ -3627,10 +3627,10 @@ extern PGresult *_CKPQexecParams(PGconn *conn, const char *qry,
const int *paramFormats,
int resultFormat,
bool isread, WHERE_FFL_ARGS);
// Force use CKPQ... for PQ functions in use
#define PQexec CKPQexec
#define PQexecParams CKPQexecParams
extern ExecStatusType _CKPQResultStatus(PGresult *res, WHERE_FFL_ARGS);
#define CKPQResultStatus(_res) _CKPQResultStatus(_res, WHERE_FFL_HERE)
extern void _CKPQClear(PGresult *res, WHERE_FFL_ARGS);
#define CKPQClear(_res) _CKPQClear(_res, WHERE_FFL_HERE)
#define PGLOG(__LOG, __str, __rescode, __conn) do { \
char *__buf = pqerrmsg(__conn); \
@ -3644,12 +3644,16 @@ extern PGresult *_CKPQexecParams(PGconn *conn, const char *qry,
#define PGLOGNOTICE(_str, _rescode, _conn) PGLOG(LOGNOTICE, _str, _rescode, _conn)
extern char *pqerrmsg(PGconn *conn);
extern bool CKPQConn(PGconn **conn);
extern void CKPQDisco(PGconn **conn, bool conned);
extern bool _CKPQConn(PGconn **conn, WHERE_FFL_ARGS);
#define CKPQConn(_conn) _CKPQConn(_conn, WHERE_FFL_HERE)
extern bool _CKPQDisco(PGconn **conn, bool conned, WHERE_FFL_ARGS);
#define CKPQDisco(_conn, _conned) _CKPQDisco(_conn, _conned, WHERE_FFL_HERE)
#define CKPQFinish(_conn) CKPQDisco(_conn, true)
extern bool _CKPQBegin(PGconn *conn, WHERE_FFL_ARGS);
#define CKPQBegin(_conn) _CKPQBegin(conn, WHERE_FFL_HERE)
extern void _CKPQEnd(PGconn *conn, bool commit, WHERE_FFL_ARGS);
#define CKPQEnd(_conn, _commit) _CKPQEnd(_conn, _commit, WHERE_FFL_HERE)
#define CKPQCommit(_conn) _CKPQEnd(_conn, true, WHERE_FFL_HERE)
extern int64_t nextid(PGconn *conn, char *idname, int64_t increment,
tv_t *cd, char *by, char *code, char *inet);

91
src/ckdb_cmd.c

@ -3872,8 +3872,6 @@ static char *cmd_setatts(PGconn *conn, char *cmd, char *id,
__maybe_unused tv_t *notcd, K_TREE *trf_root,
__maybe_unused bool reload_data)
{
ExecStatusType rescode;
PGresult *res;
bool conned = false;
K_ITEM *t_item, *u_item, *ua_item = NULL;
INTRANSIENT *in_username;
@ -3921,21 +3919,13 @@ static char *cmd_setatts(PGconn *conn, char *cmd, char *id,
*(dot++) = '\0';
// If we already had a different one, save it to the DB
if (ua_item && strcmp(useratts->attname, attname) != 0) {
if (conn == NULL) {
conn = dbconnect();
conned = true;
}
conned = CKPQConn(&conn);
if (!begun) {
// Beginning of a write txn
res = PQexec(conn, "Begin", CKPQ_WRITE);
rescode = PQresultStatus(res);
PQclear(res);
if (!PGOK(rescode)) {
PGLOGERR("Begin", rescode, conn);
begun = CKPQBegin(conn);
if (!begun) {
reason = "DBERR";
goto bats;
}
begun = true;
}
if (useratts_item_add(conn, ua_item, now, begun)) {
ua_item = NULL;
@ -3983,21 +3973,13 @@ static char *cmd_setatts(PGconn *conn, char *cmd, char *id,
t_item = next_in_ktree(ctx);
}
if (ua_item) {
if (conn == NULL) {
conn = dbconnect();
conned = true;
}
conned = CKPQConn(&conn);
if (!begun) {
// Beginning of a write txn
res = PQexec(conn, "Begin", CKPQ_WRITE);
rescode = PQresultStatus(res);
PQclear(res);
if (!PGOK(rescode)) {
PGLOGERR("Begin", rescode, conn);
begun = CKPQBegin(conn);
if (!begun) {
reason = "DBERR";
goto bats;
}
begun = true;
}
if (!useratts_item_add(conn, ua_item, now, begun)) {
reason = "DBERR";
@ -4007,15 +3989,11 @@ static char *cmd_setatts(PGconn *conn, char *cmd, char *id,
}
}
rollback:
if (!reason)
res = PQexec(conn, "Commit", CKPQ_WRITE);
else
res = PQexec(conn, "Rollback", CKPQ_WRITE);
PQclear(res);
CKPQEnd(conn, (reason == NULL));
bats:
if (conned)
PQfinish(conn);
conned = CKPQDisco(&conn, conned);
if (reason) {
if (ua_item) {
K_WLOCK(useratts_free);
@ -4207,8 +4185,6 @@ static char *cmd_setopts(PGconn *conn, char *cmd, char *id,
__maybe_unused tv_t *notcd, K_TREE *trf_root,
__maybe_unused bool reload_data)
{
ExecStatusType rescode;
PGresult *res;
bool conned = false;
K_ITEM *t_item, *oc_item = NULL, *ok = NULL;
K_TREE_CTX ctx[1];
@ -4242,21 +4218,13 @@ static char *cmd_setopts(PGconn *conn, char *cmd, char *id,
reason = "Missing value";
goto rollback;
}
if (conn == NULL) {
conn = dbconnect();
conned = true;
}
conned = CKPQConn(&conn);
if (!begun) {
// Beginning of a write txn
res = PQexec(conn, "Begin", CKPQ_WRITE);
rescode = PQresultStatus(res);
PQclear(res);
if (!PGOK(rescode)) {
PGLOGERR("Begin", rescode, conn);
begun = CKPQBegin(conn);
if (!begun) {
reason = "DBERR";
goto rollback;
}
begun = true;
}
ok = optioncontrol_item_add(conn, oc_item, now, begun);
oc_item = NULL;
@ -4299,21 +4267,13 @@ static char *cmd_setopts(PGconn *conn, char *cmd, char *id,
reason = "Missing value";
goto rollback;
}
if (conn == NULL) {
conn = dbconnect();
conned = true;
}
conned = CKPQConn(&conn);
if (!begun) {
// Beginning of a write txn
res = PQexec(conn, "Begin", CKPQ_WRITE);
rescode = PQresultStatus(res);
PQclear(res);
if (!PGOK(rescode)) {
PGLOGERR("Begin", rescode, conn);
begun = CKPQBegin(conn);
if (!begun) {
reason = "DBERR";
goto rollback;
}
begun = true;
}
ok = optioncontrol_item_add(conn, oc_item, now, begun);
oc_item = NULL;
@ -4325,17 +4285,10 @@ static char *cmd_setopts(PGconn *conn, char *cmd, char *id,
}
}
rollback:
if (begun) {
if (reason)
res = PQexec(conn, "Rollback", CKPQ_WRITE);
else
res = PQexec(conn, "Commit", CKPQ_WRITE);
if (begun)
CKPQEnd(conn, (reason == NULL));
PQclear(res);
}
if (conned)
PQfinish(conn);
conned = CKPQDisco(&conn, conned);
if (reason) {
snprintf(reply, siz, "ERR.%s", reason);
LOGERR("%s.%s.%s", cmd, id, reply);
@ -8386,10 +8339,7 @@ static char *cmd_high(PGconn *conn, char *cmd, char *id,
if (strcasecmp(action, "store") == 0) {
/* Store the shares_hi_root list in the db now,
* rather than wait for a shift process to do it */
if (!conn) {
conn = dbconnect();
conned = true;
}
conned = CKPQConn(&conn);
count = 0;
do {
did = false;
@ -8404,8 +8354,7 @@ static char *cmd_high(PGconn *conn, char *cmd, char *id,
count++;
}
} while (did);
if (conned)
PQfinish(conn);
conned = CKPQDisco(&conn, conned);
if (count) {
LOGWARNING("%s() Stored: %d high shares",
__func__, count);

6
src/ckdb_data.c

@ -6397,7 +6397,7 @@ K_ITEM *_find_markersummary(int64_t markerid, int64_t workinfoid,
bool make_markersummaries(bool msg, char *by, char *code, char *inet,
tv_t *cd, K_TREE *trf_root)
{
PGconn *conn;
PGconn *conn = NULL;
K_TREE_CTX ctx[1];
WORKMARKERS *workmarkers;
K_ITEM *wm_item, *wm_last = NULL, *s_item = NULL;
@ -6427,7 +6427,7 @@ bool make_markersummaries(bool msg, char *by, char *code, char *inet,
return false;
}
conn = dbconnect();
CKPQConn(&conn);
/* Store all shares in the DB before processing the workmarker
* This way we know that the high shares in the DB will match the start
@ -6486,7 +6486,7 @@ bool make_markersummaries(bool msg, char *by, char *code, char *inet,
tvdiff(&proc_lock_fin, &proc_lock_got));
flailed:
PQfinish(conn);
CKPQDisco(&conn, true);
if (count > 0) {
LOGWARNING("%s() Stored: %d high shares %.3fs",

1051
src/ckdb_dbio.c

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save