ss: Extract an history tree interface
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / internal / statesystem / core / backend / historytree / HistoryTreeClassic.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 Ericsson, École Polytechnique de Montréal, and others
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 * Florian Wininger - Add Extension and Leaf Node
12 * Patrick Tasse - Add message to exceptions
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.internal.statesystem.core.backend.historytree;
16
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.nio.ByteBuffer;
22 import java.nio.ByteOrder;
23 import java.nio.channels.ClosedChannelException;
24 import java.nio.channels.FileChannel;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.eclipse.tracecompass.internal.statesystem.core.Activator;
31 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
32 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
33
34 import com.google.common.annotations.VisibleForTesting;
35 import com.google.common.collect.ImmutableList;
36
37 /**
38 * Meta-container for the History Tree. This structure contains all the
39 * high-level data relevant to the tree.
40 *
41 * @author Alexandre Montplaisir
42 */
43 public class HistoryTreeClassic implements IHistoryTree {
44
45 /**
46 * The magic number for this file format. Package-private for the factory
47 * class.
48 */
49 static final int HISTORY_FILE_MAGIC_NUMBER = 0x05FFA900;
50
51 /** File format version. Increment when breaking compatibility. */
52 private static final int FILE_VERSION = 7;
53
54 // ------------------------------------------------------------------------
55 // Tree-specific configuration
56 // ------------------------------------------------------------------------
57
58 /** Container for all the configuration constants */
59 private final HTConfig fConfig;
60
61 /** Reader/writer object */
62 private final HT_IO fTreeIO;
63
64 // ------------------------------------------------------------------------
65 // Variable Fields (will change throughout the existence of the SHT)
66 // ------------------------------------------------------------------------
67
68 /** Latest timestamp found in the tree (at any given moment) */
69 private long fTreeEnd;
70
71 /** The total number of nodes that exists in this tree */
72 private int fNodeCount;
73
74 /** "Cache" to keep the active nodes in memory */
75 private final @NonNull List<@NonNull HTNode> fLatestBranch;
76
77 // ------------------------------------------------------------------------
78 // Constructors/"Destructors"
79 // ------------------------------------------------------------------------
80
81 /**
82 * Create a new State History from scratch, using a {@link HTConfig} object
83 * for configuration.
84 *
85 * @param conf
86 * The config to use for this History Tree.
87 * @throws IOException
88 * If an error happens trying to open/write to the file
89 * specified in the config
90 */
91 public HistoryTreeClassic(HTConfig conf) throws IOException {
92 /*
93 * Simple check to make sure we have enough place in the 0th block for
94 * the tree configuration
95 */
96 if (conf.getBlockSize() < TREE_HEADER_SIZE) {
97 throw new IllegalArgumentException();
98 }
99
100 fConfig = conf;
101 fTreeEnd = conf.getTreeStart();
102 fNodeCount = 0;
103 fLatestBranch = Collections.synchronizedList(new ArrayList<>());
104
105 /* Prepare the IO object */
106 fTreeIO = new HT_IO(fConfig, true);
107
108 /* Add the first node to the tree */
109 LeafNode firstNode = initNewLeafNode(-1, conf.getTreeStart());
110 fLatestBranch.add(firstNode);
111 }
112
113 /**
114 * "Reader" constructor : instantiate a SHTree from an existing tree file on
115 * disk
116 *
117 * @param existingStateFile
118 * Path/filename of the history-file we are to open
119 * @param expProviderVersion
120 * The expected version of the state provider
121 * @throws IOException
122 * If an error happens reading the file
123 */
124 public HistoryTreeClassic(File existingStateFile, int expProviderVersion) throws IOException {
125 /*
126 * Open the file ourselves, get the tree header information we need,
127 * then pass on the descriptor to the TreeIO object.
128 */
129 int rootNodeSeqNb, res;
130 int bs, maxc;
131 long startTime;
132
133 /* Java I/O mumbo jumbo... */
134 if (!existingStateFile.exists()) {
135 throw new IOException("Selected state file does not exist"); //$NON-NLS-1$
136 }
137 if (existingStateFile.length() <= 0) {
138 throw new IOException("Empty target file"); //$NON-NLS-1$
139 }
140
141 try (FileInputStream fis = new FileInputStream(existingStateFile);
142 FileChannel fc = fis.getChannel();) {
143
144 ByteBuffer buffer = ByteBuffer.allocate(TREE_HEADER_SIZE);
145
146 buffer.order(ByteOrder.LITTLE_ENDIAN);
147 buffer.clear();
148 fc.read(buffer);
149 buffer.flip();
150
151 /*
152 * Check the magic number to make sure we're opening the right type
153 * of file
154 */
155 res = buffer.getInt();
156 if (res != HISTORY_FILE_MAGIC_NUMBER) {
157 throw new IOException("Wrong magic number"); //$NON-NLS-1$
158 }
159
160 res = buffer.getInt(); /* File format version number */
161 if (res != FILE_VERSION) {
162 throw new IOException("Mismatching History Tree file format versions"); //$NON-NLS-1$
163 }
164
165 res = buffer.getInt(); /* Event handler's version number */
166 if (res != expProviderVersion &&
167 expProviderVersion != ITmfStateSystemBuilder.IGNORE_PROVIDER_VERSION) {
168 /*
169 * The existing history was built using an event handler that
170 * doesn't match the current one in the framework.
171 *
172 * Information could be all wrong. Instead of keeping an
173 * incorrect history file, a rebuild is done.
174 */
175 throw new IOException("Mismatching event handler versions"); //$NON-NLS-1$
176 }
177
178 bs = buffer.getInt(); /* Block Size */
179 maxc = buffer.getInt(); /* Max nb of children per node */
180
181 fNodeCount = buffer.getInt();
182 rootNodeSeqNb = buffer.getInt();
183 startTime = buffer.getLong();
184
185 fConfig = new HTConfig(existingStateFile, bs, maxc, expProviderVersion, startTime);
186 }
187
188 /*
189 * FIXME We close fis here and the TreeIO will then reopen the same
190 * file, not extremely elegant. But how to pass the information here to
191 * the SHT otherwise?
192 */
193 fTreeIO = new HT_IO(fConfig, false);
194
195 fLatestBranch = buildLatestBranch(rootNodeSeqNb);
196 fTreeEnd = getRootNode().getNodeEnd();
197
198 /*
199 * Make sure the history start time we read previously is consistent
200 * with was is actually in the root node.
201 */
202 if (startTime != getRootNode().getNodeStart()) {
203 throw new IOException("Inconsistent start times in the" + //$NON-NLS-1$
204 "history file, it might be corrupted."); //$NON-NLS-1$
205 }
206 }
207
208 /**
209 * Rebuild the latestBranch "cache" object by reading the nodes from disk
210 * (When we are opening an existing file on disk and want to append to it,
211 * for example).
212 *
213 * @param rootNodeSeqNb
214 * The sequence number of the root node, so we know where to
215 * start
216 * @throws ClosedChannelException
217 */
218 private @NonNull List<@NonNull HTNode> buildLatestBranch(int rootNodeSeqNb) throws ClosedChannelException {
219 List<@NonNull HTNode> list = new ArrayList<>();
220
221 HTNode nextChildNode = fTreeIO.readNode(rootNodeSeqNb);
222 list.add(nextChildNode);
223
224 /* Follow the last branch up to the leaf */
225 while (nextChildNode.getNodeType() == HTNode.NodeType.CORE) {
226 nextChildNode = fTreeIO.readNode(((CoreNode) nextChildNode).getLatestChild());
227 list.add(nextChildNode);
228 }
229 return Collections.synchronizedList(list);
230 }
231
232 @Override
233 public void closeTree(long requestedEndTime) {
234 /* This is an important operation, queries can wait */
235 synchronized (fLatestBranch) {
236 /*
237 * Work-around the "empty branches" that get created when the root
238 * node becomes full. Overwrite the tree's end time with the
239 * original wanted end-time, to ensure no queries are sent into
240 * those empty nodes.
241 *
242 * This won't be needed once extended nodes are implemented.
243 */
244 fTreeEnd = requestedEndTime;
245
246 /* Close off the latest branch of the tree */
247 for (int i = 0; i < fLatestBranch.size(); i++) {
248 fLatestBranch.get(i).closeThisNode(fTreeEnd);
249 fTreeIO.writeNode(fLatestBranch.get(i));
250 }
251
252 try (FileChannel fc = fTreeIO.getFcOut();) {
253 ByteBuffer buffer = ByteBuffer.allocate(TREE_HEADER_SIZE);
254 buffer.order(ByteOrder.LITTLE_ENDIAN);
255 buffer.clear();
256
257 /* Save the config of the tree to the header of the file */
258 fc.position(0);
259
260 buffer.putInt(HISTORY_FILE_MAGIC_NUMBER);
261
262 buffer.putInt(FILE_VERSION);
263 buffer.putInt(fConfig.getProviderVersion());
264
265 buffer.putInt(fConfig.getBlockSize());
266 buffer.putInt(fConfig.getMaxChildren());
267
268 buffer.putInt(fNodeCount);
269
270 /* root node seq. nb */
271 buffer.putInt(fLatestBranch.get(0).getSequenceNumber());
272
273 /* start time of this history */
274 buffer.putLong(fLatestBranch.get(0).getNodeStart());
275
276 buffer.flip();
277 int res = fc.write(buffer);
278 assert (res <= TREE_HEADER_SIZE);
279 /* done writing the file header */
280
281 } catch (IOException e) {
282 /*
283 * If we were able to write so far, there should not be any
284 * problem at this point...
285 */
286 throw new RuntimeException("State system write error"); //$NON-NLS-1$
287 }
288 }
289 }
290
291 // ------------------------------------------------------------------------
292 // Accessors
293 // ------------------------------------------------------------------------
294
295 @Override
296 public long getTreeStart() {
297 return fConfig.getTreeStart();
298 }
299
300 @Override
301 public long getTreeEnd() {
302 return fTreeEnd;
303 }
304
305 @Override
306 public int getNodeCount() {
307 return fNodeCount;
308 }
309
310 @Override
311 public HTNode getRootNode() {
312 return fLatestBranch.get(0);
313 }
314
315 /**
316 * Return the latest branch of the tree. That branch is immutable. Used for
317 * unit testing and debugging.
318 *
319 * @return The immutable latest branch
320 */
321 @VisibleForTesting
322 protected List<@NonNull HTNode> getLatestBranch() {
323 return ImmutableList.copyOf(fLatestBranch);
324 }
325
326 /**
327 * Read a node at sequence number
328 *
329 * @param seqNum
330 * The sequence number of the node to read
331 * @return The HTNode object
332 * @throws ClosedChannelException
333 * Exception thrown when reading the node
334 */
335 @VisibleForTesting
336 protected @NonNull HTNode getNode(int seqNum) throws ClosedChannelException {
337 // First, check in the latest branch if the node is there
338 for (HTNode node : fLatestBranch) {
339 if (node.getSequenceNumber() == seqNum) {
340 return node;
341 }
342 }
343 return fTreeIO.readNode(seqNum);
344 }
345
346 // ------------------------------------------------------------------------
347 // HT_IO interface
348 // ------------------------------------------------------------------------
349
350 @Override
351 public FileInputStream supplyATReader() {
352 return fTreeIO.supplyATReader(getNodeCount());
353 }
354
355 @Override
356 public File supplyATWriterFile() {
357 return fConfig.getStateFile();
358 }
359
360 @Override
361 public long supplyATWriterFilePos() {
362 return IHistoryTree.TREE_HEADER_SIZE
363 + ((long) getNodeCount() * fConfig.getBlockSize());
364 }
365
366 @Override
367 public HTNode readNode(int seqNumber) throws ClosedChannelException {
368 /* Try to read the node from memory */
369 synchronized (fLatestBranch) {
370 for (HTNode node : fLatestBranch) {
371 if (node.getSequenceNumber() == seqNumber) {
372 return node;
373 }
374 }
375 }
376
377 /* Read the node from disk */
378 return fTreeIO.readNode(seqNumber);
379 }
380
381 @Override
382 public void writeNode(HTNode node) {
383 fTreeIO.writeNode(node);
384 }
385
386 @Override
387 public void closeFile() {
388 fTreeIO.closeFile();
389 }
390
391 @Override
392 public void deleteFile() {
393 fTreeIO.deleteFile();
394 }
395
396 // ------------------------------------------------------------------------
397 // Operations
398 // ------------------------------------------------------------------------
399
400 @Override
401 public void insertInterval(HTInterval interval) throws TimeRangeException {
402 if (interval.getStartTime() < fConfig.getTreeStart()) {
403 throw new TimeRangeException("Interval Start:" + interval.getStartTime() + ", Config Start:" + fConfig.getTreeStart()); //$NON-NLS-1$ //$NON-NLS-2$
404 }
405 tryInsertAtNode(interval, fLatestBranch.size() - 1);
406 }
407
408 /**
409 * Inner method to find in which node we should add the interval.
410 *
411 * @param interval
412 * The interval to add to the tree
413 * @param indexOfNode
414 * The index *in the latestBranch* where we are trying the
415 * insertion
416 */
417 private void tryInsertAtNode(HTInterval interval, int indexOfNode) {
418 HTNode targetNode = fLatestBranch.get(indexOfNode);
419
420 /* Verify if there is enough room in this node to store this interval */
421 if (interval.getSizeOnDisk() > targetNode.getNodeFreeSpace()) {
422 /* Nope, not enough room. Insert in a new sibling instead. */
423 addSiblingNode(indexOfNode);
424 tryInsertAtNode(interval, fLatestBranch.size() - 1);
425 return;
426 }
427
428 /* Make sure the interval time range fits this node */
429 if (interval.getStartTime() < targetNode.getNodeStart()) {
430 /*
431 * No, this interval starts before the startTime of this node. We
432 * need to check recursively in parents if it can fit.
433 */
434 tryInsertAtNode(interval, indexOfNode - 1);
435 return;
436 }
437
438 /*
439 * Ok, there is room, and the interval fits in this time slot. Let's add
440 * it.
441 */
442 targetNode.addInterval(interval);
443
444 /* Update treeEnd if needed */
445 if (interval.getEndTime() > fTreeEnd) {
446 fTreeEnd = interval.getEndTime();
447 }
448 }
449
450 /**
451 * Method to add a sibling to any node in the latest branch. This will add
452 * children back down to the leaf level, if needed.
453 *
454 * @param indexOfNode
455 * The index in latestBranch where we start adding
456 */
457 private void addSiblingNode(int indexOfNode) {
458 synchronized (fLatestBranch) {
459 final long splitTime = fTreeEnd;
460
461 if (indexOfNode >= fLatestBranch.size()) {
462 /*
463 * We need to make sure (indexOfNode - 1) doesn't get the last
464 * node in the branch, because that one is a Leaf Node.
465 */
466 throw new IllegalStateException();
467 }
468
469 /* Check if we need to add a new root node */
470 if (indexOfNode == 0) {
471 addNewRootNode();
472 return;
473 }
474
475 /* Check if we can indeed add a child to the target parent */
476 if (((CoreNode) fLatestBranch.get(indexOfNode - 1)).getNbChildren() == fConfig.getMaxChildren()) {
477 /* If not, add a branch starting one level higher instead */
478 addSiblingNode(indexOfNode - 1);
479 return;
480 }
481
482 /* Split off the new branch from the old one */
483 for (int i = indexOfNode; i < fLatestBranch.size(); i++) {
484 fLatestBranch.get(i).closeThisNode(splitTime);
485 fTreeIO.writeNode(fLatestBranch.get(i));
486
487 CoreNode prevNode = (CoreNode) fLatestBranch.get(i - 1);
488 HTNode newNode;
489
490 switch (fLatestBranch.get(i).getNodeType()) {
491 case CORE:
492 newNode = initNewCoreNode(prevNode.getSequenceNumber(), splitTime + 1);
493 break;
494 case LEAF:
495 newNode = initNewLeafNode(prevNode.getSequenceNumber(), splitTime + 1);
496 break;
497 default:
498 throw new IllegalStateException();
499 }
500
501 prevNode.linkNewChild(newNode);
502 fLatestBranch.set(i, newNode);
503 }
504 }
505 }
506
507 /**
508 * Similar to the previous method, except here we rebuild a completely new
509 * latestBranch
510 */
511 private void addNewRootNode() {
512 final long splitTime = fTreeEnd;
513
514 HTNode oldRootNode = fLatestBranch.get(0);
515 CoreNode newRootNode = initNewCoreNode(-1, fConfig.getTreeStart());
516
517 /* Tell the old root node that it isn't root anymore */
518 oldRootNode.setParentSequenceNumber(newRootNode.getSequenceNumber());
519
520 /* Close off the whole current latestBranch */
521
522 for (int i = 0; i < fLatestBranch.size(); i++) {
523 fLatestBranch.get(i).closeThisNode(splitTime);
524 fTreeIO.writeNode(fLatestBranch.get(i));
525 }
526
527 /* Link the new root to its first child (the previous root node) */
528 newRootNode.linkNewChild(oldRootNode);
529
530 /* Rebuild a new latestBranch */
531 int depth = fLatestBranch.size();
532 fLatestBranch.clear();
533 fLatestBranch.add(newRootNode);
534
535 // Create new coreNode
536 for (int i = 1; i < depth; i++) {
537 CoreNode prevNode = (CoreNode) fLatestBranch.get(i - 1);
538 CoreNode newNode = initNewCoreNode(prevNode.getSequenceNumber(),
539 splitTime + 1);
540 prevNode.linkNewChild(newNode);
541 fLatestBranch.add(newNode);
542 }
543
544 // Create the new leafNode
545 CoreNode prevNode = (CoreNode) fLatestBranch.get(depth - 1);
546 LeafNode newNode = initNewLeafNode(prevNode.getSequenceNumber(), splitTime + 1);
547 prevNode.linkNewChild(newNode);
548 fLatestBranch.add(newNode);
549 }
550
551 /**
552 * Add a new empty core node to the tree.
553 *
554 * @param parentSeqNumber
555 * Sequence number of this node's parent
556 * @param startTime
557 * Start time of the new node
558 * @return The newly created node
559 */
560 private @NonNull CoreNode initNewCoreNode(int parentSeqNumber, long startTime) {
561 CoreNode newNode = new CoreNode(fConfig, fNodeCount, parentSeqNumber,
562 startTime);
563 fNodeCount++;
564 return newNode;
565 }
566
567 /**
568 * Add a new empty leaf node to the tree.
569 *
570 * @param parentSeqNumber
571 * Sequence number of this node's parent
572 * @param startTime
573 * Start time of the new node
574 * @return The newly created node
575 */
576 private @NonNull LeafNode initNewLeafNode(int parentSeqNumber, long startTime) {
577 LeafNode newNode = new LeafNode(fConfig, fNodeCount, parentSeqNumber,
578 startTime);
579 fNodeCount++;
580 return newNode;
581 }
582
583 @Override
584 public HTNode selectNextChild(CoreNode currentNode, long t) throws ClosedChannelException {
585 assert (currentNode.getNbChildren() > 0);
586 int potentialNextSeqNb = currentNode.getSequenceNumber();
587
588 for (int i = 0; i < currentNode.getNbChildren(); i++) {
589 if (t >= currentNode.getChildStart(i)) {
590 potentialNextSeqNb = currentNode.getChild(i);
591 } else {
592 break;
593 }
594 }
595
596 /*
597 * Once we exit this loop, we should have found a children to follow. If
598 * we didn't, there's a problem.
599 */
600 if (potentialNextSeqNb == currentNode.getSequenceNumber()) {
601 throw new IllegalStateException("No next child node found"); //$NON-NLS-1$
602 }
603
604 /*
605 * Since this code path is quite performance-critical, avoid iterating
606 * through the whole latestBranch array if we know for sure the next
607 * node has to be on disk
608 */
609 if (currentNode.isOnDisk()) {
610 return fTreeIO.readNode(potentialNextSeqNb);
611 }
612 return readNode(potentialNextSeqNb);
613 }
614
615 @Override
616 public long getFileSize() {
617 return fConfig.getStateFile().length();
618 }
619
620 // ------------------------------------------------------------------------
621 // Test/debugging methods
622 // ------------------------------------------------------------------------
623
624 /* Only used for debugging, shouldn't be externalized */
625 @SuppressWarnings("nls")
626 @Override
627 public String toString() {
628 return "Information on the current tree:\n\n" + "Blocksize: "
629 + fConfig.getBlockSize() + "\n" + "Max nb. of children per node: "
630 + fConfig.getMaxChildren() + "\n" + "Number of nodes: " + fNodeCount
631 + "\n" + "Depth of the tree: " + fLatestBranch.size() + "\n"
632 + "Size of the treefile: " + getFileSize() + "\n"
633 + "Root node has sequence number: "
634 + fLatestBranch.get(0).getSequenceNumber() + "\n"
635 + "'Latest leaf' has sequence number: "
636 + fLatestBranch.get(fLatestBranch.size() - 1).getSequenceNumber();
637 }
638
639 /**
640 * Start at currentNode and print the contents of all its children, in
641 * pre-order. Give the root node in parameter to visit the whole tree, and
642 * have a nice overview.
643 */
644 /* Only used for debugging, shouldn't be externalized */
645 @SuppressWarnings("nls")
646 private void preOrderPrint(PrintWriter writer, boolean printIntervals,
647 HTNode currentNode, int curDepth, long ts) {
648
649 writer.println(currentNode.toString());
650 /* Print intervals only if timestamp is negative or within the range of the node */
651 if (printIntervals &&
652 (ts <= 0 ||
653 (ts >= currentNode.getNodeStart() && ts <= currentNode.getNodeEnd()))) {
654 currentNode.debugPrintIntervals(writer);
655 }
656
657 switch (currentNode.getNodeType()) {
658 case LEAF:
659 /* Stop if it's the leaf node */
660 return;
661
662 case CORE:
663 try {
664 final CoreNode node = (CoreNode) currentNode;
665 /* Print the extensions, if any */
666 int extension = node.getExtensionSequenceNumber();
667 while (extension != -1) {
668 HTNode nextNode = fTreeIO.readNode(extension);
669 preOrderPrint(writer, printIntervals, nextNode, curDepth, ts);
670 }
671
672 /* Print the child nodes */
673 for (int i = 0; i < node.getNbChildren(); i++) {
674 HTNode nextNode = fTreeIO.readNode(node.getChild(i));
675 for (int j = 0; j < curDepth; j++) {
676 writer.print(" ");
677 }
678 writer.print("+-");
679 preOrderPrint(writer, printIntervals, nextNode, curDepth + 1, ts);
680 }
681 } catch (ClosedChannelException e) {
682 Activator.getDefault().logError(e.getMessage());
683 }
684 break;
685
686 default:
687 break;
688 }
689 }
690
691 @Override
692 public void debugPrintFullTree(PrintWriter writer, boolean printIntervals, long ts) {
693 /* Only used for debugging, shouldn't be externalized */
694
695 preOrderPrint(writer, false, fLatestBranch.get(0), 0, ts);
696
697 if (printIntervals) {
698 preOrderPrint(writer, true, fLatestBranch.get(0), 0, ts);
699 }
700 writer.println('\n');
701 }
702
703 }
This page took 0.046825 seconds and 5 git commands to generate.