-
Notifications
You must be signed in to change notification settings - Fork 1k
/
InputStreamGraphSource.java
396 lines (338 loc) · 14.6 KB
/
InputStreamGraphSource.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
386
387
388
389
390
391
392
393
394
395
396
/* This program is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (props, at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package org.opentripplanner.routing.impl;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.MissingNode;
import com.google.common.io.ByteStreams;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.Graph.LoadLevel;
import org.opentripplanner.routing.services.GraphSource;
import org.opentripplanner.routing.services.StreetVertexIndexFactory;
import org.opentripplanner.standalone.Router;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
/**
* The primary implementation of the GraphSource interface. The graph is loaded from a serialized
* graph from a given source.
*
*/
public class InputStreamGraphSource implements GraphSource {
public static final String GRAPH_FILENAME = "Graph.obj";
private static final Logger LOG = LoggerFactory.getLogger(InputStreamGraphSource.class);
/**
* Delay before starting to load a graph after the last modification time. In case of writing,
* we expect graph last modification time to be updated at at least that frequency. If not, you
* can either increase this value, or use an atomic move when copying the file.
* */
private static final long LOAD_DELAY_SEC = 10;
private Router router;
private String routerId;
private long graphLastModified = 0L;
private LoadLevel loadLevel;
private Object preEvictMutex = new Boolean(false);
/**
* The current used input stream implementation for getting graph data source.
*/
private Streams streams;
// TODO Why do we need a factory? There is a single one implementation.
private StreetVertexIndexFactory streetVertexIndexFactory = new DefaultStreetVertexIndexFactory();
/**
* @param routerId
* @param path
* @param loadLevel
* @return A GraphSource loading graph from the file system under a base path.
*/
public static InputStreamGraphSource newFileGraphSource(String routerId, File path,
LoadLevel loadLevel) {
return new InputStreamGraphSource(routerId, loadLevel, new FileStreams(path));
}
/**
* @param routerId
* @param path
* @param loadLevel
* @return A GraphSource loading graph from an embedded classpath resources (a graph bundled
* inside a pre-packaged WAR for example).
*/
public static InputStreamGraphSource newClasspathGraphSource(String routerId, File path,
LoadLevel loadLevel) {
return new InputStreamGraphSource(routerId, loadLevel, new ClasspathStreams(path));
}
private InputStreamGraphSource(String routerId, LoadLevel loadLevel,
Streams streams) {
this.routerId = routerId;
this.loadLevel = loadLevel;
this.streams = streams;
}
@Override
public Router getRouter() {
/*
* We synchronize on pre-evict mutex in case we are in the middle of reloading in pre-evict
* mode. In that case we must make the client wait until the new graph is loaded, because
* the old one is gone to the GC. Performance hit should be low as getGraph() is not called
* often.
*/
synchronized (preEvictMutex) {
return router;
}
}
@Override
public boolean reload(boolean force, boolean preEvict) {
/* We synchronize on 'this' to prevent multiple reloads from being called at the same time */
synchronized (this) {
long lastModified = streams.getLastModified();
boolean doReload = force ? true : checkAutoReload(lastModified);
if (!doReload)
return true;
if (preEvict) {
synchronized (preEvictMutex) {
if (router != null) {
LOG.info("Reloading '{}': pre-evicting router", routerId);
router.shutdown();
}
/*
* Forcing router to null here should remove any references to the graph once
* all current requests are done. So the next reload is supposed to have more
* memory.
*/
router = null;
router = loadGraph();
}
} else {
Router newRouter = loadGraph();
if (newRouter != null) {
// Load OK
if (router != null) {
LOG.info("Reloading '{}': post-evicting router", routerId);
router.shutdown();
}
router = newRouter; // Assignment in java is atomic
} else {
// Load failed
if (force || router == null) {
LOG.warn("Unable to load data for router '{}'.", routerId);
if (router != null) {
router.shutdown();
}
router = null;
} else {
// No shutdown, since we keep current one.
LOG.warn("Unable to load data for router '{}', keeping old data.", routerId);
}
}
}
if (router == null) {
graphLastModified = 0L;
} else {
/*
* Note: we flag even if loading failed, because we want to wait for fresh new data
* before loading again.
*/
graphLastModified = lastModified;
}
// If a router is null, it will be evicted.
return (router != null);
}
}
/**
* Check if a graph has been modified since the last time it has been loaded.
*
* @param lastModified Time of last modification of current loaded data.
* @return True if the input data has been modified and need to be reloaded.
*/
private boolean checkAutoReload(long lastModified) {
// We check only for graph file modification, not config
long validEndTime = System.currentTimeMillis() - LOAD_DELAY_SEC * 1000;
LOG.debug(
"checkAutoReload router '{}' validEndTime={} lastModified={} graphLastModified={}",
routerId, validEndTime, lastModified, graphLastModified);
if (lastModified != graphLastModified && lastModified <= validEndTime) {
// Only reload graph modified more than 1 mn ago.
LOG.info("Router ID '{}' graph input modification detected, force reload.", routerId);
return true;
} else {
return false;
}
}
@Override
public void evict() {
synchronized (this) {
if (router != null) {
router.shutdown();
router = null;
}
}
}
/**
* Do the actual operation of graph loading. Load configuration if present, and startup the
* router with the help of the router lifecycle manager.
*/
private Router loadGraph() {
final Graph newGraph;
try (InputStream is = streams.getGraphInputStream()) {
LOG.info("Loading graph...");
try {
newGraph = Graph.load(new ObjectInputStream(is), loadLevel,
streetVertexIndexFactory);
} catch (Exception ex) {
LOG.error("Exception while loading graph '{}'.", routerId, ex);
return null;
}
newGraph.routerId = (routerId);
} catch (IOException e) {
LOG.warn("Graph file not found or not openable for routerId '{}': {}", routerId, e);
return null;
}
// Decorate the graph TODO how are we "decorating" it? This appears to refer to loading its configuration.
// Even if a config file is not present on disk one could be bundled inside.
try (InputStream is = streams.getConfigInputStream()) {
JsonNode config = MissingNode.getInstance();
// TODO reuse the exact same JSON loader from OTPConfigurator
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
if (is != null) {
config = mapper.readTree(is);
} else if (newGraph.routerConfig != null) {
config = mapper.readTree(newGraph.routerConfig);
}
Router newRouter = new Router(routerId, newGraph);
newRouter.startup(config);
return newRouter;
} catch (IOException e) {
LOG.error("Can't read config file.");
LOG.error(e.getMessage());
return null;
}
}
/**
* InputStreamGraphSource delegates to some actual implementation the fact of getting the input
* stream and checking the last modification timestamp for a given routerId.
* FIXME this seems like a lot of boilerplate just to switch between FileInputStream and getResourceAsStream
* a couple of conditional blocks and a boolean field "onClasspath" might do the trick.
*/
private interface Streams {
public abstract InputStream getGraphInputStream() throws IOException;
public abstract InputStream getConfigInputStream() throws IOException;
public abstract long getLastModified();
}
private static class FileStreams implements Streams {
private File path;
private FileStreams(File path) {
this.path = path;
}
@Override
public InputStream getGraphInputStream() throws IOException {
File graphFile = new File(path, GRAPH_FILENAME);
LOG.debug("Loading graph from file '{}'", graphFile.getPath());
return new FileInputStream(graphFile);
}
@Override
public InputStream getConfigInputStream() throws IOException {
File configFile = new File(path, Router.ROUTER_CONFIG_FILENAME);
if (configFile.canRead()) {
LOG.debug("Loading config from file '{}'", configFile.getPath());
return new FileInputStream(configFile);
} else {
return null;
}
}
@Override
public long getLastModified() {
// Note: this returns 0L if the file does not exists
return new File(path, GRAPH_FILENAME).lastModified();
}
}
private static class ClasspathStreams implements Streams {
private File path;
private ClasspathStreams(File path) {
this.path = path;
}
@Override
public InputStream getGraphInputStream() {
File graphFile = new File(path, GRAPH_FILENAME);
LOG.debug("Loading graph from classpath at '{}'", graphFile.getPath());
return Thread.currentThread().getContextClassLoader()
.getResourceAsStream(graphFile.getPath());
}
@Override
public InputStream getConfigInputStream() {
File configFile = new File(path, Router.ROUTER_CONFIG_FILENAME);
LOG.debug("Trying to load config on classpath at '{}'", configFile.getPath());
return Thread.currentThread().getContextClassLoader()
.getResourceAsStream(configFile.getPath());
}
/**
* For a packaged classpath resources we assume the data won't change, so returning always
* 0L basically disable auto-reload in that case.
*/
@Override
public long getLastModified() {
return 0L;
}
}
/**
* A GraphSource factory creating InputStreamGraphSource from file.
*/
public static class FileFactory implements GraphSource.Factory {
private static final Logger LOG = LoggerFactory.getLogger(FileFactory.class);
public File basePath;
public LoadLevel loadLevel = LoadLevel.FULL;
public FileFactory(File basePath) {
this.basePath = basePath;
}
@Override
public GraphSource createGraphSource(String routerId) {
return InputStreamGraphSource.newFileGraphSource(routerId, getBasePath(routerId),
loadLevel);
}
@Override
public boolean save(String routerId, InputStream is) {
File sourceFile = new File(getBasePath(routerId), InputStreamGraphSource.GRAPH_FILENAME);
try {
// Create directory if necessary
File directory = new File(sourceFile.getParentFile().getPath());
if (!directory.exists()) {
directory.mkdir();
}
// Store the stream to disk, to be sure no data will be lost make a temporary backup
// file of the original file.
// Make backup file
File destFile = null;
if (sourceFile.exists()) {
destFile = new File(sourceFile.getPath() + ".bak");
if (destFile.exists()) {
destFile.delete();
}
sourceFile.renameTo(destFile);
}
// Store the stream
try (FileOutputStream os = new FileOutputStream(sourceFile)) {
ByteStreams.copy(is, os);
}
// And delete the backup file
sourceFile = new File(sourceFile.getPath() + ".bak");
if (sourceFile.exists()) {
sourceFile.delete();
}
} catch (Exception ex) {
LOG.error("Exception while storing graph to {}.", sourceFile.getPath(), ex);
return false;
}
return true;
}
private File getBasePath(String routerId) {
return new File(basePath, routerId);
}
}
}