1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 Ericsson, École Polytechnique de Montréal, and others
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
10 * Alexandre Montplaisir - Initial API and implementation
11 * Florian Wininger - Add Extension and Leaf Node
12 * Patrick Tasse - Add message to exceptions
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
;
18 import java
.io
.FileInputStream
;
19 import java
.io
.IOException
;
20 import java
.nio
.ByteBuffer
;
21 import java
.nio
.ByteOrder
;
22 import java
.nio
.channels
.ClosedChannelException
;
23 import java
.nio
.channels
.FileChannel
;
24 import java
.util
.ArrayList
;
25 import java
.util
.Collections
;
26 import java
.util
.List
;
28 import org
.eclipse
.jdt
.annotation
.NonNull
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
32 import com
.google
.common
.annotations
.VisibleForTesting
;
33 import com
.google
.common
.collect
.ImmutableList
;
36 * Meta-container for the History Tree. This structure contains all the
37 * high-level data relevant to the tree.
39 * @author Alexandre Montplaisir
41 public class HistoryTreeClassic
implements IHistoryTree
{
44 * The magic number for this file format. Package-private for the factory
47 static final int HISTORY_FILE_MAGIC_NUMBER
= 0x05FFA900;
49 /** File format version. Increment when breaking compatibility. */
50 private static final int FILE_VERSION
= 7;
52 // ------------------------------------------------------------------------
53 // Tree-specific configuration
54 // ------------------------------------------------------------------------
56 /** Container for all the configuration constants */
57 private final HTConfig fConfig
;
59 /** Reader/writer object */
60 private final @NonNull HT_IO fTreeIO
;
62 // ------------------------------------------------------------------------
63 // Variable Fields (will change throughout the existence of the SHT)
64 // ------------------------------------------------------------------------
66 /** Latest timestamp found in the tree (at any given moment) */
67 private long fTreeEnd
;
69 /** The total number of nodes that exists in this tree */
70 private int fNodeCount
;
72 /** "Cache" to keep the active nodes in memory */
73 private final @NonNull List
<@NonNull HTNode
> fLatestBranch
;
75 // ------------------------------------------------------------------------
76 // Constructors/"Destructors"
77 // ------------------------------------------------------------------------
80 * Create a new State History from scratch, using a {@link HTConfig} object
84 * The config to use for this History Tree.
86 * If an error happens trying to open/write to the file
87 * specified in the config
89 public HistoryTreeClassic(HTConfig conf
) throws IOException
{
91 * Simple check to make sure we have enough place in the 0th block for
92 * the tree configuration
94 if (conf
.getBlockSize() < TREE_HEADER_SIZE
) {
95 throw new IllegalArgumentException();
99 fTreeEnd
= conf
.getTreeStart();
101 fLatestBranch
= Collections
.synchronizedList(new ArrayList
<>());
103 /* Prepare the IO object */
104 fTreeIO
= new HT_IO(fConfig
, true);
106 /* Add the first node to the tree */
107 LeafNode firstNode
= initNewLeafNode(-1, conf
.getTreeStart());
108 fLatestBranch
.add(firstNode
);
112 * "Reader" constructor : instantiate a SHTree from an existing tree file on
115 * @param existingStateFile
116 * Path/filename of the history-file we are to open
117 * @param expProviderVersion
118 * The expected version of the state provider
119 * @throws IOException
120 * If an error happens reading the file
122 public HistoryTreeClassic(File existingStateFile
, int expProviderVersion
) throws IOException
{
124 * Open the file ourselves, get the tree header information we need,
125 * then pass on the descriptor to the TreeIO object.
127 int rootNodeSeqNb
, res
;
131 /* Java I/O mumbo jumbo... */
132 if (!existingStateFile
.exists()) {
133 throw new IOException("Selected state file does not exist"); //$NON-NLS-1$
135 if (existingStateFile
.length() <= 0) {
136 throw new IOException("Empty target file"); //$NON-NLS-1$
139 try (FileInputStream fis
= new FileInputStream(existingStateFile
);
140 FileChannel fc
= fis
.getChannel();) {
142 ByteBuffer buffer
= ByteBuffer
.allocate(TREE_HEADER_SIZE
);
144 buffer
.order(ByteOrder
.LITTLE_ENDIAN
);
150 * Check the magic number to make sure we're opening the right type
153 res
= buffer
.getInt();
154 if (res
!= HISTORY_FILE_MAGIC_NUMBER
) {
155 throw new IOException("Wrong magic number"); //$NON-NLS-1$
158 res
= buffer
.getInt(); /* File format version number */
159 if (res
!= FILE_VERSION
) {
160 throw new IOException("Mismatching History Tree file format versions"); //$NON-NLS-1$
163 res
= buffer
.getInt(); /* Event handler's version number */
164 if (res
!= expProviderVersion
&&
165 expProviderVersion
!= ITmfStateSystemBuilder
.IGNORE_PROVIDER_VERSION
) {
167 * The existing history was built using an event handler that
168 * doesn't match the current one in the framework.
170 * Information could be all wrong. Instead of keeping an
171 * incorrect history file, a rebuild is done.
173 throw new IOException("Mismatching event handler versions"); //$NON-NLS-1$
176 bs
= buffer
.getInt(); /* Block Size */
177 maxc
= buffer
.getInt(); /* Max nb of children per node */
179 fNodeCount
= buffer
.getInt();
180 rootNodeSeqNb
= buffer
.getInt();
181 startTime
= buffer
.getLong();
183 fConfig
= new HTConfig(existingStateFile
, bs
, maxc
, expProviderVersion
, startTime
);
187 * FIXME We close fis here and the TreeIO will then reopen the same
188 * file, not extremely elegant. But how to pass the information here to
191 fTreeIO
= new HT_IO(fConfig
, false);
193 fLatestBranch
= buildLatestBranch(rootNodeSeqNb
);
194 fTreeEnd
= getRootNode().getNodeEnd();
197 * Make sure the history start time we read previously is consistent
198 * with was is actually in the root node.
200 if (startTime
!= getRootNode().getNodeStart()) {
201 throw new IOException("Inconsistent start times in the" + //$NON-NLS-1$
202 "history file, it might be corrupted."); //$NON-NLS-1$
207 * Rebuild the latestBranch "cache" object by reading the nodes from disk
208 * (When we are opening an existing file on disk and want to append to it,
211 * @param rootNodeSeqNb
212 * The sequence number of the root node, so we know where to
214 * @throws ClosedChannelException
216 private @NonNull List
<@NonNull HTNode
> buildLatestBranch(int rootNodeSeqNb
) throws ClosedChannelException
{
217 List
<@NonNull HTNode
> list
= new ArrayList
<>();
219 HTNode nextChildNode
= fTreeIO
.readNode(rootNodeSeqNb
);
220 list
.add(nextChildNode
);
222 /* Follow the last branch up to the leaf */
223 while (nextChildNode
.getNodeType() == HTNode
.NodeType
.CORE
) {
224 nextChildNode
= fTreeIO
.readNode(((CoreNode
) nextChildNode
).getLatestChild());
225 list
.add(nextChildNode
);
227 return Collections
.synchronizedList(list
);
231 public void closeTree(long requestedEndTime
) {
232 /* This is an important operation, queries can wait */
233 synchronized (fLatestBranch
) {
235 * Work-around the "empty branches" that get created when the root
236 * node becomes full. Overwrite the tree's end time with the
237 * original wanted end-time, to ensure no queries are sent into
240 * This won't be needed once extended nodes are implemented.
242 fTreeEnd
= requestedEndTime
;
244 /* Close off the latest branch of the tree */
245 for (int i
= 0; i
< fLatestBranch
.size(); i
++) {
246 fLatestBranch
.get(i
).closeThisNode(fTreeEnd
);
247 fTreeIO
.writeNode(fLatestBranch
.get(i
));
250 try (FileChannel fc
= fTreeIO
.getFcOut();) {
251 ByteBuffer buffer
= ByteBuffer
.allocate(TREE_HEADER_SIZE
);
252 buffer
.order(ByteOrder
.LITTLE_ENDIAN
);
255 /* Save the config of the tree to the header of the file */
258 buffer
.putInt(HISTORY_FILE_MAGIC_NUMBER
);
260 buffer
.putInt(FILE_VERSION
);
261 buffer
.putInt(fConfig
.getProviderVersion());
263 buffer
.putInt(fConfig
.getBlockSize());
264 buffer
.putInt(fConfig
.getMaxChildren());
266 buffer
.putInt(fNodeCount
);
268 /* root node seq. nb */
269 buffer
.putInt(fLatestBranch
.get(0).getSequenceNumber());
271 /* start time of this history */
272 buffer
.putLong(fLatestBranch
.get(0).getNodeStart());
275 int res
= fc
.write(buffer
);
276 assert (res
<= TREE_HEADER_SIZE
);
277 /* done writing the file header */
279 } catch (IOException e
) {
281 * If we were able to write so far, there should not be any
282 * problem at this point...
284 throw new RuntimeException("State system write error"); //$NON-NLS-1$
289 // ------------------------------------------------------------------------
291 // ------------------------------------------------------------------------
294 public long getTreeStart() {
295 return fConfig
.getTreeStart();
299 public long getTreeEnd() {
304 public int getNodeCount() {
309 public HTNode
getRootNode() {
310 return fLatestBranch
.get(0);
314 * Return the latest branch of the tree. That branch is immutable. Used for
315 * unit testing and debugging.
317 * @return The immutable latest branch
320 protected List
<@NonNull HTNode
> getLatestBranch() {
321 return ImmutableList
.copyOf(fLatestBranch
);
325 * Read a node at sequence number
328 * The sequence number of the node to read
329 * @return The HTNode object
330 * @throws ClosedChannelException
331 * Exception thrown when reading the node
334 protected @NonNull HTNode
getNode(int seqNum
) throws ClosedChannelException
{
335 // First, check in the latest branch if the node is there
336 for (HTNode node
: fLatestBranch
) {
337 if (node
.getSequenceNumber() == seqNum
) {
341 return fTreeIO
.readNode(seqNum
);
345 * Retrieve the TreeIO object. Should only be used for testing.
350 protected @NonNull HT_IO
getTreeIO() {
354 // ------------------------------------------------------------------------
356 // ------------------------------------------------------------------------
359 public FileInputStream
supplyATReader() {
360 return fTreeIO
.supplyATReader(getNodeCount());
364 public File
supplyATWriterFile() {
365 return fConfig
.getStateFile();
369 public long supplyATWriterFilePos() {
370 return IHistoryTree
.TREE_HEADER_SIZE
371 + ((long) getNodeCount() * fConfig
.getBlockSize());
375 public HTNode
readNode(int seqNumber
) throws ClosedChannelException
{
376 /* Try to read the node from memory */
377 synchronized (fLatestBranch
) {
378 for (HTNode node
: fLatestBranch
) {
379 if (node
.getSequenceNumber() == seqNumber
) {
385 /* Read the node from disk */
386 return fTreeIO
.readNode(seqNumber
);
390 public void writeNode(HTNode node
) {
391 fTreeIO
.writeNode(node
);
395 public void closeFile() {
400 public void deleteFile() {
401 fTreeIO
.deleteFile();
404 // ------------------------------------------------------------------------
406 // ------------------------------------------------------------------------
409 public void insertInterval(HTInterval interval
) throws TimeRangeException
{
410 if (interval
.getStartTime() < fConfig
.getTreeStart()) {
411 throw new TimeRangeException("Interval Start:" + interval
.getStartTime() + ", Config Start:" + fConfig
.getTreeStart()); //$NON-NLS-1$ //$NON-NLS-2$
413 tryInsertAtNode(interval
, fLatestBranch
.size() - 1);
417 * Inner method to find in which node we should add the interval.
420 * The interval to add to the tree
422 * The index *in the latestBranch* where we are trying the
425 private void tryInsertAtNode(HTInterval interval
, int indexOfNode
) {
426 HTNode targetNode
= fLatestBranch
.get(indexOfNode
);
428 /* Verify if there is enough room in this node to store this interval */
429 if (interval
.getSizeOnDisk() > targetNode
.getNodeFreeSpace()) {
430 /* Nope, not enough room. Insert in a new sibling instead. */
431 addSiblingNode(indexOfNode
);
432 tryInsertAtNode(interval
, fLatestBranch
.size() - 1);
436 /* Make sure the interval time range fits this node */
437 if (interval
.getStartTime() < targetNode
.getNodeStart()) {
439 * No, this interval starts before the startTime of this node. We
440 * need to check recursively in parents if it can fit.
442 tryInsertAtNode(interval
, indexOfNode
- 1);
447 * Ok, there is room, and the interval fits in this time slot. Let's add
450 targetNode
.addInterval(interval
);
452 /* Update treeEnd if needed */
453 if (interval
.getEndTime() > fTreeEnd
) {
454 fTreeEnd
= interval
.getEndTime();
459 * Method to add a sibling to any node in the latest branch. This will add
460 * children back down to the leaf level, if needed.
463 * The index in latestBranch where we start adding
465 private void addSiblingNode(int indexOfNode
) {
466 synchronized (fLatestBranch
) {
467 final long splitTime
= fTreeEnd
;
469 if (indexOfNode
>= fLatestBranch
.size()) {
471 * We need to make sure (indexOfNode - 1) doesn't get the last
472 * node in the branch, because that one is a Leaf Node.
474 throw new IllegalStateException();
477 /* Check if we need to add a new root node */
478 if (indexOfNode
== 0) {
483 /* Check if we can indeed add a child to the target parent */
484 if (((CoreNode
) fLatestBranch
.get(indexOfNode
- 1)).getNbChildren() == fConfig
.getMaxChildren()) {
485 /* If not, add a branch starting one level higher instead */
486 addSiblingNode(indexOfNode
- 1);
490 /* Split off the new branch from the old one */
491 for (int i
= indexOfNode
; i
< fLatestBranch
.size(); i
++) {
492 fLatestBranch
.get(i
).closeThisNode(splitTime
);
493 fTreeIO
.writeNode(fLatestBranch
.get(i
));
495 CoreNode prevNode
= (CoreNode
) fLatestBranch
.get(i
- 1);
498 switch (fLatestBranch
.get(i
).getNodeType()) {
500 newNode
= initNewCoreNode(prevNode
.getSequenceNumber(), splitTime
+ 1);
503 newNode
= initNewLeafNode(prevNode
.getSequenceNumber(), splitTime
+ 1);
506 throw new IllegalStateException();
509 prevNode
.linkNewChild(newNode
);
510 fLatestBranch
.set(i
, newNode
);
516 * Similar to the previous method, except here we rebuild a completely new
519 private void addNewRootNode() {
520 final long splitTime
= fTreeEnd
;
522 HTNode oldRootNode
= fLatestBranch
.get(0);
523 CoreNode newRootNode
= initNewCoreNode(-1, fConfig
.getTreeStart());
525 /* Tell the old root node that it isn't root anymore */
526 oldRootNode
.setParentSequenceNumber(newRootNode
.getSequenceNumber());
528 /* Close off the whole current latestBranch */
530 for (int i
= 0; i
< fLatestBranch
.size(); i
++) {
531 fLatestBranch
.get(i
).closeThisNode(splitTime
);
532 fTreeIO
.writeNode(fLatestBranch
.get(i
));
535 /* Link the new root to its first child (the previous root node) */
536 newRootNode
.linkNewChild(oldRootNode
);
538 /* Rebuild a new latestBranch */
539 int depth
= fLatestBranch
.size();
540 fLatestBranch
.clear();
541 fLatestBranch
.add(newRootNode
);
543 // Create new coreNode
544 for (int i
= 1; i
< depth
; i
++) {
545 CoreNode prevNode
= (CoreNode
) fLatestBranch
.get(i
- 1);
546 CoreNode newNode
= initNewCoreNode(prevNode
.getSequenceNumber(),
548 prevNode
.linkNewChild(newNode
);
549 fLatestBranch
.add(newNode
);
552 // Create the new leafNode
553 CoreNode prevNode
= (CoreNode
) fLatestBranch
.get(depth
- 1);
554 LeafNode newNode
= initNewLeafNode(prevNode
.getSequenceNumber(), splitTime
+ 1);
555 prevNode
.linkNewChild(newNode
);
556 fLatestBranch
.add(newNode
);
560 * Add a new empty core node to the tree.
562 * @param parentSeqNumber
563 * Sequence number of this node's parent
565 * Start time of the new node
566 * @return The newly created node
568 private @NonNull CoreNode
initNewCoreNode(int parentSeqNumber
, long startTime
) {
569 CoreNode newNode
= new CoreNode(fConfig
, fNodeCount
, parentSeqNumber
,
576 * Add a new empty leaf node to the tree.
578 * @param parentSeqNumber
579 * Sequence number of this node's parent
581 * Start time of the new node
582 * @return The newly created node
584 private @NonNull LeafNode
initNewLeafNode(int parentSeqNumber
, long startTime
) {
585 LeafNode newNode
= new LeafNode(fConfig
, fNodeCount
, parentSeqNumber
,
592 public HTNode
selectNextChild(CoreNode currentNode
, long t
) throws ClosedChannelException
{
593 assert (currentNode
.getNbChildren() > 0);
594 int potentialNextSeqNb
= currentNode
.getSequenceNumber();
596 for (int i
= 0; i
< currentNode
.getNbChildren(); i
++) {
597 if (t
>= currentNode
.getChildStart(i
)) {
598 potentialNextSeqNb
= currentNode
.getChild(i
);
605 * Once we exit this loop, we should have found a children to follow. If
606 * we didn't, there's a problem.
608 if (potentialNextSeqNb
== currentNode
.getSequenceNumber()) {
609 throw new IllegalStateException("No next child node found"); //$NON-NLS-1$
613 * Since this code path is quite performance-critical, avoid iterating
614 * through the whole latestBranch array if we know for sure the next
615 * node has to be on disk
617 if (currentNode
.isOnDisk()) {
618 return fTreeIO
.readNode(potentialNextSeqNb
);
620 return readNode(potentialNextSeqNb
);
624 public long getFileSize() {
625 return fConfig
.getStateFile().length();
628 // ------------------------------------------------------------------------
629 // Test/debugging methods
630 // ------------------------------------------------------------------------
632 /* Only used for debugging, shouldn't be externalized */
633 @SuppressWarnings("nls")
635 public String
toString() {
636 return "Information on the current tree:\n\n" + "Blocksize: "
637 + fConfig
.getBlockSize() + "\n" + "Max nb. of children per node: "
638 + fConfig
.getMaxChildren() + "\n" + "Number of nodes: " + fNodeCount
639 + "\n" + "Depth of the tree: " + fLatestBranch
.size() + "\n"
640 + "Size of the treefile: " + getFileSize() + "\n"
641 + "Root node has sequence number: "
642 + fLatestBranch
.get(0).getSequenceNumber() + "\n"
643 + "'Latest leaf' has sequence number: "
644 + fLatestBranch
.get(fLatestBranch
.size() - 1).getSequenceNumber();