Skip to content

Commit

Permalink
refactor(middleend): transition to spdk client
Browse files Browse the repository at this point in the history
Signed-off-by: Artsiom Koltun <[email protected]>
  • Loading branch information
artek-koltun committed Jan 30, 2024
1 parent cff9e85 commit b1666e0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func runGrpcServer(grpcPort int, useKvm bool, store gokv.Store, spdkAddress, qmp
}()

backendServer := backend.NewServer(spdkClient, store)
middleendServer := middleend.NewServer(jsonRPC, store)
middleendServer := middleend.NewServer(spdkClient, store)

if useKvm {
log.Println("Creating KVM server.")
Expand Down
2 changes: 1 addition & 1 deletion pkg/middleend/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"sort"

"github.com/google/uuid"
"github.com/opiproject/gospdk/spdk"
pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
"github.com/opiproject/opi-spdk-bridge/pkg/spdk"
"github.com/opiproject/opi-spdk-bridge/pkg/utils"

"go.einride.tech/aip/fieldbehavior"
Expand Down
17 changes: 9 additions & 8 deletions pkg/middleend/middleend.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ package middleend
import (
"log"

"github.com/opiproject/opi-spdk-bridge/pkg/spdk"
"github.com/philippgille/gokv"
"github.com/spdk/spdk/go/rpc/client"

"github.com/opiproject/gospdk/spdk"
pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
)

Expand All @@ -25,7 +26,7 @@ type Server struct {
pb.UnimplementedMiddleendEncryptionServiceServer
pb.UnimplementedMiddleendQosVolumeServiceServer

rpc spdk.JSONRPC
rpc *spdk.SpdkClientAdapter
store gokv.Store
volumes VolumeParameters
tweakMode string
Expand All @@ -34,21 +35,21 @@ type Server struct {

// NewServer creates initialized instance of MiddleEnd server communicating
// with provided jsonRPC
func NewServer(jsonRPC spdk.JSONRPC, store gokv.Store) *Server {
return NewCustomizedServer(jsonRPC, store, spdk.TweakModeSimpleLba)
func NewServer(spdkClient client.IClient, store gokv.Store) *Server {
return NewCustomizedServer(spdkClient, store, spdk.TweakModeSimpleLba)
}

// NewCustomizedServer creates initialized instance of MiddleEnd server communicating
// with provided jsonRPC, store and non standard tweak mode
func NewCustomizedServer(jsonRPC spdk.JSONRPC, store gokv.Store, tweakMode string) *Server {
if jsonRPC == nil {
log.Panic("nil for JSONRPC is not allowed")
func NewCustomizedServer(spdkClient client.IClient, store gokv.Store, tweakMode string) *Server {
if spdkClient == nil {
log.Panic("nil for spdkClient is not allowed")
}
if store == nil {
log.Panic("nil for Store is not allowed")
}
return &Server{
rpc: jsonRPC,
rpc: spdk.NewSpdkClientAdapter(spdkClient),
store: store,
volumes: VolumeParameters{
qosVolumes: make(map[string]*pb.QosVolume),
Expand Down
8 changes: 4 additions & 4 deletions pkg/middleend/middleend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"google.golang.org/grpc/test/bufconn"

"github.com/philippgille/gokv/gomap"
"github.com/spdk/spdk/go/rpc/client"

"github.com/opiproject/gospdk/spdk"
pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
"github.com/opiproject/opi-spdk-bridge/pkg/utils"
)
Expand All @@ -42,7 +42,7 @@ type testEnv struct {
testSocket string
ctx context.Context
conn *grpc.ClientConn
jsonRPC spdk.JSONRPC
spdkClient client.IClient
}

func (e *testEnv) Close() {
Expand All @@ -56,11 +56,11 @@ func (e *testEnv) Close() {
func createTestEnvironment(spdkResponses []string) *testEnv {
env := &testEnv{}
env.testSocket = utils.GenerateSocketName("middleend")
env.ln, env.jsonRPC = utils.CreateTestSpdkServer(env.testSocket, spdkResponses)
env.ln, env.spdkClient = utils.CreateTestSpdkServer(env.testSocket, spdkResponses)
options := gomap.DefaultOptions
options.Codec = utils.ProtoCodec{}
store := gomap.NewStore(options)
env.opiSpdkServer = NewServer(env.jsonRPC, store)
env.opiSpdkServer = NewServer(env.spdkClient, store)

ctx := context.Background()
conn, err := grpc.DialContext(ctx,
Expand Down
2 changes: 1 addition & 1 deletion pkg/middleend/qos.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"sort"

"github.com/google/uuid"
"github.com/opiproject/gospdk/spdk"
pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
"github.com/opiproject/opi-spdk-bridge/pkg/spdk"
"github.com/opiproject/opi-spdk-bridge/pkg/utils"

"go.einride.tech/aip/fieldbehavior"
Expand Down
41 changes: 16 additions & 25 deletions pkg/middleend/qos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
package middleend

import (
"context"
"fmt"
"net"
"testing"

"github.com/opiproject/gospdk/spdk"
pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
"github.com/opiproject/opi-spdk-bridge/pkg/spdk"
"github.com/opiproject/opi-spdk-bridge/pkg/utils"
"github.com/spdk/spdk/go/rpc/client"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
Expand All @@ -30,28 +29,19 @@ var (
}
)

type stubJSONRRPC struct {
type stubClient struct {
params []any
}

// build time check that struct implements interface
var _ spdk.JSONRPC = (*stubJSONRRPC)(nil)
var _ client.IClient = (*stubClient)(nil)

func (s *stubJSONRRPC) GetID() uint64 {
return 0
}

func (s *stubJSONRRPC) StartUnixListener() net.Listener {
return nil
}

func (s *stubJSONRRPC) GetVersion(_ context.Context) string {
return ""
}

func (s *stubJSONRRPC) Call(_ context.Context, _ string, param interface{}, _ interface{}) error {
s.params = append(s.params, param)
return nil
func (s *stubClient) Call(_ string, params any) (*client.Response, error) {
s.params = append(s.params, params)
return &client.Response{
ID: 0,
Result: true,
}, nil
}

func TestMiddleEnd_CreateQosVolume(t *testing.T) {
Expand Down Expand Up @@ -327,8 +317,8 @@ func TestMiddleEnd_CreateQosVolume(t *testing.T) {
t.Run("valid values are sent to SPDK", func(t *testing.T) {
testEnv := createTestEnvironment([]string{})
defer testEnv.Close()
stubRPC := &stubJSONRRPC{}
testEnv.opiSpdkServer.rpc = stubRPC
stubClient := &stubClient{}
testEnv.opiSpdkServer.rpc = spdk.NewSpdkClientAdapter(stubClient)

_, _ = testEnv.client.CreateQosVolume(testEnv.ctx, &pb.CreateQosVolumeRequest{
QosVolumeId: testQosVolumeID,
Expand All @@ -344,10 +334,11 @@ func TestMiddleEnd_CreateQosVolume(t *testing.T) {
},
},
})
if len(stubRPC.params) != 1 {
t.Fatalf("Expect only one call to SPDK, received %v", stubRPC.params)
if len(stubClient.params) != 1 {
t.Fatalf("Expect only one call to SPDK, received %v", stubClient.params)
}
qosParams := stubRPC.params[0].(*spdk.BdevQoSParams)

qosParams := stubClient.params[0].(*spdk.BdevQoSParams)
expectedParams := spdk.BdevQoSParams{
Name: "volume-42",
RwIosPerSec: 1000,
Expand Down
17 changes: 17 additions & 0 deletions pkg/spdk/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2024 Intel Corporation

// Package spdk implements the spdk json-rpc protocol
package spdk

import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var (
// ErrFailedSpdkCall indicates that the bridge failed to execute SPDK call
ErrFailedSpdkCall = status.Error(codes.Unknown, "Failed to execute SPDK call")
// ErrUnexpectedSpdkCallResult indicates that the bridge got an error from SPDK
ErrUnexpectedSpdkCallResult = status.Error(codes.FailedPrecondition, "Unexpected SPDK call result.")
)

0 comments on commit b1666e0

Please sign in to comment.