mirror of
https://github.com/hanzoai/s3.git
synced 2026-10-30 14:59:09 +00:00
filer, master, iam, plugin and worker no longer use google.golang.org/grpc on
the client path.
- gRPC-free client interfaces: the generated *_grpc.pb.go client interfaces drop
"opts ...grpc.CallOption" (no caller ever passed one) and their streaming
methods return the shared seam types in s3/pb/rpc (ServerStream[T] /
BidiStream[Req,Resp]) instead of grpc.ServerStreamingClient/BidiStreamingClient.
The gRPC server scaffolding (Unimplemented*Server, Register*Server, ServiceDesc,
handler trampolines) is deleted; servers serve over the ZAP wire Dispatch.
- The ZAP adapters (filerzap/masterzap/...) implement the gRPC-free interfaces;
grpc.DialOption becomes pb.DialOption (the dial is the ZAP transport).
- Unary calls honor context deadlines: the wire client threads ctx to
transport.Conn.CallContext (v1.8.1), so a call to an unresponsive peer aborts
on the caller's deadline instead of blocking forever.
- Error semantics: the filer tags each failure with its code name ("NotFound: …",
"Internal: …") in the message, which crosses the ZAP wire and is classified by
mount/error_classifier.go — replacing grpc status/codes.
- master ZAP listener port: grpcPort+10000 could exceed 65535; made overflow-safe.
- test doubles updated to the gRPC-free interfaces (0 tests skipped).
- dead cmd/s3-db and cmd/s3-sql (postgres-wire / SQL-engine entry points whose
backends were already ripped) deleted; finished the backend-rip import cleanup.
go build ./... green; go test ./s3/... green (heavy stream benches run under
their existing -short gates).
Still on gRPC (next wave): volume_server + mq (reverted — servers/adapters not
ready), the remaining ~23 plumbing files, and the go.mod dependency itself.
168 lines
4.2 KiB
Go
168 lines
4.2 KiB
Go
package pluginworkers
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hanzoai/s3/s3/admin/plugin"
|
|
pluginworker "github.com/hanzoai/s3/s3/plugin/worker"
|
|
pluginwire "github.com/hanzoai/s3/s3/wire/plugin"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/zap-proto/go/transport"
|
|
)
|
|
|
|
// HarnessConfig configures the shared plugin worker test harness.
|
|
type HarnessConfig struct {
|
|
PluginOptions plugin.Options
|
|
WorkerOptions pluginworker.WorkerOptions
|
|
Handlers []pluginworker.JobHandler
|
|
}
|
|
|
|
// Harness manages an in-process plugin admin server and worker.
|
|
type Harness struct {
|
|
t *testing.T
|
|
|
|
pluginSvc *plugin.Plugin
|
|
|
|
adminServer *transport.Server
|
|
adminGrpcAddr string
|
|
|
|
worker *pluginworker.Worker
|
|
workerCtx context.Context
|
|
workerCancel context.CancelFunc
|
|
workerDone chan struct{}
|
|
}
|
|
|
|
// NewHarness starts a plugin admin server — serving PluginControlService.
|
|
// WorkerStream over the native ZAP transport — and a worker connected to it.
|
|
func NewHarness(t *testing.T, cfg HarnessConfig) *Harness {
|
|
t.Helper()
|
|
|
|
pluginOpts := cfg.PluginOptions
|
|
if pluginOpts.DataDir == "" {
|
|
pluginOpts.DataDir = t.TempDir()
|
|
}
|
|
|
|
pluginSvc, err := plugin.New(pluginOpts)
|
|
require.NoError(t, err)
|
|
|
|
// The worker resolves AdminServer "host:0.P" to gRPC port P, then dials the
|
|
// PluginControlService stream on the sibling ZAP port P+1 (see worker.go
|
|
// pluginZapAddress). So bind the ZAP listener on an OS-assigned port Z and
|
|
// advertise P = Z-1 — no gRPC, the WorkerStream bidi runs on pure ZAP.
|
|
streamHandler := func(method uint32, init []byte, st transport.Stream) {
|
|
if method != pluginwire.PluginControlServiceWorkerStreamOrdinal {
|
|
return
|
|
}
|
|
pluginSvc.ServeWorkerStream(init, st)
|
|
}
|
|
adminServer, err := transport.ListenStream("tcp", "127.0.0.1:0", nil, streamHandler)
|
|
require.NoError(t, err)
|
|
|
|
zapPort := adminServer.Addr().(*net.TCPAddr).Port
|
|
adminGrpcAddr := fmt.Sprintf("127.0.0.1:%d", zapPort-1)
|
|
adminAddr := fmt.Sprintf("127.0.0.1:0.%d", zapPort-1)
|
|
|
|
workerOpts := cfg.WorkerOptions
|
|
if workerOpts.AdminServer == "" {
|
|
workerOpts.AdminServer = adminAddr
|
|
}
|
|
// The WorkerStream runs on the ZAP transport (transport.Dial), carrying no
|
|
// gRPC; the zero GrpcDialOption (plaintext) is all NewWorker needs.
|
|
if workerOpts.WorkerID == "" {
|
|
workerOpts.WorkerID = "plugin-worker-test"
|
|
}
|
|
if workerOpts.WorkerVersion == "" {
|
|
workerOpts.WorkerVersion = "test"
|
|
}
|
|
if workerOpts.WorkerAddress == "" {
|
|
workerOpts.WorkerAddress = "127.0.0.1"
|
|
}
|
|
if len(cfg.Handlers) > 0 {
|
|
workerOpts.Handlers = cfg.Handlers
|
|
}
|
|
|
|
worker, err := pluginworker.NewWorker(workerOpts)
|
|
require.NoError(t, err)
|
|
|
|
workerCtx, workerCancel := context.WithCancel(context.Background())
|
|
workerDone := make(chan struct{})
|
|
go func() {
|
|
defer close(workerDone)
|
|
_ = worker.Run(workerCtx)
|
|
}()
|
|
|
|
harness := &Harness{
|
|
t: t,
|
|
pluginSvc: pluginSvc,
|
|
adminServer: adminServer,
|
|
adminGrpcAddr: adminGrpcAddr,
|
|
worker: worker,
|
|
workerCtx: workerCtx,
|
|
workerCancel: workerCancel,
|
|
workerDone: workerDone,
|
|
}
|
|
|
|
require.Eventually(t, func() bool {
|
|
return len(pluginSvc.ListWorkers()) > 0
|
|
}, 5*time.Second, 50*time.Millisecond)
|
|
|
|
t.Cleanup(func() {
|
|
harness.Shutdown()
|
|
})
|
|
|
|
return harness
|
|
}
|
|
|
|
// Plugin exposes the underlying admin plugin service.
|
|
func (h *Harness) Plugin() *plugin.Plugin {
|
|
return h.pluginSvc
|
|
}
|
|
|
|
// AdminGrpcAddress returns the gRPC address for the admin server.
|
|
func (h *Harness) AdminGrpcAddress() string {
|
|
return h.adminGrpcAddr
|
|
}
|
|
|
|
// WaitForJobType waits until a worker with the given capability is registered.
|
|
func (h *Harness) WaitForJobType(jobType string) {
|
|
h.t.Helper()
|
|
require.Eventually(h.t, func() bool {
|
|
workers := h.pluginSvc.ListWorkers()
|
|
for _, worker := range workers {
|
|
if worker == nil || worker.Capabilities == nil {
|
|
continue
|
|
}
|
|
if _, ok := worker.Capabilities[jobType]; ok {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}, 5*time.Second, 50*time.Millisecond)
|
|
}
|
|
|
|
// Shutdown stops the worker and admin server.
|
|
func (h *Harness) Shutdown() {
|
|
if h.workerCancel != nil {
|
|
h.workerCancel()
|
|
}
|
|
|
|
if h.workerDone != nil {
|
|
select {
|
|
case <-h.workerDone:
|
|
case <-time.After(2 * time.Second):
|
|
}
|
|
}
|
|
|
|
if h.adminServer != nil {
|
|
_ = h.adminServer.Close()
|
|
}
|
|
|
|
if h.pluginSvc != nil {
|
|
h.pluginSvc.Shutdown()
|
|
}
|
|
}
|