apisix icon indicating copy to clipboard operation
apisix copied to clipboard

bug: Admin API concurrently created Router ID duplicate

Open AloneFire opened this issue 6 years ago • 18 comments

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 avatar Dec 03 '20 09:12 AloneFire

@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

idbeta avatar Dec 03 '20 09:12 idbeta

原因是我想通过apisix做一个权限网关,routes 配置 是从另一个系统批量同步过来的,同步过程使用了多线程,同步结束后发现数据不一致有几条数据创建返回的ID是重复的

AloneFire avatar Dec 03 '20 09:12 AloneFire

原因是我想通过apisix做一个权限网关,routes 配置 是从另一个系统批量同步过来的,同步过程使用了多线程,同步结束后发现数据不一致有几条数据创建返回的ID是重复的

为什么不同步etcd的数据 或者使用同一个etcd集群呢

Hcreak avatar Dec 03 '20 10:12 Hcreak

为什么不同步etcd的数据

@Hcreak 怎么同步, 感觉etcd同步的文档很少见.

oldthreefeng avatar Dec 03 '20 11:12 oldthreefeng

为什么不同步etcd的数据 或者使用同一个etcd集群呢

是这样,数据源不是etcd ,同步解决方案呢当然也可以通过操作etcd或者直接就避免并发同步, 但是Admin 接口在并发状态下会出现问题我觉得这就是一个bug

AloneFire avatar Dec 04 '20 02:12 AloneFire

我测试过老版本,etcd使用v2的接口是没有这个问题的

AloneFire avatar Dec 04 '20 03:12 AloneFire

This problem does exist if use post concurrently. If you use put, there is no problem specifying a different id each time.

idbeta avatar Dec 04 '20 03:12 idbeta

This problem does exist if use post concurrently. If you use put, there is no problem specifying a different id each time.

yes,you're right

AloneFire avatar Dec 04 '20 05:12 AloneFire

我测试过老版本,etcd使用v2的接口是没有这个问题的

能给出测试过程的脚本么. @AloneFire

oldthreefeng avatar Dec 04 '20 07:12 oldthreefeng

@oldthreefeng 这个脚本不太好单独抽出来,就是同样的多线程批量插入,apisix 1.x 不会出现这个问题

AloneFire avatar Dec 04 '20 08:12 AloneFire

If you use put, there is no problem specifying a different id each time.

do put ~ this is a lot helpful for me.

oldthreefeng avatar Dec 04 '20 10:12 oldthreefeng

pls provide the steps which can reproduce this issue

membphis avatar Dec 04 '20 17:12 membphis

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 avatar Dec 07 '20 06:12 AloneFire

@AloneFire need more information:

  1. how did you install the apisix?
  2. how did you start the apisix?
  3. did you modify the apisix source code?
  4. what is the content of file conf/config.yaml?

membphis avatar Dec 08 '20 09:12 membphis

@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:

image

membphis avatar Dec 08 '20 10:12 membphis

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.

spacewander avatar Dec 08 '20 10:12 spacewander

only etcd v3 have this issue, is right? @nic-chen @Yiyiyimu

membphis avatar Dec 08 '20 15:12 membphis

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

Yiyiyimu avatar Dec 09 '20 03:12 Yiyiyimu