github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/edge/builtin.go (about)

     1  package edge
     2  
     3  import (
     4  	"github.com/ronaksoft/rony"
     5  	"github.com/ronaksoft/rony/errors"
     6  	"github.com/ronaksoft/rony/store"
     7  	"google.golang.org/protobuf/encoding/protojson"
     8  	"google.golang.org/protobuf/proto"
     9  )
    10  
    11  /*
    12     Creation Time: 2021 - Jan - 11
    13     Created by:  (ehsan)
    14     Maintainers:
    15        1.  Ehsan N. Moosa (E2)
    16     Auditor: Ehsan N. Moosa (E2)
    17     Copyright Ronak Software Group 2020
    18  */
    19  
    20  // Builtin keep track of pages distribution over Edge servers.
    21  type Builtin struct {
    22  	cluster  rony.Cluster
    23  	gateway  rony.Gateway
    24  	store    *store.Store
    25  	serverID string
    26  }
    27  
    28  func newBuiltin(edgeServer *Server) *Builtin {
    29  	b := &Builtin{
    30  		cluster:  edgeServer.Cluster(),
    31  		gateway:  edgeServer.Gateway(),
    32  		serverID: edgeServer.GetServerID(),
    33  	}
    34  
    35  	return b
    36  }
    37  
    38  func (pm *Builtin) getNodes(ctx *RequestCtx, in *rony.MessageEnvelope) {
    39  	req := rony.PoolGetNodes.Get()
    40  	defer rony.PoolGetNodes.Put(req)
    41  	res := rony.PoolEdges.Get()
    42  	defer rony.PoolEdges.Put(res)
    43  
    44  	var err error
    45  	if in.JsonEncoded {
    46  		err = protojson.Unmarshal(in.Message, req)
    47  	} else {
    48  		err = proto.UnmarshalOptions{Merge: true}.Unmarshal(in.Message, req)
    49  	}
    50  	if err != nil {
    51  		ctx.PushError(errors.ErrInvalidRequest)
    52  
    53  		return
    54  	}
    55  
    56  	if pm.cluster == nil {
    57  		res.Nodes = append(res.Nodes, &rony.Edge{
    58  			ReplicaSet: 0,
    59  			ServerID:   pm.serverID,
    60  			HostPorts:  pm.gateway.Addr(),
    61  		})
    62  	} else if len(req.ReplicaSet) == 0 {
    63  		members := pm.cluster.MembersByReplicaSet(pm.cluster.ReplicaSet())
    64  		for _, m := range members {
    65  			res.Nodes = append(res.Nodes, m.Proto(nil))
    66  		}
    67  	} else {
    68  		members := pm.cluster.MembersByReplicaSet(req.ReplicaSet...)
    69  		for _, m := range members {
    70  			res.Nodes = append(res.Nodes, m.Proto(nil))
    71  		}
    72  	}
    73  
    74  	ctx.PushMessage(rony.C_Edges, res)
    75  }
    76  
    77  func (pm *Builtin) getAllNodes(ctx *RequestCtx, in *rony.MessageEnvelope) {
    78  	req := rony.PoolGetNodes.Get()
    79  	defer rony.PoolGetNodes.Put(req)
    80  	res := rony.PoolEdges.Get()
    81  	defer rony.PoolEdges.Put(res)
    82  
    83  	var err error
    84  	if in.JsonEncoded {
    85  		err = protojson.Unmarshal(in.Message, req)
    86  	} else {
    87  		err = proto.UnmarshalOptions{Merge: true}.Unmarshal(in.Message, req)
    88  	}
    89  	if err != nil {
    90  		ctx.PushError(errors.ErrInvalidRequest)
    91  
    92  		return
    93  	}
    94  
    95  	if pm.cluster == nil {
    96  		res.Nodes = append(res.Nodes, &rony.Edge{
    97  			ReplicaSet: 0,
    98  			ServerID:   pm.serverID,
    99  			HostPorts:  pm.gateway.Addr(),
   100  		})
   101  	} else {
   102  		members := pm.cluster.Members()
   103  		for _, m := range members {
   104  			res.Nodes = append(res.Nodes, m.Proto(nil))
   105  		}
   106  	}
   107  	ctx.PushMessage(rony.C_Edges, res)
   108  }
   109  
   110  func (pm *Builtin) ping(ctx *RequestCtx, in *rony.MessageEnvelope) {
   111  	req := rony.PoolPing.Get()
   112  	defer rony.PoolPing.Put(req)
   113  	res := rony.PoolPong.Get()
   114  	defer rony.PoolPong.Put(res)
   115  
   116  	var err error
   117  	if in.JsonEncoded {
   118  		err = protojson.Unmarshal(in.Message, req)
   119  	} else {
   120  		err = proto.UnmarshalOptions{Merge: true}.Unmarshal(in.Message, req)
   121  	}
   122  	if err != nil {
   123  		ctx.PushError(errors.ErrInvalidRequest)
   124  
   125  		return
   126  	}
   127  
   128  	res.ID = req.ID
   129  	ctx.PushMessage(rony.C_Pong, res)
   130  }