-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreshold.py
111 lines (87 loc) · 3.95 KB
/
threshold.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'''
Created on 16/07/2014
Threshold Checkpointing algorithm: checkpoints are taken when a rising in the
price of the spot instance is observed inside the interval. The interval is
a % of the price the user has determined.
ckptman - Checkpointing Manager for BLCR and spot instances
2014 - GRyCAP - Universitat Politecnica de Valencia
@author: Amanda
'''
import boto.ec2
import logging
import time
import datetime
import os
import calendar
from iso8601 import parse_date
from im_connector import *
from config import *
HOUR_DURATION = 3600
# list that contains the last historic price of the spot instances
historical_price = [0,0]
# Converts the amazon timestamp (ISO 8601) into unix format
def iso2unix(timestamp):
# use iso8601.parse_date to convert the timestamp into a datetime object.
parsed = parse_date(timestamp)
# now grab a time tuple that we can feed mktime
timetuple = parsed.timetuple()
return calendar.timegm(timetuple)
# Determine if it's time to perform a checkpoint
def is_checkpoint_time(launch_time, hostname):
checkpoint = False
#############################################################################
# First we check if an hour has passed away since the creation of the node #
#############################################################################
launched = datetime.datetime.fromtimestamp(int(launch_time)).strftime('%Y-%m-%d %H:%M:%S')
logging.info("THRESHOLD: Time node " + hostname + " was launched is " + launched)
# Obtain actual time
now = time.strftime("%H:%M:%S")
logging.info("THRESHOLD: Actual time is " + now)
actual_time = int(time.time())
live_time = actual_time - launch_time
# Check if it's time to make a checkpoint
remaining_hour_time = HOUR_DURATION - live_time % HOUR_DURATION
logging.debug("THRESHOLD: Remaining hour time = %d for node %s" % (int(remaining_hour_time), hostname))
if int(remaining_hour_time) < int(CKPT_TIME_MARGIN):
checkpoint = True
return checkpoint
#############################################################################
# Second we check if there are recent variations in the spot price market #
#############################################################################
region = get_region(hostname)
logging.info("THRESHOLD: The region used to connect with AWS is " + region)
instance_type = get_instance_type(hostname)
logging.info("THRESHOLD: The instance type to check the spot prices is " + instance_type)
availability_zone= get_availability_zone(hostname)
logging.info("THRESHOLD: The availability zone to check the spot prices is " + availability_zone)
auth_data = get_credentials()
if auth_data:
for auth in auth_data:
if auth["type"]=='EC2':
access_key = auth["username"]
secret_key= auth["password"]
else:
logging.error("THRESHOLD: Error obtaining user credentials for EC2")
bid = get_user_spot_bid(hostname)
logging.info("THRESHOLD: The user's bid is " + str(bid))
# Calculate the interval values
res = bid * THRESHOLD/100
limit = bid - res
os.environ['AWS_ACCESS_KEY_ID'] = access_key
os.environ['AWS_SECRET_ACCESS_KEY'] = secret_key
# Create the EC2 connection
ec2 = boto.ec2.connect_to_region(region)
history = ec2.get_spot_price_history(instance_type=instance_type, availability_zone=availability_zone, max_results=1)
if history:
logging.debug("THRESHOLD: Current spot price for the availability zone " + availability_zone + " : " + str(history[0].price) + " at " + str(history[0].timestamp))
if(historical_price[0] != 0 and historical_price[1] != 0):
if historical_price[0] != str(iso2unix(history[0].timestamp)):
if history[0].price > float(historical_price[1]):
if history[0].price > limit:
logging.info("THRESHOLD: A variation in the spot price inside the interval has been detected")
checkpoint = True
historical_price[0] = iso2unix(history[0].timestamp)
historical_price[1] = history[0].price
else:
logging.error("THRESHOLD: Cannot get the current spot price from Amazon")
return checkpoint