bug: Admin API concurrently created Router ID duplicate
Issue description
Admin 接口并发创建 Router 得到的 ID 重复
Environment
- apisix version (cmd:
apisix version): 2.0 - OS: linux
Minimal test code / Steps to reproduce the issue
2. 3.
What's the actual result? (including assertion message & call stack if applicable)
What's the expected result?
@AloneFire Please describe your operation steps and tools in detail. but why do you want to create routes concurrently? BTW: We hope to use English in public communication channels
原因是我想通过apisix做一个权限网关,routes 配置 是从另一个系统批量同步过来的,同步过程使用了多线程,同步结束后发现数据不一致有几条数据创建返回的ID是重复的
原因是我想通过apisix做一个权限网关,routes 配置 是从另一个系统批量同步过来的,同步过程使用了多线程,同步结束后发现数据不一致有几条数据创建返回的ID是重复的
为什么不同步etcd的数据 或者使用同一个etcd集群呢
为什么不同步etcd的数据
@Hcreak 怎么同步, 感觉etcd同步的文档很少见.
为什么不同步etcd的数据 或者使用同一个etcd集群呢
是这样,数据源不是etcd ,同步解决方案呢当然也可以通过操作etcd或者直接就避免并发同步, 但是Admin 接口在并发状态下会出现问题我觉得这就是一个bug
我测试过老版本,etcd使用v2的接口是没有这个问题的
This problem does exist if use post concurrently.
If you use put, there is no problem specifying a different id each time.
This problem does exist if use
postconcurrently. If you useput, there is no problem specifying a different id each time.
yes,you're right
我测试过老版本,etcd使用v2的接口是没有这个问题的
能给出测试过程的脚本么. @AloneFire
@oldthreefeng 这个脚本不太好单独抽出来,就是同样的多线程批量插入,apisix 1.x 不会出现这个问题
If you use put, there is no problem specifying a different id each time.
do put ~ this is a lot helpful for me.
pls provide the steps which can reproduce this issue
pls provide the steps which can reproduce this issue
只是简单的并发调用 Admin POST接口
from gevent import monkey
import gevent
monkey.patch_all()
from pprint import pprint
import requests
APIKEY = "****************************"
APISIX_HOST = "http://127.0.0.1"
def create_route():
api = f"{APISIX_HOST}/apisix/admin/routes"
resp = requests.post(api, json={
"uri": "*",
"upstream_id": "one upstream_id"
}, headers={
"X-API-KEY": APIKEY
})
return resp.json().get("node", {}).get("key")
if __name__ == '__main__':
N = 10
tasks = [gevent.spawn(create_route) for n in range(N)]
gevent.joinall(tasks)
keys = [t.value for t in tasks]
pprint(keys)
assert (set([t.value for t in tasks]).__len__() == N)
@AloneFire need more information:
- how did you install the apisix?
- how did you start the apisix?
- did you modify the apisix source code?
- what is the content of file
conf/config.yaml?
@spacewander I use this script to reproduce this issue. It seems to be a bug of APISIX. please take a look at this issue
$ cat test.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import grequests
# add route
payloads = {}
for id in range(100):
payload = {
"uri": "/test_%s"%id,
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:7000": 1
}
}
}
payloads[id]=payload
unsent_request = []
header = {"X-API-KEY":"edd1c9f034335f136f87ad84b625c8f1"}
for id in payloads:
print(id)
unsent_request.append(grequests.post("http://127.0.0.1:9080/apisix/admin/routes/" ,json=payloads[id], headers = header))
responses = grequests.map(unsent_request)
for response in responses:
print(response.content)
response.close()
Same UUID:

https://github.com/apache/apisix/blob/32c7df44cacc28d33abf5c1179b0fc8afa934036/apisix/core/etcd.lua#L277-L287
I think there is a race between read and set.
Maybe we should introduce a /apisix/_gen_seq as the id generator.
only etcd v3 have this issue, is right? @nic-chen @Yiyiyimu
Yes the bug is imported in v3. The "manual suffix" in etcd v2 is added on the etcd side to make sure the duplication will not happen. I didn't consider concurrency at that time.
Agree with @spacewander