spacewander commented on a change in pull request #4559:
URL: https://github.com/apache/apisix/pull/4559#discussion_r677076966
##########
File path: apisix/plugins/request-id.lua
##########
@@ -14,25 +14,58 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-local core = require("apisix.core")
-local plugin_name = "request-id"
-local ngx = ngx
-local uuid = require("resty.jit-uuid")
+
+local ngx = ngx
+local bit = require("bit")
+local core = require("apisix.core")
+local snowflake = require("snowflake")
+local uuid = require("resty.jit-uuid")
+local process = require("ngx.process")
+local timers = require("apisix.timers")
+local tostring = tostring
+local math_pow = math.pow
+local math_ceil = math.ceil
+local math_floor = math.floor
+
+local plugin_name = "request-id"
+
+local data_machine = nil
+local snowflake_inited = nil
+
+local attr = nil
local schema = {
type = "object",
properties = {
header_name = {type = "string", default = "X-Request-Id"},
- include_in_response = {type = "boolean", default = true}
+ include_in_response = {type = "boolean", default = true},
+ algorithm = {type = "string", enum = {"uuid", "snowflake"}, default =
"uuid"}
}
}
+local attr_schema = {
+ type = "object",
+ properties = {
+ snowflake = {
+ type = "object",
+ properties = {
+ enable = {type = "boolean"},
Review comment:
Better to set a default value for the optional field
##########
File path: docs/en/latest/plugins/request-id.md
##########
@@ -72,6 +72,60 @@ X-Request-Id: fe32076a-d0a5-49a6-a361-6c244c1df956
......
```
+### Use the snowflake algorithm to generate an ID
+
+> supports using the Snowflake algorithm to generate ID.
+> read the documentation first before deciding to use snowflake. Because once
the configuration information is enabled, you cannot arbitrarily adjust the
configuration information. Failure to do so may result in duplicate ID being
generated.
+
+The Snowflake algorithm is not enabled by default and needs to be configured
in 'conf/config.yaml'.
+
+```yaml
+plugin_attr:
+ request-id:
+ snowflake:
+ enable: true
+ snowflake_epoc: 1609459200000
+ data_machine_bits: 12
+ sequence_bits: 10
+ data_machine_ttl: 30
+ data_machine_interval: 10
+```
+
+#### Configuration parameters
+
+| Name | Type | Requirement | Default | Valid |
Description |
+| ------------------- | ------- | ------------- | -------------- | ------- |
------------------------------ |
+| enable | boolean | required | false | |
When set it to true, enable the snowflake algorithm. |
+| snowflake_epoc | integer | required | 1609459200000 | |
Start timestamp (in milliseconds) |
+| data_machine_bits | integer | deprecated | 12 | |
Maximum number of supported machines (processes) `1 << data_machine_bits` |
Review comment:
Why 'deprecated'?
##########
File path: docs/en/latest/plugins/request-id.md
##########
@@ -72,6 +72,60 @@ X-Request-Id: fe32076a-d0a5-49a6-a361-6c244c1df956
......
```
+### Use the snowflake algorithm to generate an ID
+
+> supports using the Snowflake algorithm to generate ID.
+> read the documentation first before deciding to use snowflake. Because once
the configuration information is enabled, you cannot arbitrarily adjust the
configuration information. Failure to do so may result in duplicate ID being
generated.
+
+The Snowflake algorithm is not enabled by default and needs to be configured
in 'conf/config.yaml'.
+
+```yaml
+plugin_attr:
+ request-id:
+ snowflake:
+ enable: true
+ snowflake_epoc: 1609459200000
+ data_machine_bits: 12
+ sequence_bits: 10
+ data_machine_ttl: 30
+ data_machine_interval: 10
+```
+
+#### Configuration parameters
+
+| Name | Type | Requirement | Default | Valid |
Description |
+| ------------------- | ------- | ------------- | -------------- | ------- |
------------------------------ |
+| enable | boolean | required | false | |
When set it to true, enable the snowflake algorithm. |
Review comment:
```suggestion
| enable | boolean | optional | false | |
When set it to true, enable the snowflake algorithm. |
```
##########
File path: docs/zh/latest/plugins/request-id.md
##########
@@ -71,9 +72,63 @@ X-Request-Id: fe32076a-d0a5-49a6-a361-6c244c1df956
......
```
+### 使用 snowflake 算法生成ID
+
+> 支持使用 snowflake 算法来生成ID。
+> 在决定使用snowflake时,请优先阅读一下文档。因为一旦启用配置信息则不可随意调整配置信息。否则可能会导致生成重复ID。
+
+snowflake 算法默认是不启用的,需要在 `conf/config.yaml` 中开启配置。
+
+```yaml
+plugin_attr:
+ request-id:
+ snowflake:
+ enable: true
+ snowflake_epoc: 1609459200000
+ data_machine_bits: 12
+ sequence_bits: 10
+ data_machine_ttl: 30
+ data_machine_interval: 10
+```
+
+#### 配置参数
+
+| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述
|
+| ------------------- | ------- | -------- | -------------- | ------ |
------------------------------ |
+| enable | boolean | 可选 | false | | 当设置为true时,
启用snowflake算法。 |
+| snowflake_epoc | integer | 可选 | 1609459200000 | | 起始时间戳(单位:
毫秒) |
+| data_machine_bits | integer | 可选 | 12 | |
最多支持机器(进程)数量 `1 << data_machine_bits` |
+| sequence_bits | integer | 可选 | 10 | |
每个节点每毫秒内最多产生ID数量 `1 << sequence_bits` |
+| data_machine_ttl | integer | 可选 | 30 | | `etcd` 中
`data_machine` 注册有效时间(单位: 秒)|
+| data_machine_interval | integer | 可选 | 10 | | `etcd` 中
`data_machine` 续约间隔时间(单位: 秒)|
+
+- snowflake_epoc 默认起始时间为 `2021-01-01T00:00:00Z`, 按默认配置可以支持 `69年` 大约可以使用到
`2090-09-07 15:47:35Z`
+- data_machine_bits 对应的是 snowflake 定义中的 WorkerID 和
DatacenterIDd的集合,插件会为每一个进程分配一个唯一ID,最大支持进程数为 `pow(2, data_machine_bits)`。默认占 `12
bits` 最多支持 `4096` 个进程。
Review comment:
```suggestion
- data_machine_bits 对应的是 snowflake 定义中的 WorkerID 和 DatacenterID
的集合,插件会为每一个进程分配一个唯一ID,最大支持进程数为 `pow(2, data_machine_bits)`。默认占 `12 bits` 最多支持
`4096` 个进程。
```
##########
File path: docs/en/latest/plugins/request-id.md
##########
@@ -72,6 +72,60 @@ X-Request-Id: fe32076a-d0a5-49a6-a361-6c244c1df956
......
```
+### Use the snowflake algorithm to generate an ID
+
+> supports using the Snowflake algorithm to generate ID.
+> read the documentation first before deciding to use snowflake. Because once
the configuration information is enabled, you cannot arbitrarily adjust the
configuration information. Failure to do so may result in duplicate ID being
generated.
Review comment:
```suggestion
> read the documentation first before deciding to use snowflake. Because
once the configuration information is enabled, you can not arbitrarily adjust
the configuration information. Failure to do so may result in duplicate ID
being generated.
```
##########
File path: apisix/plugins/request-id.lua
##########
@@ -41,9 +74,143 @@ function _M.check_schema(conf)
end
+-- Generates the current process data machine
+local function gen_data_machine(max_number)
+ if data_machine == nil then
+ local etcd_cli, prefix = core.etcd.new()
+ local prefix = prefix .. "/plugins/request-id/snowflake/"
+ local uuid = uuid.generate_v4()
+ local id = 1
+ ::continue::
+ while (id <= max_number) do
+ local res, err = etcd_cli:grant(attr.snowflake.data_machine_ttl)
+ if err then
+ core.log.error("Etcd grant failure, err: ".. err)
+ goto continue
Review comment:
There is a chance to cause an infinite loop?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]