GTM fails to start on machines with <= 2 CPUs: "binding threads failed" (empty cpuset in bind_service_threads)

Open Beginner friendly
#557 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
74/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
databases

Research direction

Start in src/gtm/main/main.c with bind_service_threads() and bind_timekeeper_thread(), then read bind_thread_to_cores() to understand how affinity failures are handled. Reproduce with one or two CPUs using the commands in the issue. Done means GTM starts successfully in those cases, skipping an empty affinity set and using CPU 0 for the timekeeper on a single-core machine.

Written by the indexing model from the issue text.

Description

环境

  • OpenTenBase master(b2b129b)
  • 任意 Linux,1 或 2 个 CPU 核(虚拟机限核或 taskset -c 0,1 均可复现)

现象

GTM 启动即失败,报:

[src/gtm/main/main.c:2216] binding threads failed for 22

之后进程退出(部分版本表现为 FATAL: binding threads failed)。README 的「常见错误与排查」已记录该现象:
README.md:462 —— "GTM startup reports FATAL: binding threads failed (typically on machines with ≤ 2 CPU cores)",目前只有 noaffinity.so + /etc/ld.so.preload 的 workaround。

复现步骤

nproc            # 1 或 2
initgtm -D /tmp/gtm_data
gtm -D /tmp/gtm_data -p 6666

预期 GTM 正常监听;实际在 bind_service_threads() 处失败退出。

根因分析(源码)

src/gtm/main/main.c:

void bind_service_threads(void)
{
    int cpu_num = get_nprocs();
    int cpu;
    cpu_set_t cpuset;

    CPU_ZERO(&cpuset);
    for(cpu = 2; cpu < cpu_num; cpu++)   // 2 核时循环不执行,cpuset 为空
    {
        CPU_SET(cpu, &cpuset);
    }

    bind_thread_to_cores(cpuset);        // 空 cpuset -> pthread_setaffinity_np 返回 EINVAL
}

bind_thread_to_cores()(main.c:2208-2218)对失败直接 elog(ERROR, "binding threads failed for %d", r),没有降级路径;
bind_timekeeper_thread()(main.c:2220-2234)在单核机器上 CPU_SET(1, &cpuset) 同样会失败(只有 0 号核可用)。

期望行为

CPU 亲和性绑定属于性能优化,不应导致 GTM 启动失败:

  • cpuset 为空或 pthread_setaffinity_np 返回 EINVAL 时,记录 WARNING 后跳过绑定,继续启动;
  • timekeeper 在单核机器上回退到 CPU 0。

补充说明

已在 master b2b129b 上确认代码路径;仓库中没有与此对应的 issue/PR("binding threads failed" 仅在 PR #213 的文档改动中出现)。如果需要,我可以补充在 1–2 核环境下的完整启动日志。

Dominant language
C
Stars
946
Forks
693
Avg merge
5m
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from OpenTenBase/OpenTenBase

All issues in OpenTenBase/OpenTenBase

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.