ECS instance system events are scheduled or unexpected events that affect the running status of an instance. The best approach to ensure stable running of services on Elastic Compute Service (ECS) instances is to automatically process the events through a program. The problem, however, is that the program can be difficult to test. System events are generated in specific scenarios and therefore may fail to be manually triggered. This makes testing the system event processing program quite tricky. ECS instances can run quite stably for months without any errors. We need to devise a new way to test the system event processing program.
To test the system event processing program, ECS provides the open APIs CreateSimulatedSystemEvents and CancelSimulatedSystemEvents to create and cancel simulated system events.
What are simulated system events? Simulated system events are specifically created for testing the system event processing program. After configuring a simulated system event, you can view the data of the event (which is just the same as that of a real event) through various event consumption channels such as open APIs, consoles, and CloudMonitor.
Apart from generating event data, a simulated event also simulates changes throughout the lifecycle.
The following figure shows the lifecycle of a simulated event.
The following describes how to create two simulated SystemMaintenance.Reboot events and then cancel one of them.
The output of code execution is as follows:
# coding=utf-8
# make sure the sdk version is 4.10.0 or upper, you can use command 'pip show aliyun-python-sdk-ecs' to check
# if the python sdk is not installed, use 'sudo pip install aliyun-python-sdk-ecs'
# if the python sdk is installed, use 'sudo pip install --upgrade aliyun-python-sdk-ecs'
import json
import logging
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526.CancelSimulatedSystemEventsRequest import CancelSimulatedSystemEventsRequest
from aliyunsdkecs.request.v20140526.CreateSimulatedSystemEventsRequest import CreateSimulatedSystemEventsRequest
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S')
# your access key Id
ak_id = "YOU_ACCESS_KEY_ID"
# your access key secret
ak_secret = "YOU_ACCESS_SECRET"
region_id = "cn-beijing"
client = client.AcsClient(ak_id, ak_secret, region_id)
# send open api request
def _send_request(request):
request.set_accept_format('json')
try:
response_str = client.do_action_with_exception(request)
logging.info(response_str)
response_detail = json.loads(response_str)
return response_detail
except Exception as e:
logging.error(e)
def build_create_request(event_type, not_before, instance_ids):
request = CreateSimulatedSystemEventsRequest()
request.set_EventType(event_type)
request.set_NotBefore(not_before)
request.set_InstanceIds(instance_ids)
return request
def print_created_event_id(response):
error_code = response.get('Code')
if error_code is None:
event_id_list = response.get('EventIdSet').get('EventId')
print("Created %s simulated events: %s", len(event_id_list), event_id_list)
else:
print("Creating failed, error code: %s", error_code)
def get_created_event_id(response):
error_code = response.get('Code')
if error_code is None:
event_id_list = response.get('EventIdSet').get('EventId')
return event_id_list
else:
return []
def build_cancel_request(event_id):
request = CancelSimulatedSystemEventsRequest()
request.set_EventIds([event_id])
return request
if __name__ == '__main__':
request = build_create_request("SystemMaintenance.Reboot", "2018-09-01T00:00:00Z", ["i-2zegswzznxbp168sc5c9", "i-2zeimxypwhnj04sbgf5t"])
response = _send_request(request)
event_ids = get_created_event_id(response)
if event_ids:
print("Created %s simulated events: %s"%(len(event_ids), event_ids))
cancel_event_id = event_ids[0]
print("Now we cancel one event %s" % (cancel_event_id))
cancel_request = build_cancel_request(cancel_event_id)
cancel_response = _send_request(cancel_request)
if not cancel_response.get('Code'):
print("Cancel succeeded")
Output after code execution:
Created 2 simulated events: [u'e-2zec65b85gi9zwcv1kpz', u'e-2zec65b85gi9zwcv1kq0']
Wed, 22 Aug 2018 18:39:49 simulate_system_event.py[line:35] INFO {"EventIdSet":{"EventId":["e-2zec65b85gi9zwcv1kpz","e-2zec65b85gi9zwcv1kq0"]},"RequestId":"C1762464-CCC2-46EC-B233-92A4D9C1782C"}
Now we cancel one event e-2zec65b85gi9zwcv1kpz
Cancel succeeded
Wed, 22 Aug 2018 18:39:49 simulate_system_event.py[line:35] INFO {"RequestId":"44286901-1BC3-4BA0-AAAF-C3CF20578E0F"}
9 posts | 0 followers
FollowAlibaba Clouder - November 20, 2018
Alibaba Clouder - October 25, 2018
Alibaba Clouder - February 4, 2019
Alibaba Clouder - July 1, 2018
Alibaba BlockChain Service Team - August 29, 2018
Alibaba Developer - December 17, 2018
9 posts | 0 followers
FollowAn online computing service that offers elastic and secure virtual cloud servers to cater all your cloud hosting needs.
Learn MorePowerful parallel computing capabilities based on GPU technology.
Learn MoreResource management and task scheduling for large-scale batch processing
Learn MoreSuper Computing Service provides ultimate computing performance and parallel computing cluster services for high-performance computing through high-speed RDMA network and heterogeneous accelerators such as GPU.
Learn MoreMore Posts by Alibaba Cloud ECS