9
9
"github.com/google/uuid"
10
10
libcluster "github.com/tarantool/tt/lib/cluster"
11
11
"github.com/tarantool/tt/lib/connect"
12
- "go.etcd.io/etcd/api/v3/mvccpb"
13
- clientv3 "go.etcd.io/etcd/client/v3"
14
12
"gopkg.in/yaml.v2"
15
13
)
16
14
@@ -63,36 +61,22 @@ type SwitchStatusCtx struct {
63
61
TaskID string
64
62
}
65
63
66
- func makeEtcdOpts (uriOpts connect.UriOpts ) libcluster.EtcdOpts {
67
- opts := libcluster.EtcdOpts {
68
- Endpoints : []string {uriOpts .Endpoint },
69
- Username : uriOpts .Username ,
70
- Password : uriOpts .Password ,
71
- KeyFile : uriOpts .KeyFile ,
72
- CertFile : uriOpts .CertFile ,
73
- CaPath : uriOpts .CaPath ,
74
- CaFile : uriOpts .CaFile ,
75
- SkipHostVerify : uriOpts .SkipHostVerify ,
76
- Timeout : uriOpts .Timeout ,
77
- }
78
-
79
- return opts
80
- }
81
-
82
64
// Switch master instance.
83
65
func Switch (url string , switchCtx SwitchCtx ) error {
84
66
uriOpts , err := connect .CreateUriOpts (url )
85
67
if err != nil {
86
68
return fmt .Errorf ("invalid URL %q: %w" , url , err )
87
69
}
70
+ connOpts := libcluster.ConnectOpts {
71
+ Username : switchCtx .Username ,
72
+ Password : switchCtx .Password ,
73
+ }
88
74
89
- opts := makeEtcdOpts (uriOpts )
90
-
91
- etcd , err := libcluster .ConnectEtcd (opts )
75
+ conn , err := libcluster .ConnectCStorage (uriOpts , connOpts )
92
76
if err != nil {
93
- return fmt .Errorf ("unable to connect to etcd : %w" , err )
77
+ return fmt .Errorf ("unable to connect to config storage : %w" , err )
94
78
}
95
- defer etcd .Close ()
79
+ defer conn .Close ()
96
80
97
81
cmd := switchCmd {
98
82
Command : "switch" ,
@@ -109,54 +93,40 @@ func Switch(url string, switchCtx SwitchCtx) error {
109
93
key := uriOpts .Prefix + failoverPath + uuid
110
94
111
95
if switchCtx .Wait {
112
- ctx , cancel_watch := context .WithTimeout (context .Background (),
96
+ ctxWatch , cancelWatch := context .WithTimeout (context .Background (),
113
97
time .Duration (switchCtx .Timeout )* time .Second + cmdAdditionalWait )
114
- outputChan := make (chan * clientv3.Event )
115
- defer cancel_watch ()
116
-
117
- go func () {
118
- waitChan := etcd .Watch (ctx , key )
119
- defer close (outputChan )
120
-
121
- for resp := range waitChan {
122
- for _ , ev := range resp .Events {
123
- switch ev .Type {
124
- case mvccpb .PUT :
125
- outputChan <- ev
126
- }
127
- }
128
- }
129
- }()
98
+ defer cancelWatch ()
99
+ watchChan := conn .Watch (ctxWatch , key )
130
100
131
- ctx_put , cancel := context .WithTimeout (context .Background (), defaultEtcdTimeout )
132
- _ , err = etcd .Put (ctx_put , key , string (yamlCmd ))
101
+ ctx , cancel := context .WithTimeout (context .Background (), defaultEtcdTimeout )
102
+ err = conn .Put (ctx , key , string (yamlCmd ))
133
103
cancel ()
134
104
135
105
if err != nil {
136
106
return err
137
107
}
138
108
139
- for ev := range outputChan {
140
- result := switchCmdResult {}
141
- err = yaml .Unmarshal (ev .Kv . Value , & result )
109
+ for ev := range watchChan {
110
+ var result switchCmdResult
111
+ err = yaml .Unmarshal (ev .Value , & result )
142
112
if err != nil {
143
113
return err
144
114
}
145
- fmt .Printf ("%s" , ev .Kv . Value )
115
+ fmt .Printf ("%s" , ev .Value )
146
116
if result .Status == "success" || result .Status == "failed" {
147
117
return nil
148
118
}
149
119
}
150
- if ctx .Err () == context .DeadlineExceeded {
120
+ if ctxWatch .Err () == context .DeadlineExceeded {
151
121
log .Info ("Timeout for command execution reached." )
152
122
return nil
153
123
}
154
124
155
- return ctx . Err ()
125
+ return fmt . Errorf ( "unexpected problem with watch context: %w" , ctxWatch . Err () )
156
126
}
157
127
158
128
ctx , cancel := context .WithTimeout (context .Background (), defaultEtcdTimeout )
159
- _ , err = etcd .Put (ctx , key , string (yamlCmd ))
129
+ err = conn .Put (ctx , key , string (yamlCmd ))
160
130
cancel ()
161
131
162
132
if err != nil {
@@ -177,30 +147,28 @@ func SwitchStatus(url string, switchCtx SwitchStatusCtx) error {
177
147
if err != nil {
178
148
return fmt .Errorf ("invalid URL %q: %w" , url , err )
179
149
}
180
-
181
- opts := makeEtcdOpts (uriOpts )
182
-
183
- etcd , err := libcluster .ConnectEtcd (opts )
150
+ var connOpts libcluster.ConnectOpts
151
+ conn , err := libcluster .ConnectCStorage (uriOpts , connOpts )
184
152
if err != nil {
185
- return fmt .Errorf ("unable to connect to etcd : %w" , err )
153
+ return fmt .Errorf ("unable to connect to config storage : %w" , err )
186
154
}
187
- defer etcd .Close ()
155
+ defer conn .Close ()
188
156
189
157
key := uriOpts .Prefix + failoverPath + switchCtx .TaskID
190
158
191
159
ctx , cancel := context .WithTimeout (context .Background (), defaultEtcdTimeout )
192
- result , err := etcd .Get (ctx , key , clientv3 . WithLimit ( 1 ) )
160
+ result , err := conn .Get (ctx , key )
193
161
cancel ()
194
162
195
163
if err != nil {
196
164
return err
197
165
}
198
166
199
- if len (result . Kvs ) != 1 {
167
+ if len (result ) != 1 {
200
168
return fmt .Errorf ("task with id `%s` is not found" , switchCtx .TaskID )
201
169
}
202
170
203
- fmt .Print (string (result . Kvs [0 ].Value ))
171
+ fmt .Print (string (result [0 ].Value ))
204
172
205
173
return nil
206
174
}
0 commit comments