forked from pingcap/tiflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client: decouple clients out of executor managers (pingcap#46)
* client: decouple clients out of executor managers * refine pkg * add tests * address comment and add more comments
- Loading branch information
1 parent
5355972
commit 766eefe
Showing
17 changed files
with
296 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/hanfei1991/microcosm/model" | ||
"github.com/pingcap/ticdc/dm/pkg/log" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func NewClientManager() *Manager { | ||
return &Manager{ | ||
executors: make(map[model.ExecutorID]ExecutorClient), | ||
} | ||
} | ||
|
||
// TODO: We need to consider when to remove executor client and how to process transilient error. | ||
type Manager struct { | ||
mu sync.RWMutex | ||
|
||
master *MasterClient | ||
executors map[model.ExecutorID]ExecutorClient | ||
} | ||
|
||
func (c *Manager) MasterClient() *MasterClient { | ||
return c.master | ||
} | ||
|
||
func (c *Manager) ExecutorClient(id model.ExecutorID) ExecutorClient { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
return c.executors[id] | ||
} | ||
|
||
// TODO Right now the interface and params are not consistant. We should abstract a "grpc pool" | ||
// interface to maintain a pool of grpc connections. | ||
func (c *Manager) AddMasterClient(ctx context.Context, addrs []string) error { | ||
var err error | ||
c.master, err = NewMasterClient(ctx, addrs) | ||
return err | ||
} | ||
|
||
func (c *Manager) AddExecutor(id model.ExecutorID, addr string) error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
if _, ok := c.executors[id]; ok { | ||
return nil | ||
} | ||
log.L().Info("client manager adds executor", zap.String("id", string(id)), zap.String("addr", addr)) | ||
client, err := newExecutorClient(addr) | ||
if err != nil { | ||
return err | ||
} | ||
c.executors[id] = client | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package client_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hanfei1991/microcosm/client" | ||
"github.com/hanfei1991/microcosm/executor" | ||
"github.com/hanfei1991/microcosm/master" | ||
"github.com/hanfei1991/microcosm/pkg/etcdutils" | ||
"github.com/hanfei1991/microcosm/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestClientManager(t *testing.T) { | ||
test.GlobalTestFlag = true | ||
|
||
manager := client.NewClientManager() | ||
require.Nil(t, manager.MasterClient()) | ||
require.Nil(t, manager.ExecutorClient("abc")) | ||
ctx := context.Background() | ||
err := manager.AddMasterClient(ctx, []string{"127.0.0.1:1992"}) | ||
require.NotNil(t, err) | ||
require.Nil(t, manager.MasterClient()) | ||
|
||
masterCfg := &master.Config{ | ||
Etcd: &etcdutils.ConfigParams{ | ||
Name: "master1", | ||
DataDir: "/tmp/df", | ||
}, | ||
MasterAddr: "127.0.0.1:1992", | ||
KeepAliveTTL: 20000000 * time.Second, | ||
KeepAliveInterval: 200 * time.Millisecond, | ||
RPCTimeout: time.Second, | ||
} | ||
|
||
masterServer, err := master.NewServer(masterCfg, test.NewContext()) | ||
require.Nil(t, err) | ||
|
||
masterCtx, masterCancel := context.WithCancel(ctx) | ||
defer masterCancel() | ||
err = masterServer.Start(masterCtx) | ||
require.Nil(t, err) | ||
|
||
err = manager.AddMasterClient(ctx, []string{"127.0.0.1:1992"}) | ||
require.Nil(t, err) | ||
require.NotNil(t, manager.MasterClient()) | ||
|
||
executorCfg := &executor.Config{ | ||
Join: "127.0.0.1:1992", | ||
WorkerAddr: "127.0.0.1:1993", | ||
KeepAliveTTL: 20000000 * time.Second, | ||
KeepAliveInterval: 200 * time.Millisecond, | ||
RPCTimeout: time.Second, | ||
} | ||
|
||
execServer := executor.NewServer(executorCfg, test.NewContext()) | ||
execCtx, execCancel := context.WithCancel(ctx) | ||
defer execCancel() | ||
err = execServer.Start(execCtx) | ||
require.Nil(t, err) | ||
|
||
err = manager.AddExecutor("executor", "127.0.0.1:1993") | ||
require.Nil(t, err) | ||
require.NotNil(t, manager.ExecutorClient("executor")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.