Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Datasource 抽象 #170

Merged
merged 21 commits into from
Apr 5, 2023
Merged

feature: Datasource 抽象 #170

merged 21 commits into from
Apr 5, 2023

Conversation

Stone-afk
Copy link
Collaborator

No description provided.

@codecov
Copy link

codecov bot commented Mar 14, 2023

Codecov Report

Merging #170 (49f77a3) into dev (a97835a) will increase coverage by 0.69%.
The diff coverage is 75.47%.

@@            Coverage Diff             @@
##              dev     #170      +/-   ##
==========================================
+ Coverage   81.82%   82.51%   +0.69%     
==========================================
  Files          33       36       +3     
  Lines        2657     2699      +42     
==========================================
+ Hits         2174     2227      +53     
+ Misses        404      388      -16     
- Partials       79       84       +5     
Impacted Files Coverage Δ
...nal/datasource/masterslave/slaves/dns/mysql/dsn.go 66.66% <ø> (ø)
...source/masterslave/slaves/roundrobin/roundrobin.go 86.20% <50.00%> (ø)
...l/datasource/shardingsource/sharding_datasource.go 76.92% <53.84%> (ø)
sharding_select.go 54.24% <58.82%> (ø)
update.go 82.70% <60.00%> (ø)
select.go 90.24% <63.63%> (ø)
insert.go 92.77% <66.66%> (ø)
internal/datasource/masterslave/slaves/dns/dns.go 76.78% <66.66%> (ø)
internal/datasource/single/db.go 71.42% <71.42%> (ø)
delete.go 92.50% <75.00%> (ø)
... and 8 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 在 internal 下面直接创建一个 datasource。datasource 用于 sharding,但是并不归属于 sharding;
  • 在 internal/datasource 里面定义接口
  • 在 internal/datasource 里面提供三个实现:单一 DB,主从,Cluster DB
  • 将已有的 slaves 的包挪过去对应的主从实现的子包里面

以下是要尝试一下,我预期中的:

  • 让 eorm.DB 里面持有一个 datasource,而后在 eorm 层面上我们只有 DB 这个概念。也就是说,通过 eorm.DB 里面维持不同的 datasource 实现,那么它可以统一表达任意类型的 DB
  • 在上一条成立的前提下,引入一个 OpenDS(ds datasource.Datasource) 的方法
  • 在上一条成立的前提下,重构已有的代码,将 eorm 层面上对 datasource实现依赖的,都修改为对 DB 的依赖

我认为,在 ORM 层面上,应该不关心底层的 datasource 究竟是单一数据库,还是主从,还是分库分表的。虽然看起来难以彻底做到,但是至少朝着这里面去前进。

@flycash
Copy link
Contributor

flycash commented Mar 16, 2023

eorm.DB 内部维持一个 datasource 字段,然后让 DB 实现 session。 Session 接口组合你的 Datasource 接口

@Stone-afk
Copy link
Collaborator Author

这么搞普通的查询用不了主从数据库了,因为都必须实现
‘’‘
type DataSource interface {
Query(ctx context.Context, query *sharding.Query) (*sql.Rows, error)
Exec(ctx context.Context, query *sharding.Query) (sql.Result, error)
}
’‘’
必须要sharding才能使用

@flycash
Copy link
Contributor

flycash commented Mar 16, 2023

所以我说有一个普通的 DB 对应的 Datasource 实现。总共三个

@flycash
Copy link
Contributor

flycash commented Mar 16, 2023

datasource.DB 和 eorm.DB,你不要搞混了,这个是两个。后者是前者的装饰器

@flycash
Copy link
Contributor

flycash commented Mar 16, 2023

应该说,eorm.DB 是 datasource.Datasource 的装饰器,提供了 core 的那个功能

@Stone-afk
Copy link
Collaborator Author

对于事物的实现,我理解应该说在 erom.DB 里维护?但集群怎么保证事物呢

@Stone-afk
Copy link
Collaborator Author

还是说 只要保证 sharding 后的目标主库写入数据就行,还有这么一来是否非分库分表就不能使用集群cluster

@flycash
Copy link
Contributor

flycash commented Mar 16, 2023

主从和单一的,你直接搞,和正常的事务没什么区别。分库分表的,这个要另外开 issue。分库分表的事务是一个跟棘手的事情,比这个 Datasource 棘手

@Stone-afk
Copy link
Collaborator Author

'''
type Query interface {
Sql() string
Args() []any
}
'''
通过统一制定 Query 然后 在 eorm 与 sharding 里提供具体的实现,然后在cluster 里通过类型断言区分 eorm 与 sharding 的具体实现,获得额外的参数 DB 与 Datasource

@Stone-afk Stone-afk changed the title WIP: Datasource 抽象 feature: Datasource 抽象 Mar 23, 2023
@flycash
Copy link
Contributor

flycash commented Mar 23, 2023

我草!46 个文件,要命了!

Copy link
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我明天试试能不能删掉 cluster_db,看上去你这里没有用到。

internal/datasource/single/db.go Show resolved Hide resolved
internal/datasource/cluster/cluster_db.go Outdated Show resolved Hide resolved
internal/datasource/cluster/cluster_db.go Outdated Show resolved Hide resolved
internal/datasource/cluster/cluster_db.go Show resolved Hide resolved
internal/datasource/types.go Show resolved Hide resolved
internal/datasource/single/db.go Outdated Show resolved Hide resolved
internal/datasource/slaves/master_slave_db.go Outdated Show resolved Hide resolved
internal/datasource/slaves/master_slave_db.go Outdated Show resolved Hide resolved
middleware/querylog/querylog_test.go Outdated Show resolved Hide resolved
@Stone-afk
Copy link
Collaborator Author

@flycash

internal/datasource/cluster/cluster_db.go Outdated Show resolved Hide resolved
internal/datasource/types.go Show resolved Hide resolved
internal/datasource/transaction/transaction.go Outdated Show resolved Hide resolved
internal/datasource/single/db.go Outdated Show resolved Hide resolved
internal/datasource/transaction/transaction_test.go Outdated Show resolved Hide resolved
internal/datasource/masterslave/slaves/roundrobin.go Outdated Show resolved Hide resolved
@Stone-afk
Copy link
Collaborator Author

@flycash

builder.go Show resolved Hide resolved
db.go Outdated Show resolved Hide resolved
db.go Outdated Show resolved Hide resolved
db.go Show resolved Hide resolved
db.go Show resolved Hide resolved
internal/sharding/types.go Outdated Show resolved Hide resolved
middleware.go Outdated Show resolved Hide resolved
middleware_test.go Show resolved Hide resolved
transaction.go Outdated Show resolved Hide resolved
types.go Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@Stone-afk Stone-afk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mark

@flycash
Copy link
Contributor

flycash commented Mar 29, 2023

你的 Golang-CI 崩了呀!

@Stone-afk
Copy link
Collaborator Author

ci lint 和 排序的那个pr的合入有很大关系,要问 zwl 同学了

@longyue0521
Copy link
Collaborator

longyue0521 commented Mar 30, 2023

ci lint 和 排序的那个pr的合入有很大关系,要问 zwl 同学了

你更新一下你的pre-push/pre-commit及setup.sh, golangci-lint版本之前有问题所以干脆和github action一样用@latest版本,重新安装一下golangci-lint

@Stone-afk
Copy link
Collaborator Author

golangci-lint已经是最新版本了,并重新安装了
image

@Stone-afk
Copy link
Collaborator Author

image

@juniaoshaonian
Copy link
Collaborator

juniaoshaonian commented Mar 30, 2023

image

golangci-lint --version 看看版本呢,我之前換了最新的版本就行了,我的go版本是1.19的顺便这个也改一下

@juniaoshaonian
Copy link
Collaborator

你换 Go 1.19 了吗

是的

db_test.go Show resolved Hide resolved
db_test.go Outdated Show resolved Hide resolved
internal/datasource/cluster/cluster_db.go Outdated Show resolved Hide resolved
internal/datasource/masterslave/slaves/dns/dns.go Outdated Show resolved Hide resolved
@flycash
Copy link
Contributor

flycash commented Apr 1, 2023

我一直觉得这个单元测试覆盖率简直有毒,不知道怎么算的

@Stone-afk
Copy link
Collaborator Author

@flycash

@ecodeclub ecodeclub deleted a comment from flycash Apr 3, 2023
Copy link
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一个个你确定解决了的 conversation 你就点一下 resolve。不然你可能漏了...

db_test.go Outdated Show resolved Hide resolved
return ms.Exec(ctx, query)
}

//func (*clusterDB) BeginTx(_ context.Context, _ *sql.TxOptions) (datasource.Tx, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不用的代码就删掉

internal/datasource/cluster/cluster_db.go Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@Stone-afk Stone-afk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mark

@flycash flycash merged commit 19f56ff into ecodeclub:dev Apr 5, 2023
@flycash
Copy link
Contributor

flycash commented Apr 5, 2023

后续我再来微调

@Stone-afk Stone-afk deleted the dev_164 branch April 5, 2023 14:32
@flycash flycash linked an issue Apr 8, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

分库分表:Datasource 抽象
4 participants