mirror of
https://github.com/caiwx86/small-packages.git
synced 2026-11-17 13:43:01 +08:00
update 2026-09-26 11:07:27
This commit is contained in:
1 parent
7b88b273d0
commit
97ae9f68da
4898 files changed
-1712274
No files matched your search
@@ -1,19 +0,0 @@
|
||||
From: kenzok8 <kenzok8@noreply>
|
||||
Date: 2026-06-30
|
||||
Subject: [PATCH] daed: raise manual latency probe concurrency to 16
|
||||
|
||||
Raise concurrent probes 8->16 so manual latency tests finish faster
|
||||
when many nodes (incl. dead/timeout ones) are present.
|
||||
Reported: https://github.com/kenzok8/openwrt-daede/issues/20
|
||||
---
|
||||
--- a/graphql/service/node/latency_mutation_utils.go
|
||||
+++ b/graphql/service/node/latency_mutation_utils.go
|
||||
@@ -19,7 +19,7 @@
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
-const latencyProbeConcurrency = 8
|
||||
+const latencyProbeConcurrency = 16
|
||||
|
||||
func TestLatencies(ctx context.Context, ids *[]graphql.ID) ([]*LatencyResolver, error) {
|
||||
option, err := latencyProbeOption(ctx)
|
||||
@@ -1,63 +0,0 @@
|
||||
diff --git a/graphql/service/config/mutation_utils.go b/graphql/service/config/mutation_utils.go
|
||||
index 7a3c33f..fd61554 100644
|
||||
--- a/graphql/service/config/mutation_utils.go
|
||||
+++ b/graphql/service/config/mutation_utils.go
|
||||
@@ -9,11 +9,13 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
+ "os"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
+ "time"
|
||||
|
||||
"github.com/daeuniverse/dae-wing/common"
|
||||
"github.com/daeuniverse/dae-wing/dae"
|
||||
@@ -258,6 +260,15 @@ func Rename(ctx context.Context, _id graphql.ID, name string) (n int32, err erro
|
||||
|
||||
var runLock sync.Mutex
|
||||
|
||||
+const reloadWaitTimeout = 75 * time.Second
|
||||
+
|
||||
+func restartAfterReloadTimeout() {
|
||||
+ go func() {
|
||||
+ time.Sleep(time.Second)
|
||||
+ os.Exit(1)
|
||||
+ }()
|
||||
+}
|
||||
+
|
||||
func Run(d *gorm.DB, noLoad bool) (n int32, err error) {
|
||||
if ok := runLock.TryLock(); !ok {
|
||||
return 0, fmt.Errorf("the last request didn't complete; make a cup of tea and take a break")
|
||||
@@ -455,13 +466,24 @@ func Run(d *gorm.DB, noLoad bool) (n int32, err error) {
|
||||
|
||||
/// Reload with current config.
|
||||
chReloadCallback := make(chan error)
|
||||
- dae.ChReloadConfigs <- &dae.ReloadMessage{
|
||||
+ reloadMsg := &dae.ReloadMessage{
|
||||
Config: c,
|
||||
Callback: chReloadCallback,
|
||||
}
|
||||
- errReload := <-chReloadCallback
|
||||
- if errReload != nil {
|
||||
- return 0, fmt.Errorf("failed to load new config: %w; see more in log", errReload)
|
||||
+ select {
|
||||
+ case dae.ChReloadConfigs <- reloadMsg:
|
||||
+ case <-time.After(reloadWaitTimeout):
|
||||
+ restartAfterReloadTimeout()
|
||||
+ return 0, fmt.Errorf("failed to submit reload within %s; restarting daed to recover runtime", reloadWaitTimeout)
|
||||
+ }
|
||||
+ select {
|
||||
+ case errReload := <-chReloadCallback:
|
||||
+ if errReload != nil {
|
||||
+ return 0, fmt.Errorf("failed to load new config: %w; see more in log", errReload)
|
||||
+ }
|
||||
+ case <-time.After(reloadWaitTimeout):
|
||||
+ restartAfterReloadTimeout()
|
||||
+ return 0, fmt.Errorf("reload timed out after %s; restarting daed to recover runtime", reloadWaitTimeout)
|
||||
}
|
||||
|
||||
// Save running status
|
||||
@@ -1,92 +0,0 @@
|
||||
From: kenzok8 <kenzok8@noreply>
|
||||
Date: 2026-09-23
|
||||
Subject: [PATCH] daed: bound startup network wait on new wing
|
||||
|
||||
Return an error after 60 seconds and retry on the next generation.
|
||||
---
|
||||
diff --git a/dae/run.go b/dae/run.go
|
||||
index 7c9a5cc..7f00748 100644
|
||||
--- a/dae/run.go
|
||||
+++ b/dae/run.go
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"github.com/daeuniverse/dae/control"
|
||||
"github.com/daeuniverse/dae/pkg/config_parser"
|
||||
"github.com/daeuniverse/dae/pkg/logger"
|
||||
+ "github.com/daeuniverse/outbound/netproxy"
|
||||
"github.com/daeuniverse/outbound/protocol/direct"
|
||||
"github.com/mohae/deepcopy"
|
||||
"github.com/sirupsen/logrus"
|
||||
@@ -43,7 +44,8 @@ var EmptyConfig *daeConfig.Config
|
||||
// active is the generation currently serving traffic. It is nil before the
|
||||
// first generation is ready and while a reload has retired the old one.
|
||||
var active atomic.Pointer[control.ControlPlane]
|
||||
-var onceWaitingNetwork sync.Once
|
||||
+var waitingNetworkMu sync.Mutex
|
||||
+var networkReady bool
|
||||
|
||||
func init() {
|
||||
sections, err := config_parser.Parse(`global{} routing{}`)
|
||||
@@ -64,6 +66,19 @@ func ControlPlane() (*control.ControlPlane, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
+func waitForNetwork(log *logrus.Logger, directDialer netproxy.Dialer) error {
|
||||
+ waitingNetworkMu.Lock()
|
||||
+ defer waitingNetworkMu.Unlock()
|
||||
+ if networkReady {
|
||||
+ return nil
|
||||
+ }
|
||||
+ if err := WaitForNetwork(log, directDialer); err != nil {
|
||||
+ return err
|
||||
+ }
|
||||
+ networkReady = true
|
||||
+ return nil
|
||||
+}
|
||||
+
|
||||
// generation is one control plane with its listener. retired is set before
|
||||
// the plane is torn down so a Serve error from a plane that is being replaced
|
||||
// is not mistaken for the active datapath dying.
|
||||
@@ -345,9 +360,9 @@ func newControlPlane(log *logrus.Logger, dnsCache map[string]*control.DnsCache,
|
||||
|
||||
if !conf.Global.DisableWaitingNetwork && len(conf.Global.WanInterface) > 0 {
|
||||
// Wait for network for WAN ready.
|
||||
- onceWaitingNetwork.Do(func() {
|
||||
- WaitForNetwork(log, directDialers.Symmetric)
|
||||
- })
|
||||
+ if err = waitForNetwork(log, directDialers.Symmetric); err != nil {
|
||||
+ return nil, err
|
||||
+ }
|
||||
}
|
||||
|
||||
/// Get subscription -> nodeList mapping.
|
||||
diff --git a/dae/utils.go b/dae/utils.go
|
||||
index 2b4f6bf..93efb7e 100644
|
||||
--- a/dae/utils.go
|
||||
+++ b/dae/utils.go
|
||||
@@ -94,8 +94,9 @@ func preprocessWanInterfaceAuto(params *daeConfig.Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
-func WaitForNetwork(log *logrus.Logger, directDialer netproxy.Dialer) {
|
||||
+func WaitForNetwork(log *logrus.Logger, directDialer netproxy.Dialer) error {
|
||||
epo := 5 * time.Second
|
||||
+ deadline := time.Now().Add(60 * time.Second)
|
||||
client := http.Client{
|
||||
Transport: &http.Transport{
|
||||
DialContext: func(ctx context.Context, network, addr string) (c net.Conn, err error) {
|
||||
@@ -114,6 +115,9 @@ func WaitForNetwork(log *logrus.Logger, directDialer netproxy.Dialer) {
|
||||
}
|
||||
log.Infoln("Waiting for network...")
|
||||
for i := 0; ; i++ {
|
||||
+ if time.Now().After(deadline) {
|
||||
+ return fmt.Errorf("network unavailable after 60 seconds")
|
||||
+ }
|
||||
resp, err := client.Get(cmd.CheckNetworkLinks[i%len(cmd.CheckNetworkLinks)])
|
||||
if err != nil {
|
||||
log.Debugln("CheckNetwork:", err)
|
||||
@@ -133,4 +137,5 @@ func WaitForNetwork(log *logrus.Logger, directDialer netproxy.Dialer) {
|
||||
time.Sleep(epo)
|
||||
}
|
||||
log.Infoln("Network online.")
|
||||
+ return nil
|
||||
}
|
||||
@@ -1,606 +0,0 @@
|
||||
From: kenzok8 <kenzok8@noreply>
|
||||
Date: 2026-09-23
|
||||
Subject: [PATCH] daed: strictly reconcile subscription updates
|
||||
|
||||
Reconcile nodes by link, preserve fixed-group nodes, and apply committed changes.
|
||||
---
|
||||
diff --git a/cmd/run.go b/cmd/run.go
|
||||
index 7dce577..978f46c 100644
|
||||
--- a/cmd/run.go
|
||||
+++ b/cmd/run.go
|
||||
@@ -161,11 +161,8 @@ func restoreRunningState() (err error) {
|
||||
if !reload {
|
||||
return nil
|
||||
}
|
||||
- tx := db.BeginTx(context.TODO())
|
||||
// Reload.
|
||||
- if _, err = config.Run(tx, false); err != nil {
|
||||
- tx.Rollback()
|
||||
-
|
||||
+ if _, err = config.Run(context.TODO(), false); err != nil {
|
||||
// Another tx.
|
||||
// Set running = false.
|
||||
tx2 := db.BeginTx(context.TODO())
|
||||
@@ -183,7 +180,6 @@ func restoreRunningState() (err error) {
|
||||
tx2.Commit()
|
||||
return err
|
||||
}
|
||||
- tx.Commit()
|
||||
return nil
|
||||
}
|
||||
|
||||
diff --git a/graphql/mutation.go b/graphql/mutation.go
|
||||
index af065fd..d462ecc 100644
|
||||
--- a/graphql/mutation.go
|
||||
+++ b/graphql/mutation.go
|
||||
@@ -256,16 +256,7 @@ func (r *MutationResolver) SelectConfig(args *struct {
|
||||
func (r *MutationResolver) Run(args *struct {
|
||||
Dry bool
|
||||
}) (int32, error) {
|
||||
- tx := db.BeginTx(context.TODO())
|
||||
- ret, err := config.Run(tx, args.Dry)
|
||||
- if err != nil {
|
||||
- tx.Rollback()
|
||||
- return 0, err
|
||||
- }
|
||||
- if err = tx.Commit().Error; err != nil {
|
||||
- return 0, fmt.Errorf("commit run: %w", err)
|
||||
- }
|
||||
- return ret, nil
|
||||
+ return config.Run(context.TODO(), args.Dry)
|
||||
}
|
||||
|
||||
func (r *MutationResolver) CreateDns(args *struct {
|
||||
diff --git a/graphql/root_schema.go b/graphql/root_schema.go
|
||||
index 8b82db6..a96cace 100644
|
||||
--- a/graphql/root_schema.go
|
||||
+++ b/graphql/root_schema.go
|
||||
@@ -121,7 +121,7 @@ type Mutation {
|
||||
# tagSubscription is to give the subscription a new tag.
|
||||
tagSubscription(id: ID!, tag: String!): Int! @hasRole(role: ADMIN)
|
||||
|
||||
- # updateSubscription is to re-fetch subscription and resolve subscription into nodes. Old nodes that independently belong to any groups will not be removed.
|
||||
+ # updateSubscription re-fetches and strictly reconciles subscription nodes. Stale nodes pinned by fixed groups become independent nodes.
|
||||
updateSubscription(id: ID!): Subscription! @hasRole(role: ADMIN)
|
||||
|
||||
# updateSubscriptionLink is to update the subscription link without re-fetching nodes.
|
||||
diff --git a/graphql/service/config/mutation_utils.go b/graphql/service/config/mutation_utils.go
|
||||
index e6bf9da..8ab756b 100644
|
||||
--- a/graphql/service/config/mutation_utils.go
|
||||
+++ b/graphql/service/config/mutation_utils.go
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"github.com/daeuniverse/dae-wing/dae"
|
||||
"github.com/daeuniverse/dae-wing/db"
|
||||
"github.com/daeuniverse/dae-wing/graphql/service/config/global"
|
||||
+ "github.com/daeuniverse/dae-wing/graphql/service/general"
|
||||
daeConfig "github.com/daeuniverse/dae/config"
|
||||
"github.com/daeuniverse/dae/pkg/config_parser"
|
||||
"github.com/graph-gophers/graphql-go"
|
||||
@@ -269,21 +270,69 @@ func restartAfterReloadTimeout() {
|
||||
}()
|
||||
}
|
||||
|
||||
-func Run(d *gorm.DB, noLoad bool) (n int32, err error) {
|
||||
+func runTransaction(ctx context.Context, noLoad bool) (n int32, err error) {
|
||||
+ tx := db.BeginTx(ctx)
|
||||
+ if tx.Error != nil {
|
||||
+ return 0, tx.Error
|
||||
+ }
|
||||
+ n, err = runLocked(tx, noLoad)
|
||||
+ if err != nil {
|
||||
+ tx.Rollback()
|
||||
+ return 0, err
|
||||
+ }
|
||||
+ if err = tx.Commit().Error; err != nil {
|
||||
+ return 0, err
|
||||
+ }
|
||||
+ return n, nil
|
||||
+}
|
||||
+
|
||||
+func Run(ctx context.Context, noLoad bool) (n int32, err error) {
|
||||
if ok := runLock.TryLock(); !ok {
|
||||
return 0, fmt.Errorf("the last request didn't complete; make a cup of tea and take a break")
|
||||
}
|
||||
defer runLock.Unlock()
|
||||
+ return runTransaction(ctx, noLoad)
|
||||
+}
|
||||
+
|
||||
+func ApplyIfRunning(ctx context.Context) error {
|
||||
+ runLock.Lock()
|
||||
+ defer runLock.Unlock()
|
||||
+
|
||||
+ daeResolver := general.DaeResolver{Ctx: ctx}
|
||||
+ running, err := daeResolver.Running()
|
||||
+ if err != nil || !running {
|
||||
+ return err
|
||||
+ }
|
||||
+ modified, err := daeResolver.Modified()
|
||||
+ if err != nil || !modified {
|
||||
+ return err
|
||||
+ }
|
||||
+ _, err = runTransaction(ctx, false)
|
||||
+ return err
|
||||
+}
|
||||
+
|
||||
+func runLocked(d *gorm.DB, noLoad bool) (n int32, err error) {
|
||||
//// Dry run.
|
||||
if noLoad {
|
||||
ch := make(chan error)
|
||||
- dae.ChReloadConfigs <- &dae.ReloadMessage{
|
||||
+ reloadMsg := &dae.ReloadMessage{
|
||||
Config: dae.EmptyConfig,
|
||||
Callback: ch,
|
||||
}
|
||||
- err = <-ch
|
||||
- if err != nil {
|
||||
- return 0, fmt.Errorf("failed to dryrun: %w; see more in log and report bugs", err)
|
||||
+ select {
|
||||
+ case dae.ChReloadConfigs <- reloadMsg:
|
||||
+ case <-time.After(reloadWaitTimeout):
|
||||
+ restartAfterReloadTimeout()
|
||||
+ return 0, fmt.Errorf("failed to submit dryrun within %s; restarting daed to recover runtime", reloadWaitTimeout)
|
||||
+ }
|
||||
+ select {
|
||||
+ case err = <-ch:
|
||||
+ if err != nil {
|
||||
+ return 0, fmt.Errorf("failed to dryrun: %w; see more in log and report bugs", err)
|
||||
+ }
|
||||
+ case <-time.After(reloadWaitTimeout):
|
||||
+ restartAfterReloadTimeout()
|
||||
+ return 0, fmt.Errorf("dryrun timed out after %s; restarting daed to recover runtime", reloadWaitTimeout)
|
||||
}
|
||||
|
||||
// Running -> false
|
||||
diff --git a/graphql/service/subscription/mutation_utils.go b/graphql/service/subscription/mutation_utils.go
|
||||
index 7e6dc08..e87bf78 100644
|
||||
--- a/graphql/service/subscription/mutation_utils.go
|
||||
+++ b/graphql/service/subscription/mutation_utils.go
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/daeuniverse/dae-wing/dae"
|
||||
"github.com/daeuniverse/dae-wing/db"
|
||||
"github.com/daeuniverse/dae-wing/graphql/internal"
|
||||
+ "github.com/daeuniverse/dae-wing/graphql/service/config"
|
||||
"github.com/daeuniverse/dae-wing/graphql/service/node"
|
||||
"github.com/daeuniverse/dae/common/subscription"
|
||||
"github.com/go-co-op/gocron"
|
||||
@@ -175,8 +176,9 @@ func AutoUpdateVersionByIds(d *gorm.DB, ids []uint) (err error) {
|
||||
}
|
||||
|
||||
var (
|
||||
- schedulerCache = make(map[uint]*gocron.Scheduler)
|
||||
- schedulerMu sync.RWMutex
|
||||
+ schedulerCache = make(map[uint]*gocron.Scheduler)
|
||||
+ schedulerMu sync.RWMutex
|
||||
+ subscriptionUpdateMu sync.Mutex
|
||||
)
|
||||
|
||||
func UpdateAll(ctx context.Context) {
|
||||
@@ -262,68 +264,161 @@ func Update(ctx context.Context, _id graphql.ID) (r *Resolver, err error) {
|
||||
return &Resolver{Subscription: m}, nil
|
||||
}
|
||||
|
||||
-func UpdateById(ctx context.Context, subId uint) (sub *db.Subscription, err error) {
|
||||
- // Fetch node links.
|
||||
- var m db.Subscription
|
||||
- if err = db.DB(ctx).Where(&db.Subscription{ID: subId}).First(&m).Error; err != nil {
|
||||
- return nil, err
|
||||
+func reconcileSubscriptionNodes(tx *gorm.DB, subId uint, links []string) error {
|
||||
+ var existing []db.Node
|
||||
+ if err := tx.Where("subscription_id = ?", subId).Find(&existing).Error; err != nil {
|
||||
+ return err
|
||||
}
|
||||
- links, err := fetchLinks(m.Link)
|
||||
- if err != nil {
|
||||
- return nil, err
|
||||
+
|
||||
+ incoming := make(map[string]struct{}, len(links))
|
||||
+ for _, link := range links {
|
||||
+ incoming[link] = struct{}{}
|
||||
}
|
||||
|
||||
- tx := db.BeginTx(ctx)
|
||||
- defer func() {
|
||||
- if err == nil {
|
||||
- err = tx.Commit().Error
|
||||
+ current := make(map[string]struct{}, len(existing))
|
||||
+ var staleIds []uint
|
||||
+ for _, n := range existing {
|
||||
+ if _, ok := incoming[n.Link]; ok {
|
||||
+ current[n.Link] = struct{}{}
|
||||
+ continue
|
||||
+ }
|
||||
+ staleIds = append(staleIds, n.ID)
|
||||
+ }
|
||||
+
|
||||
+ fixed := make(map[uint]struct{})
|
||||
+ if len(staleIds) > 0 {
|
||||
+ var fixedIds []uint
|
||||
+ if err := tx.Table("group_nodes").
|
||||
+ Distinct("group_nodes.node_id").
|
||||
+ Joins("INNER JOIN groups ON groups.id = group_nodes.group_id").
|
||||
+ Where("groups.policy = ?", "fixed").
|
||||
+ Where("group_nodes.node_id IN ?", staleIds).
|
||||
+ Pluck("group_nodes.node_id", &fixedIds).Error; err != nil {
|
||||
+ return err
|
||||
+ }
|
||||
+ for _, id := range fixedIds {
|
||||
+ fixed[id] = struct{}{}
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ var detachIds, deleteIds []uint
|
||||
+ for _, id := range staleIds {
|
||||
+ if _, ok := fixed[id]; ok {
|
||||
+ detachIds = append(detachIds, id)
|
||||
} else {
|
||||
- tx.Rollback()
|
||||
+ deleteIds = append(deleteIds, id)
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ if len(staleIds) > 0 {
|
||||
+ if err := node.AutoUpdateVersionByIds(tx, staleIds); err != nil {
|
||||
+ return err
|
||||
+ }
|
||||
+ }
|
||||
+ if len(detachIds) > 0 {
|
||||
+ if err := tx.Model(&db.Node{}).
|
||||
+ Where("id IN ?", detachIds).
|
||||
+ Update("subscription_id", nil).Error; err != nil {
|
||||
+ return err
|
||||
+ }
|
||||
+ }
|
||||
+ if len(deleteIds) > 0 {
|
||||
+ if err := tx.Exec("DELETE FROM group_nodes WHERE node_id IN ?", deleteIds).Error; err != nil {
|
||||
+ return err
|
||||
+ }
|
||||
+ if err := tx.Where("id IN ?", deleteIds).Delete(&db.Node{}).Error; err != nil {
|
||||
+ return err
|
||||
}
|
||||
- }()
|
||||
- // Remove those nodes whose subscription are independent from any groups.
|
||||
- subQuery := tx.Raw(`select nodes.id as id
|
||||
- from nodes
|
||||
- inner join group_nodes on group_nodes.node_id = nodes.id
|
||||
- where subscription_id = ?`, subId)
|
||||
-
|
||||
- if err = tx.Where("subscription_id = ?", subId).
|
||||
- Where("id not in (?)", subQuery).
|
||||
- Select(clause.Associations).
|
||||
- Delete(&db.Node{}).Error; err != nil {
|
||||
- return nil, err
|
||||
}
|
||||
- // Import node links.
|
||||
+
|
||||
+ seen := make(map[string]struct{}, len(links))
|
||||
var args []*internal.ImportArgument
|
||||
for _, link := range links {
|
||||
+ if _, ok := seen[link]; ok {
|
||||
+ continue
|
||||
+ }
|
||||
+ seen[link] = struct{}{}
|
||||
+ if _, ok := current[link]; ok {
|
||||
+ continue
|
||||
+ }
|
||||
args = append(args, &internal.ImportArgument{Link: link})
|
||||
}
|
||||
- result, err := node.Import(tx, false, &subId, args)
|
||||
+ result, err := node.Import(tx, true, &subId, args)
|
||||
if err != nil {
|
||||
- return nil, err
|
||||
+ return errors.New("failed to import subscription node")
|
||||
}
|
||||
- hasAnyCandidate := false
|
||||
for _, r := range result {
|
||||
- if r.Error == nil || *r.Error == node.DuplicatedError.Error() {
|
||||
- hasAnyCandidate = true
|
||||
- break
|
||||
+ if r.Error != nil {
|
||||
+ return errors.New("failed to import subscription node")
|
||||
}
|
||||
}
|
||||
- if !hasAnyCandidate {
|
||||
- return nil, fmt.Errorf("interrupt to update subscription: no any valid node can be imported")
|
||||
+
|
||||
+ var count int64
|
||||
+ if err := tx.Model(&db.Node{}).Where("subscription_id = ?", subId).Count(&count).Error; err != nil {
|
||||
+ return err
|
||||
}
|
||||
- // Update updated_at and return the latest version.
|
||||
- if err = tx.Model(&m).
|
||||
+ if count == 0 {
|
||||
+ return fmt.Errorf("interrupt to update subscription: no any valid node can be imported")
|
||||
+ }
|
||||
+ return nil
|
||||
+}
|
||||
+
|
||||
+func updateSubscriptionTx(ctx context.Context, m *db.Subscription, subId uint, links []string) (err error) {
|
||||
+ subscriptionUpdateMu.Lock()
|
||||
+ defer subscriptionUpdateMu.Unlock()
|
||||
+
|
||||
+ tx := db.BeginTx(ctx)
|
||||
+ if tx.Error != nil {
|
||||
+ return tx.Error
|
||||
+ }
|
||||
+ defer func() {
|
||||
+ if err != nil {
|
||||
+ tx.Rollback()
|
||||
+ }
|
||||
+ }()
|
||||
+ var current db.Subscription
|
||||
+ if err = tx.Where(&db.Subscription{ID: subId}).First(¤t).Error; err != nil {
|
||||
+ return err
|
||||
+ }
|
||||
+ if current.Link != m.Link {
|
||||
+ return fmt.Errorf("subscription changed during update; retry")
|
||||
+ }
|
||||
+ *m = current
|
||||
+
|
||||
+ if err = reconcileSubscriptionNodes(tx, subId, links); err != nil {
|
||||
+ return err
|
||||
+ }
|
||||
+ q := tx.Model(m).
|
||||
Clauses(clause.Returning{}).
|
||||
Where(&db.Subscription{ID: subId}).
|
||||
- Update("updated_at", time.Now()).Error; err != nil {
|
||||
- return nil, err
|
||||
+ Update("updated_at", time.Now())
|
||||
+ if q.Error != nil {
|
||||
+ return q.Error
|
||||
+ }
|
||||
+ if q.RowsAffected == 0 {
|
||||
+ return fmt.Errorf("no such subscription")
|
||||
}
|
||||
-
|
||||
- // Update modified if subscription is referenced by running config.
|
||||
if err = AutoUpdateVersionByIds(tx, []uint{subId}); err != nil {
|
||||
+ return err
|
||||
+ }
|
||||
+ return tx.Commit().Error
|
||||
+}
|
||||
+
|
||||
+func UpdateById(ctx context.Context, subId uint) (sub *db.Subscription, err error) {
|
||||
+ var m db.Subscription
|
||||
+ if err = db.DB(ctx).Where(&db.Subscription{ID: subId}).First(&m).Error; err != nil {
|
||||
+ return nil, err
|
||||
+ }
|
||||
+ links, err := fetchLinks(m.Link)
|
||||
+ if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
+ if err = updateSubscriptionTx(ctx, &m, subId, links); err != nil {
|
||||
+ return nil, err
|
||||
+ }
|
||||
+ if err = config.ApplyIfRunning(ctx); err != nil {
|
||||
+ return nil, fmt.Errorf("subscription updated but failed to apply runtime: %w", err)
|
||||
+ }
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
@@ -406,6 +501,8 @@ func UpdateLink(ctx context.Context, _id graphql.ID, link string) (r *Resolver,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
+ subscriptionUpdateMu.Lock()
|
||||
+ defer subscriptionUpdateMu.Unlock()
|
||||
|
||||
tx := db.BeginTx(ctx)
|
||||
defer func() {
|
||||
diff --git a/graphql/service/subscription/reconcile_patch_test.go b/graphql/service/subscription/reconcile_patch_test.go
|
||||
new file mode 100644
|
||||
index 0000000..18ff9b9
|
||||
--- /dev/null
|
||||
+++ b/graphql/service/subscription/reconcile_patch_test.go
|
||||
@@ -0,0 +1,212 @@
|
||||
+/*
|
||||
+ * SPDX-License-Identifier: AGPL-3.0-only
|
||||
+ * Copyright (c) 2023, daeuniverse Organization <team@v2raya.org>
|
||||
+ */
|
||||
+
|
||||
+package subscription
|
||||
+
|
||||
+import (
|
||||
+ "context"
|
||||
+ "fmt"
|
||||
+ "testing"
|
||||
+ "time"
|
||||
+
|
||||
+ "github.com/daeuniverse/dae-wing/db"
|
||||
+)
|
||||
+
|
||||
+func mustNoError(t *testing.T, err error) {
|
||||
+ t.Helper()
|
||||
+ if err != nil {
|
||||
+ t.Fatal(err)
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+func assertError(t *testing.T, err error) {
|
||||
+ t.Helper()
|
||||
+ if err == nil {
|
||||
+ t.Fatal("expected error")
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+func assertEqualValues(t *testing.T, want, got interface{}) {
|
||||
+ t.Helper()
|
||||
+ if fmt.Sprint(want) != fmt.Sprint(got) {
|
||||
+ t.Fatalf("want %v, got %v", want, got)
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+func assertNotNil(t *testing.T, value *uint) {
|
||||
+ t.Helper()
|
||||
+ if value == nil {
|
||||
+ t.Fatal("expected non-nil value")
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+func assertNil(t *testing.T, value *uint) {
|
||||
+ t.Helper()
|
||||
+ if value != nil {
|
||||
+ t.Fatalf("expected nil, got %v", *value)
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+func assertZero(t *testing.T, value int64) {
|
||||
+ t.Helper()
|
||||
+ if value != 0 {
|
||||
+ t.Fatalf("expected zero, got %d", value)
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+func initReconcileTestDB(t *testing.T) context.Context {
|
||||
+ t.Helper()
|
||||
+ mustNoError(t, db.InitDatabase(t.TempDir()))
|
||||
+ return context.Background()
|
||||
+}
|
||||
+
|
||||
+func createTestSubscription(t *testing.T, ctx context.Context, links ...string) (db.Subscription, []db.Node) {
|
||||
+ t.Helper()
|
||||
+ sub := db.Subscription{
|
||||
+ UpdatedAt: time.Now(),
|
||||
+ Link: "https://example.invalid/subscription",
|
||||
+ Status: "",
|
||||
+ Info: "",
|
||||
+ }
|
||||
+ mustNoError(t, db.DB(ctx).Create(&sub).Error)
|
||||
+
|
||||
+ nodes := make([]db.Node, 0, len(links))
|
||||
+ for i, link := range links {
|
||||
+ node := db.Node{
|
||||
+ Link: link,
|
||||
+ Name: "node",
|
||||
+ Address: "192.0.2.1",
|
||||
+ Protocol: "ss",
|
||||
+ SubscriptionID: &sub.ID,
|
||||
+ }
|
||||
+ node.Name += string(rune('A' + i))
|
||||
+ mustNoError(t, db.DB(ctx).Create(&node).Error)
|
||||
+ nodes = append(nodes, node)
|
||||
+ }
|
||||
+ return sub, nodes
|
||||
+}
|
||||
+
|
||||
+func createTestGroup(t *testing.T, ctx context.Context, policy string, systemID *uint, nodes ...db.Node) db.Group {
|
||||
+ t.Helper()
|
||||
+ group := db.Group{Name: policy + time.Now().Format("150405.000000000"), Policy: policy, SystemID: systemID}
|
||||
+ mustNoError(t, db.DB(ctx).Create(&group).Error)
|
||||
+ mustNoError(t, db.DB(ctx).Model(&group).Association("Node").Append(nodes))
|
||||
+ return group
|
||||
+}
|
||||
+
|
||||
+func countGroupNode(t *testing.T, ctx context.Context, groupID, nodeID uint) int64 {
|
||||
+ t.Helper()
|
||||
+ var count int64
|
||||
+ mustNoError(t, db.DB(ctx).Table("group_nodes").
|
||||
+ Where("group_id = ? AND node_id = ?", groupID, nodeID).
|
||||
+ Count(&count).Error)
|
||||
+ return count
|
||||
+}
|
||||
+
|
||||
+func TestReconcileKeepsCurrentReferencedNode(t *testing.T) {
|
||||
+ ctx := initReconcileTestDB(t)
|
||||
+ sub, nodes := createTestSubscription(t, ctx, "ss://current")
|
||||
+ group := createTestGroup(t, ctx, "min_moving_avg", nil, nodes[0])
|
||||
+
|
||||
+ mustNoError(t, reconcileSubscriptionNodes(db.DB(ctx), sub.ID, []string{"ss://current"}))
|
||||
+
|
||||
+ var got db.Node
|
||||
+ mustNoError(t, db.DB(ctx).First(&got, nodes[0].ID).Error)
|
||||
+ assertNotNil(t, got.SubscriptionID)
|
||||
+ assertEqualValues(t, sub.ID, *got.SubscriptionID)
|
||||
+ assertEqualValues(t, 1, countGroupNode(t, ctx, group.ID, got.ID))
|
||||
+}
|
||||
+
|
||||
+func TestReconcileDeletesStaleUnreferencedNode(t *testing.T) {
|
||||
+ ctx := initReconcileTestDB(t)
|
||||
+ sub, nodes := createTestSubscription(t, ctx, "ss://current", "ss://stale")
|
||||
+
|
||||
+ mustNoError(t, reconcileSubscriptionNodes(db.DB(ctx), sub.ID, []string{"ss://current"}))
|
||||
+
|
||||
+ var count int64
|
||||
+ mustNoError(t, db.DB(ctx).Model(&db.Node{}).Where("id = ?", nodes[1].ID).Count(&count).Error)
|
||||
+ assertZero(t, count)
|
||||
+}
|
||||
+
|
||||
+func TestReconcileDeletesStaleNormalGroupNode(t *testing.T) {
|
||||
+ ctx := initReconcileTestDB(t)
|
||||
+ system := db.System{Running: true}
|
||||
+ mustNoError(t, db.DB(ctx).Create(&system).Error)
|
||||
+ sub, nodes := createTestSubscription(t, ctx, "ss://current", "ss://stale")
|
||||
+ group := createTestGroup(t, ctx, "min_moving_avg", &system.ID, nodes...)
|
||||
+
|
||||
+ mustNoError(t, reconcileSubscriptionNodes(db.DB(ctx), sub.ID, []string{"ss://current"}))
|
||||
+
|
||||
+ var gotGroup db.Group
|
||||
+ mustNoError(t, db.DB(ctx).First(&gotGroup, group.ID).Error)
|
||||
+ assertEqualValues(t, 1, gotGroup.Version)
|
||||
+ assertEqualValues(t, 0, countGroupNode(t, ctx, group.ID, nodes[1].ID))
|
||||
+}
|
||||
+
|
||||
+func TestReconcileDetachesStaleFixedNode(t *testing.T) {
|
||||
+ ctx := initReconcileTestDB(t)
|
||||
+ sub, nodes := createTestSubscription(t, ctx, "ss://current", "ss://fixed-stale")
|
||||
+ group := createTestGroup(t, ctx, "fixed", nil, nodes[1])
|
||||
+
|
||||
+ mustNoError(t, reconcileSubscriptionNodes(db.DB(ctx), sub.ID, []string{"ss://current"}))
|
||||
+
|
||||
+ var got db.Node
|
||||
+ mustNoError(t, db.DB(ctx).First(&got, nodes[1].ID).Error)
|
||||
+ assertNil(t, got.SubscriptionID)
|
||||
+ assertEqualValues(t, 1, countGroupNode(t, ctx, group.ID, got.ID))
|
||||
+}
|
||||
+
|
||||
+func TestReconcilePreservesNodeReferencedByFixedAndNormalGroups(t *testing.T) {
|
||||
+ ctx := initReconcileTestDB(t)
|
||||
+ sub, nodes := createTestSubscription(t, ctx, "ss://current", "ss://shared-stale")
|
||||
+ fixed := createTestGroup(t, ctx, "fixed", nil, nodes[1])
|
||||
+ normal := createTestGroup(t, ctx, "min", nil, nodes[1])
|
||||
+
|
||||
+ mustNoError(t, reconcileSubscriptionNodes(db.DB(ctx), sub.ID, []string{"ss://current"}))
|
||||
+
|
||||
+ var got db.Node
|
||||
+ mustNoError(t, db.DB(ctx).First(&got, nodes[1].ID).Error)
|
||||
+ assertNil(t, got.SubscriptionID)
|
||||
+ assertEqualValues(t, 1, countGroupNode(t, ctx, fixed.ID, got.ID))
|
||||
+ assertEqualValues(t, 1, countGroupNode(t, ctx, normal.ID, got.ID))
|
||||
+}
|
||||
+
|
||||
+func TestReconcileAllLinksAlreadyExistSucceeds(t *testing.T) {
|
||||
+ ctx := initReconcileTestDB(t)
|
||||
+ sub, nodes := createTestSubscription(t, ctx, "ss://one", "ss://two")
|
||||
+
|
||||
+ mustNoError(t, reconcileSubscriptionNodes(db.DB(ctx), sub.ID, []string{"ss://one", "ss://two", "ss://two"}))
|
||||
+
|
||||
+ var count int64
|
||||
+ mustNoError(t, db.DB(ctx).Model(&db.Node{}).Where("subscription_id = ?", sub.ID).Count(&count).Error)
|
||||
+ assertEqualValues(t, len(nodes), count)
|
||||
+}
|
||||
+
|
||||
+func TestReconcileRollsBackWhenAnyIncomingNodeIsInvalid(t *testing.T) {
|
||||
+ ctx := initReconcileTestDB(t)
|
||||
+ sub, nodes := createTestSubscription(t, ctx, "ss://current", "ss://stale")
|
||||
+ tx := db.BeginTx(ctx)
|
||||
+
|
||||
+ assertError(t, reconcileSubscriptionNodes(tx, sub.ID, []string{"ss://current", "not-a-node-link"}))
|
||||
+ mustNoError(t, tx.Rollback().Error)
|
||||
+
|
||||
+ var count int64
|
||||
+ mustNoError(t, db.DB(ctx).Model(&db.Node{}).Where("id IN ?", []uint{nodes[0].ID, nodes[1].ID}).Count(&count).Error)
|
||||
+ assertEqualValues(t, 2, count)
|
||||
+}
|
||||
+
|
||||
+func TestUpdateSubscriptionRejectsChangedLink(t *testing.T) {
|
||||
+ ctx := initReconcileTestDB(t)
|
||||
+ sub, nodes := createTestSubscription(t, ctx, "ss://current")
|
||||
+ snapshot := sub
|
||||
+ mustNoError(t, db.DB(ctx).Model(&sub).Update("link", "https://example.invalid/changed").Error)
|
||||
+
|
||||
+ assertError(t, updateSubscriptionTx(ctx, &snapshot, sub.ID, []string{"ss://replacement"}))
|
||||
+
|
||||
+ var got db.Node
|
||||
+ mustNoError(t, db.DB(ctx).First(&got, nodes[0].ID).Error)
|
||||
+ assertNotNil(t, got.SubscriptionID)
|
||||
+ assertEqualValues(t, sub.ID, *got.SubscriptionID)
|
||||
+}
|
||||
@@ -1,24 +0,0 @@
|
||||
diff --git a/graphql/service/subscription/mutation_utils.go b/graphql/service/subscription/mutation_utils.go
|
||||
index e01a342..c6bc3e1 100644
|
||||
--- a/graphql/service/subscription/mutation_utils.go
|
||||
+++ b/graphql/service/subscription/mutation_utils.go
|
||||
@@ -171,6 +171,8 @@ func AutoUpdateVersionByIds(d *gorm.DB, ids []uint) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
+const scheduledUpdateTimeout = 3 * time.Minute
|
||||
+
|
||||
var (
|
||||
schedulerCache = make(map[uint]*gocron.Scheduler)
|
||||
schedulerMu sync.RWMutex
|
||||
@@ -224,7 +226,9 @@ func AddUpdateScheduler(ctx context.Context, id uint) {
|
||||
}
|
||||
logrus.Info("Subscription " + tag + " update task enabled, with exp " + sub.CronExp)
|
||||
_, err := s.Cron(sub.CronExp).Do(func() {
|
||||
- if _, err := UpdateById(context.Background(), sub.ID); err != nil {
|
||||
+ ctx, cancel := context.WithTimeout(context.Background(), scheduledUpdateTimeout)
|
||||
+ defer cancel()
|
||||
+ if _, err := UpdateById(ctx, sub.ID); err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
})
|
||||
@@ -1,73 +0,0 @@
|
||||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
Subject: [PATCH] daed: open sqlite database in WAL mode
|
||||
MIME-Version: 1.0
|
||||
Content-Type: text/plain; charset=UTF-8
|
||||
Content-Transfer-Encoding: 8bit
|
||||
|
||||
订阅自动更新写库时,WebUI 登录查询被锁 5 秒后报 database is locked。
|
||||
|
||||
开 WAL 并把 busy_timeout 提到 30 秒,读不再被写事务阻塞;WAL 附带的两个文件跟主库一样收紧到 0640。
|
||||
---
|
||||
db/db.go | 17 ++++++++++++-----
|
||||
pkg/sqlite/sqlite_mipsarch.go | 4 +++-
|
||||
pkg/sqlite/sqlite_others.go | 4 +++-
|
||||
3 files changed, 18 insertions(+), 7 deletions(-)
|
||||
|
||||
diff --git a/db/db.go b/db/db.go
|
||||
index a79022b..6145443 100644
|
||||
--- a/db/db.go
|
||||
+++ b/db/db.go
|
||||
@@ -50,13 +50,20 @@ func InitDatabase(configDir string) (err error) {
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
- if fi, err := os.Stat(path); err != nil {
|
||||
- return err
|
||||
- } else if fi.Mode()&0037 > 0 {
|
||||
- // Too open, chmod it to 0640.
|
||||
- if err = os.Chmod(path, 0640); err != nil {
|
||||
+ for _, p := range []string{path, path + "-wal", path + "-shm"} {
|
||||
+ fi, err := os.Stat(p)
|
||||
+ if os.IsNotExist(err) {
|
||||
+ continue
|
||||
+ }
|
||||
+ if err != nil {
|
||||
return err
|
||||
}
|
||||
+ if fi.Mode()&0037 > 0 {
|
||||
+ // Too open, chmod it to 0640.
|
||||
+ if err = os.Chmod(p, 0640); err != nil {
|
||||
+ return err
|
||||
+ }
|
||||
+ }
|
||||
}
|
||||
|
||||
return nil
|
||||
diff --git a/pkg/sqlite/sqlite_mipsarch.go b/pkg/sqlite/sqlite_mipsarch.go
|
||||
index 7b8c1b4..8c275e5 100644
|
||||
--- a/pkg/sqlite/sqlite_mipsarch.go
|
||||
+++ b/pkg/sqlite/sqlite_mipsarch.go
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
+const pragmas = "?_journal_mode=WAL&_busy_timeout=30000&_synchronous=FULL"
|
||||
+
|
||||
func Open(dsn string) gorm.Dialector {
|
||||
- return sqlite.Open(dsn)
|
||||
+ return sqlite.Open(dsn + pragmas)
|
||||
}
|
||||
diff --git a/pkg/sqlite/sqlite_others.go b/pkg/sqlite/sqlite_others.go
|
||||
index 4598651..83d8b80 100644
|
||||
--- a/pkg/sqlite/sqlite_others.go
|
||||
+++ b/pkg/sqlite/sqlite_others.go
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
+const pragmas = "?_pragma=journal_mode(WAL)&_pragma=busy_timeout(30000)&_pragma=synchronous(FULL)"
|
||||
+
|
||||
func Open(dsn string) gorm.Dialector {
|
||||
- return sqlite.Open(dsn)
|
||||
+ return sqlite.Open(dsn + pragmas)
|
||||
}
|
||||
@@ -1,166 +0,0 @@
|
||||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
Subject: [PATCH] daed: reload dae outside the database transaction
|
||||
MIME-Version: 1.0
|
||||
Content-Type: text/plain; charset=UTF-8
|
||||
Content-Transfer-Encoding: 8bit
|
||||
|
||||
配置生效前先提交读事务,reload 完再用短事务写运行状态。
|
||||
|
||||
原来整个 reload 都在事务里,最长可占着数据库锁一百多秒;写状态时按 ID 重查仍存在的分组,避免把 reload 期间被删的分组重新插回去。
|
||||
---
|
||||
graphql/service/config/mutation_utils.go | 77 ++++++++++++++++--------
|
||||
1 file changed, 53 insertions(+), 24 deletions(-)
|
||||
|
||||
diff --git a/graphql/service/config/mutation_utils.go b/graphql/service/config/mutation_utils.go
|
||||
index feb94d3..59f0ebd 100644
|
||||
--- a/graphql/service/config/mutation_utils.go
|
||||
+++ b/graphql/service/config/mutation_utils.go
|
||||
@@ -270,28 +270,12 @@ func restartAfterReloadTimeout() {
|
||||
}()
|
||||
}
|
||||
|
||||
-func runTransaction(ctx context.Context, noLoad bool) (n int32, err error) {
|
||||
- tx := db.BeginTx(ctx)
|
||||
- if tx.Error != nil {
|
||||
- return 0, tx.Error
|
||||
- }
|
||||
- n, err = runLocked(tx, noLoad)
|
||||
- if err != nil {
|
||||
- tx.Rollback()
|
||||
- return 0, err
|
||||
- }
|
||||
- if err = tx.Commit().Error; err != nil {
|
||||
- return 0, err
|
||||
- }
|
||||
- return n, nil
|
||||
-}
|
||||
-
|
||||
func Run(ctx context.Context, noLoad bool) (n int32, err error) {
|
||||
if ok := runLock.TryLock(); !ok {
|
||||
return 0, fmt.Errorf("the last request didn't complete; make a cup of tea and take a break")
|
||||
}
|
||||
defer runLock.Unlock()
|
||||
- return runTransaction(ctx, noLoad)
|
||||
+ return runLocked(ctx, noLoad)
|
||||
}
|
||||
|
||||
func ApplyIfRunning(ctx context.Context) error {
|
||||
@@ -307,11 +291,11 @@ func ApplyIfRunning(ctx context.Context) error {
|
||||
if err != nil || !modified {
|
||||
return err
|
||||
}
|
||||
- _, err = runTransaction(ctx, false)
|
||||
+ _, err = runLocked(ctx, false)
|
||||
return err
|
||||
}
|
||||
|
||||
-func runLocked(d *gorm.DB, noLoad bool) (n int32, err error) {
|
||||
+func runLocked(ctx context.Context, noLoad bool) (n int32, err error) {
|
||||
//// Dry run.
|
||||
if noLoad {
|
||||
ch := make(chan error)
|
||||
@@ -336,18 +320,38 @@ func runLocked(d *gorm.DB, noLoad bool) (n int32, err error) {
|
||||
}
|
||||
|
||||
// Running -> false
|
||||
+ tx := db.BeginTx(ctx)
|
||||
+ if tx.Error != nil {
|
||||
+ return 0, tx.Error
|
||||
+ }
|
||||
var sys db.System
|
||||
- if err = d.Model(&db.System{}).FirstOrCreate(&sys).Error; err != nil {
|
||||
+ if err = tx.Model(&db.System{}).FirstOrCreate(&sys).Error; err != nil {
|
||||
+ tx.Rollback()
|
||||
return 0, err
|
||||
}
|
||||
- if err = d.Model(&sys).Updates(map[string]interface{}{
|
||||
+ if err = tx.Model(&sys).Updates(map[string]interface{}{
|
||||
"running": false,
|
||||
}).Error; err != nil {
|
||||
+ tx.Rollback()
|
||||
+ return 0, err
|
||||
+ }
|
||||
+ if err = tx.Commit().Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
+ d := db.BeginTx(ctx)
|
||||
+ if d.Error != nil {
|
||||
+ return 0, d.Error
|
||||
+ }
|
||||
+ readTx := d
|
||||
+ defer func() {
|
||||
+ if readTx != nil {
|
||||
+ readTx.Rollback()
|
||||
+ }
|
||||
+ }()
|
||||
+
|
||||
//// Run selected global+dns+routing.
|
||||
/// Get them from database and parse them to daeConfig.
|
||||
var mConfig db.Config
|
||||
@@ -513,6 +517,11 @@ func runLocked(d *gorm.DB, noLoad bool) (n int32, err error) {
|
||||
c.Node = append(c.Node, daeConfig.KeyableString(fmt.Sprintf("%v:%v", node.uniqueName, node.dbNode.Link)))
|
||||
}
|
||||
|
||||
+ if err = readTx.Commit().Error; err != nil {
|
||||
+ return 0, err
|
||||
+ }
|
||||
+ readTx = nil
|
||||
+
|
||||
/// Reload with current config.
|
||||
chReloadCallback := make(chan error)
|
||||
reloadMsg := &dae.ReloadMessage{
|
||||
@@ -536,20 +545,37 @@ func runLocked(d *gorm.DB, noLoad bool) (n int32, err error) {
|
||||
}
|
||||
|
||||
// Save running status
|
||||
+ tx := db.BeginTx(context.WithoutCancel(ctx))
|
||||
+ if tx.Error != nil {
|
||||
+ return 0, tx.Error
|
||||
+ }
|
||||
+ defer func() {
|
||||
+ if err != nil {
|
||||
+ tx.Rollback()
|
||||
+ }
|
||||
+ }()
|
||||
var sys db.System
|
||||
- if err = d.Model(&db.System{}).FirstOrCreate(&sys).Error; err != nil {
|
||||
+ if err = tx.Model(&db.System{}).FirstOrCreate(&sys).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var gvs uint
|
||||
var gids []string
|
||||
+ groupIds := make([]uint, 0, len(groups))
|
||||
for _, g := range groups {
|
||||
gvs += g.Version
|
||||
gids = append(gids, fmt.Sprintf("%x", g.ID))
|
||||
+ groupIds = append(groupIds, g.ID)
|
||||
+ }
|
||||
+ liveGroups := make([]db.Group, 0, len(groupIds))
|
||||
+ if len(groupIds) > 0 {
|
||||
+ if err = tx.Where("id IN ?", groupIds).Find(&liveGroups).Error; err != nil {
|
||||
+ return 0, err
|
||||
+ }
|
||||
}
|
||||
sort.Slice(gids, func(i, j int) bool {
|
||||
return gids[i] < gids[j]
|
||||
})
|
||||
- if err = d.Model(&sys).Updates(map[string]interface{}{
|
||||
+ if err = tx.Model(&sys).Updates(map[string]interface{}{
|
||||
"running": true,
|
||||
"running_config_id": mConfig.ID,
|
||||
"running_config_version": mConfig.Version,
|
||||
@@ -562,7 +588,10 @@ func runLocked(d *gorm.DB, noLoad bool) (n int32, err error) {
|
||||
}).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
- if err = d.Model(&sys).Association("RunningGroups").Replace(groups); err != nil {
|
||||
+ if err = tx.Model(&sys).Association("RunningGroups").Replace(liveGroups); err != nil {
|
||||
+ return 0, err
|
||||
+ }
|
||||
+ if err = tx.Commit().Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
Subject: [PATCH] daed: serialize scheduled subscription updates
|
||||
MIME-Version: 1.0
|
||||
Content-Type: text/plain; charset=UTF-8
|
||||
Content-Transfer-Encoding: 8bit
|
||||
|
||||
订阅更新的入口读也纳入同一把锁,整轮更新串行,排到才开始计时。
|
||||
|
||||
多个订阅定时任务撞在同一分钟时会互相锁死。
|
||||
---
|
||||
.../service/subscription/mutation_utils.go | 29 +++++++++++++------
|
||||
1 file changed, 20 insertions(+), 9 deletions(-)
|
||||
|
||||
diff --git a/graphql/service/subscription/mutation_utils.go b/graphql/service/subscription/mutation_utils.go
|
||||
index c6bc3e1..e2aec32 100644
|
||||
--- a/graphql/service/subscription/mutation_utils.go
|
||||
+++ b/graphql/service/subscription/mutation_utils.go
|
||||
@@ -225,12 +225,8 @@ func AddUpdateScheduler(ctx context.Context, id uint) {
|
||||
tag = *sub.Tag
|
||||
}
|
||||
logrus.Info("Subscription " + tag + " update task enabled, with exp " + sub.CronExp)
|
||||
- _, err := s.Cron(sub.CronExp).Do(func() {
|
||||
- ctx, cancel := context.WithTimeout(context.Background(), scheduledUpdateTimeout)
|
||||
- defer cancel()
|
||||
- if _, err := UpdateById(ctx, sub.ID); err != nil {
|
||||
- logrus.Error(err)
|
||||
- }
|
||||
+ _, err := s.Cron(sub.CronExp).SingletonMode().Do(func() {
|
||||
+ runScheduledUpdate(sub.ID)
|
||||
})
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to schedule subscription %d update: invalid cron expression '%s': %v", sub.ID, sub.CronExp, err)
|
||||
@@ -364,9 +360,6 @@ func reconcileSubscriptionNodes(tx *gorm.DB, subId uint, links []string) error {
|
||||
}
|
||||
|
||||
func updateSubscriptionTx(ctx context.Context, m *db.Subscription, subId uint, links []string) (err error) {
|
||||
- subscriptionUpdateMu.Lock()
|
||||
- defer subscriptionUpdateMu.Unlock()
|
||||
-
|
||||
tx := db.BeginTx(ctx)
|
||||
if tx.Error != nil {
|
||||
return tx.Error
|
||||
@@ -404,7 +397,25 @@ func updateSubscriptionTx(ctx context.Context, m *db.Subscription, subId uint, l
|
||||
return tx.Commit().Error
|
||||
}
|
||||
|
||||
+func runScheduledUpdate(subId uint) {
|
||||
+ subscriptionUpdateMu.Lock()
|
||||
+ defer subscriptionUpdateMu.Unlock()
|
||||
+
|
||||
+ ctx, cancel := context.WithTimeout(context.Background(), scheduledUpdateTimeout)
|
||||
+ defer cancel()
|
||||
+ if _, err := updateByIdLocked(ctx, subId); err != nil {
|
||||
+ logrus.Error(err)
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
func UpdateById(ctx context.Context, subId uint) (sub *db.Subscription, err error) {
|
||||
+ subscriptionUpdateMu.Lock()
|
||||
+ defer subscriptionUpdateMu.Unlock()
|
||||
+
|
||||
+ return updateByIdLocked(ctx, subId)
|
||||
+}
|
||||
+
|
||||
+func updateByIdLocked(ctx context.Context, subId uint) (sub *db.Subscription, err error) {
|
||||
var m db.Subscription
|
||||
if err = db.DB(ctx).Where(&db.Subscription{ID: subId}).First(&m).Error; err != nil {
|
||||
return nil, err
|
||||
@@ -1,28 +0,0 @@
|
||||
From: kenzok8 <kenzok8@noreply>
|
||||
Date: 2026-09-23
|
||||
Subject: [PATCH] daed: keep reload callbacks nonblocking
|
||||
|
||||
A timed-out request must not block the daemon when reload later finishes.
|
||||
---
|
||||
diff --git a/graphql/service/config/mutation_utils.go b/graphql/service/config/mutation_utils.go
|
||||
index 5996eaa..64229e8 100644
|
||||
--- a/graphql/service/config/mutation_utils.go
|
||||
+++ b/graphql/service/config/mutation_utils.go
|
||||
@@ -298,7 +298,7 @@ func ApplyIfRunning(ctx context.Context) error {
|
||||
func runLocked(ctx context.Context, noLoad bool) (n int32, err error) {
|
||||
//// Dry run.
|
||||
if noLoad {
|
||||
- ch := make(chan error)
|
||||
+ ch := make(chan error, 1)
|
||||
reloadMsg := &dae.ReloadMessage{
|
||||
Config: dae.EmptyConfig,
|
||||
Callback: ch,
|
||||
@@ -523,7 +523,7 @@ func runLocked(ctx context.Context, noLoad bool) (n int32, err error) {
|
||||
readTx = nil
|
||||
|
||||
/// Reload with current config.
|
||||
- chReloadCallback := make(chan error)
|
||||
+ chReloadCallback := make(chan error, 1)
|
||||
reloadMsg := &dae.ReloadMessage{
|
||||
Config: c,
|
||||
Callback: chReloadCallback,
|
||||
Reference in new issue
Block a user