[Patch 1/2] tabled: fix objid when less than 500

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



When we start over an empty directory, we start with ID 1 in memory and
assume that eventually it will be written out and all will be ok.
Unfortunately, it's wrong. If tabled is interrupted before it created
500 objects, the counter is not written and so objects get confused.
So, force-write the new counter.

I also roll an API change to objid_xxx that I carried for a while.
It permits the objid_xxx to be used where tabled.h is not included,
for example in tdbadm.

Signed-off-by: Pete Zaitcev <zaitcev@xxxxxxxxxx>

---
 include/tdb.h   |    4 ++
 server/object.c |    2 -
 server/server.c |    2 -
 server/tabled.h |    3 --
 server/util.c   |   67 ++++++++++++++++++++++++++--------------------
 5 files changed, 45 insertions(+), 33 deletions(-)

commit 9b0a6c22661095962b3b562a5d9f24d914d0b1e9
Author: Master <zaitcev@xxxxxxxxxxxxxxxxxxx>
Date:   Thu Oct 22 22:27:07 2009 -0600

    Fix objid and incorporate small API change.

diff --git a/include/tdb.h b/include/tdb.h
index e12d00e..8895704 100644
--- a/include/tdb.h
+++ b/include/tdb.h
@@ -122,4 +122,8 @@ extern int tdb_up(struct tabledb *tdb, unsigned int open_flags);
 extern void tdb_down(struct tabledb *tdb);
 extern void tdb_fini(struct tabledb *tdb);
 
+/* util.c */
+uint64_t objid_next(uint64_t *state, struct tabledb *tdbp);
+int objid_init(uint64_t *state, struct tabledb *tdbp);
+
 #endif /* __TDB_H__ */
diff --git a/server/object.c b/server/object.c
index 19b90ec..8fc5c6f 100644
--- a/server/object.c
+++ b/server/object.c
@@ -773,7 +773,7 @@ bool object_put_body(struct client *cli, const char *user, const char *bucket,
 		return cli_err(cli, InternalError);
 	}
 
-	objid = objid_next();
+	objid = objid_next(&tabled_srv.object_count, &tdb);
 
 	rc = open_chunks(&cli->out_ch, &tabled_srv.all_stor,
 			 cli, objid, content_len);
diff --git a/server/server.c b/server/server.c
index f7ea446..2a504e2 100644
--- a/server/server.c
+++ b/server/server.c
@@ -1690,7 +1690,7 @@ static void tdb_state_process(enum st_tdb new_state)
 		if (tdb_up(&tdb, db_flags))
 			return;
 
-		if (objid_init()) {
+		if (objid_init(&tabled_srv.object_count, &tdb)) {
 			tdb_down(&tdb);
 			return;
 		}
diff --git a/server/tabled.h b/server/tabled.h
index 91eb025..d10835e 100644
--- a/server/tabled.h
+++ b/server/tabled.h
@@ -295,9 +295,6 @@ extern void md5str(const unsigned char *digest, char *outstr);
 extern void req_sign(struct http_req *req, const char *bucket, const char *key,
 	      char *b64hmac_out);
 
-uint64_t objid_next(void);
-int objid_init(void);
-
 /* server.c */
 extern int debugging;
 extern struct server tabled_srv;
diff --git a/server/util.c b/server/util.c
index 508b517..51866e7 100644
--- a/server/util.c
+++ b/server/util.c
@@ -188,10 +188,10 @@ void md5str(const unsigned char *digest, char *outstr)
 	outstr[MD5_DIGEST_LENGTH * 2] = 0;
 }
 
-uint64_t objid_next(void)
+uint64_t objid_next(uint64_t *obj_count, struct tabledb *tdbp)
 {
-	DB_ENV *dbenv = tdb.env;
-	DB *oids = tdb.oids;
+	DB_ENV *dbenv = tdbp->env;
+	DB *oids = tdbp->oids;
 	DB_TXN *txn = NULL;
 	DBT pkey, pval;
 	int recno;
@@ -201,7 +201,7 @@ uint64_t objid_next(void)
 
 	recno = 1;
 
-	objcount = ++tabled_srv.object_count;
+	objcount = ++(*obj_count);
 	if (objcount % OBJID_STEP != 0)
 		return objcount;
 
@@ -246,10 +246,10 @@ err_out_begin:
  * We could auto-init, but the explicit initialization makes aborts
  * more debuggable and less unexpected, as they happen before requests come.
  */
-int objid_init(void)
+int objid_init(uint64_t *obj_count, struct tabledb *tdbp)
 {
-	DB_ENV *dbenv = tdb.env;
-	DB *oids = tdb.oids;
+	DB_ENV *dbenv = tdbp->env;
+	DB *oids = tdbp->oids;
 	DB_TXN *txn = NULL;
 	DBT pkey, pval;
 	int recno;
@@ -265,6 +265,7 @@ int objid_init(void)
 
 	memset(&pval, 0, sizeof(pval));
 	pval.data = &cntbuf;
+	pval.size = sizeof(uint64_t);
 	pval.ulen = sizeof(uint64_t);
 	pval.flags = DB_DBT_USERMEM;
 
@@ -277,39 +278,49 @@ int objid_init(void)
 
 	/* read existing counter, if any */
 	rc = oids->get(oids, txn, &pkey, &pval, DB_RMW);
-	if (rc == DB_NOTFOUND) {
+	if (rc) {
+		if (rc != DB_NOTFOUND) {
+			applog(LOG_ERR, "objid_init get error %d", rc);
+			txn->abort(txn);
+			return -1;
+		}
 		objcount = 1;
-	} else if (rc) {
-		applog(LOG_ERR, "objid_init get error %d", rc);
-		txn->abort(txn);
-		return -1;
 	} else {
 		if (pval.size != sizeof(uint64_t)) {
 			applog(LOG_ERR, "objid_init got size %d", pval.size);
 			txn->abort(txn);
 			return -1;
 		}
+
 		objcount = GUINT64_FROM_LE(cntbuf);
 		if (debugging)
 			applog(LOG_INFO, "objid_init initial %llX",
 			       (unsigned long long) objcount);
 		objcount += OBJID_STEP;
+	}
 
-		/*
-		 * Commit new step block for two reasons:
-		 *  - if we crash before next step commit
-		 *  - better verify now that writing IDs works ok
-		 */
-		cntbuf = GUINT64_TO_LE(objcount);
-
-		rc = oids->put(oids, txn, &pkey, &pval, 0);
-		if (rc) {
-			dbenv->err(dbenv, rc, "oids->put");
-			rc = txn->abort(txn);
-			if (rc)
-				dbenv->err(dbenv, rc, "DB_ENV->txn_abort");
-			return -1;
-		}
+	/*
+	 * Commit new step block for two reasons:
+	 *  - if we crash before next step commit
+	 *  - better verify now that writing IDs works ok
+	 */
+	cntbuf = GUINT64_TO_LE(objcount);
+
+	memset(&pkey, 0, sizeof(pkey));
+	pkey.data = &recno;
+	pkey.size = sizeof(recno);
+
+	memset(&pval, 0, sizeof(pval));
+	pval.data = &cntbuf;
+	pval.size = sizeof(uint64_t);
+
+	rc = oids->put(oids, txn, &pkey, &pval, 0);
+	if (rc) {
+		dbenv->err(dbenv, rc, "oids->put");
+		rc = txn->abort(txn);
+		if (rc)
+			dbenv->err(dbenv, rc, "DB_ENV->txn_abort");
+		return -1;
 	}
 
 	rc = txn->commit(txn, 0);
@@ -323,7 +334,7 @@ int objid_init(void)
 		       (unsigned long long) objcount);
 		return -1;
 	}
-	tabled_srv.object_count = objcount;
+	*obj_count = objcount;
 	return 0;
 }
 
--
To unsubscribe from this list: send the line "unsubscribe hail-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Fedora Clound]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux