/
ResourceManager.java
252 lines (222 loc) · 8.58 KB
/
ResourceManager.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
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 io.atomix.copycat.manager;
import io.atomix.catalogue.client.session.Session;
import io.atomix.catalogue.server.Commit;
import io.atomix.catalogue.server.StateMachine;
import io.atomix.catalogue.server.StateMachineExecutor;
import io.atomix.copycat.resource.ResourceOperation;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Function;
/**
* Resource manager.
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
public class ResourceManager extends StateMachine {
private StateMachineExecutor executor;
private final Map<String, Long> paths = new HashMap<>();
private final Map<Long, ResourceHolder> resources = new HashMap<>();
private final Map<Long, SessionHolder> sessions = new HashMap<>();
private final ResourceCommitPool commits = new ResourceCommitPool();
@Override
public void configure(StateMachineExecutor executor) {
this.executor = executor;
executor.register(ResourceOperation.class, (Function<Commit<ResourceOperation>, Object>) this::operateResource);
executor.register(GetResource.class, this::getResource);
executor.register(CreateResource.class, this::createResource);
executor.register(DeleteResource.class, this::deleteResource);
executor.register(ResourceExists.class, this::resourceExists);
}
/**
* Performs an operation on a resource.
*/
@SuppressWarnings("unchecked")
private Object operateResource(Commit<ResourceOperation> commit) {
ResourceHolder resource;
SessionHolder session = sessions.get(commit.operation().resource());
if (session != null) {
resource = resources.get(session.resource);
} else {
resource = resources.get(commit.operation().resource());
}
if (resource == null) {
throw new ResourceManagerException("unknown resource: " + commit.operation().resource());
}
if (session == null) {
session = resource.sessions.get(commit.session().id());
if (session == null) {
throw new ResourceManagerException("unknown resource session: " + commit.session().id());
}
}
ResourceCommit resourceCommit = commits.acquire(commit, session.session);
return resource.executor.execute(resourceCommit);
}
/**
* Gets a resource.
*/
protected long getResource(Commit<GetResource> commit) {
String path = commit.operation().path();
Long resourceId = paths.get(path);
if (resourceId == null) {
resourceId = commit.index();
paths.put(path, commit.index());
try {
StateMachine stateMachine = commit.operation().type().newInstance();
ResourceStateMachineExecutor executor = new ResourceStateMachineExecutor(commit.index(), this.executor);
ResourceHolder resource = new ResourceHolder(path, stateMachine, executor);
resources.put(resourceId, resource);
stateMachine.init(executor);
resource.sessions.put(commit.session().id(), new SessionHolder(resourceId, commit, new ManagedResourceSession(resourceId, commit.session())));
} catch (InstantiationException | IllegalAccessException e) {
throw new ResourceManagerException("failed to instantiate state machine", e);
}
} else {
ResourceHolder resource = resources.get(resourceId);
if (resource == null || resource.stateMachine.getClass() != commit.operation().type()) {
throw new ResourceManagerException("inconsistent resource type: " + commit.operation().type());
}
commit.clean();
}
return resourceId;
}
/**
* Applies a create resource commit.
*/
private long createResource(Commit<CreateResource> commit) {
String path = commit.operation().path();
Long resourceId = paths.get(path);
long id;
ResourceHolder resource;
if (resourceId == null) {
resourceId = commit.index();
paths.put(path, commit.index());
try {
StateMachine stateMachine = commit.operation().type().newInstance();
ResourceStateMachineExecutor executor = new ResourceStateMachineExecutor(commit.index(), this.executor);
resource = new ResourceHolder(path, stateMachine, executor);
resources.put(resourceId, resource);
stateMachine.init(executor);
} catch (InstantiationException | IllegalAccessException e) {
throw new ResourceManagerException("failed to instantiate state machine", e);
}
} else {
resource = resources.get(resourceId);
if (resource == null || resource.stateMachine.getClass() != commit.operation().type()) {
throw new ResourceManagerException("inconsistent resource type: " + commit.operation().type());
}
}
id = commit.index();
ManagedResourceSession session = new ManagedResourceSession(id, commit.session());
sessions.put(id, new SessionHolder(resourceId, commit, session));
resource.stateMachine.register(session);
return id;
}
/**
* Checks if a resource exists.
*/
protected boolean resourceExists(Commit<ResourceExists> commit) {
return paths.containsKey(commit.operation().path());
}
/**
* Applies a delete resource commit.
*/
protected boolean deleteResource(Commit<DeleteResource> commit) {
ResourceHolder resource = resources.remove(commit.operation().resource());
if (resource == null) {
throw new ResourceManagerException("unknown resource: " + commit.operation().resource());
}
resource.executor.close();
paths.remove(resource.path);
Iterator<Map.Entry<Long, SessionHolder>> iterator = sessions.entrySet().iterator();
while (iterator.hasNext()) {
SessionHolder session = iterator.next().getValue();
if (session.resource == commit.operation().resource()) {
iterator.remove();
session.commit.clean();
}
}
return true;
}
@Override
public void expire(Session session) {
for (ResourceHolder resource : resources.values()) {
SessionHolder resourceSession = resource.sessions.get(session.id());
if (resourceSession != null) {
resource.stateMachine.expire(resourceSession.session);
}
}
for (SessionHolder sessionHolder : sessions.values()) {
if (sessionHolder.session.id() == session.id()) {
ResourceHolder resource = resources.get(sessionHolder.resource);
if (resource != null) {
resource.stateMachine.expire(sessionHolder.session);
}
}
}
}
@Override
public void close(Session session) {
for (ResourceHolder resource : resources.values()) {
SessionHolder resourceSession = resource.sessions.remove(session.id());
if (resourceSession != null) {
resource.stateMachine.close(resourceSession.session);
resourceSession.commit.clean();
}
}
Iterator<Map.Entry<Long, SessionHolder>> iterator = sessions.entrySet().iterator();
while (iterator.hasNext()) {
SessionHolder sessionHolder = iterator.next().getValue();
if (sessionHolder.session.id() == session.id()) {
ResourceHolder resource = resources.get(sessionHolder.resource);
if (resource != null) {
resource.stateMachine.close(sessionHolder.session);
}
sessionHolder.commit.clean();
iterator.remove();
}
}
}
/**
* Resource holder.
*/
private static class ResourceHolder {
private final String path;
private final StateMachine stateMachine;
private final ResourceStateMachineExecutor executor;
private final Map<Long, SessionHolder> sessions = new HashMap<>();
private ResourceHolder(String path, StateMachine stateMachine, ResourceStateMachineExecutor executor) {
this.path = path;
this.stateMachine = stateMachine;
this.executor = executor;
}
}
/**
* Session holder.
*/
private static class SessionHolder {
private final long resource;
private final Commit commit;
private final ManagedResourceSession session;
private SessionHolder(long resource, Commit commit, ManagedResourceSession session) {
this.resource = resource;
this.commit = commit;
this.session = session;
}
}
}