File: ukm_database_backend.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (430 lines) | stat: -rw-r--r-- 14,974 bytes parent folder | download | duplicates (3)
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
420
421
422
423
424
425
426
427
428
429
430
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/segmentation_platform/internal/database/ukm_database_backend.h"

#include <vector>

#include "base/check_is_test.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/rand_util.h"
#include "base/strings/stringprintf.h"
#include "base/task/sequenced_task_runner.h"
#include "components/segmentation_platform/internal/database/ukm_metrics_table.h"
#include "components/segmentation_platform/internal/database/ukm_types.h"
#include "components/segmentation_platform/internal/database/ukm_url_table.h"
#include "components/segmentation_platform/internal/database/uma_metrics_table.h"
#include "sql/database.h"
#include "sql/statement.h"
#include "sql/transaction.h"

namespace segmentation_platform {

namespace {

BASE_FEATURE(kSqlWALModeOnSegmentationDatabase,
             "SqlWALModeOnSegmentationDatabase",
             base::FEATURE_ENABLED_BY_DEFAULT);

// Up to 10 updates are batched, because ~10 UKM metrics recorded in db per
// page load and approximately a commit every page load. This might need update
// if the metric count increases in the future.
static constexpr int kChangeCountToCommit = 10;

bool SanityCheckUrl(const GURL& url, UrlId url_id) {
  return url.is_valid() && !url.is_empty() && !url_id.is_null();
}

std::string BindValuesToStatement(
    const std::vector<processing::ProcessedValue>& bind_values,
    sql::Statement& statement) {
  std::stringstream debug_string;
  for (unsigned i = 0; i < bind_values.size(); ++i) {
    const processing::ProcessedValue& value = bind_values[i];
    switch (value.type) {
      case processing::ProcessedValue::Type::BOOL:
        debug_string << i << ":" << value.bool_val << " ";
        statement.BindBool(i, value.bool_val);
        break;
      case processing::ProcessedValue::Type::INT:
        debug_string << i << ":" << value.int_val << " ";
        statement.BindInt(i, value.int_val);
        break;
      case processing::ProcessedValue::Type::FLOAT:
        debug_string << i << ":" << value.float_val << " ";
        statement.BindDouble(i, value.float_val);
        break;
      case processing::ProcessedValue::Type::DOUBLE:
        debug_string << i << ":" << value.double_val << " ";
        statement.BindDouble(i, value.double_val);
        break;
      case processing::ProcessedValue::Type::STRING:
        debug_string << i << ":" << value.str_val << " ";
        statement.BindString(i, value.str_val);
        break;
      case processing::ProcessedValue::Type::TIME:
        debug_string << i << ":" << value.time_val << " ";
        statement.BindTime(i, value.time_val);
        break;
      case processing::ProcessedValue::Type::INT64:
        debug_string << i << ":" << value.int64_val << " ";
        statement.BindInt64(i, value.int64_val);
        break;
      case processing::ProcessedValue::Type::URL:
        debug_string << i << ":"
                     << UkmUrlTable::GetDatabaseUrlString(*value.url) << " ";
        statement.BindString(i, UkmUrlTable::GetDatabaseUrlString(*value.url));
        break;
      case processing::ProcessedValue::Type::UNKNOWN:
        NOTREACHED();
    }
  }
  return debug_string.str();
}

float GetSingleFloatOutput(sql::Statement& statement) {
  sql::ColumnType output_type = statement.GetColumnType(0);
  switch (output_type) {
    case sql::ColumnType::kBlob:
    case sql::ColumnType::kText:
      NOTREACHED();
    case sql::ColumnType::kFloat:
      return statement.ColumnDouble(0);
    case sql::ColumnType::kInteger:
      return statement.ColumnInt64(0);
    case sql::ColumnType::kNull:
      return 0;
  }
}

void ErrorCallback(int code, sql::Statement* stmt) {
  VLOG(1) << "SQL run error " << code;
}

}  // namespace

UkmDatabaseBackend::UkmDatabaseBackend(
    const base::FilePath& database_path,
    bool in_memory,
    scoped_refptr<base::SequencedTaskRunner> callback_task_runner)
    : database_path_(database_path),
      in_memory_(in_memory),
      callback_task_runner_(callback_task_runner),
      db_(sql::DatabaseOptions().set_wal_mode(
              base::FeatureList::IsEnabled(kSqlWALModeOnSegmentationDatabase)),
          /*tag=*/"UKMMetrics"),
      metrics_table_(&db_),
      url_table_(&db_),
      uma_metrics_table_(&db_) {
  DETACH_FROM_SEQUENCE(sequence_checker_);
  db_.set_error_callback(base::BindRepeating(&ErrorCallback));
}

UkmDatabaseBackend::~UkmDatabaseBackend() {
  if (current_transaction_) {
    current_transaction_->Commit();
    current_transaction_.reset();
  }
}

void UkmDatabaseBackend::InitDatabase(SuccessCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  SCOPED_UMA_HISTOGRAM_TIMER("SegmentationPlatform.UkmDatabase.InitTime");
  base::File::Error error{};
  bool result = true;
  if (in_memory_) {
    CHECK_IS_TEST();
    result = db_.OpenInMemory();
  } else if (!base::CreateDirectoryAndGetError(database_path_.DirName(),
                                               &error) ||
             !db_.Open(database_path_)) {
    // TODO(ssid): On failure retry opening the database or delete backend or
    // open in memory for session.
    LOG(ERROR) << "Failed to open UKM database: " << error << " "
               << db_.GetErrorMessage();
    result = false;
  }
  if (result) {
    result = metrics_table_.InitTable() && url_table_.InitTable() &&
             uma_metrics_table_.InitTable();
  }
  status_ = result ? Status::INIT_SUCCESS : Status::INIT_FAILED;

  if (status_ == Status::INIT_SUCCESS) {
    RestartTransaction();
  }
  callback_task_runner_->PostTask(FROM_HERE,
                                  base::BindOnce(std::move(callback), result));
}

void UkmDatabaseBackend::StoreUkmEntry(ukm::mojom::UkmEntryPtr entry) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  SCOPED_UMA_HISTOGRAM_TIMER("SegmentationPlatform.Database.StoreUkmEntry");
  if (status_ != Status::INIT_SUCCESS) {
    return;
  }

  MetricsRowEventId event_id =
      MetricsRowEventId::FromUnsafeValue(base::RandUint64());
  // If we have an URL ID for the entry, then use it, otherwise the URL ID will
  // be updated when to all metrics when UpdateUrlForUkmSource() is called.
  UrlId url_id;
  auto it = source_to_url_.find(entry->source_id);
  if (it != source_to_url_.end())
    url_id = it->second;

  UkmMetricsTable::MetricsRow row = {
      .event_timestamp = base::Time::Now(),
      .url_id = url_id,
      .source_id = entry->source_id,
      .event_id = event_id,
      .event_hash = UkmEventHash::FromUnsafeValue(entry->event_hash)};
  for (const auto& metric_and_value : entry->metrics) {
    row.metric_hash = UkmMetricHash::FromUnsafeValue(metric_and_value.first);
    row.metric_value = metric_and_value.second;
    metrics_table_.AddUkmEvent(row);
  }
  TrackChangesInTransaction(entry->metrics.size());
}

void UkmDatabaseBackend::UpdateUrlForUkmSource(ukm::SourceId source_id,
                                               const GURL& url,
                                               bool is_validated,
                                               const std::string& profile_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  SCOPED_UMA_HISTOGRAM_TIMER(
      "SegmentationPlatform.Database.UpdateUrlForUkmSource");
  if (status_ != Status::INIT_SUCCESS) {
    return;
  }

  UrlId url_id = UkmUrlTable::GenerateUrlId(url);
  if (!SanityCheckUrl(url, url_id)) {
    return;
  }

  if (!url_table_.IsUrlInTable(url_id)) {
    if (is_validated) {
      url_table_.WriteUrl(url, url_id, base::Time::Now(), profile_id);
      // Remove from list so we don't add the URL again to table later.
      urls_not_validated_.erase(url_id);
    } else {
      urls_not_validated_.insert(url_id);
    }
  } else {
    url_table_.UpdateUrlTimestamp(url_id, base::Time::Now());
  }
  // Keep track of source to URL ID mapping for future metrics.
  source_to_url_[source_id] = url_id;
  // Update all entries in metrics table with the URL ID.
  metrics_table_.UpdateUrlIdForSource(source_id, url_id);

  TrackChangesInTransaction(2);  // 2 updates above.
}

void UkmDatabaseBackend::OnUrlValidated(const GURL& url,
                                        const std::string& profile_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (status_ != Status::INIT_SUCCESS) {
    return;
  }

  UrlId url_id = UkmUrlTable::GenerateUrlId(url);
  // Write URL to table only if it's needed and it's not already added.
  if (urls_not_validated_.count(url_id) && SanityCheckUrl(url, url_id)) {
    url_table_.WriteUrl(url, url_id, base::Time::Now(), profile_id);
    urls_not_validated_.erase(url_id);
  }
  TrackChangesInTransaction(1);
}

void UkmDatabaseBackend::RemoveUrls(const std::vector<GURL>& urls,
                                    bool all_urls) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  SCOPED_UMA_HISTOGRAM_TIMER("SegmentationPlatform.Database.RemoveUrls");
  if (status_ != Status::INIT_SUCCESS) {
    return;
  }

  if (all_urls) {
    DeleteAllUrls();
    return;
  }

  std::vector<UrlId> url_ids;
  for (const GURL& url : urls) {
    UrlId id = UkmUrlTable::GenerateUrlId(url);
    // Do not accidentally remove all entries without URL (kInvalidUrlID).
    if (!SanityCheckUrl(url, id))
      continue;
    url_ids.push_back(id);
    urls_not_validated_.erase(id);
  }
  url_table_.RemoveUrls(url_ids);
  metrics_table_.DeleteEventsForUrls(url_ids);

  // Force commit so that we don't store URLs longer than needed.
  RestartTransaction();
}

void UkmDatabaseBackend::AddUmaMetric(const std::string& profile_id,
                                      const UmaMetricEntry& row) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  SCOPED_UMA_HISTOGRAM_TIMER("SegmentationPlatform.Database.AddUmaMetric");
  if (status_ != Status::INIT_SUCCESS) {
    return;
  }
  uma_metrics_table_.AddUmaMetric(profile_id, row);
}

void UkmDatabaseBackend::RunReadOnlyQueries(QueryList&& queries,
                                            QueryCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  SCOPED_UMA_HISTOGRAM_TIMER(
      "SegmentationPlatform.Database.RunReadOnlyQueries");
  if (status_ != Status::INIT_SUCCESS) {
    callback_task_runner_->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback), false,
                                  processing::IndexedTensors()));
    return;
  }

  bool success = true;
  processing::IndexedTensors result;
  for (const auto& index_and_query : queries) {
    const processing::FeatureIndex index = index_and_query.first;
    const UkmDatabase::CustomSqlQuery& query = index_and_query.second;
    std::string debug_query = query.query;

    sql::Statement statement(db_.GetReadonlyStatement(query.query));
    debug_query +=
        " Bind values: " + BindValuesToStatement(query.bind_values, statement);

    if (!statement.is_valid()) {
      VLOG(1) << "Failed to run SQL query " << debug_query;
      success = false;
      break;
    }
    while (statement.Step()) {
      float output = GetSingleFloatOutput(statement);
      result[index].push_back(processing::ProcessedValue::FromFloat(output));
    }
    if (!result.count(index) || result.at(index).empty() ||
        !statement.Succeeded()) {
      VLOG(1) << "Failed to run SQL query " << debug_query;
      success = false;
      break;
    }

    if (VLOG_IS_ON(1)) {
      std::string outputs;
      for (const auto& val : result[index]) {
        outputs.append(base::StringPrintf("%f,", val.float_val));
      }
      VLOG(1) << "Output from SQL query " << debug_query
              << " Result: " << outputs;
    }
  }
  callback_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(std::move(callback), success, std::move(result)));
}

void UkmDatabaseBackend::DeleteEntriesOlderThan(base::Time time) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (status_ != Status::INIT_SUCCESS) {
    return;
  }

  std::vector<UrlId> deleted_urls =
      metrics_table_.DeleteEventsBeforeTimestamp(time);
  url_table_.RemoveUrls(deleted_urls);
  url_table_.DeleteUrlsBeforeTimestamp(time);

  // Force commit so that we don't store URLs longer than needed.
  RestartTransaction();
}

void UkmDatabaseBackend::CleanupItems(const std::string& profile_id,
                                      std::vector<CleanupItem> cleanup_items) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (status_ != Status::INIT_SUCCESS) {
    return;
  }

  // This needs to support clean up for UKM data.
  // Only `cleanup_items` with uma types should be sent to uma table.
  std::erase_if(cleanup_items,
                [](const CleanupItem& item) { return !item.IsUma(); });
  uma_metrics_table_.CleanupItems(profile_id, cleanup_items);
  TrackChangesInTransaction(cleanup_items.size());
}

void UkmDatabaseBackend::CommitTransactionForTesting() {
  RestartTransaction();
}

void UkmDatabaseBackend::RollbackTransactionForTesting() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  CHECK(current_transaction_);
  current_transaction_->Rollback();
  current_transaction_.reset();
}

void UkmDatabaseBackend::DeleteAllUrls() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  CHECK_EQ(status_, Status::INIT_SUCCESS);

  // Remove all metrics associated with any URL, but retain the metrics that are
  // not keyed on URL.
  bool success = db_.Execute("DELETE FROM metrics WHERE url_id!=0");
  // TODO(ssid): sqlite uses truncate optimization on DELETE statements without
  // WHERE clause. Maybe replace the DROP and CREATE with DELETE if the
  // performance is better.
  success = success && db_.Execute("DROP TABLE urls");
  success = success && url_table_.InitTable();
  DCHECK(success);
  RestartTransaction();
}

void UkmDatabaseBackend::TrackChangesInTransaction(int change_count) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // No transaction has begun, begin one.
  if (!current_transaction_) {
    RestartTransaction();
    // Ignore change_count since no transaction has begun yet.
    return;
  }

  change_count_ += change_count;

  // If enough changes are made, commit them and begin a new transaction.
  if (change_count_ > kChangeCountToCommit) {
    RestartTransaction();
  }
}

void UkmDatabaseBackend::RestartTransaction() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (current_transaction_) {
    current_transaction_->Commit();
    current_transaction_.reset();
  }

  change_count_ = 0;
  current_transaction_ = std::make_unique<sql::Transaction>(&db_);
  if (!current_transaction_->Begin()) {
    current_transaction_.reset();
  }

  // Forces the wal file to be in sync with the main database.
  std::ignore = db_.Execute("PRAGMA wal_checkpoint(TRUNCATE)");
}

}  // namespace segmentation_platform