forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequenceFetcher.java
More file actions
171 lines (148 loc) · 6.53 KB
/
Copy pathSequenceFetcher.java
File metadata and controls
171 lines (148 loc) · 6.53 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
// 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
// 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.utils.db;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.persistence.TableGenerator;
import org.apache.log4j.Logger;
import com.cloud.utils.concurrency.NamedThreadFactory;
/**
* Since Mysql does not have sequence support, we have
* table retrieval was inside a transaction, the value
* gets locked until the transaction is over.
*
* allocation size.
*
*/
public class SequenceFetcher {
private final static Logger s_logger = Logger.getLogger(SequenceFetcher.class);
ExecutorService _executors;
private final static Random random = new Random();
public <T> T getNextSequence(Class<T> clazz, TableGenerator tg) {
return getNextSequence(clazz, tg, null, false);
}
public <T> T getNextSequence(Class<T> clazz, TableGenerator tg, Object key) {
return getNextSequence(clazz, tg, key, false);
}
public <T> T getRandomNextSequence(Class<T> clazz, TableGenerator tg) {
return getNextSequence(clazz, tg, null, true);
}
public <T> T getNextSequence(Class<T> clazz, TableGenerator tg, Object key, boolean isRandom) {
Future<T> future = _executors.submit(new Fetcher<T>(clazz, tg, key, isRandom));
try {
return future.get();
} catch (Exception e) {
s_logger.warn("Unable to get sequeunce for " + tg.table() + ":" + tg.pkColumnValue(), e);
return null;
}
}
protected SequenceFetcher() {
_executors = new ThreadPoolExecutor(100, 100, 120l, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(250), new NamedThreadFactory("SequenceFetcher"));
}
protected static final SequenceFetcher s_instance = new SequenceFetcher();
public static SequenceFetcher getInstance() {
return s_instance;
}
protected class Fetcher<T> implements Callable<T> {
TableGenerator _tg;
Class<T> _clazz;
Object _key;
boolean isRandom = false;
protected Fetcher(Class<T> clazz, TableGenerator tg, Object key, boolean isRandom) {
_tg = tg;
_clazz = clazz;
_key = key;
this.isRandom = isRandom;
}
@Override @SuppressWarnings("unchecked")
public T call() throws Exception {
try {
PreparedStatement stmt = null;
StringBuilder sql = new StringBuilder("SELECT ");
sql.append(_tg.valueColumnName()).append(" FROM ").append(_tg.table());
sql.append(" WHERE ").append(_tg.pkColumnName()).append(" = ? FOR UPDATE");
Transaction txn = Transaction.open("Sequence");
PreparedStatement selectStmt = txn.prepareStatement(sql.toString());
if (_key == null) {
selectStmt.setString(1, _tg.pkColumnValue());
} else {
selectStmt.setObject(1, _key);
}
sql = new StringBuilder("UPDATE ");
sql.append(_tg.table()).append(" SET ").append(_tg.valueColumnName()).append("=").append("?+?");
sql.append(" WHERE ").append(_tg.pkColumnName()).append("=?");
PreparedStatement updateStmt = txn.prepareStatement(sql.toString());
if(isRandom){
updateStmt.setInt(2, random.nextInt(10) + 1);
} else {
updateStmt.setInt(2, _tg.allocationSize());
}
if (_key == null) {
updateStmt.setString(3, _tg.pkColumnValue());
} else {
updateStmt.setObject(3, _key);
}
ResultSet rs = null;
try {
txn.start();
stmt = selectStmt;
rs = stmt.executeQuery();
Object obj = null;
while (rs.next()) {
if (_clazz.isAssignableFrom(Long.class)) {
obj = rs.getLong(1);
} else if (_clazz.isAssignableFrom(Integer.class)) {
obj = rs.getInt(1);
} else {
obj = rs.getObject(1);
}
}
if (obj == null) {
s_logger.warn("Unable to get a sequence: " + updateStmt.toString());
return null;
}
updateStmt.setObject(1, obj);
stmt = updateStmt;
int rows = stmt.executeUpdate();
assert rows == 1 : "Come on....how exactly did we update this many rows " + rows + " for " + updateStmt.toString();
txn.commit();
return (T)obj;
} catch (SQLException e) {
s_logger.warn("Caught this exception when running: " + (stmt != null ? stmt.toString() : ""), e);
} finally {
if (rs != null) {
rs.close();
}
selectStmt.close();
updateStmt.close();
txn.close();
}
} catch (Exception e) {
s_logger.warn("Caught this exception when running.", e);
}
return null;
}
}
}