Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplication in pagination #383

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions pkg/backend/aio.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (s *Server) UpdateAioController(_ context.Context, in *pb.UpdateAioControll
// ListAioControllers lists Aio controllers
func (s *Server) ListAioControllers(_ context.Context, in *pb.ListAioControllersRequest) (*pb.ListAioControllersResponse, error) {
log.Printf("ListAioControllers: Received from client: %v", in)
size, offset, perr := server.ExtractPagination(in.PageSize, in.PageToken, s.Pagination)
pageToken, perr := s.Pagination.PageToken(in.PageSize, in.PageToken)
if perr != nil {
log.Printf("error: %v", perr)
return nil, perr
Expand All @@ -164,20 +164,14 @@ func (s *Server) ListAioControllers(_ context.Context, in *pb.ListAioControllers
return nil, err
}
log.Printf("Received from SPDK: %v", result)
token := ""
log.Printf("Limiting result len(%d) to [%d:%d]", len(result), offset, size)
result, hasMoreElements := server.LimitPagination(result, offset, size)
if hasMoreElements {
token = uuid.New().String()
s.Pagination[token] = offset + size
}
Blobarray := make([]*pb.AioController, len(result))
for i := range result {
r := &result[i]
Blobarray[i] = &pb.AioController{Handle: &pc.ObjectKey{Value: r.Name}, BlockSize: r.BlockSize, BlocksCount: r.NumBlocks}
}
sortAioControllers(Blobarray)
return &pb.ListAioControllersResponse{AioControllers: Blobarray, NextPageToken: token}, nil
page := server.LimitToPage(pageToken, Blobarray)
return &pb.ListAioControllersResponse{AioControllers: page.List, NextPageToken: page.NextToken}, nil
}

// GetAioController gets an Aio controller
Expand Down
5 changes: 3 additions & 2 deletions pkg/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package backend
import (
"github.com/opiproject/gospdk/spdk"
pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
"github.com/opiproject/opi-spdk-bridge/pkg/server"
)

// TODO: can we combine all of volume types into a single list?
Expand All @@ -28,7 +29,7 @@ type Server struct {

rpc spdk.JSONRPC
Volumes VolumeParameters
Pagination map[string]int
Pagination server.Pagination
}

// NewServer creates initialized instance of BackEnd server communicating
Expand All @@ -41,6 +42,6 @@ func NewServer(jsonRPC spdk.JSONRPC) *Server {
NullVolumes: make(map[string]*pb.NullDebug),
NvmeVolumes: make(map[string]*pb.NVMfRemoteController),
},
Pagination: make(map[string]int),
Pagination: server.NewPagination(),
}
}
12 changes: 3 additions & 9 deletions pkg/backend/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (s *Server) UpdateNullDebug(_ context.Context, in *pb.UpdateNullDebugReques
// ListNullDebugs lists Null Debug instances
func (s *Server) ListNullDebugs(_ context.Context, in *pb.ListNullDebugsRequest) (*pb.ListNullDebugsResponse, error) {
log.Printf("ListNullDebugs: Received from client: %v", in)
size, offset, perr := server.ExtractPagination(in.PageSize, in.PageToken, s.Pagination)
pageToken, perr := s.Pagination.PageToken(in.PageSize, in.PageToken)
if perr != nil {
log.Printf("error: %v", perr)
return nil, perr
Expand All @@ -164,20 +164,14 @@ func (s *Server) ListNullDebugs(_ context.Context, in *pb.ListNullDebugsRequest)
return nil, err
}
log.Printf("Received from SPDK: %v", result)
token := ""
log.Printf("Limiting result len(%d) to [%d:%d]", len(result), offset, size)
result, hasMoreElements := server.LimitPagination(result, offset, size)
if hasMoreElements {
token = uuid.New().String()
s.Pagination[token] = offset + size
}
Blobarray := make([]*pb.NullDebug, len(result))
for i := range result {
r := &result[i]
Blobarray[i] = &pb.NullDebug{Handle: &pc.ObjectKey{Value: r.Name}, Uuid: &pc.Uuid{Value: r.UUID}, BlockSize: r.BlockSize, BlocksCount: r.NumBlocks}
}
sortNullDebugs(Blobarray)
return &pb.ListNullDebugsResponse{NullDebugs: Blobarray, NextPageToken: token}, nil
page := server.LimitToPage(pageToken, Blobarray)
return &pb.ListNullDebugsResponse{NullDebugs: page.List, NextPageToken: page.NextToken}, nil
}

// GetNullDebug gets a a Null Debug instance
Expand Down
12 changes: 3 additions & 9 deletions pkg/backend/nvme.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (s *Server) NVMfRemoteControllerReset(_ context.Context, in *pb.NVMfRemoteC
// ListNVMfRemoteControllers lists an NVMf remote controllers
func (s *Server) ListNVMfRemoteControllers(_ context.Context, in *pb.ListNVMfRemoteControllersRequest) (*pb.ListNVMfRemoteControllersResponse, error) {
log.Printf("ListNVMfRemoteControllers: Received from client: %v", in)
size, offset, perr := server.ExtractPagination(in.PageSize, in.PageToken, s.Pagination)
pageToken, perr := s.Pagination.PageToken(in.PageSize, in.PageToken)
if perr != nil {
log.Printf("error: %v", perr)
return nil, perr
Expand All @@ -126,13 +126,6 @@ func (s *Server) ListNVMfRemoteControllers(_ context.Context, in *pb.ListNVMfRem
return nil, err
}
log.Printf("Received from SPDK: %v", result)
token := ""
log.Printf("Limiting result len(%d) to [%d:%d]", len(result), offset, size)
result, hasMoreElements := server.LimitPagination(result, offset, size)
if hasMoreElements {
token = uuid.New().String()
s.Pagination[token] = offset + size
}
Blobarray := make([]*pb.NVMfRemoteController, len(result))
for i := range result {
r := &result[i]
Expand All @@ -148,7 +141,8 @@ func (s *Server) ListNVMfRemoteControllers(_ context.Context, in *pb.ListNVMfRem
}
}
sortNVMfRemoteControllers(Blobarray)
return &pb.ListNVMfRemoteControllersResponse{NvMfRemoteControllers: Blobarray, NextPageToken: token}, nil
page := server.LimitToPage(pageToken, Blobarray)
return &pb.ListNVMfRemoteControllersResponse{NvMfRemoteControllers: page.List, NextPageToken: page.NextToken}, nil
}

// GetNVMfRemoteController gets an NVMf remote controller
Expand Down
13 changes: 3 additions & 10 deletions pkg/frontend/blk.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (s *Server) UpdateVirtioBlk(_ context.Context, in *pb.UpdateVirtioBlkReques
// ListVirtioBlks lists Virtio block devices
func (s *Server) ListVirtioBlks(_ context.Context, in *pb.ListVirtioBlksRequest) (*pb.ListVirtioBlksResponse, error) {
log.Printf("ListVirtioBlks: Received from client: %v", in)
size, offset, perr := server.ExtractPagination(in.PageSize, in.PageToken, s.Pagination)
pageToken, perr := s.Pagination.PageToken(in.PageSize, in.PageToken)
if perr != nil {
log.Printf("error: %v", perr)
return nil, perr
Expand All @@ -122,13 +122,6 @@ func (s *Server) ListVirtioBlks(_ context.Context, in *pb.ListVirtioBlksRequest)
return nil, err
}
log.Printf("Received from SPDK: %v", result)
token := ""
log.Printf("Limiting result len(%d) to [%d:%d]", len(result), offset, size)
result, hasMoreElements := server.LimitPagination(result, offset, size)
if hasMoreElements {
token = uuid.New().String()
s.Pagination[token] = offset + size
}
Blobarray := make([]*pb.VirtioBlk, len(result))
for i := range result {
r := &result[i]
Expand All @@ -138,8 +131,8 @@ func (s *Server) ListVirtioBlks(_ context.Context, in *pb.ListVirtioBlksRequest)
VolumeId: &pc.ObjectKey{Value: "TBD"}}
}
sortVirtioBlks(Blobarray)

return &pb.ListVirtioBlksResponse{VirtioBlks: Blobarray, NextPageToken: token}, nil
page := server.LimitToPage(pageToken, Blobarray)
return &pb.ListVirtioBlksResponse{VirtioBlks: page.List, NextPageToken: page.NextToken}, nil
}

// GetVirtioBlk gets a Virtio block device
Expand Down
8 changes: 6 additions & 2 deletions pkg/frontend/blk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,16 @@ func TestFrontEnd_ListVirtioBlks(t *testing.T) {
"subsystem-test",
[]*pb.VirtioBlk{
{
Id: &pc.ObjectKey{Value: "virtio-blk-42"},
Id: &pc.ObjectKey{Value: "VblkEmu0pf2"},
PcieId: &pb.PciEndpoint{PhysicalFunction: int32(1)},
VolumeId: &pc.ObjectKey{Value: "TBD"},
},
},
[]string{`{"jsonrpc":"2.0","id":%d,"result":[{"ctrlr":"VblkEmu0pf0","emulation_manager":"mlx5_0","type":"virtio_blk","pci_index":0,"pci_bdf":"ca:00.4"},{"ctrlr":"virtio-blk-42","emulation_manager":"mlx5_0","type":"virtio_blk","pci_index":0,"pci_bdf":"ca:00.4"},{"ctrlr":"VblkEmu0pf2","emulation_manager":"mlx5_0","type":"virtio_blk","pci_index":0,"pci_bdf":"ca:00.4"}],"error":{"code":0,"message":""}}`},
[]string{`{"jsonrpc":"2.0","id":%d,"result":[
{"ctrlr":"VblkEmu0pf0","emulation_manager":"mlx5_0","type":"virtio_blk","pci_index":0,"pci_bdf":"ca:00.4"},
{"ctrlr":"virtio-blk-42","emulation_manager":"mlx5_0","type":"virtio_blk","pci_index":0,"pci_bdf":"ca:00.4"},
{"ctrlr":"VblkEmu0pf2","emulation_manager":"mlx5_0","type":"virtio_blk","pci_index":0,"pci_bdf":"ca:00.4"}
],"error":{"code":0,"message":""}}`},
codes.OK,
"",
true,
Expand Down
5 changes: 3 additions & 2 deletions pkg/frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

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

// SubsystemListener interface is used to provide SPDK call params to create/delete
Expand Down Expand Up @@ -42,7 +43,7 @@ type Server struct {
rpc spdk.JSONRPC
Nvme NvmeParameters
Virt VirtioParameters
Pagination map[string]int
Pagination server.Pagination
}

// NewServer creates initialized instance of FrontEnd server communicating
Expand All @@ -61,7 +62,7 @@ func NewServer(jsonRPC spdk.JSONRPC) *Server {
ScsiCtrls: make(map[string]*pb.VirtioScsiController),
ScsiLuns: make(map[string]*pb.VirtioScsiLun),
},
Pagination: make(map[string]int),
Pagination: server.NewPagination(),
}
}

Expand Down
35 changes: 13 additions & 22 deletions pkg/frontend/nvme.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (s *Server) UpdateNVMeSubsystem(_ context.Context, in *pb.UpdateNVMeSubsyst
// ListNVMeSubsystems lists NVMe Subsystems
func (s *Server) ListNVMeSubsystems(_ context.Context, in *pb.ListNVMeSubsystemsRequest) (*pb.ListNVMeSubsystemsResponse, error) {
log.Printf("ListNVMeSubsystems: Received from client: %v", in)
size, offset, perr := server.ExtractPagination(in.PageSize, in.PageToken, s.Pagination)
pageToken, perr := s.Pagination.PageToken(in.PageSize, in.PageToken)
if perr != nil {
log.Printf("error: %v", perr)
return nil, perr
Expand All @@ -200,20 +200,14 @@ func (s *Server) ListNVMeSubsystems(_ context.Context, in *pb.ListNVMeSubsystems
return nil, err
}
log.Printf("Received from SPDK: %v", result)
token := ""
log.Printf("Limiting result len(%d) to [%d:%d]", len(result), offset, size)
result, hasMoreElements := server.LimitPagination(result, offset, size)
if hasMoreElements {
token = uuid.New().String()
s.Pagination[token] = offset + size
}
Blobarray := make([]*pb.NVMeSubsystem, len(result))
for i := range result {
r := &result[i]
Blobarray[i] = &pb.NVMeSubsystem{Spec: &pb.NVMeSubsystemSpec{Nqn: r.Nqn, SerialNumber: r.SerialNumber, ModelNumber: r.ModelNumber}}
}
sortNVMeSubsystems(Blobarray)
return &pb.ListNVMeSubsystemsResponse{NvMeSubsystems: Blobarray, NextPageToken: token}, nil
page := server.LimitToPage(pageToken, Blobarray)
return &pb.ListNVMeSubsystemsResponse{NvMeSubsystems: page.List, NextPageToken: page.NextToken}, nil
}

// GetNVMeSubsystem gets NVMe Subsystems
Expand Down Expand Up @@ -360,14 +354,18 @@ func (s *Server) UpdateNVMeController(_ context.Context, in *pb.UpdateNVMeContro
// ListNVMeControllers lists NVMe controllers
func (s *Server) ListNVMeControllers(_ context.Context, in *pb.ListNVMeControllersRequest) (*pb.ListNVMeControllersResponse, error) {
log.Printf("Received from client: %v", in.Parent)
pageToken, perr := s.Pagination.PageToken(in.PageSize, in.PageToken)
if perr != nil {
log.Printf("error: %v", perr)
return nil, perr
}
Blobarray := []*pb.NVMeController{}
for _, controller := range s.Nvme.Controllers {
Blobarray = append(Blobarray, controller)
}
sortNVMeControllers(Blobarray)
token := uuid.New().String()
s.Pagination[token] = int(in.PageSize)
return &pb.ListNVMeControllersResponse{NvMeControllers: Blobarray, NextPageToken: token}, nil
page := server.LimitToPage(pageToken, Blobarray)
return &pb.ListNVMeControllersResponse{NvMeControllers: page.List, NextPageToken: page.NextToken}, nil
}

// GetNVMeController gets an NVMe controller
Expand Down Expand Up @@ -501,7 +499,7 @@ func (s *Server) UpdateNVMeNamespace(_ context.Context, in *pb.UpdateNVMeNamespa
// ListNVMeNamespaces lists NVMe namespaces
func (s *Server) ListNVMeNamespaces(_ context.Context, in *pb.ListNVMeNamespacesRequest) (*pb.ListNVMeNamespacesResponse, error) {
log.Printf("ListNVMeNamespaces: Received from client: %v", in)
size, offset, perr := server.ExtractPagination(in.PageSize, in.PageToken, s.Pagination)
pageToken, perr := s.Pagination.PageToken(in.PageSize, in.PageToken)
if perr != nil {
log.Printf("error: %v", perr)
return nil, perr
Expand All @@ -523,18 +521,10 @@ func (s *Server) ListNVMeNamespaces(_ context.Context, in *pb.ListNVMeNamespaces
return nil, err
}
log.Printf("Received from SPDK: %v", result)
token := ""
Blobarray := []*pb.NVMeNamespace{}
for i := range result {
rr := &result[i]
if rr.Nqn == nqn || nqn == "" {
log.Printf("Limiting result len(%d) to [%d:%d]", len(result), offset, size)
hasMoreElements := false
rr.Namespaces, hasMoreElements = server.LimitPagination(rr.Namespaces, offset, size)
if hasMoreElements {
token = uuid.New().String()
s.Pagination[token] = offset + size
}
for j := range rr.Namespaces {
r := &rr.Namespaces[j]
Blobarray = append(Blobarray, &pb.NVMeNamespace{Spec: &pb.NVMeNamespaceSpec{HostNsid: int32(r.Nsid)}})
Expand All @@ -543,7 +533,8 @@ func (s *Server) ListNVMeNamespaces(_ context.Context, in *pb.ListNVMeNamespaces
}
if len(Blobarray) > 0 {
sortNVMeNamespaces(Blobarray)
return &pb.ListNVMeNamespacesResponse{NvMeNamespaces: Blobarray, NextPageToken: token}, nil
page := server.LimitToPage(pageToken, Blobarray)
return &pb.ListNVMeNamespacesResponse{NvMeNamespaces: page.List, NextPageToken: page.NextToken}, nil
}

msg := fmt.Sprintf("Could not find any namespaces for NQN: %s", nqn)
Expand Down
31 changes: 13 additions & 18 deletions pkg/frontend/scsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func sortScsiControllers(controllers []*pb.VirtioScsiController) {
})
}

func sortScsiLuns(controllers []*pb.VirtioScsiLun) {
sort.Slice(controllers, func(i int, j int) bool {
return controllers[i].Id.Value < controllers[j].Id.Value
})
}

// CreateVirtioScsiController creates a Virtio SCSI controller
func (s *Server) CreateVirtioScsiController(_ context.Context, in *pb.CreateVirtioScsiControllerRequest) (*pb.VirtioScsiController, error) {
log.Printf("CreateVirtioScsiController: Received from client: %v", in)
Expand Down Expand Up @@ -108,7 +114,7 @@ func (s *Server) UpdateVirtioScsiController(_ context.Context, in *pb.UpdateVirt
// ListVirtioScsiControllers lists Virtio SCSI controllers
func (s *Server) ListVirtioScsiControllers(_ context.Context, in *pb.ListVirtioScsiControllersRequest) (*pb.ListVirtioScsiControllersResponse, error) {
log.Printf("ListVirtioScsiControllers: Received from client: %v", in)
size, offset, perr := server.ExtractPagination(in.PageSize, in.PageToken, s.Pagination)
pageToken, perr := s.Pagination.PageToken(in.PageSize, in.PageToken)
if perr != nil {
log.Printf("error: %v", perr)
return nil, perr
Expand All @@ -120,20 +126,14 @@ func (s *Server) ListVirtioScsiControllers(_ context.Context, in *pb.ListVirtioS
return nil, err
}
log.Printf("Received from SPDK: %v", result)
token := ""
log.Printf("Limiting result len(%d) to [%d:%d]", len(result), offset, size)
result, hasMoreElements := server.LimitPagination(result, offset, size)
if hasMoreElements {
token = uuid.New().String()
s.Pagination[token] = offset + size
}
Blobarray := make([]*pb.VirtioScsiController, len(result))
for i := range result {
r := &result[i]
Blobarray[i] = &pb.VirtioScsiController{Id: &pc.ObjectKey{Value: r.Ctrlr}}
}
sortScsiControllers(Blobarray)
return &pb.ListVirtioScsiControllersResponse{VirtioScsiControllers: Blobarray, NextPageToken: token}, nil
page := server.LimitToPage(pageToken, Blobarray)
return &pb.ListVirtioScsiControllersResponse{VirtioScsiControllers: page.List, NextPageToken: page.NextToken}, nil
}

// GetVirtioScsiController gets a Virtio SCSI controller
Expand Down Expand Up @@ -249,7 +249,7 @@ func (s *Server) UpdateVirtioScsiLun(_ context.Context, in *pb.UpdateVirtioScsiL
// ListVirtioScsiLuns lists Virtio SCSI LUNs
func (s *Server) ListVirtioScsiLuns(_ context.Context, in *pb.ListVirtioScsiLunsRequest) (*pb.ListVirtioScsiLunsResponse, error) {
log.Printf("ListVirtioScsiLuns: Received from client: %v", in)
size, offset, perr := server.ExtractPagination(in.PageSize, in.PageToken, s.Pagination)
pageToken, perr := s.Pagination.PageToken(in.PageSize, in.PageToken)
if perr != nil {
log.Printf("error: %v", perr)
return nil, perr
Expand All @@ -261,19 +261,14 @@ func (s *Server) ListVirtioScsiLuns(_ context.Context, in *pb.ListVirtioScsiLuns
return nil, err
}
log.Printf("Received from SPDK: %v", result)
token := ""
log.Printf("Limiting result len(%d) to [%d:%d]", len(result), offset, size)
result, hasMoreElements := server.LimitPagination(result, offset, size)
if hasMoreElements {
token = uuid.New().String()
s.Pagination[token] = offset + size
}
Blobarray := make([]*pb.VirtioScsiLun, len(result))
for i := range result {
r := &result[i]
Blobarray[i] = &pb.VirtioScsiLun{VolumeId: &pc.ObjectKey{Value: r.Ctrlr}}
}
return &pb.ListVirtioScsiLunsResponse{VirtioScsiLuns: Blobarray, NextPageToken: token}, nil
sortScsiLuns(Blobarray)
page := server.LimitToPage(pageToken, Blobarray)
return &pb.ListVirtioScsiLunsResponse{VirtioScsiLuns: page.List, NextPageToken: page.NextToken}, nil
}

// GetVirtioScsiLun gets a Virtio SCSI LUN
Expand Down
Loading