forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVmRulesetLogDaoImpl.java
More file actions
200 lines (174 loc) · 7.54 KB
/
Copy pathVmRulesetLogDaoImpl.java
File metadata and controls
200 lines (174 loc) · 7.54 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// 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.network.security.dao;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLTransactionRollbackException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.ejb.Local;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.network.security.VmRulesetLogVO;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.Transaction;
@Component
@Local(value={VmRulesetLogDao.class})
public class VmRulesetLogDaoImpl extends GenericDaoBase<VmRulesetLogVO, Long> implements VmRulesetLogDao {
protected static Logger s_logger = Logger.getLogger(VmRulesetLogDaoImpl.class);
private SearchBuilder<VmRulesetLogVO> VmIdSearch;
private String INSERT_OR_UPDATE = "INSERT INTO op_vm_ruleset_log (instance_id, created, logsequence) " +
" VALUES(?, now(), 1) ON DUPLICATE KEY UPDATE logsequence=logsequence+1";
private static HashMap<Integer, String> cachedPrepStmtStrings = new HashMap<Integer, String>();
final static private int cacheStringSizes [] = {512, 256, 128, 64, 32, 16, 8, 4, 2, 1};
static {
//prepare the cache.
for (int size: cacheStringSizes) {
cachedPrepStmtStrings.put(size, createPrepStatementString(size));
}
}
private static String createPrepStatementString(int numItems) {
StringBuilder builder = new StringBuilder("INSERT INTO op_vm_ruleset_log (instance_id, created, logsequence) VALUES ");
for (int i=0; i < numItems-1; i++) {
builder.append("(?, now(), 1), ");
}
builder.append("(?, now(), 1) ");
builder.append(" ON DUPLICATE KEY UPDATE logsequence=logsequence+1");
return builder.toString();
}
protected VmRulesetLogDaoImpl() {
VmIdSearch = createSearchBuilder();
VmIdSearch.and("vmId", VmIdSearch.entity().getInstanceId(), SearchCriteria.Op.EQ);
VmIdSearch.done();
}
@Override
public VmRulesetLogVO findByVmId(long vmId) {
SearchCriteria<VmRulesetLogVO> sc = VmIdSearch.create();
sc.setParameters("vmId", vmId);
return findOneIncludingRemovedBy(sc);
}
@Override
public int createOrUpdate(Set<Long> workItems) {
//return createOrUpdateUsingBatch(workItems);
return createOrUpdateUsingMultiInsert(workItems);
}
private int executeWithRetryOnDeadlock(Transaction txn, String pstmt, List<Long> vmIds) throws SQLException {
int numUpdated = 0;
final int maxTries = 3;
for (int i=0; i < maxTries; i++) {
try {
PreparedStatement stmtInsert = txn.prepareAutoCloseStatement(pstmt);
int argIndex = 1;
for (Long vmId: vmIds) {
stmtInsert.setLong(argIndex++, vmId);
}
numUpdated = stmtInsert.executeUpdate();
i = maxTries;
} catch (SQLTransactionRollbackException e1) {
if (i < maxTries-1) {
int delayMs = (i+1)*1000;
s_logger.debug("Caught a deadlock exception while inserting security group rule log, retrying in " + delayMs);
try {
Thread.sleep(delayMs);
} catch(InterruptedException ie) {
}
}
else
s_logger.warn("Caught another deadlock exception while retrying inserting security group rule log, giving up");
}
}
if (s_logger.isTraceEnabled()) {
s_logger.trace("Inserted or updated " + numUpdated + " rows");
}
return numUpdated;
}
protected int createOrUpdateUsingMultiInsert(Set<Long> workItems) {
Transaction txn = Transaction.currentTxn();
int size = workItems.size();
int count = 0;
Iterator<Long> workIter = workItems.iterator();
int remaining = size;
try {
for (int stmtSize : cacheStringSizes) {
int numStmts = remaining / stmtSize;
if (numStmts > 0) {
String pstmt = cachedPrepStmtStrings.get(stmtSize);
for (int i=0; i < numStmts; i++) {
List<Long> vmIds = new ArrayList<Long>();
for (int argIndex=1; argIndex <= stmtSize; argIndex++) {
Long vmId = workIter.next();
vmIds.add(vmId);
}
int numUpdated = executeWithRetryOnDeadlock(txn, pstmt, vmIds);
if (s_logger.isTraceEnabled()) {
s_logger.trace("Inserted or updated " + numUpdated + " rows");
}
if (numUpdated > 0)
count += stmtSize;
}
remaining = remaining - numStmts * stmtSize;
}
}
} catch (SQLException sqe) {
s_logger.warn("Failed to execute multi insert ", sqe);
}
return count;
}
protected int createOrUpdateUsingBatch(Set<Long> workItems) {
Transaction txn = Transaction.currentTxn();
PreparedStatement stmtInsert = null;
int [] queryResult = null;
int count=0;
boolean success = true;
try {
stmtInsert = txn.prepareAutoCloseStatement(INSERT_OR_UPDATE);
txn.start();
for (Long vmId: workItems) {
stmtInsert.setLong(1, vmId);
stmtInsert.addBatch();
count++;
if (count % 16 ==0) {
queryResult = stmtInsert.executeBatch();
stmtInsert.clearBatch();
}
}
queryResult = stmtInsert.executeBatch();
txn.commit();
if (s_logger.isTraceEnabled())
s_logger.trace("Updated or inserted " + workItems.size() + " log items");
} catch (SQLException e) {
s_logger.warn("Failed to execute batch update statement for ruleset log: ", e);
txn.rollback();
success = false;
}
if (!success && queryResult != null) {
Long [] arrayItems = new Long[workItems.size()];
workItems.toArray(arrayItems);
for (int i=0; i < queryResult.length; i++) {
if (queryResult[i] < 0 ) {
s_logger.debug("Batch query update failed for vm " + arrayItems[i]);
}
}
}
return count;
}
}