Skip to content

Commit b81b536

Browse files
committed
feat: finish litefs config env
1 parent 2739d65 commit b81b536

File tree

8 files changed

+45
-15
lines changed

8 files changed

+45
-15
lines changed

docker-compose-dev.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ services:
2424
- AGENT_SECRET=123123
2525
- RUN_MODE=master
2626
- LITEFS_IS_MASTER=true
27+
- LITEFS_LISTEN_ADDR=:20202
2728
ports:
2829
- 8080:8080
2930
- 8888:8888
@@ -51,4 +52,5 @@ services:
5152
- MASTER_ENDPOINT=http://vorker-master:8888
5253
- NODE_NAME=agent-1
5354
- TUNNEL_HOST=vorker-master
54-
- LITEFS_IS_MASTER=false
55+
- LITEFS_IS_MASTER=false
56+
- LITEFS_LISTEN_ADDR=:20202

docker-compose.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services:
55
build: ./
66
# image: vaalacat/vorker
77
volumes:
8-
- /tmp/workerd-master:/workerd # change to your own path
8+
- /tmp/workerd-master:/var/lib/litefs # change to your own path
99
environment:
1010
- WORKER_URL_SUFFIX=.example.com # worker url = SCHEME://WORKERNAME+WORKER_URL_SUFFIX eg: .example.com
1111
- SCHEME=http
@@ -14,6 +14,7 @@ services:
1414
- JWT_SECRET=123123
1515
- AGENT_SECRET=123123
1616
- RUN_MODE=master
17+
- LITEFS_LISTEN_ADDR=:20202
1718
ports:
1819
- 8080:8080 # reverse proxy port
1920
- 8888:8888 # admin webui port
@@ -24,11 +25,12 @@ services:
2425
build: ./
2526
# image: vaalacat/vorker
2627
volumes:
27-
- /tmp/workerd-agent:/workerd # change to your own path
28+
- /tmp/workerd-agent:/var/lib/litefs # change to your own path
2829
environment:
2930
- WORKER_URL_SUFFIX=.example.com # worker url = SCHEME://WORKERNAME+WORKER_URL_SUFFIX eg: .example.com
3031
- AGENT_SECRET=123123 # must be same as master
3132
- RUN_MODE=agent
3233
- MASTER_ENDPOINT=http://vorker-master:8888
3334
- TUNNEL_RELAY_ENDPOINT=vorker-master:18080
3435
- NODE_NAME=agent-1 # must be different amone agents
36+
- LITEFS_LISTEN_ADDR=:20201

litefs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ lease:
66
type: "static"
77
advertise-url: http://127.0.0.1:20202
88
candidate: $LITEFS_IS_MASTER
9+
http:
10+
addr: $LITEFS_LISTEN_ADDR
911
# exec:
1012
# - cmd: "/bin/sh"

models/init.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ func NodeWorkersInit() {
1515
}
1616
logrus.Infof("this node will init %d workers", len(workerRecords))
1717
for _, worker := range workerRecords {
18-
worker.Flush()
19-
utils.GenWorkerConfig(worker.Worker)
18+
if err := worker.Flush(); err != nil {
19+
logrus.WithError(err).Errorf("init failed to flush worker, worker is: [%+v]", worker.Worker)
20+
}
21+
if err := utils.GenWorkerConfig(worker.Worker); err != nil {
22+
logrus.WithError(err).Errorf("init failed to gen worker config, worker is: [%+v]", worker.Worker)
23+
}
2024
exec.ExecManager.RunCmd(worker.GetUID(), []string{})
2125
}
2226
}

models/workerd.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/google/uuid"
1818
"github.com/imroc/req/v3"
19+
"github.com/samber/lo"
1920
"github.com/sirupsen/logrus"
2021
"google.golang.org/protobuf/proto"
2122
"gorm.io/gorm"
@@ -224,7 +225,7 @@ func (w *Worker) Update() error {
224225
w.Port = int32(port)
225226
tunnel.GetClient().Delete(w.GetUID())
226227
tunnel.GetClient().Add(w.GetUID(),
227-
utils.WorkerHostPrefix(w.GetName()), int(w.GetPort()))
228+
utils.WorkerHostPrefix(w.GetName()), port)
228229
err = w.UpdateFile()
229230
if err != nil {
230231
return err
@@ -285,20 +286,15 @@ func (w *Worker) Flush() error {
285286
defs.KeyWorkerProto: wp,
286287
})
287288
}
288-
port, err := utils.GetAvailablePort(defs.DefaultHostName)
289-
if err != nil {
290-
return err
291-
}
292289
if len(w.TunnelID) == 0 {
293290
w.TunnelID = uuid.New().String()
294291
}
295292

296-
if err = w.DeleteFile(); err != nil {
293+
if err := w.DeleteFile(); err != nil {
297294
return err
298295
}
299296

300-
w.Port = int32(port)
301-
if err = w.Update(); err != nil {
297+
if err := w.Update(); err != nil {
302298
return err
303299
}
304300
return nil
@@ -350,9 +346,14 @@ func SyncWorkers(workerList *entities.WorkerList) error {
350346
return err
351347
}
352348

349+
oldWorkerUIDMap := lo.SliceToMap(oldWorkers, func(w *Worker) (string, bool) { return w.UID, true })
350+
353351
for _, worker := range workerList.Workers {
354352
modelWorker := &Worker{Worker: worker}
355353
UIDs = append(UIDs, worker.UID)
354+
if _, ok := oldWorkerUIDMap[worker.UID]; ok {
355+
continue
356+
}
356357

357358
if err := modelWorker.Delete(); err != nil && err != gorm.ErrRecordNotFound {
358359
logrus.WithError(err).Errorf("sync workers db delete error, worker is: %+v", worker)
@@ -377,6 +378,11 @@ func SyncWorkers(workerList *entities.WorkerList) error {
377378
partialFail = true
378379
continue
379380
}
381+
if err := utils.GenWorkerConfig(modelWorker.Worker); err != nil {
382+
logrus.WithError(err).Errorf("sync workers gen config error, worker is: %+v", worker)
383+
partialFail = true
384+
continue
385+
}
380386
}
381387

382388
// delete workers that not in workerList

services/agent/sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ func SyncCall() error {
3737
if err := models.SyncWorkers(workerList); err != nil {
3838
return err
3939
}
40-
return workerd.GenCapnpConfig()
40+
return nil
4141
}

services/workerd/capnp.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"path/filepath"
66
"vorker/conf"
77
"vorker/defs"
8+
"vorker/entities"
89
"vorker/models"
910
"vorker/utils"
1011

@@ -18,10 +19,17 @@ func GenCapnpConfig() error {
1819
}
1920

2021
workerList := models.Trans2Entities(workerRecords)
21-
fileMap := utils.BuildCapfile(workerList)
2222

2323
var hasError bool
2424
for _, worker := range workerList {
25+
w := &models.Worker{Worker: worker}
26+
if err := w.Flush(); err != nil {
27+
logrus.WithError(err).Errorf("failed to flush worker, worker is: %+v", worker)
28+
hasError = true
29+
continue
30+
}
31+
fileMap := utils.BuildCapfile([]*entities.Worker{w.Worker})
32+
2533
if fileContent, ok := fileMap[worker.GetUID()]; ok {
2634
err := utils.WriteFile(
2735
filepath.Join(

services/workerd/flush.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ func FlushAllEndpoint(c *gin.Context) {
5757
return
5858
}
5959

60+
if err := GenCapnpConfig(); err != nil {
61+
common.RespErr(c, common.RespCodeInternalError, err.Error(), nil)
62+
logrus.WithError(err).Error("gen capnp config error")
63+
return
64+
}
65+
6066
common.RespOK(c, "flush worker success", nil)
6167
}
6268

0 commit comments

Comments
 (0)