init
This commit is contained in:
1 parent
48eaf48797
commit
e32e55df15
20 files changed
+369
-117
No files matched your search
+21
-1
@@ -11,6 +11,7 @@ import (
|
||||
var (
|
||||
ErrNoLeader = errors.New("no leader in cluster")
|
||||
ErrLeaderClientNotInitialized = errors.New("leader client not initialized")
|
||||
ErrNotLeader = errors.New("node is not a leader")
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -27,12 +28,20 @@ type ClusterClient struct {
|
||||
}
|
||||
|
||||
func (client *ClusterClient) Shutdown() error {
|
||||
client.leaderMu.Lock()
|
||||
defer client.leaderMu.Unlock()
|
||||
|
||||
if client.leaderClient != nil {
|
||||
client.leaderClient.Close()
|
||||
client.leaderClient = nil
|
||||
}
|
||||
|
||||
return client.r.GracefulShutdown()
|
||||
}
|
||||
|
||||
func (client *ClusterClient) SyncApply(command []byte) (interface{}, error) {
|
||||
client.leaderMu.RLock()
|
||||
defer client.leaderMu.Unlock()
|
||||
defer client.leaderMu.RUnlock()
|
||||
|
||||
if !client.leaderState.leaderElected {
|
||||
return nil, ErrNoLeader
|
||||
@@ -48,6 +57,17 @@ func (client *ClusterClient) SyncApply(command []byte) (interface{}, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (client *ClusterClient) SyncApplyOnLeader(command []byte) (interface{}, error) {
|
||||
client.leaderMu.RLock()
|
||||
defer client.leaderMu.RUnlock()
|
||||
|
||||
if !client.leaderState.isLeader {
|
||||
return nil, ErrNotLeader
|
||||
}
|
||||
|
||||
return client.r.SyncApply(command)
|
||||
}
|
||||
|
||||
func (client *ClusterClient) listenLeader() {
|
||||
for n := range client.r.LeaderCh() {
|
||||
logger.Debug(n)
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"isp-config-service/entity"
|
||||
)
|
||||
|
||||
type CommandType int
|
||||
|
||||
const (
|
||||
UpdateModule CommandType = iota
|
||||
DeleteModule
|
||||
UpdateConfiguration
|
||||
DeleteConfiguration
|
||||
UpdateSchema
|
||||
DeleteSchema
|
||||
)
|
||||
|
||||
type Command interface {
|
||||
Apply(s State) State
|
||||
}
|
||||
|
||||
type UpdateModuleCommand struct {
|
||||
Module entity.Module
|
||||
}
|
||||
|
||||
type operation struct {
|
||||
Command CommandType
|
||||
Payload []byte
|
||||
}
|
||||
|
||||
func UnmarshalCommand(data []byte) (Command, error) {
|
||||
op := operation{}
|
||||
if err := json.Unmarshal(data, &op); err != nil {
|
||||
return nil, errors.WithMessage(err, "unmarshal operation")
|
||||
}
|
||||
|
||||
switch op.Command {
|
||||
case UpdateModule:
|
||||
|
||||
case DeleteModule:
|
||||
case UpdateConfiguration:
|
||||
case DeleteConfiguration:
|
||||
case UpdateSchema:
|
||||
case DeleteSchema:
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
+4
-1
@@ -1,5 +1,8 @@
|
||||
package cluster
|
||||
|
||||
const (
|
||||
applyCommandEvent = "CONFIG_CLUSTER:APPLY_COMMAND"
|
||||
ApplyCommandEvent = "CONFIG_CLUSTER:APPLY_COMMAND"
|
||||
|
||||
TokenParam = "token"
|
||||
ClusterParam = "cluster"
|
||||
)
|
||||
@@ -3,9 +3,7 @@ package cluster
|
||||
import (
|
||||
"github.com/cenkalti/backoff"
|
||||
gosocketio "github.com/integration-system/golang-socketio"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
"github.com/integration-system/isp-lib/logger"
|
||||
"isp-config-service/conf"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
@@ -15,7 +13,7 @@ type SocketLeaderClient struct {
|
||||
}
|
||||
|
||||
func (c *SocketLeaderClient) Send(data []byte, timeout time.Duration) (string, error) {
|
||||
return c.c.Ack(applyCommandEvent, data, timeout)
|
||||
return c.c.Ack(ApplyCommandEvent, data, timeout)
|
||||
}
|
||||
|
||||
func (c *SocketLeaderClient) Dial(timeout time.Duration) error {
|
||||
@@ -29,7 +27,7 @@ func (c *SocketLeaderClient) Close() {
|
||||
}
|
||||
|
||||
func NewSocketLeaderClient(address string) *SocketLeaderClient {
|
||||
socketIoAddress := getSocketIoAddress(address)
|
||||
socketIoAddress := getSocketIoUrl(address)
|
||||
client := gosocketio.NewClientBuilder().
|
||||
EnableReconnection().
|
||||
ReconnectionTimeout(1 * time.Second).
|
||||
@@ -42,12 +40,20 @@ func NewSocketLeaderClient(address string) *SocketLeaderClient {
|
||||
}
|
||||
}
|
||||
|
||||
func getSocketIoAddress(address string) string {
|
||||
func getSocketIoUrl(address string) string {
|
||||
addr, err := net.ResolveTCPAddr("tcp", address)
|
||||
if err != nil {
|
||||
panic(err) //must never occured
|
||||
}
|
||||
|
||||
cfg := config.Get().(*conf.Configuration)
|
||||
return net.JoinHostPort(addr.IP.String(), cfg.WS.Rest.Port)
|
||||
/*cfg := config.Get().(*conf.Configuration)
|
||||
port, err := strconv.Atoi(cfg.WS.Rest.Port)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}*/
|
||||
|
||||
return gosocketio.GetUrl(addr.IP.String(), 9001, false, map[string]string{
|
||||
ClusterParam: "true",
|
||||
}) //TODO for test
|
||||
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package cluster
|
||||
|
||||
import "isp-config-service/entity"
|
||||
|
||||
type State struct {
|
||||
Configurations map[int64]entity.Config
|
||||
Modules map[int32]entity.Module
|
||||
Instances map[int32]entity.Instance
|
||||
Schemas map[int32]entity.ConfigSchema
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/raft"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/mohae/deepcopy"
|
||||
"github.com/pkg/errors"
|
||||
"io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
json = jsoniter.ConfigFastest
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
state State
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func (f *Store) Apply(l *raft.Log) interface{} {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Snapshot returns a snapshot of the key-value store.
|
||||
func (f *Store) Snapshot() (raft.FSMSnapshot, error) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
copied := deepcopy.Copy(f.state).(State)
|
||||
|
||||
return &fsmSnapshot{copied}, nil
|
||||
}
|
||||
|
||||
// Restore stores the key-value store to a previous state.
|
||||
func (f *Store) Restore(rc io.ReadCloser) error {
|
||||
s := State{}
|
||||
|
||||
if err := json.NewDecoder(rc).Decode(&s); err != nil {
|
||||
return errors.WithMessage(err, "unmarshal state")
|
||||
}
|
||||
|
||||
f.state = s
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type fsmSnapshot struct {
|
||||
state State
|
||||
}
|
||||
|
||||
func (f *fsmSnapshot) Persist(sink raft.SnapshotSink) error {
|
||||
err := func() error {
|
||||
b, err := json.Marshal(f.state)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := sink.Write(b); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return sink.Close()
|
||||
}()
|
||||
|
||||
if err != nil {
|
||||
_ = sink.Cancel()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (f *fsmSnapshot) Release() {}
|
||||
Reference in new issue
Block a user