Skip to content
Permalink
Browse files
Merge pull request #1494 from wwbmmm/bthread-inherit-span
Support inherit span on bthread create
  • Loading branch information
wwbmmm committed Mar 28, 2022
2 parents 3adb310 + 15ed805 commit 44bad72fa546339a7b0975be3ff94f5935e36af3
Showing 4 changed files with 54 additions and 0 deletions.
@@ -55,3 +55,14 @@ TRACEPRINTF("Hello rpcz %d", 123);
```

这条annotation会按其发生时间插入到对应请求的rpcz中。从这个角度看,rpcz是请求级的日志。如果你用TRACEPRINTF打印了沿路的上下文,便可看到请求在每个阶段停留的时间,牵涉到的数据集和参数。这是个很有用的功能。

## 跨bthread传递trace上下文

有的业务在处理server请求的时候,会创建子bthread,在子bthread中发起rpc调用。默认情况下,子bthread中的rpc调用跟原来的请求无法建立关联,trace就会断掉。这种情况下,可以在创建子bthread时,指定BTHREAD_INHERIT_SPAN标志,来显式地建立trace上文关联,如:

```c++
bthread_attr_t attr = { BTHREAD_STACKTYPE_NORMAL, BTHREAD_INHERIT_SPAN, NULL };
bthread_start_urgent(&tid, &attr, thread_proc, arg);
```

注意:使用这种方式创建子bthread来发送rpc,请确保rpc在server返回response之前完成,否则可能导致使用被释放的Span对象而出core。
@@ -379,6 +379,9 @@ int TaskGroup::start_foreground(TaskGroup** pg,
CHECK(m->stack == NULL);
m->attr = using_attr;
m->local_storage = LOCAL_STORAGE_INIT;
if (using_attr.flags & BTHREAD_INHERIT_SPAN) {
m->local_storage.rpcz_parent_span = tls_bls.rpcz_parent_span;
}
m->cpuwide_start_ns = start_ns;
m->stat = EMPTY_STAT;
m->tid = make_tid(*m->version_butex, slot);
@@ -434,6 +437,9 @@ int TaskGroup::start_background(bthread_t* __restrict th,
CHECK(m->stack == NULL);
m->attr = using_attr;
m->local_storage = LOCAL_STORAGE_INIT;
if (using_attr.flags & BTHREAD_INHERIT_SPAN) {
m->local_storage.rpcz_parent_span = tls_bls.rpcz_parent_span;
}
m->cpuwide_start_ns = start_ns;
m->stat = EMPTY_STAT;
m->tid = make_tid(*m->version_butex, slot);
@@ -46,6 +46,7 @@ static const bthread_attrflags_t BTHREAD_LOG_START_AND_FINISH = 8;
static const bthread_attrflags_t BTHREAD_LOG_CONTEXT_SWITCH = 16;
static const bthread_attrflags_t BTHREAD_NOSIGNAL = 32;
static const bthread_attrflags_t BTHREAD_NEVER_QUIT = 64;
static const bthread_attrflags_t BTHREAD_INHERIT_SPAN = 128;

// Key of thread-local data, created by bthread_key_create.
typedef struct {
@@ -26,6 +26,10 @@
#include "bthread/unstable.h"
#include "bthread/task_meta.h"

namespace bthread {
extern __thread bthread::LocalStorage tls_bls;
}

namespace {
class BthreadTest : public ::testing::Test{
protected:
@@ -508,6 +512,38 @@ TEST_F(BthreadTest, bthread_usleep) {
ASSERT_EQ(0, bthread_join(th2, NULL));
}

static const bthread_attr_t BTHREAD_ATTR_NORMAL_WITH_SPAN =
{ BTHREAD_STACKTYPE_NORMAL, BTHREAD_INHERIT_SPAN, NULL };

void* test_parent_span(void* p) {
uint64_t *q = (uint64_t *)p;
*q = (uint64_t)(bthread::tls_bls.rpcz_parent_span);
LOG(INFO) << "span id in thread is " << *q;
return NULL;
}

TEST_F(BthreadTest, test_span) {
uint64_t p1 = 0;
uint64_t p2 = 0;

uint64_t target = 0xBADBEAFUL;
LOG(INFO) << "target span id is " << target;

bthread::tls_bls.rpcz_parent_span = (void*)target;
bthread_t th1;
ASSERT_EQ(0, bthread_start_urgent(&th1, &BTHREAD_ATTR_NORMAL_WITH_SPAN,
test_parent_span, &p1));
ASSERT_EQ(0, bthread_join(th1, NULL));

bthread_t th2;
ASSERT_EQ(0, bthread_start_background(&th2, NULL,
test_parent_span, &p2));
ASSERT_EQ(0, bthread_join(th2, NULL));

ASSERT_EQ(p1, target);
ASSERT_NE(p2, target);
}

void* dummy_thread(void*) {
return NULL;
}

0 comments on commit 44bad72

Please sign in to comment.