Technology | GitLab 服务器在进行PR时后台服务器报错_ERROR_right sibling's left-link doesn't match

2024/06/30 fix 共 1975 字,约 6 分钟
wushui

问题描述

在进行PR(Pull Request)时,GitLab 服务器后台报错:ERROR: right sibling's left-link doesn't match: block 3814 links to 28248 instead of expected 15392 in index "index_merge_request_diff_commits_on_sha"

微信图片编辑_20240630143408

image-20240630152343207

问题背景

这个错误通常是由于数据库索引的损坏引起的。数据库中的索引就像是一本书的目录,帮助你快速找到需要的内容。当索引出现问题时,就好比书的目录页有错误,你在寻找某个章节时可能会找不到或者找到错误的内容。

修复思路

  1. 检查索引问题
  2. 增加查询超时时间
  3. 优化表结构
  4. 删除并重建索引
  5. 处理可能发生的异常问题,例如索引重建超时

步骤详解

检查索引问题

首先,我们需要确认问题是出在索引上。可以使用 gitlab-psql 命令来检查索引的状态。

sudo gitlab-psql -d gitlabhq_production -c "REINDEX INDEX index_merge_request_diff_commits_on_sha;"

增加查询超时时间

如果重建索引过程中发生超时错误,我们需要增加查询超时时间。

image-20240630151857557

修改 PostgreSQL 配置文件

  1. 打开 PostgreSQL 配置文件(postgresql.conf)。
  2. 找到 statement_timeout 配置项,并将其设置为更大的值,例如 10 分钟。
statement_timeout = '10min'
  1. 重新加载 PostgreSQL 配置:
sudo systemctl reload postgresql

临时增加超时时间

也可以在执行命令时临时增加超时时间:

sudo gitlab-psql -d gitlabhq_production -c "SET statement_timeout = 600000; REINDEX INDEX index_merge_request_diff_commits_on_sha;"

image-20240630151935070

优化表结构

在执行索引操作前,先进行表优化:

sudo gitlab-psql -d gitlabhq_production -c "VACUUM FULL VERBOSE ANALYZE merge_request_diff_commits;"

删除并重建索引

如果增加超时时间和优化表结构无效,可能需要删除并重新创建索引:

sudo gitlab-psql -d gitlabhq_production -c "DROP INDEX IF EXISTS index_merge_request_diff_commits_on_sha;"
sudo gitlab-psql -d gitlabhq_production -c "CREATE INDEX index_merge_request_diff_commits_on_sha ON merge_request_diff_commits (sha);"

重启GitLab

索引重建完成之后,重启gitlab服务器:

sudo gitlab-ctl restart

image-20240630152626499

常见问题处理

索引重建超时

在执行 REINDEX INDEX 命令时,可能会遇到超时错误。这时可以按照以下步骤解决:

  1. 增加查询超时时间
  2. 优化表结构
  3. 删除并重建索引

综合处理脚本如下:

#!/bin/bash

# 增加查询超时时间
sudo gitlab-psql -d gitlabhq_production -c "SET statement_timeout = 600000;"

# 进行表优化
sudo gitlab-psql -d gitlabhq_production -c "VACUUM FULL VERBOSE ANALYZE merge_request_diff_commits;"

# 删除并重新创建索引 非必要慎用!
sudo gitlab-psql -d gitlabhq_production -c "DROP INDEX IF EXISTS index_merge_request_diff_commits_on_sha;"
sudo gitlab-psql -d gitlabhq_production -c "CREATE INDEX index_merge_request_diff_commits_on_sha ON merge_request_diff_commits (sha);"

# 检查操作结果
sudo gitlab-psql -d gitlabhq_production -c "\d+ merge_request_diff_commits"

总结报告

通过上述步骤,我们解决了 GitLab 服务器在进行 PR 时后台报错的问题。以下是总结报告的流程图:

image-20240630155457346

关键步骤

  • 检查索引问题:确认索引损坏
  • 增加查询超时时间:防止重建索引超时
  • 优化表结构:提高索引操作效率
  • 删除并重建索引:修复索引问题

通过系统的排查和修复,我们成功地解决了索引损坏导致的错误,确保 GitLab 服务器的稳定运行。

文档信息

Search

    Table of Contents