-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoledb.cpp
162 lines (134 loc) · 5.05 KB
/
Doledb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "Doledb.h"
#include "SpiderCache.h"
#include "SpiderLoop.h"
#include "SpiderColl.h"
#include "ScopedLock.h"
#include "Collectiondb.h"
#include "Conf.h"
#include "Loop.h"
Doledb g_doledb;
static void nukeAllDoledbsPeriodically(int, void *);
/////////////////////////
///////////////////////// DOLEDB
/////////////////////////
// reset rdb
void Doledb::reset() { m_rdb.reset(); }
bool Doledb::init ( ) {
if(g_conf.m_doledbNukeInterval>0) {
log(LOG_INFO,"spider: nuking Doledb periodically is enabled, interval = %d seconds", g_conf.m_doledbNukeInterval);
g_loop.registerSleepCallback(g_conf.m_doledbNukeInterval*1000, NULL, nukeAllDoledbsPeriodically, "nukeAllDoledbsPeriodically");
} else
log(LOG_INFO,"@spider: nuking Doledb periodically is disabled");
// . what's max # of tree nodes?
// . assume avg spider rec size (url) is about 45
// . 45 + 33 bytes overhead in tree is 78
// . use 5MB for the tree
int32_t maxTreeMem = 150000000; // 150MB
int32_t maxTreeNodes = maxTreeMem / 78;
// initialize our own internal rdb
return m_rdb.init ( "doledb" ,
-1 , // fixedDataSize
2 , // MinFilesToMerge
maxTreeMem ,
maxTreeNodes ,
false, // half keys?
12, // key size
false); //useIndexFile
}
//
// remove all recs from doledb for the given collection
//
static void nukeDoledbWrapper ( int fd , void *state ) {
g_loop.unregisterSleepCallback ( state , nukeDoledbWrapper );
collnum_t collnum = *(collnum_t *)state;
nukeDoledb ( collnum );
}
void nukeDoledb ( collnum_t collnum ) {
// in case we changed url filters for this collection #
g_spiderLoop.nukeWinnerListCache(collnum);
// . nuke doledb for this collnum
// . it will unlink the files and maps for doledb for this collnum
// . it will remove all recs of this collnum from its tree too
if (g_doledb.getRdb()->isSavingTree()) {
g_loop.registerSleepCallback(100, &collnum, nukeDoledbWrapper, "Doledb::nukeDoledbWrapper");
return;
}
// . ok, tree is not saving, it should complete entirely from this call
g_doledb.getRdb()->deleteAllRecs(collnum);
SpiderColl *sc = g_spiderCache.getSpiderCollIffNonNull(collnum);
if ( sc ) {
// . make sure to nuke m_doledbIpTable as well
sc->clearDoledbIpTable();
// need to recompute this!
//sc->m_ufnMapValid = false;
{
ScopedLock sl(sc->m_waitingTree.getLock());
// log it
log("spider: rebuilding %s from doledb nuke", sc->getCollName());
// activate a scan if not already activated
sc->m_waitingTreeNeedsRebuild = true;
// if a scan is ongoing, this will re-set it
sc->resetWaitingTreeNextKey();
// clear it?
sc->m_waitingTree.clear_unlocked();
sc->clearWaitingTable();
}
// kick off the spiderdb scan to rep waiting tree and doledb
sc->populateWaitingTreeFromSpiderdb(false);
}
// note it
log("spider: finished nuking doledb for coll (%" PRId32")",
(int32_t)collnum);
}
static void nukeAllDoledbsWrapper(int /*fd*/, void *state) {
g_loop.unregisterSleepCallback(state, nukeAllDoledbsWrapper);
nukeAllDoledbs();
}
void nukeAllDoledbs() {
log(LOG_INFO,"spider: Beginning nuking all doledbs");
if (g_doledb.getRdb()->isSavingTree()) {
g_loop.registerSleepCallback(100, NULL, nukeAllDoledbsWrapper, "nukeAllDoledbsWrapper");
return;
}
for(collnum_t collnum=g_collectiondb.getFirstCollnum();
collnum<g_collectiondb.getNumRecs();
collnum++)
{
if(g_collectiondb.getRec(collnum)) {
// in case we changed url filters for this collection #
g_spiderLoop.nukeWinnerListCache(collnum);
// . nuke doledb for this collnum
// . it will unlink the files and maps for doledb for this collnum
// . it will remove all recs of this collnum from its tree too
g_doledb.getRdb()->deleteAllRecs(collnum);
SpiderColl *sc = g_spiderCache.getSpiderCollIffNonNull(collnum);
if ( sc ) {
// . make sure to nuke m_doledbIpTable as well
sc->clearDoledbIpTable();
// need to recompute this!
//sc->m_ufnMapValid = false;
{
ScopedLock sl(sc->m_waitingTree.getLock());
// log it
log("spider: rebuilding %s from doledb nuke", sc->getCollName());
// activate a scan if not already activated
sc->m_waitingTreeNeedsRebuild = true;
// if a scan is ongoing, this will re-set it
sc->resetWaitingTreeNextKey();
// clear it?
sc->m_waitingTree.clear_unlocked();
sc->clearWaitingTable();
}
// kick off the spiderdb scan to rep waiting tree and doledb
sc->populateWaitingTreeFromSpiderdb(false);
}
}
}
// note it
log(LOG_INFO,"spider: finished nuking all doledbs");
}
//Nuking doledb+waitingtree shouldn't really be necessary but the code handling spiderdb+doledb+doledbiptable+waitingtree+waitingtable+doledbiptable isn't 100% error-free
//so we sometimes end up with lost records and priority inversions. The easiest solution for this right now is to periodically nuke all doledbs.
static void nukeAllDoledbsPeriodically(int, void *) {
nukeAllDoledbs();
}