#include "include/types.h"
-#include "common/Timer.h"
-
#include <list>
#include <set>
#include <ext/hash_map>
class FakeStoreCollections {
private:
Mutex faker_lock;
+ ObjectStore *store;
hash_map<coll_t, set<object_t> > fakecollections;
public:
+ FakeStoreCollections(ObjectStore *s) : store(s) {}
+
// faked collections
int list_collections(list<coll_t>& ls) {
faker_lock.Lock();
Context *onsafe=0) {
faker_lock.Lock();
fakecollections[c].size();
- if (onsafe) g_timer.add_event_after(2.0,onsafe);
+ if (onsafe) store->sync(onsafe);
faker_lock.Unlock();
return 0;
}
if (fakecollections.count(c)) {
fakecollections.erase(c);
//fakecattr.erase(c);
- if (onsafe) g_timer.add_event_after(2.0,onsafe);
+ if (onsafe) store->sync(onsafe);
} else
r = -1;
faker_lock.Unlock();
Context *onsafe=0) {
faker_lock.Lock();
fakecollections[c].insert(o);
- if (onsafe) g_timer.add_event_after(2.0,onsafe);
+ if (onsafe) store->sync(onsafe);
faker_lock.Unlock();
return 0;
}
Context *onsafe=0) {
faker_lock.Lock();
fakecollections[c].erase(o);
- if (onsafe) g_timer.add_event_after(2.0,onsafe);
+ if (onsafe) store->sync(onsafe);
faker_lock.Unlock();
return 0;
}
};
Mutex faker_lock;
+ ObjectStore *store;
hash_map<object_t, FakeAttrSet> fakeoattrs;
hash_map<coll_t, FakeAttrSet> fakecattrs;
public:
+ FakeStoreAttrs(ObjectStore *s) : store(s) {}
+
int setattr(object_t oid, const char *name,
const void *value, size_t size,
Context *onsafe=0) {
faker_lock.Lock();
int r = fakeoattrs[oid].setattr(name, value, size);
- if (onsafe) g_timer.add_event_after(2.0,onsafe);
+ if (onsafe) store->sync(onsafe);
faker_lock.Unlock();
return r;
}
Context *onsafe=0) {
faker_lock.Lock();
int r = fakeoattrs[oid].rmattr(name);
- if (onsafe) g_timer.add_event_after(2.0,onsafe);
+ if (onsafe) store->sync(onsafe);
faker_lock.Unlock();
return r;
}
Context *onsafe=0) {
faker_lock.Lock();
int r = fakecattrs[c].setattr(name, value, size);
- if (onsafe) g_timer.add_event_after(2.0,onsafe);
+ if (onsafe) store->sync(onsafe);
faker_lock.Unlock();
return r;
}
Context *onsafe=0) {
faker_lock.Lock();
int r = fakecattrs[c].rmattr(name);
- if (onsafe) g_timer.add_event_after(2.0,onsafe);
+ if (onsafe) store->sync(onsafe);
faker_lock.Unlock();
return r;
}
-FakeStore::FakeStore(char *base, int whoami)
-{
- this->basedir = base;
- this->whoami = whoami;
-}
int FakeStore::mount()
return r;
}
- /*{
- char name[80];
- sprintf(name,"osd%d.fakestore.threadpool", whoami);
- fsync_threadpool = new ThreadPool<FakeStore, pair<int,Context*> >(name, g_conf.fakestore_syncthreads,
- (void (*)(FakeStore*, pair<int,Context*>*))dofsync,
- this);
- }*/
-
// all okay.
return 0;
}
{
dout(5) << "finalize" << endl;
- // close collections db files
- //close_collections();
-
- //delete fsync_threadpool;
-
if (g_conf.fakestore_dev) {
char cmd[100];
dout(0) << "umounting" << endl;
return ::statfs(mydir.c_str(), buf);
}
-///////////
-/*void FakeStore::do_fsync(int fd, Context *c)
-{
- ::fsync(fd);
- ::close(fd);
- dout(10) << "do_fsync finished on " << fd << " context " << c << endl;
- c->finish(0);
- delete c;
-}*/
-////
-
void FakeStore::get_dir(string& dir) {
char s[30];
sprintf(s, "%d", whoami);
fn = basedir + "/" + s;
// dout(1) << "oname is " << fn << endl;
}
-/*void FakeStore::get_collfn(coll_t c, string &fn) {
- char s[100];
- sprintf(s, "%d/%02llx/%016llx.co", whoami, HASH_FUNC(c), c);
- fn = basedir + "/" + s;
- }*/
dout(1) << "mkfs in " << mydir << endl;
- //close_collections();
// make sure my dir exists
r = ::stat(mydir.c_str(), &st);
string fn;
get_oname(oid,fn);
int r = ::unlink(fn.c_str());
- if (onsafe)
- g_timer.add_event_after((float)g_conf.fakestore_fake_sync,
- onsafe);
+ if (onsafe) sync(onsafe);
return r;
}
string fn;
get_oname(oid,fn);
int r = ::truncate(fn.c_str(), size);
- if (onsafe)
- g_timer.add_event_after((float)g_conf.fakestore_fake_sync,
- onsafe);
+ if (onsafe) sync(onsafe);
return r;
}
return got;
}
-int FakeStore::write(object_t oid,
- off_t offset, size_t len,
- bufferlist& bl,
- bool do_fsync) {
- dout(20) << "write " << oid << " len " << len << " off " << offset << endl;
-
- string fn;
- get_oname(oid,fn);
-
- ::mknod(fn.c_str(), 0644, 0); // in case it doesn't exist yet.
-
- int flags = O_WRONLY;//|O_CREAT;
- if (do_fsync && g_conf.fakestore_writesync) flags |= O_SYNC;
- int fd = ::open(fn.c_str(), flags);
- if (fd < 0) {
- dout(1) << "write couldn't open " << fn.c_str() << " flags " << flags << " errno " << errno << " " << strerror(errno) << endl;
- return fd;
- }
- ::flock(fd, LOCK_EX); // lock for safety
- //::fchmod(fd, 0664);
-
- // seek
- off_t actual = lseek(fd, offset, SEEK_SET);
- int did = 0;
- assert(actual == offset);
-
- // write buffers
- for (list<bufferptr>::iterator it = bl.buffers().begin();
- it != bl.buffers().end();
- it++) {
- int r = ::write(fd, (*it).c_str(), (*it).length());
- if (r > 0)
- did += r;
- else {
- dout(1) << "couldn't write to " << fn.c_str() << " len " << len << " off " << offset << " errno " << errno << " " << strerror(errno) << endl;
- }
- }
-
- if (did < 0) {
- dout(1) << "couldn't write to " << fn.c_str() << " len " << len << " off " << offset << " errno " << errno << " " << strerror(errno) << endl;
- }
-
- // sync to to disk?
- if (do_fsync && g_conf.fakestore_fsync) ::fsync(fd); // fsync or fdatasync?
-
- ::flock(fd, LOCK_UN);
- ::close(fd);
-
- return did;
-}
int FakeStore::write(object_t oid,
off_t offset, size_t len,
::flock(fd, LOCK_UN);
// schedule sync
- if (onsafe) {
- if (g_conf.fakestore_fake_sync) {
- g_timer.add_event_after((float)g_conf.fakestore_fake_sync,
- onsafe);
- ::close(fd);
- } else {
- assert(0); //queue_fsync(fd, onsafe);
- }
- } else {
- ::close(fd);
- }
+ if (onsafe) sync(onsafe);
+
+ ::close(fd);
return did;
}
-
-// ------------------
-// attributes
-
-
-/*
-int FakeStore::setattr(object_t oid, const char *name,
- void *value, size_t size)
-{
- if (g_conf.fakestore_fakeattr) {
- lock.Lock();
- int r = fakeoattrs[oid].setattr(name, value, size);
- lock.Unlock();
- return r;
- } else {
- string fn;
- get_oname(oid, fn);
- int r = setxattr(fn.c_str(), name, value, size, 0);
- if (r == -1)
- cerr << " errno is " << errno << " " << strerror(errno) << endl;
- assert(r == 0);
- return r;
- }
-}
-
-
-int FakeStore::getattr(object_t oid, const char *name,
- void *value, size_t size)
-{
- if (g_conf.fakestore_fakeattr) {
- lock.Lock();
- int r = fakeoattrs[oid].getattr(name, value, size);
- lock.Unlock();
- return r;
- } else {
- string fn;
- get_oname(oid, fn);
- int r = getxattr(fn.c_str(), name, value, size);
- // assert(r == 0);
- return r;
- }
-}
-
-int FakeStore::listattr(object_t oid, char *attrs, size_t size)
-{
- if (g_conf.fakestore_fakeattr) {
- lock.Lock();
- int r = fakeoattrs[oid].listattr(attrs,size);
- lock.Unlock();
- return r;
- } else {
- string fn;
- get_oname(oid, fn);
- return listxattr(fn.c_str(), attrs, size);
+class C_FakeSync : public Context {
+public:
+ Context *c;
+ int *n;
+ C_FakeSync(Context *c_, int *n_) : c(c_), n(n_) {
+ ++*n;
}
-}
-*/
-
-
-
-// ------------------
-// collections
-
-// helpers
-
-
-/*
-int FakeStore::collection_setattr(coll_t cid, const char *name,
- void *value, size_t size)
-{
- int r;
- lock.Lock();
- if (!collections.is_open()) open_collections();
- if (g_conf.fakestore_fakeattr) {
- r = fakecattrs[cid].setattr(name, value, size);
- } else {
- string fn;
- get_collfn(cid,fn);
- r = setxattr(fn.c_str(), name, value, size, 0);
+ void finish(int r) {
+ c->finish(r);
+ --(*n);
+ //cout << "sync, " << *n << " still unsync" << endl;
}
- lock.Unlock();
- return r;
-}
+};
-
-int FakeStore::collection_getattr(coll_t cid, const char *name,
- void *value, size_t size)
+void FakeStore::sync(Context *onsafe)
{
- int r;
- lock.Lock();
- if (!collections.is_open()) open_collections();
- if (g_conf.fakestore_fakeattr) {
- r = fakecattrs[cid].getattr(name, value, size);
+ if (g_conf.fakestore_fake_sync) {
+ g_timer.add_event_after((float)g_conf.fakestore_fake_sync,
+ new C_FakeSync(onsafe, &unsync));
+
} else {
- string fn;
- get_collfn(cid,fn);
- r = getxattr(fn.c_str(), name, value, size);
+ assert(0); // der..no implemented anymore
}
- lock.Unlock();
- return r;
}
-int FakeStore::collection_listattr(coll_t cid, char *attrs, size_t size)
-{
- int r;
- lock.Lock();
- if (!collections.is_open()) open_collections();
- if (g_conf.fakestore_fakeattr) {
- r = fakecattrs[cid].listattr(attrs,size);
- } else {
- string fn;
- get_collfn(cid, fn);
- r = listxattr(fn.c_str(), attrs, size);
- }
- lock.Unlock();
- return r;
-}
-*/
string basedir;
int whoami;
+ int unsync;
+
Mutex lock;
// fns
void wipe_dir(string mydir);
- /*
- // async fsync
- class ThreadPool<class FakeStore, pair<int, class Context*> > *fsync_threadpool;
- void queue_fsync(int fd, class Context *c) {
- fsync_threadpool->put_op(new pair<int, class Context*>(fd,c));
- }
public:
- void do_fsync(int fd, class Context *c);
- static void dofsync(class FakeStore *f, pair<int, class Context*> *af) {
- f->do_fsync(af->first, af->second);
- delete af;
+ FakeStore(char *base, int whoami) : FakeStoreAttrs(this), FakeStoreCollections(this)
+ {
+ this->basedir = base;
+ this->whoami = whoami;
+ unsync = 0;
}
- */
- public:
- FakeStore(char *base, int whoami);
int mount();
int umount();
int read(object_t oid,
off_t offset, size_t len,
bufferlist& bl);
- int write(object_t oid,
- off_t offset, size_t len,
- bufferlist& bl,
- bool fsync);
int write(object_t oid,
off_t offset, size_t len,
bufferlist& bl,
Context *onsafe);
+ void sync(Context *onsafe);
};
#endif
for (list<int>::iterator p = t.ops.begin();
p != t.ops.end();
p++) {
- Context *last = 0;
- if (p == t.ops.end()) last = onsafe;
-
switch (*p) {
case Transaction::OP_READ:
{
off_t offset = t.offsets.front(); t.offsets.pop_front();
size_t len = t.lengths.front(); t.lengths.pop_front();
bufferlist bl = t.bls.front(); t.bls.pop_front();
- write(oid, offset, len, bl, last);
+ write(oid, offset, len, bl, 0);
}
break;
{
object_t oid = t.oids.front(); t.oids.pop_front();
off_t len = t.offsets.front(); t.offsets.pop_front();
- truncate(oid, len, last);
+ truncate(oid, len, 0);
}
break;
case Transaction::OP_REMOVE:
{
object_t oid = t.oids.front(); t.oids.pop_front();
- remove(oid, last);
+ remove(oid, 0);
}
break;
object_t oid = t.oids.front(); t.oids.pop_front();
const char *attrname = t.attrnames.front(); t.attrnames.pop_front();
pair<const void*,int> attrval = t.attrvals.front(); t.attrvals.pop_front();
- setattr(oid, attrname, attrval.first, attrval.second, last);
+ setattr(oid, attrname, attrval.first, attrval.second, 0);
}
break;
case Transaction::OP_SETATTRS:
{
object_t oid = t.oids.front(); t.oids.pop_front();
map<string,bufferptr> *pattrset = t.pattrsets.front(); t.pattrsets.pop_front();
- setattrs(oid, *pattrset, last);
+ setattrs(oid, *pattrset, 0);
}
break;
{
object_t oid = t.oids.front(); t.oids.pop_front();
const char *attrname = t.attrnames.front(); t.attrnames.pop_front();
- rmattr(oid, attrname, last);
+ rmattr(oid, attrname, 0);
}
break;
case Transaction::OP_MKCOLL:
{
coll_t cid = t.cids.front(); t.cids.pop_front();
- create_collection(cid, last);
+ create_collection(cid, 0);
}
break;
case Transaction::OP_RMCOLL:
{
coll_t cid = t.cids.front(); t.cids.pop_front();
- destroy_collection(cid, last);
+ destroy_collection(cid, 0);
}
break;
{
coll_t cid = t.cids.front(); t.cids.pop_front();
object_t oid = t.oids.front(); t.oids.pop_front();
- collection_add(cid, oid, last);
+ collection_add(cid, oid, 0);
}
break;
{
coll_t cid = t.cids.front(); t.cids.pop_front();
object_t oid = t.oids.front(); t.oids.pop_front();
- collection_remove(cid, oid, last);
+ collection_remove(cid, oid, 0);
}
break;
coll_t cid = t.cids.front(); t.cids.pop_front();
const char *attrname = t.attrnames.front(); t.attrnames.pop_front();
pair<const void*,int> attrval = t.attrvals.front(); t.attrvals.pop_front();
- collection_setattr(cid, attrname, attrval.first, attrval.second, last);
+ collection_setattr(cid, attrname, attrval.first, attrval.second, 0);
}
break;
{
coll_t cid = t.cids.front(); t.cids.pop_front();
const char *attrname = t.attrnames.front(); t.attrnames.pop_front();
- collection_rmattr(cid, attrname, last);
+ collection_rmattr(cid, attrname, 0);
}
break;
assert(0);
}
}
+
+ if (onsafe) sync(onsafe);
+
return 0; // FIXME count errors
}
void *value, size_t size) {return 0;} //= 0;
virtual int collection_listattr(coll_t cid, char *attrs, size_t size) {return 0;} //= 0;
+ virtual void sync(Context *onsync) {};
virtual void sync() {};
+
virtual void _fake_writes(bool b) {};
virtual void _get_frag_stat(FragmentationStat& st) {};