|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package ioswitch2
-
- import (
- "context"
- "io"
-
- "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
- "gitlink.org.cn/cloudream/common/pkgs/types"
- "gitlink.org.cn/cloudream/common/utils/io2"
- "gitlink.org.cn/cloudream/common/utils/serder"
- stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
- hubrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/grpc/hub"
- cortypes "gitlink.org.cn/cloudream/jcs-pub/coordinator/types"
- )
-
- var _ = serder.UseTypeUnionExternallyTagged(types.Ref(types.NewTypeUnion[exec.WorkerInfo](
- (*HubWorker)(nil),
- (*HttpHubWorker)(nil),
- )))
-
- type HubWorker struct {
- Hub cortypes.Hub
- Address cortypes.GRPCAddressInfo
- }
-
- func (w *HubWorker) NewClient() (exec.WorkerClient, error) {
- cli, err := stgglb.HubRPCPool.Acquire(stgglb.SelectGRPCAddress(w.Hub, w.Address))
- if err != nil {
- return nil, err
- }
-
- return &HubWorkerClient{hubID: w.Hub.HubID, cli: cli}, nil
- }
-
- func (w *HubWorker) String() string {
- return w.Hub.String()
- }
-
- func (w *HubWorker) Equals(worker exec.WorkerInfo) bool {
- aw, ok := worker.(*HubWorker)
- if !ok {
- return false
- }
-
- return w.Hub.HubID == aw.Hub.HubID
- }
-
- type HubWorkerClient struct {
- hubID cortypes.HubID
- cli *hubrpc.PoolClient
- }
-
- func (c *HubWorkerClient) ExecutePlan(ctx context.Context, plan exec.Plan) error {
- return c.cli.ExecuteIOPlan(ctx, plan)
- }
- func (c *HubWorkerClient) SendStream(ctx context.Context, planID exec.PlanID, id exec.VarID, stream io.ReadCloser) error {
- return c.cli.SendStream(ctx, planID, id, io2.CounterCloser(stream, func(cnt int64, err error) {
- if stgglb.Stats.HubTransfer != nil {
- stgglb.Stats.HubTransfer.RecordOutput(c.hubID, cnt, err == nil || err == io.EOF)
- }
- }))
- }
- func (c *HubWorkerClient) SendVar(ctx context.Context, planID exec.PlanID, id exec.VarID, value exec.VarValue) error {
- return c.cli.SendVar(ctx, planID, id, value)
- }
- func (c *HubWorkerClient) GetStream(ctx context.Context, planID exec.PlanID, streamID exec.VarID, signalID exec.VarID, signal exec.VarValue) (io.ReadCloser, error) {
- str, err := c.cli.GetStream(ctx, planID, streamID, signalID, signal)
- if err != nil {
- return nil, err
- }
-
- return io2.CounterCloser(str, func(cnt int64, err error) {
- if stgglb.Stats.HubTransfer != nil {
- stgglb.Stats.HubTransfer.RecordInput(c.hubID, cnt, err == nil || err == io.EOF)
- }
- }), nil
- }
- func (c *HubWorkerClient) GetVar(ctx context.Context, planID exec.PlanID, varID exec.VarID, signalID exec.VarID, signal exec.VarValue) (exec.VarValue, error) {
- return c.cli.GetVar(ctx, planID, varID, signalID, signal)
- }
- func (c *HubWorkerClient) Close() error {
- stgglb.HubRPCPool.Release(c.cli)
- return nil
- }
|