Skip to content

Commit 4a33b77

Browse files
author
willzhen
committed
Update readme
1 parent dc3bf9a commit 4a33b77

File tree

5 files changed

+132
-10
lines changed

5 files changed

+132
-10
lines changed

task_scheduler/README.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

task_scheduler/example/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func main() {
3333
case <-c:
3434
return
3535
default:
36-
container.AddTask(context.Background(),
36+
sch.AddTask(context.Background(),
3737
framework.Task{
3838
TaskId: strconv.Itoa(i),
3939
TaskItem: task.ExampleTask{

task_scheduler/readme.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,123 @@
11
# 轻量级任务调度框架
2+
3+
## 框架的设计思想和背景
4+
业务后台开发,经常会遇到很多并发低,处理耗时长的限频任务,比如流媒体行业的视频转码、裁剪。这些任务的调度流程整体上没有差别,但是细节上又会有很多差异,以至于每新增一个任务类型,都要把调度部分的代码 copy 一份后修修改改。随着任务类型的越来越多,copy 代码的方式效率低下,且容易改出问题,这时候需要一个任务调度框架,既能够统一任务调度的流程,也能够适应差异。
5+
6+
### 任务系统的整体设计
7+
一个完善的任务系统包含任务管理模块和任务调度模块。
8+
9+
任务管理负责任务的创建、启动、停止、删除、拉取状态、拉取分析数据等操作,多类型的任务是可以共用的。
10+
11+
任务调度负责任务限频、具体的业务执行、结果处理等流程,不同任务的类型的调度模块无法共用。
12+
13+
![image](https://user-images.githubusercontent.com/15645203/210738936-74ac3abf-8fc7-4570-a440-0a071b87daa5.png)
14+
15+
### 任务调度框架
16+
作者一直想实现一个通用的任务管理框架,但是长时间陷入了应该如何存储任务的纠结境地:
17+
18+
1. 存 DB 可以满足异步任务需要可持久化,有状态的需求,但是需要轮询DB,性能上满足不了大流量场景。
19+
20+
2. 用 Kafka、内存等队列的方式存取任务,性能上很好,但是任务无状态,不能满足任务有状态的场景。
21+
22+
经过长时间的对比不同类型任务的执行流程,发现这些任务在大的流程上没有区别,细节上差别很大。最终,发现一个任务系统可以抽象成为三部分
23+
24+
1. 任务容器——用来存储任务相关数据。
25+
2. 任务执行器——真正的执行任务的逻辑。
26+
3. 任务调度器——对任务容器和任务执行器进行一些逻辑操作和逻辑配合,完成整体的任务调度流程。
27+
28+
其中,容器和执行器和业务相关、可以用一系列的接口(interface)来抽象,开发者根据自己的业务实现接口。任务调度流程比较估计,可以由框架实现。
29+
30+
![image](https://user-images.githubusercontent.com/15645203/210739259-86ef6480-097f-4189-98ac-3fe670dbe40b.png)
31+
32+
基于抽象的容器和执行器,固定的任务调度和执行的流程如下图
33+
34+
![image](https://user-images.githubusercontent.com/15645203/210739392-637269f6-a009-4345-92b7-8e8b92d1b3a5.png)
35+
36+
主要分成两个主线程
37+
38+
1. 维护任务状态线程:主要负责感知运行中的任务执行情况,状态的转移,以及运行成功后结果的导出过程(资源转存、数据落库等)。
39+
40+
2. 调度线程:负责对等待中的任务进行限频和调度。
41+
42+
总结下来,整体的设计思想是通过抽象出任务容器和任务执行器接口来实现调度流程的共享。
43+
44+
开发者只需要实现自己的任务容器接口和任务执行器接口,用任务容器和任务执行器创建任务调度器,即可轻易的实现任务调度。该框架可以支持多副本的调度器,如果使用关系型 db 作为容器,注意使用 db 的原子性防止任务重复调度。
45+
46+
### 任务容器分类
47+
48+
任务根据不同的维度,任务可以分成
49+
50+
#### 同步任务和异步任务
51+
本框架主要实现了任务异步化,可以轻易的通过执行器的实现把同步任务转换成异步任务。注意:实现同步任务执行器的时候,不要阻塞 Start 方法,而是在单独的协程执行任务。
52+
53+
#### 可持久任务和不可持久化任务
54+
任务的是否可持久化,通俗来说,就是任务执行完成以后,是否还能查询到任务相关的信息和记录。
55+
56+
1. 不可持久化任务——比如存储在内存队列里面的任务,执行完成以后,或者服务宕机、重启以后,任务相关的数据消失,无迹可寻。
57+
2. 可持久化任务——一般是存储在 DB 里面的任务。
58+
59+
根据是否可持久化,我们继续对任务容器抽象,分成两类任务容器:
60+
61+
- [MemeoryContainer](https://github.com/memory-overflow/go-common-library/blob/main/task_scheduler/container/memory_container/memory_container.go)——内存型任务容器,优点:可以快读快写,缺点:不可持久化。MemeoryContainer 实际上是可以和业务无关的,所以框架预置了三种MemeoryContainer——[queueContainer](https://github.com/memory-overflow/go-common-library/blob/main/task_scheduler/container/memory_container/queue_container.go),[orderedMapContainer](https://github.com/memory-overflow/go-common-library/blob/main/task_scheduler/container/memory_container/orderedmap_container.go),[redisContainer](https://github.com/memory-overflow/go-common-library/blob/main/task_scheduler/container/memory_container/redis_container.go)
62+
63+
- [queueContainer](https://github.com/memory-overflow/go-common-library/blob/main/task_scheduler/container/memory_container/queue_container.go):queueContainer 队列型容器,任务无状态,无优先级,先进先出,任务数据,多进程数据无法共享数据
64+
65+
- [orderedMapContainer](https://github.com/memory-overflow/go-common-library/blob/main/task_scheduler/container/memory_container/orderedmap_container.go)[OrderedMap](https://github.com/memory-overflow/go-common-library/blob/main/stl_extension/ordered_map.go) 作为容器,支持任务优先级,多进程数据无法共享数据
66+
67+
- [redisContainer](https://github.com/memory-overflow/go-common-library/blob/main/task_scheduler/container/memory_container/redis_container.go):redis 作为容器,支持任务优先级,并且可以多进程,多副本共享数据
68+
69+
- [PersistContainer](https://github.com/memory-overflow/go-common-library/blob/main/task_scheduler/container/persist_container/persist_container.go)——可持久化任务容器,优点:可持久化存储,缺点:依赖db、需要扫描表,对 db 压力比较大。开发者可以参考[exampleSQLContainer](https://github.com/memory-overflow/go-common-library/blob/main/task_scheduler/container/persist_container/example_sql_container.go) 实现自己的 SQLContainer,修改数据表的结构。
70+
71+
由于 MemeoryContainer 和 PersistContainer 各有优缺点,如果可以组合两种容器,生成一种新的任务容器[combinationContainer](https://github.com/memory-overflow/go-common-library/blob/main/task_scheduler/container/combination_container.go),既能够通过内存实现快写快读,又能够通过DB实现可持久化。
72+
![image](https://user-images.githubusercontent.com/15645203/210742391-ae2c60ac-9f19-4d1a-947b-634e3a3855ef.png)
73+
74+
## usage
75+
76+
### 使用内存容器实现 a+b 任务调度
77+
78+
有一个计算 a+b 的服务,由于该 a+b 是一种新的高维空间的计算规则,计算非常耗时耗资源,所以该服务设计成为异步的。该服务主要有三个接口
79+
1. /add, 输入 a, b,返回 taskId。
80+
2. /status,输入 taskId, 返回该任务的状态,是否已经完成,或者计算失败。
81+
3. /result,输入 taskId,如果任务已经完成,返回计算结果。
82+
83+
服务代码参考 [add_service.go](https://github.com/memory-overflow/go-common-library/blob/main/task_scheduler/example/add_service/add_service.go)
84+
85+
现在我们通过本任务调度框架实现一个 a+b 任务调度系统,可以控制任务并发数,并且按照队列依次调度。
86+
87+
#### 实现 a+b 任务执行器
88+
首先,需要实现一个 a+b 任务的执行器,执行器实际上就是调用 a+b 服务的接口。执行器的实现参考[example_actuator.go](https://github.com/memory-overflow/go-common-library/blob/main/task_scheduler/example/actuator/example_actuator.go)
89+
90+
#### 实现 a+b 任务容器
91+
这里,我们直接使用队列来作为任务容器,所以可以直接用框架预置的 queueContainer 作为任务容器,无需单独实现。
92+
93+
#### 实现调度
94+
参考代码[main.go](https://github.com/memory-overflow/go-common-library/blob/main/task_scheduler/example/main.go)
95+
96+
```go
97+
// 构建任务容器,队列长度 10000
98+
container := memeorycontainer.MakeQueueContainer(10000, 100*time.Millisecond)
99+
// 构建任务执行器
100+
actuator := actuator.MakeExampleActuator()
101+
// 构建调度器,自动开启调度
102+
sch := framework.MakeNewScheduler(
103+
context.Background(),
104+
container, actuator,
105+
framework.Config{
106+
TaskLimit: 5, // 任务并发限制
107+
ScanInterval: 100*time.Millisecond, // 系统扫描轮询周期,内存容器可以快速扫描,入股是 db 容器要注意配置合理的扫描间隔,防止对 db 造成比较大压力。
108+
})
109+
110+
// 添加任务
111+
for i := 0; i < 1000; i++ {
112+
sch.AddTask(context.Background(),
113+
framework.Task{
114+
TaskId: strconv.Itoa(i), // 每个任务都需要绑定一个唯一 id
115+
TaskItem: task.ExampleTask{
116+
TaskId: uint32(i),
117+
A: r.Int31() % 1000,
118+
B: r.Int31() % 1000,
119+
},
120+
})
121+
}
122+
123+
```

text/readme.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ text模块提供了一些字符串处理相关的算法能力。
1212
## SliceSame
1313
- SliceSame——判断两个字符串数字是否相同。
1414

15-
example: [TestTextSim](https://github.com/memory-overflow/go-common-library/blob/main/text/text_test.go#L29)
15+
example: [TestSliceSmae](https://github.com/memory-overflow/go-common-library/blob/main/text/text_test.go#L29)
1616
```go
1717
import (
1818
"testing"
1919

2020
"github.com/memory-overflow/go-common-library/text"
2121
)
2222

23-
func TestTextSim(t *testing.T) {
24-
sim := text.TextSim("编辑距离测试", "测试一下距离")
25-
t.Logf("sim: %f", sim)
23+
func TestSliceSmae(t *testing.T) {
24+
a := []string{"3", "2", "1"}
25+
same := text.SliceSame(a, a)
26+
t.Logf("is same: %v", same)
27+
// test can not change order of a
28+
t.Log(a)
2629
}
2730
```
2831

@@ -56,7 +59,7 @@ func TestActrie(t *testing.T) {
5659
## 计算文本编辑距离
5760
编辑距离(Edit Distance):是一个度量两个字符序列之间差异的字符串度量标准,两个单词之间的编辑距离是将一个单词转换为另一个单词所需的单字符编辑(插入、删除或替换)的最小数量。一般来说,编辑距离越小,两个串的相似度越大。
5861

59-
example: [TestActrie](https://github.com/memory-overflow/go-common-library/blob/main/text/text_test.go#L24)
62+
example: [TestLevenshtein](https://github.com/memory-overflow/go-common-library/blob/main/text/text_test.go#L24)
6063
```go
6164
import (
6265
"testing"

text/text_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ func TestSliceSmae(t *testing.T) {
3030
a := []string{"3", "2", "1"}
3131
same := text.SliceSame(a, a)
3232
t.Logf("is same: %v", same)
33+
// test can not change order of a
3334
t.Log(a)
3435
}

0 commit comments

Comments
 (0)