Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / objectstore / test_idempotent.cc
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>
16 #include <sstream>
17 #include <boost/scoped_ptr.hpp>
18 #include "os/filestore/FileStore.h"
19 #include "global/global_init.h"
20 #include "common/ceph_argparse.h"
21 #include "common/debug.h"
22 #include "test/common/ObjectContents.h"
23 #include "FileStoreTracker.h"
24 #include "kv/KeyValueDB.h"
25 #include "os/ObjectStore.h"
26
27 void usage(const string &name) {
28   std::cerr << "Usage: " << name << " [new|continue] store_path store_journal db_path"
29             << std::endl;
30 }
31
32 template <typename T>
33 typename T::iterator rand_choose(T &cont) {
34   if (cont.size() == 0) {
35     return cont.end();
36   }
37   int index = rand() % cont.size();
38   typename T::iterator retval = cont.begin();
39
40   for (; index > 0; --index) ++retval;
41   return retval;
42 }
43
44 int main(int argc, char **argv) {
45   vector<const char*> args;
46   argv_to_vec(argc, (const char **)argv, args);
47
48   auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
49                          CODE_ENVIRONMENT_UTILITY, 0);
50   common_init_finish(g_ceph_context);
51   g_ceph_context->_conf->apply_changes(NULL);
52
53   std::cerr << "args: " << args << std::endl;
54   if (args.size() < 4) {
55     usage(argv[0]);
56     return 1;
57   }
58
59   string store_path(args[1]);
60   string store_dev(args[2]);
61   string db_path(args[3]);
62
63   bool start_new = false;
64   if (string(args[0]) == string("new")) start_new = true;
65
66   KeyValueDB *_db = KeyValueDB::create(g_ceph_context, "leveldb", db_path);
67   assert(!_db->create_and_open(std::cerr));
68   boost::scoped_ptr<KeyValueDB> db(_db);
69   boost::scoped_ptr<ObjectStore> store(new FileStore(cct.get(), store_path,
70                                                      store_dev));
71
72   ObjectStore::Sequencer osr(__func__);
73   coll_t coll(spg_t(pg_t(0,12),shard_id_t::NO_SHARD));
74
75   if (start_new) {
76     std::cerr << "mkfs" << std::endl;
77     assert(!store->mkfs());
78     ObjectStore::Transaction t;
79     assert(!store->mount());
80     t.create_collection(coll, 0);
81     store->apply_transaction(&osr, std::move(t));
82   } else {
83     assert(!store->mount());
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 }