Skip to content

Commit b7a489b

Browse files
committed
Factor code handling backends in Ueberbackend::handle.
This allows making the current backend pointer private and makes the behaviour more explicit: - all backends will be queried in sequence. - as soon as one backend answers the given (qname, qtype) query, none of the remaining backends will be queried until the next UeberBackend::lookup call (which starts a fresh new query). Signed-off-by: Miod Vallat <[email protected]>
1 parent 8e3c138 commit b7a489b

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

pdns/ueberbackend.cc

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,8 @@ void UeberBackend::lookup(const QType& qtype, const DNSName& qname, domainid_t z
806806

807807
d_qtype = qtype.getCode();
808808

809-
d_handle.i = 0;
809+
d_handle.parent = this;
810+
d_handle.backendIndex = 0;
810811
d_handle.qtype = s_doANYLookupsOnly ? QType::ANY : qtype;
811812
d_handle.qname = qname;
812813
d_handle.zoneId = zoneId;
@@ -827,8 +828,7 @@ void UeberBackend::lookup(const QType& qtype, const DNSName& qname, domainid_t z
827828
// cout<<"UeberBackend::lookup("<<qname<<"|"<<DNSRecordContent::NumberToType(qtype.getCode())<<"): uncached"<<endl;
828829
d_negcached = d_cached = false;
829830
d_answers.clear();
830-
(d_handle.d_hinterBackend = backends[d_handle.i++].get())->lookup(d_handle.qtype, d_handle.qname, d_handle.zoneId, d_handle.pkt_p);
831-
++(*s_backendQueries);
831+
d_handle.selectNextBackend();
832832
}
833833
else if (cacheResult == CacheResult::NegativeMatch) {
834834
// cout<<"UeberBackend::lookup("<<qname<<"|"<<DNSRecordContent::NumberToType(qtype.getCode())<<"): NEGcached"<<endl;
@@ -842,8 +842,6 @@ void UeberBackend::lookup(const QType& qtype, const DNSName& qname, domainid_t z
842842
d_cached = true;
843843
d_cachehandleiter = d_answers.begin();
844844
}
845-
846-
d_handle.parent = this;
847845
}
848846

849847
void UeberBackend::getAllDomains(vector<DomainInfo>* domains, bool getSerial, bool include_disabled)
@@ -1078,33 +1076,41 @@ UeberBackend::handle::~handle()
10781076
--instances;
10791077
}
10801078

1079+
// Set d_hinterBackend to the next available backend from the parents list,
1080+
// reset it to nullptr if the whole list has been processed.
1081+
// Invokes lookup on behalf of the newly picked backend if successful.
1082+
void UeberBackend::handle::selectNextBackend()
1083+
{
1084+
if (backendIndex < parent->backends.size()) {
1085+
d_hinterBackend = parent->backends[backendIndex++].get();
1086+
d_hinterBackend->lookup(qtype, qname, zoneId, pkt_p);
1087+
++(*s_backendQueries);
1088+
}
1089+
else {
1090+
d_hinterBackend = nullptr;
1091+
}
1092+
}
1093+
10811094
bool UeberBackend::handle::get(DNSZoneRecord& record)
10821095
{
10831096
DLOG(g_log << "Ueber get() was called for a " << qtype << " record" << endl);
10841097
bool isMore = false;
10851098
while (d_hinterBackend != nullptr && !(isMore = d_hinterBackend->get(record))) { // this backend out of answers
1086-
if (i < parent->backends.size()) {
1087-
DLOG(g_log << "Backend #" << i << " of " << parent->backends.size()
1088-
<< " out of answers, taking next" << endl);
1089-
1090-
d_hinterBackend = parent->backends[i++].get();
1091-
d_hinterBackend->lookup(qtype, qname, zoneId, pkt_p);
1092-
++(*s_backendQueries);
1099+
DLOG(g_log << "Backend #" << backendIndex << " of " << parent->backends.size()
1100+
<< " out of answers, trying next" << endl);
1101+
selectNextBackend();
1102+
if (d_hinterBackend != nullptr) {
1103+
DLOG(g_log << "Now asking backend #" << backendIndex << endl);
10931104
}
1094-
else {
1095-
break;
1096-
}
1097-
1098-
DLOG(g_log << "Now asking backend #" << i << endl);
10991105
}
11001106

1101-
if (!isMore && i == parent->backends.size()) {
1107+
if (d_hinterBackend == nullptr) {
11021108
DLOG(g_log << "UeberBackend reached end of backends" << endl);
11031109
return false;
11041110
}
11051111

11061112
DLOG(g_log << "Found an answering backend - will not try another one" << endl);
1107-
i = parent->backends.size(); // don't go on to the next backend
1113+
backendIndex = parent->backends.size(); // don't go on to the next backend
11081114
return true;
11091115
}
11101116

pdns/ueberbackend.hh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,23 @@ public:
7373

7474
//! The UeberBackend class where this handle belongs to
7575
UeberBackend* parent{nullptr};
76-
//! The current real backend, which is answering questions
77-
DNSBackend* d_hinterBackend{nullptr};
7876

7977
//! DNSPacket who asked this question
8078
DNSPacket* pkt_p{nullptr};
8179
DNSName qname;
8280

8381
//! Index of the current backend within the backends vector
84-
unsigned int i{0};
82+
size_t backendIndex{0};
8583
QType qtype;
8684
domainid_t zoneId{UnknownDomainID};
8785

86+
void selectNextBackend();
87+
8888
private:
8989
static AtomicCounter instances;
90+
91+
//! The currently selected real backend, which is answering questions
92+
DNSBackend* d_hinterBackend{nullptr};
9093
};
9194

9295
void lookup(const QType& qtype, const DNSName& qname, domainid_t zoneId, DNSPacket* pkt_p = nullptr);

0 commit comments

Comments
 (0)