aboutsummaryrefslogtreecommitdiff
path: root/libbrep/build.hxx
blob: 89908800af19235eb6b00c2d3b130ab8a09c0b02 (plain)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// file      : libbrep/build.hxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef LIBBREP_BUILD_HXX
#define LIBBREP_BUILD_HXX

#include <chrono>

#include <odb/core.hxx>
#include <odb/section.hxx>

#include <libbutl/target-triplet.mxx>

#include <libbbot/manifest.hxx>

#include <libbrep/types.hxx>
#include <libbrep/utility.hxx>

// Must be included last (see assert in libbrep/common.hxx).
//
#include <libbrep/common.hxx>
#include <libbrep/build-package.hxx>

// Used by the data migration entries.
//
#define LIBBREP_BUILD_SCHEMA_VERSION_BASE 9

#pragma db model version(LIBBREP_BUILD_SCHEMA_VERSION_BASE, 12, closed)

// We have to keep these mappings at the global scope instead of inside
// the brep namespace because they need to be also effective in the
// bbot namespace from which we "borrow" types (and some of them use the mapped
// types).
//
#pragma db map type(bbot::result_status) as(std::string) \
  to(to_string (?))                                      \
  from(bbot::to_result_status (?))

namespace brep
{
  #pragma db value
  struct build_id
  {
    package_id package;
    string configuration;
    string toolchain_name;
    canonical_version toolchain_version;

    build_id () = default;
    build_id (package_id p, string c, string n, const brep::version& v)
        : package (move (p)),
          configuration (move (c)),
          toolchain_name (move (n)),
          toolchain_version (v) {}
  };

  inline bool
  operator< (const build_id& x, const build_id& y)
  {
    if (x.package != y.package)
      return x.package < y.package;

    if (int r = x.configuration.compare (y.configuration))
      return r < 0;

    if (int r = x.toolchain_name.compare (y.toolchain_name))
      return r < 0;

    return compare_version_lt (x.toolchain_version, y.toolchain_version, true);
  }

  // These allow comparing objects that have package, configuration,
  // toolchain_name, and toolchain_version data members to build_id values.
  // The idea is that this works for both query members of build id types as
  // well as for values of the build_id type.
  //
  template <typename T>
  inline auto
  operator== (const T& x, const build_id& y)
    -> decltype (x.package == y.package               &&
                 x.configuration == y.configuration   &&
                 x.toolchain_name == y.toolchain_name &&
                 x.toolchain_version.epoch == y.toolchain_version.epoch)
  {
    return x.package == y.package               &&
           x.configuration == y.configuration   &&
           x.toolchain_name == y.toolchain_name &&
           compare_version_eq (x.toolchain_version, y.toolchain_version, true);
  }

  template <typename T>
  inline auto
  operator!= (const T& x, const build_id& y)
    -> decltype (x.package == y.package               &&
                 x.configuration == y.configuration   &&
                 x.toolchain_name == y.toolchain_name &&
                 x.toolchain_version.epoch == y.toolchain_version.epoch)
  {
    return x.package != y.package               ||
           x.configuration != y.configuration   ||
           x.toolchain_name != y.toolchain_name ||
           compare_version_ne (x.toolchain_version, y.toolchain_version, true);
  }

  // build_state
  //
  enum class build_state: std::uint8_t
  {
    building,
    built
  };

  string
  to_string (build_state);

  build_state
  to_build_state (const string&); // May throw invalid_argument.

  inline ostream&
  operator<< (ostream& os, build_state s) {return os << to_string (s);}

  #pragma db map type(build_state) as(string) \
    to(to_string (?))                         \
    from(brep::to_build_state (?))

  // force_state
  //
  enum class force_state: std::uint8_t
  {
    unforced,
    forcing,  // Rebuild is forced while being in the building state.
    forced    // Rebuild is forced while being in the built state.
  };

  string
  to_string (force_state);

  force_state
  to_force_state (const string&); // May throw invalid_argument.

  inline ostream&
  operator<< (ostream& os, force_state s) {return os << to_string (s);}

  #pragma db map type(force_state) as(string) \
    to(to_string (?))                         \
    from(brep::to_force_state (?))

  // result_status
  //
  using bbot::result_status;

  using optional_result_status = optional<result_status>;

  #pragma db map type(optional_result_status) as(optional_string) \
    to((?) ? bbot::to_string (*(?)) : brep::optional_string ())   \
    from((?)                                                      \
         ? bbot::to_result_status (*(?))                          \
         : brep::optional_result_status ())

  // target_triplet
  //
  #pragma db map type(butl::target_triplet) as(string) \
    to((?).string ())                                  \
    from(butl::target_triplet (?))

  // operation_results
  //
  using bbot::operation_result;
  #pragma db value(operation_result) definition

  using bbot::operation_results;

  #pragma db object pointer(shared_ptr) session
  class build
  {
  public:
    using timestamp_type    = brep::timestamp;
    using package_name_type = brep::package_name;

    // Create the build object with the building state, non-existent status,
    // the timestamp set to now and the force state set to unforced.
    //
    build (string tenant,
           package_name_type,
           version,
           string configuration,
           string toolchain_name, version toolchain_version,
           optional<string> agent_fingerprint,
           optional<string> agent_challenge,
           string machine, string machine_summary,
           butl::target_triplet);

    build_id id;

    string& tenant;                     // Tracks id.package.tenant.
    package_name_type& package_name;    // Tracks id.package.name.
    upstream_version package_version;   // Original of id.package.version.
    string& configuration;              // Tracks id.configuration.
    string& toolchain_name;             // Tracks id.toolchain_name.
    upstream_version toolchain_version; // Original of id.toolchain_version.

    build_state state;

    // Time of the last state change (the creation time initially).
    //
    timestamp_type timestamp;

    force_state force;

    // Must be present for the built state, may be present for the building
    // state.
    //
    optional<result_status> status;

    // Time of setting the result status that can be considered as the build
    // task completion (currently all the result_status values). Initialized
    // with timestamp_nonexistent by default.
    //
    // Note that in the future we may not consider abort and abnormal as the
    // task completion and, for example, proceed with automatic rebuild (the
    // flake monitor idea).
    //
    timestamp_type completion_timestamp;

    // May be present only for the building state.
    //
    optional<string> agent_fingerprint;
    optional<string> agent_challenge;

    string machine;
    string machine_summary;
    butl::target_triplet target;

    // Note that the logs are stored as std::string/TEXT which is Ok since
    // they are UTF-8 and our database is UTF-8.
    //
    operation_results results;
    odb::section results_section;

    // Database mapping.
    //
    #pragma db member(id) id column("")

    #pragma db member(tenant) transient
    #pragma db member(package_name) transient
    #pragma db member(package_version) \
      set(this.package_version.init (this.id.package.version, (?)))
    #pragma db member(configuration) transient
    #pragma db member(toolchain_name) transient
    #pragma db member(toolchain_version) \
      set(this.toolchain_version.init (this.id.toolchain_version, (?)))

    // Speed-up queries with ordering the result by the timestamp.
    //
    #pragma db member(timestamp) index

    // @@ TMP remove when 0.13.0 is released.
    //
    #pragma db member(completion_timestamp) default(0)

    #pragma db member(results) id_column("") value_column("") \
      section(results_section)

    #pragma db member(results_section) load(lazy) update(always)

    build (const build&) = delete;
    build& operator= (const build&) = delete;

  private:
    friend class odb::access;

    build ()
        : tenant (id.package.tenant),
          package_name (id.package.name),
          configuration (id.configuration),
          toolchain_name (id.toolchain_name) {}
  };

  // Note that ADL can't find the equal operator in join conditions, so we use
  // the function call notation for them.
  //

  // Toolchains of existing buildable package builds.
  //
  #pragma db view object(build)                                       \
    object(build_package inner:                                       \
           brep::operator== (build::id.package, build_package::id) && \
           build_package::buildable == "buildable")                   \
    query(distinct)
  struct toolchain
  {
    string name;
    upstream_version version;

    // Database mapping. Note that the version member must be loaded after
    // the virtual members since the version_ member must filled by that time.
    //
    #pragma db member(name) column(build::id.toolchain_name)

    #pragma db member(version) column(build::toolchain_version) \
      set(this.version.init (this.version_, (?)))

    #pragma db member(epoch) virtual(uint16_t)  \
      before(version) access(version_.epoch)    \
      column(build::id.toolchain_version.epoch)

    #pragma db member(canonical_upstream) virtual(std::string) \
      before(version) access(version_.canonical_upstream)      \
      column(build::id.toolchain_version.canonical_upstream)

    #pragma db member(canonical_release) virtual(std::string) \
      before(version) access(version_.canonical_release)      \
      column(build::id.toolchain_version.canonical_release)

    #pragma db member(revision) virtual(uint16_t)  \
      before(version) access(version_.revision)    \
      column(build::id.toolchain_version.revision)

  private:
    friend class odb::access;

    #pragma db transient
    canonical_version version_;
  };

  // Build of an existing buildable package.
  //
  #pragma db view                                                      \
    object(build)                                                      \
    object(build_package inner:                                        \
           brep::operator== (build::id.package, build_package::id) &&  \
           build_package::buildable == "buildable")                    \
    object(build_tenant: build_package::id.tenant == build_tenant::id)
  struct package_build
  {
    shared_ptr<brep::build> build;
  };

  #pragma db view                                                      \
    object(build)                                                      \
    object(build_package inner:                                        \
           brep::operator== (build::id.package, build_package::id) &&  \
           build_package::buildable == "buildable")                    \
    object(build_tenant: build_package::id.tenant == build_tenant::id)
  struct package_build_count
  {
    size_t result;

    operator size_t () const {return result;}

    // Database mapping.
    //
    #pragma db member(result) column("count(" + build::id.package.name + ")")
  };

  // Used to track the package build delays since the last build or, if not
  // present, since the first opportunity to build the package.
  //
  #pragma db object pointer(shared_ptr) session
  class build_delay
  {
  public:
    using package_name_type = brep::package_name;

    // If toolchain version is empty, then the object represents a minimum
    // delay across all versions of the toolchain.
    //
    build_delay (string tenant,
                 package_name_type, version,
                 string configuration,
                 string toolchain_name, version toolchain_version,
                 timestamp package_timestamp);

    build_id id;

    string& tenant;                     // Tracks id.package.tenant.
    package_name_type& package_name;    // Tracks id.package.name.
    upstream_version package_version;   // Original of id.package.version.
    string& configuration;              // Tracks id.configuration.
    string& toolchain_name;             // Tracks id.toolchain_name.
    upstream_version toolchain_version; // Original of id.toolchain_version.

    // Time of the latest delay report. Initialized with timestamp_nonexistent
    // by default.
    //
    timestamp report_timestamp;

    // Time when the package is initially considered as buildable for this
    // configuration and toolchain. It is used to track the build delay if the
    // build object is absent (the first build task is not yet issued, the
    // build is removed by brep-clean, etc).
    //
    timestamp package_timestamp;

    // Database mapping.
    //
    #pragma db member(id) id column("")

    #pragma db member(tenant) transient
    #pragma db member(package_name) transient
    #pragma db member(package_version) \
      set(this.package_version.init (this.id.package.version, (?)))
    #pragma db member(configuration) transient
    #pragma db member(toolchain_name) transient
    #pragma db member(toolchain_version) \
      set(this.toolchain_version.init (this.id.toolchain_version, (?)))

  private:
    friend class odb::access;

    build_delay ()
        : tenant (id.package.tenant),
          package_name (id.package.name),
          configuration (id.configuration),
          toolchain_name (id.toolchain_name) {}
  };
}

#endif // LIBBREP_BUILD_HXX