@@ -32,11 +32,17 @@ var (
3232 errETCDCAFailedToAppend = errors .New ("failed to append etcd CA certificate" )
3333)
3434
35+ const (
36+ etcdDefaultCallTimeout = 10 * time .Second
37+ etcdDefragCallTimeout = 5 * time .Minute
38+ )
39+
3540type EtcdClient interface {
3641 GetStatuses (ctx context.Context ) (map [string ]* clientv3.StatusResponse , error )
3742 CreateSnapshot (ctx context.Context ) (* clientv3.SnapshotResponse , error )
3843 ListAlarms (ctx context.Context ) (* clientv3.AlarmResponse , error )
3944 DisarmAlarm (ctx context.Context , alarm * clientv3.AlarmMember ) error
45+ Defragment (ctx context.Context ) error
4046}
4147
4248type etcdClient struct {
@@ -104,6 +110,7 @@ func (e *etcdClient) GetStatuses(ctx context.Context) (map[string]*clientv3.Stat
104110 e .clientCertificate ,
105111 e .endpoints ,
106112 e .serverPort ,
113+ etcdDefaultCallTimeout ,
107114 clientv3 .Client .Status ,
108115 )
109116 },
@@ -140,6 +147,24 @@ func (e *etcdClient) ListAlarms(ctx context.Context) (*clientv3.AlarmResponse, e
140147 )
141148}
142149
150+ func (e * etcdClient ) Defragment (ctx context.Context ) error {
151+ return tracing .WithSpan1 (ctx , tracer , "EtcdClient.Defragment" ,
152+ func (ctx context.Context , span trace.Span ) error {
153+ _ , err := callETCDFuncOnAllMembers (
154+ ctx ,
155+ e .tracerProvider ,
156+ e .caPool ,
157+ e .clientCertificate ,
158+ e .endpoints ,
159+ e .serverPort ,
160+ etcdDefragCallTimeout ,
161+ clientv3 .Client .Defragment ,
162+ )
163+ return err
164+ },
165+ )
166+ }
167+
143168func (e * etcdClient ) DisarmAlarm (ctx context.Context , alarm * clientv3.AlarmMember ) error {
144169 return tracing .WithSpan1 (ctx , tracer , "EtcdClient.DisarmAlarm" ,
145170 func (ctx context.Context , span trace.Span ) error {
@@ -172,6 +197,7 @@ func callETCDFuncOnAllMembers[R any](
172197 clientCertificate tls.Certificate ,
173198 endpoints map [string ]string ,
174199 etcdServerPort int32 ,
200+ callTimeout time.Duration ,
175201 etcdFunc func (client clientv3.Client , ctx context.Context , endpoint string ) (* R , error ),
176202) (map [string ]* R , error ) {
177203 return tracing .WithSpan (ctx , tracer , "CallETCDFuncOnAllMembers" ,
@@ -182,7 +208,15 @@ func callETCDFuncOnAllMembers[R any](
182208 results := make (map [string ]* R , len (endpointMap ))
183209 var errs error
184210 for _ , endpoint := range endpointMap {
185- result , err := callETCDFuncOnMember (ctx , tracerProvider , endpoint , caPool , clientCertificate , etcdFunc )
211+ result , err := callETCDFuncOnMember (
212+ ctx ,
213+ tracerProvider ,
214+ endpoint ,
215+ caPool ,
216+ clientCertificate ,
217+ callTimeout ,
218+ etcdFunc ,
219+ )
186220 errs = errors .Join (errs , err )
187221 results [endpoint ] = result
188222 }
@@ -238,6 +272,7 @@ func callETCDFuncOnMember[R any](
238272 endpoint string ,
239273 caPool * x509.CertPool ,
240274 clientCertificate tls.Certificate ,
275+ callTimeout time.Duration ,
241276 etcdFunc func (client clientv3.Client , ctx context.Context , endpoint string ) (R , error ),
242277) (R , error ) {
243278 return tracing .WithSpan (ctx , tracer , "CallETCDFuncOnMember" ,
@@ -279,7 +314,7 @@ func callETCDFuncOnMember[R any](
279314
280315 var result R
281316 _ , _ , err = slices .AttemptWithDelay (4 , 5 * time .Second , func (_ int , _ time.Duration ) (err error ) {
282- ctx , cancel := context .WithTimeout (ctx , 10 * time . Second )
317+ ctx , cancel := context .WithTimeout (ctx , callTimeout )
283318 defer cancel ()
284319 result , err = etcdFunc (* etcdClient , ctx , endpoint )
285320 return err
@@ -300,7 +335,13 @@ func callETCDFuncOnAnyMember[R any](
300335) (R , error ) {
301336 return tracing .WithSpan (ctx , tracer , "CallETCDFuncOnAnyMember" ,
302337 func (ctx context.Context , span trace.Span ) (R , error ) {
303- return callETCDFuncOnMember (ctx , tracerProvider , endpoint , caPool , clientCertificate ,
338+ return callETCDFuncOnMember (
339+ ctx ,
340+ tracerProvider ,
341+ endpoint ,
342+ caPool ,
343+ clientCertificate ,
344+ etcdDefaultCallTimeout ,
304345 func (client clientv3.Client , ctx context.Context , _ string ) (R , error ) {
305346 return etcdFunc (client , ctx )
306347 },
0 commit comments