summaryrefslogtreecommitdiffstats
path: root/src/libcephsqlite.cc
blob: a6e8a48833a8dcef7636069c902a48f2d8f10ad8 (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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2021 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License version 2.1, as published by
 * the Free Software Foundation.  See file COPYING.
 *
 */

#include <boost/smart_ptr/intrusive_ptr.hpp>
#include <fmt/format.h>

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <iomanip>
#include <iostream>
#include <regex>
#include <sstream>
#include <string_view>

#include <limits.h>
#include <string.h>

#include <sqlite3ext.h>
SQLITE_EXTENSION_INIT1

#include "include/ceph_assert.h"
#include "include/rados/librados.hpp"

#include "common/Clock.h"
#include "common/Formatter.h"
#include "common/ceph_argparse.h"
#include "common/ceph_mutex.h"
#include "common/common_init.h"
#include "common/config.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/perf_counters.h"
#include "common/version.h"

#include "include/libcephsqlite.h"
#include "SimpleRADOSStriper.h"

#define dout_subsys ceph_subsys_cephsqlite
#undef dout_prefix
#define dout_prefix *_dout << "cephsqlite: " << __func__ << ": "
#define d(vfs,lvl) ldout(getcct(vfs), (lvl)) << "(client." << getdata(vfs).cluster.get_instance_id() << ") "
#define dv(lvl) d(vfs,(lvl))
#define df(lvl) d(f->vfs,(lvl)) << f->loc << " "

enum {
  P_FIRST = 0xf0000,
  P_OP_OPEN,
  P_OP_DELETE,
  P_OP_ACCESS,
  P_OP_FULLPATHNAME,
  P_OP_CURRENTTIME,
  P_OPF_CLOSE,
  P_OPF_READ,
  P_OPF_WRITE,
  P_OPF_TRUNCATE,
  P_OPF_SYNC,
  P_OPF_FILESIZE,
  P_OPF_LOCK,
  P_OPF_UNLOCK,
  P_OPF_CHECKRESERVEDLOCK,
  P_OPF_FILECONTROL,
  P_OPF_SECTORSIZE,
  P_OPF_DEVICECHARACTERISTICS,
  P_LAST,
};

struct cephsqlite_appdata {
  ~cephsqlite_appdata() {
    if (logger) {
      cct->get_perfcounters_collection()->remove(logger.get());
    }
    if (striper_logger) {
      cct->get_perfcounters_collection()->remove(striper_logger.get());
    }
  }
  int setup_perf() {
    ceph_assert(cct);
    PerfCountersBuilder plb(cct.get(), "libcephsqlite_vfs", P_FIRST, P_LAST);
    plb.add_time_avg(P_OP_OPEN, "op_open", "Time average of Open operations");
    plb.add_time_avg(P_OP_DELETE, "op_delete", "Time average of Delete operations");
    plb.add_time_avg(P_OP_ACCESS, "op_access", "Time average of Access operations");
    plb.add_time_avg(P_OP_FULLPATHNAME, "op_fullpathname", "Time average of FullPathname operations");
    plb.add_time_avg(P_OP_CURRENTTIME, "op_currenttime", "Time average of Currenttime operations");
    plb.add_time_avg(P_OPF_CLOSE, "opf_close", "Time average of Close file operations");
    plb.add_time_avg(P_OPF_READ, "opf_read", "Time average of Read file operations");
    plb.add_time_avg(P_OPF_WRITE, "opf_write", "Time average of Write file operations");
    plb.add_time_avg(P_OPF_TRUNCATE, "opf_truncate", "Time average of Truncate file operations");
    plb.add_time_avg(P_OPF_SYNC, "opf_sync", "Time average of Sync file operations");
    plb.add_time_avg(P_OPF_FILESIZE, "opf_filesize", "Time average of FileSize file operations");
    plb.add_time_avg(P_OPF_LOCK, "opf_lock", "Time average of Lock file operations");
    plb.add_time_avg(P_OPF_UNLOCK, "opf_unlock", "Time average of Unlock file operations");
    plb.add_time_avg(P_OPF_CHECKRESERVEDLOCK, "opf_checkreservedlock", "Time average of CheckReservedLock file operations");
    plb.add_time_avg(P_OPF_FILECONTROL, "opf_filecontrol", "Time average of FileControl file operations");
    plb.add_time_avg(P_OPF_SECTORSIZE, "opf_sectorsize", "Time average of SectorSize file operations");
    plb.add_time_avg(P_OPF_DEVICECHARACTERISTICS, "opf_devicecharacteristics", "Time average of DeviceCharacteristics file operations");
    logger.reset(plb.create_perf_counters());
    if (int rc = SimpleRADOSStriper::config_logger(cct.get(), "libcephsqlite_striper", &striper_logger); rc < 0) {
      return rc;
    }
    cct->get_perfcounters_collection()->add(logger.get());
    cct->get_perfcounters_collection()->add(striper_logger.get());
    return 0;
  }
  int init_cluster() {
    ceph_assert(cct);
    ldout(cct, 5) << "initializing RADOS handle as " << cct->_conf->name << dendl;
    if (int rc = cluster.init_with_context(cct.get()); rc < 0) {
      lderr(cct) << "cannot initialize RADOS: " << cpp_strerror(rc) << dendl;
      return rc;
    }
    if (int rc = cluster.connect(); rc < 0) {
      lderr(cct) << "cannot connect: " << cpp_strerror(rc) << dendl;
      return rc;
    }
    auto s = cluster.get_addrs();
    ldout(cct, 5) << "completed connection to RADOS with address " << s << dendl;
    return 0;
  }

  boost::intrusive_ptr<CephContext> cct;
  std::unique_ptr<PerfCounters> logger;
  std::shared_ptr<PerfCounters> striper_logger;
  librados::Rados cluster;
  struct sqlite3_vfs vfs{};
};

struct cephsqlite_fileloc {
  std::string pool;
  std::string radosns;
  std::string name;
};

struct cephsqlite_fileio {
  librados::IoCtx ioctx;
  std::unique_ptr<SimpleRADOSStriper> rs;
};

std::ostream& operator<<(std::ostream &out, const cephsqlite_fileloc& fileloc) {
  return out
    << "["
    << fileloc.pool
    << ":"
    << fileloc.radosns
    << "/"
    << fileloc.name
    << "]"
    ;
}

struct cephsqlite_file {
  sqlite3_file base;
  struct sqlite3_vfs* vfs = nullptr;
  int flags = 0;
  // There are 5 lock states: https://sqlite.org/c3ref/c_lock_exclusive.html
  int lock = 0;
  struct cephsqlite_fileloc loc{};
  struct cephsqlite_fileio io{};
};


#define getdata(vfs) (*((cephsqlite_appdata*)((vfs)->pAppData)))

static CephContext* getcct(sqlite3_vfs* vfs)
{
  auto&& appd = getdata(vfs);
  auto& cct = appd.cct;
  if (cct) {
    return cct.get();
  }

  /* bootstrap cct */
  std::vector<const char*> env_args;
  env_to_vec(env_args, "CEPH_ARGS");
  std::string cluster, conf_file_list; // unused
  CephInitParameters iparams = ceph_argparse_early_args(env_args, CEPH_ENTITY_TYPE_CLIENT, &cluster, &conf_file_list);
  cct = boost::intrusive_ptr<CephContext>(common_preinit(iparams, CODE_ENVIRONMENT_LIBRARY, 0), false);
  cct->_conf.parse_config_files(nullptr, &std::cerr, 0);
  cct->_conf.parse_env(cct->get_module_type()); // environment variables override
  cct->_conf.apply_changes(nullptr);
  common_init_finish(cct.get());

  if (int rc = appd.setup_perf(); rc < 0) {
    ceph_abort("cannot setup perf counters");
  }

  if (int rc = appd.init_cluster(); rc < 0) {
    ceph_abort("cannot setup RADOS cluster handle");
  }

  return cct.get();
}

static int Lock(sqlite3_file *file, int ilock)
{
  auto f = (cephsqlite_file*)file;
  auto start = ceph::coarse_mono_clock::now();
  df(5) << std::hex << ilock << dendl;

  auto& lock = f->lock;
  ceph_assert(!f->io.rs->is_locked() || lock > SQLITE_LOCK_NONE);
  ceph_assert(lock <= ilock);
  if (!f->io.rs->is_locked() && ilock > SQLITE_LOCK_NONE) {
    if (int rc = f->io.rs->lock(0); rc < 0) {
      df(5) << "failed: " << rc << dendl;
      return SQLITE_IOERR;
    }
  }

  lock = ilock;
  auto end = ceph::coarse_mono_clock::now();
  getdata(f->vfs).logger->tinc(P_OPF_LOCK, end-start);
  return SQLITE_OK;
}

static int Unlock(sqlite3_file *file, int ilock)
{
  auto f = (cephsqlite_file*)file;
  auto start = ceph::coarse_mono_clock::now();
  df(5) << std::hex << ilock << dendl;

  auto& lock = f->lock;
  ceph_assert(lock == SQLITE_LOCK_NONE || (lock > SQLITE_LOCK_NONE && f->io.rs->is_locked()));
  ceph_assert(lock >= ilock);
  if (ilock <= SQLITE_LOCK_NONE && SQLITE_LOCK_NONE < lock) {
    if (int rc = f->io.rs->unlock(); rc < 0) {
      df(5) << "failed: " << rc << dendl;
      return SQLITE_IOERR;
    }
  }

  lock = ilock;
  auto end = ceph::coarse_mono_clock::now();
  getdata(f->vfs).logger->tinc(P_OPF_UNLOCK, end-start);
  return SQLITE_OK;
}

static int CheckReservedLock(sqlite3_file *file, int *result)
{
  auto f = (cephsqlite_file*)file;
  auto start = ceph::coarse_mono_clock::now();
  df(5) << dendl;

  auto& lock = f->lock;
  if (lock > SQLITE_LOCK_SHARED) {
    *result = 1;
  }

  df(10);
  f->io.rs->print_lockers(*_dout);
  *_dout << dendl;

  *result = 0;
  auto end = ceph::coarse_mono_clock::now();
  getdata(f->vfs).logger->tinc(P_OPF_CHECKRESERVEDLOCK, end-start);
  return SQLITE_OK;
}

static int Close(sqlite3_file *file)
{
  auto f = (cephsqlite_file*)file;
  auto start = ceph::coarse_mono_clock::now();
  df(5) << dendl;
  f->~cephsqlite_file();
  auto end = ceph::coarse_mono_clock::now();
  getdata(f->vfs).logger->tinc(P_OPF_CLOSE, end-start);
  return SQLITE_OK;
}

static int Read(sqlite3_file *file, void *buf, int len, sqlite_int64 off)
{
  auto f = (cephsqlite_file*)file;
  auto start = ceph::coarse_mono_clock::now();
  df(5) << buf << " " << off << "~" << len << dendl;

  if (int rc = f->io.rs->read(buf, len, off); rc < 0) {
    df(5) << "read failed: " << cpp_strerror(rc) << dendl;
    return SQLITE_IOERR_READ;
  } else {
    df(5) << "= " << rc << dendl;
    auto end = ceph::coarse_mono_clock::now();
    getdata(f->vfs).logger->tinc(P_OPF_READ, end-start);
    if (rc < len) {
      memset(buf, 0, len-rc);
      return SQLITE_IOERR_SHORT_READ;
    } else {
      return SQLITE_OK;
    }
  }
}

static int Write(sqlite3_file *file, const void *buf, int len, sqlite_int64 off)
{
  auto f = (cephsqlite_file*)file;
  auto start = ceph::coarse_mono_clock::now();
  df(5) << off << "~" << len << dendl;

  if (int rc = f->io.rs->write(buf, len, off); rc < 0) {
    df(5) << "write failed: " << cpp_strerror(rc) << dendl;
    return SQLITE_IOERR_WRITE;
  } else {
    df(5) << "= " << rc << dendl;
    auto end = ceph::coarse_mono_clock::now();
    getdata(f->vfs).logger->tinc(P_OPF_WRITE, end-start);
    return SQLITE_OK;
  }

}

static int Truncate(sqlite3_file *file, sqlite_int64 size)
{
  auto f = (cephsqlite_file*)file;
  auto start = ceph::coarse_mono_clock::now();
  df(5) << size << dendl;

  if (int rc = f->io.rs->truncate(size); rc < 0) {
    df(5) << "truncate failed: " << cpp_strerror(rc) << dendl;
    return SQLITE_IOERR;
  }

  auto end = ceph::coarse_mono_clock::now();
  getdata(f->vfs).logger->tinc(P_OPF_TRUNCATE, end-start);
  return SQLITE_OK;
}

static int Sync(sqlite3_file *file, int flags)
{
  auto f = (cephsqlite_file*)file;
  auto start = ceph::coarse_mono_clock::now();
  df(5) << flags << dendl;

  if (int rc = f->io.rs->flush(); rc < 0) {
    df(5) << "failed: " << cpp_strerror(rc) << dendl;
    return SQLITE_IOERR;
  }

  df(5) << " = 0" << dendl;

  auto end = ceph::coarse_mono_clock::now();
  getdata(f->vfs).logger->tinc(P_OPF_SYNC, end-start);
  return SQLITE_OK;
}


static int FileSize(sqlite3_file *file, sqlite_int64 *osize)
{
  auto f = (cephsqlite_file*)file;
  auto start = ceph::coarse_mono_clock::now();
  df(5) << dendl;

  uint64_t size = 0;
  if (int rc = f->io.rs->stat(&size); rc < 0) {
    df(5) << "stat failed: " << cpp_strerror(rc) << dendl;
    return SQLITE_NOTFOUND;
  }

  *osize = (sqlite_int64)size;

  df(5) << "= " << size << dendl;

  auto end = ceph::coarse_mono_clock::now();
  getdata(f->vfs).logger->tinc(P_OPF_FILESIZE, end-start);
  return SQLITE_OK;
}


static bool parsepath(std::string_view path, struct cephsqlite_fileloc* fileloc)
{
  static const std::regex re1{"^/*(\\*[[:digit:]]+):([[:alnum:]\\-_.]*)/([[:alnum:]\\-._]+)$"};
  static const std::regex re2{"^/*([[:alnum:]\\-_.]+):([[:alnum:]\\-_.]*)/([[:alnum:]\\-._]+)$"};

  std::cmatch cm;
  if (!std::regex_match(path.data(), cm, re1)) {
    if (!std::regex_match(path.data(), cm, re2)) {
      return false;
    }
  }
  fileloc->pool = cm[1];
  fileloc->radosns = cm[2];
  fileloc->name = cm[3];

  return true;
}

static int makestriper(sqlite3_vfs* vfs, const cephsqlite_fileloc& loc, cephsqlite_fileio* io)
{
  auto&& appd = getdata(vfs);
  auto& cct = appd.cct;
  auto& cluster = appd.cluster;
  bool gotmap = false;

  dv(10) << loc << dendl;

enoent_retry:
  if (loc.pool[0] == '*') {
    std::string err;
    int64_t id = strict_strtoll(loc.pool.c_str()+1, 10, &err);
    ceph_assert(err.empty());
    if (int rc = cluster.ioctx_create2(id, io->ioctx); rc < 0) {
      if (rc == -ENOENT && !gotmap) {
        cluster.wait_for_latest_osdmap();
        gotmap = true;
        goto enoent_retry;
      }
      dv(10) << "cannot create ioctx: " << cpp_strerror(rc) << dendl;
      return rc;
    }
  } else {
    if (int rc = cluster.ioctx_create(loc.pool.c_str(), io->ioctx); rc < 0) {
      if (rc == -ENOENT && !gotmap) {
        cluster.wait_for_latest_osdmap();
        gotmap = true;
        goto enoent_retry;
      }
      dv(10) << "cannot create ioctx: " << cpp_strerror(rc) << dendl;
      return rc;
    }
  }

  if (!loc.radosns.empty())
    io->ioctx.set_namespace(loc.radosns);

  io->rs = std::make_unique<SimpleRADOSStriper>(io->ioctx, loc.name);
  io->rs->set_logger(appd.striper_logger);
  io->rs->set_lock_timeout(cct->_conf.get_val<std::chrono::milliseconds>("cephsqlite_lock_renewal_timeout"));
  io->rs->set_lock_interval(cct->_conf.get_val<std::chrono::milliseconds>("cephsqlite_lock_renewal_interval"));
  io->rs->set_blocklist_the_dead(cct->_conf.get_val<bool>("cephsqlite_blocklist_dead_locker"));

  return 0;
}

static int SectorSize(sqlite3_file* sf)
{
  static const int size = 65536;
  auto start = ceph::coarse_mono_clock::now();
  auto f = (cephsqlite_file*)sf;
  df(5) << " = " << size << dendl;
  auto end = ceph::coarse_mono_clock::now();
  getdata(f->vfs).logger->tinc(P_OPF_SECTORSIZE, end-start);
  return size;
}

static int FileControl(sqlite3_file* sf, int op, void *arg)
{
  auto f = (cephsqlite_file*)sf;
  auto start = ceph::coarse_mono_clock::now();
  df(5) << op << ", " << arg << dendl;
  auto end = ceph::coarse_mono_clock::now();
  getdata(f->vfs).logger->tinc(P_OPF_FILECONTROL, end-start);
  return SQLITE_NOTFOUND;
}

static int DeviceCharacteristics(sqlite3_file* sf)
{
  auto f = (cephsqlite_file*)sf;
  auto start = ceph::coarse_mono_clock::now();
  df(5) << dendl;
  static const int c = 0
      |SQLITE_IOCAP_ATOMIC
      |SQLITE_IOCAP_POWERSAFE_OVERWRITE
      |SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
      |SQLITE_IOCAP_SAFE_APPEND
      ;
  auto end = ceph::coarse_mono_clock::now();
  getdata(f->vfs).logger->tinc(P_OPF_DEVICECHARACTERISTICS, end-start);
  return c;
}

static int Open(sqlite3_vfs *vfs, const char *name, sqlite3_file *file,
                int flags,  int *oflags)
{
  static const sqlite3_io_methods io = {
    1,                        /* iVersion */
    Close,                    /* xClose */
    Read,                     /* xRead */
    Write,                    /* xWrite */
    Truncate,                 /* xTruncate */
    Sync,                     /* xSync */
    FileSize,                 /* xFileSize */
    Lock,                     /* xLock */
    Unlock,                   /* xUnlock */
    CheckReservedLock,        /* xCheckReservedLock */
    FileControl,              /* xFileControl */
    SectorSize,               /* xSectorSize */
    DeviceCharacteristics     /* xDeviceCharacteristics */
  };

  auto start = ceph::coarse_mono_clock::now();
  bool gotmap = false;
  auto& cluster = getdata(vfs).cluster;

  /* we are not going to create temporary files */
  if (name == NULL) {
    dv(-1) << " cannot open temporary database" << dendl;
    return SQLITE_CANTOPEN;
  }
  auto path = std::string_view(name);
  if (path == ":memory:"sv) {
    dv(-1) << " cannot open temporary database" << dendl;
    return SQLITE_IOERR;
  }

  dv(5) << path << " flags=" << std::hex << flags << dendl;

  auto f = new (file)cephsqlite_file();
  f->vfs = vfs;
  if (!parsepath(path, &f->loc)) {
    ceph_assert(0); /* xFullPathname validates! */
  }
  f->flags = flags;

enoent_retry:
  if (int rc = makestriper(vfs, f->loc, &f->io); rc < 0) {
    f->~cephsqlite_file();
    dv(5) << "cannot open striper" << dendl;
    return SQLITE_IOERR;
  }

  if (flags & SQLITE_OPEN_CREATE) {
    dv(10) << "OPEN_CREATE" << dendl;
    if (int rc = f->io.rs->create(); rc < 0 && rc != -EEXIST) {
      if (rc == -ENOENT && !gotmap) {
        /* we may have an out of date OSDMap which cancels the op in the
         * Objecter. Try to get a new one and retry. This is mostly noticable
         * in testing when pools are getting created/deleted left and right.
         */
        dv(5) << "retrying create after getting latest OSDMap" << dendl;
        cluster.wait_for_latest_osdmap();
        gotmap = true;
        goto enoent_retry;
      }
      dv(5) << "file cannot be created: " << cpp_strerror(rc) << dendl;
      return SQLITE_IOERR;
    }
  }

  if (int rc = f->io.rs->open(); rc < 0) {
    if (rc == -ENOENT && !gotmap) {
      /* See comment above for create case. */
      dv(5) << "retrying open after getting latest OSDMap" << dendl;
      cluster.wait_for_latest_osdmap();
      gotmap = true;
      goto enoent_retry;
    }
    dv(10) << "cannot open striper: " << cpp_strerror(rc) << dendl;
    return rc;
  }

  if (oflags) {
    *oflags = flags;
  }
  f->base.pMethods = &io;
  auto end = ceph::coarse_mono_clock::now();
  getdata(vfs).logger->tinc(P_OP_OPEN, end-start);
  return SQLITE_OK;
}

/*
** Delete the file identified by argument path. If the dsync parameter
** is non-zero, then ensure the file-system modification to delete the
** file has been synced to disk before returning.
*/
static int Delete(sqlite3_vfs* vfs, const char* path, int dsync)
{
  auto start = ceph::coarse_mono_clock::now();
  dv(5) << "'" << path << "', " << dsync << dendl;

  cephsqlite_fileloc fileloc;
  if (!parsepath(path, &fileloc)) {
    dv(5) << "path does not parse!" << dendl;
    return SQLITE_NOTFOUND;
  }

  cephsqlite_fileio io;
  if (int rc = makestriper(vfs, fileloc, &io); rc < 0) {
    dv(5) << "cannot open striper" << dendl;
    return SQLITE_IOERR;
  }

  if (int rc = io.rs->lock(0); rc < 0) {
    return SQLITE_IOERR;
  }

  if (int rc = io.rs->remove(); rc < 0) {
    dv(5) << "= " << rc << dendl;
    return SQLITE_IOERR_DELETE;
  }

  /* No need to unlock */
  dv(5) << "= 0" << dendl;
  auto end = ceph::coarse_mono_clock::now();
  getdata(vfs).logger->tinc(P_OP_DELETE, end-start);

  return SQLITE_OK;
}

/*
** Query the file-system to see if the named file exists, is readable or
** is both readable and writable.
*/
static int Access(sqlite3_vfs* vfs, const char* path, int flags, int* result)
{
  auto start = ceph::coarse_mono_clock::now();
  dv(5) << path << " " << std::hex << flags << dendl;

  cephsqlite_fileloc fileloc;
  if (!parsepath(path, &fileloc)) {
    dv(5) << "path does not parse!" << dendl;
    return SQLITE_NOTFOUND;
  }

  cephsqlite_fileio io;
  if (int rc = makestriper(vfs, fileloc, &io); rc < 0) {
    dv(5) << "cannot open striper" << dendl;
    return SQLITE_IOERR;
  }

  if (int rc = io.rs->open(); rc < 0) {
    if (rc == -ENOENT) {
      *result = 0;
      return SQLITE_OK;
    } else {
      dv(10) << "cannot open striper: " << cpp_strerror(rc) << dendl;
      *result = 0;
      return SQLITE_IOERR;
    }
  }

  uint64_t size = 0;
  if (int rc = io.rs->stat(&size); rc < 0) {
    dv(5) << "= " << rc << " (" << cpp_strerror(rc) << ")" << dendl;
    *result = 0;
  } else {
    dv(5) << "= 0" << dendl;
    *result = 1;
  }

  auto end = ceph::coarse_mono_clock::now();
  getdata(vfs).logger->tinc(P_OP_ACCESS, end-start);
  return SQLITE_OK;
}

/* This method is only called once for each database. It provides a chance to
 * reformat the path into a canonical format.
 */
static int FullPathname(sqlite3_vfs* vfs, const char* ipath, int opathlen, char* opath)
{
  auto start = ceph::coarse_mono_clock::now();
  auto path = std::string_view(ipath);

  dv(5) << "1: " <<  path << dendl;

  cephsqlite_fileloc fileloc;
  if (!parsepath(path, &fileloc)) {
    dv(5) << "path does not parse!" << dendl;
    return SQLITE_NOTFOUND;
  }
  dv(5) << " parsed " << fileloc << dendl;

  auto p = fmt::format("{}:{}/{}", fileloc.pool, fileloc.radosns, fileloc.name);
  if (p.size() >= (size_t)opathlen) {
    dv(5) << "path too long!" << dendl;
    return SQLITE_CANTOPEN;
  }
  strcpy(opath, p.c_str());
  dv(5) << " output " << p << dendl;

  auto end = ceph::coarse_mono_clock::now();
  getdata(vfs).logger->tinc(P_OP_FULLPATHNAME, end-start);
  return SQLITE_OK;
}

static int CurrentTime(sqlite3_vfs* vfs, sqlite3_int64* time)
{
  auto start = ceph::coarse_mono_clock::now();
  dv(5) << time << dendl;

  auto t = ceph_clock_now();
  *time = t.to_msec() + 2440587.5*86400000; /* julian days since 1970 converted to ms */

  auto end = ceph::coarse_mono_clock::now();
  getdata(vfs).logger->tinc(P_OP_CURRENTTIME, end-start);
  return SQLITE_OK;
}

LIBCEPHSQLITE_API int cephsqlite_setcct(CephContext* cct, char** ident)
{
  ldout(cct, 1) << "cct: " << cct << dendl;

  if (sqlite3_api == nullptr) {
    lderr(cct) << "API violation: must have sqlite3 init libcephsqlite" << dendl;
    return -EINVAL;
  }

  auto vfs = sqlite3_vfs_find("ceph");
  if (!vfs) {
    lderr(cct) << "API violation: must have sqlite3 init libcephsqlite" << dendl;
    return -EINVAL;
  }

  auto& appd = getdata(vfs);
  appd.cct = cct;
  if (int rc = appd.setup_perf(); rc < 0) {
    appd.cct = nullptr;
    return rc;
  }
  if (int rc = appd.init_cluster(); rc < 0) {
    appd.cct = nullptr;
    return rc;
  }

  auto s = appd.cluster.get_addrs();
  if (ident) {
    *ident = strdup(s.c_str());
  }

  ldout(cct, 1) << "complete" << dendl;

  return 0;
}

static void f_perf(sqlite3_context* ctx, int argc, sqlite3_value** argv)
{
  auto vfs = (sqlite3_vfs*)sqlite3_user_data(ctx);
  dv(10) << dendl;
  auto&& appd = getdata(vfs);
  JSONFormatter f(false);
  f.open_object_section("ceph_perf");
  appd.logger->dump_formatted(&f, false);
  appd.striper_logger->dump_formatted(&f, false);
  f.close_section();
  {
    CachedStackStringStream css;
    f.flush(*css);
    auto sv = css->strv();
    dv(20) << " = " << sv << dendl;
    sqlite3_result_text(ctx, sv.data(), sv.size(), SQLITE_TRANSIENT);
  }
}

static void f_status(sqlite3_context* ctx, int argc, sqlite3_value** argv)
{
  auto vfs = (sqlite3_vfs*)sqlite3_user_data(ctx);
  dv(10) << dendl;
  auto&& appd = getdata(vfs);
  JSONFormatter f(false);
  f.open_object_section("ceph_status");
  f.dump_int("id", appd.cluster.get_instance_id());
  f.dump_string("addr", appd.cluster.get_addrs());
  f.close_section();
  {
    CachedStackStringStream css;
    f.flush(*css);
    auto sv = css->strv();
    dv(20) << " = " << sv << dendl;
    sqlite3_result_text(ctx, sv.data(), sv.size(), SQLITE_TRANSIENT);
  }
}

static int autoreg(sqlite3* db, char** err, const struct sqlite3_api_routines* thunk)
{
  auto vfs = sqlite3_vfs_find("ceph");
  if (!vfs) {
    ceph_abort("ceph vfs not found");
  }

  if (int rc = sqlite3_create_function(db, "ceph_perf", 0, SQLITE_UTF8, vfs, f_perf, nullptr, nullptr); rc) {
    return rc;
  }

  if (int rc = sqlite3_create_function(db, "ceph_status", 0, SQLITE_UTF8, vfs, f_status, nullptr, nullptr); rc) {
    return rc;
  }

  return SQLITE_OK;
}

LIBCEPHSQLITE_API int sqlite3_cephsqlite_init(sqlite3* db, char** err, const sqlite3_api_routines* api)
{
  SQLITE_EXTENSION_INIT2(api);

  auto vfs = sqlite3_vfs_find("ceph");
  if (!vfs) {
    auto appd = new cephsqlite_appdata;
    vfs = &appd->vfs;
    vfs->iVersion = 2;
    vfs->szOsFile = sizeof(struct cephsqlite_file);
    vfs->mxPathname = 4096;
    vfs->zName = "ceph";
    vfs->pAppData = appd;
    vfs->xOpen = Open;
    vfs->xDelete = Delete;
    vfs->xAccess = Access;
    vfs->xFullPathname = FullPathname;
    vfs->xCurrentTimeInt64 = CurrentTime;
    appd->cct = nullptr;
    sqlite3_vfs_register(vfs, 0);
  }

  if (int rc = sqlite3_auto_extension((void(*)(void))autoreg); rc) {
    return rc;
  }
  if (int rc = autoreg(db, err, api); rc) {
    return rc;
  }

  return SQLITE_OK_LOAD_PERMANENTLY;
}