mirror of
https://github.com/singchia/frontier.git
synced 2025-10-04 16:02:46 +08:00
optimize: iclm example ui
This commit is contained in:
14
Makefile
14
Makefile
@@ -5,6 +5,7 @@ CC?=cc
|
|||||||
|
|
||||||
all: frontier frontlas
|
all: frontier frontlas
|
||||||
|
|
||||||
|
# binary
|
||||||
.PHONY: frontier
|
.PHONY: frontier
|
||||||
frontier:
|
frontier:
|
||||||
CC=${CC} CGO_ENABLED=1 go build -trimpath -ldflags "-s -w" -o ./bin/frontier cmd/frontier/main.go
|
CC=${CC} CGO_ENABLED=1 go build -trimpath -ldflags "-s -w" -o ./bin/frontier cmd/frontier/main.go
|
||||||
@@ -21,10 +22,12 @@ frontlas:
|
|||||||
frontlas-linux:
|
frontlas-linux:
|
||||||
CC=${CC} GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -trimpath -ldflags "-s -w" -o ./bin/frontlas cmd/frontlas/main.go
|
CC=${CC} GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -trimpath -ldflags "-s -w" -o ./bin/frontlas cmd/frontlas/main.go
|
||||||
|
|
||||||
|
# example
|
||||||
.PHONY: examples
|
.PHONY: examples
|
||||||
examples:
|
examples:
|
||||||
make -C examples
|
make -C examples
|
||||||
|
|
||||||
|
# clean
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
rm ./bin/frontier || true
|
rm ./bin/frontier || true
|
||||||
@@ -32,6 +35,7 @@ clean:
|
|||||||
make clean -C examples
|
make clean -C examples
|
||||||
make clean -C test/bench
|
make clean -C test/bench
|
||||||
|
|
||||||
|
# install
|
||||||
.PHONY: install-frontier
|
.PHONY: install-frontier
|
||||||
install-frontier:
|
install-frontier:
|
||||||
install -m 0755 -d $(DESTDIR)$(BINDIR)
|
install -m 0755 -d $(DESTDIR)$(BINDIR)
|
||||||
@@ -46,10 +50,15 @@ install-frontlas:
|
|||||||
install -m 0755 ./bin/frontier $(DESTDIR)$(BINDIR)
|
install -m 0755 ./bin/frontier $(DESTDIR)$(BINDIR)
|
||||||
install -m 0755 ./etc/frontier.yaml $(DESTDIR)$(CONFDIR)
|
install -m 0755 ./etc/frontier.yaml $(DESTDIR)$(CONFDIR)
|
||||||
|
|
||||||
|
# image
|
||||||
.PHONY: image-frontier
|
.PHONY: image-frontier
|
||||||
image-frontier:
|
image-frontier:
|
||||||
docker buildx build -t ${REGISTRY}/frontier:${VERSION} -f images/Dockerfile.frontier .
|
docker buildx build -t ${REGISTRY}/frontier:${VERSION} -f images/Dockerfile.frontier .
|
||||||
|
|
||||||
|
.PHONY: image-frontlas
|
||||||
|
image-frontlas:
|
||||||
|
docker buildx build -t ${REGISTRY}/frontlas:${VERSION} -f images/Dockerfile.frontlas .
|
||||||
|
|
||||||
.PHONY: image-gen-api
|
.PHONY: image-gen-api
|
||||||
image-gen-api:
|
image-gen-api:
|
||||||
docker buildx build -t image-gen-api:${VERSION} -f images/Dockerfile.controlplane-api .
|
docker buildx build -t image-gen-api:${VERSION} -f images/Dockerfile.controlplane-api .
|
||||||
@@ -58,11 +67,13 @@ image-gen-api:
|
|||||||
image-gen-swagger:
|
image-gen-swagger:
|
||||||
docker buildx build -t frontier-gen-swagger:${VERSION} -f images/Dockerfile.controlplane-swagger .
|
docker buildx build -t frontier-gen-swagger:${VERSION} -f images/Dockerfile.controlplane-swagger .
|
||||||
|
|
||||||
|
# container
|
||||||
.PHONY: container-frontier
|
.PHONY: container-frontier
|
||||||
container-frontier:
|
container-frontier:
|
||||||
docker rm -f frontier
|
docker rm -f frontier
|
||||||
docker run -d --name frontier -p 2431:2431 -p 2432:2432 frontier:${VERSION} --config /usr/conf/frontier.yaml -v 5
|
docker run -d --name frontier -p 2431:2431 -p 2432:2432 frontier:${VERSION} --config /usr/conf/frontier.yaml -v 1
|
||||||
|
|
||||||
|
# api
|
||||||
.PHONY: api-frontier
|
.PHONY: api-frontier
|
||||||
api-frontier:
|
api-frontier:
|
||||||
docker run --rm -v ${PWD}/api/controlplane/frontier/v1:/api/controlplane/frontier/v1 image-gen-api:${VERSION}
|
docker run --rm -v ${PWD}/api/controlplane/frontier/v1:/api/controlplane/frontier/v1 image-gen-api:${VERSION}
|
||||||
@@ -71,6 +82,7 @@ api-frontier:
|
|||||||
api-frontlas:
|
api-frontlas:
|
||||||
docker run --rm -v ${PWD}/api/controlplane/frontlas/v1:/api/controlplane/frontlas/v1 image-gen-api:${VERSION}
|
docker run --rm -v ${PWD}/api/controlplane/frontlas/v1:/api/controlplane/frontlas/v1 image-gen-api:${VERSION}
|
||||||
|
|
||||||
|
# bench
|
||||||
.PHONY: bench
|
.PHONY: bench
|
||||||
bench: container-frontier
|
bench: container-frontier
|
||||||
make bench -C test/bench
|
make bench -C test/bench
|
||||||
|
@@ -1,3 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
func main() {}
|
|
@@ -14,6 +14,9 @@ frontier_plane:
|
|||||||
listen:
|
listen:
|
||||||
network: tcp
|
network: tcp
|
||||||
addr: 0.0.0.0:30021
|
addr: 0.0.0.0:30021
|
||||||
|
expiration:
|
||||||
|
service_meta: 30
|
||||||
|
edge_meta: 30
|
||||||
redis:
|
redis:
|
||||||
mode: standalone
|
mode: standalone
|
||||||
standalone:
|
standalone:
|
||||||
|
@@ -36,7 +36,7 @@ func main() {
|
|||||||
address := pflag.String("address", "127.0.0.1:30012", "address to dial")
|
address := pflag.String("address", "127.0.0.1:30012", "address to dial")
|
||||||
loglevel := pflag.String("loglevel", "info", "log level, trace debug info warn error")
|
loglevel := pflag.String("loglevel", "info", "log level, trace debug info warn error")
|
||||||
meta := pflag.String("meta", "test", "meta to set on connection")
|
meta := pflag.String("meta", "test", "meta to set on connection")
|
||||||
methods := pflag.String("methods", "", "method name, support echo, calculate")
|
methods := pflag.String("methods", "", "method name, support echo")
|
||||||
label := pflag.String("label", "label-01", "label to message or rpc")
|
label := pflag.String("label", "label-01", "label to message or rpc")
|
||||||
|
|
||||||
pflag.Parse()
|
pflag.Parse()
|
||||||
@@ -64,6 +64,20 @@ func main() {
|
|||||||
if *methods != "" {
|
if *methods != "" {
|
||||||
methodSlice = strings.Split(*methods, ",")
|
methodSlice = strings.Split(*methods, ",")
|
||||||
}
|
}
|
||||||
|
go func() {
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
// register
|
||||||
|
for _, method := range methodSlice {
|
||||||
|
switch method {
|
||||||
|
case "echo":
|
||||||
|
err = cli.Register(context.TODO(), "echo", echo)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("\n> register echo err: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// receive on edge
|
// receive on edge
|
||||||
go func() {
|
go func() {
|
||||||
@@ -73,7 +87,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("> receive err:", err)
|
fmt.Println("\n> receive err:", err)
|
||||||
fmt.Println(">>> ")
|
fmt.Println(">>> ")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -89,32 +103,17 @@ func main() {
|
|||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
return
|
return
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
fmt.Println("> accept stream err:", err)
|
fmt.Println("\n> accept stream err:", err)
|
||||||
fmt.Print(">>> ")
|
fmt.Print(">>> ")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Println("> accept stream", st.StreamID())
|
fmt.Println("\n> accept stream", st.StreamID())
|
||||||
fmt.Print(">>> ")
|
fmt.Print(">>> ")
|
||||||
sns.Store(strconv.FormatUint(st.StreamID(), 10), st)
|
sns.Store(strconv.FormatUint(st.StreamID(), 10), st)
|
||||||
go handleStream(st)
|
go handleStream(st)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
|
||||||
time.Sleep(200 * time.Millisecond)
|
|
||||||
// register
|
|
||||||
for _, method := range methodSlice {
|
|
||||||
switch method {
|
|
||||||
case "echo":
|
|
||||||
err = cli.Register(context.TODO(), "echo", echo)
|
|
||||||
if err != nil {
|
|
||||||
armlog.Info("> register echo err:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
cursor := "1"
|
cursor := "1"
|
||||||
fmt.Print(">>> ")
|
fmt.Print(">>> ")
|
||||||
|
|
||||||
@@ -291,7 +290,7 @@ func handleStream(stream geminio.Stream) {
|
|||||||
for {
|
for {
|
||||||
msg, err := stream.Receive(context.TODO())
|
msg, err := stream.Receive(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("\n> stream receive err:", err)
|
fmt.Printf("\n> streamID: %d receive err: %s\n", stream.StreamID(), err)
|
||||||
fmt.Print(">>> ")
|
fmt.Print(">>> ")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -305,11 +304,11 @@ func handleStream(stream geminio.Stream) {
|
|||||||
data := make([]byte, 1024)
|
data := make([]byte, 1024)
|
||||||
_, err := stream.Read(data)
|
_, err := stream.Read(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("\n> stream read err:", err)
|
fmt.Printf("\n> streamID: %d read err: %s\n", stream.StreamID(), err)
|
||||||
fmt.Print(">>> ")
|
fmt.Print(">>> ")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("> read data:", stream.ClientID(),
|
fmt.Println("\n> read data:", stream.ClientID(),
|
||||||
string(data))
|
string(data))
|
||||||
fmt.Print(">>> ")
|
fmt.Print(">>> ")
|
||||||
}
|
}
|
||||||
@@ -331,7 +330,7 @@ func handleStream(stream geminio.Stream) {
|
|||||||
|
|
||||||
func echo(ctx context.Context, req geminio.Request, rsp geminio.Response) {
|
func echo(ctx context.Context, req geminio.Request, rsp geminio.Response) {
|
||||||
edgeID := req.ClientID()
|
edgeID := req.ClientID()
|
||||||
fmt.Printf("\n> call rpc, method: %s edgeID: %d streamID: %d data: %s\n", "echo", edgeID, req.StreamID(), string(req.Data()))
|
fmt.Printf("\n> rpc called, method: %s edgeID: %d streamID: %d data: %s\n", "echo", edgeID, req.StreamID(), string(req.Data()))
|
||||||
fmt.Print(">>> ")
|
fmt.Print(">>> ")
|
||||||
rsp.SetData(req.Data())
|
rsp.SetData(req.Data())
|
||||||
}
|
}
|
||||||
|
@@ -77,7 +77,7 @@ func main() {
|
|||||||
loglevel := pflag.String("loglevel", "info", "log level, trace debug info warn error")
|
loglevel := pflag.String("loglevel", "info", "log level, trace debug info warn error")
|
||||||
serviceName := pflag.String("service", "foo", "service name")
|
serviceName := pflag.String("service", "foo", "service name")
|
||||||
topics := pflag.String("topics", "", "topics to receive message, empty means without consuming")
|
topics := pflag.String("topics", "", "topics to receive message, empty means without consuming")
|
||||||
methods := pflag.String("methods", "", "method name, support echo, calculate")
|
methods := pflag.String("methods", "", "method name, support echo")
|
||||||
printmessage = pflag.Bool("printmessage", false, "whether print message out")
|
printmessage = pflag.Bool("printmessage", false, "whether print message out")
|
||||||
stats := pflag.Bool("stats", false, "print statistics or not")
|
stats := pflag.Bool("stats", false, "print statistics or not")
|
||||||
|
|
||||||
@@ -159,7 +159,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("> receive err:", err)
|
fmt.Println("\n> receive err:", err)
|
||||||
fmt.Print(">>> ")
|
fmt.Print(">>> ")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -185,10 +185,11 @@ func main() {
|
|||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
return
|
return
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
fmt.Println("> accept stream err:", err)
|
fmt.Println("\n> accept stream err:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Println("> accept stream", st.ClientID(), st.StreamID())
|
fmt.Println("\n> accept stream", st.ClientID(), st.StreamID())
|
||||||
|
fmt.Print(">>> ")
|
||||||
sns.Store(strconv.FormatUint(st.StreamID(), 10), st)
|
sns.Store(strconv.FormatUint(st.StreamID(), 10), st)
|
||||||
go handleStream(st)
|
go handleStream(st)
|
||||||
}
|
}
|
||||||
@@ -381,7 +382,7 @@ func handleStream(stream geminio.Stream) {
|
|||||||
for {
|
for {
|
||||||
msg, err := stream.Receive(context.TODO())
|
msg, err := stream.Receive(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("\n> stream receive err:", err)
|
fmt.Printf("\n> streamID: %d receive err: %s\n", stream.StreamID(), err)
|
||||||
fmt.Print(">>> ")
|
fmt.Print(">>> ")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -404,7 +405,7 @@ func handleStream(stream geminio.Stream) {
|
|||||||
data := make([]byte, 1024)
|
data := make([]byte, 1024)
|
||||||
_, err := stream.Read(data)
|
_, err := stream.Read(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("\n> read err:", err)
|
fmt.Printf("\n> streamID: %d read err: %s\n", stream.StreamID(), err)
|
||||||
fmt.Print(">>> ")
|
fmt.Print(">>> ")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -472,7 +473,7 @@ func echo(ctx context.Context, req geminio.Request, rsp geminio.Response) {
|
|||||||
value = ld.Data
|
value = ld.Data
|
||||||
}
|
}
|
||||||
if *printmessage {
|
if *printmessage {
|
||||||
fmt.Printf("\n> call rpc, method: %s edgeID: %d streamID: %d data: %s\n", "echo", edgeID, req.StreamID(), string(value))
|
fmt.Printf("\n> rpc called, method: %s edgeID: %d streamID: %d data: %s\n", "echo", req.ClientID(), req.StreamID(), string(value))
|
||||||
fmt.Print(">>> ")
|
fmt.Print(">>> ")
|
||||||
}
|
}
|
||||||
rsp.SetData(value)
|
rsp.SetData(value)
|
||||||
|
@@ -122,6 +122,10 @@ type Redis struct {
|
|||||||
|
|
||||||
type FrontierManager struct {
|
type FrontierManager struct {
|
||||||
Listen config.Listen `yaml:"listen"`
|
Listen config.Listen `yaml:"listen"`
|
||||||
|
Expiration struct {
|
||||||
|
ServiceMeta int `yaml:"service_meta"` // service meta expiration in redis, in seconds, default 86400s
|
||||||
|
EdgeMeta int `yaml:"edge_meta"` // edge meta expiration in redis, in seconds, default 86400s
|
||||||
|
} `yaml:"expiration,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
@@ -218,6 +222,8 @@ func genDefaultConfig(writer io.Writer) error {
|
|||||||
conf.Redis.Standalone.Network = "tcp"
|
conf.Redis.Standalone.Network = "tcp"
|
||||||
conf.Redis.Standalone.Addr = "127.0.0.1:6379"
|
conf.Redis.Standalone.Addr = "127.0.0.1:6379"
|
||||||
conf.Redis.Standalone.DB = 0
|
conf.Redis.Standalone.DB = 0
|
||||||
|
conf.FrontierManager.Expiration.EdgeMeta = 30
|
||||||
|
conf.FrontierManager.Expiration.ServiceMeta = 30
|
||||||
|
|
||||||
data, err := yaml.Marshal(conf)
|
data, err := yaml.Marshal(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -100,7 +100,8 @@ func (fm *FrontierManager) EdgeOnline(ctx context.Context, req geminio.Request,
|
|||||||
UpdateTime: time.Now().Unix(),
|
UpdateTime: time.Now().Unix(),
|
||||||
}, edgeHeartbeatInterval)
|
}, edgeHeartbeatInterval)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("frontier manager edge online, set edge and alive err: %s", err)
|
klog.Errorf("frontier manager edge online, set edge and alive err: %s, frontierID: %s, edgeID: %d",
|
||||||
|
err, edgeOnline.FrontierID, edgeOnline.EdgeID)
|
||||||
rsp.SetError(err)
|
rsp.SetError(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -116,7 +117,7 @@ func (fm *FrontierManager) EdgeOffline(ctx context.Context, req geminio.Request,
|
|||||||
}
|
}
|
||||||
err = fm.repo.DeleteEdge(edgeOffline.EdgeID)
|
err = fm.repo.DeleteEdge(edgeOffline.EdgeID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("frontier manager edge offline, delete edge err: %s", err)
|
klog.Errorf("frontier manager edge offline, delete edge err: %s, edgeID: %d", err, edgeOffline.EdgeID)
|
||||||
rsp.SetError(err)
|
rsp.SetError(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -132,7 +133,7 @@ func (fm *FrontierManager) EdgeHeartbeat(ctx context.Context, req geminio.Reques
|
|||||||
}
|
}
|
||||||
err = fm.repo.ExpireEdge(edgeHB.EdgeID, edgeHeartbeatInterval)
|
err = fm.repo.ExpireEdge(edgeHB.EdgeID, edgeHeartbeatInterval)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("frontier manager edge heartbeat, expire edge err: %s", err)
|
klog.Errorf("frontier manager edge heartbeat, expire edge err: %s, edgeID: %d", err, edgeHB.EdgeID)
|
||||||
rsp.SetError(err)
|
rsp.SetError(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -154,7 +155,8 @@ func (fm *FrontierManager) ServiceOnline(ctx context.Context, req geminio.Reques
|
|||||||
UpdateTime: time.Now().Unix(),
|
UpdateTime: time.Now().Unix(),
|
||||||
}, serviceHeartbeatInterval)
|
}, serviceHeartbeatInterval)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("frontier manager service online, set service and alive err: %s", err)
|
klog.Errorf("frontier manager service online, set service and alive err: %s, frontierID: %s, serviceID: %d",
|
||||||
|
err, serviceOnline.FrontierID, serviceOnline.ServiceID)
|
||||||
rsp.SetError(err)
|
rsp.SetError(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -170,7 +172,7 @@ func (fm *FrontierManager) ServiceOffline(ctx context.Context, req geminio.Reque
|
|||||||
}
|
}
|
||||||
err = fm.repo.DeleteService(serviceOffline.ServiceID)
|
err = fm.repo.DeleteService(serviceOffline.ServiceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("frontier manager service offline, delete service err: %s", err)
|
klog.Errorf("frontier manager service offline, delete service err: %s, serviceID: %d", err, serviceOffline.ServiceID)
|
||||||
rsp.SetError(err)
|
rsp.SetError(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -186,7 +188,7 @@ func (fm *FrontierManager) ServiceHeartbeat(ctx context.Context, req geminio.Req
|
|||||||
}
|
}
|
||||||
err = fm.repo.ExpireService(serviceHB.ServiceID, serviceHeartbeatInterval)
|
err = fm.repo.ExpireService(serviceHB.ServiceID, serviceHeartbeatInterval)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("frontier manager service heartbeat, expire service err: %s", err)
|
klog.Errorf("frontier manager service heartbeat, expire service err: %s, serviceID: %d", err, serviceHB.ServiceID)
|
||||||
rsp.SetError(err)
|
rsp.SetError(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -203,7 +205,7 @@ func (fm *FrontierManager) FrontierStats(ctx context.Context, req geminio.Reques
|
|||||||
}
|
}
|
||||||
err = fm.repo.SetFrontierCount(stats.FrontierID, stats.EdgeCount, stats.ServiceCount)
|
err = fm.repo.SetFrontierCount(stats.FrontierID, stats.EdgeCount, stats.ServiceCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("frontier manager frontier stats, set frontier count err: %s", err)
|
klog.Errorf("frontier manager frontier stats, set frontier count err: %s, frontierID: %s", err, stats.FrontierID)
|
||||||
rsp.SetError(err)
|
rsp.SetError(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@@ -35,42 +35,43 @@ type RDS interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Dao struct {
|
type Dao struct {
|
||||||
|
conf *config.Configuration
|
||||||
mode int
|
mode int
|
||||||
rds RDS
|
rds RDS
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDao(config *config.Configuration) (*Dao, error) {
|
func NewDao(conf *config.Configuration) (*Dao, error) {
|
||||||
var (
|
var (
|
||||||
rds RDS
|
rds RDS
|
||||||
mode int
|
mode int
|
||||||
)
|
)
|
||||||
conf := config.Redis
|
redisconf := conf.Redis
|
||||||
switch conf.Mode {
|
switch redisconf.Mode {
|
||||||
case "standalone":
|
case "standalone":
|
||||||
sconf := conf.Standalone
|
sconf := redisconf.Standalone
|
||||||
opt := &redis.Options{
|
opt := &redis.Options{
|
||||||
Network: sconf.Network,
|
Network: sconf.Network,
|
||||||
Addr: sconf.Addr,
|
Addr: sconf.Addr,
|
||||||
ClientName: conf.ClientName,
|
ClientName: redisconf.ClientName,
|
||||||
Protocol: conf.Protocol,
|
Protocol: redisconf.Protocol,
|
||||||
Username: conf.Username,
|
Username: redisconf.Username,
|
||||||
Password: conf.Password,
|
Password: redisconf.Password,
|
||||||
DB: sconf.DB,
|
DB: sconf.DB,
|
||||||
MaxRetries: conf.MaxRetries,
|
MaxRetries: redisconf.MaxRetries,
|
||||||
MinRetryBackoff: time.Duration(conf.MinRetryBackoff) * time.Second,
|
MinRetryBackoff: time.Duration(redisconf.MinRetryBackoff) * time.Second,
|
||||||
MaxRetryBackoff: time.Duration(conf.MaxRetryBackoff) * time.Second,
|
MaxRetryBackoff: time.Duration(redisconf.MaxRetryBackoff) * time.Second,
|
||||||
DialTimeout: time.Duration(conf.DialTimeout) * time.Second,
|
DialTimeout: time.Duration(redisconf.DialTimeout) * time.Second,
|
||||||
ReadTimeout: time.Duration(conf.ReadTimeout) * time.Second,
|
ReadTimeout: time.Duration(redisconf.ReadTimeout) * time.Second,
|
||||||
WriteTimeout: time.Duration(conf.WriteTimeout) * time.Second,
|
WriteTimeout: time.Duration(redisconf.WriteTimeout) * time.Second,
|
||||||
PoolFIFO: conf.PoolFIFO,
|
PoolFIFO: redisconf.PoolFIFO,
|
||||||
PoolSize: conf.PoolSize,
|
PoolSize: redisconf.PoolSize,
|
||||||
PoolTimeout: time.Duration(conf.PoolTimeout) * time.Second,
|
PoolTimeout: time.Duration(redisconf.PoolTimeout) * time.Second,
|
||||||
MinIdleConns: conf.MinIdleConns,
|
MinIdleConns: redisconf.MinIdleConns,
|
||||||
MaxIdleConns: conf.MaxIdleConns,
|
MaxIdleConns: redisconf.MaxIdleConns,
|
||||||
ConnMaxIdleTime: time.Duration(conf.ConnMaxIdleTime) * time.Second,
|
ConnMaxIdleTime: time.Duration(redisconf.ConnMaxIdleTime) * time.Second,
|
||||||
ConnMaxLifetime: time.Duration(conf.ConnMaxLifetime) * time.Second,
|
ConnMaxLifetime: time.Duration(redisconf.ConnMaxLifetime) * time.Second,
|
||||||
DisableIndentity: conf.DisableIndentity,
|
DisableIndentity: redisconf.DisableIndentity,
|
||||||
IdentitySuffix: conf.IdentitySuffix,
|
IdentitySuffix: redisconf.IdentitySuffix,
|
||||||
}
|
}
|
||||||
rds = redis.NewClient(opt)
|
rds = redis.NewClient(opt)
|
||||||
_, err := rds.Ping(context.TODO()).Result()
|
_, err := rds.Ping(context.TODO()).Result()
|
||||||
@@ -81,33 +82,33 @@ func NewDao(config *config.Configuration) (*Dao, error) {
|
|||||||
mode = modeStandalone
|
mode = modeStandalone
|
||||||
|
|
||||||
case "sentinel":
|
case "sentinel":
|
||||||
sconf := conf.Sentinel
|
sconf := redisconf.Sentinel
|
||||||
opt := &redis.FailoverOptions{
|
opt := &redis.FailoverOptions{
|
||||||
MasterName: sconf.MasterName,
|
MasterName: sconf.MasterName,
|
||||||
SentinelAddrs: sconf.Addrs,
|
SentinelAddrs: sconf.Addrs,
|
||||||
Protocol: conf.Protocol,
|
Protocol: redisconf.Protocol,
|
||||||
Username: conf.Username,
|
Username: redisconf.Username,
|
||||||
Password: conf.Password,
|
Password: redisconf.Password,
|
||||||
DB: sconf.DB,
|
DB: sconf.DB,
|
||||||
ClientName: conf.ClientName,
|
ClientName: redisconf.ClientName,
|
||||||
RouteByLatency: sconf.RouteByLatency,
|
RouteByLatency: sconf.RouteByLatency,
|
||||||
RouteRandomly: sconf.RouteRandomly,
|
RouteRandomly: sconf.RouteRandomly,
|
||||||
ReplicaOnly: sconf.ReplicaOnly,
|
ReplicaOnly: sconf.ReplicaOnly,
|
||||||
UseDisconnectedReplicas: sconf.UseDisconnectedReplicas,
|
UseDisconnectedReplicas: sconf.UseDisconnectedReplicas,
|
||||||
MinRetryBackoff: time.Duration(conf.MinRetryBackoff) * time.Second,
|
MinRetryBackoff: time.Duration(redisconf.MinRetryBackoff) * time.Second,
|
||||||
MaxRetryBackoff: time.Duration(conf.MaxRetryBackoff) * time.Second,
|
MaxRetryBackoff: time.Duration(redisconf.MaxRetryBackoff) * time.Second,
|
||||||
DialTimeout: time.Duration(conf.DialTimeout) * time.Second,
|
DialTimeout: time.Duration(redisconf.DialTimeout) * time.Second,
|
||||||
ReadTimeout: time.Duration(conf.ReadTimeout) * time.Second,
|
ReadTimeout: time.Duration(redisconf.ReadTimeout) * time.Second,
|
||||||
WriteTimeout: time.Duration(conf.WriteTimeout) * time.Second,
|
WriteTimeout: time.Duration(redisconf.WriteTimeout) * time.Second,
|
||||||
PoolFIFO: conf.PoolFIFO,
|
PoolFIFO: redisconf.PoolFIFO,
|
||||||
PoolSize: conf.PoolSize,
|
PoolSize: redisconf.PoolSize,
|
||||||
PoolTimeout: time.Duration(conf.PoolTimeout) * time.Second,
|
PoolTimeout: time.Duration(redisconf.PoolTimeout) * time.Second,
|
||||||
MinIdleConns: conf.MinIdleConns,
|
MinIdleConns: redisconf.MinIdleConns,
|
||||||
MaxIdleConns: conf.MaxIdleConns,
|
MaxIdleConns: redisconf.MaxIdleConns,
|
||||||
ConnMaxIdleTime: time.Duration(conf.ConnMaxIdleTime) * time.Second,
|
ConnMaxIdleTime: time.Duration(redisconf.ConnMaxIdleTime) * time.Second,
|
||||||
ConnMaxLifetime: time.Duration(conf.ConnMaxLifetime) * time.Second,
|
ConnMaxLifetime: time.Duration(redisconf.ConnMaxLifetime) * time.Second,
|
||||||
DisableIndentity: conf.DisableIndentity,
|
DisableIndentity: redisconf.DisableIndentity,
|
||||||
IdentitySuffix: conf.IdentitySuffix,
|
IdentitySuffix: redisconf.IdentitySuffix,
|
||||||
}
|
}
|
||||||
rds = redis.NewFailoverClient(opt)
|
rds = redis.NewFailoverClient(opt)
|
||||||
_, err := rds.Ping(context.TODO()).Result()
|
_, err := rds.Ping(context.TODO()).Result()
|
||||||
@@ -118,30 +119,30 @@ func NewDao(config *config.Configuration) (*Dao, error) {
|
|||||||
mode = modeSentinel
|
mode = modeSentinel
|
||||||
|
|
||||||
case "cluster":
|
case "cluster":
|
||||||
cconf := conf.Cluster
|
cconf := redisconf.Cluster
|
||||||
opt := &redis.ClusterOptions{
|
opt := &redis.ClusterOptions{
|
||||||
Addrs: cconf.Addrs,
|
Addrs: cconf.Addrs,
|
||||||
Protocol: conf.Protocol,
|
Protocol: redisconf.Protocol,
|
||||||
Username: conf.Username,
|
Username: redisconf.Username,
|
||||||
Password: conf.Password,
|
Password: redisconf.Password,
|
||||||
ClientName: conf.ClientName,
|
ClientName: redisconf.ClientName,
|
||||||
MaxRedirects: cconf.MaxRedirects,
|
MaxRedirects: cconf.MaxRedirects,
|
||||||
RouteByLatency: cconf.RouteByLatency,
|
RouteByLatency: cconf.RouteByLatency,
|
||||||
RouteRandomly: cconf.RouteRandomly,
|
RouteRandomly: cconf.RouteRandomly,
|
||||||
MinRetryBackoff: time.Duration(conf.MinRetryBackoff) * time.Second,
|
MinRetryBackoff: time.Duration(redisconf.MinRetryBackoff) * time.Second,
|
||||||
MaxRetryBackoff: time.Duration(conf.MaxRetryBackoff) * time.Second,
|
MaxRetryBackoff: time.Duration(redisconf.MaxRetryBackoff) * time.Second,
|
||||||
DialTimeout: time.Duration(conf.DialTimeout) * time.Second,
|
DialTimeout: time.Duration(redisconf.DialTimeout) * time.Second,
|
||||||
ReadTimeout: time.Duration(conf.ReadTimeout) * time.Second,
|
ReadTimeout: time.Duration(redisconf.ReadTimeout) * time.Second,
|
||||||
WriteTimeout: time.Duration(conf.WriteTimeout) * time.Second,
|
WriteTimeout: time.Duration(redisconf.WriteTimeout) * time.Second,
|
||||||
PoolFIFO: conf.PoolFIFO,
|
PoolFIFO: redisconf.PoolFIFO,
|
||||||
PoolSize: conf.PoolSize,
|
PoolSize: redisconf.PoolSize,
|
||||||
PoolTimeout: time.Duration(conf.PoolTimeout) * time.Second,
|
PoolTimeout: time.Duration(redisconf.PoolTimeout) * time.Second,
|
||||||
MinIdleConns: conf.MinIdleConns,
|
MinIdleConns: redisconf.MinIdleConns,
|
||||||
MaxIdleConns: conf.MaxIdleConns,
|
MaxIdleConns: redisconf.MaxIdleConns,
|
||||||
ConnMaxIdleTime: time.Duration(conf.ConnMaxIdleTime) * time.Second,
|
ConnMaxIdleTime: time.Duration(redisconf.ConnMaxIdleTime) * time.Second,
|
||||||
ConnMaxLifetime: time.Duration(conf.ConnMaxLifetime) * time.Second,
|
ConnMaxLifetime: time.Duration(redisconf.ConnMaxLifetime) * time.Second,
|
||||||
DisableIndentity: conf.DisableIndentity,
|
DisableIndentity: redisconf.DisableIndentity,
|
||||||
IdentitySuffix: conf.IdentitySuffix,
|
IdentitySuffix: redisconf.IdentitySuffix,
|
||||||
}
|
}
|
||||||
rds = redis.NewClusterClient(opt)
|
rds = redis.NewClusterClient(opt)
|
||||||
_, err := rds.Ping(context.TODO()).Result()
|
_, err := rds.Ping(context.TODO()).Result()
|
||||||
@@ -155,6 +156,7 @@ func NewDao(config *config.Configuration) (*Dao, error) {
|
|||||||
return nil, apis.ErrUnsupportRedisServerMode
|
return nil, apis.ErrUnsupportRedisServerMode
|
||||||
}
|
}
|
||||||
return &Dao{
|
return &Dao{
|
||||||
|
conf: conf,
|
||||||
rds: rds,
|
rds: rds,
|
||||||
mode: mode,
|
mode: mode,
|
||||||
}, nil
|
}, nil
|
||||||
|
@@ -8,7 +8,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/singchia/frontier/pkg/frontlas/apis"
|
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -138,8 +137,9 @@ func (dao *Dao) SetEdgeAndAlive(edgeID uint64, edge *Edge, expiration time.Durat
|
|||||||
}
|
}
|
||||||
|
|
||||||
pipeliner := dao.rds.TxPipeline()
|
pipeliner := dao.rds.TxPipeline()
|
||||||
// edge
|
// edge meta TODO expiration to custom
|
||||||
pipeliner.Set(context.TODO(), getEdgeKey(edgeID), edgeData, 24*time.Hour)
|
pipeliner.Set(context.TODO(), getEdgeKey(edgeID), edgeData,
|
||||||
|
time.Duration(dao.conf.FrontierManager.Expiration.ServiceMeta)*time.Second)
|
||||||
// alive
|
// alive
|
||||||
pipeliner.Set(context.TODO(), getAliveEdgeKey(edgeID), 1, expiration)
|
pipeliner.Set(context.TODO(), getAliveEdgeKey(edgeID), 1, expiration)
|
||||||
// frontier edge_count
|
// frontier edge_count
|
||||||
@@ -154,13 +154,22 @@ func (dao *Dao) SetEdgeAndAlive(edgeID uint64, edge *Edge, expiration time.Durat
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dao *Dao) ExpireEdge(edgeID uint64, expiration time.Duration) error {
|
func (dao *Dao) ExpireEdge(edgeID uint64, expiration time.Duration) error {
|
||||||
ok, err := dao.rds.Expire(context.TODO(), getAliveEdgeKey(edgeID), expiration).Result()
|
pipeliner := dao.rds.TxPipeline()
|
||||||
|
// edge meta TODO expiration to custom
|
||||||
|
pipeliner.Expire(context.TODO(), getEdgeKey(edgeID),
|
||||||
|
time.Duration(dao.conf.FrontierManager.Expiration.ServiceMeta)*time.Second)
|
||||||
|
// edge alive
|
||||||
|
pipeliner.Expire(context.TODO(), getAliveEdgeKey(edgeID), expiration)
|
||||||
|
|
||||||
|
cmds, err := pipeliner.Exec(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("dao expire edge, expire err: %s", err)
|
klog.Errorf("dao expire edge, pipeliner err: %s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !ok {
|
for _, cmd := range cmds {
|
||||||
return apis.ErrExpireFailed
|
if cmd.Err() != nil {
|
||||||
|
return cmd.Err()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -8,7 +8,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/singchia/frontier/pkg/frontlas/apis"
|
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -136,8 +135,9 @@ func (dao *Dao) SetServiceAndAlive(serviceID uint64, service *Service, expiratio
|
|||||||
}
|
}
|
||||||
|
|
||||||
pipeliner := dao.rds.TxPipeline()
|
pipeliner := dao.rds.TxPipeline()
|
||||||
// service, // TODO set expiration
|
// service meta TODO expiration to custom
|
||||||
pipeliner.Set(context.TODO(), getServiceKey(serviceID), serviceData, 24*time.Hour)
|
pipeliner.Set(context.TODO(), getServiceKey(serviceID), serviceData,
|
||||||
|
time.Duration(dao.conf.FrontierManager.Expiration.ServiceMeta)*time.Second)
|
||||||
// alive
|
// alive
|
||||||
pipeliner.Set(context.TODO(), getAliveServiceKey(serviceID), 1, expiration)
|
pipeliner.Set(context.TODO(), getAliveServiceKey(serviceID), 1, expiration)
|
||||||
// frontier service_count
|
// frontier service_count
|
||||||
@@ -152,20 +152,29 @@ func (dao *Dao) SetServiceAndAlive(serviceID uint64, service *Service, expiratio
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dao *Dao) ExpireService(serviceID uint64, expiration time.Duration) error {
|
func (dao *Dao) ExpireService(serviceID uint64, expiration time.Duration) error {
|
||||||
ok, err := dao.rds.Expire(context.TODO(), getAliveServiceKey(serviceID), expiration).Result()
|
pipeliner := dao.rds.TxPipeline()
|
||||||
|
// service meta TODO expiration to custom
|
||||||
|
pipeliner.Expire(context.TODO(), getServiceKey(serviceID),
|
||||||
|
time.Duration(dao.conf.FrontierManager.Expiration.ServiceMeta)*time.Second)
|
||||||
|
// service alive
|
||||||
|
pipeliner.Expire(context.TODO(), getAliveServiceKey(serviceID), expiration)
|
||||||
|
|
||||||
|
cmds, err := pipeliner.Exec(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("dao expire service, expire err: %s", err)
|
klog.Errorf("dao expire service, pipeliner err: %s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !ok {
|
for _, cmd := range cmds {
|
||||||
return apis.ErrExpireFailed
|
if cmd.Err() != nil {
|
||||||
|
return cmd.Err()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dao *Dao) DeleteService(serviceID uint64) error {
|
func (dao *Dao) DeleteService(serviceID uint64) error {
|
||||||
_, err := dao.rds.Eval(context.TODO(), deleteFrontierScript,
|
_, err := dao.rds.Eval(context.TODO(), deleteFrontierScript,
|
||||||
[]string{getServiceKey(serviceID), getAliveServiceKey(serviceID), servicesKeyPrefix}).Result()
|
[]string{getServiceKey(serviceID), getAliveServiceKey(serviceID), frontiersKeyPrefix}).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("dao delete service, eval err: %s", err)
|
klog.Errorf("dao delete service, eval err: %s", err)
|
||||||
return err
|
return err
|
||||||
|
@@ -1,19 +1,19 @@
|
|||||||
local frontier_key = KEYS[1]
|
local service_key = KEYS[1]
|
||||||
local frontier_alive_key = KEYS[2]
|
local service_alive_key = KEYS[2]
|
||||||
local frontier_key_prefix = KEYS[3]
|
local frontier_key_prefix = KEYS[3]
|
||||||
|
|
||||||
-- get frontier and it's frontier_id
|
-- get frontier and it's frontier_id
|
||||||
local frontier = redis.call("GET", frontier_key)
|
local frontier = redis.call("GET", service_key)
|
||||||
if frontier then
|
if frontier then
|
||||||
local value = cjson.decode(frontier)
|
local value = cjson.decode(frontier)
|
||||||
local frontier_id = value['frontier_id']
|
local frontier_id = value['frontier_id']
|
||||||
if frontier_id then
|
if frontier_id then
|
||||||
-- decrement the frontier_count in frontier
|
-- decrement the frontier_count in frontier
|
||||||
local frontier_key = frontier_key_prefix .. tostring(frontier_id)
|
local frontier_key = frontier_key_prefix .. tostring(frontier_id)
|
||||||
redis.call("HINCRBY", frontier_key, "frontier_count", -1)
|
redis.call("HINCRBY", frontier_key, "service_count", -1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- remove frontier alive
|
-- remove frontier alive
|
||||||
local ret = redis.call("DEL", frontier_alive_key)
|
local ret = redis.call("DEL", service_alive_key)
|
||||||
return ret
|
return ret
|
@@ -23,7 +23,7 @@ func (bm *BiMap) Set(key, value any) {
|
|||||||
|
|
||||||
bm.kv[key] = value
|
bm.kv[key] = value
|
||||||
ks, ok := bm.vk[value]
|
ks, ok := bm.vk[value]
|
||||||
if ok {
|
if !ok {
|
||||||
ks = map[any]struct{}{}
|
ks = map[any]struct{}{}
|
||||||
}
|
}
|
||||||
ks[key] = struct{}{}
|
ks[key] = struct{}{}
|
||||||
|
Reference in New Issue
Block a user