• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanopub-registry / 23548564094

25 Mar 2026 03:15PM UTC coverage: 31.259% (-0.4%) from 31.704%
23548564094

Pull #90

github

web-flow
Merge 566d7831d into 1a3640ad0
Pull Request #90: perf: parallel stream loading, batched task scheduling, skip idle peer discovery

206 of 740 branches covered (27.84%)

Branch coverage included in aggregate %.

693 of 2136 relevant lines covered (32.44%)

5.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.33
src/main/java/com/knowledgepixels/registry/RegistryPeerConnector.java
1
package com.knowledgepixels.registry;
2

3
import com.mongodb.ErrorCategory;
4
import com.mongodb.MongoWriteException;
5
import com.mongodb.client.ClientSession;
6
import com.mongodb.client.MongoCursor;
7
import org.apache.http.HttpResponse;
8
import org.apache.http.client.methods.HttpGet;
9
import org.apache.http.client.methods.HttpHead;
10
import org.apache.http.util.EntityUtils;
11
import org.bson.Document;
12
import org.nanopub.Nanopub;
13
import org.nanopub.NanopubUtils;
14
import org.nanopub.jelly.NanopubStream;
15
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
17

18
import java.io.IOException;
19
import java.io.InputStream;
20
import java.util.ArrayList;
21
import java.util.Collections;
22
import java.util.List;
23
import java.util.concurrent.atomic.AtomicLong;
24

25
import static com.knowledgepixels.registry.RegistryDB.*;
26

27
/**
28
 * Checks peer Nanopub Registries for new nanopublications and loads them.
29
 */
30
public class RegistryPeerConnector {
31

32
    private RegistryPeerConnector() {}
33

34
    private static final Logger log = LoggerFactory.getLogger(RegistryPeerConnector.class);
12✔
35

36
    public static void checkPeers(ClientSession s) {
37
        List<String> peerUrls = new ArrayList<>(Utils.getPeerUrls());
×
38
        Collections.shuffle(peerUrls);
×
39

40
        for (String peerUrl : peerUrls) {
×
41
            try {
42
                checkPeer(s, peerUrl);
×
43
            } catch (Exception ex) {
×
44
                log.info("Error checking peer {}: {}", peerUrl, ex.getMessage());
×
45
            }
×
46
        }
×
47
    }
×
48

49
    static void checkPeer(ClientSession s, String peerUrl) throws IOException {
50
        log.info("Checking peer: {}", peerUrl);
×
51

52
        HttpResponse resp = NanopubUtils.getHttpClient().execute(new HttpHead(peerUrl));
×
53
        int httpStatus = resp.getStatusLine().getStatusCode();
×
54
        EntityUtils.consumeQuietly(resp.getEntity());
×
55
        if (httpStatus < 200 || httpStatus >= 300) {
×
56
            log.info("Failed to reach peer {}: {}", peerUrl, httpStatus);
×
57
            return;
×
58
        }
59

60
        if (isTestInstance(resp)) {
×
61
            log.info("Skipping peer {} because it is a test instance", peerUrl);
×
62
            return;
×
63
        }
64

65
        String status = getHeader(resp, "Nanopub-Registry-Status");
×
66
        if (!"ready".equals(status) && !"updating".equals(status)) {
×
67
            log.info("Peer {} in non-ready state: {}", peerUrl, status);
×
68
            return;
×
69
        }
70

71
        Long peerSetupId = getHeaderLong(resp, "Nanopub-Registry-Setup-Id");
×
72
        Long peerLoadCounter = getHeaderLong(resp, "Nanopub-Registry-Load-Counter");
×
73
        if (peerSetupId == null || peerLoadCounter == null) {
×
74
            log.info("Peer {} missing setupId or loadCounter headers", peerUrl);
×
75
            return;
×
76
        }
77

78
        syncWithPeer(s, peerUrl, peerSetupId, peerLoadCounter);
×
79
    }
×
80

81
    static void syncWithPeer(ClientSession s, String peerUrl, long peerSetupId, long peerLoadCounter) {
82
        Document peerState = getPeerState(s, peerUrl);
12✔
83
        Long lastSetupId = peerState != null ? peerState.getLong("setupId") : null;
24✔
84
        Long lastLoadCounter = peerState != null ? peerState.getLong("loadCounter") : null;
24✔
85

86
        if (lastSetupId != null && !lastSetupId.equals(peerSetupId)) {
21✔
87
            log.info("Peer {} was reset (setupId changed), resetting tracking", peerUrl);
12✔
88
            deletePeerState(s, peerUrl);
9✔
89
            lastLoadCounter = null;
6✔
90
        }
91

92
        long effectiveLoadCounter = lastLoadCounter != null ? lastLoadCounter : 0;
21✔
93

94
        if (lastLoadCounter != null && lastLoadCounter.equals(peerLoadCounter)) {
21!
95
            log.info("Peer {} has no new nanopubs (loadCounter unchanged: {})", peerUrl, peerLoadCounter);
21✔
96
        } else if (lastLoadCounter != null) {
6!
97
            // Fetch all nanopubs added since our last known position.
98
            // TODO Add per-pubkey afterCounter tracking for more targeted incremental sync
99
            long delta = peerLoadCounter - lastLoadCounter;
×
100
            log.info("Peer {} has {} new nanopubs, fetching recent", peerUrl, delta);
×
101
            long lastReceived = loadRecentNanopubs(s, peerUrl, lastLoadCounter);
×
102
            if (lastReceived > 0) {
×
103
                effectiveLoadCounter = lastReceived;
×
104
            }
105
            // Only discover new pubkeys when the peer has new data
106
            discoverPubkeys(s, peerUrl);
×
107
        } else {
×
108
            log.info("Peer {} is new, pubkey discovery will handle initial sync", peerUrl);
12✔
109
            discoverPubkeys(s, peerUrl);
9✔
110
        }
111
        updatePeerState(s, peerUrl, peerSetupId, effectiveLoadCounter);
15✔
112
    }
3✔
113

114
    /**
115
     * Fetches nanopubs from a peer after the given counter.
116
     * @return the counter of the last successfully received nanopub, or -1 if none were received
117
     */
118
    private static long loadRecentNanopubs(ClientSession s, String peerUrl, long afterCounter) {
119
        String requestUrl = peerUrl + "nanopubs.jelly?afterCounter=" + afterCounter;
×
120
        log.info("Fetching recent nanopubs from: {}", requestUrl);
×
121
        AtomicLong lastReceivedCounter = new AtomicLong(-1);
×
122
        try {
123
            HttpResponse resp = NanopubUtils.getHttpClient().execute(new HttpGet(requestUrl));
×
124
            int httpStatus = resp.getStatusLine().getStatusCode();
×
125
            if (httpStatus < 200 || httpStatus >= 300) {
×
126
                EntityUtils.consumeQuietly(resp.getEntity());
×
127
                log.info("Request failed: {} {}", requestUrl, httpStatus);
×
128
                return -1;
×
129
            }
130
            try (InputStream is = resp.getEntity().getContent()) {
×
131
                NanopubLoader.loadStreamInParallel(
×
132
                        NanopubStream.fromByteStream(is).getAsNanopubs().peek(m -> {
×
133
                            // Track counter in the main thread as items are consumed from the stream
134
                            if (m.isSuccess() && m.getCounter() > 0) {
×
135
                                lastReceivedCounter.set(m.getCounter());
×
136
                            }
137
                        }),
×
138
                        np -> {
139
                            try (ClientSession workerSession = RegistryDB.getClient().startSession()) {
×
140
                                String pubkey = RegistryDB.getPubkey(np);
×
141
                                if (pubkey != null) {
×
142
                                    RegistryDB.loadNanopubVerified(workerSession, np, pubkey, null);
×
143
                                    NanopubLoader.simpleLoad(workerSession, np, pubkey);
×
144
                                }
145
                            }
146
                        });
×
147
            }
148
        } catch (IOException ex) {
×
149
            log.info("Failed to fetch recent nanopubs from {}: {}", peerUrl, ex.getMessage());
×
150
        }
×
151
        log.info("Last received counter from {}: {}", peerUrl, lastReceivedCounter.get());
×
152
        return lastReceivedCounter.get();
×
153
    }
154

155
    static void discoverPubkeys(ClientSession s, String peerUrl) {
156
        log.info("Discovering pubkeys from peer: {}", peerUrl);
12✔
157
        try {
158
            List<String> peerPubkeys = Utils.retrieveListFromJsonUrl(peerUrl + "pubkeys.json");
×
159
            int discovered = 0;
×
160
            for (String pubkeyHash : peerPubkeys) {
×
161
                Document filter = new Document("pubkey", pubkeyHash).append("type", NanopubLoader.INTRO_TYPE_HASH);
×
162
                if (!has(s, "lists", filter)) {
×
163
                    try {
164
                        insert(s, "lists", new Document("pubkey", pubkeyHash)
×
165
                                .append("type", NanopubLoader.INTRO_TYPE_HASH)
×
166
                                .append("status", EntryStatus.encountered.getValue()));
×
167
                    } catch (MongoWriteException e) {
×
168
                        if (e.getError().getCategory() != ErrorCategory.DUPLICATE_KEY) throw e;
×
169
                    }
×
170
                    discovered++;
×
171
                } else if (!has(s, "lists", new Document(filter).append("status", EntryStatus.loaded.getValue()))) {
×
172
                    // Set status to encountered if not already loaded (fixes null-status entries from older code)
173
                    collection("lists").updateMany(s, filter,
×
174
                            new Document("$set", new Document("status", EntryStatus.encountered.getValue())));
×
175
                    discovered++;
×
176
                }
177
            }
×
178
            log.info("Discovered {} new pubkeys from peer {}", discovered, peerUrl);
×
179
        } catch (Exception ex) {
3✔
180
            log.info("Failed to discover pubkeys from {}: {}", peerUrl, ex.getMessage());
18✔
181
        }
×
182
    }
3✔
183

184
    static Document getPeerState(ClientSession s, String peerUrl) {
185
        try (MongoCursor<Document> cursor = collection(Collection.PEER_STATE.toString())
27✔
186
                .find(s, new Document("_id", peerUrl)).cursor()) {
9✔
187
            return cursor.hasNext() ? cursor.next() : null;
33✔
188
        }
189
    }
190

191
    static void updatePeerState(ClientSession s, String peerUrl, long setupId, long loadCounter) {
192
        collection(Collection.PEER_STATE.toString()).updateOne(s,
63✔
193
                new Document("_id", peerUrl),
194
                new Document("$set", new Document("_id", peerUrl)
195
                        .append("setupId", setupId)
12✔
196
                        .append("loadCounter", loadCounter)
9✔
197
                        .append("lastChecked", System.currentTimeMillis())),
24✔
198
                new com.mongodb.client.model.UpdateOptions().upsert(true));
3✔
199
    }
3✔
200

201
    static void deletePeerState(ClientSession s, String peerUrl) {
202
        collection(Collection.PEER_STATE.toString()).deleteOne(s, new Document("_id", peerUrl));
33✔
203
    }
3✔
204

205
    static boolean isTestInstance(HttpResponse resp) {
206
        return "true".equals(getHeader(resp, "Nanopub-Registry-Test-Instance"));
18✔
207
    }
208

209
    static String getHeader(HttpResponse resp, String name) {
210
        return resp.getFirstHeader(name) != null ? resp.getFirstHeader(name).getValue() : null;
33✔
211
    }
212

213
    static Long getHeaderLong(HttpResponse resp, String name) {
214
        String value = getHeader(resp, name);
12✔
215
        if (value == null || "null".equals(value)) return null;
24✔
216
        try {
217
            return Long.parseLong(value);
12✔
218
        } catch (NumberFormatException ex) {
3✔
219
            return null;
6✔
220
        }
221
    }
222

223
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc