22
22
23
23
from .leaderelectionrecord import LeaderElectionRecord
24
24
25
- logging .basicConfig (level = logging .INFO )
26
25
27
26
"""
28
27
This package implements leader election using an annotation in a Kubernetes
@@ -54,9 +53,7 @@ async def run(self):
54
53
# Try to create/ acquire a lock
55
54
if await self .acquire ():
56
55
logging .info (
57
- "{} successfully acquired lease" .format (
58
- self .election_config .lock .identity
59
- )
56
+ "%s successfully acquired lease" , self .election_config .lock .identity
60
57
)
61
58
62
59
task = asyncio .create_task (self .election_config .onstarted_leading )
@@ -76,7 +73,7 @@ async def run(self):
76
73
77
74
async def acquire (self ):
78
75
# Follower
79
- logging .info ("{} is a follower" . format ( self .election_config .lock .identity ) )
76
+ logging .info ("%s is a follower" , self .election_config .lock .identity )
80
77
retry_period = self .election_config .retry_period
81
78
82
79
while True :
@@ -135,16 +132,14 @@ async def try_acquire_or_renew(self):
135
132
if not lock_status :
136
133
if json .loads (old_election_record .body )["code" ] != HTTPStatus .NOT_FOUND :
137
134
logging .info (
138
- "Error retrieving resource lock {} as {}" .format (
139
- self .election_config .lock .name , old_election_record .reason
140
- )
135
+ "Error retrieving resource lock %s as %s" ,
136
+ self .election_config .lock .name , old_election_record .reason ,
141
137
)
142
138
return False
143
139
144
140
logging .info (
145
- "{} is trying to create a lock" .format (
146
- leader_election_record .holder_identity
147
- )
141
+ "%s is trying to create a lock" ,
142
+ leader_election_record .holder_identity ,
148
143
)
149
144
create_status = await self .election_config .lock .create (
150
145
name = self .election_config .lock .name ,
@@ -154,9 +149,7 @@ async def try_acquire_or_renew(self):
154
149
155
150
if not create_status :
156
151
logging .info (
157
- "{} Failed to create lock" .format (
158
- leader_election_record .holder_identity
159
- )
152
+ "%s Failed to create lock" , leader_election_record .holder_identity
160
153
)
161
154
return False
162
155
@@ -186,7 +179,7 @@ async def try_acquire_or_renew(self):
186
179
!= old_election_record .holder_identity
187
180
):
188
181
logging .info (
189
- "Leader has switched to {}" . format ( old_election_record .holder_identity )
182
+ "Leader has switched to %s" , old_election_record .holder_identity
190
183
)
191
184
192
185
if (
@@ -204,9 +197,8 @@ async def try_acquire_or_renew(self):
204
197
> int (now_timestamp * 1000 )
205
198
):
206
199
logging .info (
207
- "yet to finish lease_duration, lease held by {} and has not expired" .format (
208
- old_election_record .holder_identity
209
- )
200
+ "yet to finish lease_duration, lease held by %s and has not expired" ,
201
+ old_election_record .holder_identity ,
210
202
)
211
203
return False
212
204
@@ -227,17 +219,14 @@ async def update_lock(self, leader_election_record):
227
219
228
220
if not update_status :
229
221
logging .info (
230
- "{} failed to acquire lease" .format (
231
- leader_election_record .holder_identity
232
- )
222
+ "%s failed to acquire lease" , leader_election_record .holder_identity
233
223
)
234
224
return False
235
225
236
226
self .observed_record = leader_election_record
237
227
self .observed_time_milliseconds = int (time .time () * 1000 )
238
228
logging .info (
239
- "leader {} has successfully acquired lease" .format (
240
- leader_election_record .holder_identity
241
- )
229
+ "leader %s has successfully acquired lease" ,
230
+ leader_election_record .holder_identity ,
242
231
)
243
232
return True
0 commit comments