]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: rework X-Trans-Id header to be conform with Swift API. 5047/head
authorRadoslaw Zarzynski <rzarzynski@mirantis.com>
Thu, 6 Aug 2015 13:52:58 +0000 (15:52 +0200)
committerRadoslaw Zarzynski <rzarzynski@mirantis.com>
Thu, 6 Aug 2015 14:27:04 +0000 (16:27 +0200)
Fixes: #12108
Signed-off-by: Radoslaw Zarzynski <rzarzynski@mirantis.com>
src/rgw/rgw_common.cc
src/rgw/rgw_common.h
src/rgw/rgw_main.cc
src/rgw/rgw_rados.cc
src/rgw/rgw_rados.h
src/rgw/rgw_rest.cc

index 57bedc3ef52feccf3d84c92e3e1d6cd76a728a53..682f7fb46763429e791c4e427965f778ab237be3 100644 (file)
@@ -184,18 +184,6 @@ req_state::~req_state() {
   delete object_acl;
 }
 
-void req_state::gen_trans_id()
-{
-  char buf[256];
-  timeval timetest;
-  gettimeofday(&timetest, NULL);
-  if (strftime(buf, sizeof(buf), "%Y%m%d:%H%M%S",gmtime(&(timetest.tv_sec))) ==  0)
-    return;
-
-  snprintf(buf + strlen(buf), sizeof(buf)-strlen(buf) ,":%03ld", timetest.tv_usec/1000);
-  trans_id = req_id + "-" + buf;
-}
-
 struct str_len {
   const char *str;
   int len;
index 34c5c12110094814db059e564469a0fd3b9de3c7..9582c8c23c2066a50f08f5e894d04d2bd59b776b 100644 (file)
@@ -1071,8 +1071,6 @@ struct req_state {
 
    req_state(CephContext *_cct, class RGWEnv *e);
    ~req_state();
-
-   void gen_trans_id();
 };
 
 /** Store basic data on an object */
index bbf2ed6e6d488a759344c66da0670929dabc71f5..860300c94b876cfcd39d6e86160929e22c7203a1 100644 (file)
@@ -556,10 +556,9 @@ static int process_request(RGWRados *store, RGWREST *rest, RGWRequest *req, RGWC
   s->obj_ctx = &rados_ctx;
 
   s->req_id = store->unique_id(req->id);
+  s->trans_id = store->unique_trans_id(req->id);
 
-  s->gen_trans_id();
-
-  req->log(s, "initializing");
+  req->log_format(s, "initializing for trans_id = %s", s->trans_id.c_str());
 
   RGWOp *op = NULL;
   int init_error = 0;
index b5c359fa941e111ec851c9659e430a5f39cf4795..1bbf6a169ea3d25bc18f6fdfb05edf847dc680d2 100644 (file)
@@ -1546,6 +1546,8 @@ int RGWRados::init_complete()
   if (ret < 0)
     return ret;
 
+  init_unique_trans_id_deps();
+
   ret = region_map.read(cct, this);
   if (ret < 0) {
     if (ret != -ENOENT) {
index 25caf2ca1234228ea443dce366a73fbe3058b127..a4a2efff6b153910e1c4a9cc8efddd77722e7599 100644 (file)
@@ -1250,6 +1250,7 @@ protected:
 
   string region_name;
   string zone_name;
+  string trans_id_suffix;
 
   RGWQuotaHandler *quota_handler;
 
@@ -2110,6 +2111,34 @@ public:
     return s;
   }
 
+  void init_unique_trans_id_deps() {
+    char buf[16 + 2 + 1]; /* uint64_t needs 16, 2 hyphens add further 2 */
+
+    snprintf(buf, sizeof(buf), "-%llx-", (unsigned long long)instance_id());
+    url_encode(string(buf) + zone.name, trans_id_suffix);
+  }
+
+  /* In order to preserve compability with Swift API, transaction ID
+   * should contain at least 32 characters satisfying following spec:
+   *  - first 21 chars must be in range [0-9a-f]. Swift uses this
+   *    space for storing fragment of UUID obtained through a call to
+   *    uuid4() function of Python's uuid module;
+   *  - char no. 22 must be a hyphen;
+   *  - at least 10 next characters constitute hex-formatted timestamp
+   *    padded with zeroes if necessary. All bytes must be in [0-9a-f]
+   *    range;
+   *  - last, optional part of transaction ID is any url-encoded string
+   *    without restriction on length. */
+  string unique_trans_id(const uint64_t unique_num) {
+    char buf[41]; /* 2 + 21 + 1 + 16 (timestamp can consume up to 16) + 1 */
+    time_t timestamp = time(NULL);
+
+    snprintf(buf, sizeof(buf), "tx%021llx-%010llx",
+             (unsigned long long)unique_num,
+             (unsigned long long)timestamp);
+
+    return string(buf) + trans_id_suffix;
+  }
 
   void get_log_pool_name(string& name) {
     name = zone.log_pool.name;
index a03e31fabf2ec243a600d7222b1ec04b8f6503e3..e47fc7a091d927cd5d9dc5d30e140592f20dab83 100644 (file)
@@ -497,7 +497,7 @@ void dump_start(struct req_state *s)
 void dump_trans_id(req_state *s)
 {
   if (s->prot_flags & RGW_REST_SWIFT) {
-    s->cio->print("X-Trans-Id: ts-%s\r\n", s->trans_id.c_str());
+    s->cio->print("X-Trans-Id: %s\r\n", s->trans_id.c_str());
   }
   else {
     s->cio->print("x-amz-request-id: %s\r\n", s->trans_id.c_str());