ceph rgw:object tagging实现
object tagging也是L版本新增加的特性,在L版本的release notes中描述如下:
S3 Object Tagging API has been added; while APIs are supported for GET/PUT/DELETE object tags and in PUT object API, there is no support for tags on Policies & Lifecycle yet
主要关注点是
- PUT/GET/DELETE Object Tags的实现和使用情况
- 与lifecycle或policy结合的功能的实现情况
实现
rgw对tagging的实现比较简单,一个rgw object的tags就存储在这个rgw object对应的rados的header对象的xattr中。使用时也是直接从对应对象的xattr查询。相关代码片段见附录。
关注点1:PUT/GET/DELETE Object Tags操作
创建用于测试的bucket和object:testbukcet/testobj。
用curl模拟PUT/GET/DELETE Object Tags的http请求。
PUT Object Tags
将tagging信息以xml格式存在文件中,使用PUT方法设置object tags。
文档:http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUTtagging.html
#!/bin/bash
access_key="0555b35654ad1656d804"
secret_key="h7GhxuBLTrlhVUyxSPUKUV8r/2EI4ngqJxD7iBdBYLhwluN30JaT3Q=="
date=$(date -R -u)
string_to_sign="PUT\n\n\n${date}\n/testbucket/testobj?tagging"
signature=$(echo -en ${string_to_sign} | openssl sha1 -hmac ${secret_key} -binary | base64)
content_length="126"
curl "http://127.0.0.1:8000/testbucket/testobj?tagging" \
-H "Content-Length: ${content_length}" \
-H "Date: ${date}" \
-H "User-Agent: cmh-test" \
-H "Authorization: AWS ${access_key}:${signature}" \
-H "Expect:" \
-X PUT -T ./testtagging.xml -v
GET Object Tags
文档:http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGETtagging.html
#!/bin/bash
access_key="0555b35654ad1656d804"
secret_key="h7GhxuBLTrlhVUyxSPUKUV8r/2EI4ngqJxD7iBdBYLhwluN30JaT3Q=="
date=$(date -R -u)
string_to_sign="GET\n\n\n${date}\n/testbucket/testobj?tagging"
signature=$(echo -en ${string_to_sign} | openssl sha1 -hmac ${secret_key} -binary | base64)
curl "http://127.0.0.1:8000/testbucket/testobj?tagging" \
-H "Date: ${date}" \
-H "User-Agent: cmh-test" \
-H "Authorization: AWS ${access_key}:${signature}" \
-H "Expect:" \
-X GET -v
DELETE Object Tags
文档:http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectDELETEtagging.html
#!/bin/bash
access_key="0555b35654ad1656d804"
secret_key="h7GhxuBLTrlhVUyxSPUKUV8r/2EI4ngqJxD7iBdBYLhwluN30JaT3Q=="
date=$(date -R -u)
string_to_sign="DELETE\n\n\n${date}\n/testbucket/testobj?tagging"
signature=$(echo -en ${string_to_sign} | openssl sha1 -hmac ${secret_key} -binary | base64)
curl "http://127.0.0.1:8000/testbucket/testobj?tagging" \
-H "Date: ${date}" \
-H "User-Agent: cmh-test" \
-H "Authorization: AWS ${access_key}:${signature}" \
-H "Expect:" \
-X DELETE -v
根据前面所说,tagging的内容是存储在testobj对应的rados 对象的xattr中的,在增删tagging前后,可以使用下面的命令看查看testobj的xattr增改。
$ ./bin/rados -p default.rgw.buckets.data listxattr 38d08ed7-3883-49de-ab89-0dea7c8c960f.4162.2_testobj
关注点2:lifecycle根据tag进行对象淘汰
这部分在L版本的release notes的描述中,是不被支持的。但在master版本的代码中是有写的,具体见附录最后“lifecycle机制中的tagging应用”。
附录:代码片段
RGWPutObjTags::execute()
设置obj tagging的代码,可以看到,代码将tag信息以RGW_ATTR_TAGS
作为key,存储在对应object的xattr中。
void RGWPutObjTags::execute(){
op_ret = get_params();
if (op_ret < 0)
return;
if (s->object.empty()){
op_ret= -EINVAL; // we only support tagging on existing objects
return;
}
rgw_obj obj;
obj = rgw_obj(s->bucket, s->object);
store->set_atomic(s->obj_ctx, obj);
op_ret = modify_obj_attr(store, s, obj, RGW_ATTR_TAGS, tags_bl);
if (op_ret == -ECANCELED){
op_ret = -ERR_TAG_CONFLICT;
}
}
RGWGetObjTags::execute()
获取tag的代码,可以看到,代码获取了对应object的xattr,然后通过RGW_ATTR_TAGS作为key读取。
void RGWGetObjTags::execute(){
rgw_obj obj;
map<string,bufferlist> attrs;
obj = rgw_obj(s->bucket, s->object);
store->set_atomic(s->obj_ctx, obj);
op_ret = get_obj_attrs(store, s, obj, attrs);
if (op_ret < 0) {
ldout(s->cct, 0) << "ERROR: failed to get obj attrs, obj=" << obj
<< " ret=" << op_ret << dendl;
return;
}
auto tags = attrs.find(RGW_ATTR_TAGS);
if(tags != attrs.end()){
has_tags = true;
tags_bl.append(tags->second);
}
send_response_data(tags_bl);
}
RGWDeleteObjTags::execute()
void RGWDeleteObjTags::execute(){
if (s->object.empty())
return;
rgw_obj obj;
obj = rgw_obj(s->bucket, s->object);
store->set_atomic(s->obj_ctx, obj);
map <string, bufferlist> attrs;
map <string, bufferlist> rmattr;
bufferlist bl;
rmattr[RGW_ATTR_TAGS] = bl;
op_ret = store->set_attrs(s->obj_ctx, s->bucket_info, obj, attrs, &rmattr);
}
lifecycle机制中的tagging应用
在worker线程遍历lifecycle对应的bucket下的objects时,其中有一步就是查询该对象xattr中的tags,与lifecycle规则中的tags进行对比。
bucket_lc_process
函数代码片段:
if (prefix_iter->second.obj_tags != boost::none) {
bufferlist tags_bl;
// 从object的xattr中获得tags
int ret = read_obj_tags(store, bucket_info, obj, rctx, tags_bl);
// read_obj_tags的代码实现,可以看到,它做的就是获取对象的xattr,并使用RGW_ATTR_TAGS作为key获得tags对应的entry。
/*
static int read_obj_tags(RGWRados *store, RGWBucketInfo& bucket_info, rgw_obj& obj, RGWObjectCtx& ctx, bufferlist& tags_bl)
{
RGWRados::Object op_target(store, bucket_info, ctx, obj);
RGWRados::Object::Read read_op(&op_target);
return read_op.get_attr(RGW_ATTR_TAGS, tags_bl);
}
*/
RGWObjTags dest_obj_tags;
try {
auto iter = tags_bl.begin();
// 将bufferlist中的tags信息decode到dest_obj_tags中
dest_obj_tags.decode(iter);
} catch (buffer::error& err) {
ldout(cct,0) << "ERROR: caught buffer::error, couldn't decode TagSet" << dendl;
return -EIO;
}
// 判断当前遍历的object的tags和lifecycle中指定的tags是否相同,如果不相同则跳过该object,否则进行后续的其他验证和删除操作
if (!includes(dest_obj_tags.get_tags().begin(),
dest_obj_tags.get_tags().end(),
prefix_iter->second.obj_tags->get_tags().begin(),
prefix_iter->second.obj_tags->get_tags().end())){
ldout(cct, 20) << __func__ << "() skipping obj " << key << " as tags do not match" << dendl;
continue;
}
}