forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCheckedReservation.java
More file actions
142 lines (125 loc) · 5.37 KB
/
CheckedReservation.java
File metadata and controls
142 lines (125 loc) · 5.37 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package com.cloud.resourcelimit;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.reservation.ReservationVO;
import org.apache.cloudstack.reservation.dao.ReservationDao;
import org.apache.cloudstack.user.ResourceReservation;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import com.cloud.configuration.Resource.ResourceType;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.user.Account;
import com.cloud.user.ResourceLimitService;
import com.cloud.utils.db.GlobalLock;
import com.cloud.utils.exception.CloudRuntimeException;
public class CheckedReservation implements AutoCloseable, ResourceReservation {
private static final Logger LOG = Logger.getLogger(CheckedReservation.class);
private static final int TRY_TO_GET_LOCK_TIME = 120;
private GlobalLock quotaLimitLock;
ReservationDao reservationDao;
private final Account account;
private final ResourceType resourceType;
private Long amount;
private ResourceReservation reservation;
private String getContextParameterKey() {
return String.format("%s-%s", ResourceReservation.class.getSimpleName(), resourceType.getName());
}
/**
* - check if adding a reservation is allowed
* - create DB entry for reservation
* - hold the id of this record as a ticket for implementation
*
* @param amount positive number of the resource type to reserve
* @throws ResourceAllocationException
*/
public CheckedReservation(Account account, ResourceType resourceType, Long amount, ReservationDao reservationDao, ResourceLimitService resourceLimitService) throws ResourceAllocationException {
this.reservationDao = reservationDao;
this.account = account;
this.resourceType = resourceType;
this.amount = amount;
this.reservation = null;
setGlobalLock(account, resourceType);
if (this.amount != null && this.amount <= 0) {
if(LOG.isDebugEnabled()){
LOG.debug(String.format("not reserving no amount of resources for %s in domain %d, type: %s, %s ", account.getAccountName(), account.getDomainId(), resourceType, amount));
}
this.amount = null;
}
if (this.amount != null) {
if(quotaLimitLock.lock(TRY_TO_GET_LOCK_TIME)) {
try {
resourceLimitService.checkResourceLimit(account,resourceType,amount);
ReservationVO reservationVO = new ReservationVO(account.getAccountId(), account.getDomainId(), resourceType, amount);
this.reservation = reservationDao.persist(reservationVO);
CallContext.current().putContextParameter(getContextParameterKey(), reservation.getId());
} catch (NullPointerException npe) {
throw new CloudRuntimeException("not enough means to check limits", npe);
} finally {
quotaLimitLock.unlock();
}
} else {
throw new ResourceAllocationException(String.format("unable to acquire resource reservation \"%s\"", quotaLimitLock.getName()), resourceType);
}
} else {
if(LOG.isDebugEnabled()){
LOG.debug(String.format("not reserving no amount of resources for %s in domain %d, type: %s ", account.getAccountName(), account.getDomainId(), resourceType));
}
}
}
@NotNull
private void setGlobalLock(Account account, ResourceType resourceType) {
String lockName = String.format("CheckedReservation-%s/%d", account.getDomainId(), resourceType.getOrdinal());
setQuotaLimitLock(GlobalLock.getInternLock(lockName));
}
protected void setQuotaLimitLock(GlobalLock quotaLimitLock) {
this.quotaLimitLock = quotaLimitLock;
}
@Override
public void close() throws Exception {
if (this.reservation != null) {
CallContext.current().removeContextParameter(getContextParameterKey());
reservationDao.remove(reservation.getId());
reservation = null;
}
}
public Account getAccount() {
return account;
}
@Override
public Long getAccountId() {
return account.getId();
}
@Override
public Long getDomainId() {
return account.getDomainId();
}
@Override
public ResourceType getResourceType() {
return resourceType;
}
@Override
public Long getReservedAmount() {
return amount;
}
@Override
public long getId() {
return this.reservation.getId();
}
}