forked from MarginaliaSearch/MarginaliaSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPingJobScheduler.java
More file actions
294 lines (242 loc) · 11.1 KB
/
Copy pathPingJobScheduler.java
File metadata and controls
294 lines (242 loc) · 11.1 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package nu.marginalia.ping;
import com.google.inject.Inject;
import nu.marginalia.coordination.DomainCoordinator;
import nu.marginalia.coordination.DomainLock;
import nu.marginalia.model.EdgeDomain;
import nu.marginalia.ping.fetcher.PingDnsFetcher;
import nu.marginalia.ping.model.*;
import nu.marginalia.ping.svc.DnsPingService;
import nu.marginalia.ping.svc.HttpPingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
/** PingJobScheduler is responsible for scheduling and processing ping jobs
* for both HTTP pings and DNS lookups. It manages a queue of jobs and processes them
* in separate threads, ensuring that domains are pinged and DNS records are updated
* efficiently.
*/
public class PingJobScheduler {
private final HttpPingService httpPingService;
private final DnsPingService dnsPingService;
private final DomainCoordinator domainCoordinator;
private final PingDnsFetcher dnsFetcher;
private final PingDao pingDao;
private static final Logger logger = LoggerFactory.getLogger(PingJobScheduler.class);
private static final UpdateSchedule<RootDomainReference, RootDomainReference> dnsUpdateSchedule
= new UpdateSchedule<>(250_000);
private static final UpdateSchedule<DomainReference, HistoricalAvailabilityData> availabilityUpdateSchedule
= new UpdateSchedule<>(250_000);
public volatile Instant dnsLastSync = Instant.now();
public volatile Instant availabilityLastSync = Instant.now();
public volatile boolean ready = false;
public volatile boolean running = false;
private final List<Thread> allThreads = new ArrayList<>();
@Inject
public PingJobScheduler(HttpPingService httpPingService,
DnsPingService dnsPingService,
DomainCoordinator domainCoordinator,
PingDnsFetcher dnsFetcher,
PingDao pingDao)
{
this.httpPingService = httpPingService;
this.dnsPingService = dnsPingService;
this.domainCoordinator = domainCoordinator;
this.dnsFetcher = dnsFetcher;
this.pingDao = pingDao;
}
public void run(Instant endTs) {
running = true;
availabilityUpdateSchedule.replaceQueue(pingDao.getDomainUpdateSchedule(Integer.MAX_VALUE));
dnsUpdateSchedule.replaceQueue(pingDao.getDnsUpdateSchedule(50_000));
dnsLastSync = Instant.now();
availabilityLastSync = Instant.now();
int availabilityThreads = Integer.getInteger("ping.availabilityThreads", 8);
int pingThreads = Integer.getInteger("ping.dnsThreads", 2);
for (int i = 0; i < availabilityThreads; i++) {
allThreads.add(Thread.ofPlatform().daemon().name("availability-job-consumer-" + i).start(this::availabilityJobConsumer));
}
for (int i = 0; i < pingThreads; i++) {
allThreads.add(Thread.ofPlatform().daemon().name("dns-job-consumer-" + i).start(this::dnsJobConsumer));
}
while (Instant.now().isBefore(endTs)) {
if (!availabilityUpdateSchedule.hasAvailableJobs()) {
logger.info("Refilling domain jobs");
availabilityUpdateSchedule.replaceQueue(pingDao.getDomainUpdateSchedule(Integer.MAX_VALUE));
}
if (!dnsUpdateSchedule.hasAvailableJobs()) {
logger.info("Refilling DNS jobs");
dnsUpdateSchedule.replaceQueue(pingDao.getDnsUpdateSchedule(50_000));
}
try {
Thread.sleep(Duration.ofMinutes(1));
}
catch (InterruptedException ex) {
break;
}
}
stop();
}
public void stop() {
running = false;
for (Thread thread : allThreads) {
try {
thread.join(Duration.ofSeconds(30));
thread.interrupt();
thread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.error("Failed to join thread: " + thread.getName(), e);
}
}
dnsFetcher.shutDown();
synchronized (this) {
notifyAll();
}
}
private void availabilityJobConsumer() {
while (running && !Thread.interrupted()) {
try {
DomainReference ref = availabilityUpdateSchedule.nextIf(domain -> {
EdgeDomain domainObj = new EdgeDomain(domain.domainName());
if (!domainCoordinator.isLockableHint(domainObj)) {
return false; // Skip locked domains
}
return true; // Process this domain
});
if (ref == null)
continue;
var maybeLock = domainCoordinator.tryLockDomain(ref.asEdgeDomain());
// If we've failed the TOCTOU race, we'll reschedule this for later
if (maybeLock.isEmpty()) {
rescheduleLockedDomain(ref);
continue;
}
DomainLock lock = maybeLock.get();
List<WritableModel> objects;
try (lock) {
long nextId = ref.domainId();
HistoricalAvailabilityData data = pingDao.getHistoricalAvailabilityData(nextId);
if (data == null) {
logger.warn("No availability data found for ID: {}", nextId);
continue; // No data to process, skip this iteration
}
@Nullable
DomainReference reference = null;
@Nullable
DomainAvailabilityRecord availabilityRecord = null;
@Nullable
DomainSecurityRecord domainSecurityRecord = null;
switch (data) {
case HistoricalAvailabilityData.JustDomainReference(DomainReference justRef) ->
{
reference = justRef;
}
case HistoricalAvailabilityData.JustAvailability(String domain,
DomainAvailabilityRecord availability) ->
{
reference = new DomainReference(availability.domainId(), availability.nodeId(), domain);
availabilityRecord = availability;
}
case HistoricalAvailabilityData.AvailabilityAndSecurity(String domain,
DomainAvailabilityRecord availability,
DomainSecurityRecord security) ->
{
reference = new DomainReference(availability.domainId(), availability.nodeId(), domain);
availabilityRecord = availability;
domainSecurityRecord = security;
}
};
if (reference == null)
continue;
objects = httpPingService.pingDomain(reference, availabilityRecord, domainSecurityRecord);
}
pingDao.write(objects);
// Re-schedule the next update time for the domain
for (WritableModel object : objects) {
var ts = object.nextUpdateTime();
if (ts != null) {
availabilityUpdateSchedule.add(ref, ts);
break;
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.error("Availability job consumer interrupted", e);
break;
} catch (Exception e) {
logger.error("Error processing availability job", e);
}
}
}
// We expect a pareto distribution in number of subdomains, so
// this map will stay pretty small.
private ConcurrentHashMap<String, Instant> lastDomainReschedule = new ConcurrentHashMap<>();
private void rescheduleLockedDomain(DomainReference ref) {
String topDomain = ref.asEdgeDomain().topDomain;
Instant newScheduledTime = lastDomainReschedule.compute(topDomain,
(k,v) -> {
Instant now = Instant.now();
if (v == null || v.isBefore(now)) {
return now.plus(Duration.ofSeconds(10));
} else {
return v.plus(Duration.ofSeconds(5));
}
});
availabilityUpdateSchedule.add(ref, newScheduledTime);
}
private void dnsJobConsumer() {
while (running && !Thread.interrupted()) {
try {
RootDomainReference ref = dnsUpdateSchedule.next();
if (ref == null) {
continue;
}
try {
@Nullable
String domainName = null;
@Nullable
DomainDnsRecord oldRecord = null;
switch(ref) {
case RootDomainReference.ByIdAndName(long id, String name) -> {
domainName = oldRecord.rootDomainName();
oldRecord = Objects.requireNonNull(pingDao.getDomainDnsRecord(id));
}
case RootDomainReference.ByName(String name) -> {
domainName = name;
oldRecord = pingDao.getDomainDnsRecord(name);
}
};
if (domainName == null)
continue;
List<WritableModel> objects = dnsPingService.pingDomain(domainName, oldRecord);
pingDao.write(objects);
// Re-schedule the next update time for the domain
for (WritableModel object : objects) {
var ts = object.nextUpdateTime();
if (ts != null) {
dnsUpdateSchedule.add(ref, ts);
break;
}
}
}
catch (Exception e) {
logger.error("Error processing DNS job for domain: " + ref, e);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.error("DNS job consumer interrupted", e);
break;
} catch (Exception e) {
logger.error("Error processing DNS job", e);
}
}
}
}