@@ -24,7 +24,7 @@ import (
24
24
"github.com/gin-gonic/gin"
25
25
"github.com/pingcap/errors"
26
26
"github.com/pingcap/log"
27
- tidbkv "github.com/pingcap/tidb/kv"
27
+ "github.com/pingcap/tidb/kv"
28
28
"github.com/pingcap/tiflow/cdc/api"
29
29
"github.com/pingcap/tiflow/cdc/capture"
30
30
"github.com/pingcap/tiflow/cdc/model"
@@ -68,30 +68,34 @@ func (h *OpenAPIV2) createChangefeed(c *gin.Context) {
68
68
_ = c .Error (cerror .WrapError (cerror .ErrAPIInvalidParam , err ))
69
69
return
70
70
}
71
+ var pdClient pd.Client
72
+ var kvStorage kv.Storage
73
+ // if PDAddrs is empty, use the default pdClient
71
74
if len (cfg .PDAddrs ) == 0 {
72
75
up , err := getCaptureDefaultUpstream (h .capture )
73
76
if err != nil {
74
77
_ = c .Error (err )
75
78
return
76
79
}
77
- cfg .PDConfig = getUpstreamPDConfig (up )
78
- }
79
- credential := cfg .PDConfig .toCredential ()
80
-
81
- timeoutCtx , cancel := context .WithTimeout (ctx , 30 * time .Second )
82
- defer cancel ()
83
- pdClient , err := h .helpers .getPDClient (timeoutCtx , cfg .PDAddrs , credential )
84
- if err != nil {
85
- _ = c .Error (cerror .WrapError (cerror .ErrAPIGetPDClientFailed , err ))
86
- return
87
- }
88
- defer pdClient .Close ()
89
-
90
- // verify tables todo: del kvstore
91
- kvStorage , err := h .helpers .createTiStore (cfg .PDAddrs , credential )
92
- if err != nil {
93
- _ = c .Error (cerror .WrapError (cerror .ErrNewStore , err ))
94
- return
80
+ pdClient = up .PDClient
81
+ kvStorage = up .KVStorage
82
+ } else {
83
+ credential := cfg .PDConfig .toCredential ()
84
+ timeoutCtx , cancel := context .WithTimeout (ctx , 30 * time .Second )
85
+ defer cancel ()
86
+ var err error
87
+ pdClient , err = h .helpers .getPDClient (timeoutCtx , cfg .PDAddrs , credential )
88
+ if err != nil {
89
+ _ = c .Error (cerror .WrapError (cerror .ErrAPIGetPDClientFailed , err ))
90
+ return
91
+ }
92
+ defer pdClient .Close ()
93
+ // verify tables todo: del kvstore
94
+ kvStorage , err = h .helpers .createTiStore (cfg .PDAddrs , credential )
95
+ if err != nil {
96
+ _ = c .Error (cerror .WrapError (cerror .ErrNewStore , err ))
97
+ return
98
+ }
95
99
}
96
100
// We should not close kvStorage since all kvStorage in cdc is the same one.
97
101
// defer kvStorage.Close()
@@ -132,19 +136,24 @@ func (h *OpenAPIV2) createChangefeed(c *gin.Context) {
132
136
CertAllowedCN : cfg .CertAllowedCN ,
133
137
}
134
138
135
- // cannot create changefeed if there are running lightning/restore tasks
136
- tlsCfg , err := credential .ToTLSConfig ()
137
- if err != nil {
138
- _ = c .Error (err )
139
- return
140
- }
141
-
142
- cli , err := h .helpers .getEtcdClient (cfg .PDAddrs , tlsCfg )
143
- if err != nil {
144
- _ = c .Error (err )
145
- return
139
+ var etcdCli * clientv3.Client
140
+ if len (cfg .PDAddrs ) == 0 {
141
+ etcdCli = h .capture .GetEtcdClient ().GetEtcdClient ().Unwrap ()
142
+ } else {
143
+ credential := cfg .PDConfig .toCredential ()
144
+ // cannot create changefeed if there are running lightning/restore tasks
145
+ tlsCfg , err := credential .ToTLSConfig ()
146
+ if err != nil {
147
+ _ = c .Error (err )
148
+ return
149
+ }
150
+ etcdCli , err = h .helpers .getEtcdClient (cfg .PDAddrs , tlsCfg )
151
+ if err != nil {
152
+ _ = c .Error (err )
153
+ return
154
+ }
146
155
}
147
- err = hasRunningImport (ctx , cli )
156
+ err = hasRunningImport (ctx , etcdCli )
148
157
if err != nil {
149
158
log .Error ("failed to create changefeed" , zap .Error (err ))
150
159
_ = c .Error (
@@ -299,20 +308,23 @@ func (h *OpenAPIV2) verifyTable(c *gin.Context) {
299
308
_ = c .Error (cerror .WrapError (cerror .ErrAPIInvalidParam , err ))
300
309
return
301
310
}
311
+ var kvStore kv.Storage
312
+ // if PDAddrs is empty, use the default upstream
302
313
if len (cfg .PDAddrs ) == 0 {
303
314
up , err := getCaptureDefaultUpstream (h .capture )
304
315
if err != nil {
305
316
_ = c .Error (err )
306
317
return
307
318
}
308
- cfg .PDConfig = getUpstreamPDConfig (up )
309
- }
310
- credential := cfg .PDConfig .toCredential ()
311
-
312
- kvStore , err := h .helpers .createTiStore (cfg .PDAddrs , credential )
313
- if err != nil {
314
- _ = c .Error (err )
315
- return
319
+ kvStore = up .KVStorage
320
+ } else {
321
+ credential := cfg .PDConfig .toCredential ()
322
+ var err error
323
+ kvStore , err = h .helpers .createTiStore (cfg .PDAddrs , credential )
324
+ if err != nil {
325
+ _ = c .Error (errors .Trace (err ))
326
+ return
327
+ }
316
328
}
317
329
replicaCfg := cfg .ReplicaConfig .ToInternalReplicaConfig ()
318
330
ineligibleTables , eligibleTables , err := h .helpers .
@@ -419,7 +431,7 @@ func (h *OpenAPIV2) updateChangefeed(c *gin.Context) {
419
431
return
420
432
}
421
433
422
- var storage tidbkv .Storage
434
+ var storage kv .Storage
423
435
// if PDAddrs is not empty, use it to create a new kvstore
424
436
// Note: upManager is nil in some unit test cases
425
437
if len (updateCfConfig .PDAddrs ) != 0 || upManager == nil {
@@ -866,48 +878,55 @@ func (h *OpenAPIV2) synced(c *gin.Context) {
866
878
cfg .ReplicaConfig .SyncedStatus .CheckpointInterval = status .CheckpointInterval
867
879
cfg .ReplicaConfig .SyncedStatus .SyncedCheckInterval = status .SyncedCheckInterval
868
880
}
881
+ if c .Request .Body != nil && c .Request .ContentLength > 0 {
882
+ if err := c .BindJSON (cfg ); err != nil {
883
+ _ = c .Error (cerror .WrapError (cerror .ErrAPIInvalidParam , err ))
884
+ return
885
+ }
886
+ }
869
887
870
888
// try to get pd client to get pd time, and determine synced status based on the pd time
889
+ var pdClient pd.Client
871
890
if len (cfg .PDAddrs ) == 0 {
872
891
up , err := getCaptureDefaultUpstream (h .capture )
873
892
if err != nil {
874
893
_ = c .Error (err )
875
894
return
876
895
}
877
- cfg .PDConfig = getUpstreamPDConfig (up )
878
- }
879
- credential := cfg .PDConfig .toCredential ()
880
-
881
- timeoutCtx , cancel := context .WithTimeout (ctx , timeout )
882
- defer cancel ()
896
+ pdClient = up .PDClient
897
+ } else {
898
+ credential := cfg .PDConfig .toCredential ()
899
+ timeoutCtx , cancel := context .WithTimeout (ctx , timeout )
900
+ defer cancel ()
883
901
884
- pdClient , err := h .helpers .getPDClient (timeoutCtx , cfg .PDAddrs , credential )
885
- if err != nil {
886
- // case 1. we can't get pd client, pd may be unavailable.
887
- // if pullerResolvedTs - checkpointTs > checkpointInterval, data is not synced
888
- // otherwise, if pd is unavailable, we decide data whether is synced based on
889
- // the time difference between current time and lastSyncedTs.
890
- var message string
891
- if (oracle .ExtractPhysical (status .PullerResolvedTs ) - oracle .ExtractPhysical (status .CheckpointTs )) >
892
- cfg .ReplicaConfig .SyncedStatus .CheckpointInterval * 1000 {
893
- message = fmt .Sprintf ("%s. Besides the data is not finish syncing" , err .Error ())
894
- } else {
895
- message = fmt .Sprintf ("%s. You should check the pd status first. If pd status is normal, means we don't finish sync data. " +
896
- "If pd is offline, please check whether we satisfy the condition that " +
897
- "the time difference from lastSyncedTs to the current time from the time zone of pd is greater than %v secs. " +
898
- "If it's satisfied, means the data syncing is totally finished" , err , cfg .ReplicaConfig .SyncedStatus .SyncedCheckInterval )
902
+ pdClient , err = h .helpers .getPDClient (timeoutCtx , cfg .PDAddrs , credential )
903
+ if err != nil {
904
+ // case 1. we can't get pd client, pd may be unavailable.
905
+ // if pullerResolvedTs - checkpointTs > checkpointInterval, data is not synced
906
+ // otherwise, if pd is unavailable, we decide data whether is synced based on
907
+ // the time difference between current time and lastSyncedTs.
908
+ var message string
909
+ if (oracle .ExtractPhysical (status .PullerResolvedTs ) - oracle .ExtractPhysical (status .CheckpointTs )) >
910
+ cfg .ReplicaConfig .SyncedStatus .CheckpointInterval * 1000 {
911
+ message = fmt .Sprintf ("%s. Besides the data is not finish syncing" , err .Error ())
912
+ } else {
913
+ message = fmt .Sprintf ("%s. You should check the pd status first. If pd status is normal, means we don't finish sync data. " +
914
+ "If pd is offline, please check whether we satisfy the condition that " +
915
+ "the time difference from lastSyncedTs to the current time from the time zone of pd is greater than %v secs. " +
916
+ "If it's satisfied, means the data syncing is totally finished" , err , cfg .ReplicaConfig .SyncedStatus .SyncedCheckInterval )
917
+ }
918
+ c .JSON (http .StatusOK , SyncedStatus {
919
+ Synced : false ,
920
+ SinkCheckpointTs : model .JSONTime (oracle .GetTimeFromTS (status .CheckpointTs )),
921
+ PullerResolvedTs : model .JSONTime (oracle .GetTimeFromTS (status .PullerResolvedTs )),
922
+ LastSyncedTs : model .JSONTime (oracle .GetTimeFromTS (status .LastSyncedTs )),
923
+ NowTs : model .JSONTime (time .Unix (0 , 0 )),
924
+ Info : message ,
925
+ })
926
+ return
899
927
}
900
- c .JSON (http .StatusOK , SyncedStatus {
901
- Synced : false ,
902
- SinkCheckpointTs : model .JSONTime (oracle .GetTimeFromTS (status .CheckpointTs )),
903
- PullerResolvedTs : model .JSONTime (oracle .GetTimeFromTS (status .PullerResolvedTs )),
904
- LastSyncedTs : model .JSONTime (oracle .GetTimeFromTS (status .LastSyncedTs )),
905
- NowTs : model .JSONTime (time .Unix (0 , 0 )),
906
- Info : message ,
907
- })
908
- return
928
+ defer pdClient .Close ()
909
929
}
910
- defer pdClient .Close ()
911
930
// get time from pd
912
931
physicalNow , _ , _ := pdClient .GetTS (ctx )
913
932
@@ -1027,12 +1046,3 @@ func getCaptureDefaultUpstream(cp capture.Capture) (*upstream.Upstream, error) {
1027
1046
}
1028
1047
return up , nil
1029
1048
}
1030
-
1031
- func getUpstreamPDConfig (up * upstream.Upstream ) PDConfig {
1032
- return PDConfig {
1033
- PDAddrs : up .PdEndpoints ,
1034
- KeyPath : up .SecurityConfig .KeyPath ,
1035
- CAPath : up .SecurityConfig .CAPath ,
1036
- CertPath : up .SecurityConfig .CertPath ,
1037
- }
1038
- }
0 commit comments