设置"max-replicas": 1,为什么建立的表还是三副本?

为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。

  • 【TiDB 版本】:tidb-v4.0.6
  • 【问题描述】:测试环境,设置在pd中设置,“max-replicas”: 1,为什么建立的表还是三副本?是还需要做其它什么设置吗?
mysql> select tidb_version()\G
*************************** 1. row ***************************
tidb_version(): Release Version: v4.0.6
Edition: Community
Git Commit Hash: 51d365fc45fdfc039eb204a96268c5bd1c55075f
Git Branch: heads/refs/tags/v4.0.6
UTC Build Time: 2020-09-15 09:50:30
GoVersion: go1.13
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
1 row in set (0.00 sec)

mysql> create table t(id int);
Query OK, 0 rows affected (1.03 sec)

mysql> insert into t values(1),(2),(3);
Query OK, 3 rows affected (0.03 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> show table t regions;
+-----------+-----------+---------+-----------+-----------------+-------------------+------------+---------------+------------+----------------------+------------------+
| REGION_ID | START_KEY | END_KEY | LEADER_ID | LEADER_STORE_ID | PEERS             | SCATTERING | WRITTEN_BYTES | READ_BYTES | APPROXIMATE_SIZE(MB) | APPROXIMATE_KEYS |
+-----------+-----------+---------+-----------+-----------------+-------------------+------------+---------------+------------+----------------------+------------------+
|         2 | t_81_     |         |      1426 |               6 | 1426, 4013, 31698 |          0 |             0 |          0 |                    1 |               17 |
+-----------+-----------+---------+-----------+-----------------+-------------------+------------+---------------+------------+----------------------+------------------+
1 row in set (0.01 sec)

mysql>

pd-ctl

? config show
{
  "replication": {
    "enable-placement-rules": "false",
    "location-labels": "",
    "max-replicas": 1,
    "strictly-match-label": "false"
  },

若提问为性能优化、故障排查类问题,请下载脚本运行。终端输出的打印结果,请务必全选并复制粘贴上传。

1、用 pd-ctl 看下 region 2 再看看当前的情况:

https://docs.pingcap.com/zh/tidb/stable/pd-control#region-region_id---jqquery-string

2、pd-ctl config show all 是什么样的情况

现在是单副本了。 能在建立表的时候,指定单福本吗?不通过pd调度删除其它副本

» region 2 
{
  "id": 2,
  "start_key": "7480000000000000FF545F728000000000FF1A46320000000000FA",
  "end_key": "",
  "epoch": {
    "conf_ver": 45,
    "version": 1204
  },
  "peers": [
    {
      "id": 1426,
      "store_id": 6
    }
  ],
  "leader": {
    "id": 1426,
    "store_id": 6
  },
  "written_bytes": 0,
  "read_bytes": 0,
  "written_keys": 0,
  "read_keys": 0,
  "approximate_size": 108,
  "approximate_keys": 1006801
}

» 

config show all

» config show all;
{
  "client-urls": "http://0.0.0.0:2379",
  "peer-urls": "http://10.205.33.214:2380",
  "advertise-client-urls": "http://10.205.33.214:2379",
  "advertise-peer-urls": "http://10.205.33.214:2380",
  "name": "pd-10.205.33.214-2379",
  "data-dir": "/vdb/tidb/data/pd-2379",
  "force-new-cluster": false,
  "enable-grpc-gateway": true,
  "initial-cluster": "pd-10.205.33.213-2379=http://10.205.33.213:2380,pd-10.205.33.214-2379=http://10.205.33.214:2380,pd-10.205.33.215-2379=http://10.205.33.215:2380",
  "initial-cluster-state": "new",
  "initial-cluster-token": "pd-cluster",
  "join": "",
  "lease": 3,
  "log": {
    "level": "",
    "format": "text",
    "disable-timestamp": false,
    "file": {
      "filename": "/vdb/tidb/deploy/pd-2379/log/pd.log",
      "max-size": 300,
      "max-days": 0,
      "max-backups": 0
    },
    "development": false,
    "disable-caller": false,
    "disable-stacktrace": false,
    "disable-error-verbose": true,
    "sampling": null
  },
  "tso-save-interval": "3s",
  "metric": {
    "job": "pd-10.205.33.214-2379",
    "address": "",
    "interval": "15s"
  },
  "schedule": {
    "max-snapshot-count": 3,
    "max-pending-peer-count": 16,
    "max-merge-region-size": 20,
    "max-merge-region-keys": 200000,
    "split-merge-interval": "1h0m0s",
    "enable-one-way-merge": "false",
    "enable-cross-table-merge": "false",
    "patrol-region-interval": "100ms",
    "max-store-down-time": "30m0s",
    "leader-schedule-limit": 4,
    "leader-schedule-policy": "count",
    "region-schedule-limit": 2048,
    "replica-schedule-limit": 64,
    "merge-schedule-limit": 8,
    "hot-region-schedule-limit": 4,
    "hot-region-cache-hits-threshold": 3,
    "store-limit": {
      "1": {
        "add-peer": 15,
        "remove-peer": 15
      },
      "4": {
        "add-peer": 15,
        "remove-peer": 15
      },
      "6": {
        "add-peer": 15,
        "remove-peer": 15
      },
      "8": {
        "add-peer": 15,
        "remove-peer": 15
      },
      "96005": {
        "add-peer": 15,
        "remove-peer": 15
      },
      "96006": {
        "add-peer": 15,
        "remove-peer": 15
      }
    },
    "tolerant-size-ratio": 0,
    "low-space-ratio": 0.8,
    "high-space-ratio": 0.7,
    "scheduler-max-waiting-operator": 5,
    "enable-remove-down-replica": "true",
    "enable-replace-offline-replica": "false",
    "enable-make-up-replica": "false",
    "enable-remove-extra-replica": "true",
    "enable-location-replacement": "true",
    "enable-debug-metrics": "false",
    "schedulers-v2": [
      {
        "type": "balance-region",
        "args": null,
        "disable": false,
        "args-payload": ""
      },
      {
        "type": "balance-leader",
        "args": null,
        "disable": false,
        "args-payload": ""
      },
      {
        "type": "hot-region",
        "args": null,
        "disable": false,
        "args-payload": ""
      },
      {
        "type": "label",
        "args": null,
        "disable": false,
        "args-payload": ""
      },
      {
        "type": "evict-leader",
        "args": [
          "1"
        ],
        "disable": false,
        "args-payload": ""
      },
      {
        "type": "evict-leader",
        "args": [
          "8"
        ],
        "disable": false,
        "args-payload": ""
      },
      {
        "type": "evict-leader",
        "args": [
          "6"
        ],
        "disable": false,
        "args-payload": ""
      },
      {
        "type": "evict-leader",
        "args": [
          "4"
        ],
        "disable": false,
        "args-payload": ""
      }
    ],
    "schedulers-payload": {
      "balance-hot-region-scheduler": null,
      "balance-leader-scheduler": {
        "name": "balance-leader-scheduler",
        "ranges": [
          {
            "end-key": "",
            "start-key": ""
          }
        ]
      },
      "balance-region-scheduler": {
        "name": "balance-region-scheduler",
        "ranges": [
          {
            "end-key": "",
            "start-key": ""
          }
        ]
      },
      "evict-leader-scheduler": {
        "store-id-ranges": {
          "4": [
            {
              "end-key": "",
              "start-key": ""
            }
          ]
        }
      },
      "label-scheduler": {
        "name": "label-scheduler",
        "ranges": [
          {
            "end-key": "",
            "start-key": ""
          }
        ]
      }
    },
    "store-limit-mode": "manual"
  },
  "replication": {
    "max-replicas": 1,
    "location-labels": "zone,host",
    "strictly-match-label": "false",
    "enable-placement-rules": "false"
  },
  "pd-server": {
    "use-region-storage": "true",
    "max-gap-reset-ts": "24h0m0s",
    "key-type": "table",
    "runtime-services": "",
    "metric-storage": "",
    "dashboard-address": "http://10.205.33.213:2379",
    "trace-region-flow": "false"
  },
  "cluster-version": "4.0.6",
  "quota-backend-bytes": "8GiB",
  "auto-compaction-mode": "periodic",
  "auto-compaction-retention-v2": "1h",
  "TickInterval": "500ms",
  "ElectionInterval": "3s",
  "PreVote": true,
  "security": {
    "cacert-path": "",
    "cert-path": "",
    "key-path": "",
    "cert-allowed-cn": null
  },
  "label-property": {},
  "WarningMsgs": null,
  "DisableStrictReconfigCheck": false,
  "HeartbeatStreamBindInterval": "1m0s",
  "LeaderPriorityCheckInterval": "1m0s",
  "dashboard": {
    "tidb-cacert-path": "",
    "tidb-cert-path": "",
    "tidb-key-path": "",
    "public-path-prefix": "",
    "internal-proxy": false,
    "enable-telemetry": true,
    "enable-experimental": false
  },
  "replication-mode": {
    "replication-mode": "majority",
    "dr-auto-sync": {
      "label-key": "",
      "primary": "",
      "dr": "",
      "primary-replicas": 0,
      "dr-replicas": 0,
      "wait-store-timeout": "1m0s",
      "wait-sync-timeout": "1m0s"
    }
  }
}

»

等到副本都是1的时候,建立的表直接是1副本。

当前环境,不支持对某张表单独指定副本数,生产环境强烈建议使用 3 副本,以确保高可用,测试环境如果对数据的可用性要求不高,可以使用单幅本,单幅本需通过 pd 参数来指定~

好的 生产环境是三副本。这个是测试环境

:+1:

我将max-replicas由5修改为3,pd运行配置显示已经是3个副本了,新建的表为啥还是5个副本

[config]
merge-schedule-limit = 8
[config.schedule]
max-merge-region-keys = 300000
max-merge-region-size = 30

[label-property]

[[label-property.reject-leader]]
key = “zone”
value = “sh3”

[replication]
location-labels = [“zone”, “host”]
max-replicas = 3

[schedule]
leader-schedule-limit = 4
region-schedule-limit = 2048
replica-schedule-limit = 64
~
怎么解决啊

确认TiDB集群的拓扑结构和配置中是否定义了默认的副本数量

1 个赞

新的版本tidb可以直接用命令改
如修改region副本数replica

set config pd replication.max-replicas=1;

查看结果,由3改成了1

show config where NAME like ‘%max-replicas%’ ;

你和楼主的问题都是因为placement rule功能导致的,开始placement rule后,max-replicas参数将不再生效,参考: https://docs.pingcap.com/zh/tidb/stable/configure-placement-rules#开启-placement-rules-特性

2 个赞

从文档看,新点的版本默认开启placement rule

我测试过只要执行 set config pd replication.max-replicas =1;很快3副本都会自动转成1个副本,并不是不生效

你把你测试步骤贴出来,可能高版本有这块优化,官方文档还没来得及,下面是当前v7.5官方文档原话

注意

开启 Placement Rules 后,原先的 max-replicaslocation-labels 配置项将不再生效。如果需要调整副本策略,应当使用 Placement Rules 相关接口。

我测试了下,最新7.6.0版本3副本:

创建测试表a,查看确实是3副本:

用命令 set config pd replication.max-replicas =1;改成1副本再看表的副本数,确实是1副本了:

参数改回3副本:

查看集群配置文件placement rule启用的:
image

1 个赞

官方文档中:开启 Placement Rules 后,原先的 max-replicaslocation-labels 配置项将不再生效。如果需要调整副本策略,应当使用 Placement Rules 相关接口。

我记得我测试过,是在集群配置文件修改副本数无效,而 set config pd replication.max-replicas有效, set config pd replication.max-replicas应该就是使用 Placement Rules 相关接口的

开启 Placement Rules 后,原来的 max-replicaslocation-labels 配置项将不再生效。应当使用 Placement Rules 相关接口。

set config pd replication.max-replicas后可以看下placement rule的规则,看下是否自动修改了placement rule

3副本:
image
1副本:
image

1 个赞

max-replicaslocation-labels应该是被placement rule覆盖。
就像isolation-level可以覆盖max-replicas的效果。
通过拓扑 label 进行副本调度 | PingCAP 文档中心

配置文件改没用,需要用sql命令行改 set config pd replication.max-replicas=1