about summary refs log tree commit diff
path: root/third_party/nix/src/libstore/nar-info-disk-cache.cc
blob: c98882bff3197d83b227a9dca949c10eeae06529 (plain) (blame)
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
#include "libstore/nar-info-disk-cache.hh"

#include <absl/strings/str_cat.h>
#include <absl/strings/str_split.h>
#include <glog/logging.h>
#include <sqlite3.h>

#include "libstore/globals.hh"
#include "libstore/sqlite.hh"
#include "libutil/sync.hh"

namespace nix {

static const char* schema = R"sql(

create table if not exists BinaryCaches (
    id        integer primary key autoincrement not null,
    url       text unique not null,
    timestamp integer not null,
    storeDir  text not null,
    wantMassQuery integer not null,
    priority  integer not null
);

create table if not exists NARs (
    cache            integer not null,
    hashPart         text not null,
    namePart         text,
    url              text,
    compression      text,
    fileHash         text,
    fileSize         integer,
    narHash          text,
    narSize          integer,
    refs             text,
    deriver          text,
    sigs             text,
    ca               text,
    timestamp        integer not null,
    present          integer not null,
    primary key (cache, hashPart),
    foreign key (cache) references BinaryCaches(id) on delete cascade
);

create table if not exists LastPurge (
    dummy            text primary key,
    value            integer
);

)sql";

class NarInfoDiskCacheImpl : public NarInfoDiskCache {
 public:
  /* How often to purge expired entries from the cache. */
  const int purgeInterval = 24 * 3600;

  struct Cache {
    int id;
    Path storeDir;
    bool wantMassQuery;
    int priority;
  };

  struct State {
    SQLite db;
    SQLiteStmt insertCache, queryCache, insertNAR, insertMissingNAR, queryNAR,
        purgeCache;
    std::map<std::string, Cache> caches;
  };

  Sync<State> _state;

  NarInfoDiskCacheImpl() {
    auto state(_state.lock());

    Path dbPath = getCacheDir() + "/nix/binary-cache-v6.sqlite";
    createDirs(dirOf(dbPath));

    state->db = SQLite(dbPath);

    if (sqlite3_busy_timeout(state->db, 60 * 60 * 1000) != SQLITE_OK) {
      throwSQLiteError(state->db, "setting timeout");
    }

    // We can always reproduce the cache.
    state->db.exec("pragma synchronous = off");
    state->db.exec("pragma main.journal_mode = truncate");

    state->db.exec(schema);

    state->insertCache.create(
        state->db,
        "insert or replace into BinaryCaches(url, timestamp, storeDir, "
        "wantMassQuery, priority) values (?, ?, ?, ?, ?)");

    state->queryCache.create(state->db,
                             "select id, storeDir, wantMassQuery, priority "
                             "from BinaryCaches where url = ?");

    state->insertNAR.create(
        state->db,
        "insert or replace into NARs(cache, hashPart, namePart, url, "
        "compression, fileHash, fileSize, narHash, "
        "narSize, refs, deriver, sigs, ca, timestamp, present) values (?, ?, "
        "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)");

    state->insertMissingNAR.create(
        state->db,
        "insert or replace into NARs(cache, hashPart, timestamp, present) "
        "values (?, ?, ?, 0)");

    state->queryNAR.create(
        state->db,
        "select present, namePart, url, compression, fileHash, fileSize, "
        "narHash, narSize, refs, deriver, sigs, ca from NARs where cache = ? "
        "and hashPart = ? and ((present = 0 and timestamp > ?) or (present = 1 "
        "and timestamp > ?))");

    /* Periodically purge expired entries from the database. */
    retrySQLite<void>([&]() {
      auto now = time(nullptr);

      SQLiteStmt queryLastPurge(state->db, "select value from LastPurge");
      auto queryLastPurge_(queryLastPurge.use());

      if (!queryLastPurge_.next() ||
          queryLastPurge_.getInt(0) < now - purgeInterval) {
        SQLiteStmt(state->db,
                   "delete from NARs where ((present = 0 and timestamp < ?) or "
                   "(present = 1 and timestamp < ?))")
            .use()(now - settings.ttlNegativeNarInfoCache)(
                now - settings.ttlPositiveNarInfoCache)
            .exec();

        DLOG(INFO) << "deleted " << sqlite3_changes(state->db)
                   << " entries from the NAR info disk cache";

        SQLiteStmt(
            state->db,
            "insert or replace into LastPurge(dummy, value) values ('', ?)")
            .use()(now)
            .exec();
      }
    });
  }

  static Cache& getCache(State& state, const std::string& uri) {
    auto i = state.caches.find(uri);
    if (i == state.caches.end()) {
      abort();
    }
    return i->second;
  }

  void createCache(const std::string& uri, const Path& storeDir,
                   bool wantMassQuery, int priority) override {
    retrySQLite<void>([&]() {
      auto state(_state.lock());

      // FIXME: race

      state->insertCache
          .use()(uri)(time(nullptr))(storeDir)(
              static_cast<int64_t>(wantMassQuery))(priority)
          .exec();
      assert(sqlite3_changes(state->db) == 1);
      state->caches[uri] = Cache{(int)sqlite3_last_insert_rowid(state->db),
                                 storeDir, wantMassQuery, priority};
    });
  }

  bool cacheExists(const std::string& uri, bool& wantMassQuery,
                   int& priority) override {
    return retrySQLite<bool>([&]() {
      auto state(_state.lock());

      auto i = state->caches.find(uri);
      if (i == state->caches.end()) {
        auto queryCache(state->queryCache.use()(uri));
        if (!queryCache.next()) {
          return false;
        }
        state->caches.emplace(
            uri, Cache{(int)queryCache.getInt(0), queryCache.getStr(1),
                       queryCache.getInt(2) != 0, (int)queryCache.getInt(3)});
      }

      auto& cache(getCache(*state, uri));

      wantMassQuery = cache.wantMassQuery;
      priority = cache.priority;

      return true;
    });
  }

  std::pair<Outcome, std::shared_ptr<NarInfo>> lookupNarInfo(
      const std::string& uri, const std::string& hashPart) override {
    return retrySQLite<std::pair<Outcome, std::shared_ptr<NarInfo>>>(
        [&]() -> std::pair<Outcome, std::shared_ptr<NarInfo>> {
          auto state(_state.lock());

          auto& cache(getCache(*state, uri));

          auto now = time(nullptr);

          auto queryNAR(state->queryNAR.use()(cache.id)(hashPart)(
              now - settings.ttlNegativeNarInfoCache)(
              now - settings.ttlPositiveNarInfoCache));

          if (!queryNAR.next()) {
            return {oUnknown, nullptr};
          }

          if (queryNAR.getInt(0) == 0) {
            return {oInvalid, nullptr};
          }

          auto narInfo = make_ref<NarInfo>();

          auto namePart = queryNAR.getStr(1);
          narInfo->path = cache.storeDir + "/" + hashPart +
                          (namePart.empty() ? "" : "-" + namePart);
          narInfo->url = queryNAR.getStr(2);
          narInfo->compression = queryNAR.getStr(3);
          if (!queryNAR.isNull(4)) {
            narInfo->fileHash = Hash(queryNAR.getStr(4));
          }
          narInfo->fileSize = queryNAR.getInt(5);
          narInfo->narHash = Hash(queryNAR.getStr(6));
          narInfo->narSize = queryNAR.getInt(7);
          for (auto r : absl::StrSplit(queryNAR.getStr(8), absl::ByChar(' '))) {
            narInfo->references.insert(absl::StrCat(cache.storeDir, "/", r));
          }
          if (!queryNAR.isNull(9)) {
            narInfo->deriver = cache.storeDir + "/" + queryNAR.getStr(9);
          }
          for (auto& sig :
               absl::StrSplit(queryNAR.getStr(10), absl::ByChar(' '))) {
            narInfo->sigs.insert(std::string(sig));
          }
          narInfo->ca = queryNAR.getStr(11);

          return {oValid, narInfo};
        });
  }

  void upsertNarInfo(const std::string& uri, const std::string& hashPart,
                     std::shared_ptr<ValidPathInfo> info) override {
    retrySQLite<void>([&]() {
      auto state(_state.lock());

      auto& cache(getCache(*state, uri));

      if (info) {
        auto narInfo = std::dynamic_pointer_cast<NarInfo>(info);

        assert(hashPart == storePathToHash(info->path));

        state->insertNAR
            .use()(cache.id)(hashPart)(storePathToName(info->path))(
                narInfo ? narInfo->url : "", narInfo != nullptr)(
                narInfo ? narInfo->compression : "", narInfo != nullptr)(
                narInfo && narInfo->fileHash ? narInfo->fileHash.to_string()
                                             : "",
                narInfo && narInfo->fileHash)(
                narInfo ? narInfo->fileSize : 0,
                narInfo != nullptr &&
                    (narInfo->fileSize != 0u))(info->narHash.to_string())(
                info->narSize)(concatStringsSep(" ", info->shortRefs()))(
                !info->deriver.empty() ? baseNameOf(info->deriver) : "",
                !info->deriver.empty())(concatStringsSep(" ", info->sigs))(
                info->ca)(time(nullptr))
            .exec();

      } else {
        state->insertMissingNAR.use()(cache.id)(hashPart)(time(nullptr)).exec();
      }
    });
  }
};

ref<NarInfoDiskCache> getNarInfoDiskCache() {
  static ref<NarInfoDiskCache> cache = make_ref<NarInfoDiskCacheImpl>();
  return cache;
}

}  // namespace nix