DualWriter: Add mode 5 that always uses storage (#93169)

This commit is contained in:
Ryan McKinley 2024-09-10 20:07:06 +03:00 committed by GitHub
parent c8dc4d93d4
commit d724d463b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 5 deletions

View File

@ -96,11 +96,17 @@ const (
// reading and writing to Storage on a best effort basis for the sake of collecting metrics. // reading and writing to Storage on a best effort basis for the sake of collecting metrics.
Mode1 Mode1
// Mode2 is the dual writing mode that represents writing to LegacyStorage and Storage and reading from LegacyStorage. // Mode2 is the dual writing mode that represents writing to LegacyStorage and Storage and reading from LegacyStorage.
// The objects written to storage will include any labels and annotations.
// When reading values, the results will be from Storage when they exist, otherwise from legacy storage
Mode2 Mode2
// Mode3 represents writing to LegacyStorage and Storage and reading from Storage. // Mode3 represents writing to LegacyStorage and Storage and reading from Storage.
// NOTE: Requesting mode3 will only happen when after a background sync job succeeds
Mode3 Mode3
// Mode4 represents writing and reading from Storage. // Mode4 represents writing and reading from Storage.
// NOTE: Requesting mode4 will only happen when after a background sync job succeeds
Mode4 Mode4
// Mode5 uses storage regardless of the background sync state
Mode5
) )
// TODO: make this function private as there should only be one public way of setting the dual writing mode // TODO: make this function private as there should only be one public way of setting the dual writing mode
@ -126,7 +132,7 @@ func NewDualWriter(
case Mode3: case Mode3:
// write to both, read from storage only // write to both, read from storage only
return newDualWriterMode3(legacy, storage, metrics, resource) return newDualWriterMode3(legacy, storage, metrics, resource)
case Mode4: case Mode4, Mode5:
return storage return storage
default: default:
return newDualWriterMode1(legacy, storage, metrics, resource) return newDualWriterMode1(legacy, storage, metrics, resource)
@ -199,7 +205,7 @@ func SetDualWritingMode(
if !valid && ok { if !valid && ok {
// Only log if "ok" because initially all instances will have mode unset for playlists. // Only log if "ok" because initially all instances will have mode unset for playlists.
klog.Info("invalid dual writing mode for playlists mode:", m) klog.Infof("invalid dual writing mode for %s mode: %v", entity, m)
} }
if !valid || !ok { if !valid || !ok {

View File

@ -21,6 +21,7 @@ import (
utilversion "k8s.io/apiserver/pkg/util/version" utilversion "k8s.io/apiserver/pkg/util/version"
k8sscheme "k8s.io/client-go/kubernetes/scheme" k8sscheme "k8s.io/client-go/kubernetes/scheme"
k8stracing "k8s.io/component-base/tracing" k8stracing "k8s.io/component-base/tracing"
"k8s.io/klog/v2"
"k8s.io/kube-openapi/pkg/common" "k8s.io/kube-openapi/pkg/common"
"github.com/grafana/grafana/pkg/apiserver/endpoints/filters" "github.com/grafana/grafana/pkg/apiserver/endpoints/filters"
@ -174,25 +175,36 @@ func InstallAPIs(
mode = resourceConfig.DualWriterMode mode = resourceConfig.DualWriterMode
} }
// Force using storage only -- regardless of internal synchronization state
if mode == grafanarest.Mode5 {
return storage, nil
}
// TODO: inherited context from main Grafana process // TODO: inherited context from main Grafana process
ctx := context.Background() ctx := context.Background()
// Moving from one version to the next can only happen after the previous step has // Moving from one version to the next can only happen after the previous step has
// successfully synchronized. // successfully synchronized.
currentMode, err := grafanarest.SetDualWritingMode(ctx, kvStore, legacy, storage, key, mode, reg, serverLock, getRequestInfo(gr, namespaceMapper)) requestInfo := getRequestInfo(gr, namespaceMapper)
currentMode, err := grafanarest.SetDualWritingMode(ctx, kvStore, legacy, storage, key, mode, reg, serverLock, requestInfo)
if err != nil { if err != nil {
return nil, err return nil, err
} }
switch currentMode { switch currentMode {
case grafanarest.Mode0: case grafanarest.Mode0:
return legacy, nil return legacy, nil
case grafanarest.Mode4: case grafanarest.Mode4, grafanarest.Mode5:
return storage, nil return storage, nil
default: default:
} }
if storageOpts.DualWriterDataSyncJobEnabled[key] { if storageOpts.DualWriterDataSyncJobEnabled[key] {
grafanarest.StartPeriodicDataSyncer(ctx, currentMode, legacy, storage, key, reg, serverLock, getRequestInfo(gr, namespaceMapper)) grafanarest.StartPeriodicDataSyncer(ctx, currentMode, legacy, storage, key, reg, serverLock, requestInfo)
}
// when unable to use
if currentMode != mode {
klog.Warningf("Requested DualWrite mode: %d, but using %d for %+v", mode, currentMode, gr)
} }
return grafanarest.NewDualWriter(currentMode, legacy, storage, reg, key), nil return grafanarest.NewDualWriter(currentMode, legacy, storage, reg, key), nil
} }