/
AbstractDataToRouteReader.java
278 lines (239 loc) · 10.1 KB
/
AbstractDataToRouteReader.java
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU Lesser General Public License (the
* "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* 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 org.jumpmind.symmetric.route;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.common.logging.ILog;
import org.jumpmind.symmetric.db.IDbDialect;
import org.jumpmind.symmetric.model.Channel;
import org.jumpmind.symmetric.model.Data;
import org.jumpmind.symmetric.service.IDataService;
import org.jumpmind.symmetric.service.ISqlProvider;
import org.jumpmind.symmetric.util.AppUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.JdbcUtils;
/**
* This class is responsible for reading data for the purpose of routing. It
* reads ahead and tries to keep a blocking queue populated for other threads to
* process.
*/
abstract public class AbstractDataToRouteReader implements IDataToRouteReader {
protected ILog log;
protected BlockingQueue<Data> dataQueue;
protected ISqlProvider sqlProvider;
protected JdbcTemplate jdbcTemplate;
protected ChannelRouterContext context;
protected IDataService dataService;
protected boolean reading = true;
protected IDbDialect dbDialect;
public AbstractDataToRouteReader(ILog log, ISqlProvider sqlProvider,
ChannelRouterContext context, IDataService dataService) {
this.log = log;
this.jdbcTemplate = dataService != null ? dataService.getJdbcTemplate() : null;
this.dbDialect = dataService != null ? dataService.getDbDialect() : null;
this.dataQueue = new LinkedBlockingQueue<Data>(
dbDialect != null ? dbDialect.getRouterDataPeekAheadCount() : 1000);
this.sqlProvider = sqlProvider;
this.context = context;
this.dataService = dataService;
}
public Data take() {
Data data = null;
try {
int queryTimeout = jdbcTemplate.getQueryTimeout();
data = dataQueue.poll(queryTimeout < 3600 ? 3600 : queryTimeout, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.warn(e);
}
if (data == null) {
log.warn("RouterDataReaderNotResponding");
} else if (data instanceof EOD) {
data = null;
}
return data;
}
/**
* Start out by selecting only the rows that need routed.
*/
protected PreparedStatement prepareStatment(Connection c) throws SQLException {
throw new NotImplementedException();
}
protected String getSql(String sqlName, Channel channel) {
String select = sqlProvider.getSql(sqlName);
if (!channel.isUseOldDataToRoute()) {
select = select.replace("d.old_data", "''");
}
if (!channel.isUseRowDataToRoute()) {
select = select.replace("d.row_data", "''");
}
if (!channel.isUsePkDataToRoute()) {
select = select.replace("d.pk_data", "''");
}
return dbDialect == null ? select : dbDialect.massageDataExtractionSql(select, channel);
}
public void run() {
try {
execute();
} catch (Throwable ex) {
log.error(ex);
}
}
protected void execute() {
jdbcTemplate.execute(new ConnectionCallback<Integer>() {
public Integer doInConnection(Connection c) throws SQLException, DataAccessException {
int dataCount = 0;
PreparedStatement ps = null;
ResultSet rs = null;
boolean autoCommit = c.getAutoCommit();
try {
if (dbDialect.requiresAutoCommitFalseToSetFetchSize()) {
c.setAutoCommit(false);
}
long maxDataToRoute = context.getChannel().getMaxDataToRoute();
int peekAheadCount = dbDialect.getRouterDataPeekAheadCount();
String lastTransactionId = null;
List<Data> peekAheadQueue = new ArrayList<Data>(peekAheadCount);
ps = prepareStatment(c);
rs = executeQuery(ps);
boolean moreData = true;
while (dataCount <= maxDataToRoute || lastTransactionId != null) {
if (moreData) {
moreData = fillPeekAheadQueue(peekAheadQueue, peekAheadCount, rs);
}
if (lastTransactionId == null && peekAheadQueue.size() > 0) {
Data data = peekAheadQueue.remove(0);
copyToQueue(data);
dataCount++;
lastTransactionId = data.getTransactionId();
} else if (lastTransactionId != null && peekAheadQueue.size() > 0) {
Iterator<Data> datas = peekAheadQueue.iterator();
int dataWithSameTransactionIdCount = 0;
while (datas.hasNext()) {
Data data = datas.next();
if (lastTransactionId.equals(data.getTransactionId())) {
dataWithSameTransactionIdCount++;
datas.remove();
copyToQueue(data);
dataCount++;
}
}
if (dataWithSameTransactionIdCount == 0) {
lastTransactionId = null;
}
} else if (peekAheadQueue.size() == 0) {
// we've reached the end of the result set
break;
}
}
return dataCount;
} finally {
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(ps);
rs = null;
ps = null;
if (dbDialect.requiresAutoCommitFalseToSetFetchSize()) {
c.commit();
c.setAutoCommit(autoCommit);
}
copyToQueue(new EOD());
reading = false;
}
}
});
}
protected boolean fillPeekAheadQueue(List<Data> peekAheadQueue, int peekAheadCount, ResultSet rs)
throws SQLException {
boolean moreData = true;
int toRead = peekAheadCount - peekAheadQueue.size();
int dataCount = 0;
long ts = System.currentTimeMillis();
while (reading && dataCount < toRead) {
if (rs.next()) {
if (process(rs)) {
Data data = dataService.readData(rs);
context.setLastDataIdForTransactionId(data);
peekAheadQueue.add(data);
dataCount++;
context.incrementStat(System.currentTimeMillis() - ts,
ChannelRouterContext.STAT_READ_DATA_MS);
} else {
context.incrementStat(System.currentTimeMillis() - ts,
ChannelRouterContext.STAT_REREAD_DATA_MS);
}
ts = System.currentTimeMillis();
} else {
moreData = false;
break;
}
}
return moreData;
}
protected ResultSet executeQuery(PreparedStatement ps) throws SQLException {
long ts = System.currentTimeMillis();
ResultSet rs = ps.executeQuery();
long executeTimeInMs = System.currentTimeMillis() - ts;
context.incrementStat(executeTimeInMs, ChannelRouterContext.STAT_QUERY_TIME_MS);
if (executeTimeInMs > Constants.LONG_OPERATION_THRESHOLD) {
log.warn("RoutedDataSelectedInTime", executeTimeInMs, context.getChannel()
.getChannelId());
} else if (log.isDebugEnabled()) {
log.debug("RoutedDataSelectedInTime", executeTimeInMs, context.getChannel()
.getChannelId());
}
return rs;
}
protected void copyToQueue(Data data) {
long ts = System.currentTimeMillis();
while (!dataQueue.offer(data) && reading) {
AppUtils.sleep(50);
}
context.incrementStat(System.currentTimeMillis() - ts,
ChannelRouterContext.STAT_ENQUEUE_DATA_MS);
}
public boolean isReading() {
return reading;
}
public void setReading(boolean reading) {
this.reading = reading;
}
public BlockingQueue<Data> getDataQueue() {
return dataQueue;
}
protected boolean process(ResultSet rs) throws SQLException {
return StringUtils.isBlank(rs.getString(13));
}
class EOD extends Data {
private static final long serialVersionUID = 1L;
}
}