mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
161 lines
5.5 KiB
Go
161 lines
5.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"sync"
|
|
"testing"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore"
|
|
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/filerpb"
|
|
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/masterpb"
|
|
)
|
|
|
|
type groupFiler struct {
|
|
filerpb.UnimplementedSeaweedFilerServer
|
|
master string
|
|
store *chunkstore.MemFiler
|
|
}
|
|
|
|
func (f *groupFiler) GetFilerConfiguration(context.Context, *filerpb.GetFilerConfigurationRequest) (*filerpb.GetFilerConfigurationResponse, error) {
|
|
return &filerpb.GetFilerConfigurationResponse{Masters: []string{f.master}}, nil
|
|
}
|
|
|
|
// Exercise the catalog startup probe through the real SWFS gRPC adapter,
|
|
// retaining exclusive-create and conditional-update semantics in the fake.
|
|
func (f *groupFiler) CreateEntry(ctx context.Context, r *filerpb.CreateEntryRequest) (*filerpb.CreateEntryResponse, error) {
|
|
err := f.store.CreateEntry(ctx, r.Directory, r.Entry, r.OExcl)
|
|
if errors.Is(err, chunkstore.ErrExists) {
|
|
return &filerpb.CreateEntryResponse{ErrorCode: filerpb.FilerError_ENTRY_ALREADY_EXISTS}, nil
|
|
}
|
|
return &filerpb.CreateEntryResponse{}, groupFilerError(err)
|
|
}
|
|
func (f *groupFiler) UpdateEntry(ctx context.Context, r *filerpb.UpdateEntryRequest) (*filerpb.UpdateEntryResponse, error) {
|
|
return &filerpb.UpdateEntryResponse{}, groupFilerError(f.store.UpdateEntry(ctx, r.Directory, r.Entry, r.ExpectedExtended))
|
|
}
|
|
func (f *groupFiler) LookupDirectoryEntry(ctx context.Context, r *filerpb.LookupDirectoryEntryRequest) (*filerpb.LookupDirectoryEntryResponse, error) {
|
|
e, err := f.store.LookupEntry(ctx, r.Directory, r.Name)
|
|
return &filerpb.LookupDirectoryEntryResponse{Entry: e}, groupFilerError(err)
|
|
}
|
|
func (f *groupFiler) ListEntries(r *filerpb.ListEntriesRequest, stream grpc.ServerStreamingServer[filerpb.ListEntriesResponse]) error {
|
|
entries, err := f.store.ListEntries(stream.Context(), r.Directory, r.Prefix, r.StartFromFileName, r.Limit)
|
|
if err != nil {
|
|
return groupFilerError(err)
|
|
}
|
|
for _, e := range entries {
|
|
if err := stream.Send(&filerpb.ListEntriesResponse{Entry: e}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (f *groupFiler) DeleteEntry(ctx context.Context, r *filerpb.DeleteEntryRequest) (*filerpb.DeleteEntryResponse, error) {
|
|
if r.IsRecursive {
|
|
return nil, status.Error(codes.InvalidArgument, "recursive delete not supported by this fixture")
|
|
}
|
|
err := f.store.DeleteEntry(ctx, r.Directory, r.Name, r.ExpectedExtended)
|
|
// MemFiler does not materialize parent directories on child creation.
|
|
if errors.Is(err, chunkstore.ErrNotFound) {
|
|
err = nil
|
|
}
|
|
return &filerpb.DeleteEntryResponse{}, groupFilerError(err)
|
|
}
|
|
func groupFilerError(err error) error {
|
|
switch {
|
|
case err == nil:
|
|
return nil
|
|
case errors.Is(err, chunkstore.ErrPrecondition):
|
|
return status.Error(codes.FailedPrecondition, err.Error())
|
|
case errors.Is(err, chunkstore.ErrNotFound):
|
|
return status.Error(codes.NotFound, err.Error())
|
|
default:
|
|
return status.Error(codes.Internal, err.Error())
|
|
}
|
|
}
|
|
|
|
type groupMaster struct {
|
|
masterpb.UnimplementedSeaweedServer
|
|
filers []string
|
|
mu sync.Mutex
|
|
leader string
|
|
}
|
|
|
|
func (m *groupMaster) GetMasterConfiguration(context.Context, *masterpb.GetMasterConfigurationRequest) (*masterpb.GetMasterConfigurationResponse, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return &masterpb.GetMasterConfigurationResponse{Leader: m.leader}, nil
|
|
}
|
|
|
|
func (m *groupMaster) ListClusterNodes(context.Context, *masterpb.ListClusterNodesRequest) (*masterpb.ListClusterNodesResponse, error) {
|
|
r := &masterpb.ListClusterNodesResponse{}
|
|
for _, a := range m.filers {
|
|
r.ClusterNodes = append(r.ClusterNodes, &masterpb.ListClusterNodesResponse_ClusterNode{Address: a})
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
func serve(t *testing.T, register func(*grpc.Server)) string {
|
|
t.Helper()
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv := grpc.NewServer()
|
|
register(srv)
|
|
go func() { _ = srv.Serve(lis) }()
|
|
t.Cleanup(srv.Stop)
|
|
return lis.Addr().String()
|
|
}
|
|
|
|
// blockvolume's chunk wiring runs the single-filer check at startup: the
|
|
// configured filer listed as the one filer passes; a group with a second
|
|
// filer is refused.
|
|
func TestChunkWiringRefusesAMultiFilerGroup(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
other []string
|
|
ok bool
|
|
}{
|
|
{"one filer", nil, true},
|
|
{"two filers", []string{"10.0.0.2:8888"}, false},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
filer := lis.Addr().String()
|
|
_, fport, _ := net.SplitHostPort(filer)
|
|
self := "127.0.0.1:8888." + fport // the filer as a master lists it
|
|
master := &groupMaster{filers: append([]string{self}, c.other...)}
|
|
m := serve(t, func(s *grpc.Server) {
|
|
masterpb.RegisterSeaweedServer(s, master)
|
|
})
|
|
_, mport, _ := net.SplitHostPort(m)
|
|
master.mu.Lock()
|
|
master.leader = "127.0.0.1:9333." + mport
|
|
master.mu.Unlock()
|
|
srv := grpc.NewServer()
|
|
filerpb.RegisterSeaweedFilerServer(srv, &groupFiler{master: "127.0.0.1:9333." + mport, store: chunkstore.NewMemFiler()})
|
|
go func() { _ = srv.Serve(lis) }()
|
|
t.Cleanup(srv.Stop)
|
|
w, err := newChunkWiring(chunkFlags{filer: filer, path: defaultChunkPath}, nil)
|
|
if c.ok {
|
|
if err != nil {
|
|
t.Fatalf("one filer refused: %v", err)
|
|
}
|
|
_ = w.close()
|
|
return
|
|
}
|
|
if !errors.Is(err, chunkstore.ErrNotSingleFiler) {
|
|
t.Fatalf("two filers: %v; want chunkstore.ErrNotSingleFiler", err)
|
|
}
|
|
})
|
|
}
|
|
}
|