]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/auth-zonecache.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "auth-zonecache.hh"
29 #include "arguments.hh"
30 #include "cachecleaner.hh"
33 AuthZoneCache::AuthZoneCache(size_t mapsCount
) :
36 S
.declare("zone-cache-hit", "Number of zone cache hits");
37 S
.declare("zone-cache-miss", "Number of zone cache misses");
38 S
.declare("zone-cache-size", "Number of entries in the zone cache", StatType::gauge
);
40 d_statnumhit
= S
.getPointer("zone-cache-hit");
41 d_statnummiss
= S
.getPointer("zone-cache-miss");
42 d_statnumentries
= S
.getPointer("zone-cache-size");
45 bool AuthZoneCache::getEntry(const DNSName
& zone
, int& zoneId
)
47 auto& mc
= getMap(zone
);
50 auto map
= mc
.d_map
.read_lock();
51 auto iter
= map
->find(zone
);
52 if (iter
!= map
->end()) {
54 zoneId
= iter
->second
.zoneId
;
67 bool AuthZoneCache::isEnabled() const
69 return d_refreshinterval
> 0;
72 void AuthZoneCache::clear()
74 purgeLockedCollectionsVector(d_maps
);
77 void AuthZoneCache::replace(const vector
<std::tuple
<DNSName
, int>>& zone_indices
)
79 if (!d_refreshinterval
)
82 size_t count
= zone_indices
.size();
83 vector
<cmap_t
> newMaps(d_maps
.size());
86 for (const std::tuple
<DNSName
, int>& tup
: zone_indices
) {
87 const DNSName
& zone
= std::get
<0>(tup
);
89 val
.zoneId
= std::get
<1>(tup
);
90 auto& mc
= newMaps
[getMapIndex(zone
)];
91 auto iter
= mc
.find(zone
);
92 if (iter
!= mc
.end()) {
93 iter
->second
= std::move(val
);
96 mc
.emplace(zone
, val
);
101 // process zone updates done while data collection for replace() was already in progress.
102 auto pending
= d_pending
.lock();
103 assert(pending
->d_replacePending
); // make sure we never forget to call setReplacePending()
104 for (const std::tuple
<DNSName
, int, bool>& tup
: pending
->d_pendingUpdates
) {
105 const DNSName
& zone
= std::get
<0>(tup
);
107 val
.zoneId
= std::get
<1>(tup
);
108 bool insert
= std::get
<2>(tup
);
109 auto& mc
= newMaps
[getMapIndex(zone
)];
110 auto iter
= mc
.find(zone
);
111 if (iter
!= mc
.end()) {
113 iter
->second
= std::move(val
);
121 mc
.emplace(zone
, val
);
126 for (size_t mapIndex
= 0; mapIndex
< d_maps
.size(); mapIndex
++) {
127 auto& mc
= d_maps
[mapIndex
];
128 auto map
= mc
.d_map
.write_lock();
129 *map
= std::move(newMaps
[mapIndex
]);
132 pending
->d_pendingUpdates
.clear();
133 pending
->d_replacePending
= false;
135 d_statnumentries
->store(count
);
139 void AuthZoneCache::add(const DNSName
& zone
, const int zoneId
)
141 if (!d_refreshinterval
)
145 auto pending
= d_pending
.lock();
146 if (pending
->d_replacePending
) {
147 pending
->d_pendingUpdates
.emplace_back(zone
, zoneId
, true);
154 int mapIndex
= getMapIndex(zone
);
156 auto& mc
= d_maps
[mapIndex
];
157 auto map
= mc
.d_map
.write_lock();
158 auto iter
= map
->find(zone
);
159 if (iter
!= map
->end()) {
160 iter
->second
= std::move(val
);
163 map
->emplace(zone
, val
);
164 (*d_statnumentries
)++;
169 void AuthZoneCache::remove(const DNSName
& zone
)
171 if (!d_refreshinterval
)
175 auto pending
= d_pending
.lock();
176 if (pending
->d_replacePending
) {
177 pending
->d_pendingUpdates
.emplace_back(zone
, -1, false);
181 int mapIndex
= getMapIndex(zone
);
183 auto& mc
= d_maps
[mapIndex
];
184 auto map
= mc
.d_map
.write_lock();
185 if (map
->erase(zone
)) {
186 (*d_statnumentries
)--;
191 void AuthZoneCache::setReplacePending()
193 if (!d_refreshinterval
)
197 auto pending
= d_pending
.lock();
198 pending
->d_replacePending
= true;
199 pending
->d_pendingUpdates
.clear();