Skip to content

Commit

Permalink
Merge pull request #5721: RGW Swift API: X-Trans-Id header is wrongly…
Browse files Browse the repository at this point in the history
… formatted

Reviewed-by: Loic Dachary <ldachary@redhat.com>
  • Loading branch information
ldachary committed Aug 31, 2015
2 parents a907059 + da00bed commit 52662ce
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/rgw/rgw_common.h
Expand Up @@ -1057,6 +1057,8 @@ struct req_state {

string req_id;

string trans_id;

req_info info;

req_state(CephContext *_cct, class RGWEnv *e);
Expand Down
3 changes: 2 additions & 1 deletion src/rgw/rgw_main.cc
Expand Up @@ -555,8 +555,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);

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;
Expand Down
2 changes: 2 additions & 0 deletions src/rgw/rgw_rados.cc
Expand Up @@ -1520,6 +1520,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) {
Expand Down
29 changes: 29 additions & 0 deletions src/rgw/rgw_rados.h
Expand Up @@ -1250,6 +1250,7 @@ class RGWRados

string region_name;
string zone_name;
string trans_id_suffix;

RGWQuotaHandler *quota_handler;

Expand Down Expand Up @@ -2069,6 +2070,34 @@ class RGWRados
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;
Expand Down
12 changes: 12 additions & 0 deletions src/rgw/rgw_rest.cc
Expand Up @@ -492,10 +492,22 @@ 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: %s\r\n", s->trans_id.c_str());
}
else {
s->cio->print("x-amz-request-id: %s\r\n", s->trans_id.c_str());
}
}

void end_header(struct req_state *s, RGWOp *op, const char *content_type, const int64_t proposed_content_length)
{
string ctype;

dump_trans_id(s);

if (op) {
dump_access_control(s, op);
}
Expand Down

0 comments on commit 52662ce

Please sign in to comment.