/
ResourceManager.java
385 lines (333 loc) · 10.5 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* 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 net.kuujo.copycat.manager;
import net.kuujo.copycat.ResourceCommand;
import net.kuujo.copycat.ResourceOperation;
import net.kuujo.copycat.ResourceQuery;
import net.kuujo.copycat.log.Compaction;
import net.kuujo.copycat.raft.*;
import java.util.*;
/**
* Resource manager.
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
public class ResourceManager extends StateMachine {
private static final String PATH_SEPARATOR = "/";
private NodeHolder node;
private final Map<Long, NodeHolder> nodes = new HashMap<>();
private final Map<Long, StateMachine> resources = new HashMap<>();
private final Map<Long, Session> sessions = new HashMap<>();
/**
* Initializes the path.
*/
private void init(Commit commit) {
if (node == null) {
node = new NodeHolder(PATH_SEPARATOR, PATH_SEPARATOR, commit.index(), commit.timestamp());
}
}
/**
* Applies resource commands.
*/
@SuppressWarnings("unchecked")
@Apply({ResourceCommand.class, ResourceQuery.class})
protected Object commandResource(Commit<? extends ResourceOperation> commit) {
StateMachine resource = resources.get(commit.operation().resource());
if (resource != null) {
return resource.apply(new Commit(commit.index(), commit.session(), commit.timestamp(), commit.operation().operation()));
}
throw new IllegalArgumentException("unknown resource: " + commit.operation().resource());
}
/**
* Filters resource commands.
*/
@SuppressWarnings("unchecked")
@Filter(ResourceCommand.class)
protected boolean filterResource(Commit<ResourceCommand> commit, Compaction compaction) {
StateMachine resource = resources.get(commit.operation().resource());
return resource != null && resource.filter(new Commit(commit.index(), commit.session(), commit.timestamp(), commit.operation().operation()), compaction);
}
/**
* Applies a create commit.
*/
@Apply(CreatePath.class)
protected boolean createPath(Commit<CreatePath> commit) {
String path = commit.operation().path();
init(commit);
NodeHolder node = this.node;
StringBuilder currentPath = new StringBuilder();
boolean created = false;
for (String name : path.split(PATH_SEPARATOR)) {
if (!name.equals("")) {
currentPath.append("/").append(name);
NodeHolder child = node.children.get(name);
if (child == null) {
child = new NodeHolder(name, currentPath.toString(), commit.index(), commit.timestamp());
node.children.put(child.name, child);
created = true;
}
node = child;
}
}
return created;
}
/**
* Filters a create commit.
*/
@Filter(CreatePath.class)
protected boolean filterCreatePath(Commit<CreatePath> commit, Compaction compaction) {
String path = commit.operation().path();
init(commit);
NodeHolder node = this.node;
for (String name : path.split(PATH_SEPARATOR)) {
if (!name.equals("")) {
NodeHolder child = node.children.get(name);
if (child == null) {
return false;
}
node = child;
}
}
return node.version == commit.index();
}
/**
* Applies an exists commit.
*/
@Apply(PathExists.class)
protected boolean pathExists(Commit<PathExists> commit) {
String path = commit.operation().path();
if (this.node == null)
return false;
NodeHolder node = this.node;
for (String name : path.split(PATH_SEPARATOR)) {
if (!name.equals("")) {
node = node.children.get(name);
if (node == null) {
return false;
}
}
}
return true;
}
/**
* Applies a getPath children commit.
*/
@SuppressWarnings("unchecked")
@Apply(PathChildren.class)
protected List<String> pathChildren(Commit<PathChildren> commit) {
String path = commit.operation().path();
if (this.node == null)
return Collections.EMPTY_LIST;
NodeHolder node = this.node;
for (String name : path.split(PATH_SEPARATOR)) {
if (!name.equals("")) {
node = node.children.get(name);
if (node == null) {
return Collections.EMPTY_LIST;
}
}
}
return new ArrayList<>(node.children.keySet());
}
/**
* Applies a delete commit.
*/
@Apply(DeletePath.class)
protected boolean deletePath(Commit<DeletePath> commit) {
String path = commit.operation().path();
init(commit);
NodeHolder parent = null;
NodeHolder node = this.node;
for (String name : path.split(PATH_SEPARATOR)) {
if (!name.equals("")) {
parent = node;
node = node.children.get(name);
if (node == null) {
return false;
}
}
}
if (parent != null) {
parent.children.remove(node.name);
return true;
}
return false;
}
/**
* Filters a delete path commit.
*/
@Filter(value=DeletePath.class, compaction=Compaction.Type.MAJOR)
protected boolean filterDeletePath(Commit<DeletePath> commit, Compaction compaction) {
return commit.index() >= compaction.index();
}
/**
* Applies a create resource commit.
*/
@Apply(CreateResource.class)
protected long createResource(Commit<CreateResource> commit) {
String path = commit.operation().path();
init(commit);
NodeHolder node = this.node;
StringBuilder currentPath = new StringBuilder();
for (String name : path.split(PATH_SEPARATOR)) {
if (!name.equals("")) {
currentPath.append("/").append(name);
NodeHolder child = node.children.get(name);
if (child == null) {
child = new NodeHolder(name, currentPath.toString(), commit.index(), commit.timestamp());
node.children.put(child.name, child);
}
node = child;
}
}
if (node.resource == 0) {
node.resource = commit.index();
try {
StateMachine resource = commit.operation().type().newInstance();
nodes.put(node.resource, node);
resources.put(node.resource, resource);
} catch (InstantiationException | IllegalAccessException e) {
throw new ResourceManagerException("failed to instantiate state machine", e);
}
}
return node.resource;
}
/**
* Filters a create resource commit.
*/
@Filter(CreateResource.class)
protected boolean filterCreateResource(Commit<CreateResource> commit, Compaction compaction) {
return resources.containsKey(commit.index());
}
/**
* Applies a delete resource commit.
*/
@Apply(DeleteResource.class)
protected boolean deleteResource(Commit<DeleteResource> commit) {
init(commit);
NodeHolder node = nodes.remove(commit.operation().resource());
if (node != null) {
node.resource = 0;
}
return resources.remove(commit.operation().resource()) != null;
}
/**
* Filters a delete resource commit.
*/
@Filter(value=DeleteResource.class, compaction=Compaction.Type.MAJOR)
protected boolean filterDeleteResource(Commit<DeleteResource> commit, Compaction compaction) {
return commit.index() >= compaction.index();
}
/**
* Applies an add listener commit.
*/
@Apply(AddListener.class)
protected boolean addListener(Commit<AddListener> commit) {
String path = commit.operation().path();
init(commit);
NodeHolder node = this.node;
for (String name : path.split(PATH_SEPARATOR)) {
if (!name.equals("")) {
node = node.children.get(name);
if (node == null) {
throw new ResourceManagerException("unknown path: " + path);
}
}
}
if (node.listeners.containsKey(commit.session().id())) {
node.listeners.put(commit.session().id(), node.listeners.get(commit.session().id()) + 1);
} else {
node.listeners.put(commit.session().id(), 1);
}
return true;
}
/**
* Applies a remove listener commit.
*/
@Apply(RemoveListener.class)
protected boolean removeListener(Commit<RemoveListener> commit) {
String path = commit.operation().path();
init(commit);
NodeHolder node = this.node;
for (String name : path.split(PATH_SEPARATOR)) {
if (!name.equals("")) {
node = node.children.get(name);
if (node == null) {
throw new ResourceManagerException("unknown path: " + path);
}
}
}
if (node.listeners.containsKey(commit.session().id())) {
int count = node.listeners.get(commit.session().id());
if (count == 1) {
node.listeners.remove(commit.session().id());
} else {
node.listeners.put(commit.session().id(), count - 1);
}
}
return true;
}
/**
* Removes a session from all nodes.
*/
private void removeSession(NodeHolder node, long session) {
node.listeners.remove(session);
for (NodeHolder child : node.children.values()) {
removeSession(child, session);
}
}
@Override
public void register(Session session) {
sessions.put(session.id(), session);
for (StateMachine stateMachine : resources.values()) {
stateMachine.register(session);
}
}
@Override
public void close(Session session) {
sessions.remove(session.id());
removeSession(node, session.id());
for (StateMachine stateMachine : resources.values()) {
stateMachine.close(session);
}
}
@Override
public void expire(Session session) {
sessions.remove(session.id());
removeSession(node, session.id());
for (StateMachine stateMachine : resources.values()) {
stateMachine.expire(session);
}
}
/**
* Node holder.
*/
private static class NodeHolder {
private final String name;
private final String path;
private final long version;
private final long timestamp;
private long resource;
private final Map<Long, Integer> listeners = new HashMap<>();
private final Map<String, NodeHolder> children = new LinkedHashMap<>();
public NodeHolder(String name, String path, long version, long timestamp) {
this.name = name;
this.path = path;
this.version = version;
this.timestamp = timestamp;
}
}
}