optimize: service default buffer size and update online addr notification

This commit is contained in:
Auztin Zhai
2024-05-27 23:59:12 -04:00
parent efd974924d
commit a7030554e5
9 changed files with 44 additions and 26 deletions

View File

@@ -58,8 +58,11 @@ func newclusterServiceEnd(addr string, opts ...ServiceOption) (*clusterServiceEn
cc := clusterv1.NewClusterServiceClient(conn) cc := clusterv1.NewClusterServiceClient(conn)
end := &clusterServiceEnd{ end := &clusterServiceEnd{
cc: cc, cc: cc,
serviceOption: &serviceOption{}, serviceOption: &serviceOption{
readBufferSize: 1024,
writeBufferSize: 1024,
},
rpcs: map[string]geminio.RPC{}, rpcs: map[string]geminio.RPC{},
topics: mapset.NewSet[string](), topics: mapset.NewSet[string](),
edgefrontiers: mapmap.NewBiMap(), edgefrontiers: mapmap.NewBiMap(),

View File

@@ -67,8 +67,8 @@ func newServiceEnd(dialer client.Dialer, opts ...ServiceOption) (*serviceEnd, er
func newRetryServiceEnd(dialer client.Dialer, opts ...ServiceOption) (*serviceEnd, error) { func newRetryServiceEnd(dialer client.Dialer, opts ...ServiceOption) (*serviceEnd, error) {
// options // options
sopt := &serviceOption{ sopt := &serviceOption{
readBufferSize: -1, readBufferSize: 1024,
writeBufferSize: -1, writeBufferSize: 1024,
} }
for _, opt := range opts { for _, opt := range opts {
opt(sopt) opt(sopt)

View File

@@ -59,7 +59,8 @@ func main() {
// get edge // get edge
cli, err := edge.NewEdge(dialer, cli, err := edge.NewEdge(dialer,
edge.OptionEdgeLog(armlog.DefaultLog), edge.OptionEdgeMeta([]byte(*meta))) edge.OptionEdgeLog(armlog.DefaultLog),
edge.OptionEdgeMeta([]byte(*meta)))
if err != nil { if err != nil {
armlog.Info("new edge err:", err) armlog.Info("new edge err:", err)
return return

View File

@@ -102,6 +102,7 @@ func main() {
methods := pflag.String("methods", "", "method name, support echo") 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")
nostdin = pflag.Bool("nostdin", false, "nostdin mode, no stdin will be accepted") nostdin = pflag.Bool("nostdin", false, "nostdin mode, no stdin will be accepted")
buffersize := pflag.Int("buffer", 8192, "buffer size set for service")
stats := pflag.Bool("stats", false, "print statistics or not") stats := pflag.Bool("stats", false, "print statistics or not")
pflag.Parse() pflag.Parse()
@@ -121,7 +122,7 @@ func main() {
opt := []service.ServiceOption{ opt := []service.ServiceOption{
service.OptionServiceLog(armlog.DefaultLog), service.OptionServiceLog(armlog.DefaultLog),
service.OptionServiceName(*serviceName), service.OptionServiceName(*serviceName),
service.OptionServiceBufferSize(8192, 8192)} service.OptionServiceBufferSize(*buffersize, *buffersize)}
if *topics != "" { if *topics != "" {
topicSlice = strings.Split(*topics, ",") topicSlice = strings.Split(*topics, ",")
opt = append(opt, service.OptionServiceReceiveTopics(topicSlice)) opt = append(opt, service.OptionServiceReceiveTopics(topicSlice))

2
go.mod
View File

@@ -1,5 +1,7 @@
module github.com/singchia/frontier module github.com/singchia/frontier
replace "github.com/singchia/geminio" => ../geminio
go 1.22 go 1.22
require ( require (

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
v1 "github.com/singchia/frontier/api/controlplane/frontier/v1" v1 "github.com/singchia/frontier/api/controlplane/frontier/v1"
"github.com/singchia/frontier/pkg/frontier/repo/dao/membuntdb"
"github.com/singchia/frontier/pkg/frontier/repo/model" "github.com/singchia/frontier/pkg/frontier/repo/model"
"github.com/singchia/frontier/pkg/frontier/repo/query" "github.com/singchia/frontier/pkg/frontier/repo/query"
) )
@@ -50,7 +51,11 @@ func (cps *ControlPlaneService) listEdges(_ context.Context, req *v1.ListEdgesRe
} }
count, err := cps.repo.CountEdges(query) count, err := cps.repo.CountEdges(query)
if err != nil { if err != nil {
return nil, err if err != membuntdb.ErrUnimplemented {
return nil, err
} else {
count = -1
}
} }
retEdges := transferEdges(edges) retEdges := transferEdges(edges)
return &v1.ListEdgesResponse{ return &v1.ListEdgesResponse{
@@ -112,7 +117,11 @@ func (cps *ControlPlaneService) listEdgeRPCs(_ context.Context, req *v1.ListEdge
} }
count, err := cps.repo.CountEdgeRPCs(query) count, err := cps.repo.CountEdgeRPCs(query)
if err != nil { if err != nil {
return nil, err if err != membuntdb.ErrUnimplemented {
return nil, err
} else {
count = -1
}
} }
return &v1.ListEdgeRPCsResponse{ return &v1.ListEdgeRPCsResponse{
Rpcs: rpcs, Rpcs: rpcs,

View File

@@ -187,7 +187,6 @@ func (em *edgeManager) ListStreams(edgeID uint64) []geminio.Stream {
} }
func (em *edgeManager) DelEdgeByID(edgeID uint64) error { func (em *edgeManager) DelEdgeByID(edgeID uint64) error {
// TODO test it
em.mtx.RLock() em.mtx.RLock()
defer em.mtx.RUnlock() defer em.mtx.RUnlock()

View File

@@ -68,11 +68,11 @@ func (em *edgeManager) online(end geminio.End) error {
// inform others // inform others
if em.informer != nil { if em.informer != nil {
em.informer.EdgeOnline(end.ClientID(), end.Meta(), end.Addr()) em.informer.EdgeOnline(end.ClientID(), end.Meta(), end.RemoteAddr())
} }
// exchange to service // exchange to service
if em.exchange != nil { if em.exchange != nil {
err := em.exchange.EdgeOnline(end.ClientID(), end.Meta(), end.Addr()) err := em.exchange.EdgeOnline(end.ClientID(), end.Meta(), end.RemoteAddr())
if err == apis.ErrServiceNotOnline { if err == apis.ErrServiceNotOnline {
return nil return nil
} }

View File

@@ -8,9 +8,9 @@ import (
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
armlog "github.com/jumboframes/armorigo/log" armlog "github.com/jumboframes/armorigo/log"
@@ -32,7 +32,8 @@ func main() {
count := pflag.Int("count", 10000, "edges to dial") count := pflag.Int("count", 10000, "edges to dial")
topic := pflag.String("topic", "test", "topic to specific") topic := pflag.String("topic", "test", "topic to specific")
nseconds := pflag.Int("nseconds", 10, "publish message every n seconds for every edge") nseconds := pflag.Int("nseconds", 10, "publish message every n seconds for every edge")
sourceIPs := pflag.String("source_ips", "", "source ips to dial, if your ") msg := pflag.String("msg", "testtesttest", "the message content to publish")
sourceIPs := pflag.String("source_ips", "", "source ips to dial, if you want dial more than \"65535\" source ports")
pprof := pflag.String("pprof", "", "pprof addr to listen") pprof := pflag.String("pprof", "", "pprof addr to listen")
pflag.Parse() pflag.Parse()
@@ -43,7 +44,8 @@ func main() {
} }
ips := []string{} ips := []string{}
idx := 0 idx := int64(0)
if *sourceIPs != "" { if *sourceIPs != "" {
ips = strings.Split(*sourceIPs, ",") ips = strings.Split(*sourceIPs, ",")
idx = 0 idx = 0
@@ -52,9 +54,11 @@ func main() {
dialer := func() (net.Conn, error) { dialer := func() (net.Conn, error) {
if len(ips) != 0 { if len(ips) != 0 {
for retry := 0; retry < 2; retry++ { for retry := 0; retry < 3; retry++ {
thisidx := atomic.LoadInt64(&idx)
ip := ips[int(thisidx)%len(ips)]
localAddr := &net.TCPAddr{ localAddr := &net.TCPAddr{
IP: net.ParseIP(ips[idx]), IP: net.ParseIP(ip),
} }
dialer := &net.Dialer{ dialer := &net.Dialer{
LocalAddr: localAddr, LocalAddr: localAddr,
@@ -66,14 +70,10 @@ func main() {
} }
if strings.Contains(err.Error(), "cannot assign requested address") || if strings.Contains(err.Error(), "cannot assign requested address") ||
strings.Contains(err.Error(), "address already in use") { strings.Contains(err.Error(), "address already in use") {
fmt.Println("source ip:", localAddr.IP.String(), localAddr.Port, err) atomic.AddInt64(&idx, 1)
idx += 1
if idx >= len(ips) {
return nil, err
}
continue continue
} }
fmt.Println("source ip:", localAddr.IP.String(), localAddr.Port) fmt.Printf("unknown dial err: %s, source ip: %s:%d \n", err, localAddr.IP.String(), localAddr.Port)
return nil, err return nil, err
} }
} }
@@ -96,7 +96,11 @@ func main() {
go func(i int) { go func(i int) {
defer wg.Done() defer wg.Done()
// avoid congestion of connection // avoid congestion of connection
random := rand.Intn(*count/100) + 1 n := *count / 100
if n == 0 {
n = 1
}
random := rand.Intn(n) + 1
time.Sleep(time.Second * time.Duration(random)) time.Sleep(time.Second * time.Duration(random))
// new edge connection // new edge connection
cli, err := edge.NewEdge(dialer, cli, err := edge.NewEdge(dialer,
@@ -112,9 +116,8 @@ func main() {
mtx.Unlock() mtx.Unlock()
// publish message in loop // publish message in loop
for { for {
str := strconv.FormatInt(int64(i), 10) gmsg := cli.NewMessage([]byte(*msg))
msg := cli.NewMessage([]byte(str)) err := cli.Publish(context.TODO(), *topic, gmsg)
err := cli.Publish(context.TODO(), *topic, msg)
if err != nil { if err != nil {
fmt.Println("publish err", err) fmt.Println("publish err", err)
break break