@@ -310,9 +310,89 @@ const (
310
310
// RuleOp is for batching placement rule actions. The action type is
311
311
// distinguished by the field `Action`.
312
312
type RuleOp struct {
313
- * placement.TiFlashRule // information of the placement rule to add/delete the operation type
314
- Action RuleOpType `json:"action"`
315
- DeleteByIDPrefix bool `json:"delete_by_id_prefix"` // if action == delete, delete by the prefix of id
313
+ * placement.TiFlashRule // information of the placement rule to add/delete the operation type
314
+ Action RuleOpType
315
+ DeleteByIDPrefix bool // if action == delete, delete by the prefix of id
316
+ }
317
+
318
+ var _ json.Marshaler = (* RuleOp )(nil )
319
+ var _ json.Unmarshaler = (* RuleOp )(nil )
320
+
321
+ type ruleOp struct {
322
+ GroupID string `json:"group_id"`
323
+ ID string `json:"id"`
324
+ Index int `json:"index,omitempty"`
325
+ Override bool `json:"override,omitempty"`
326
+ Role placement.PeerRoleType `json:"role"`
327
+ Count int `json:"count"`
328
+ Constraints placement.Constraints `json:"label_constraints,omitempty"`
329
+ LocationLabels []string `json:"location_labels,omitempty"`
330
+ IsolationLevel string `json:"isolation_level,omitempty"`
331
+ StartKeyHex string `json:"start_key"`
332
+ EndKeyHex string `json:"end_key"`
333
+ Action RuleOpType `json:"action"`
334
+ DeleteByIDPrefix bool `json:"delete_by_id_prefix"`
335
+ }
336
+
337
+ // MarshalJSON implements json.Marshaler interface for RuleOp.
338
+ func (r * RuleOp ) MarshalJSON () ([]byte , error ) {
339
+ return json .Marshal (& ruleOp {
340
+ GroupID : r .GroupID ,
341
+ ID : r .ID ,
342
+ Index : r .Index ,
343
+ Override : r .Override ,
344
+ Role : r .Role ,
345
+ Count : r .Count ,
346
+ Constraints : r .Constraints ,
347
+ LocationLabels : r .LocationLabels ,
348
+ IsolationLevel : r .IsolationLevel ,
349
+ StartKeyHex : hex .EncodeToString (codec .EncodeBytes (nil , r .StartKey )),
350
+ EndKeyHex : hex .EncodeToString (codec .EncodeBytes (nil , r .EndKey )),
351
+ Action : r .Action ,
352
+ DeleteByIDPrefix : r .DeleteByIDPrefix ,
353
+ })
354
+ }
355
+
356
+ // UnmarshalJSON implements json.Unmarshaler interface for RuleOp.
357
+ func (r * RuleOp ) UnmarshalJSON (bytes []byte ) error {
358
+ var rule ruleOp
359
+ if err := json .Unmarshal (bytes , & rule ); err != nil {
360
+ return err
361
+ }
362
+ * r = RuleOp {
363
+ TiFlashRule : & placement.TiFlashRule {
364
+ GroupID : rule .GroupID ,
365
+ ID : rule .ID ,
366
+ Index : rule .Index ,
367
+ Override : rule .Override ,
368
+ Role : rule .Role ,
369
+ Count : rule .Count ,
370
+ Constraints : rule .Constraints ,
371
+ LocationLabels : rule .LocationLabels ,
372
+ IsolationLevel : rule .IsolationLevel ,
373
+ },
374
+ Action : rule .Action ,
375
+ DeleteByIDPrefix : rule .DeleteByIDPrefix ,
376
+ }
377
+
378
+ startKey , err := hex .DecodeString (rule .StartKeyHex )
379
+ if err != nil {
380
+ return err
381
+ }
382
+
383
+ endKey , err := hex .DecodeString (rule .EndKeyHex )
384
+ if err != nil {
385
+ return err
386
+ }
387
+
388
+ _ , r .StartKey , err = codec .DecodeBytes (startKey , nil )
389
+ if err != nil {
390
+ return err
391
+ }
392
+
393
+ _ , r .EndKey , err = codec .DecodeBytes (endKey , nil )
394
+
395
+ return err
316
396
}
317
397
318
398
func (m * TiFlashReplicaManagerCtx ) doSetPlacementRuleBatch (ctx context.Context , rules []placement.TiFlashRule ) error {
0 commit comments