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
.Collection
;
26 import java
.util
.Collections
;
27 import java
.util
.List
;
29 import org
.eclipse
.jdt
.annotation
.NonNull
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
33 import com
.google
.common
.annotations
.VisibleForTesting
;
34 import com
.google
.common
.collect
.ImmutableList
;
37 * Meta-container for the History Tree. This structure contains all the
38 * high-level data relevant to the tree.
40 * @author Alexandre Montplaisir
42 public class HistoryTreeClassic
implements IHistoryTree
{
45 * The magic number for this file format. Package-private for the factory
48 static final int HISTORY_FILE_MAGIC_NUMBER
= 0x05FFA900;
50 /** File format version. Increment when breaking compatibility. */
51 private static final int FILE_VERSION
= 7;
53 // ------------------------------------------------------------------------
54 // Tree-specific configuration
55 // ------------------------------------------------------------------------
57 /** Container for all the configuration constants */
58 private final HTConfig fConfig
;
60 /** Reader/writer object */
61 private final @NonNull HT_IO fTreeIO
;
63 // ------------------------------------------------------------------------
64 // Variable Fields (will change throughout the existence of the SHT)
65 // ------------------------------------------------------------------------
67 /** Latest timestamp found in the tree (at any given moment) */
68 private long fTreeEnd
;
70 /** The total number of nodes that exists in this tree */
71 private int fNodeCount
;
73 /** "Cache" to keep the active nodes in memory */
74 private final @NonNull List
<@NonNull HTNode
> fLatestBranch
;
76 // ------------------------------------------------------------------------
77 // Constructors/"Destructors"
78 // ------------------------------------------------------------------------
81 * Create a new State History from scratch, using a {@link HTConfig} object
85 * The config to use for this History Tree.
87 * If an error happens trying to open/write to the file
88 * specified in the config
90 public HistoryTreeClassic(HTConfig conf
) throws IOException
{
92 * Simple check to make sure we have enough place in the 0th block for
93 * the tree configuration
95 if (conf
.getBlockSize() < TREE_HEADER_SIZE
) {
96 throw new IllegalArgumentException();
100 fTreeEnd
= conf
.getTreeStart();
102 fLatestBranch
= Collections
.synchronizedList(new ArrayList
<>());
104 /* Prepare the IO object */
105 fTreeIO
= new HT_IO(fConfig
, true);
107 /* Add the first node to the tree */
108 LeafNode firstNode
= initNewLeafNode(-1, conf
.getTreeStart());
109 fLatestBranch
.add(firstNode
);
113 * "Reader" constructor : instantiate a SHTree from an existing tree file on
116 * @param existingStateFile
117 * Path/filename of the history-file we are to open
118 * @param expProviderVersion
119 * The expected version of the state provider
120 * @throws IOException
121 * If an error happens reading the file
123 public HistoryTreeClassic(File existingStateFile
, int expProviderVersion
) throws IOException
{
125 * Open the file ourselves, get the tree header information we need,
126 * then pass on the descriptor to the TreeIO object.
128 int rootNodeSeqNb
, res
;
132 /* Java I/O mumbo jumbo... */
133 if (!existingStateFile
.exists()) {
134 throw new IOException("Selected state file does not exist"); //$NON-NLS-1$
136 if (existingStateFile
.length() <= 0) {
137 throw new IOException("Empty target file"); //$NON-NLS-1$
140 try (FileInputStream fis
= new FileInputStream(existingStateFile
);
141 FileChannel fc
= fis
.getChannel();) {
143 ByteBuffer buffer
= ByteBuffer
.allocate(TREE_HEADER_SIZE
);
144 buffer
.order(ByteOrder
.LITTLE_ENDIAN
);
147 res
= fc
.read(buffer
);
148 if (res
!= TREE_HEADER_SIZE
) {
149 throw new IOException("Invalid header size"); //$NON-NLS-1$
155 * Check the magic number to make sure we're opening the right type
158 res
= buffer
.getInt();
159 if (res
!= HISTORY_FILE_MAGIC_NUMBER
) {
160 throw new IOException("Wrong magic number"); //$NON-NLS-1$
163 res
= buffer
.getInt(); /* File format version number */
164 if (res
!= FILE_VERSION
) {
165 throw new IOException("Mismatching History Tree file format versions"); //$NON-NLS-1$
168 res
= buffer
.getInt(); /* Event handler's version number */
169 if (res
!= expProviderVersion
&&
170 expProviderVersion
!= ITmfStateSystemBuilder
.IGNORE_PROVIDER_VERSION
) {
172 * The existing history was built using an event handler that
173 * doesn't match the current one in the framework.
175 * Information could be all wrong. Instead of keeping an
176 * incorrect history file, a rebuild is done.
178 throw new IOException("Mismatching event handler versions"); //$NON-NLS-1$
181 bs
= buffer
.getInt(); /* Block Size */
182 maxc
= buffer
.getInt(); /* Max nb of children per node */
184 fNodeCount
= buffer
.getInt();
185 rootNodeSeqNb
= buffer
.getInt();
186 startTime
= buffer
.getLong();
188 fConfig
= new HTConfig(existingStateFile
, bs
, maxc
, expProviderVersion
, startTime
);
192 * FIXME We close fis here and the TreeIO will then reopen the same
193 * file, not extremely elegant. But how to pass the information here to
196 fTreeIO
= new HT_IO(fConfig
, false);
198 fLatestBranch
= buildLatestBranch(rootNodeSeqNb
);
199 fTreeEnd
= getRootNode().getNodeEnd();
202 * Make sure the history start time we read previously is consistent
203 * with was is actually in the root node.
205 if (startTime
!= getRootNode().getNodeStart()) {
206 throw new IOException("Inconsistent start times in the" + //$NON-NLS-1$
207 "history file, it might be corrupted."); //$NON-NLS-1$
212 * Rebuild the latestBranch "cache" object by reading the nodes from disk
213 * (When we are opening an existing file on disk and want to append to it,
216 * @param rootNodeSeqNb
217 * The sequence number of the root node, so we know where to
219 * @throws ClosedChannelException
221 private @NonNull List
<@NonNull HTNode
> buildLatestBranch(int rootNodeSeqNb
) throws ClosedChannelException
{
222 List
<@NonNull HTNode
> list
= new ArrayList
<>();
224 HTNode nextChildNode
= fTreeIO
.readNode(rootNodeSeqNb
);
225 list
.add(nextChildNode
);
227 /* Follow the last branch up to the leaf */
228 while (nextChildNode
.getNodeType() == HTNode
.NodeType
.CORE
) {
229 nextChildNode
= fTreeIO
.readNode(((CoreNode
) nextChildNode
).getLatestChild());
230 list
.add(nextChildNode
);
232 return Collections
.synchronizedList(list
);
236 public void closeTree(long requestedEndTime
) {
237 /* This is an important operation, queries can wait */
238 synchronized (fLatestBranch
) {
240 * Work-around the "empty branches" that get created when the root
241 * node becomes full. Overwrite the tree's end time with the
242 * original wanted end-time, to ensure no queries are sent into
245 * This won't be needed once extended nodes are implemented.
247 fTreeEnd
= requestedEndTime
;
249 /* Close off the latest branch of the tree */
250 for (int i
= 0; i
< fLatestBranch
.size(); i
++) {
251 fLatestBranch
.get(i
).closeThisNode(fTreeEnd
);
252 fTreeIO
.writeNode(fLatestBranch
.get(i
));
255 try (FileChannel fc
= fTreeIO
.getFcOut();) {
256 ByteBuffer buffer
= ByteBuffer
.allocate(TREE_HEADER_SIZE
);
257 buffer
.order(ByteOrder
.LITTLE_ENDIAN
);
260 /* Save the config of the tree to the header of the file */
263 buffer
.putInt(HISTORY_FILE_MAGIC_NUMBER
);
265 buffer
.putInt(FILE_VERSION
);
266 buffer
.putInt(fConfig
.getProviderVersion());
268 buffer
.putInt(fConfig
.getBlockSize());
269 buffer
.putInt(fConfig
.getMaxChildren());
271 buffer
.putInt(fNodeCount
);
273 /* root node seq. nb */
274 buffer
.putInt(fLatestBranch
.get(0).getSequenceNumber());
276 /* start time of this history */
277 buffer
.putLong(fLatestBranch
.get(0).getNodeStart());
280 int res
= fc
.write(buffer
);
281 assert (res
<= TREE_HEADER_SIZE
);
282 /* done writing the file header */
284 } catch (IOException e
) {
286 * If we were able to write so far, there should not be any
287 * problem at this point...
289 throw new RuntimeException("State system write error"); //$NON-NLS-1$
294 // ------------------------------------------------------------------------
296 // ------------------------------------------------------------------------
299 public long getTreeStart() {
300 return fConfig
.getTreeStart();
304 public long getTreeEnd() {
309 public int getNodeCount() {
314 public HTNode
getRootNode() {
315 return fLatestBranch
.get(0);
319 * Return the latest branch of the tree. That branch is immutable. Used for
320 * unit testing and debugging.
322 * @return The immutable latest branch
325 protected List
<@NonNull HTNode
> getLatestBranch() {
326 return ImmutableList
.copyOf(fLatestBranch
);
330 * Read a node at sequence number
333 * The sequence number of the node to read
334 * @return The HTNode object
335 * @throws ClosedChannelException
336 * Exception thrown when reading the node
339 protected @NonNull HTNode
getNode(int seqNum
) throws ClosedChannelException
{
340 // First, check in the latest branch if the node is there
341 for (HTNode node
: fLatestBranch
) {
342 if (node
.getSequenceNumber() == seqNum
) {
346 return fTreeIO
.readNode(seqNum
);
350 * Retrieve the TreeIO object. Should only be used for testing.
355 protected @NonNull HT_IO
getTreeIO() {
359 // ------------------------------------------------------------------------
361 // ------------------------------------------------------------------------
364 public FileInputStream
supplyATReader() {
365 return fTreeIO
.supplyATReader(getNodeCount());
369 public File
supplyATWriterFile() {
370 return fConfig
.getStateFile();
374 public long supplyATWriterFilePos() {
375 return IHistoryTree
.TREE_HEADER_SIZE
376 + ((long) getNodeCount() * fConfig
.getBlockSize());
380 public HTNode
readNode(int seqNumber
) throws ClosedChannelException
{
381 /* Try to read the node from memory */
382 synchronized (fLatestBranch
) {
383 for (HTNode node
: fLatestBranch
) {
384 if (node
.getSequenceNumber() == seqNumber
) {
390 /* Read the node from disk */
391 return fTreeIO
.readNode(seqNumber
);
395 public void writeNode(HTNode node
) {
396 fTreeIO
.writeNode(node
);
400 public void closeFile() {
405 public void deleteFile() {
406 fTreeIO
.deleteFile();
409 // ------------------------------------------------------------------------
411 // ------------------------------------------------------------------------
414 public void insertInterval(HTInterval interval
) throws TimeRangeException
{
415 if (interval
.getStartTime() < fConfig
.getTreeStart()) {
416 throw new TimeRangeException("Interval Start:" + interval
.getStartTime() + ", Config Start:" + fConfig
.getTreeStart()); //$NON-NLS-1$ //$NON-NLS-2$
418 tryInsertAtNode(interval
, fLatestBranch
.size() - 1);
422 * Inner method to find in which node we should add the interval.
425 * The interval to add to the tree
427 * The index *in the latestBranch* where we are trying the
430 private void tryInsertAtNode(HTInterval interval
, int indexOfNode
) {
431 HTNode targetNode
= fLatestBranch
.get(indexOfNode
);
433 /* Verify if there is enough room in this node to store this interval */
434 if (interval
.getSizeOnDisk() > targetNode
.getNodeFreeSpace()) {
435 /* Nope, not enough room. Insert in a new sibling instead. */
436 addSiblingNode(indexOfNode
);
437 tryInsertAtNode(interval
, fLatestBranch
.size() - 1);
441 /* Make sure the interval time range fits this node */
442 if (interval
.getStartTime() < targetNode
.getNodeStart()) {
444 * No, this interval starts before the startTime of this node. We
445 * need to check recursively in parents if it can fit.
447 tryInsertAtNode(interval
, indexOfNode
- 1);
452 * Ok, there is room, and the interval fits in this time slot. Let's add
455 targetNode
.addInterval(interval
);
457 /* Update treeEnd if needed */
458 if (interval
.getEndTime() > fTreeEnd
) {
459 fTreeEnd
= interval
.getEndTime();
464 * Method to add a sibling to any node in the latest branch. This will add
465 * children back down to the leaf level, if needed.
468 * The index in latestBranch where we start adding
470 private void addSiblingNode(int indexOfNode
) {
471 synchronized (fLatestBranch
) {
472 final long splitTime
= fTreeEnd
;
474 if (indexOfNode
>= fLatestBranch
.size()) {
476 * We need to make sure (indexOfNode - 1) doesn't get the last
477 * node in the branch, because that one is a Leaf Node.
479 throw new IllegalStateException();
482 /* Check if we need to add a new root node */
483 if (indexOfNode
== 0) {
488 /* Check if we can indeed add a child to the target parent */
489 if (((CoreNode
) fLatestBranch
.get(indexOfNode
- 1)).getNbChildren() == fConfig
.getMaxChildren()) {
490 /* If not, add a branch starting one level higher instead */
491 addSiblingNode(indexOfNode
- 1);
495 /* Split off the new branch from the old one */
496 for (int i
= indexOfNode
; i
< fLatestBranch
.size(); i
++) {
497 fLatestBranch
.get(i
).closeThisNode(splitTime
);
498 fTreeIO
.writeNode(fLatestBranch
.get(i
));
500 CoreNode prevNode
= (CoreNode
) fLatestBranch
.get(i
- 1);
503 switch (fLatestBranch
.get(i
).getNodeType()) {
505 newNode
= initNewCoreNode(prevNode
.getSequenceNumber(), splitTime
+ 1);
508 newNode
= initNewLeafNode(prevNode
.getSequenceNumber(), splitTime
+ 1);
511 throw new IllegalStateException();
514 prevNode
.linkNewChild(newNode
);
515 fLatestBranch
.set(i
, newNode
);
521 * Similar to the previous method, except here we rebuild a completely new
524 private void addNewRootNode() {
525 final long splitTime
= fTreeEnd
;
527 HTNode oldRootNode
= fLatestBranch
.get(0);
528 CoreNode newRootNode
= initNewCoreNode(-1, fConfig
.getTreeStart());
530 /* Tell the old root node that it isn't root anymore */
531 oldRootNode
.setParentSequenceNumber(newRootNode
.getSequenceNumber());
533 /* Close off the whole current latestBranch */
535 for (int i
= 0; i
< fLatestBranch
.size(); i
++) {
536 fLatestBranch
.get(i
).closeThisNode(splitTime
);
537 fTreeIO
.writeNode(fLatestBranch
.get(i
));
540 /* Link the new root to its first child (the previous root node) */
541 newRootNode
.linkNewChild(oldRootNode
);
543 /* Rebuild a new latestBranch */
544 int depth
= fLatestBranch
.size();
545 fLatestBranch
.clear();
546 fLatestBranch
.add(newRootNode
);
548 // Create new coreNode
549 for (int i
= 1; i
< depth
; i
++) {
550 CoreNode prevNode
= (CoreNode
) fLatestBranch
.get(i
- 1);
551 CoreNode newNode
= initNewCoreNode(prevNode
.getSequenceNumber(),
553 prevNode
.linkNewChild(newNode
);
554 fLatestBranch
.add(newNode
);
557 // Create the new leafNode
558 CoreNode prevNode
= (CoreNode
) fLatestBranch
.get(depth
- 1);
559 LeafNode newNode
= initNewLeafNode(prevNode
.getSequenceNumber(), splitTime
+ 1);
560 prevNode
.linkNewChild(newNode
);
561 fLatestBranch
.add(newNode
);
565 * Add a new empty core node to the tree.
567 * @param parentSeqNumber
568 * Sequence number of this node's parent
570 * Start time of the new node
571 * @return The newly created node
573 private @NonNull CoreNode
initNewCoreNode(int parentSeqNumber
, long startTime
) {
574 CoreNode newNode
= new CoreNode(fConfig
, fNodeCount
, parentSeqNumber
,
581 * Add a new empty leaf node to the tree.
583 * @param parentSeqNumber
584 * Sequence number of this node's parent
586 * Start time of the new node
587 * @return The newly created node
589 private @NonNull LeafNode
initNewLeafNode(int parentSeqNumber
, long startTime
) {
590 LeafNode newNode
= new LeafNode(fConfig
, fNodeCount
, parentSeqNumber
,
597 public Collection
<HTNode
> selectNextChildren(CoreNode currentNode
, long t
) throws ClosedChannelException
{
598 assert (currentNode
.getNbChildren() > 0);
599 int potentialNextSeqNb
= currentNode
.getSequenceNumber();
601 for (int i
= 0; i
< currentNode
.getNbChildren(); i
++) {
602 if (t
>= currentNode
.getChildStart(i
)) {
603 potentialNextSeqNb
= currentNode
.getChild(i
);
610 * Once we exit this loop, we should have found a children to follow. If
611 * we didn't, there's a problem.
613 if (potentialNextSeqNb
== currentNode
.getSequenceNumber()) {
614 throw new IllegalStateException("No next child node found"); //$NON-NLS-1$
618 * Since this code path is quite performance-critical, avoid iterating
619 * through the whole latestBranch array if we know for sure the next
620 * node has to be on disk
622 if (currentNode
.isOnDisk()) {
623 return Collections
.singleton(fTreeIO
.readNode(potentialNextSeqNb
));
625 return Collections
.singleton(readNode(potentialNextSeqNb
));
629 public long getFileSize() {
630 return fConfig
.getStateFile().length();
633 // ------------------------------------------------------------------------
634 // Test/debugging methods
635 // ------------------------------------------------------------------------
637 /* Only used for debugging, shouldn't be externalized */
638 @SuppressWarnings("nls")
640 public String
toString() {
641 return "Information on the current tree:\n\n" + "Blocksize: "
642 + fConfig
.getBlockSize() + "\n" + "Max nb. of children per node: "
643 + fConfig
.getMaxChildren() + "\n" + "Number of nodes: " + fNodeCount
644 + "\n" + "Depth of the tree: " + fLatestBranch
.size() + "\n"
645 + "Size of the treefile: " + getFileSize() + "\n"
646 + "Root node has sequence number: "
647 + fLatestBranch
.get(0).getSequenceNumber() + "\n"
648 + "'Latest leaf' has sequence number: "
649 + fLatestBranch
.get(fLatestBranch
.size() - 1).getSequenceNumber();