GithubHelp home page GithubHelp logo

dementeddevil / zen.trunk Goto Github PK

View Code? Open in Web Editor NEW
5.0 4.0 0.0 3.5 MB

Block storage framework and a collection of projects built on top of this high-performance subsystem.

C# 78.92% C++ 2.17% C 0.05% CSS 8.03% JavaScript 9.21% ANTLR 1.30% HTML 0.32% ASP.NET 0.01%
virtual-memory scatter-gather overlapped-io database database-engine pinvoke asynchronous-programming acid

zen.trunk's Introduction

Zen Trunk

Introduction

Trunk started life as an investigation into whether it was possible to write a block storage engine and then build a database engine on top of it.

It's the conclusion of what happens when you read the book "Inside SQL Server 6.5" and find that the level of detail in the book is almost enough to actually cut code... Well I say "almost"; clearly it definitely is possible!

The codebase has been rewritten a few times over the years;

v0.1 started life as an APM-based implementation that was very difficult to follow.

v0.2 was rewritten with System.Task-based calls; slightly less difficult to follow but still very tricky.

v0.3 was a comprehensive rewrite that switched to Task-based async/await and brought in TPL Dataflow. This change was a major milestone. The code became much easier to follow and understand!

v0.4 was where the locking mechanism was rewritten with full state machine/dataflow semantics (it took a while to get all the bugs out of that)

v0.5 saw the low-level memory management classes wrapped in SafeHandle based classes - this was done following the best practices used by the .NET Framework engineers so we should never ever leak handles or memory blocks no matter what the process does. This also meant that much of the dispose implementation patterns used in so many other classes could be simplified; no need for destructors!

v0.6 logical code layout changes - split the namespaces into separate assemblies and broke out the buffer-field serialization classes.

v0.7 improved unit testing closed lots of runtime bugs and really tidied up the runtime behaviour

v0.8 fixed the device mount/dismount logic which was rather broken and also fixed the database create logic

v0.9 resolved long standing issues in the caching buffer device

v0.10 corrected the implementation of the SQL parser where it builds the C# expression tree for CREATE DATABASE arguments - all file-spec arguments are now built into a full expression tree rather than taking the shortcut of passing an initialised constant expression. Much better as it fits better with the way the expression tree visitor is supposed to work.

v0.11 finally saw the addition of audio sample support

v0.12 saw the audio index support added. The fixes and implementation refactoring here will be back-ported into the table index manager

I still have no idea when v1.0 will hit the streets but the journey has been lots of fun so far!!

Architecture

Currently standing at 17 projects (excluding the bits associated with the WiX-based installer) the solution is split into a number of layers;

Infrastructure helpers

These assemblies contain threading helpers and streaming primitives. The streaming classes are very useful for anyone wanting to implement pull-based streams, buffered streams etc.

Virtual memory manager

This assembly handles wrapping of calls to low-level Win32 APIs VirtualAlloc, VirtualFree etc allowing the manipulation of VirtualBuffer objects that can be safely persisted via unbuffered asynchronous I/O operations for maximum speed.

Has an extension of FileStream class that supports sparse files and unbuffered scatter/gather I/O for even greater throughput; the scatter/gather support has been extensively tested however the sparse file support has not been fully utilised as yet.

Finally contains device class abstractions that allow a file (or collection of files) to be represented as a virtual device that knows how to read and write fixed-size pages. This is a core competency needed for block-based I/O across a collection of files. Essential for databases but also somewhat handy in other applications from remote storage buffering, torrent engines, file-servers etc.

This little lot represents the neucleus for any data-storage engine that needs high-performance I/O and as such could be used for a torrent engine, high-performance logging system or (in this case) a database engine.

Storage engine

This jumbo assembly contains the logic for handling ACID transactions of pages.

This single sentence means;

  • it has a CachingBufferDevice that handles queuing reads and writes to the underlying storage to that it is possible to take advantage of the scatter/gather feature to read from or write to many different pages in a single operation.

  • it has code to perform immediate writes to and recovery from a transaction log.

  • it has locking primitives that allow all the ACID lock types including support for lock hierarchies and lock escalation.

  • it has support for organising table data.

  • it has support for indexing table data.

  • it has support for organising audio sample data.

  • it has support for indexing audio sample data.

  • it supports identification and lock tracking by session and transaction identifiers.

Query manager

This assembly handles the translation of SQL into C# expression trees that execute actions against the storage engine. The engine is powered by the amazing ANTLR v4 project. The SQL grammar is T-SQL although so far almost nothing has been implemented!

Basically CREATE DATABASE and SET TRANSACTION ISOLATION LEVEL. We might have BEGIN TRANS/COMMIT/ROLLBACK too now I think about it!

Network manager

This assembly handles management of the network connection between client and the service including the establishment of session and transaction scopes. The network protocol is proprietary and utilises SuperSocket for TCP/IP based communications. No support for NamedPipes, SharedFiles or anything else at present.

Trunk service

This assembly interfaces with the Windows Service Control Manager and provides the process host. The service is designed to be multi-instance capable (default instance and named instances)

Installer support

A suite of components to support installing the service. Creating an installer capable of installing instance-based services is actually rather complicated - especially if you wish to correctly support product upgrade scenarios, selective uninstall/reinstall and keep everything in a good, consistent state. The solution used is based on Windows Installer for XML (WiX) and involves a surprising amount of C++ and custom stuff!

Torrent Engine

A collection of bits - the goal of which was to create a BitTorrent engine that sat on top of the Virtual Memory Manager.

zen.trunk's People

Contributors

dementeddevil avatar im-adrianlewis avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

zen.trunk's Issues

Remove hard-coded use of 8192 byte page size.

As a system developer I would like to establish whether a larger page size will be more efficient in storing and streaming audio data than the current 8192 byte data pages.

Update the code so that the page size can be easily changed and all other metrics which depend upon it are updated accordingly. Paying particular attention to logs and distribution pages.

Acceptance Criteria
Given a page size of 8192 bytes
When I store an audio stream
Then the system behaves as it currently does

Given a page size of 16384 bytes
When I store an audio stream
Then the system consumes approximately half as many pages as the 8k test

Given a page size of 32768 bytes
When I store an audio stream
Then the system consumes approximately half as many pages as the 8k test

What parts do you think are portable to .NET Core and which parts aren't?

The code in this repo is very impressive. Do you have an idea as to how much of it is portable to .NET Core and how much isn't? I know the virtual memory manager is probably the part that is most likely to remain Windows-only.
I'm very interested in this as a possible all-C# replacement for sqlite. While sqlite is very good, if I want to customise things with the actual sqlite database engine I have to write C code which is something I'm not very good at and do not have time to learn.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.