[#414] ir: Define Control service

Define `ControlService` for IR similar to the one from storage node. Add
`HealthStatus` RPC which returns health status of the IR application.
Implement getters, setters and methods to sign/verify the messages.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-06-09 18:37:03 +03:00 committed by Alex Vanin
parent 41a30d6ec2
commit 93803b1a90
9 changed files with 350 additions and 0 deletions

View file

@ -0,0 +1,34 @@
package control
import (
"github.com/nspcc-dev/neofs-api-go/rpc/grpc"
"github.com/nspcc-dev/neofs-api-go/rpc/message"
)
type requestWrapper struct {
message.Message
m grpc.Message
}
func (w *requestWrapper) ToGRPCMessage() grpc.Message {
return w.m
}
type healthCheckResponseWrapper struct {
m *HealthCheckResponse
}
func (w *healthCheckResponseWrapper) ToGRPCMessage() grpc.Message {
return w.m
}
func (w *healthCheckResponseWrapper) FromGRPCMessage(m grpc.Message) error {
var ok bool
w.m, ok = m.(*HealthCheckResponse)
if !ok {
return message.NewUnexpectedMessageType(m, w.m)
}
return nil
}

View file

@ -0,0 +1,34 @@
package control
import (
"github.com/nspcc-dev/neofs-api-go/rpc/client"
"github.com/nspcc-dev/neofs-api-go/rpc/common"
)
const serviceName = "ircontrol.ControlService"
const (
rpcHealthCheck = "HealthCheck"
)
// HealthCheck executes ControlService.HealthCheck RPC.
func HealthCheck(
cli *client.Client,
req *HealthCheckRequest,
opts ...client.CallOption,
) (*HealthCheckResponse, error) {
wResp := &healthCheckResponseWrapper{
m: new(HealthCheckResponse),
}
wReq := &requestWrapper{
m: req,
}
err := client.SendUnary(cli, common.CallMethodInfoUnary(serviceName, rpcHealthCheck), wReq, wResp, opts...)
if err != nil {
return nil, err
}
return wResp.m, nil
}

View file

@ -0,0 +1,148 @@
package control
import (
"github.com/nspcc-dev/neofs-api-go/util/proto"
)
// StableMarshal reads binary representation of health check request body
// in protobuf binary format.
//
// If buffer length is less than x.StableSize(), new buffer is allocated.
//
// Returns any error encountered which did not allow writing the data completely.
// Otherwise, returns the buffer in which the data is written.
//
// Structures with the same field values have the same binary format.
func (x *HealthCheckRequest_Body) StableMarshal(buf []byte) ([]byte, error) {
return buf, nil
}
// StableSize returns binary size of health check request body
// in protobuf binary format.
//
// Structures with the same field values have the same binary size.
func (x *HealthCheckRequest_Body) StableSize() int {
return 0
}
// SetBody sets health check request body.
func (x *HealthCheckRequest) SetBody(v *HealthCheckRequest_Body) {
if x != nil {
x.Body = v
}
}
// SetSignature sets signature of the health check request body.
func (x *HealthCheckRequest) SetSignature(body *Signature) {
if x != nil {
x.Signature = body
}
}
// ReadSignedData reads signed data of health check request to buf.
//
// If buffer length is less than x.SignedDataSize(), new buffer is allocated.
//
// Returns any error encountered which did not allow writing the data completely.
// Otherwise, returns the buffer in which the data is written.
//
// Structures with the same field values have the same signed data.
func (x *HealthCheckRequest) ReadSignedData(buf []byte) ([]byte, error) {
return x.GetBody().StableMarshal(buf)
}
// SignedDataSize returns binary size of the signed data
// of health check request.
//
// Structures with the same field values have the same signed data size.
func (x *HealthCheckRequest) SignedDataSize() int {
return x.GetBody().StableSize()
}
// SetHealthStatus sets health status of the IR application.
func (x *HealthCheckResponse_Body) SetHealthStatus(v HealthStatus) {
if x != nil {
x.HealthStatus = v
}
}
const (
_ = iota
healthRespBodyHealthStatusFNum
)
// StableMarshal reads binary representation of health check response body
// in protobuf binary format.
//
// If buffer length is less than x.StableSize(), new buffer is allocated.
//
// Returns any error encountered which did not allow writing the data completely.
// Otherwise, returns the buffer in which the data is written.
//
// Structures with the same field values have the same binary format.
func (x *HealthCheckResponse_Body) StableMarshal(buf []byte) ([]byte, error) {
if x == nil {
return []byte{}, nil
}
if sz := x.StableSize(); len(buf) < sz {
buf = make([]byte, sz)
}
_, err := proto.EnumMarshal(healthRespBodyHealthStatusFNum, buf, int32(x.HealthStatus))
if err != nil {
return nil, err
}
return buf, nil
}
// StableSize returns binary size of health check response body
// in protobuf binary format.
//
// Structures with the same field values have the same binary size.
func (x *HealthCheckResponse_Body) StableSize() int {
if x == nil {
return 0
}
size := 0
size += proto.EnumSize(healthRespBodyHealthStatusFNum, int32(x.HealthStatus))
return size
}
// SetBody sets health check response body.
func (x *HealthCheckResponse) SetBody(v *HealthCheckResponse_Body) {
if x != nil {
x.Body = v
}
}
// SetSignature sets signature of the health check response body.
func (x *HealthCheckResponse) SetSignature(v *Signature) {
if x != nil {
x.Signature = v
}
}
// ReadSignedData reads signed data of health check response to buf.
//
// If buffer length is less than x.SignedDataSize(), new buffer is allocated.
//
// Returns any error encountered which did not allow writing the data completely.
// Otherwise, returns the buffer in which the data is written.
//
// Structures with the same field values have the same signed data.
func (x *HealthCheckResponse) ReadSignedData(buf []byte) ([]byte, error) {
return x.GetBody().StableMarshal(buf)
}
// SignedDataSize returns binary size of the signed data
// of health check response.
//
// Structures with the same field values have the same signed data size.
func (x *HealthCheckResponse) SignedDataSize() int {
return x.GetBody().StableSize()
}

BIN
pkg/services/control/ir/service.pb.go generated Normal file

Binary file not shown.

View file

@ -0,0 +1,43 @@
syntax = "proto3";
package ircontrol;
import "pkg/services/control/ir/types.proto";
option go_package = "github.com/nspcc-dev/neofs-node/pkg/services/ir/control";
// `ControlService` provides an interface for internal work with the Inner Ring node.
service ControlService {
// Performs health check of the IR node.
rpc HealthCheck (HealthCheckRequest) returns (HealthCheckResponse);
}
// Health check request.
message HealthCheckRequest {
// Health check request body.
message Body {
}
// Body of health check request message.
Body body = 1;
// Body signature.
// Should be signed by node key or one of
// the keys configured by the node.
Signature signature = 2;
}
// Health check response.
message HealthCheckResponse {
// Health check response body
message Body {
// Health status of IR node application.
HealthStatus health_status = 1;
}
// Body of health check response message.
Body body = 1;
// Body signature.
Signature signature = 2;
}

View file

@ -0,0 +1,47 @@
package control_test
import (
"testing"
control "github.com/nspcc-dev/neofs-node/pkg/services/control/ir"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)
type protoMessage interface {
StableMarshal([]byte) ([]byte, error)
proto.Message
}
func testStableMarshal(t *testing.T, m1, m2 protoMessage, cmp func(m1, m2 protoMessage) bool) {
data, err := m1.StableMarshal(nil)
require.NoError(t, err)
require.NoError(t, proto.Unmarshal(data, m2))
require.True(t, cmp(m1, m2))
}
func TestHealthCheckResponse_Body_StableMarshal(t *testing.T) {
testStableMarshal(t,
generateHealthCheckResponseBody(),
new(control.HealthCheckResponse_Body),
func(m1, m2 protoMessage) bool {
return equalHealthCheckResponseBodies(
m1.(*control.HealthCheckResponse_Body),
m2.(*control.HealthCheckResponse_Body),
)
},
)
}
func generateHealthCheckResponseBody() *control.HealthCheckResponse_Body {
body := new(control.HealthCheckResponse_Body)
body.SetHealthStatus(control.HealthStatus_SHUTTING_DOWN)
return body
}
func equalHealthCheckResponseBodies(b1, b2 *control.HealthCheckResponse_Body) bool {
return b1.GetHealthStatus() == b2.GetHealthStatus()
}

View file

@ -0,0 +1,15 @@
package control
// SetKey sets public key used for signing.
func (x *Signature) SetKey(v []byte) {
if x != nil {
x.Key = v
}
}
// SetSign sets binary signature.
func (x *Signature) SetSign(v []byte) {
if x != nil {
x.Sign = v
}
}

BIN
pkg/services/control/ir/types.pb.go generated Normal file

Binary file not shown.

View file

@ -0,0 +1,29 @@
syntax = "proto3";
package ircontrol;
option go_package = "github.com/nspcc-dev/neofs-node/pkg/services/ir/control";
// Signature of some message.
message Signature {
// Public key used for signing.
bytes key = 1 [json_name = "key"];
// Binary signature.
bytes sign = 2 [json_name = "signature"];
}
// Health status of the IR application.
enum HealthStatus {
// Undefined status, default value.
HEALTH_STATUS_UNDEFINED = 0;
// IR application is starting.
STARTING = 1;
// IR application is started and serves all services.
READY = 2;
// IR application is shutting down.
SHUTTING_DOWN = 3;
}