-
Notifications
You must be signed in to change notification settings - Fork 131
/
SharedMemoryCacheJobStorage.java
186 lines (164 loc) · 5.44 KB
/
SharedMemoryCacheJobStorage.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
package org.dcache.srm.scheduler;
import org.springframework.dao.DataAccessException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import org.dcache.srm.request.Job;
import static java.util.concurrent.TimeUnit.SECONDS;
/**
* Canonicalizing and caching job storage decorator suitable for Terracotta.
*
* Guarantees that for a given job ID, the same Job instance is returned as long
* as the job is not in a final state. Jobs in a final state are not canonicalized.
*
* All non-final jobs are cached in a static SharedMemoryCache instance suitable
* for use as a root object with Terracotta. The expiration time of such jobs are
* periodically checked and if passed the jobs are expired.
*
* Since the cache is shared among all instances of SharedMemoryCacheJobStorage,
* Job IDs must be unique over all instances. This has the additional benefit
* that the cache allows early detection of job IDs that don't belong to the
* decorated job storage: We can detect if an ID belongs to a job storage with a
* different type and avoid an expensive probe from the decorated storage.
*/
public class SharedMemoryCacheJobStorage<J extends Job> implements JobStorage<J>
{
private static final SharedMemoryCache sharedMemoryCache =
new SharedMemoryCache();
private static final Timer timer = new Timer("Job expiration", true);
private final JobStorage<J> storage;
private final Class<J> type;
private final Set<J> jobsToExpire = Collections.newSetFromMap(new ConcurrentHashMap<J, Boolean>());
public SharedMemoryCacheJobStorage(JobStorage<J> storage, Class<J> type)
{
this.storage = storage;
this.type = type;
}
private J canonicalize(J job)
{
if (job == null) {
return null;
}
J canonicalJob = sharedMemoryCache.canonicalize(job);
if (job == canonicalJob) {
updateExpirationSet(job);
}
return canonicalJob;
}
private void updateExpirationSet(J job)
{
if (job.getState().isFinalState()) {
jobsToExpire.remove(job);
} else {
jobsToExpire.add(job);
}
}
@Override
public void init() throws DataAccessException
{
storage.init();
for (J job : storage.getActiveJobs()) {
canonicalize(job);
}
timer.schedule(new ExpirationTask(), SECONDS.toMillis(10), SECONDS.toMillis(60));
}
@Override
public J getJob(long jobId) throws DataAccessException
{
Job jobFromCache = sharedMemoryCache.getJob(jobId);
if (jobFromCache == null) {
return canonicalize(storage.getJob(jobId));
} else if (type.isInstance(jobFromCache)) {
return type.cast(jobFromCache);
} else {
return null;
}
}
@Override
public J getJob(long jobId, Connection connection) throws SQLException
{
Job jobFromCache = sharedMemoryCache.getJob(jobId);
if (jobFromCache == null) {
return canonicalize(storage.getJob(jobId, connection));
} else if (type.isInstance(jobFromCache)) {
return type.cast(jobFromCache);
} else {
return null;
}
}
@Override
public Set<J> getJobs(String scheduler) throws DataAccessException
{
Set<J> result = new HashSet<>();
for (J job : storage.getJobs(scheduler)) {
result.add(canonicalize(job));
}
return result;
}
@Override
public Set<J> getJobs(String scheduler, State state) throws DataAccessException
{
Set<J> result = new HashSet<>();
for (J job : storage.getJobs(scheduler, state)) {
result.add(canonicalize(job));
}
return result;
}
@Override
public void saveJob(J job, boolean force) throws DataAccessException
{
storage.saveJob(job, force);
sharedMemoryCache.update(job);
updateExpirationSet(job);
}
@Override
public boolean isJdbcLogRequestHistoryInDBEnabled()
{
return storage.isJdbcLogRequestHistoryInDBEnabled();
}
@Override
public Set<Long> getLatestCompletedJobIds(int maxNum) throws DataAccessException
{
return storage.getLatestCompletedJobIds(maxNum);
}
@Override
public Set<Long> getLatestDoneJobIds(int maxNum) throws DataAccessException
{
return storage.getLatestDoneJobIds(maxNum);
}
@Override
public Set<Long> getLatestFailedJobIds(int maxNum) throws DataAccessException
{
return storage.getLatestFailedJobIds(maxNum);
}
@Override
public Set<Long> getLatestCanceledJobIds(int maxNum) throws DataAccessException
{
return storage.getLatestCanceledJobIds(maxNum);
}
@Override
public Set<J> getActiveJobs()
{
return sharedMemoryCache.getJobs(type);
}
private class ExpirationTask extends TimerTask
{
@Override
public void run()
{
for (J job : jobsToExpire) {
try {
job.checkExpiration();
} catch (RuntimeException e) {
Thread thread = Thread.currentThread();
thread.getUncaughtExceptionHandler().uncaughtException(thread, e);
}
}
}
}
}