@@ -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"
@@ -72,30 +73,34 @@ func (h *OpenAPIV2) createChangefeed(c *gin.Context) {
72
73
_ = c .Error (cerror .WrapError (cerror .ErrAPIInvalidParam , err ))
73
74
return
74
75
}
76
+ var pdClient pd.Client
77
+ var kvStorage kv.Storage
78
+ // if PDAddrs is empty, use the default pdClient
75
79
if len (cfg .PDAddrs ) == 0 {
76
80
up , err := getCaptureDefaultUpstream (h .capture )
77
81
if err != nil {
78
82
_ = c .Error (err )
79
83
return
80
84
}
81
- cfg .PDConfig = getUpstreamPDConfig (up )
82
- }
83
- credential := cfg .PDConfig .toCredential ()
84
-
85
- timeoutCtx , cancel := context .WithTimeout (ctx , 30 * time .Second )
86
- defer cancel ()
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
-
94
- // verify tables todo: del kvstore
95
- kvStorage , err := h .helpers .createTiStore (ctx , cfg .PDAddrs , credential )
96
- if err != nil {
97
- _ = c .Error (cerror .WrapError (cerror .ErrNewStore , err ))
98
- return
85
+ pdClient = up .PDClient
86
+ kvStorage = up .KVStorage
87
+ } else {
88
+ credential := cfg .PDConfig .toCredential ()
89
+ timeoutCtx , cancel := context .WithTimeout (ctx , 30 * time .Second )
90
+ defer cancel ()
91
+ var err error
92
+ pdClient , err = h .helpers .getPDClient (timeoutCtx , cfg .PDAddrs , credential )
93
+ if err != nil {
94
+ _ = c .Error (cerror .WrapError (cerror .ErrAPIGetPDClientFailed , err ))
95
+ return
96
+ }
97
+ defer pdClient .Close ()
98
+ // verify tables todo: del kvstore
99
+ kvStorage , err = h .helpers .createTiStore (ctx , cfg .PDAddrs , credential )
100
+ if err != nil {
101
+ _ = c .Error (cerror .WrapError (cerror .ErrNewStore , err ))
102
+ return
103
+ }
99
104
}
100
105
ctrl , err := h .capture .GetController ()
101
106
if err != nil {
@@ -142,19 +147,24 @@ func (h *OpenAPIV2) createChangefeed(c *gin.Context) {
142
147
CertAllowedCN : cfg .CertAllowedCN ,
143
148
}
144
149
145
- // cannot create changefeed if there are running lightning/restore tasks
146
- tlsCfg , err := credential .ToTLSConfig ()
147
- if err != nil {
148
- _ = c .Error (err )
149
- return
150
- }
151
-
152
- cli , err := h .helpers .getEtcdClient (ctx , cfg .PDAddrs , tlsCfg )
153
- if err != nil {
154
- _ = c .Error (err )
155
- return
150
+ var etcdCli * clientv3.Client
151
+ if len (cfg .PDAddrs ) == 0 {
152
+ etcdCli = h .capture .GetEtcdClient ().GetEtcdClient ().Unwrap ()
153
+ } else {
154
+ credential := cfg .PDConfig .toCredential ()
155
+ // cannot create changefeed if there are running lightning/restore tasks
156
+ tlsCfg , err := credential .ToTLSConfig ()
157
+ if err != nil {
158
+ _ = c .Error (err )
159
+ return
160
+ }
161
+ etcdCli , err = h .helpers .getEtcdClient (ctx , cfg .PDAddrs , tlsCfg )
162
+ if err != nil {
163
+ _ = c .Error (err )
164
+ return
165
+ }
156
166
}
157
- err = hasRunningImport (ctx , cli )
167
+ err = hasRunningImport (ctx , etcdCli )
158
168
if err != nil {
159
169
log .Error ("failed to create changefeed" , zap .Error (err ))
160
170
_ = c .Error (
@@ -329,21 +339,26 @@ func (h *OpenAPIV2) verifyTable(c *gin.Context) {
329
339
_ = c .Error (cerror .WrapError (cerror .ErrAPIInvalidParam , err ))
330
340
return
331
341
}
342
+ ctx := c .Request .Context ()
343
+ var kvStore tidbkv.Storage
344
+ // if PDAddrs is empty, use the default upstream
332
345
if len (cfg .PDAddrs ) == 0 {
333
346
up , err := getCaptureDefaultUpstream (h .capture )
334
347
if err != nil {
335
348
_ = c .Error (err )
336
349
return
337
350
}
338
- cfg .PDConfig = getUpstreamPDConfig (up )
339
- }
340
- credential := cfg .PDConfig .toCredential ()
341
- ctx := c .Request .Context ()
342
- kvStore , err := h .helpers .createTiStore (ctx , cfg .PDAddrs , credential )
343
- if err != nil {
344
- _ = c .Error (err )
345
- return
351
+ kvStore = up .KVStorage
352
+ } else {
353
+ credential := cfg .PDConfig .toCredential ()
354
+ var err error
355
+ kvStore , err = h .helpers .createTiStore (ctx , cfg .PDAddrs , credential )
356
+ if err != nil {
357
+ _ = c .Error (errors .Trace (err ))
358
+ return
359
+ }
346
360
}
361
+
347
362
uri , err := url .Parse (cfg .SinkURI )
348
363
if err != nil {
349
364
_ = c .Error (err )
@@ -926,48 +941,55 @@ func (h *OpenAPIV2) synced(c *gin.Context) {
926
941
cfg .ReplicaConfig .SyncedStatus .CheckpointInterval = status .CheckpointInterval
927
942
cfg .ReplicaConfig .SyncedStatus .SyncedCheckInterval = status .SyncedCheckInterval
928
943
}
944
+ if c .Request .Body != nil && c .Request .ContentLength > 0 {
945
+ if err := c .BindJSON (cfg ); err != nil {
946
+ _ = c .Error (cerror .WrapError (cerror .ErrAPIInvalidParam , err ))
947
+ return
948
+ }
949
+ }
929
950
930
951
// try to get pd client to get pd time, and determine synced status based on the pd time
952
+ var pdClient pd.Client
931
953
if len (cfg .PDAddrs ) == 0 {
932
954
up , err := getCaptureDefaultUpstream (h .capture )
933
955
if err != nil {
934
956
_ = c .Error (err )
935
957
return
936
958
}
937
- cfg .PDConfig = getUpstreamPDConfig (up )
938
- }
939
- credential := cfg .PDConfig .toCredential ()
940
-
941
- timeoutCtx , cancel := context .WithTimeout (ctx , timeout )
942
- defer cancel ()
959
+ pdClient = up .PDClient
960
+ } else {
961
+ credential := cfg .PDConfig .toCredential ()
962
+ timeoutCtx , cancel := context .WithTimeout (ctx , timeout )
963
+ defer cancel ()
943
964
944
- pdClient , err := h .helpers .getPDClient (timeoutCtx , cfg .PDAddrs , credential )
945
- if err != nil {
946
- // case 1. we can't get pd client, pd may be unavailable.
947
- // if pullerResolvedTs - checkpointTs > checkpointInterval, data is not synced
948
- // otherwise, if pd is unavailable, we decide data whether is synced based on
949
- // the time difference between current time and lastSyncedTs.
950
- var message string
951
- if (oracle .ExtractPhysical (status .PullerResolvedTs ) - oracle .ExtractPhysical (status .CheckpointTs )) >
952
- cfg .ReplicaConfig .SyncedStatus .CheckpointInterval * 1000 {
953
- message = fmt .Sprintf ("%s. Besides the data is not finish syncing" , err .Error ())
954
- } else {
955
- message = fmt .Sprintf ("%s. You should check the pd status first. If pd status is normal, means we don't finish sync data. " +
956
- "If pd is offline, please check whether we satisfy the condition that " +
957
- "the time difference from lastSyncedTs to the current time from the time zone of pd is greater than %v secs. " +
958
- "If it's satisfied, means the data syncing is totally finished" , err , cfg .ReplicaConfig .SyncedStatus .SyncedCheckInterval )
965
+ pdClient , err = h .helpers .getPDClient (timeoutCtx , cfg .PDAddrs , credential )
966
+ if err != nil {
967
+ // case 1. we can't get pd client, pd may be unavailable.
968
+ // if pullerResolvedTs - checkpointTs > checkpointInterval, data is not synced
969
+ // otherwise, if pd is unavailable, we decide data whether is synced based on
970
+ // the time difference between current time and lastSyncedTs.
971
+ var message string
972
+ if (oracle .ExtractPhysical (status .PullerResolvedTs ) - oracle .ExtractPhysical (status .CheckpointTs )) >
973
+ cfg .ReplicaConfig .SyncedStatus .CheckpointInterval * 1000 {
974
+ message = fmt .Sprintf ("%s. Besides the data is not finish syncing" , err .Error ())
975
+ } else {
976
+ message = fmt .Sprintf ("%s. You should check the pd status first. If pd status is normal, means we don't finish sync data. " +
977
+ "If pd is offline, please check whether we satisfy the condition that " +
978
+ "the time difference from lastSyncedTs to the current time from the time zone of pd is greater than %v secs. " +
979
+ "If it's satisfied, means the data syncing is totally finished" , err , cfg .ReplicaConfig .SyncedStatus .SyncedCheckInterval )
980
+ }
981
+ c .JSON (http .StatusOK , SyncedStatus {
982
+ Synced : false ,
983
+ SinkCheckpointTs : model .JSONTime (oracle .GetTimeFromTS (status .CheckpointTs )),
984
+ PullerResolvedTs : model .JSONTime (oracle .GetTimeFromTS (status .PullerResolvedTs )),
985
+ LastSyncedTs : model .JSONTime (oracle .GetTimeFromTS (status .LastSyncedTs )),
986
+ NowTs : model .JSONTime (time .Unix (0 , 0 )),
987
+ Info : message ,
988
+ })
989
+ return
959
990
}
960
- c .JSON (http .StatusOK , SyncedStatus {
961
- Synced : false ,
962
- SinkCheckpointTs : model .JSONTime (oracle .GetTimeFromTS (status .CheckpointTs )),
963
- PullerResolvedTs : model .JSONTime (oracle .GetTimeFromTS (status .PullerResolvedTs )),
964
- LastSyncedTs : model .JSONTime (oracle .GetTimeFromTS (status .LastSyncedTs )),
965
- NowTs : model .JSONTime (time .Unix (0 , 0 )),
966
- Info : message ,
967
- })
968
- return
991
+ defer pdClient .Close ()
969
992
}
970
- defer pdClient .Close ()
971
993
// get time from pd
972
994
physicalNow , _ , _ := pdClient .GetTS (ctx )
973
995
@@ -1087,12 +1109,3 @@ func getCaptureDefaultUpstream(cp capture.Capture) (*upstream.Upstream, error) {
1087
1109
}
1088
1110
return up , nil
1089
1111
}
1090
-
1091
- func getUpstreamPDConfig (up * upstream.Upstream ) PDConfig {
1092
- return PDConfig {
1093
- PDAddrs : up .PdEndpoints ,
1094
- KeyPath : up .SecurityConfig .KeyPath ,
1095
- CAPath : up .SecurityConfig .CAPath ,
1096
- CertPath : up .SecurityConfig .CertPath ,
1097
- }
1098
- }
0 commit comments