Skip to content

Commit a388ed7

Browse files
author
kishan
committed
Bug 13326: Added is_elastic flag to IP address usage. Added new column in usage_ip_address, defaults to false. size column will contain is_elastic info in cloud_usage table
Status 13326: resolved fixed Reviewed-By: Nitin
1 parent b70f740 commit a388ed7

10 files changed

Lines changed: 65 additions & 33 deletions

File tree

core/src/com/cloud/event/UsageEventVO.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,16 @@ public UsageEventVO(String usageType, long accountId, long zoneId, long resource
9393
this.resourceName = resourceName;
9494
}
9595

96-
public UsageEventVO(String usageType, long accountId, long zoneId, long resourceId, String resourceName, long size, String resourceType) {
96+
//IPAddress usage event
97+
public UsageEventVO(String usageType, long accountId, long zoneId, long ipAddressId, String ipAddress, boolean isSourceNat, String guestType, boolean isElastic) {
9798
this.type = usageType;
9899
this.accountId = accountId;
99100
this.zoneId = zoneId;
100-
this.resourceId = resourceId;
101-
this.resourceName = resourceName;
102-
this.size = size;
103-
this.resourceType = resourceType;
101+
this.resourceId = ipAddressId;
102+
this.resourceName = ipAddress;
103+
this.size = (isSourceNat ? 1L : 0L);
104+
this.resourceType = guestType;
105+
this.templateId = (isElastic ? 1L : 0L);
104106
}
105107

106108
public UsageEventVO(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId, String resourceType) {

server/src/com/cloud/network/NetworkManagerImpl.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,6 @@ protected void markPublicIpAsAllocated(IPAddressVO addr) {
439439
Transaction txn = Transaction.currentTxn();
440440

441441
Account owner = _accountMgr.getAccount(addr.getAllocatedToAccountId());
442-
long isSourceNat = (addr.isSourceNat()) ? 1 : 0;
443442

444443
txn.start();
445444
addr.setState(IpAddress.State.Allocated);
@@ -450,8 +449,8 @@ protected void markPublicIpAsAllocated(IPAddressVO addr) {
450449
VlanVO vlan = _vlanDao.findById(addr.getVlanId());
451450

452451
String guestType = vlan.getVlanType().toString();
453-
454-
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_NET_IP_ASSIGN, owner.getId(), addr.getDataCenterId(), addr.getId(), addr.getAddress().toString(), isSourceNat, guestType);
452+
453+
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_NET_IP_ASSIGN, owner.getId(), addr.getDataCenterId(), addr.getId(), addr.getAddress().toString(), addr.isSourceNat(), guestType, addr.getElastic());
455454
_usageEventDao.persist(usageEvent);
456455
// don't increment resource count for direct ip addresses
457456
if (addr.getAssociatedWithNetworkId() != null) {
@@ -3722,15 +3721,13 @@ public IPAddressVO markIpAsUnavailable(long addrId) {
37223721
_resourceLimitMgr.decrementResourceCount(_ipAddressDao.findById(addrId).getAllocatedToAccountId(), ResourceType.public_ip);
37233722
}
37243723

3725-
long isSourceNat = (ip.isSourceNat()) ? 1 : 0;
3726-
37273724
// Save usage event
37283725
if (ip.getAllocatedToAccountId() != Account.ACCOUNT_ID_SYSTEM) {
37293726
VlanVO vlan = _vlanDao.findById(ip.getVlanId());
37303727

37313728
String guestType = vlan.getVlanType().toString();
37323729

3733-
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_NET_IP_RELEASE, ip.getAllocatedToAccountId(), ip.getDataCenterId(), addrId, ip.getAddress().addr(), isSourceNat, guestType);
3730+
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_NET_IP_RELEASE, ip.getAllocatedToAccountId(), ip.getDataCenterId(), addrId, ip.getAddress().addr(), ip.isSourceNat(), guestType, ip.getElastic());
37343731
_usageEventDao.persist(usageEvent);
37353732
}
37363733

server/src/com/cloud/upgrade/dao/Upgrade218to22.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,13 +1882,12 @@ private UsageEventVO convertIPEvent(EventVO event, Connection conn) throws IOExc
18821882
pstmt.close();
18831883

18841884
boolean isSourceNat = Boolean.parseBoolean(ipEventParams.getProperty("sourceNat"));
1885-
long isSourceNatLong = isSourceNat ? 1 : 0;
18861885

18871886
if (EventTypes.EVENT_NET_IP_ASSIGN.equals(event.getType())) {
18881887
zoneId = Long.parseLong(ipEventParams.getProperty("dcId"));
1889-
usageEvent = new UsageEventVO(EventTypes.EVENT_NET_IP_ASSIGN, event.getAccountId(), zoneId, ipId, ipAddress, isSourceNatLong,"");
1888+
usageEvent = new UsageEventVO(EventTypes.EVENT_NET_IP_ASSIGN, event.getAccountId(), zoneId, ipId, ipAddress, isSourceNat,"", false);
18901889
} else if (EventTypes.EVENT_NET_IP_RELEASE.equals(event.getType())) {
1891-
usageEvent = new UsageEventVO(EventTypes.EVENT_NET_IP_RELEASE, event.getAccountId(), zoneId, ipId, ipAddress, isSourceNatLong,"");
1890+
usageEvent = new UsageEventVO(EventTypes.EVENT_NET_IP_RELEASE, event.getAccountId(), zoneId, ipId, ipAddress, isSourceNat,"", false);
18921891
}
18931892
return usageEvent;
18941893
}

server/src/com/cloud/usage/UsageIPAddressVO.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public class UsageIPAddressVO {
4747

4848
@Column(name="is_source_nat")
4949
private boolean isSourceNat = false;
50-
50+
51+
@Column(name="is_elastic")
52+
private boolean isElastic = false;
53+
5154
@Column(name="assigned")
5255
@Temporal(value=TemporalType.TIMESTAMP)
5356
private Date assigned = null;
@@ -59,13 +62,14 @@ public class UsageIPAddressVO {
5962
protected UsageIPAddressVO() {
6063
}
6164

62-
public UsageIPAddressVO(long id, long accountId, long domainId, long zoneId, String address, boolean isSourceNat, Date assigned, Date released) {
65+
public UsageIPAddressVO(long id, long accountId, long domainId, long zoneId, String address, boolean isSourceNat, boolean isElastic, Date assigned, Date released) {
6366
this.id = id;
6467
this.accountId = accountId;
6568
this.domainId = domainId;
6669
this.zoneId = zoneId;
6770
this.address = address;
68-
this.isSourceNat = isSourceNat;
71+
this.isSourceNat = isSourceNat;
72+
this.isElastic = isElastic;
6973
this.assigned = assigned;
7074
this.released = released;
7175
}
@@ -99,6 +103,10 @@ public String getAddress() {
99103

100104
public boolean isSourceNat() {
101105
return isSourceNat;
106+
}
107+
108+
public boolean isElastic() {
109+
return isElastic;
102110
}
103111

104112
public Date getAssigned() {

server/src/com/cloud/usage/UsageVO.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,24 @@ public UsageVO(Long zoneId, Long accountId, Long domainId, String description, S
150150
this.startDate = startDate;
151151
this.endDate = endDate;
152152
}
153-
153+
154+
//IPAddress Usage
155+
public UsageVO(Long zoneId, Long accountId, Long domainId, String description, String usageDisplay,
156+
int usageType, Double rawUsage, Long usageId, long size, String type, Date startDate, Date endDate) {
157+
this.zoneId = zoneId;
158+
this.accountId = accountId;
159+
this.domainId = domainId;
160+
this.description = description;
161+
this.usageDisplay = usageDisplay;
162+
this.usageType = usageType;
163+
this.rawUsage = rawUsage;
164+
this.usageId = usageId;
165+
this.size = size;
166+
this.type = type;
167+
this.startDate = startDate;
168+
this.endDate = endDate;
169+
}
170+
154171
public Long getId() {
155172
return id;
156173
}

server/src/com/cloud/usage/dao/UsageIPAddressDaoImpl.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public class UsageIPAddressDaoImpl extends GenericDaoBase<UsageIPAddressVO, Long
4141
public static final Logger s_logger = Logger.getLogger(UsageIPAddressDaoImpl.class.getName());
4242

4343
protected static final String UPDATE_RELEASED = "UPDATE usage_ip_address SET released = ? WHERE account_id = ? AND public_ip_address = ? and released IS NULL";
44-
protected static final String GET_USAGE_RECORDS_BY_ACCOUNT = "SELECT id, account_id, domain_id, zone_id, public_ip_address, is_source_nat, assigned, released " +
44+
protected static final String GET_USAGE_RECORDS_BY_ACCOUNT = "SELECT id, account_id, domain_id, zone_id, public_ip_address, is_source_nat, is_elastic, assigned, released " +
4545
"FROM usage_ip_address " +
4646
"WHERE account_id = ? AND ((released IS NULL AND assigned <= ?) OR (assigned BETWEEN ? AND ?) OR " +
4747
" (released BETWEEN ? AND ?) OR ((assigned <= ?) AND (released >= ?)))";
48-
protected static final String GET_USAGE_RECORDS_BY_DOMAIN = "SELECT id, account_id, domain_id, zone_id, public_ip_address, is_source_nat, assigned, released " +
48+
protected static final String GET_USAGE_RECORDS_BY_DOMAIN = "SELECT id, account_id, domain_id, zone_id, public_ip_address, is_source_nat, is_elastic, assigned, released " +
4949
"FROM usage_ip_address " +
5050
"WHERE domain_id = ? AND ((released IS NULL AND assigned <= ?) OR (assigned BETWEEN ? AND ?) OR " +
5151
" (released BETWEEN ? AND ?) OR ((assigned <= ?) AND (released >= ?)))";
52-
protected static final String GET_ALL_USAGE_RECORDS = "SELECT id, account_id, domain_id, zone_id, public_ip_address, is_source_nat, assigned, released " +
52+
protected static final String GET_ALL_USAGE_RECORDS = "SELECT id, account_id, domain_id, zone_id, public_ip_address, is_source_nat, is_elastic, assigned, released " +
5353
"FROM usage_ip_address " +
5454
"WHERE (released IS NULL AND assigned <= ?) OR (assigned BETWEEN ? AND ?) OR " +
5555
" (released BETWEEN ? AND ?) OR ((assigned <= ?) AND (released >= ?))";
@@ -118,11 +118,12 @@ public List<UsageIPAddressVO> getUsageRecords(Long accountId, Long domainId, Dat
118118
Long dId = Long.valueOf(rs.getLong(3));
119119
Long zId = Long.valueOf(rs.getLong(4));
120120
String addr = rs.getString(5);
121-
Boolean isSourceNat = Boolean.valueOf(rs.getBoolean(6));
121+
Boolean isSourceNat = Boolean.valueOf(rs.getBoolean(6));
122+
Boolean isElastic = Boolean.valueOf(rs.getBoolean(7));
122123
Date assignedDate = null;
123124
Date releasedDate = null;
124-
String assignedTS = rs.getString(7);
125-
String releasedTS = rs.getString(8);
125+
String assignedTS = rs.getString(8);
126+
String releasedTS = rs.getString(9);
126127

127128
if (assignedTS != null) {
128129
assignedDate = DateUtil.parseDateString(s_gmtTimeZone, assignedTS);
@@ -131,7 +132,7 @@ public List<UsageIPAddressVO> getUsageRecords(Long accountId, Long domainId, Dat
131132
releasedDate = DateUtil.parseDateString(s_gmtTimeZone, releasedTS);
132133
}
133134

134-
usageRecords.add(new UsageIPAddressVO(id, acctId, dId, zId, addr, isSourceNat, assignedDate, releasedDate));
135+
usageRecords.add(new UsageIPAddressVO(id, acctId, dId, zId, addr, isSourceNat, isElastic, assignedDate, releasedDate));
135136
}
136137
} catch (Exception e) {
137138
txn.rollback();

setup/db/create-schema-premium.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ CREATE TABLE `cloud_usage`.`usage_ip_address` (
9595
`zone_id` bigint unsigned NOT NULL,
9696
`public_ip_address` varchar(15) NOT NULL,
9797
`is_source_nat` smallint(1) NOT NULL,
98+
`is_elastic` smallint(1) NOT NULL,
9899
`assigned` DATETIME NOT NULL,
99100
`released` DATETIME NULL,
100101
UNIQUE KEY (`id`, `assigned`)

setup/db/db/schema-2214to30.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ UPDATE `cloud_usage`.`usage_network` set agg_bytes_received = net_bytes_received
305305
ALTER TABLE `cloud_usage`.`usage_vpn_user` ADD INDEX `i_usage_vpn_user__account_id`(`account_id`);
306306
ALTER TABLE `cloud_usage`.`usage_vpn_user` ADD INDEX `i_usage_vpn_user__created`(`created`);
307307
ALTER TABLE `cloud_usage`.`usage_vpn_user` ADD INDEX `i_usage_vpn_user__deleted`(`deleted`);
308-
308+
ALTER TABLE `cloud_usage`.`usage_ip_address` ADD COLUMN `is_elastic` smallint(1) NOT NULL default '0';
309309

310310
DELETE FROM `cloud`.`configuration` WHERE name='host.capacity.checker.wait';
311311
DELETE FROM `cloud`.`configuration` WHERE name='host.capacity.checker.interval';

usage/src/com/cloud/usage/UsageManagerImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,8 @@ private void createIPHelperEvent(UsageEventVO event) {
10101010
long id = event.getResourceId();
10111011
long sourceNat = event.getSize();
10121012
boolean isSourceNat = (sourceNat == 1) ? true : false ;
1013-
UsageIPAddressVO ipAddressVO = new UsageIPAddressVO(id, event.getAccountId(), acct.getDomainId(), zoneId, ipAddress, isSourceNat, event.getCreateDate(), null);
1013+
boolean isElastic = (event.getTemplateId() == 1) ? true : false ;
1014+
UsageIPAddressVO ipAddressVO = new UsageIPAddressVO(id, event.getAccountId(), acct.getDomainId(), zoneId, ipAddress, isSourceNat, isElastic, event.getCreateDate(), null);
10141015
m_usageIPAddressDao.persist(ipAddressVO);
10151016
} else if (EventTypes.EVENT_NET_IP_RELEASE.equals(event.getType())) {
10161017
SearchCriteria<UsageIPAddressVO> sc = m_usageIPAddressDao.createSearchCriteria();

usage/src/com/cloud/usage/parser/IPAddressUsageParser.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
7878
String key = ""+IpId;
7979

8080
// store the info in the IP map
81-
IPMap.put(key, new IpInfo(usageIp.getZoneId(), IpId, usageIp.getAddress(), usageIp.isSourceNat()));
81+
IPMap.put(key, new IpInfo(usageIp.getZoneId(), IpId, usageIp.getAddress(), usageIp.isSourceNat(), usageIp.isElastic()));
8282

8383
Date IpAssignDate = usageIp.getAssigned();
8484
Date IpReleaseDeleteDate = usageIp.getReleased();
@@ -104,7 +104,7 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
104104
// Only create a usage record if we have a runningTime of bigger than zero.
105105
if (useTime > 0L) {
106106
IpInfo info = IPMap.get(ipIdKey);
107-
createUsageRecord(info.getZoneId(), useTime, startDate, endDate, account, info.getIpId(), info.getIPAddress(), info.isSourceNat());
107+
createUsageRecord(info.getZoneId(), useTime, startDate, endDate, account, info.getIpId(), info.getIPAddress(), info.isSourceNat(), info.isElastic);
108108
}
109109
}
110110

@@ -123,7 +123,7 @@ private static void updateIpUsageData(Map<String, Pair<Long, Long>> usageDataMap
123123
usageDataMap.put(key, ipUsageInfo);
124124
}
125125

126-
private static void createUsageRecord(long zoneId, long runningTime, Date startDate, Date endDate, AccountVO account, long IpId, String IPAddress, boolean isSourceNat) {
126+
private static void createUsageRecord(long zoneId, long runningTime, Date startDate, Date endDate, AccountVO account, long IpId, String IPAddress, boolean isSourceNat, boolean isElastic) {
127127
if (s_logger.isDebugEnabled()) {
128128
s_logger.debug("Total usage time " + runningTime + "ms");
129129
}
@@ -141,8 +141,8 @@ private static void createUsageRecord(long zoneId, long runningTime, Date startD
141141

142142
// Create the usage record
143143

144-
UsageVO usageRecord = new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs",
145-
UsageTypes.IP_ADDRESS, new Double(usage), null, null, null, null, IpId, startDate, endDate, (isSourceNat?"SourceNat":""));
144+
UsageVO usageRecord = new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", UsageTypes.IP_ADDRESS, new Double(usage), IpId,
145+
(isElastic?1:0), (isSourceNat?"SourceNat":""), startDate, endDate);
146146
m_usageDao.persist(usageRecord);
147147
}
148148

@@ -151,12 +151,14 @@ private static class IpInfo {
151151
private long IpId;
152152
private String IPAddress;
153153
private boolean isSourceNat;
154+
private boolean isElastic;
154155

155-
public IpInfo(long zoneId,long IpId, String IPAddress, boolean isSourceNat) {
156+
public IpInfo(long zoneId,long IpId, String IPAddress, boolean isSourceNat, boolean isElastic) {
156157
this.zoneId = zoneId;
157158
this.IpId = IpId;
158159
this.IPAddress = IPAddress;
159160
this.isSourceNat = isSourceNat;
161+
this.isElastic = isElastic;
160162
}
161163

162164
public long getZoneId() {
@@ -174,6 +176,10 @@ public String getIPAddress() {
174176
public boolean isSourceNat() {
175177
return isSourceNat;
176178
}
179+
180+
public boolean isElastic() {
181+
return isElastic;
182+
}
177183
}
178184

179185
}

0 commit comments

Comments
 (0)