Commit Graph

773 Commits

Author SHA1 Message Date
Christopher Haster 21012dbb1b Some minor append cleanup, mostly around follow conditions 2023-02-12 15:23:48 -06:00
Christopher Haster 6a5aae98fc Cleaned up the leaf splitting condition
Turns out if do the less-than condition first, we can simplify things. I
think this is due to how we bias the tree weights.
2023-02-12 15:22:21 -06:00
Christopher Haster 5c42e52324 Revert "A different thought on tracking found tags during lfs_rbyd_append" 2023-02-12 15:21:42 -06:00
Christopher Haster 209e70897c A different thought on tracking found tags during lfs_rbyd_append
The idea here is to just use a separate variable for found tags, instead
of stealing a bit for tracking which tags were found.

I was hoping this would simplify more operations on the tags to avoid
some masking operations, but it doesn't seem like it.
2023-02-12 15:19:03 -06:00
Christopher Haster 3d008b793d This method of leaf splitting works, but I want to explore another option
Committing current progress.
2023-02-12 15:18:15 -06:00
Christopher Haster d8540974d4 Significant cleanup of lfs_rbyd_append, simplified pruning rules 2023-02-12 15:17:54 -06:00
Christopher Haster 17a043f4b8 Merged lfs_rbyd_append and lfs_rbyd_delete
Now we have one massive chimera of a function that does everything. This
is good software design, trust me.
2023-02-12 15:14:23 -06:00
Christopher Haster 179a1df3d3 Changed rbyd removes to remove from the tree, similar to deletes
Tag removal is basically a range-delete of one that doesn't change
the tree weights. This deduplicates the two methods of deleting tags and
completely gets rid of tombstoning.

Note we still need a "removed tag" encoding so that we can invalidate
tags that may be found during fetch operations. Fortunately this
encoding is basically free due to overlap with alt encoding.
2023-02-12 15:14:06 -06:00
Christopher Haster 92ce5df949 Added tests for theoretical bound on tree height and recovery from delete-all
- Unless there is a bug, rbyd trees should be strictly <= (2*log2(n)+1)
  in height. The extra +1 from traditional red-black trees is due to the
  introduced to-be-pruned alt, but since we clean those up as soon as we
  can, only one will ever exist in any search path.

  This also holds true with range deletion, however the definition of n
  changes to the number of tree operations. This is the same for tombstoning.

- Delete-all recovery is a bit tricky because we have no tree at that
  point, which is weird for an append-only data-structure.
2023-02-12 15:14:01 -06:00
Christopher Haster 9a1ce1be47 Removed erroneous new red edges on deletion 2023-02-12 15:14:01 -06:00
Christopher Haster 1a5efcfa89 Removed unnecessary use of graft in early continues 2023-02-12 15:14:01 -06:00
Christopher Haster 788e98a989 Made delete remove the path from the tree completely, fixed failing tests 2023-02-12 15:13:52 -06:00
Christopher Haster 045a1098fd Added similar delete permutation tests, note that some are failing 2023-02-12 15:02:45 -06:00
Christopher Haster b14851b657 Added more tests, mostly around remove permutations 2023-02-12 15:02:27 -06:00
Christopher Haster dc5e74ac41 Added range deletion testing 2023-02-12 15:02:11 -06:00
Christopher Haster fbb6bd0919 A working delete implementation?
Though note this still uses tombstoning.
2023-02-12 15:01:05 -06:00
Christopher Haster b621774759 Adopted upper/lower bounds in lfs_rbyd_append
There are two ways to represent the bounds in the search down the rbyd
tree:

1. Using lower/upper bounds and the id we are searching for:

     lower bound    id    upper bound
     |              |     |
     v              v     v
   <-a--b--c--d--e--f--g--h->

2. Using the lower/upper weights, which implicitly encodes the id,
   saving a word:

       lower weight     upper weight
       |                |
     .-'-----------. .--'-.
   <-a--b--c--d--e--f--g--h->

Now that I am diving deep into the rbyd algorithm again, the lower/upper
weight based approach just isn't worth the extra mental steps required
to understand what the algorithm is doing. Besides, we likely pay for
the implicit id anyways since we need enough state to remove the
ambiguity of sparse tags.
2023-02-12 14:52:46 -06:00
Christopher Haster d78c4412b6 Half-adopted upper/lower bounds in lookup and append 2023-02-12 14:51:34 -06:00
Christopher Haster 55e9df571b Minor test_rbyd improvements
Attempted to implement TEST_PERMUTATIONS but that went nowhere useful.
Unfortunately test defines don't have a way to represent arrays, and
adding such a representation would be unreasonably complicated.

For practical purposes it's better to just do the permutation generation
inside the tests. Maybe at some point it would be nice to integrate
permutation testing so the state gets hashed somehow as a part of the
test failure, but now is not the time.
2023-02-12 14:49:04 -06:00
Christopher Haster ef7ee6eb7d Added delete permutation testing (failing as expected) and some minor tweaks 2023-02-12 14:48:33 -06:00
Christopher Haster 22879c1cbb Avoided issue with tail deletions by tossing out-of-bound lookups early 2023-02-12 14:47:38 -06:00
Christopher Haster 5d9e7c8e86 Moved lifetimes in dbgrbyd.py so lifetimes and jumps can both be rendered
$ ./scripts/dbgrbyd.py disk 4096 0 -g -j
  mdir 0x0, rev 1, size 59
  off             tag                     data (truncated)
  00000004: .     createreg id1 4         aa aa aa aa              ....  <--.
  0000000c: |     altblt x80d0 x4                                        -' |
  00000010: | .   createreg id2 4         cc cc cc cc              ....  <. |
  00000018: | |   altrlt x80d0 x4                                        -|-'
  0000001c: | |   altbgt x8000 x10                                       -'
  00000020: | .\  createreg id2 4         bb bb bb bb              ....
  00000028: | | | fcrc 5                  51 53 7d 52 01           QS}R.
  0000002f: | | | crc0 7                  5f db 22 8a 1b 1b 1b     _."....
2023-02-12 14:44:28 -06:00
Christopher Haster 1b6e1fdd33 Fixed delete issue with cut starting in a red edge
Though this is a bit of an ugly fix...

Red edges mess everything up during deletion since we always need to
go down red edges in case we need to do a red flip. This puts our
algorithm into an awkward state.

It would be easier to do away with colors completely when we start
deleting (after all this branch of the tree _will_ be shrinking), but
this conflicts with the ultimate goal of deduplicate rbyd append.
2023-02-12 14:41:47 -06:00
Christopher Haster ca710b5a29 Initial, very, very rough implementation of rbyd range deletion
Tree deletion is such a pain. It always seems like an easy addition to
the core algorithm but always comes with problems.

The initial plan for deletes was to iterate through all tags, tombstone,
and then adjust weights as needed. This accomplishes deletes with little
change to the rbyd algorithm, but adds a complex traversal inside the
commit logic. Doable in one commit, but complex. It also risks weird
unintuitive corner cases since the cost of deletion grows with the number
of tags being deleted (O(m log n)).

But this rbyd data structure is a tree, so in theory it's possible to
delete a whole range of tags in a single O(log n) operation.

---

This is a proof-of-concept range deletion algorithm for rbyd trees.

Note, this does not preserve rbyd's balancing properties! But it is no
worse than tombstoning. This is acceptable for littlefs as any
unbalanced trees will be rebalanced during compaction.

The idea is to follow the same underlying dhara algorithm, where we
follow a search path and save any alt pointers not taken, but we follow
both search paths that form the outside of the range, and only keep
outside edges.

For example, a tree:

        .-------o-------.
        |               |
    .---o---.       .---o---.
    |       |       |       |
  .-o-.   .-o-.   .-o-.   .-o-.
  |   |   |   |   |   |   |   |
  a   b   c   d   e   f   g   h

To delete the range d-e, we would search for d, and search for e:

        ********o********
        *               *
    .---*****       *****---.
    |       *       *       |
  .-o-.   .-***   ***-.   .-o-.
  |   |   |   *   *   |   |   |
  a   b   c   d   e   f   g   h

And keep the outside edges:

    .---                 ---.
    |                       |
  .-o-.   .-         -.   .-o-.
  |   |   |           |   |   |
  a   b   c           f   g   h

But how do we combine the outside edges? The simpler option is to do
both searches seperately, one after the other. This would end up with a
tree like this:

    .---------o
    |         |
  .-o-.   .---o
  |   |   |   |
  a   b   c   o---------.
              |         |
              o---.   .-o-.
              |   |   |   |
              _   f   g   h

But this horribly throws off the balance of our tree! It's worse than
tombstoning, and gets worse with more tags.

An alternative strategy, which is used here, is to alternate edges as we
descend down the tree. This unfortunately is more complex, and requires
~2x the RAM, but better preserves the balance of our tree. It isn't
perfect, because we lose color information, but we can leave that up to
compaction:

  .---------o
  |         |
.-o-.       o---------.
|   |       |         |
a   b   .---o       .-o-.
        |   |       |   |
        c   o---.   g   h
            |   |
            _   f

I also hope this can be merged into lfs_rbyd_append, deduplicating the
entire core rbyd append algorithm.
2023-02-12 13:29:06 -06:00
Christopher Haster cde3ba4cd8 Different rbyd struct usage in commit, moved append into its own function
Under any optimization level the compiler should inline lfs_rbyd_append
into lfs_rbyd_commit, but this code organization was desperately needed.
2023-02-12 13:25:51 -06:00
Christopher Haster 12edc5aee3 Added some ascii art to dbgrbyd.py to help debug how ids change over time
An example:

  $ ./scripts/dbgrbyd.py disk 4096 0 -i
  mdir 0x0, rev 1, size 59
  off       tag                     data (truncated)
  00000004: create x01 id1 4        aa aa aa aa              ....      .
  0000000c: altblt x80d0 x4                                            |
  00000010: create x01 id2 4        cc cc cc cc              ....      | .
  00000018: altrlt x80d0 x4                                            | |
  0000001c: altbgt x8000 x10                                           | |
  00000020: create x01 id2 4        bb bb bb bb              ....      | .\
  00000028: fcrc 5                  51 53 7d 52 01           QS}R.     | | |
  0000002f: crc0 7                  5f db 22 8a 1b 1b 1b     _."....   | | |
2023-02-12 13:23:56 -06:00
Christopher Haster 2979e6273a Added weight-adjusting insert operations, though it is currently very hacky 2023-02-12 13:22:45 -06:00
Christopher Haster 6c66e20349 Added lfs_rbyd_get, bumped hint back up to full during lookup
The idea behind the full hint is that we may stay on the current search
path, benefiting from a loaded cache. Though this should probably be
benchmarked eventually.
2023-02-12 13:21:41 -06:00
Christopher Haster 3c17c94b94 Added test_rbyd_large with better boundary conditions near end-of-block
test_rbyd_large also doubles as a decent fuzz test, since it involves
many more tags than the permutation testing can ever hit.
2023-02-12 13:20:12 -06:00
Christopher Haster 8d4991df6a Added the option to error on no valid commit to dbgrbyd.py
Considered adding --ignore-errors to watch.py, but it doesn't really
make sense with watch.py's implementation. watch.py would need to not update
in realtime, which conflicts with other use cases.
2023-02-12 13:19:46 -06:00
Christopher Haster 5cdda57373 Added the ability to remove rbyd tags via tombstoning
It's quite lucky a spare bit is free in the tag encoding, this means we
don't need a reserved length value as originally planned. We end up using
all of the bits that overlap the alt pointer encoding, which is nice and
unexpected.
2023-02-12 13:16:55 -06:00
Christopher Haster 2b475662e9 Rbyd test tweak: limited 0-tag lookups to traversal testing 2023-02-12 13:16:13 -06:00
Christopher Haster 1108191184 Added permutation testing of rbyd lookups over multiple commits
In theory spreading rbyd trees across multiple commits shouldn't change
anything, but the whole point of testing is to catch things that are not
in the theory.
2023-02-12 13:15:15 -06:00
Christopher Haster d6ad74555b Made dbgrbyd.py a bit more resilient to truncated data 2023-02-12 13:14:54 -06:00
Christopher Haster a4fd070fc1 Dropped attr-size down to at most 28-bits (4 leb128 bytes)
This really doesn't need to be this large. The extra 4-bits is not worth
the required extra byte of waste in every commit (CRCs tags are forced
to use a fully expanded leb128 due to a catch-22 issue).
2023-02-12 13:12:44 -06:00
Christopher Haster 084094116f Got all <=7 lookup permutations working, required a recoloring fix
The alt pointers change position, but the colors are supposed to stay
the same. If you randomly mix up the colors it sure does make things
confusing.
2023-02-12 13:11:18 -06:00
Christopher Haster e73f1c0e8d A number of small rbyd tweaks and cleanup 2023-02-12 13:11:06 -06:00
Christopher Haster d9b419d36a Rbyd prune now working 2023-02-12 13:09:25 -06:00
Christopher Haster 471f12c79a Fixed issue with rcache going out-of-date when overlapping pcache
This was a inter-abstraction-layer assumption that won't hold true with
the rbyd blocks.

The whole caching layer probably deserves a rewrite at this point.
2023-02-12 13:07:29 -06:00
Christopher Haster b03420ccfc Got rbyd yellow splitting working 2023-02-12 13:06:58 -06:00
Christopher Haster b48f7fcfb0 Added some more debug utilities to dbgrbyd.py, mainly --rbyd and --jumps
Not only is this a genuinely useful debugging tool, it looks very cool:

  $ ./scripts/dbgrbyd.py disk 4096 0 -j
  mdir 0x0, rev 1, size 73
  off       tag                     data (truncated)
  00000004: gstate x01 4            aa aa aa aa              ....      <----.
  0000000b: altblt x98 x4                                              -' | |
  0000000e: gstate x04 4            bb bb bb bb              ....      <------.
  00000015: altrlt x98 x4                                              -|-' | |
  00000018: altbgt x7ee8 xe                                            -'   | |
  0000001c: gstate x02 4            cc cc cc cc              ....      <.   | |
  00000023: altrlt x98 x4                                              -|---' |
  00000026: altrlt x80 x1c                                             -'     |
  00000029: altbgt x7e68 xe                                            -------'
  0000002d: gstate x03 4            dd dd dd dd              ....
  00000034: fcrc 5                  05 3f aa db 01           .?...
  0000003b: crc0 8                  5c 12 29 d9 1b 1b 1b 1b  \.).....
2023-02-12 13:02:59 -06:00
Christopher Haster ac021f623c Added rbyd traverse testing 2023-02-12 13:02:31 -06:00
Christopher Haster e95ca03ff1 Minor rbyd tweaks and cleanup 2023-02-12 13:01:52 -06:00
Christopher Haster 53efaac243 Ok, now fixed rflip, branch/jump needed tweaking 2023-02-12 13:01:29 -06:00
Christopher Haster fe28837861 Rbyd trees with 4-leaves now working, fixed lfs_rtag_flip bug
- This is when flips starts happening during lfs_rbyd_append
- lfs_rtag_flip had an off-by-one math mistake
2023-02-12 12:58:55 -06:00
Christopher Haster c5fec90465 Rbyd rflips are now working, quite nicely actually
It turns out statefulness works quite well with this algorithm (The
prototype was in Haskell, which created some artificial problems. I
think it may have just been too high-level a language for this
near-instruction-level algorithm).
2023-02-12 12:58:29 -06:00
Christopher Haster 05276cef9a Added a bias to alt weights so in-between tags prefer larger tags
This bias makes it so that tag lookups always find a tag strictly >= the
requested tag, unless we are at the end of the tree.

This makes tree traversal trivial, which is quite nice.

Need to remove ntag now, it's no longer needed.
2023-02-12 12:49:19 -06:00
Christopher Haster 168977ffad Implemented lfs_rbyd_lookup
Though maybe this should not require exact matches?
2023-02-12 12:48:06 -06:00
Christopher Haster 024aaeba56 Some small tweaks
- Moved alt encoding 0x1 => 0x4, which can lead to slightly better
  lookup tables, the perturb bit takes the same place as the color bit,
  which means both can be ignored in readonly operations.

- Dropped lfs_rbyd_fetchmatch, asking each lfs_rbyd_fetch to include NULL
  isn't that bad.

New encoding:

  tags:
  iiii iiiiiii iiiiiTT TTTTTTt ttt0tpv
                   ^--------^------^^^- 16-bit id
                            '------|||- 8-bit type2
                                   '||- 5-bit type1
                                    '|- perturb bit
                                     '- valid bit
  llll lllllll lllllll lllllll lllllll
                                     ^- n-bit length

  alts:
  wwww wwwwwww wwwwwww wwwwwww www1dcv
                                 ^^^-^- 28-bit weight
                                  '|-|- color bit
                                   '-|- direction bit
                                     '- valid bit
  jjjj jjjjjjj jjjjjjj jjjjjjj jjjjjjj
                                     ^- n-bit jump
2023-02-12 12:40:19 -06:00
Christopher Haster 9a0e3fc749 More rbyd tests, multi-commit now working 2023-02-12 12:39:17 -06:00