-
Notifications
You must be signed in to change notification settings - Fork 131
/
AsynchronousSaveJobStorage.java
153 lines (131 loc) · 4.81 KB
/
AsynchronousSaveJobStorage.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
package org.dcache.srm.scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import org.dcache.srm.request.Job;
public class AsynchronousSaveJobStorage<J extends Job> implements JobStorage<J>
{
private static final Logger LOGGER = LoggerFactory.getLogger(AsynchronousSaveJobStorage.class);
private final JobStorage<J> storage;
private final ConcurrentMap<Long,UpdateState> states = new ConcurrentHashMap<>();
private final Executor executor;
private static enum UpdateState
{
QUEUED_FORCED, QUEUED_NOT_FORCED, PROCESSING
}
public AsynchronousSaveJobStorage(JobStorage<J> storage, Executor executor)
{
this.storage = storage;
this.executor = executor;
}
@Override
public void init() throws DataAccessException
{
storage.init();
}
@Override
public J getJob(long jobId) throws DataAccessException
{
return storage.getJob(jobId);
}
@Override
public J getJob(long jobId, Connection connection) throws SQLException
{
return storage.getJob(jobId, connection);
}
@Override
public Set<J> getJobs(String scheduler) throws DataAccessException
{
return storage.getJobs(scheduler);
}
@Override
public Set<J> getJobs(String scheduler, State state) throws DataAccessException
{
return storage.getJobs(scheduler, state);
}
public void saveJob(final J job, final boolean force)
{
if (!force && !isJdbcLogRequestHistoryInDBEnabled()) {
return;
}
UpdateState state;
if (force) {
state = states.put(job.getId(), UpdateState.QUEUED_FORCED);
} else {
while ((state = states.putIfAbsent(job.getId(), UpdateState.QUEUED_NOT_FORCED)) == UpdateState.PROCESSING &&
!states.replace(job.getId(), UpdateState.PROCESSING, UpdateState.QUEUED_NOT_FORCED));
}
if (state == null) {
boolean success = false;
try {
Runnable task =
new Runnable()
{
@Override
public void run()
{
UpdateState state = states.put(job.getId(), UpdateState.PROCESSING);
try {
storage.saveJob(job, state == UpdateState.QUEUED_FORCED);
} catch (DataAccessException e) {
LOGGER.error("SQL statement failed: {}", e.getMessage());
} catch (Throwable e) {
Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
} finally {
if (!states.remove(job.getId(), UpdateState.PROCESSING)) {
executor.execute(this);
}
}
}
};
executor.execute(task);
success = true;
} catch (RejectedExecutionException e) {
// ignore the saving errors, this will affect monitoring and
// future status updates, but is not critical
LOGGER.error("Persistence of request {} failed, queue is too long.", job.getId());
} finally {
if (!success) {
states.remove(job.getId());
}
}
}
}
@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() throws DataAccessException
{
return storage.getActiveJobs();
}
}