mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
kms: run provider health checks without the manager's lock (#13295)
* kms: run provider health checks without the manager's lock GetKMSHealth held the manager's read lock while it asked every provider for a key, and a remote KMS can take its whole timeout to answer. A provider change waited behind it for the write lock, and every request looking up a provider then waited behind the change. The checks now run on a copy of the providers. SetDefaultKMSProvider and SetBucketKMSProvider check that the provider exists and set it under one lock, so a provider removed in between can't be left as the default or as a bucket's provider. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * kms: health-check only the provider a load adds Loading a provider checked every provider loaded so far, so a configuration with N providers made N(N+1)/2 remote calls at startup, and one slow provider could spend the shared 10s budget and get the new provider reported unhealthy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * kms: run provider health checks concurrently The checks shared one deadline and ran one after another, so a provider stuck until the deadline got every provider checked after it reported unhealthy, and startup waited for the checks in turn. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
3 files changed
+154
-35
No files matched your search
+38
-29
@@ -2,6 +2,7 @@ package kms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -242,18 +243,17 @@ func (km *KMSManager) AddKMSProvider(name string, config *KMSConfig) error {
|
||||
|
||||
// SetDefaultKMSProvider sets the default KMS provider
|
||||
func (km *KMSManager) SetDefaultKMSProvider(name string) error {
|
||||
km.mu.RLock()
|
||||
km.mu.Lock()
|
||||
_, exists := km.providers[name]
|
||||
km.mu.RUnlock()
|
||||
if exists {
|
||||
km.defaultKMS = name
|
||||
}
|
||||
km.mu.Unlock()
|
||||
|
||||
if !exists {
|
||||
return fmt.Errorf("KMS provider %s does not exist", name)
|
||||
}
|
||||
|
||||
km.mu.Lock()
|
||||
km.defaultKMS = name
|
||||
km.mu.Unlock()
|
||||
|
||||
glog.V(1).Infof("Set default KMS provider to %s", name)
|
||||
return nil
|
||||
}
|
||||
@@ -264,18 +264,17 @@ func (km *KMSManager) SetBucketKMSProvider(bucket, providerName string) error {
|
||||
return fmt.Errorf("bucket name cannot be empty")
|
||||
}
|
||||
|
||||
km.mu.RLock()
|
||||
km.mu.Lock()
|
||||
_, exists := km.providers[providerName]
|
||||
km.mu.RUnlock()
|
||||
if exists {
|
||||
km.bucketKMS[bucket] = providerName
|
||||
}
|
||||
km.mu.Unlock()
|
||||
|
||||
if !exists {
|
||||
return fmt.Errorf("KMS provider %s does not exist", providerName)
|
||||
}
|
||||
|
||||
km.mu.Lock()
|
||||
km.bucketKMS[bucket] = providerName
|
||||
km.mu.Unlock()
|
||||
|
||||
glog.V(2).Infof("Set KMS provider for bucket %s to %s", bucket, providerName)
|
||||
return nil
|
||||
}
|
||||
@@ -463,26 +462,36 @@ func (km *KMSManager) ValidateKeyForBucket(ctx context.Context, bucket, keyID st
|
||||
|
||||
// GetKMSHealth returns health status of all KMS providers
|
||||
func (km *KMSManager) GetKMSHealth(ctx context.Context) map[string]error {
|
||||
// The checks run without the lock: one can wait on a remote KMS for its whole timeout,
|
||||
// and a writer queued behind it would block every request looking up a provider.
|
||||
km.mu.RLock()
|
||||
defer km.mu.RUnlock()
|
||||
|
||||
health := make(map[string]error)
|
||||
|
||||
providers := make(map[string]KMSProvider, len(km.providers))
|
||||
for name, provider := range km.providers {
|
||||
// Try to perform a basic operation to check health
|
||||
// We'll use DescribeKey with a dummy key - the error will tell us if KMS is reachable
|
||||
req := &DescribeKeyRequest{KeyID: "health-check-dummy-key"}
|
||||
_, err := provider.DescribeKey(ctx, req)
|
||||
|
||||
// If it's a "not found" error, KMS is healthy but key doesn't exist (expected)
|
||||
if kmsErr, ok := err.(*KMSError); ok && kmsErr.Code == ErrCodeNotFoundException {
|
||||
health[name] = nil // Healthy
|
||||
} else if err != nil {
|
||||
health[name] = err // Unhealthy
|
||||
} else {
|
||||
health[name] = nil // Healthy (shouldn't happen with dummy key, but just in case)
|
||||
}
|
||||
providers[name] = provider
|
||||
}
|
||||
km.mu.RUnlock()
|
||||
|
||||
health := make(map[string]error, len(providers))
|
||||
var mu sync.Mutex
|
||||
var wg sync.WaitGroup
|
||||
for name, provider := range providers {
|
||||
wg.Go(func() {
|
||||
err := checkHealth(ctx, provider)
|
||||
mu.Lock()
|
||||
health[name] = err
|
||||
mu.Unlock()
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
return health
|
||||
}
|
||||
|
||||
// checkHealth asks for a key that doesn't exist, so NotFound means the KMS is reachable.
|
||||
func checkHealth(ctx context.Context, provider KMSProvider) error {
|
||||
_, err := provider.DescribeKey(ctx, &DescribeKeyRequest{KeyID: "health-check-dummy-key"})
|
||||
var kmsErr *KMSError
|
||||
if errors.As(err, &kmsErr) && kmsErr.Code == ErrCodeNotFoundException {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -173,12 +173,12 @@ func (loader *ConfigLoader) loadSingleKMSProvider(providerName string, config ma
|
||||
}
|
||||
|
||||
// Test the provider with a health check
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
health := loader.manager.GetKMSHealth(ctx)
|
||||
if providerHealth, exists := health[providerName]; exists && providerHealth != nil {
|
||||
glog.Warningf("KMS provider %s health check failed: %v", providerName, providerHealth)
|
||||
if provider, err := loader.manager.GetKMSProviderByName(providerName); err == nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := checkHealth(ctx, provider); err != nil {
|
||||
glog.Warningf("KMS provider %s health check failed: %v", providerName, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package kms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -110,3 +111,112 @@ func TestAddKMSProviderDoesNotBlockLookups(t *testing.T) {
|
||||
t.Fatalf("AddKMSProvider (slow): %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type stalledDescribe struct {
|
||||
KMSProvider
|
||||
started, release chan struct{}
|
||||
}
|
||||
|
||||
func (s *stalledDescribe) DescribeKey(ctx context.Context, req *DescribeKeyRequest) (*DescribeKeyResponse, error) {
|
||||
close(s.started)
|
||||
<-s.release
|
||||
return nil, &KMSError{Code: ErrCodeNotFoundException}
|
||||
}
|
||||
|
||||
func TestHealthCheckDoesNotBlockChanges(t *testing.T) {
|
||||
RegisterProvider("health-test", func(util.Configuration) (KMSProvider, error) {
|
||||
return newCountingKMSProvider(), nil
|
||||
})
|
||||
stalled := &stalledDescribe{KMSProvider: newCountingKMSProvider(), started: make(chan struct{}), release: make(chan struct{})}
|
||||
mgr := &KMSManager{
|
||||
providers: map[string]KMSProvider{"remote": stalled},
|
||||
configs: make(map[string]*KMSConfig),
|
||||
bucketKMS: make(map[string]string),
|
||||
}
|
||||
|
||||
checked := make(chan struct{})
|
||||
go func() {
|
||||
mgr.GetKMSHealth(context.Background())
|
||||
close(checked)
|
||||
}()
|
||||
<-stalled.started
|
||||
added := make(chan error, 1)
|
||||
go func() { added <- mgr.AddKMSProvider("other", &KMSConfig{Provider: "health-test"}) }()
|
||||
select {
|
||||
case err := <-added:
|
||||
if err != nil {
|
||||
t.Fatalf("AddKMSProvider: %v", err)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("a provider change waited for a health check")
|
||||
}
|
||||
|
||||
close(stalled.release)
|
||||
<-checked
|
||||
}
|
||||
|
||||
type describeCounter struct {
|
||||
KMSProvider
|
||||
calls int
|
||||
}
|
||||
|
||||
func (d *describeCounter) DescribeKey(ctx context.Context, req *DescribeKeyRequest) (*DescribeKeyResponse, error) {
|
||||
d.calls++
|
||||
return nil, &KMSError{Code: ErrCodeNotFoundException}
|
||||
}
|
||||
|
||||
func TestLoadingAProviderChecksOnlyThatProvider(t *testing.T) {
|
||||
var created []*describeCounter
|
||||
RegisterProvider("load-check-test", func(util.Configuration) (KMSProvider, error) {
|
||||
p := &describeCounter{KMSProvider: newCountingKMSProvider()}
|
||||
created = append(created, p)
|
||||
return p, nil
|
||||
})
|
||||
loader := &ConfigLoader{manager: &KMSManager{
|
||||
providers: make(map[string]KMSProvider),
|
||||
configs: make(map[string]*KMSConfig),
|
||||
bucketKMS: make(map[string]string),
|
||||
}}
|
||||
providers := make(map[string]interface{})
|
||||
for _, name := range []string{"a", "b", "c"} {
|
||||
providers[name] = map[string]interface{}{"type": "load-check-test", "cache_enabled": false}
|
||||
}
|
||||
|
||||
if err := loader.loadKMSProviders(providers); err != nil {
|
||||
t.Fatalf("loadKMSProviders: %v", err)
|
||||
}
|
||||
if len(created) != 3 {
|
||||
t.Fatalf("created %d providers, want 3", len(created))
|
||||
}
|
||||
for i, p := range created {
|
||||
if p.calls != 1 {
|
||||
t.Errorf("provider %d was checked %d times, want once", i, p.calls)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthChecksDoNotWaitForEachOther(t *testing.T) {
|
||||
release := make(chan struct{})
|
||||
a := &stalledDescribe{KMSProvider: newCountingKMSProvider(), started: make(chan struct{}), release: release}
|
||||
b := &stalledDescribe{KMSProvider: newCountingKMSProvider(), started: make(chan struct{}), release: release}
|
||||
mgr := &KMSManager{
|
||||
providers: map[string]KMSProvider{"a": a, "b": b},
|
||||
configs: make(map[string]*KMSConfig),
|
||||
bucketKMS: make(map[string]string),
|
||||
}
|
||||
|
||||
done := make(chan map[string]error, 1)
|
||||
go func() { done <- mgr.GetKMSHealth(context.Background()) }()
|
||||
for _, s := range []*stalledDescribe{a, b} {
|
||||
select {
|
||||
case <-s.started:
|
||||
case <-time.After(time.Second):
|
||||
close(release)
|
||||
t.Fatal("a provider's health check waited for another's")
|
||||
}
|
||||
}
|
||||
close(release)
|
||||
if health := <-done; len(health) != 2 || health["a"] != nil || health["b"] != nil {
|
||||
t.Fatalf("health = %v, want both providers healthy", health)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user