/
ClusterService.java
159 lines (139 loc) · 6.03 KB
/
ClusterService.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
/*
* 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.service.impl;
import static org.jumpmind.symmetric.service.ClusterConstants.HEARTBEAT;
import static org.jumpmind.symmetric.service.ClusterConstants.PULL;
import static org.jumpmind.symmetric.service.ClusterConstants.PURGE_DATA_GAPS;
import static org.jumpmind.symmetric.service.ClusterConstants.PURGE_INCOMING;
import static org.jumpmind.symmetric.service.ClusterConstants.PURGE_OUTGOING;
import static org.jumpmind.symmetric.service.ClusterConstants.PURGE_STATISTICS;
import static org.jumpmind.symmetric.service.ClusterConstants.PUSH;
import static org.jumpmind.symmetric.service.ClusterConstants.ROUTE;
import static org.jumpmind.symmetric.service.ClusterConstants.SYNCTRIGGERS;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.time.DateUtils;
import org.jumpmind.db.sql.ISqlRowMapper;
import org.jumpmind.db.sql.Row;
import org.jumpmind.db.sql.UniqueKeyException;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.db.ISymmetricDialect;
import org.jumpmind.symmetric.model.Lock;
import org.jumpmind.symmetric.service.IClusterService;
import org.jumpmind.symmetric.service.IParameterService;
import org.jumpmind.symmetric.util.AppUtils;
/**
* @see IClusterService
*/
public class ClusterService extends AbstractService implements IClusterService {
protected String serverId = AppUtils.getServerId();
public ClusterService(IParameterService parameterService, ISymmetricDialect dialect) {
super(parameterService, dialect);
setSqlMap(new ClusterServiceSqlMap(symmetricDialect.getPlatform(),
createSqlReplacementTokens()));
}
public void initLockTable() {
initLockTable(ROUTE);
initLockTable(PULL);
initLockTable(PUSH);
initLockTable(HEARTBEAT);
initLockTable(PURGE_INCOMING);
initLockTable(PURGE_OUTGOING);
initLockTable(PURGE_STATISTICS);
initLockTable(SYNCTRIGGERS);
initLockTable(PURGE_DATA_GAPS);
}
public void initLockTable(final String action) {
try {
sqlTemplate.update(getSql("insertLockSql"), new Object[] { action });
log.debug("Inserted into the NODE_LOCK table for {}", action);
} catch (UniqueKeyException ex) {
log.debug("Failed to insert to the NODE_LOCK table for {}. Must be initialized already.", action);
}
}
public void clearAllLocks() {
sqlTemplate.update(getSql("clearAllLocksSql"));
}
public boolean lock(final String action) {
if (isClusteringEnabled()) {
final Date timeout = DateUtils.add(new Date(), Calendar.MILLISECOND,
(int) -parameterService.getLong(ParameterConstants.CLUSTER_LOCK_TIMEOUT_MS));
return lock(action, timeout, new Date(), serverId);
} else {
return true;
}
}
protected boolean lock(String action, Date timeToBreakLock, Date timeLockAquired,
String serverId) {
return sqlTemplate.update(getSql("aquireLockSql"), new Object[] { serverId,
timeLockAquired, action, timeToBreakLock, serverId }) == 1;
}
public Map<String, Lock> findLocks() {
final Map<String, Lock> locks = new HashMap<String, Lock>();
if (isClusteringEnabled()) {
sqlTemplate.query(getSql("findLocksSql"), new ISqlRowMapper<Lock>() {
public Lock mapRow(Row rs) {
Lock lock = new Lock();
lock.setLockAction(rs.getString("lock_action"));
lock.setLockingServerId(rs.getString("locking_server_id"));
lock.setLockTime(rs.getDateTime("lock_time"));
lock.setLastLockingServerId(rs.getString("last_locking_server_id"));
lock.setLastLockTime(rs.getDateTime("last_lock_time"));
locks.put(lock.getLockAction(), lock);
return lock;
}
});
}
return locks;
}
public String getServerId() {
return serverId;
}
public void unlock(final String action) {
if (isClusteringEnabled()) {
if (!unlock(action, serverId)) {
log.warn("Failed to release lock for action:{} server:{}", action, serverId);
}
}
}
protected boolean unlock(String action, String serverId) {
return sqlTemplate.update(getSql("releaseLockSql"), new Object[] { serverId, action,
serverId }) > 0;
}
public boolean isClusteringEnabled() {
return parameterService.is(ParameterConstants.CLUSTER_LOCKING_ENABLED);
}
public void aquireInfiniteLock(String action) {
if (isClusteringEnabled()) {
Date futureTime = DateUtils.add(new Date(), Calendar.YEAR, 100);
lock(action, new Date(), futureTime, Lock.STOPPED);
}
}
public void clearInfiniteLock(String action) {
Map<String, Lock> all = findLocks();
Lock lock = all.get(action);
if (lock != null && Lock.STOPPED.equals(lock.getLockingServerId())) {
unlock(action, Lock.STOPPED);
}
}
}