@@ -25,6 +25,7 @@ import (
25
25
"github.com/gin-gonic/gin"
26
26
"github.com/pingcap/errors"
27
27
"github.com/pingcap/log"
28
+ "github.com/pingcap/tidb/pkg/kv"
28
29
tidbkv "github.com/pingcap/tidb/pkg/kv"
29
30
"github.com/pingcap/tiflow/cdc/api"
30
31
"github.com/pingcap/tiflow/cdc/capture"
@@ -66,30 +67,34 @@ func (h *OpenAPIV2) createChangefeed(c *gin.Context) {
66
67
_ = c .Error (cerror .WrapError (cerror .ErrAPIInvalidParam , err ))
67
68
return
68
69
}
70
+ var pdClient pd.Client
71
+ var kvStorage kv.Storage
72
+ // if PDAddrs is empty, use the default pdClient
69
73
if len (cfg .PDAddrs ) == 0 {
70
74
up , err := getCaptureDefaultUpstream (h .capture )
71
75
if err != nil {
72
76
_ = c .Error (err )
73
77
return
74
78
}
75
- cfg .PDConfig = getUpstreamPDConfig (up )
76
- }
77
- credential := cfg .PDConfig .toCredential ()
78
-
79
- timeoutCtx , cancel := context .WithTimeout (ctx , 30 * time .Second )
80
- defer cancel ()
81
- pdClient , err := h .helpers .getPDClient (timeoutCtx , cfg .PDAddrs , credential )
82
- if err != nil {
83
- _ = c .Error (cerror .WrapError (cerror .ErrAPIGetPDClientFailed , err ))
84
- return
85
- }
86
- defer pdClient .Close ()
87
-
88
- // verify tables todo: del kvstore
89
- kvStorage , err := h .helpers .createTiStore (ctx , cfg .PDAddrs , credential )
90
- if err != nil {
91
- _ = c .Error (cerror .WrapError (cerror .ErrNewStore , err ))
92
- return
79
+ pdClient = up .PDClient
80
+ kvStorage = up .KVStorage
81
+ } else {
82
+ credential := cfg .PDConfig .toCredential ()
83
+ timeoutCtx , cancel := context .WithTimeout (ctx , 30 * time .Second )
84
+ defer cancel ()
85
+ var err error
86
+ pdClient , err = h .helpers .getPDClient (timeoutCtx , cfg .PDAddrs , credential )
87
+ if err != nil {
88
+ _ = c .Error (cerror .WrapError (cerror .ErrAPIGetPDClientFailed , err ))
89
+ return
90
+ }
91
+ defer pdClient .Close ()
92
+ // verify tables todo: del kvstore
93
+ kvStorage , err = h .helpers .createTiStore (ctx , cfg .PDAddrs , credential )
94
+ if err != nil {
95
+ _ = c .Error (cerror .WrapError (cerror .ErrNewStore , err ))
96
+ return
97
+ }
93
98
}
94
99
provider := h .capture .StatusProvider ()
95
100
owner , err := h .capture .GetOwner ()
@@ -136,19 +141,24 @@ func (h *OpenAPIV2) createChangefeed(c *gin.Context) {
136
141
CertAllowedCN : cfg .CertAllowedCN ,
137
142
}
138
143
139
- // cannot create changefeed if there are running lightning/restore tasks
140
- tlsCfg , err := credential .ToTLSConfig ()
141
- if err != nil {
142
- _ = c .Error (err )
143
- return
144
- }
145
-
146
- cli , err := h .helpers .getEtcdClient (ctx , cfg .PDAddrs , tlsCfg )
147
- if err != nil {
148
- _ = c .Error (err )
149
- return
144
+ var etcdCli * clientv3.Client
145
+ if len (cfg .PDAddrs ) == 0 {
146
+ etcdCli = h .capture .GetEtcdClient ().GetEtcdClient ().Unwrap ()
147
+ } else {
148
+ credential := cfg .PDConfig .toCredential ()
149
+ // cannot create changefeed if there are running lightning/restore tasks
150
+ tlsCfg , err := credential .ToTLSConfig ()
151
+ if err != nil {
152
+ _ = c .Error (err )
153
+ return
154
+ }
155
+ etcdCli , err = h .helpers .getEtcdClient (ctx , cfg .PDAddrs , tlsCfg )
156
+ if err != nil {
157
+ _ = c .Error (err )
158
+ return
159
+ }
150
160
}
151
- err = hasRunningImport (ctx , cli )
161
+ err = hasRunningImport (ctx , etcdCli )
152
162
if err != nil {
153
163
log .Error ("failed to create changefeed" , zap .Error (err ))
154
164
_ = c .Error (
@@ -319,21 +329,26 @@ func (h *OpenAPIV2) verifyTable(c *gin.Context) {
319
329
_ = c .Error (cerror .WrapError (cerror .ErrAPIInvalidParam , err ))
320
330
return
321
331
}
332
+ ctx := c .Request .Context ()
333
+ var kvStore tidbkv.Storage
334
+ // if PDAddrs is empty, use the default upstream
322
335
if len (cfg .PDAddrs ) == 0 {
323
336
up , err := getCaptureDefaultUpstream (h .capture )
324
337
if err != nil {
325
338
_ = c .Error (err )
326
339
return
327
340
}
328
- cfg .PDConfig = getUpstreamPDConfig (up )
329
- }
330
- credential := cfg .PDConfig .toCredential ()
331
- ctx := c .Request .Context ()
332
- kvStore , err := h .helpers .createTiStore (ctx , cfg .PDAddrs , credential )
333
- if err != nil {
334
- _ = c .Error (err )
335
- return
341
+ kvStore = up .KVStorage
342
+ } else {
343
+ credential := cfg .PDConfig .toCredential ()
344
+ var err error
345
+ kvStore , err = h .helpers .createTiStore (ctx , cfg .PDAddrs , credential )
346
+ if err != nil {
347
+ _ = c .Error (errors .Trace (err ))
348
+ return
349
+ }
336
350
}
351
+
337
352
uri , err := url .Parse (cfg .SinkURI )
338
353
if err != nil {
339
354
_ = c .Error (err )
@@ -924,48 +939,55 @@ func (h *OpenAPIV2) synced(c *gin.Context) {
924
939
cfg .ReplicaConfig .SyncedStatus .CheckpointInterval = status .CheckpointInterval
925
940
cfg .ReplicaConfig .SyncedStatus .SyncedCheckInterval = status .SyncedCheckInterval
926
941
}
942
+ if c .Request .Body != nil && c .Request .ContentLength > 0 {
943
+ if err := c .BindJSON (cfg ); err != nil {
944
+ _ = c .Error (cerror .WrapError (cerror .ErrAPIInvalidParam , err ))
945
+ return
946
+ }
947
+ }
927
948
928
949
// try to get pd client to get pd time, and determine synced status based on the pd time
950
+ var pdClient pd.Client
929
951
if len (cfg .PDAddrs ) == 0 {
930
952
up , err := getCaptureDefaultUpstream (h .capture )
931
953
if err != nil {
932
954
_ = c .Error (err )
933
955
return
934
956
}
935
- cfg .PDConfig = getUpstreamPDConfig (up )
936
- }
937
- credential := cfg .PDConfig .toCredential ()
938
-
939
- timeoutCtx , cancel := context .WithTimeout (ctx , timeout )
940
- defer cancel ()
957
+ pdClient = up .PDClient
958
+ } else {
959
+ credential := cfg .PDConfig .toCredential ()
960
+ timeoutCtx , cancel := context .WithTimeout (ctx , timeout )
961
+ defer cancel ()
941
962
942
- pdClient , err := h .helpers .getPDClient (timeoutCtx , cfg .PDAddrs , credential )
943
- if err != nil {
944
- // case 1. we can't get pd client, pd may be unavailable.
945
- // if pullerResolvedTs - checkpointTs > checkpointInterval, data is not synced
946
- // otherwise, if pd is unavailable, we decide data whether is synced based on
947
- // the time difference between current time and lastSyncedTs.
948
- var message string
949
- if (oracle .ExtractPhysical (status .PullerResolvedTs ) - oracle .ExtractPhysical (status .CheckpointTs )) >
950
- cfg .ReplicaConfig .SyncedStatus .CheckpointInterval * 1000 {
951
- message = fmt .Sprintf ("%s. Besides the data is not finish syncing" , err .Error ())
952
- } else {
953
- message = fmt .Sprintf ("%s. You should check the pd status first. If pd status is normal, means we don't finish sync data. " +
954
- "If pd is offline, please check whether we satisfy the condition that " +
955
- "the time difference from lastSyncedTs to the current time from the time zone of pd is greater than %v secs. " +
956
- "If it's satisfied, means the data syncing is totally finished" , err , cfg .ReplicaConfig .SyncedStatus .SyncedCheckInterval )
963
+ pdClient , err = h .helpers .getPDClient (timeoutCtx , cfg .PDAddrs , credential )
964
+ if err != nil {
965
+ // case 1. we can't get pd client, pd may be unavailable.
966
+ // if pullerResolvedTs - checkpointTs > checkpointInterval, data is not synced
967
+ // otherwise, if pd is unavailable, we decide data whether is synced based on
968
+ // the time difference between current time and lastSyncedTs.
969
+ var message string
970
+ if (oracle .ExtractPhysical (status .PullerResolvedTs ) - oracle .ExtractPhysical (status .CheckpointTs )) >
971
+ cfg .ReplicaConfig .SyncedStatus .CheckpointInterval * 1000 {
972
+ message = fmt .Sprintf ("%s. Besides the data is not finish syncing" , err .Error ())
973
+ } else {
974
+ message = fmt .Sprintf ("%s. You should check the pd status first. If pd status is normal, means we don't finish sync data. " +
975
+ "If pd is offline, please check whether we satisfy the condition that " +
976
+ "the time difference from lastSyncedTs to the current time from the time zone of pd is greater than %v secs. " +
977
+ "If it's satisfied, means the data syncing is totally finished" , err , cfg .ReplicaConfig .SyncedStatus .SyncedCheckInterval )
978
+ }
979
+ c .JSON (http .StatusOK , SyncedStatus {
980
+ Synced : false ,
981
+ SinkCheckpointTs : model .JSONTime (oracle .GetTimeFromTS (status .CheckpointTs )),
982
+ PullerResolvedTs : model .JSONTime (oracle .GetTimeFromTS (status .PullerResolvedTs )),
983
+ LastSyncedTs : model .JSONTime (oracle .GetTimeFromTS (status .LastSyncedTs )),
984
+ NowTs : model .JSONTime (time .Unix (0 , 0 )),
985
+ Info : message ,
986
+ })
987
+ return
957
988
}
958
- c .JSON (http .StatusOK , SyncedStatus {
959
- Synced : false ,
960
- SinkCheckpointTs : model .JSONTime (oracle .GetTimeFromTS (status .CheckpointTs )),
961
- PullerResolvedTs : model .JSONTime (oracle .GetTimeFromTS (status .PullerResolvedTs )),
962
- LastSyncedTs : model .JSONTime (oracle .GetTimeFromTS (status .LastSyncedTs )),
963
- NowTs : model .JSONTime (time .Unix (0 , 0 )),
964
- Info : message ,
965
- })
966
- return
989
+ defer pdClient .Close ()
967
990
}
968
- defer pdClient .Close ()
969
991
// get time from pd
970
992
physicalNow , _ , _ := pdClient .GetTS (ctx )
971
993
@@ -1094,12 +1116,3 @@ func getCaptureDefaultUpstream(cp capture.Capture) (*upstream.Upstream, error) {
1094
1116
}
1095
1117
return up , nil
1096
1118
}
1097
-
1098
- func getUpstreamPDConfig (up * upstream.Upstream ) PDConfig {
1099
- return PDConfig {
1100
- PDAddrs : up .PdEndpoints ,
1101
- KeyPath : up .SecurityConfig .KeyPath ,
1102
- CAPath : up .SecurityConfig .CAPath ,
1103
- CertPath : up .SecurityConfig .CertPath ,
1104
- }
1105
- }
0 commit comments