refactor: reorganize cluster and cluster2 packages; remove deprecated files and introduce new error handling structures

This commit is contained in:
tt
2025-01-15 10:20:39 +08:00
parent 9221153d84
commit 5e51bdf5f1
123 changed files with 436 additions and 303 deletions

View File

@@ -30,25 +30,25 @@ http {
# api负载均衡
upstream wukongimapi {
server 192.168.3.54:5001;
server 192.168.3.54:5002;
server 192.168.3.54:5003;
server 192.168.3.57:5001;
server 192.168.3.57:5002;
server 192.168.3.57:5003;
}
# demo负载均衡
upstream wukongimdemo {
server 192.168.3.54:5172;
server 192.168.3.57:5172;
}
# manager负载均衡
upstream wukongimanager {
server 192.168.3.54:5300;
server 192.168.3.54:5302;
server 192.168.3.54:5303;
server 192.168.3.57:5300;
server 192.168.3.57:5302;
server 192.168.3.57:5303;
}
# ws负载均衡
upstream wukongimws {
server 192.168.3.54:5210;
server 192.168.3.54:5220;
server 192.168.3.54:5230;
server 192.168.3.57:5210;
server 192.168.3.57:5220;
server 192.168.3.57:5230;
}
# http api转发
server {
@@ -110,9 +110,9 @@ http {
stream {
# tcp负载均衡
upstream wukongimtcp {
server 192.168.3.54:5110;
server 192.168.3.54:5120;
server 192.168.3.54:5130;
server 192.168.3.57:5110;
server 192.168.3.57:5120;
server 192.168.3.57:5130;
}
server {
listen 5100;

View File

@@ -6,19 +6,19 @@ global:
scrape_configs:
- job_name: node-exporter
static_configs:
- targets: ['192.168.3.54:9100']
- targets: ['192.168.3.57:9100']
- job_name: wukongim-trace-metrics-1
static_configs:
- targets: ['192.168.3.54:5300']
- targets: ['192.168.3.57:5300']
labels:
id: "1"
- job_name: wukongim-trace-metrics-2
static_configs:
- targets: ['192.168.3.54:5302']
- targets: ['192.168.3.57:5302']
labels:
id: "2"
- job_name: wukongim-trace-metrics-3
static_configs:
- targets: ['192.168.3.54:5303']
- targets: ['192.168.3.57:5303']
labels:
id: "3"

View File

@@ -11,7 +11,7 @@ import (
"github.com/WuKongIM/WuKongIM/internal/options"
"github.com/WuKongIM/WuKongIM/internal/service"
"github.com/WuKongIM/WuKongIM/internal/types"
cluster "github.com/WuKongIM/WuKongIM/pkg/cluster2/cluster"
cluster "github.com/WuKongIM/WuKongIM/pkg/cluster/cluster"
"github.com/WuKongIM/WuKongIM/pkg/wkdb"
"github.com/WuKongIM/WuKongIM/pkg/wkhttp"
"github.com/WuKongIM/WuKongIM/pkg/wklog"

View File

@@ -6,7 +6,7 @@ import (
"github.com/WuKongIM/WuKongIM/internal/options"
"github.com/WuKongIM/WuKongIM/internal/service"
cluster "github.com/WuKongIM/WuKongIM/pkg/cluster2/cluster"
cluster "github.com/WuKongIM/WuKongIM/pkg/cluster/cluster"
"github.com/WuKongIM/WuKongIM/pkg/trace"
"github.com/WuKongIM/WuKongIM/pkg/wkhttp"
"github.com/WuKongIM/WuKongIM/pkg/wklog"

View File

@@ -9,7 +9,7 @@ import (
"github.com/WuKongIM/WuKongIM/internal/options"
"github.com/WuKongIM/WuKongIM/internal/service"
cluster "github.com/WuKongIM/WuKongIM/pkg/cluster2/cluster"
cluster "github.com/WuKongIM/WuKongIM/pkg/cluster/cluster"
"github.com/WuKongIM/WuKongIM/pkg/trace"
"github.com/WuKongIM/WuKongIM/pkg/wkhttp"
"github.com/WuKongIM/WuKongIM/pkg/wklog"

View File

@@ -10,7 +10,7 @@ import (
"github.com/WuKongIM/WuKongIM/internal/eventbus"
"github.com/WuKongIM/WuKongIM/internal/options"
"github.com/WuKongIM/WuKongIM/internal/service"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/network"
"github.com/WuKongIM/WuKongIM/pkg/wkdb"
"github.com/WuKongIM/WuKongIM/pkg/wkhttp"

View File

@@ -158,14 +158,6 @@ func (i *Ingress) handleUpdateTag(c *wkserver.Context) {
}
service.TagManager.SetChannelTag(req.ChannelId, req.ChannelType, newTagKey)
}
} else {
_, err = service.TagManager.MakeTagWithTagKey(tagKey, req.Uids)
if err != nil {
i.Error("handleUpdateTag: make tag failed", zap.Error(err))
c.WriteErr(err)
return
}
}
}
c.WriteOk()

View File

@@ -9,7 +9,7 @@ import (
"github.com/WuKongIM/WuKongIM/internal/options"
"github.com/WuKongIM/WuKongIM/internal/service"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/network"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"

View File

@@ -25,10 +25,10 @@ import (
userevent "github.com/WuKongIM/WuKongIM/internal/user/event"
userhandler "github.com/WuKongIM/WuKongIM/internal/user/handler"
"github.com/WuKongIM/WuKongIM/internal/webhook"
cluster "github.com/WuKongIM/WuKongIM/pkg/cluster2/cluster"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/store"
cluster "github.com/WuKongIM/WuKongIM/pkg/cluster/cluster"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/store"
"github.com/WuKongIM/WuKongIM/pkg/trace"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"github.com/WuKongIM/WuKongIM/pkg/wknet"

View File

@@ -10,7 +10,7 @@ import (
"github.com/WuKongIM/WuKongIM/internal/options"
"github.com/WuKongIM/WuKongIM/pkg/client"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/wkdb"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
wkproto "github.com/WuKongIM/WuKongIMGoProto"

View File

@@ -1,5 +1,5 @@
package service
import "github.com/WuKongIM/WuKongIM/pkg/cluster2/icluster"
import "github.com/WuKongIM/WuKongIM/pkg/cluster/icluster"
var Cluster icluster.ICluster // 分布式接口

View File

@@ -1,5 +1,5 @@
package service
import "github.com/WuKongIM/WuKongIM/pkg/cluster2/store"
import "github.com/WuKongIM/WuKongIM/pkg/cluster/store"
var Store *store.Store // 存储相关接口

View File

@@ -1,7 +1,7 @@
package channel
import (
"github.com/WuKongIM/WuKongIM/pkg/cluster2/icluster"
"github.com/WuKongIM/WuKongIM/pkg/cluster/icluster"
"github.com/WuKongIM/WuKongIM/pkg/raft/raftgroup"
"github.com/WuKongIM/WuKongIM/pkg/raft/types"
"github.com/WuKongIM/WuKongIM/pkg/wkdb"

View File

@@ -36,7 +36,12 @@ func NewServer(opts *Options) *Server {
}
s.storage = newStorage(opts.DB, s)
for i := 0; i < opts.GroupCount; i++ {
rg := raftgroup.New(raftgroup.NewOptions(raftgroup.WithLogPrefix("channel"), raftgroup.WithTransport(opts.Transport), raftgroup.WithStorage(s.storage)))
rg := raftgroup.New(raftgroup.NewOptions(
raftgroup.WithLogPrefix("channel"),
raftgroup.WithNotNeedApplied(true),
raftgroup.WithTransport(opts.Transport),
raftgroup.WithStorage(s.storage)),
)
s.raftGroups = append(s.raftGroups, rg)
}
s.wake.channels = make(map[string]bool)

View File

@@ -62,6 +62,10 @@ func (s *storage) LeaderLastLogTerm(key string) (uint32, error) {
return s.db.LeaderLastTerm(key)
}
func (s *storage) LeaderTermGreaterEqThan(key string, term uint32) (uint32, error) {
return s.db.LeaderLastTermGreaterEqThan(key, term)
}
func (s *storage) GetLogs(key string, startLogIndex uint64, endLogIndex uint64, limitSize uint64) ([]types.Log, error) {
channelID, channelType := wkutil.ChannelFromlKey(key)
var (

View File

@@ -7,7 +7,7 @@ import (
"net/http"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/network"
"github.com/WuKongIM/WuKongIM/pkg/wkhttp"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"

View File

@@ -6,7 +6,7 @@ import (
"math/rand/v2"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/wkdb"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
"go.uber.org/zap"

View File

@@ -4,7 +4,7 @@ import (
"context"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"

View File

@@ -21,7 +21,12 @@ func (s *Server) onMessage(conn gnet.Conn, m *proto.Message) {
default:
if s.onMessageFnc != nil {
fromNodeId := s.uidToServerId(wkserver.GetUidFromContext(conn))
s.onMessageFnc(fromNodeId, m)
err := s.onMessagePool.Submit(func() {
s.onMessageFnc(fromNodeId, m)
})
if err != nil {
s.Error("onMessage: submit onMessageFnc failed", zap.Error(err))
}
}
}

View File

@@ -6,7 +6,7 @@ import (
"fmt"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/wkdb"
"github.com/WuKongIM/WuKongIM/pkg/wkserver"
"github.com/WuKongIM/WuKongIM/pkg/wkserver/proto"

View File

@@ -8,9 +8,9 @@ import (
"strings"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/store"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/store"
"github.com/WuKongIM/WuKongIM/pkg/network"
rafttype "github.com/WuKongIM/WuKongIM/pkg/raft/types"
"github.com/WuKongIM/WuKongIM/pkg/wkdb"

View File

@@ -4,8 +4,8 @@ import (
"time"
"github.com/WuKongIM/WuKongIM/pkg/auth"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/raft/raftgroup"
)

View File

@@ -4,7 +4,7 @@ import (
"context"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
rafttypes "github.com/WuKongIM/WuKongIM/pkg/raft/types"
"github.com/WuKongIM/WuKongIM/pkg/wkdb"
"github.com/WuKongIM/WuKongIM/pkg/wklog"

View File

@@ -10,12 +10,12 @@ import (
"sync"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/channel"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/event"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/slot"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/store"
"github.com/WuKongIM/WuKongIM/pkg/cluster/channel"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/event"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/slot"
"github.com/WuKongIM/WuKongIM/pkg/cluster/store"
rafttype "github.com/WuKongIM/WuKongIM/pkg/raft/types"
"github.com/WuKongIM/WuKongIM/pkg/trace"
"github.com/WuKongIM/WuKongIM/pkg/wkdb"
@@ -24,6 +24,7 @@ import (
"github.com/WuKongIM/WuKongIM/pkg/wkserver/proto"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
"github.com/lni/goutils/syncutil"
"github.com/panjf2000/ants/v2"
"github.com/panjf2000/gnet/v2"
"go.uber.org/zap"
)
@@ -52,11 +53,12 @@ type Server struct {
opts *Options
apiPrefix string // api前缀
wklog.Log
cancelCtx context.Context
cancelFnc context.CancelFunc
onMessageFnc func(fromNodeId uint64, msg *proto.Message)
uptime time.Time // 服务启动时间
stopper *syncutil.Stopper
cancelCtx context.Context
cancelFnc context.CancelFunc
onMessageFnc func(fromNodeId uint64, msg *proto.Message)
onMessagePool *ants.Pool
uptime time.Time // 服务启动时间
stopper *syncutil.Stopper
sync.Mutex
}
@@ -81,6 +83,8 @@ func New(opts *Options) *Server {
opts.ConfigOptions.Transport = newNodeTransport(s)
}
s.onMessagePool, _ = ants.NewPool(6000, ants.WithNonblocking(true))
s.rpcServer = newRpcServer(s)
s.rpcClient = newRpcClient(s)

View File

@@ -6,8 +6,8 @@ import (
"testing"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
rafttypes "github.com/WuKongIM/WuKongIM/pkg/raft/types"
)

View File

@@ -4,7 +4,7 @@ import (
"context"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/wkdb"
"github.com/WuKongIM/WuKongIM/pkg/wkserver"
"github.com/WuKongIM/WuKongIM/pkg/wkserver/proto"

View File

@@ -1,6 +1,6 @@
package icluster
import "github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
import "github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
type Node interface {

View File

@@ -1,6 +1,10 @@
package icluster
import "github.com/WuKongIM/WuKongIM/pkg/raft/types"
import (
"context"
"github.com/WuKongIM/WuKongIM/pkg/raft/types"
)
type Slot interface {
// SlotLeaderId 获取槽领导节点的id
@@ -9,6 +13,7 @@ type Slot interface {
GetSlotId(v string) uint32
// ProposeUntilApplied 提交数据直到应用
ProposeUntilApplied(slotId uint32, data []byte) (*types.ProposeResp, error)
ProposeUntilAppliedTimeout(ctx context.Context, slotId uint32, data []byte) (*types.ProposeResp, error)
// Propose 提交数据
Propose(slotId uint32, data []byte) (*types.ProposeResp, error)
}

View File

@@ -3,7 +3,7 @@ package clusterconfig
import (
"encoding/binary"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
wkproto "github.com/WuKongIM/WuKongIMGoProto"
)

View File

@@ -6,7 +6,7 @@ import (
"sync"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
"go.uber.org/zap"

View File

@@ -1,6 +1,6 @@
package clusterconfig
import "github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
import "github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
type IEvent interface {
// OnConfigChange 配置变更(不要阻塞此方法)

View File

@@ -5,8 +5,8 @@ import (
"os"
"path"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
pb "github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
pb "github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/raft/raft"
rafttypes "github.com/WuKongIM/WuKongIM/pkg/raft/types"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
@@ -81,20 +81,23 @@ func (s *Server) Options() *Options {
func (s *Server) initRaft() error {
var raftConfig rafttypes.Config
if s.config.isInitialized() {
raftConfig = configToRaftConfig(s.config)
raftConfig = s.configToRaftConfig(s.config)
} else {
replicas := make([]uint64, 0, len(s.opts.InitNodes))
var leader uint64
if len(s.opts.InitNodes) > 0 {
for nodeId := range s.opts.InitNodes {
replicas = append(replicas, nodeId)
}
} else {
} else { // 单节点启动
leader = s.opts.NodeId
replicas = append(replicas, s.opts.NodeId)
}
raftConfig = rafttypes.Config{
Replicas: replicas,
Term: 1,
Leader: leader,
}
}
@@ -172,7 +175,7 @@ func (s *Server) switchConfig(cfg *Config) {
s.raft.Step(rafttypes.Event{
Type: rafttypes.ConfChange,
Config: configToRaftConfig(cfg),
Config: s.configToRaftConfig(cfg),
})
}
@@ -287,18 +290,24 @@ func (s *Server) genConfigId() uint64 {
return uint64(s.cfgGenId.Generate().Int64())
}
func configToRaftConfig(cfg *Config) rafttypes.Config {
func (s *Server) configToRaftConfig(cfg *Config) rafttypes.Config {
replicas := make([]uint64, 0, len(cfg.nodes()))
for _, node := range cfg.nodes() {
replicas = append(replicas, node.Id)
}
var leader uint64
if len(replicas) == 1 && replicas[0] == s.opts.NodeId { // 单机模式
leader = s.opts.NodeId
}
return rafttypes.Config{
Replicas: replicas,
MigrateFrom: cfg.cfg.MigrateFrom,
MigrateTo: cfg.cfg.MigrateTo,
Learners: cfg.cfg.Learners,
Term: cfg.cfg.Term,
Leader: leader,
// Term: cfg.cfg.Term, // 不需要设置term不设置表示使用当前termConfig的term只是应用的最新的term不表示是日志的term
}
}

View File

@@ -3,7 +3,7 @@ package clusterconfig
import (
"encoding/binary"
pb "github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
pb "github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/raft/types"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
"go.uber.org/zap"

View File

@@ -3,7 +3,7 @@ package clusterconfig
import (
"encoding/binary"
pb "github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
pb "github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"go.uber.org/zap"
)

View File

@@ -5,7 +5,7 @@ import (
"testing"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/raft/types"
"github.com/stretchr/testify/assert"
)

View File

@@ -6,7 +6,7 @@ import (
"math"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/clusterconfig/key"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/clusterconfig/key"
"github.com/WuKongIM/WuKongIM/pkg/raft/types"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
@@ -75,12 +75,6 @@ func (p *PebbleShardLogStorage) AppendLogs(logs []types.Log, termStartIndexInfo
if err != nil {
return err
}
// 日志最大index
err = p.saveMaxIndexWithWriter(lg.Index, batch, p.noSync)
if err != nil {
return err
}
}
if termStartIndexInfo != nil {
@@ -147,7 +141,7 @@ func (p *PebbleShardLogStorage) TruncateLogTo(index uint64) error {
return err
}
return p.saveMaxIndex(index - 1)
return nil
}
// func (p *PebbleShardLogStorage) realLastIndex(shardNo string) (uint64, error) {
@@ -277,34 +271,46 @@ func (p *PebbleShardLogStorage) FirstIndex() (uint64, error) {
}
func (p *PebbleShardLogStorage) LastIndex() (uint64, error) {
lastIndex, _, err := p.getMaxIndex()
return lastIndex, err
log, err := p.LastLog()
if err != nil {
return 0, err
}
return log.Index, err
}
func (p *PebbleShardLogStorage) LastIndexAndTerm() (uint64, uint32, error) {
lastIndex, err := p.LastIndex()
log, err := p.LastLog()
if err != nil {
return 0, 0, err
}
if lastIndex == 0 {
return 0, 0, nil
}
log, err := p.getLog(lastIndex)
if err != nil {
return 0, 0, err
}
return lastIndex, log.Term, nil
return log.Index, log.Term, nil
}
func (p *PebbleShardLogStorage) LastLog() (types.Log, error) {
lastIndex, err := p.LastIndex()
if err != nil {
return types.Log{}, err
iter := p.db.NewIter(&pebble.IterOptions{
LowerBound: key.NewLogKey(0),
UpperBound: key.NewLogKey(math.MaxUint64),
})
defer iter.Close()
if iter.Last() && iter.Valid() {
data := make([]byte, len(iter.Value()))
copy(data, iter.Value())
timeData := data[len(data)-8:]
tm := binary.BigEndian.Uint64(timeData)
var log types.Log
err := log.Unmarshal(data[:len(data)-8])
if err != nil {
return log, err
}
log.Time = time.Unix(0, int64(tm))
return log, nil
}
if lastIndex == 0 {
return types.Log{}, nil
}
return p.getLog(lastIndex)
return types.EmptyLog, nil
}
func (p *PebbleShardLogStorage) getLog(index uint64) (types.Log, error) {
@@ -332,10 +338,6 @@ func (p *PebbleShardLogStorage) getLog(index uint64) (types.Log, error) {
}
func (p *PebbleShardLogStorage) setLastIndex(index uint64) error {
return p.saveMaxIndex(index)
}
func (p *PebbleShardLogStorage) setAppliedIndex(index uint64) error {
maxIndexKeyData := key.NewAppliedIndexKey()
maxIndexdata := make([]byte, 8)
@@ -366,7 +368,15 @@ func (p *PebbleShardLogStorage) AppliedIndex() (uint64, error) {
}
func (p *PebbleShardLogStorage) LastIndexAndAppendTime() (uint64, uint64, error) {
return p.getMaxIndex()
log, err := p.LastLog()
if err != nil {
return 0, 0, err
}
var tm uint64
if !log.Time.IsZero() {
tm = uint64(log.Time.UnixNano())
}
return log.Index, tm, nil
}
func (p *PebbleShardLogStorage) SetLeaderTermStartIndex(term uint32, index uint64) error {
@@ -411,7 +421,7 @@ func (p *PebbleShardLogStorage) GetTermStartIndex(term uint32) (uint64, error) {
}
// 获取大于或等于term的lastTerm
func (p *PebbleShardLogStorage) LeaderLastTermGreaterThan(term uint32) (uint32, error) {
func (p *PebbleShardLogStorage) LeaderTermGreaterEqThan(term uint32) (uint32, error) {
iter := p.db.NewIter(&pebble.IterOptions{
LowerBound: key.NewLeaderTermStartIndexKey(term),
UpperBound: key.NewLeaderTermStartIndexKey(math.MaxUint32),
@@ -447,10 +457,6 @@ func (p *PebbleShardLogStorage) DeleteLeaderTermStartIndexGreaterThanTerm(term u
return batch.Commit(p.wo)
}
func (p *PebbleShardLogStorage) saveMaxIndex(index uint64) error {
return p.saveMaxIndexWithWriter(index, p.db, p.wo)
}
func (p *PebbleShardLogStorage) saveMaxIndexWithWriter(index uint64, w pebble.Writer, o *pebble.WriteOptions) error {
maxIndexKeyData := key.NewMaxIndexKey()
maxIndexdata := make([]byte, 8)
@@ -462,20 +468,3 @@ func (p *PebbleShardLogStorage) saveMaxIndexWithWriter(index uint64, w pebble.Wr
err := w.Set(maxIndexKeyData, append(maxIndexdata, lastTimeData...), o)
return err
}
// GetMaxIndex 获取最大的index 和最后一次写入的时间
func (p *PebbleShardLogStorage) getMaxIndex() (uint64, uint64, error) {
maxIndexKeyData := key.NewMaxIndexKey()
maxIndexdata, closer, err := p.db.Get(maxIndexKeyData)
if err != nil {
if err == pebble.ErrNotFound {
return 0, 0, nil
}
return 0, 0, err
}
defer closer.Close()
if len(maxIndexdata) == 0 {
return 0, 0, nil
}
return binary.BigEndian.Uint64(maxIndexdata[:8]), binary.BigEndian.Uint64(maxIndexdata[8:]), nil
}

View File

@@ -1,6 +1,6 @@
package event
import "github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
import "github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
type IEvent interface {

View File

@@ -1,7 +1,7 @@
package event
import (
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
)

View File

@@ -1,7 +1,7 @@
package event
import (
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
"go.uber.org/zap"
)

View File

@@ -1,7 +1,7 @@
package event
import (
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
)
func (h *handler) handleSlotLeaderElection() error {

View File

@@ -1,7 +1,7 @@
package event
import (
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
"go.uber.org/zap"
)

View File

@@ -5,8 +5,8 @@ import (
"math/big"
"sync"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
)

View File

@@ -1,14 +1,17 @@
package event
import (
"fmt"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"go.uber.org/zap"
)
// 分布式初始化事件
func (h *handler) handleClusterInit() {
fmt.Println("handleClusterInit--->")
cfg := &types.Config{
SlotCount: h.cfgOptions.SlotCount,
SlotReplicaCount: h.cfgOptions.SlotMaxReplicaCount,

View File

@@ -3,7 +3,7 @@ package event
import (
"fmt"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
)

View File

@@ -4,7 +4,7 @@ import (
"sync"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/clusterconfig"
"github.com/WuKongIM/WuKongIM/pkg/raft/types"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"github.com/lni/goutils/syncutil"
@@ -85,6 +85,7 @@ func (s *Server) handleEvents() {
}
if s.cfgServer.IsLeader() {
// 分布式初始化
if !s.cfgServer.IsInitialized() {
s.handler.handleClusterInit()
@@ -129,9 +130,15 @@ func (s *Server) tick() {
s.pong.Unlock()
if tk >= s.cfgOptions.PongMaxTick && node.Online { // 超过最大pong tick数认为节点离线
s.pong.Lock()
s.pong.tickMap[node.Id] = 0
s.pong.Unlock()
// 节点离线
s.handler.handleOnlineStatus(node.Id, false)
} else if tk < s.cfgOptions.PongMaxTick && !node.Online {
s.pong.Lock()
s.pong.tickMap[node.Id] = 0
s.pong.Unlock()
// 节点在线
s.handler.handleOnlineStatus(node.Id, true)
}

View File

@@ -1,6 +1,6 @@
package handler
import "github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
import "github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
type Handler struct {
}

View File

@@ -1,7 +1,7 @@
package slot
import (
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
rafttype "github.com/WuKongIM/WuKongIM/pkg/raft/types"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
)
@@ -77,9 +77,9 @@ func (s *Server) slotToConfig(st *types.Slot) rafttype.Config {
MigrateTo: st.MigrateTo,
Replicas: st.Replicas,
Learners: st.Learners,
Term: st.Term,
Leader: st.Leader,
Role: role,
// Term: st.Term, // 不需要设置term不设置表示使用当前termtypes.Slot的term只是应用的最新的term不表示是日志的term
Leader: st.Leader,
Role: role,
}
return cfg

View File

@@ -1,6 +1,7 @@
package slot
import (
"context"
"time"
"github.com/WuKongIM/WuKongIM/pkg/raft/types"
@@ -32,6 +33,24 @@ func (s *Server) ProposeUntilApplied(slotId uint32, data []byte) (*types.Propose
return s.raftGroup.ProposeUntilApplied(shardNo, logId, data)
}
func (s *Server) ProposeUntilAppliedTimeout(ctx context.Context, slotId uint32, data []byte) (*types.ProposeResp, error) {
shardNo := SlotIdToKey(slotId)
logId := s.GenLogId()
resps, err := s.raftGroup.ProposeBatchUntilAppliedTimeout(ctx, shardNo, types.ProposeReqSet{
{
Id: logId,
Data: data,
},
})
if err != nil {
return nil, err
}
if len(resps) == 0 {
return nil, nil
}
return resps[0], nil
}
func (s *Server) MustWaitAllSlotsReady(timeout time.Duration) {
}

View File

@@ -1,7 +1,7 @@
package slot
import (
"github.com/WuKongIM/WuKongIM/pkg/cluster2/icluster"
"github.com/WuKongIM/WuKongIM/pkg/cluster/icluster"
"github.com/WuKongIM/WuKongIM/pkg/raft/raftgroup"
"github.com/WuKongIM/WuKongIM/pkg/raft/types"
)

View File

@@ -2,7 +2,6 @@ package slot
import (
"context"
"fmt"
"path"
"strconv"
"sync"
@@ -47,8 +46,6 @@ func (s *Server) Start() error {
return err
}
fmt.Println("slot server start")
err = s.raftGroup.Start()
if err != nil {
return err

View File

@@ -1,7 +1,7 @@
package slot
import (
"github.com/WuKongIM/WuKongIM/pkg/cluster2/node/types"
"github.com/WuKongIM/WuKongIM/pkg/cluster/node/types"
"github.com/WuKongIM/WuKongIM/pkg/raft/raft"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"go.uber.org/zap"

View File

@@ -8,7 +8,7 @@ import (
"math"
"time"
"github.com/WuKongIM/WuKongIM/pkg/cluster2/slot/key"
"github.com/WuKongIM/WuKongIM/pkg/cluster/slot/key"
"github.com/WuKongIM/WuKongIM/pkg/raft/types"
"github.com/WuKongIM/WuKongIM/pkg/wkdb"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
@@ -191,12 +191,6 @@ func (p *PebbleShardLogStorage) AppendLogs(shardNo string, logs []types.Log, ter
return err
}
}
lastLog := logs[len(logs)-1]
err := p.saveMaxIndexWrite(shardNo, lastLog.Index, batch)
if err != nil {
p.Panic("saveMaxIndexWrite failed", zap.Error(err))
return err
}
return batch.Commit(p.sync)
}
@@ -207,12 +201,12 @@ func (p *PebbleShardLogStorage) TruncateLogTo(shardNo string, index uint64) erro
return errors.New("index must be greater than 0")
}
lastIndex, _, err := p.getMaxIndex(shardNo)
lastLog, err := p.lastLog(shardNo)
if err != nil {
p.Error("TruncateLogTo: getMaxIndex error", zap.Error(err))
return err
}
lastIndex := lastLog.Index
if index >= lastIndex {
return nil
}
@@ -233,7 +227,7 @@ func (p *PebbleShardLogStorage) TruncateLogTo(shardNo string, index uint64) erro
if err != nil {
return err
}
return p.saveMaxIndex(shardNo, index)
return nil
}
// func (p *PebbleShardLogStorage) realLastIndex(shardNo string) (uint64, error) {
@@ -379,8 +373,11 @@ func (p *PebbleShardLogStorage) GetLogsInReverseOrder(shardNo string, startLogIn
}
func (p *PebbleShardLogStorage) LastIndex(shardNo string) (uint64, error) {
lastIndex, _, err := p.getMaxIndex(shardNo)
return lastIndex, err
lastLog, err := p.lastLog(shardNo)
if err != nil {
return 0, err
}
return lastLog.Index, err
}
func (p *PebbleShardLogStorage) LastIndexAndTerm(shardNo string) (uint64, uint32, error) {
@@ -428,8 +425,32 @@ func (p *PebbleShardLogStorage) getLog(shardNo string, index uint64) (types.Log,
}
func (p *PebbleShardLogStorage) SetLastIndex(shardNo string, index uint64) error {
return p.saveMaxIndex(shardNo, index)
func (p *PebbleShardLogStorage) lastLog(shardNo string) (types.Log, error) {
iter := p.shardDB(shardNo).NewIter(&pebble.IterOptions{
LowerBound: key.NewLogKey(shardNo, 0),
UpperBound: key.NewLogKey(shardNo, math.MaxUint64),
})
defer iter.Close()
if iter.Last() && iter.Valid() {
data := make([]byte, len(iter.Value()))
copy(data, iter.Value())
logData := data[:len(data)-8]
timeData := data[len(data)-8:]
tm := binary.BigEndian.Uint64(timeData)
var log types.Log
err := log.Unmarshal(logData)
if err != nil {
return types.EmptyLog, err
}
log.Time = time.Unix(0, int64(tm))
return log, nil
}
return types.EmptyLog, nil
}
func (p *PebbleShardLogStorage) SetAppliedIndex(shardNo string, index uint64) error {
@@ -463,10 +484,6 @@ func (p *PebbleShardLogStorage) AppliedIndex(shardNo string) (uint64, error) {
return binary.BigEndian.Uint64(maxIndexdata[:8]), nil
}
func (p *PebbleShardLogStorage) LastIndexAndAppendTime(shardNo string) (uint64, uint64, error) {
return p.getMaxIndex(shardNo)
}
func (p *PebbleShardLogStorage) SetLeaderTermStartIndex(shardNo string, term uint32, index uint64) error {
leaderTermStartIndexKeyData := key.NewLeaderTermStartIndexKey(shardNo, term)
@@ -508,7 +525,7 @@ func (p *PebbleShardLogStorage) GetTermStartIndex(shardNo string, term uint32) (
return binary.BigEndian.Uint64(leaderTermStartIndexData), nil
}
func (p *PebbleShardLogStorage) LeaderLastTermGreaterThan(shardNo string, term uint32) (uint32, error) {
func (p *PebbleShardLogStorage) LeaderTermGreaterEqThan(shardNo string, term uint32) (uint32, error) {
iter := p.shardDB(shardNo).NewIter(&pebble.IterOptions{
LowerBound: key.NewLeaderTermStartIndexKey(shardNo, term),
UpperBound: key.NewLeaderTermStartIndexKey(shardNo, math.MaxUint32),
@@ -543,49 +560,6 @@ func (p *PebbleShardLogStorage) DeleteLeaderTermStartIndexGreaterThanTerm(shardN
return batch.Commit(p.sync)
}
func (p *PebbleShardLogStorage) saveMaxIndex(shardNo string, index uint64) error {
batch := p.shardDB(shardNo).NewBatch()
defer batch.Close()
err := p.saveMaxIndexWrite(shardNo, index, batch)
if err != nil {
return err
}
return batch.Commit(p.sync)
}
func (p *PebbleShardLogStorage) saveMaxIndexWrite(shardNo string, index uint64, w *pebble.Batch) error {
maxIndexKeyData := key.NewMaxIndexKey(shardNo)
maxIndexdata := make([]byte, 8)
binary.BigEndian.PutUint64(maxIndexdata, index)
lastTime := time.Now().UnixNano()
lastTimeData := make([]byte, 8)
binary.BigEndian.PutUint64(lastTimeData, uint64(lastTime))
return w.Set(maxIndexKeyData, append(maxIndexdata, lastTimeData...), p.noSync)
}
// GetMaxIndex 获取最大的index 和最后一次写入的时间
func (p *PebbleShardLogStorage) getMaxIndex(shardNo string) (uint64, uint64, error) {
maxIndexKeyData := key.NewMaxIndexKey(shardNo)
maxIndexdata, closer, err := p.shardDB(shardNo).Get(maxIndexKeyData)
if closer != nil {
defer closer.Close()
}
if err != nil {
if err == pebble.ErrNotFound {
return 0, 0, nil
}
return 0, 0, err
}
if len(maxIndexdata) == 0 {
return 0, 0, nil
}
return binary.BigEndian.Uint64(maxIndexdata[:8]), binary.BigEndian.Uint64(maxIndexdata[8:]), nil
}
// type localStorage struct {
// db *pebble.DB
// dbDir string

View File

@@ -5,6 +5,7 @@ import (
"time"
"github.com/WuKongIM/WuKongIM/pkg/wkdb"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
@@ -18,7 +19,7 @@ func (s *Store) AddOrUpdateConversations(conversations []wkdb.Conversation) erro
}
// 提交最近会话
timeoutctx, cancel := context.WithTimeout(context.Background(), time.Minute*1)
timeoutctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
g, _ := errgroup.WithContext(timeoutctx)
@@ -38,14 +39,19 @@ func (s *Store) AddOrUpdateConversations(conversations []wkdb.Conversation) erro
if err != nil {
return err
}
_, err = s.opts.Slot.ProposeUntilApplied(slotId, cmdData)
_, err = s.opts.Slot.ProposeUntilAppliedTimeout(timeoutctx, slotId, cmdData)
if err != nil {
s.Error("ProposeUntilAppliedTimeout failed", zap.Error(err), zap.Uint32("slotId", slotId), zap.Int("conversations", len(conversations)))
return err
}
return nil
})
}
err := g.Wait()
if err != nil {
s.Error("AddOrUpdateConversations failed", zap.Error(err), zap.Int("conversations", len(conversations)))
return err
}
return err
}

View File

@@ -1,7 +1,7 @@
package store
import (
"github.com/WuKongIM/WuKongIM/pkg/cluster2/icluster"
"github.com/WuKongIM/WuKongIM/pkg/cluster/icluster"
"github.com/WuKongIM/WuKongIM/pkg/wkdb"
)

View File

@@ -9,7 +9,7 @@ import (
// ApplyLogs 应用槽日志
func (s *Store) ApplySlotLogs(slotId uint32, logs []types.Log) error {
for _, log := range logs {
err := s.applyLog(log)
err := s.applyLog(slotId, log)
if err != nil {
s.Panic("apply log err", zap.Error(err), zap.Uint64("index", log.Index), zap.ByteString("data", log.Data))
return err
@@ -18,7 +18,7 @@ func (s *Store) ApplySlotLogs(slotId uint32, logs []types.Log) error {
return nil
}
func (s *Store) applyLog(log types.Log) error {
func (s *Store) applyLog(slotId uint32, log types.Log) error {
cmd := &CMD{}
err := cmd.Unmarshal(log.Data)
if err != nil {

Some files were not shown because too many files have changed in this diff Show More