]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/objectstore/test_idempotent.cc
buildsys: change download over to reef release
[ceph.git] / ceph / src / test / objectstore / test_idempotent.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15#include <iostream>
f67539c2 16#include <iterator>
7c673cae
FG
17#include <sstream>
18#include <boost/scoped_ptr.hpp>
19#include "os/filestore/FileStore.h"
20#include "global/global_init.h"
21#include "common/ceph_argparse.h"
22#include "common/debug.h"
23#include "test/common/ObjectContents.h"
24#include "FileStoreTracker.h"
25#include "kv/KeyValueDB.h"
26#include "os/ObjectStore.h"
27
20effc67
TL
28using namespace std;
29
7c673cae
FG
30void usage(const string &name) {
31 std::cerr << "Usage: " << name << " [new|continue] store_path store_journal db_path"
32 << std::endl;
33}
34
35template <typename T>
36typename T::iterator rand_choose(T &cont) {
f67539c2
TL
37 if (std::empty(cont)) {
38 return std::end(cont);
7c673cae 39 }
f67539c2 40 return std::next(std::begin(cont), rand() % cont.size());
7c673cae
FG
41}
42
43int main(int argc, char **argv) {
20effc67 44 auto args = argv_to_vec(argc, argv);
7c673cae 45 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
11fdf7f2
TL
46 CODE_ENVIRONMENT_UTILITY,
47 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
7c673cae 48 common_init_finish(g_ceph_context);
11fdf7f2 49 cct->_conf.apply_changes(nullptr);
7c673cae
FG
50
51 std::cerr << "args: " << args << std::endl;
52 if (args.size() < 4) {
53 usage(argv[0]);
54 return 1;
55 }
56
57 string store_path(args[1]);
58 string store_dev(args[2]);
59 string db_path(args[3]);
60
61 bool start_new = false;
62 if (string(args[0]) == string("new")) start_new = true;
63
64 KeyValueDB *_db = KeyValueDB::create(g_ceph_context, "leveldb", db_path);
11fdf7f2 65 ceph_assert(!_db->create_and_open(std::cerr));
7c673cae
FG
66 boost::scoped_ptr<KeyValueDB> db(_db);
67 boost::scoped_ptr<ObjectStore> store(new FileStore(cct.get(), store_path,
68 store_dev));
69
7c673cae 70 coll_t coll(spg_t(pg_t(0,12),shard_id_t::NO_SHARD));
11fdf7f2 71 ObjectStore::CollectionHandle ch;
7c673cae
FG
72
73 if (start_new) {
74 std::cerr << "mkfs" << std::endl;
11fdf7f2 75 ceph_assert(!store->mkfs());
7c673cae 76 ObjectStore::Transaction t;
11fdf7f2
TL
77 ceph_assert(!store->mount());
78 ch = store->create_new_collection(coll);
7c673cae 79 t.create_collection(coll, 0);
11fdf7f2 80 store->queue_transaction(ch, std::move(t));
7c673cae 81 } else {
11fdf7f2
TL
82 ceph_assert(!store->mount());
83 ch = store->open_collection(coll);
7c673cae
FG
84 }
85
86 FileStoreTracker tracker(store.get(), db.get());
87
88 set<string> objects;
89 for (unsigned i = 0; i < 10; ++i) {
90 stringstream stream;
91 stream << "Object_" << i;
92 tracker.verify(coll, stream.str(), true);
93 objects.insert(stream.str());
94 }
95
96 while (1) {
97 FileStoreTracker::Transaction t;
98 for (unsigned j = 0; j < 100; ++j) {
99 int val = rand() % 100;
100 if (val < 30) {
101 t.write(coll, *rand_choose(objects));
102 } else if (val < 60) {
103 t.clone(coll, *rand_choose(objects),
104 *rand_choose(objects));
105 } else if (val < 70) {
106 t.remove(coll, *rand_choose(objects));
107 } else {
108 t.clone_range(coll, *rand_choose(objects),
109 *rand_choose(objects));
110 }
111 }
112 tracker.submit_transaction(t);
113 tracker.verify(coll, *rand_choose(objects));
114 }
115 return 0;
116}