File: fetcher.cc

package info (click to toggle)
golang-github-google-certificate-transparency 0.0~git20160709.0.0f6e3d1~ds1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 5,676 kB
  • sloc: cpp: 35,278; python: 11,838; java: 1,911; sh: 1,885; makefile: 950; xml: 520; ansic: 225
file content (318 lines) | stat: -rw-r--r-- 9,762 bytes parent folder | download
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "fetcher/fetcher.h"

#include <gflags/gflags.h>
#include <glog/logging.h>
#include <memory>
#include <mutex>

#include "base/macros.h"
#include "log/log_verifier.h"
#include "monitoring/monitoring.h"

using cert_trans::AsyncLogClient;
using cert_trans::LoggedEntry;
using cert_trans::PeerGroup;
using std::bind;
using std::lock_guard;
using std::move;
using std::mutex;
using std::placeholders::_1;
using std::string;
using std::to_string;
using std::unique_lock;
using std::unique_ptr;
using std::vector;
using util::Status;
using util::Task;
using util::TaskHold;

DEFINE_int32(fetcher_concurrent_fetches, 2,
             "number of concurrent fetch requests");
DEFINE_int32(fetcher_batch_size, 1000,
             "maximum number of entries to fetch per request");

namespace cert_trans {

Counter<string>* num_invalid_entries_fetched =
    Counter<string>::New("num_invalid_entries_fetched", "reason",
                         "Number of invalid entries fetched from remote peers "
                         "broken down by reason.");


namespace {


struct Range {
  enum State {
    HAVE,
    FETCHING,
    WANT,
  };

  Range(State state, int64_t size, unique_ptr<Range> next = nullptr)
      : state_(state), size_(size), next_(move(next)) {
    CHECK(state_ == HAVE || state_ == FETCHING || state_ == WANT);
    CHECK_GT(size_, 0);
  };

  State state_;
  int64_t size_;
  unique_ptr<Range> next_;
};


struct FetchState {
  FetchState(Database* db, unique_ptr<PeerGroup> peer_group,
             const LogVerifier* log_verifier, Task* task);

  void WalkEntries();
  void FetchRange(const unique_lock<mutex>& lock, Range* current,
                  int64_t index, Task* range_task);
  void WriteToDatabase(int64_t index, Range* range,
                       const vector<AsyncLogClient::Entry>* retval,
                       Task* range_task, Task* fetch_task);

  Database* const db_;
  const unique_ptr<PeerGroup> peer_group_;
  const LogVerifier* const log_verifier_;
  Task* const task_;

  mutex lock_;
  int64_t start_;
  unique_ptr<Range> entries_;

 private:
  DISALLOW_COPY_AND_ASSIGN(FetchState);
};


FetchState::FetchState(Database* db, unique_ptr<PeerGroup> peer_group,
                       const LogVerifier* log_verifier, Task* task)
    : db_(CHECK_NOTNULL(db)),
      peer_group_(move(peer_group)),
      log_verifier_(CHECK_NOTNULL(log_verifier)),
      task_(CHECK_NOTNULL(task)),
      start_(db_->TreeSize()) {
  // TODO(pphaneuf): Might be better to get that as a parameter?
  const int64_t remote_tree_size(peer_group_->TreeSize());
  CHECK_GE(start_, 0);

  // Nothing to do...
  if (remote_tree_size <= start_) {
    VLOG(1) << "nothing to do: we have " << start_ << " entries, remote has "
            << remote_tree_size;
    task_->Return();
    return;
  }

  entries_.reset(new Range(Range::WANT, remote_tree_size - start_));

  WalkEntries();
}


// This is called either when starting the fetching, or when fetching
// a range completed. In that both cases, there's a hold on our task,
// so it shouldn't go away from under us.
void FetchState::WalkEntries() {
  if (!task_->IsActive()) {
    // We've already stopped, for one reason or another, no point
    // getting anything started.
    return;
  }

  if (task_->CancelRequested()) {
    task_->Return(Status::CANCELLED);
    return;
  }

  unique_lock<mutex> lock(lock_);

  // Prune fetched and unavailable sequences at the beginning.
  const int64_t remote_tree_size(peer_group_->TreeSize());
  while (entries_ &&
         (entries_->state_ == Range::HAVE ||
          (entries_->state_ == Range::WANT && remote_tree_size < start_))) {
    VLOG(1) << "pruning " << entries_->size_ << " at offset " << start_;
    start_ += entries_->size_;
    entries_ = move(entries_->next_);
  }

  // Are we done?
  if (!entries_) {
    task_->Return();
    return;
  }

  int64_t index(start_);
  int num_fetch(0);
  for (Range *current = entries_.get(); current;
       index += current->size_, current = current->next_.get()) {
    // Coalesce with the next Range, if possible.
    if (current->state_ != Range::FETCHING) {
      while (current->next_ && current->next_->state_ == current->state_) {
        current->size_ += current->next_->size_;
        current->next_ = move(current->next_->next_);
      }
    }

    switch (current->state_) {
      case Range::HAVE:
        VLOG(2) << "at offset " << index << ", we have " << current->size_
                << " entries";
        break;

      case Range::FETCHING:
        VLOG(2) << "at offset " << index << ", fetching " << current->size_
                << " entries";
        ++num_fetch;
        break;

      case Range::WANT:
        VLOG(2) << "at offset " << index << ", we want " << current->size_
                << " entries";

        // Do not start a fetch if we think our peer group does not
        // have it.
        if (index >= remote_tree_size) {
          break;
        }

        // If the range is bigger than the maximum batch size, split it.
        if (current->size_ > FLAGS_fetcher_batch_size) {
          current->next_.reset(
              new Range(Range::WANT, current->size_ - FLAGS_fetcher_batch_size,
                        move(current->next_)));
          current->size_ = FLAGS_fetcher_batch_size;
        }

        FetchRange(lock, current, index,
                   task_->AddChild(bind(&FetchState::WalkEntries, this)));
        ++num_fetch;

        break;
    }

    if (num_fetch >= FLAGS_fetcher_concurrent_fetches ||
        index >= remote_tree_size) {
      break;
    }
  }
}


void FetchState::FetchRange(const unique_lock<mutex>& lock, Range* current,
                            int64_t index, Task* range_task) {
  CHECK(lock.owns_lock());
  const int64_t end_index(index + current->size_ - 1);
  VLOG(1) << "fetching from offset " << index << " to " << end_index;

  vector<AsyncLogClient::Entry>* const retval(
      new vector<AsyncLogClient::Entry>);
  range_task->DeleteWhenDone(retval);

  current->state_ = Range::FETCHING;

  peer_group_->FetchEntries(index, end_index, retval,
                            range_task->AddChild(
                                bind(&FetchState::WriteToDatabase, this, index,
                                     current, retval, range_task, _1)));
}


void FetchState::WriteToDatabase(int64_t index, Range* range,
                                 const vector<AsyncLogClient::Entry>* retval,
                                 Task* range_task, Task* fetch_task) {
  if (!fetch_task->status().ok()) {
    LOG(INFO) << "error fetching entries at index " << index << ": "
              << fetch_task->status();
    lock_guard<mutex> lock(lock_);
    range->state_ = Range::WANT;
    range_task->Return(fetch_task->status());
    return;
  }

  CHECK_GT(retval->size(), static_cast<size_t>(0));

  VLOG(1) << "received " << retval->size() << " entries at offset " << index;
  int64_t processed(0);
  for (const auto& entry : *retval) {
    LoggedEntry cert;
    if (!cert.CopyFromClientLogEntry(entry)) {
      LOG(WARNING) << "could not convert entry to a LoggedEntry";
      num_invalid_entries_fetched->Increment("format");
      break;
    }
    if (entry.sct) {
      *cert.mutable_sct() = *entry.sct;
      // If we have the full SCT (because this LogEntry came from another
      // internal node which supports our private "give me the SCT too"
      // option), then verify that the signature is good.
      const LogVerifier::LogVerifyResult verify_result(
          log_verifier_->VerifySignedCertificateTimestamp(
              cert.contents().entry(), cert.sct()));
      VLOG(1) << "SCT verify entry #" << index << ": "
              << LogVerifier::VerifyResultString(verify_result);
      if (verify_result != LogVerifier::VERIFY_OK) {
        num_invalid_entries_fetched->Increment("sct_verify_failed");
        const string msg("Failed to verify SCT signature for entry# " +
                         to_string(index) + " : " +
                         LogVerifier::VerifyResultString(verify_result));
        LOG(WARNING) << msg;
        task_->Return(Status(util::error::FAILED_PRECONDITION, msg));
        return;
      }
    }
    cert.set_sequence_number(index++);
    if (db_->CreateSequencedEntry(cert) == Database::OK) {
      ++processed;
    } else {
      LOG(WARNING) << "could not insert entry into the database:\n"
                   << cert.DebugString();
      break;
    }
  }

  {
    lock_guard<mutex> lock(lock_);
    // TODO(pphaneuf): If we have problems fetching entries, to what
    // point should we retry? Or should we just return on the task
    // with an error?
    if (processed > 0) {
      // If we don't receive everything, split up the range.
      if (range->size_ > processed) {
        range->next_.reset(new Range(Range::WANT, range->size_ - processed,
                                     move(range->next_)));
        range->size_ = processed;
      }

      range->state_ = Range::HAVE;
    } else {
      range->state_ = Range::WANT;
    }
  }

  if (static_cast<uint64_t>(processed) < retval->size()) {
    // We couldn't insert everything that we received into the
    // database, this is fairly serious, return an error for the
    // overall operation and let the higher level deal with it.
    task_->Return(Status(util::error::INTERNAL,
                         "could not write some entries to the database"));
  }

  range_task->Return();
}


}  // namespace


void FetchLogEntries(Database* db, unique_ptr<PeerGroup> peer_group,
                     const LogVerifier* log_verifier, Task* task) {
  TaskHold hold(task);
  task->DeleteWhenDone(
      new FetchState(db, move(peer_group), log_verifier, task));
}


}  // namespace cert_trans