6
6
"fmt"
7
7
8
8
"github.com/grafana/grafana/pkg/infra/kvstore"
9
- "github.com/grafana/grafana/pkg/services/featuremgmt"
10
9
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
10
"k8s.io/apimachinery/pkg/runtime"
12
11
"k8s.io/apiserver/pkg/registry/rest"
@@ -82,15 +81,25 @@ type DualWriter interface {
82
81
type DualWriterMode int
83
82
84
83
const (
85
- Mode1 DualWriterMode = iota + 1
84
+ // Mode0 represents writing to and reading from solely LegacyStorage. This mode is enabled when the
85
+ // `unifiedStorage` feature flag is not set. All reads and writes are made to LegacyStorage. None are made to Storage.
86
+ Mode0 DualWriterMode = iota
87
+ // Mode1 represents writing to and reading from LegacyStorage for all primary functionality while additionally
88
+ // reading and writing to Storage on a best effort basis for the sake of collecting metrics.
89
+ Mode1
90
+ // Mode2 is the dual writing mode that represents writing to LegacyStorage and Storage and reading from LegacyStorage.
86
91
Mode2
92
+ // Mode3 represents writing to LegacyStorage and Storage and reading from Storage.
87
93
Mode3
94
+ // Mode4 represents writing and reading from Storage.
88
95
Mode4
89
96
)
90
97
91
98
// NewDualWriter returns a new DualWriter.
92
99
func NewDualWriter (mode DualWriterMode , legacy LegacyStorage , storage Storage ) DualWriter {
93
100
switch mode {
101
+ // It is not possible to initialize a mode 0 dual writer. Mode 0 represents
102
+ // writing to legacy storage without `unifiedStorage` enabled.
94
103
case Mode1 :
95
104
// read and write only from legacy storage
96
105
return newDualWriterMode1 (legacy , storage )
@@ -129,12 +138,14 @@ func (u *updateWrapper) UpdatedObject(ctx context.Context, oldObj runtime.Object
129
138
func SetDualWritingMode (
130
139
ctx context.Context ,
131
140
kvs * kvstore.NamespacedKVStore ,
132
- features featuremgmt.FeatureToggles ,
133
- entity string ,
134
141
legacy LegacyStorage ,
135
142
storage Storage ,
143
+ entity string ,
144
+ desiredMode DualWriterMode ,
136
145
) (DualWriter , error ) {
137
146
toMode := map [string ]DualWriterMode {
147
+ // It is not possible to initialize a mode 0 dual writer. Mode 0 represents
148
+ // writing to legacy storage without `unifiedStorage` enabled.
138
149
"1" : Mode1 ,
139
150
"2" : Mode2 ,
140
151
"3" : Mode3 ,
@@ -166,7 +177,7 @@ func SetDualWritingMode(
166
177
}
167
178
168
179
// Desired mode is 2 and current mode is 1
169
- if features . IsEnabledGlobally ( featuremgmt . FlagDualWritePlaylistsMode2 ) && (currentMode == Mode1 ) {
180
+ if ( desiredMode == Mode2 ) && (currentMode == Mode1 ) {
170
181
// This is where we go through the different gates to allow the instance to migrate from mode 1 to mode 2.
171
182
// There are none between mode 1 and mode 2
172
183
currentMode = Mode2
@@ -176,17 +187,16 @@ func SetDualWritingMode(
176
187
return nil , errDualWriterSetCurrentMode
177
188
}
178
189
}
179
- // #TODO enable this check when we have a flag/config for setting mode 1 as the desired mode
180
- // if features.IsEnabledGlobally(featuremgmt.FlagDualWritePlaylistsMode1) && (currentMode == Mode2) {
181
- // // This is where we go through the different gates to allow the instance to migrate from mode 2 to mode 1.
182
- // // There are none between mode 1 and mode 2
183
- // currentMode = Mode1
184
-
185
- // err := kvs.Set(ctx, entity, fmt.Sprint(currentMode))
186
- // if err != nil {
187
- // return nil, errDualWriterSetCurrentMode
188
- // }
189
- // }
190
+ if (desiredMode == Mode1 ) && (currentMode == Mode2 ) {
191
+ // This is where we go through the different gates to allow the instance to migrate from mode 2 to mode 1.
192
+ // There are none between mode 1 and mode 2
193
+ currentMode = Mode1
194
+
195
+ err := kvs .Set (ctx , entity , fmt .Sprint (currentMode ))
196
+ if err != nil {
197
+ return nil , errDualWriterSetCurrentMode
198
+ }
199
+ }
190
200
191
201
// #TODO add support for other combinations of desired and current modes
192
202
0 commit comments