summaryrefslogtreecommitdiffstats
path: root/src/crimson/os/alienstore/alien_store.cc
blob: cb55532545b47cdc90bb992cae5b962514dbd0cd (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "alien_collection.h"
#include "alien_store.h"

#include <map>
#include <string_view>
#include <boost/algorithm/string/trim.hpp>
#include <fmt/format.h>
#include <fmt/ostream.h>

#include <seastar/core/alien.hh>
#include <seastar/core/future-util.hh>
#include <seastar/core/reactor.hh>

#include "common/ceph_context.h"
#include "global/global_context.h"
#include "include/Context.h"
#include "os/bluestore/BlueStore.h"
#include "os/ObjectStore.h"
#include "os/Transaction.h"

#include "crimson/common/log.h"
#include "crimson/os/futurized_store.h"

namespace {
  seastar::logger& logger()
  {
    return crimson::get_logger(ceph_subsys_filestore);
  }

class OnCommit final: public Context
{
  int cpuid;
  Context *oncommit;
  seastar::promise<> &alien_done;
public:
  OnCommit(
    int id,
    seastar::promise<> &done,
    Context *oncommit,
    ceph::os::Transaction& txn)
    : cpuid(id), oncommit(oncommit),
      alien_done(done) {}

  void finish(int) final {
    return seastar::alien::submit_to(cpuid, [this] {
      if (oncommit) oncommit->complete(0);
      alien_done.set_value();
      return seastar::make_ready_future<>();
    }).wait();
  }
};
}

namespace crimson::os {

AlienStore::AlienStore(const std::string& path, const ConfigValues& values)
  : path{path}
{
  cct = std::make_unique<CephContext>(CEPH_ENTITY_TYPE_OSD);
  g_ceph_context = cct.get();
  cct->_conf.set_config_values(values);
  store = std::make_unique<BlueStore>(cct.get(), path);

  long cpu_id = 0;
  if (long nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); nr_cpus != -1) {
    cpu_id = nr_cpus - 1;
  } else {
    logger().error("{}: unable to get nproc: {}", __func__, errno);
    cpu_id = -1;
  }
  tp = std::make_unique<crimson::os::ThreadPool>(1, 128, cpu_id);
}

seastar::future<> AlienStore::start()
{
  return tp->start();
}

seastar::future<> AlienStore::stop()
{
  return tp->submit([this] {
    for (auto [cid, ch]: coll_map)
      static_cast<AlienCollection*>(ch.get())->collection.reset();
    store.reset();
  }).then([this] {
    return tp->stop();
  });
}

AlienStore::~AlienStore() = default;

seastar::future<> AlienStore::mount()
{
  logger().debug("{}", __func__);
  return tp->submit([this] {
    return store->mount();
  }).then([] (int r) {
    assert(r == 0);
    return seastar::now();
  });
}

seastar::future<> AlienStore::umount()
{
  logger().info("{}", __func__);
  return transaction_gate.close().then([this] {
    return tp->submit([this] {
      return store->umount();
    });
  }).then([] (int r) {
    assert(r == 0);
    return seastar::now();
  });
}

seastar::future<> AlienStore::mkfs(uuid_d osd_fsid)
{
  logger().debug("{}", __func__);
  store->set_fsid(osd_fsid);
  return tp->submit([this] {
    return store->mkfs();
  }).then([] (int r) {
    assert(r == 0);
    return seastar::now();
  });
}

seastar::future<std::tuple<std::vector<ghobject_t>, ghobject_t>>
AlienStore::list_objects(CollectionRef ch,
                        const ghobject_t& start,
                        const ghobject_t& end,
                        uint64_t limit) const
{
  logger().debug("{}", __func__);
  return seastar::do_with(std::vector<ghobject_t>(), ghobject_t(),
                          [=] (auto &objects, auto &next) {
    objects.reserve(limit);
    return tp->submit([=, &objects, &next] {
      auto c = static_cast<AlienCollection*>(ch.get());
      return store->collection_list(c->collection, start, end,
                                    store->get_ideal_list_max(),
                                    &objects, &next);
    }).then([&objects, &next] (int r) {
      assert(r == 0);
      return seastar::make_ready_future<std::tuple<std::vector<ghobject_t>, ghobject_t>>(
	std::make_tuple(std::move(objects), std::move(next)));
    });
  });
}

seastar::future<CollectionRef> AlienStore::create_new_collection(const coll_t& cid)
{
  logger().debug("{}", __func__);
  return tp->submit([this, cid] {
    return store->create_new_collection(cid);
  }).then([this, cid] (ObjectStore::CollectionHandle c) {
    CollectionRef ch;
    auto cp = coll_map.find(c->cid);
    if (cp == coll_map.end()) {
      ch = new AlienCollection(c);
      coll_map[c->cid] = ch;
    } else {
      ch = cp->second;
      auto ach = static_cast<AlienCollection*>(ch.get());
      if (ach->collection != c) {
        ach->collection = c;
      }
    }
    return seastar::make_ready_future<CollectionRef>(ch);
  });

}

seastar::future<CollectionRef> AlienStore::open_collection(const coll_t& cid)
{
  logger().debug("{}", __func__);
  return tp->submit([this, cid] {
    return store->open_collection(cid);
  }).then([this] (ObjectStore::CollectionHandle c) {
    CollectionRef ch;
    auto cp = coll_map.find(c->cid);
    if (cp == coll_map.end()){
      ch = new AlienCollection(c);
      coll_map[c->cid] = ch;
    } else {
      ch = cp->second;
      auto ach = static_cast<AlienCollection*>(ch.get());
      if (ach->collection != c){
        ach->collection = c;
      }
    }
    return seastar::make_ready_future<CollectionRef>(ch);
  });
}

seastar::future<std::vector<coll_t>> AlienStore::list_collections()
{
  logger().debug("{}", __func__);

  return seastar::do_with(std::vector<coll_t>{}, [=] (auto &ls) {
    return tp->submit([this, &ls] {
      return store->list_collections(ls);
    }).then([&ls] (int r) {
      assert(r == 0);
      return seastar::make_ready_future<std::vector<coll_t>>(std::move(ls));
    });
  });
}

AlienStore::read_errorator::future<ceph::bufferlist>
AlienStore::read(CollectionRef ch,
                 const ghobject_t& oid,
                 uint64_t offset,
                 size_t len,
                 uint32_t op_flags)
{
  logger().debug("{}", __func__);
  return seastar::do_with(ceph::bufferlist{}, [=] (auto &bl) {
    return tp->submit([=, &bl] {
      auto c = static_cast<AlienCollection*>(ch.get());
      return store->read(c->collection, oid, offset, len, bl, op_flags);
    }).then([&bl] (int r) -> read_errorator::future<ceph::bufferlist> {
      if (r == -ENOENT) {
        return crimson::ct_error::enoent::make();
      } else if (r == -EIO) {
        return crimson::ct_error::input_output_error::make();
      } else {
        return read_errorator::make_ready_future<ceph::bufferlist>(std::move(bl));
      }
    });
  });
}

AlienStore::read_errorator::future<ceph::bufferlist>
AlienStore::readv(CollectionRef ch,
		  const ghobject_t& oid,
		  interval_set<uint64_t>& m,
		  uint32_t op_flags)
{
  logger().debug("{}", __func__);
  return seastar::do_with(ceph::bufferlist{},
    [this, ch, oid, &m, op_flags](auto& bl) {
    return tp->submit([this, ch, oid, &m, op_flags, &bl] {
      auto c = static_cast<AlienCollection*>(ch.get());
      return store->readv(c->collection, oid, m, bl, op_flags);
    }).then([&bl](int r) -> read_errorator::future<ceph::bufferlist> {
      if (r == -ENOENT) {
        return crimson::ct_error::enoent::make();
      } else if (r == -EIO) {
        return crimson::ct_error::input_output_error::make();
      } else {
        return read_errorator::make_ready_future<ceph::bufferlist>(std::move(bl));
      }
    });
  });
}

AlienStore::get_attr_errorator::future<ceph::bufferptr>
AlienStore::get_attr(CollectionRef ch,
                     const ghobject_t& oid,
                     std::string_view name) const
{
  logger().debug("{}", __func__);
  return seastar::do_with(ceph::bufferptr{}, [=] (auto &value) {
    return tp->submit([=, &value] {
      auto c =static_cast<AlienCollection*>(ch.get());
      return store->getattr(c->collection, oid,
		            static_cast<std::string>(name).c_str(), value);
    }).then([oid, &value] (int r) -> get_attr_errorator::future<ceph::bufferptr> {
      if (r == -ENOENT) {
        return crimson::ct_error::enoent::make();
      } else if (r == -ENODATA) {
        return crimson::ct_error::enodata::make();
      } else {
        return get_attr_errorator::make_ready_future<ceph::bufferptr>(
          std::move(value));
      }
    });
  });
}

AlienStore::get_attrs_ertr::future<AlienStore::attrs_t>
AlienStore::get_attrs(CollectionRef ch,
                      const ghobject_t& oid)
{
  logger().debug("{}", __func__);
  return seastar::do_with(attrs_t{}, [=] (auto &aset) {
    return tp->submit([=, &aset] {
      auto c = static_cast<AlienCollection*>(ch.get());
      return store->getattrs(c->collection, oid,
		             reinterpret_cast<map<string,bufferptr>&>(aset));
    }).then([&aset] (int r) -> get_attrs_ertr::future<attrs_t> {
      if (r == -ENOENT) {
        return crimson::ct_error::enoent::make();
      } else {
        return get_attrs_ertr::make_ready_future<attrs_t>(std::move(aset));
      }
    });
  });
}

auto AlienStore::omap_get_values(CollectionRef ch,
                                 const ghobject_t& oid,
                                 const set<string>& keys)
  -> read_errorator::future<omap_values_t>
{
  logger().debug("{}", __func__);
  return seastar::do_with(omap_values_t{}, [=] (auto &values) {
    return tp->submit([=, &values] {
      auto c = static_cast<AlienCollection*>(ch.get());
      return store->omap_get_values(c->collection, oid, keys,
		                    reinterpret_cast<map<string, bufferlist>*>(&values));
    }).then([&values] (int r) -> read_errorator::future<omap_values_t> {
      if (r == -ENOENT) {
        return crimson::ct_error::enoent::make();
      } else {
        assert(r == 0);
        return read_errorator::make_ready_future<omap_values_t>(std::move(values));
      }
    });
  });
}

auto AlienStore::omap_get_values(CollectionRef ch,
                                 const ghobject_t &oid,
                                 const std::optional<string> &start)
  -> read_errorator::future<std::tuple<bool, omap_values_t>>
{
  logger().debug("{} with_start", __func__);
  return seastar::do_with(omap_values_t{}, [=] (auto &values) {
    return tp->submit([=, &values] {
      auto c = static_cast<AlienCollection*>(ch.get());
      return store->omap_get_values(c->collection, oid, start,
		                    reinterpret_cast<map<string, bufferlist>*>(&values));
    }).then([&values] (int r)
      -> read_errorator::future<std::tuple<bool, omap_values_t>> {
      if (r == -ENOENT) {
        return crimson::ct_error::enoent::make();
      } else if (r < 0){
        logger().error("omap_get_values(start): {}", r);
        return crimson::ct_error::input_output_error::make();
      } else {
        return read_errorator::make_ready_future<std::tuple<bool, omap_values_t>>(
          std::make_tuple(true, std::move(values)));
      }
    });
  });
}

seastar::future<> AlienStore::do_transaction(CollectionRef ch,
                                             ceph::os::Transaction&& txn)
{
  logger().debug("{}", __func__);
  auto id = seastar::this_shard_id();
  auto done = seastar::promise<>();
  return seastar::do_with(
    std::move(txn),
    std::move(done),
    [this, ch, id] (auto &txn, auto &done) {
      return seastar::with_gate(transaction_gate, [this, ch, id, &txn, &done] {
	return tp_mutex.lock().then ([this, ch, id, &txn, &done] {
	  Context *crimson_wrapper =
	    ceph::os::Transaction::collect_all_contexts(txn);
	  return tp->submit([this, ch, id, crimson_wrapper, &txn, &done] {
	    txn.register_on_commit(new OnCommit(id, done, crimson_wrapper, txn));
	    auto c = static_cast<AlienCollection*>(ch.get());
	    return store->queue_transaction(c->collection, std::move(txn));
	  });
	}).then([this, &done] (int r) {
	  assert(r == 0);
	  tp_mutex.unlock();
	  return done.get_future();
	});
      });
    });
}

seastar::future<> AlienStore::write_meta(const std::string& key,
                                         const std::string& value)
{
  logger().debug("{}", __func__);
  return tp->submit([=] {
    return store->write_meta(key, value);
  }).then([] (int r) {
    assert(r == 0);
    return seastar::make_ready_future<>();
  });
}

seastar::future<std::tuple<int, std::string>>
AlienStore::read_meta(const std::string& key)
{
  logger().debug("{}", __func__);
  return tp->submit([this, key] {
    std::string value;
    int r = store->read_meta(key, &value);
    if (r > 0) {
      value.resize(r);
      boost::algorithm::trim_right_if(value,
        [] (unsigned char c) {return isspace(c);});
    } else {
      value.clear();
    }
    return std::make_pair(r, value);
  }).then([] (auto entry) {
    return seastar::make_ready_future<std::tuple<int, std::string>>(
      std::move(entry));
  });
}

uuid_d AlienStore::get_fsid() const
{
  logger().debug("{}", __func__);
  return store->get_fsid();
}

seastar::future<store_statfs_t> AlienStore::stat() const
{
  logger().info("{}", __func__);
  return seastar::do_with(store_statfs_t{}, [this] (store_statfs_t &st) {
    return tp->submit([this, &st] {
      return store->statfs(&st, nullptr);
    }).then([&st] (int r) {
      assert(r == 0);
      return seastar::make_ready_future<store_statfs_t>(std::move(st));
    });
  });
}

unsigned AlienStore::get_max_attr_name_length() const
{
  logger().info("{}", __func__);
  return 256;
}

seastar::future<struct stat> AlienStore::stat(
  CollectionRef ch,
  const ghobject_t& oid)
{
  return seastar::do_with((struct stat){}, [this, ch, oid](auto& st) {
    return tp->submit([this, ch, oid, &st] {
      auto c = static_cast<AlienCollection*>(ch.get());
      store->stat(c->collection, oid, &st);
      return st;
    });
  });
}

auto AlienStore::omap_get_header(CollectionRef ch,
                                 const ghobject_t& oid)
  -> read_errorator::future<ceph::bufferlist>
{
  return seastar::do_with(ceph::bufferlist(), [=](auto& bl) {
    return tp->submit([=, &bl] {
      auto c = static_cast<AlienCollection*>(ch.get());
      return store->omap_get_header(c->collection, oid, &bl);
    }).then([&bl] (int r) -> read_errorator::future<ceph::bufferlist> {
      if (r == -ENOENT) {
        return crimson::ct_error::enoent::make();
      } else if (r < 0) {
        logger().error("omap_get_header: {}", r);
        return crimson::ct_error::input_output_error::make();
      } else {
        return read_errorator::make_ready_future<ceph::bufferlist>(std::move(bl));
      }
    });
  });
}

seastar::future<std::map<uint64_t, uint64_t>> AlienStore::fiemap(
  CollectionRef ch,
  const ghobject_t& oid,
  uint64_t off,
  uint64_t len)
{
  return seastar::do_with(std::map<uint64_t, uint64_t>(), [=](auto& destmap) {
    return tp->submit([=, &destmap] {
      auto c = static_cast<AlienCollection*>(ch.get());
      return store->fiemap(c->collection, oid, off, len, destmap);
    }).then([&destmap] (int i) {
      return seastar::make_ready_future
	  <std::map<uint64_t, uint64_t>>
	  (std::move(destmap));
    });
  });
}

seastar::future<FuturizedStore::OmapIteratorRef> AlienStore::get_omap_iterator(
  CollectionRef ch,
  const ghobject_t& oid)
{
  return tp->submit([=] {
    auto c = static_cast<AlienCollection*>(ch.get());
    auto iter = store->get_omap_iterator(c->collection, oid);
    return FuturizedStore::OmapIteratorRef(
	      new AlienStore::AlienOmapIterator(iter,
						this));
  });
}

//TODO: each iterator op needs one submit, this is not efficient,
//      needs further optimization.
seastar::future<> AlienStore::AlienOmapIterator::seek_to_first()
{
  return store->tp->submit([=] {
    return iter->seek_to_first();
  }).then([] (int r) {
    assert(r == 0);
    return seastar::now();
  });
}

seastar::future<> AlienStore::AlienOmapIterator::upper_bound(
  const std::string& after)
{
  return store->tp->submit([this, after] {
    return iter->upper_bound(after);
  }).then([] (int r) {
    assert(r == 0);
    return seastar::now();
  });
}

seastar::future<> AlienStore::AlienOmapIterator::lower_bound(
  const std::string& to)
{
  return store->tp->submit([this, to] {
    return iter->lower_bound(to);
  }).then([] (int r) {
    assert(r == 0);
    return seastar::now();
  });
}

seastar::future<> AlienStore::AlienOmapIterator::next()
{
  return store->tp->submit([this] {
    return iter->next();
  }).then([] (int r) {
    assert(r == 0);
    return seastar::now();
  });
}

bool AlienStore::AlienOmapIterator::valid() const
{
  return iter->valid();
}

std::string AlienStore::AlienOmapIterator::key()
{
  return iter->key();
}

seastar::future<std::string> AlienStore::AlienOmapIterator::tail_key()
{
  return store->tp->submit([this] {
    return iter->tail_key();
  });
}

ceph::buffer::list AlienStore::AlienOmapIterator::value()
{
  return iter->value();
}

int AlienStore::AlienOmapIterator::status() const
{
  return iter->status();
}

}