Skip to content

Commit 33e6551

Browse files
author
Marcelo Rodrigues Da Silva Soares
committed
Merge branch 'dev' into 'master'
Dev > Master See merge request dbdev/dbaas!9
2 parents c59dce8 + 573ad1b commit 33e6551

File tree

9 files changed

+171
-28
lines changed

9 files changed

+171
-28
lines changed

dbaas/api/task.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ class TaskAPI(viewsets.ReadOnlyModelViewSet):
160160
'maintenance.tasks.restart_database',
161161
'notification.tasks.change_database_persistence',
162162
'maintenance.tasks.task_upgrade_disk_type',
163+
'maintenance.tasks.auto_upgrade_database_vm_offering',
163164
]
164165

165166
model = TaskHistory

dbaas/drivers/base.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ def is_replication_ok(self, instance):
253253
def switch_master(self, instance=None, preferred_slave_instance=None):
254254
raise NotImplementedError()
255255

256+
def switch_master_with_stepdowntime(self, instance=None, preferred_slave_instance=None, stepdown_time=60):
257+
raise NotImplementedError()
258+
256259
def get_database_instances(self, ):
257260
driver_name = self.name.upper()
258261
instances = [instance
@@ -369,6 +372,35 @@ def check_replication_and_switch(self, instance, attempts=100,
369372
"Could not switch master because of replication's delay"
370373
)
371374

375+
def check_replication_and_switch_with_stepdown_time(self, instance, attempts=100,
376+
check_is_master_attempts=5,
377+
preferred_slave_instance=None,
378+
stepdown_time=60):
379+
LOG.info("Check Replication with StepDown time of %s seconds", stepdown_time)
380+
from time import sleep
381+
for attempt in range(0, attempts):
382+
if self.is_replication_ok(instance):
383+
self.switch_master_with_stepdowntime(instance, preferred_slave_instance, stepdown_time)
384+
LOG.info("Switch master returned ok...")
385+
386+
check_is_master_attempts_count = check_is_master_attempts
387+
while self.check_instance_is_master(instance,
388+
default_timeout=False):
389+
if check_is_master_attempts_count == 0:
390+
break
391+
check_is_master_attempts_count -= 1
392+
sleep(10)
393+
else:
394+
return
395+
396+
raise Exception("Could not change master")
397+
398+
LOG.info("Waiting 10s to check replication...")
399+
sleep(10)
400+
raise Exception(
401+
"Could not switch master because of replication's delay"
402+
)
403+
372404
def get_database_agents(self):
373405
""" Returns database agents list"""
374406
raise NotImplementedError()

dbaas/drivers/mongodb.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,16 @@ def switch_master(self, instance=None, preferred_slave_instance=None):
546546
except pymongo.errors.AutoReconnect:
547547
pass
548548

549+
def switch_master_with_stepdowntime(self, instance=None, preferred_slave_instance=None, stepdown_time=60):
550+
client = self.get_client(None)
551+
try:
552+
client.admin.command(
553+
'replSetStepDown', stepdown_time,
554+
secondaryCatchUpPeriodSecs=60
555+
)
556+
except pymongo.errors.AutoReconnect:
557+
pass
558+
549559
def get_database_agents(self):
550560
return []
551561

dbaas/maintenance/task_auto_upgrade_vm_offering.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from physical.models import (Plan, DatabaseInfra, Instance, Pool)
77
from util.providers import get_auto_upgrade_vm_settings
88
from workflow.workflow import steps_for_instances
9-
from util import get_vm_name
9+
from util import get_vm_name, email_notifications
1010

1111
LOG = logging.getLogger(__name__)
1212

@@ -72,6 +72,8 @@ def task_auto_upgrade_vm_offering(database, task, retry_from=None, resize_target
7272

7373
if create_temporary_instance: # se precisar criar nova temporary instance, cria
7474
LOG.info("Creating temporary instance")
75+
email_notifications.upgrade_offering_notification(database, resize_target)
76+
7577
for i in range(number_of_instances):
7678
instance = None
7779
last_vm_created += 1
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<h2>Automatic GCP offering upgrade for Database {{ database.name }}</h2>
2+
3+
We would like to inform you that an automatic offer update is being carried out on DBaaS to meet your database <a href="{{database_url}}">{{database.name}}</a> needs.
4+
<br><br>
5+
The update will be done automatically from {{ current_offering.name }} to {{ future_offering.name }}.
6+
<br><br>
7+
Please be aware that this upgrade may increase the costs.
8+
Any questions please contact the DBDevops responsible for your team.
9+
<br><br>
10+
11+
{% if database.team.email %}
12+
You are receiving this email because in our records you are in team {{ database.team.name }}.<br>
13+
If this is not right, contact the DBaaS system administrators.
14+
{% else %}
15+
<h3>Team {{ database.team.name }} has no email set!</h3>
16+
{% endif %}
17+
<br><br><br>
18+
Regards,<br>
19+
DBaaS notification robot<br>
20+
{{domain}}<br>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Automatic GCP offering upgrade for Database {{ database.name }}
2+
3+
We would like to inform you that an automatic offer update is being carried out on DBaaS to meet your database {{database.name}} needs.
4+
5+
The update will be done automatically from {{ current_offering.name }} to {{ future_offering.name }}.
6+
7+
Please be aware that this upgrade may increase the costs.
8+
Any questions please contact the DBDevops responsible for your team.
9+
10+
{% if database.team.email %}
11+
You are receiving this email because in our records you are in team {{ database.team.name }}.<br>
12+
If this is not right, contact the DBaaS system administrators.
13+
{% else %}
14+
Team {{ database.team.name }} has no email set!
15+
{% endif %}
16+
17+
Regards,
18+
DBaaS notification robot
19+
{{domain}}

dbaas/util/email_notifications.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,30 @@ def disk_resize_notification(database, new_disk, usage_percentage):
157157
)
158158

159159

160-
def schedule_task_notification(database, scheduled_task, is_new,
161-
is_task_warning=False):
160+
def upgrade_offering_notification(database, resize_target):
161+
LOG.info('Notifying auto upgrade offering to database: {}'.format(database))
162+
163+
current_offering = database.databaseinfra.offering
164+
future_offering = database.get_future_offering(resize_target)
165+
166+
subject = _('[DBaaS] Database {} auto upgrade offering to {}').format(database, future_offering.name)
167+
template = "auto_upgrade_offering_notification"
168+
169+
context = {
170+
'domain': get_domain(),
171+
'database': database,
172+
'current_offering': current_offering,
173+
'future_offering': future_offering,
174+
'database_url': get_database_url(database.id)
175+
}
176+
177+
send_mail_template(
178+
subject, template, email_from(), email_to(database.team),
179+
fail_silently=False, attachments=None, context=context
180+
)
181+
182+
183+
def schedule_task_notification(database, scheduled_task, is_new, is_task_warning=False):
162184

163185
subject_tmpl = '[DBaaS] Automatic Task {} for Database {}'
164186
if is_task_warning:

dbaas/workflow/steps/util/vm.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
import logging
23
from time import sleep
34
from dbaas_credentials.models import CredentialType
45
from util import get_credentials_for
@@ -7,6 +8,8 @@
78
CHANGE_MASTER_ATTEMPS = 30
89
CHANGE_MASTER_SECONDS = 15
910

11+
LOG = logging.getLogger(__name__)
12+
1013

1114
class HostStatus(object):
1215
@staticmethod
@@ -159,33 +162,47 @@ class ChangeMasterTemporaryInstance(ChangeMaster):
159162

160163
@property
161164
def is_valid(self):
162-
if self.instance.temporary or self.check_master_is_temporary():
165+
master_temporary = self.check_master_is_temporary()
166+
# so executa para a VM tepmoraria, e se a Master nao eh temporaria
167+
if not self.instance.temporary or master_temporary:
163168
return False
164-
return super(ChangeMasterTemporaryInstance, self).is_valid
165169

166-
def check_master_is_temporary(self):
170+
return True
171+
172+
def check_master_is_temporary(self, wait_seconds=0):
173+
LOG.info("Checking master is temporary instance")
174+
LOG.debug("Willl sleep for %s seconds before checking", wait_seconds)
175+
sleep(wait_seconds)
176+
167177
master = self.driver.get_master_instance()
168-
if master.temporary:
169-
return True
170-
return False
178+
LOG.info("Master instance is %s", master)
179+
LOG.info("Master is temporary? %s", master.temporary)
180+
181+
if master is None or not master.temporary:
182+
return False
183+
184+
return True
171185

172186
def change_master(self):
173187
error = None
174188

175189
for _ in range(CHANGE_MASTER_ATTEMPS):
176-
if self.is_slave:
177-
return
190+
error = None
178191
try:
179-
self.driver.check_replication_and_switch(self.target_instance)
180-
if not self.check_master_is_temporary():
192+
LOG.info("Trying to change master. Attempt %s", _)
193+
self.driver.check_replication_and_switch_with_stepdown_time(self.target_instance, stepdown_time=300)
194+
master_is_temporary = self.check_master_is_temporary(wait_seconds=60)
195+
196+
if not master_is_temporary:
181197
raise Exception('Master is not the temporary instance')
198+
199+
return
182200
except Exception as e:
183201
error = e
184202
sleep(CHANGE_MASTER_SECONDS)
185-
else:
186-
return
187203

188-
raise error
204+
if error is not None:
205+
raise error
189206

190207
def do(self):
191208
if not self.is_valid:
@@ -206,19 +223,18 @@ def change_master(self):
206223
error = None
207224

208225
for _ in range(CHANGE_MASTER_ATTEMPS):
209-
if self.is_slave:
210-
return
211226
try:
212227
self.driver.check_replication_and_switch(self.target_instance)
213-
if self.check_master_is_temporary():
228+
if self.check_master_is_temporary(wait_seconds=60):
214229
raise Exception('Master is the temporary instance')
230+
231+
return
215232
except Exception as e:
216233
error = e
217234
sleep(CHANGE_MASTER_SECONDS)
218-
else:
219-
return
220235

221-
raise error
236+
if error is not None:
237+
raise error
222238

223239

224240
class ChangeMasterDatabaseMigrate(ChangeMaster):

dbaas/workflow/steps/util/zabbix.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,22 @@ def __unicode__(self):
334334
def do(self):
335335
if not self.is_valid:
336336
return
337-
self.zabbix_provider.disable_alarms()
337+
# Try and Exception to fix error Session terminated
338+
try:
339+
self.zabbix_provider.disable_alarms()
340+
except:
341+
self.provider_write = None
342+
self.zabbix_provider.disable_alarms()
338343

339344
def undo(self):
340345
if not self.is_valid:
341346
return
342-
self.zabbix_provider.enable_alarms()
347+
# Try and Exception to fix error Session terminated
348+
try:
349+
self.zabbix_provider.enable_alarms()
350+
except:
351+
self.provider_write = None
352+
self.zabbix_provider.enable_alarms()
343353

344354

345355
class EnableAlarms(ZabbixStep):
@@ -354,11 +364,22 @@ def is_valid(self):
354364
def do(self):
355365
if not self.is_valid:
356366
return
357-
358-
self.zabbix_provider.enable_alarms()
367+
# Try and Exception to fix error Session terminated
368+
try:
369+
self.zabbix_provider.enable_alarms()
370+
except:
371+
self.provider_write = None
372+
self.zabbix_provider.enable_alarms()
359373

360374
def undo(self):
361-
self.zabbix_provider.disable_alarms()
375+
if not self.is_valid:
376+
return
377+
# Try and Exception to fix error Session terminated
378+
try:
379+
self.zabbix_provider.disable_alarms()
380+
except:
381+
self.provider_write = None
382+
self.zabbix_provider.disable_alarms()
362383

363384

364385
class UpdateMonitoring(ZabbixStep):
@@ -422,4 +443,4 @@ def do(self):
422443
host_name=self.instance.dns,
423444
macro='{$MONGO_SSL}',
424445
value=value
425-
)
446+
)

0 commit comments

Comments
 (0)