fusen-render/main.go

143 lines
3.6 KiB
Go
Raw Normal View History

2023-07-28 11:04:21 +00:00
package fusenrender
2023-07-29 22:50:58 +00:00
import (
"flag"
"fmt"
2023-07-31 09:09:23 +00:00
"log"
2023-07-29 22:50:58 +00:00
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
2023-07-31 17:46:51 +00:00
"github.com/474420502/execute/triggered"
2023-07-29 22:50:58 +00:00
"github.com/lni/dragonboat/v4"
"github.com/lni/dragonboat/v4/config"
"github.com/lni/dragonboat/v4/logger"
)
2023-08-05 20:26:57 +00:00
var shardID uint64 = 128
2023-07-29 22:50:58 +00:00
var DequeueHandler = triggered.RegisterExecute(func(params *triggered.Params[bool]) {
2023-08-02 10:16:05 +00:00
2023-08-01 03:14:20 +00:00
params.Shared.Value(func(v any) {
2023-08-05 20:26:57 +00:00
cs := stateClient.GetNoOPSession()
for i := 0; ; i++ {
2023-07-31 17:46:51 +00:00
2023-08-05 20:26:57 +00:00
item, err := stateClient.PopItem(cs, "test")
if err != nil {
log.Println(err)
2023-07-31 17:46:51 +00:00
break
2023-08-05 20:26:57 +00:00
}
if item == nil {
return
}
2023-08-03 10:34:20 +00:00
2023-08-07 11:03:22 +00:00
PopChannel <- item
2023-07-31 17:46:51 +00:00
}
2023-08-05 20:26:57 +00:00
})
2023-08-01 11:49:00 +00:00
})
2023-07-31 09:09:23 +00:00
2023-08-05 20:26:57 +00:00
func StartNode(cfg *ConfigServer) {
replicaID := cfg.ServerID
addr := cfg.Address()
2023-07-29 21:11:04 +00:00
// addr := "localhost"
2023-07-29 22:50:58 +00:00
// addr = fmt.Sprintf("%s:%d", addr, port)
flag.Parse()
if len(addr) == 0 && replicaID != 1 && replicaID != 2 && replicaID != 3 {
fmt.Fprintf(os.Stderr, "node id must be 1, 2 or 3 when address is not specified\n")
os.Exit(1)
}
// https://github.com/golang/go/issues/17393
if runtime.GOOS == "darwin" {
signal.Ignore(syscall.Signal(0xd))
}
initialMembers := make(map[uint64]string)
// when joining a new node which is not an initial members, the initialMembers
// map should be empty.
// when restarting a node that is not a member of the initial nodes, you can
// leave the initialMembers to be empty. we still populate the initialMembers
// here for simplicity.
2023-08-06 16:40:26 +00:00
for idx, v := range cfg.Cluster {
2023-07-29 22:50:58 +00:00
// key is the ReplicaID, ReplicaID is not allowed to be 0
// value is the raft address
initialMembers[uint64(idx+1)] = v
}
// for simplicity, in this example program, addresses of all those 3 initial
// raft members are hard coded. when address is not specified on the command
// line, we assume the node being launched is an initial raft member.
var nodeAddr = initialMembers[uint64(replicaID)]
fmt.Fprintf(os.Stdout, "node address: %s\n", nodeAddr)
// change the log verbosity
2023-08-01 03:14:20 +00:00
logger.GetLogger("dragonboat").SetLevel(logger.ERROR)
2023-07-29 22:50:58 +00:00
logger.GetLogger("raft").SetLevel(logger.ERROR)
2023-08-01 03:14:20 +00:00
logger.GetLogger("raftpb").SetLevel(logger.ERROR)
logger.GetLogger("logdb").SetLevel(logger.ERROR)
logger.GetLogger("rsm").SetLevel(logger.ERROR)
logger.GetLogger("transport").SetLevel(logger.ERROR)
logger.GetLogger("grpc").SetLevel(logger.ERROR)
2023-07-29 22:50:58 +00:00
// config for raft node
// See GoDoc for all available options
rc := config.Config{
// ShardID and ReplicaID of the raft node
ReplicaID: uint64(replicaID),
2023-08-05 20:26:57 +00:00
ShardID: shardID,
2023-07-29 22:50:58 +00:00
ElectionRTT: 10,
HeartbeatRTT: 1,
CheckQuorum: true,
2023-08-04 10:59:53 +00:00
SnapshotEntries: 10,
2023-07-29 22:50:58 +00:00
CompactionOverhead: 5,
}
datadir := filepath.Join(
"example-data",
"queue-data",
fmt.Sprintf("node%d", replicaID))
nhc := config.NodeHostConfig{
WALDir: datadir,
// NodeHostDir is where everything else is stored.
NodeHostDir: datadir,
// RTTMillisecond is the average round trip time between NodeHosts (usually
// on two machines/vms), it is in millisecond. Such RTT includes the
// processing delays caused by NodeHosts, not just the network delay between
// two NodeHost instances.
RTTMillisecond: 200,
// RaftAddress is used to identify the NodeHost instance
RaftAddress: nodeAddr,
}
2023-07-31 17:18:55 +00:00
2023-07-29 22:50:58 +00:00
nh, err := dragonboat.NewNodeHost(nhc)
if err != nil {
panic(err)
}
2023-07-30 07:56:18 +00:00
2023-08-05 20:26:57 +00:00
stateClient = &StateClient{nh: nh}
2023-08-04 10:59:53 +00:00
// 把引用计数设置为0
DequeueHandler.RefCountAdd(-1)
2023-08-01 03:14:20 +00:00
// 设置共享的参数
DequeueHandler.WithShared(nh)
2023-07-31 17:18:55 +00:00
2023-08-07 11:03:22 +00:00
if err := nh.StartReplica(initialMembers, false, NewSMQueue, rc); err != nil {
2023-07-29 22:50:58 +00:00
fmt.Fprintf(os.Stderr, "failed to add cluster, %v\n", err)
os.Exit(1)
}
2023-08-05 20:26:57 +00:00
HttpListen(nh, cfg.Port-1000)
2023-07-28 11:04:21 +00:00
}