diff options
Diffstat (limited to 'src/research')
| -rw-r--r-- | src/research/encoding-binary-data-into-dna-sequence.md | 345 | ||||
| -rw-r--r-- | src/research/using-digitalocean-spaces-object-storage-with-fuse.md | 260 |
2 files changed, 605 insertions, 0 deletions
diff --git a/src/research/encoding-binary-data-into-dna-sequence.md b/src/research/encoding-binary-data-into-dna-sequence.md new file mode 100644 index 0000000..3bfeaab --- /dev/null +++ b/src/research/encoding-binary-data-into-dna-sequence.md | |||
| @@ -0,0 +1,345 @@ | |||
| 1 | title: Encoding binary data into DNA sequence | ||
| 2 | date: 2019-01-03 | ||
| 3 | tags: research | ||
| 4 | hide: false | ||
| 5 | ---- | ||
| 6 | |||
| 7 | ## Initial thoughts | ||
| 8 | |||
| 9 | Imagine a world where you could go outside and take a leaf from a tree and put it through your personal DNA sequencer and get data like music, videos or computer programs from it. Well, this is all possible now. It was not done on a large scale because it is quite expensive to create DNA strands but it's possible. | ||
| 10 | |||
| 11 | Encoding data into DNA sequence is relatively simple process once you understand the relationship between binary data and nucleotides and scientists have been making large leaps in this field in order to provide viable long-term storage solution for our data that would potentially survive our specie if case of global disaster. We could imprint all the world's knowledge into plants and ensure the survival of our knowledge. | ||
| 12 | |||
| 13 | More optimistic usage for this technology would be easier storage of ever growing data we produce every day. Once machines for sequencing DNA become fast enough and cheaper this could mean the next evolution of storing data and abandoning classical hard and solid state drives in data warehouses. | ||
| 14 | |||
| 15 | As we currently stand this is still not viable but it is quite an amazing and cool technology. | ||
| 16 | |||
| 17 | My interests in this field are purely in encoding processes and experimental testing mainly because I don't have the access to this expensive machines. My initial goal was to create a toolkit that can be used by everybody to encode their data into a proper DNA sequence. | ||
| 18 | |||
| 19 | ## Glossary | ||
| 20 | |||
| 21 | **deoxyribose** | ||
| 22 | : A five-carbon sugar molecule with a hydrogen atom rather than a hydroxyl group in the 2′ position; the sugar component of DNA nucleotides. | ||
| 23 | |||
| 24 | **double helix** | ||
| 25 | : The molecular shape of DNA in which two strands of nucleotides wind around each other in a spiral shape. | ||
| 26 | |||
| 27 | **nitrogenous base** | ||
| 28 | : A nitrogen-containing molecule that acts as a base; often referring to one of the purine or pyrimidine components of nucleic acids. | ||
| 29 | |||
| 30 | **phosphate group** | ||
| 31 | : A molecular group consisting of a central phosphorus atom bound to four oxygen atoms. | ||
| 32 | |||
| 33 | **RGB** | ||
| 34 | : The RGB color model is an additive color model in which red, green and blue light are added together in various ways to reproduce a broad array of colors. | ||
| 35 | |||
| 36 | **GCC** | ||
| 37 | : The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. | ||
| 38 | |||
| 39 | ## Data encoding | ||
| 40 | |||
| 41 | **TL;DR:** Encoding involves the use of a code to change original data into a form that can be used by an external process [^1]. | ||
| 42 | |||
| 43 | Encoding is the process of converting data into a format required for a number of information processing needs, including: | ||
| 44 | |||
| 45 | - Program compiling and execution | ||
| 46 | - Data transmission, storage and compression/decompression | ||
| 47 | - Application data processing, such as file conversion | ||
| 48 | |||
| 49 | Encoding can have two meanings[^1]: | ||
| 50 | |||
| 51 | - In computer technology, encoding is the process of applying a specific code, such as letters, symbols and numbers, to data for conversion into an equivalent cipher. | ||
| 52 | - In electronics, encoding refers to analog to digital conversion. | ||
| 53 | |||
| 54 | ## Quick history of DNA | ||
| 55 | |||
| 56 | - **1869** - Friedrich Miescher identifies "nuclein". | ||
| 57 | - **1900s** - The Eugenics Movement. | ||
| 58 | - **1900** – Mendel's theories are rediscovered by researchers. | ||
| 59 | - **1944** - Oswald Avery identifies DNA as the 'transforming principle'. | ||
| 60 | - **1952** - Rosalind Franklin photographs crystallized DNA fibres. | ||
| 61 | - **1953** - James Watson and Francis Crick discover the double helix structure of DNA. | ||
| 62 | - **1965** - Marshall Nirenberg is the first person to sequence the bases in each codon. | ||
| 63 | - **1983** - Huntington's disease is the first mapped genetic disease. | ||
| 64 | - **1990** - The Human Genome Project begins. | ||
| 65 | - **1995** - Haemophilus Influenzae is the first bacterium genome sequenced. | ||
| 66 | - **1996** - Dolly the sheep is cloned. | ||
| 67 | - **1999** - First human chromosome is decoded. | ||
| 68 | - **2000** – Genetic code of the fruit fly is decoded. | ||
| 69 | - **2002** – Mouse is the first mammal to have its genome decoded. | ||
| 70 | - **2003** – The Human Genome Project is completed. | ||
| 71 | - **2013** – DNA Worldwide and Eurofins Forensic discover identical twins have differences in their genetic makeup [^2]. | ||
| 72 | |||
| 73 | ## What is DNA? | ||
| 74 | |||
| 75 | Deoxyribonucleic acid, a self-replicating material which is **present in nearly all living organisms** as the main constituent of chromosomes. It is the **carrier of genetic information**. | ||
| 76 | |||
| 77 | > The nitrogen in our DNA, the calcium in our teeth, the iron in our blood, the carbon in our apple pies were made in the interiors of collapsing stars. We are made of starstuff. | ||
| 78 | > | ||
| 79 | > **-- Carl Sagan, Cosmos** | ||
| 80 | |||
| 81 | The nucleotide in DNA consists of a sugar (deoxyribose), one of four bases (cytosine (C), thymine (T), adenine (A), guanine (G)), and a phosphate. Cytosine and thymine are pyrimidine bases, while adenine and guanine are purine bases. The sugar and the base together are called a nucleoside. | ||
| 82 | |||
| 83 |  | ||
| 84 | |||
| 85 | *DNA (a) forms a double stranded helix, and (b) adenine pairs with thymine and cytosine pairs with guanine. (credit a: modification of work by Jerome Walker, Dennis Myts) [^3]* | ||
| 86 | |||
| 87 | ## Encode binary data into DNA sequence | ||
| 88 | |||
| 89 | As an input file you can use any file you want: | ||
| 90 | - ASCII files, | ||
| 91 | - Compiled programs, | ||
| 92 | - Multimedia files (MP3, MP4, MVK, etc), | ||
| 93 | - Images, | ||
| 94 | - Database files, | ||
| 95 | - etc. | ||
| 96 | |||
| 97 | Note: If you would copy all the bytes from RAM to file or pipe data to file you could encode also this data as long as you provide file pointer to the encoder. | ||
| 98 | |||
| 99 | ### Basic Encoding | ||
| 100 | |||
| 101 | As already mentioned, the Basic Encoding is based on a simple mapping. Since DNA is composed of 4 nucleotides (Adenine, Cytosine, Guanine, Thymine; usually referred using the first letter). Using this technique we can encode | ||
| 102 | |||
| 103 | $$ log_2(4) = log_2(2^2) = 2 bits $$ | ||
| 104 | |||
| 105 | using a single nucleotide. In this way, we are able to use the 4 bases that compose the DNA strand to encode each byte of data. | ||
| 106 | |||
| 107 | | Two bits | Nucleotides | | ||
| 108 | | -------- | ---------------- | | ||
| 109 | | 00 | **A** (Adenine) | | ||
| 110 | | 10 | **G** (Guanine) | | ||
| 111 | | 01 | **C** (Cytosine) | | ||
| 112 | | 11 | **T** (Thymine) | | ||
| 113 | |||
| 114 | With this in mind we can simply encode any data by using two-bit to Nucleotides conversion | ||
| 115 | |||
| 116 | ```pascal | ||
| 117 | { Algorithm 1: Naive byte array to DNA encode } | ||
| 118 | procedure EncodeToDNASequence(f) string | ||
| 119 | begin | ||
| 120 | enc string | ||
| 121 | while not eof(f) do | ||
| 122 | c byte := buffer[0] { Read 1 byte from buffer } | ||
| 123 | bin integer := sprintf('08b', c) { Convert to string binary } | ||
| 124 | for e in range[0, 2, 4, 6] do | ||
| 125 | if e[0] == 48 and e[1] == 48 then { 0x00 - A (Adenine) } | ||
| 126 | enc += 'A' | ||
| 127 | else if e[0] == 48 and e[1] == 49 then { 0x01 - G (Guanine) } | ||
| 128 | enc += 'G' | ||
| 129 | else if e[0] == 49 and e[1] == 48 then { 0x10 - C (Cytosine) } | ||
| 130 | enc += 'C' | ||
| 131 | else if e[0] == 49 and e[1] == 49 then { 0x11 - T (Thymine) } | ||
| 132 | enc += 'T' | ||
| 133 | return enc { Return DNA sequence } | ||
| 134 | end | ||
| 135 | ``` | ||
| 136 | |||
| 137 | Another encoding would be **Goldman encoding**. Using this encoding helps with Nonsense mutation (amino acids replaced by a stop codon) that occurs and is the most problematic during translation because it leads to truncated amino acid sequences, which in turn results in truncated proteins. [^4] | ||
| 138 | |||
| 139 | [Where to store big data? In DNA: Nick Goldman at TEDxPrague](https://www.youtube.com/watch?v=a4PiGWNsIEU) | ||
| 140 | |||
| 141 | ### FASTA file format | ||
| 142 | |||
| 143 | In bioinformatics, FASTA format is a text-based format for representing either nucleotide sequences or peptide sequences, in which nucleotides or amino acids are represented using single-letter codes. The format also allows for sequence names and comments to precede the sequences. The format originates from the FASTA software package, but has now become a standard in the field of bioinformatics. [^5] | ||
| 144 | |||
| 145 | The first line in a FASTA file started either with a ">" (greater-than) symbol or, less frequently, a ";" (semicolon) was taken as a comment. Subsequent lines starting with a semicolon would be ignored by software. Since the only comment used was the first, it quickly became used to hold a summary description of the sequence, often starting with a unique library accession number, and with time it has become commonplace to always use ">" for the first line and to not use ";" comments (which would otherwise be ignored). | ||
| 146 | |||
| 147 | ```text | ||
| 148 | ;LCBO - Prolactin precursor - Bovine | ||
| 149 | ; a sample sequence in FASTA format | ||
| 150 | MDSKGSSQKGSRLLLLLVVSNLLLCQGVVSTPVCPNGPGNCQVSLRDLFDRAVMVSHYIHDLSS | ||
| 151 | EMFNEFDKRYAQGKGFITMALNSCHTSSLPTPEDKEQAQQTHHEVLMSLILGLLRSWNDPLYHL | ||
| 152 | VTEVRGMKGAPDAILSRAIEIEEENKRLLEGMEMIFGQVIPGAKETEPYPVWSGLPSLQTKDED | ||
| 153 | ARYSAFYNLLHCLRRDSSKIDTYLKLLNCRIIYNNNC* | ||
| 154 | |||
| 155 | >MCHU - Calmodulin - Human, rabbit, bovine, rat, and chicken | ||
| 156 | ADQLTEEQIAEFKEAFSLFDKDGDGTITTKELGTVMRSLGQNPTEAELQDMINEVDADGNGTID | ||
| 157 | FPEFLTMMARKMKDTDSEEEIREAFRVFDKDGNGYISAAELRHVMTNLGEKLTDEEVDEMIREA | ||
| 158 | DIDGDGQVNYEEFVQMMTAK* | ||
| 159 | |||
| 160 | >gi|5524211|gb|AAD44166.1| cytochrome b [Elephas maximus maximus] | ||
| 161 | LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV | ||
| 162 | EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG | ||
| 163 | LLILILLLLLLALLSPDMLGDPDNHMPADPLNTPLHIKPEWYFLFAYAILRSVPNKLGGVLALFLSIVIL | ||
| 164 | GLMPFLHTSKHRSMMLRPLSQALFWTLTMDLLTLTWIGSQPVEYPYTIIGQMASILYFSIILAFLPIAGX | ||
| 165 | IENY | ||
| 166 | ``` | ||
| 167 | |||
| 168 | FASTA format was extended by [FASTQ](https://en.wikipedia.org/wiki/FASTQ_format) format from the [Sanger Centre](https://www.sanger.ac.uk/) in Cambridge. | ||
| 169 | |||
| 170 | ### PNG encoded DNA sequence | ||
| 171 | |||
| 172 | | Nucleotides | RGB | Color name | | ||
| 173 | | ------------- | ----------- | ---------- | | ||
| 174 | | A -> Adenine | (0,0,255) | Blue | | ||
| 175 | | G -> Guanine | (0,100,0) | Green | | ||
| 176 | | C -> Cytosine | (255,0,0) | Red | | ||
| 177 | | T -> Thymine | (255,255,0) | Yellow | | ||
| 178 | |||
| 179 | With this in mind we can create a simple algorithm to create PNG representation of a DNA sequence. | ||
| 180 | |||
| 181 | ```pascal | ||
| 182 | { Algorithm 2: Naive DNA to PNG encode from FASTA file } | ||
| 183 | procedure EncodeDNASequenceToPNG(f) | ||
| 184 | begin | ||
| 185 | i image | ||
| 186 | while not eof(f) do | ||
| 187 | c char := buffer[0] { Read 1 char from buffer } | ||
| 188 | case c of | ||
| 189 | 'A': color := RGB(0, 0, 255) { Blue } | ||
| 190 | 'G': color := RGB(0, 100, 0) { Green } | ||
| 191 | 'C': color := RGB(255, 0, 0) { Red } | ||
| 192 | 'T': color := RGB(255, 255, 0) { Yellow } | ||
| 193 | drawRect(i, [x, y], color) | ||
| 194 | save(i) { Save PNG image } | ||
| 195 | end | ||
| 196 | ``` | ||
| 197 | |||
| 198 | ## Encoding text file in practice | ||
| 199 | |||
| 200 | In this example we will take a simple text file as our input stream for encoding. This file will have a quote from Niels Bohr and saved as txt file. | ||
| 201 | |||
| 202 | > How wonderful that we have met with a paradox. Now we have some hope of making progress. | ||
| 203 | > ― Niels Bohr | ||
| 204 | |||
| 205 | First we encode text file into FASTA file. | ||
| 206 | |||
| 207 | ```bash | ||
| 208 | ./dnae-encode -i quote.txt -o quote.fa | ||
| 209 | 2019/01/10 00:38:29 Gathering input file stats | ||
| 210 | 2019/01/10 00:38:29 Starting encoding ... | ||
| 211 | 106 B / 106 B [==================================] 100.00% 0s | ||
| 212 | 2019/01/10 00:38:29 Saving to FASTA file ... | ||
| 213 | 2019/01/10 00:38:29 Output FASTA file length is 438 B | ||
| 214 | 2019/01/10 00:38:29 Process took 987.263µs | ||
| 215 | 2019/01/10 00:38:29 Done ... | ||
| 216 | ``` | ||
| 217 | |||
| 218 | Output of `quote.fa` file contains the encoded DNA sequence in ASCII format. | ||
| 219 | |||
| 220 | ```text | ||
| 221 | >SEQ1 | ||
| 222 | GACAGCTTGTGTACAAGTGTGCTTGCTCGCGAGCGGGTACGCGCGTGGGCTAACAAGTGA | ||
| 223 | GCCAGCAGGTGAACAAGTGTGCGGACAAGCCAGCAGGTGCGCGGACAAGCTGGCGGGTGA | ||
| 224 | ACAAGTGTGCCGGTGAGCCAACAAGCAGACAAGTAAGCAGGTACGCAGGCGAGCTTGTCA | ||
| 225 | ACTCACAAGATCGCTTGTGTACAAGTGTGCGGACAAGCCAGCAGGTGCGCGGACAAGTAT | ||
| 226 | GCTTGCTGGCGGACAAGCCAGCTTGTAAGCGGACAAGCTTGCGCACAAGCTGGCAGGCCT | ||
| 227 | GCCGGCTCGCGTACAAATTCACAAGTAAGTACGCTTGCGTGTACGCGGGTATGTATACTC | ||
| 228 | AACCTCACCAAACGGGACAAGATCGCCGGCGGGCTAGTATACAAGAACGCTTGCCAGTAC | ||
| 229 | AACC | ||
| 230 | ``` | ||
| 231 | |||
| 232 | Then we encode FASTA file from previous operation to encode this data into PNG. | ||
| 233 | |||
| 234 | ```bash | ||
| 235 | ./dnae-png -i quote.fa -o quote.png | ||
| 236 | 2019/01/10 00:40:09 Gathering input file stats ... | ||
| 237 | 2019/01/10 00:40:09 Deconstructing FASTA file ... | ||
| 238 | 2019/01/10 00:40:09 Compositing image file ... | ||
| 239 | 424 / 424 [==================================] 100.00% 0s | ||
| 240 | 2019/01/10 00:40:09 Saving output file ... | ||
| 241 | 2019/01/10 00:40:09 Output image file length is 1.1 kB | ||
| 242 | 2019/01/10 00:40:09 Process took 19.036117ms | ||
| 243 | 2019/01/10 00:40:09 Done ... | ||
| 244 | ``` | ||
| 245 | |||
| 246 | After encoding into PNG format this file looks like this. | ||
| 247 | |||
| 248 |  | ||
| 249 | |||
| 250 | The larger the input stream is the larger the PNG file would be. | ||
| 251 | |||
| 252 | Compiled basic Hello World C program with [GCC](https://www.gnu.org/software/gcc/) would [look like](/files/dna-sequence/sample.png). | ||
| 253 | |||
| 254 | ```c | ||
| 255 | // gcc -O3 -o sample sample.c | ||
| 256 | #include <stdio.h> | ||
| 257 | |||
| 258 | main() { | ||
| 259 | printf("Hello, world!\n"); | ||
| 260 | return 0; | ||
| 261 | } | ||
| 262 | ``` | ||
| 263 | |||
| 264 | ## Toolkit for encoding data | ||
| 265 | |||
| 266 | I have created a toolkit with two main programs: | ||
| 267 | - dnae-encode (encodes file into FASTA file) | ||
| 268 | - dnae-png (encodes FASTA file into PNG) | ||
| 269 | |||
| 270 | Toolkit with full source code is available on [github.com/mitjafelicijan/dna-encoding](https://github.com/mitjafelicijan/dna-encoding). | ||
| 271 | |||
| 272 | ### dnae-encode | ||
| 273 | |||
| 274 | ```bash | ||
| 275 | > ./dnae-encode --help | ||
| 276 | usage: dnae-encode --input=INPUT [<flags>] | ||
| 277 | |||
| 278 | A command-line application that encodes file into DNA sequence. | ||
| 279 | |||
| 280 | Flags: | ||
| 281 | --help Show context-sensitive help (also try --help-long and --help-man). | ||
| 282 | -i, --input=INPUT Input file (ASCII or binary) which will be encoded into DNA sequence. | ||
| 283 | -o, --output="out.fa" Output file which stores DNA sequence in FASTA format. | ||
| 284 | -s, --sequence=SEQ1 The description line (defline) or header/identifier line, gives a name and/or a unique identifier for the sequence. | ||
| 285 | -c, --columns=60 Row characters length (no more than 120 characters). Devices preallocate fixed line sizes in software. | ||
| 286 | --version Show application version. | ||
| 287 | ``` | ||
| 288 | |||
| 289 | ### dnae-png | ||
| 290 | |||
| 291 | ```bash | ||
| 292 | > ./dnae-png --help | ||
| 293 | usage: dnae-png --input=INPUT [<flags>] | ||
| 294 | |||
| 295 | A command-line application that encodes FASTA file into PNG image. | ||
| 296 | |||
| 297 | Flags: | ||
| 298 | --help Show context-sensitive help (also try --help-long and --help-man). | ||
| 299 | -i, --input=INPUT Input FASTA file which will be encoded into PNG image. | ||
| 300 | -o, --output="out.png" Output file in PNG format that represents DNA sequence in graphical way. | ||
| 301 | -s, --size=10 Size of pairings of DNA bases on image in pixels (lower resolution lower file size). | ||
| 302 | --version Show application version. | ||
| 303 | ``` | ||
| 304 | |||
| 305 | ## Benchmarks | ||
| 306 | |||
| 307 | First we generate some binary sample data with dd. | ||
| 308 | |||
| 309 | ```bash | ||
| 310 | dd if=<(openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero) of=1KB.bin bs=1KB count=1 iflag=fullblock | ||
| 311 | ``` | ||
| 312 | |||
| 313 | Our freshly generated 1KB file looks something like this (its full of garbage data as intended). | ||
| 314 | |||
| 315 |  | ||
| 316 | |||
| 317 | We create following binary files: | ||
| 318 | - 1KB.bin | ||
| 319 | - 10KB.bin | ||
| 320 | - 100KB.bin | ||
| 321 | - 1MB.bin | ||
| 322 | - 10MB.bin | ||
| 323 | - 100MB.bin | ||
| 324 | |||
| 325 | After this we create FASTA files for all the binary files by encoding them into DNA sequence. | ||
| 326 | |||
| 327 | ```bash | ||
| 328 | ./dnae-encode -i 100MB.bin -o 100MB.fa | ||
| 329 | ``` | ||
| 330 | |||
| 331 | Then we GZIP all the FASTA files to see how much the can be compressed. | ||
| 332 | |||
| 333 | ```bash | ||
| 334 | gzip -9 < 10MB.fa > 10MB.fa.gz | ||
| 335 | ``` | ||
| 336 | |||
| 337 | [Download ODS file with benchmarks](/files/dna-sequence/benchmarks.ods). | ||
| 338 | |||
| 339 | ## References | ||
| 340 | |||
| 341 | [^1]: https://www.techopedia.com/definition/948/encoding | ||
| 342 | [^2]: https://www.dna-worldwide.com/resource/160/history-dna-timeline | ||
| 343 | [^3]: https://opentextbc.ca/biology/chapter/9-1-the-structure-of-dna/ | ||
| 344 | [^4]: https://arxiv.org/abs/1801.04774 | ||
| 345 | [^5]: https://en.wikipedia.org/wiki/FASTA_format | ||
diff --git a/src/research/using-digitalocean-spaces-object-storage-with-fuse.md b/src/research/using-digitalocean-spaces-object-storage-with-fuse.md new file mode 100644 index 0000000..099fbef --- /dev/null +++ b/src/research/using-digitalocean-spaces-object-storage-with-fuse.md | |||
| @@ -0,0 +1,260 @@ | |||
| 1 | title: Using DigitalOcean Spaces Object Storage with FUSE | ||
| 2 | date: 2018-01-16 | ||
| 3 | tags: research | ||
| 4 | hide: false | ||
| 5 | ---- | ||
| 6 | |||
| 7 | Couple of months ago [DigitalOcean](https://www.digitalocean.com) introduced new product called [Spaces](https://blog.digitalocean.com/introducing-spaces-object-storage/) which is Object Storage very similar to Amazon's S3. This really peaked my interest, because this was something I was missing and even the thought of going over the internet for such functionality was in no interest to me. Also in fashion with their previous pricing this also is very cheap and pricing page is a no-brainer compared to AWS or GCE. [Prices are clearly and precisely defined and outlined](https://www.digitalocean.com/pricing/). You must love them for that :) | ||
| 8 | |||
| 9 | ### Initial requirements | ||
| 10 | |||
| 11 | * Is it possible to use them as a mounted drive with FUSE? (tl;dr YES) | ||
| 12 | * Will the performance degrade over time and over different sizes of objects? (tl;dr NO&YES) | ||
| 13 | * Can storage be mounted on multiple machines at the same time and be writable? (tl;dr YES) | ||
| 14 | |||
| 15 | > Let me be clear. This scripts I use are made just for benchmarking and are not intended to be used in real-life situations. Besides that, I am looking into using this approaches but adding caching service in front of it and then dumping everything as an object to storage. This could potentially be some interesting post of itself. But in case you would need real-time data without eventual consistency please take this scripts as they are: not usable in such situations. | ||
| 16 | |||
| 17 | ## Is it possible to use them as a mounted drive with FUSE? | ||
| 18 | |||
| 19 | Well, actually they can be used in such manor. Because they are similar to [AWS S3](https://aws.amazon.com/s3/) many tools are available and you can find many articles and [Stackoverflow items](https://stackoverflow.com/search?q=s3+fuse). | ||
| 20 | |||
| 21 | To make this work you will need DigitalOcean account. If you don't have one you will not be able to test this code. But if you have an account then you go and [create new Droplet](https://cloud.digitalocean.com/droplets/new?size=s-1vcpu-1gb®ion=ams3&distro=debian&distroImage=debian-9-x64&options=private_networking,install_agent). If you click on this link you will already have preselected Debian 9 with smallest VM option. | ||
| 22 | |||
| 23 | * Please be sure to add you SSH key, because we will login to this machine remotely. | ||
| 24 | * If you change your region please remember which one you choose because we will need this information when we try to mount space to our machine. | ||
| 25 | |||
| 26 | Instuctions on how to use SSH keys and how to setup them are available in article [How To Use SSH Keys with DigitalOcean Droplets](https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-digitalocean-droplets). | ||
| 27 | |||
| 28 |  | ||
| 29 | |||
| 30 | After we created Droplet it's time to create new Space. This is done by clicking on a button [Create](https://cloud.digitalocean.com/spaces/new) (right top corner) and selecting Spaces. Choose pronounceable ```Unique name``` because we will use it in examples below. You can either choose Private or Public, it doesn't matter in our case. And you can always change that in the future. | ||
| 31 | |||
| 32 | When you have created new Space we should [generate Access key](https://cloud.digitalocean.com/settings/api/tokens). This link will guide to the page when you can generate this key. After you create new one, please save provided Key and Secret because Secret will not be shown again. | ||
| 33 | |||
| 34 |  | ||
| 35 | |||
| 36 | Now that we have new Space and Access key we should SSH into our machine. | ||
| 37 | |||
| 38 | ```bash | ||
| 39 | # replace IP with the ip of your newly created droplet | ||
| 40 | ssh root@IP | ||
| 41 | |||
| 42 | # this will install utilities for mounting storage objects as FUSE | ||
| 43 | apt install s3fs | ||
| 44 | |||
| 45 | # we now need to provide credentials (access key we created earlier) | ||
| 46 | # replace KEY and SECRET with your own credentials but leave the colon between them | ||
| 47 | # we also need to set proper permissions | ||
| 48 | echo "KEY:SECRET" > .passwd-s3fs | ||
| 49 | chmod 600 .passwd-s3fs | ||
| 50 | |||
| 51 | # now we mount space to our machine | ||
| 52 | # replace UNIQUE-NAME with the name you choose earlier | ||
| 53 | # if you choose different region for your space be careful about -ourl option (ams3) | ||
| 54 | s3fs UNIQUE-NAME /mnt/ -ourl=https://ams3.digitaloceanspaces.com -ouse_cache=/tmp | ||
| 55 | |||
| 56 | # now we try to create a file | ||
| 57 | # once you mount it may take a couple of seconds to retrieve data | ||
| 58 | echo "Hello cruel world" > /mnt/hello.txt | ||
| 59 | ``` | ||
| 60 | |||
| 61 | After all this you can return to your browser and go to [DigitalOcean Spaces](https://cloud.digitalocean.com/spaces) and click on your created space. If file hello.txt is present you have successfully mounted space to your machine and wrote data to it. | ||
| 62 | |||
| 63 | I choose the same region for my Droplet and my Space but you don't have to. You can have different regions. What this actually does to performance I don't know. | ||
| 64 | |||
| 65 | Additional information on FUSE: | ||
| 66 | |||
| 67 | * [Github project page for s3fs](https://github.com/s3fs-fuse/s3fs-fuse) | ||
| 68 | * [FUSE - Filesystem in Userspace](https://en.wikipedia.org/wiki/Filesystem_in_Userspace) | ||
| 69 | |||
| 70 | ## Will the performance degrade over time and over different sizes of objects? | ||
| 71 | |||
| 72 | For this task I didn't want to just read and write text files or uploading images. I actually wanted to figure out if using something like SQlite is viable in this case. | ||
| 73 | |||
| 74 | ### Measurement experiment 1: File copy | ||
| 75 | |||
| 76 | ```bash | ||
| 77 | # first we create some dummy files at different sizes | ||
| 78 | dd if=/dev/zero of=10KB.dat bs=1024 count=10 #10KB | ||
| 79 | dd if=/dev/zero of=100KB.dat bs=1024 count=100 #100KB | ||
| 80 | dd if=/dev/zero of=1MB.dat bs=1024 count=1024 #1MB | ||
| 81 | dd if=/dev/zero of=10MB.dat bs=1024 count=10240 #10MB | ||
| 82 | |||
| 83 | # now we set time command to only return real | ||
| 84 | TIMEFORMAT=%R | ||
| 85 | |||
| 86 | # now lets test it | ||
| 87 | (time cp 10KB.dat /mnt/) |& tee -a 10KB.results.txt | ||
| 88 | |||
| 89 | # and now we automate | ||
| 90 | # this will perform the same operation 100 times | ||
| 91 | # this will output results into separated files based on objecty size | ||
| 92 | n=0; while (( n++ < 100 )); do (time cp 10KB.dat /mnt/10KB.$n.dat) |& tee -a 10KB.results.txt; done | ||
| 93 | n=0; while (( n++ < 100 )); do (time cp 100KB.dat /mnt/100KB.$n.dat) |& tee -a 100KB.results.txt; done | ||
| 94 | n=0; while (( n++ < 100 )); do (time cp 1MB.dat /mnt/1MB.$n.dat) |& tee -a 1MB.results.txt; done | ||
| 95 | n=0; while (( n++ < 100 )); do (time cp 10MB.dat /mnt/10MB.$n.dat) |& tee -a 10MB.results.txt; done | ||
| 96 | ``` | ||
| 97 | |||
| 98 | Files of size 100MB were not successfully transferred and ended up displaying error (cp: failed to close '/mnt/100MB.1.dat': Operation not permitted). | ||
| 99 | |||
| 100 | As I suspected, object size is not really that important. Sadly I don't have the time to test performance over periods of time. But if some of you would do it please send me your data. I would be interested in seeing results. | ||
| 101 | |||
| 102 | **Here are plotted results** | ||
| 103 | |||
| 104 | You can download [raw result here](/files/do-fuse/copy-benchmarks.tsv). Measurements are in seconds. | ||
| 105 | |||
| 106 | <script src="//cdn.plot.ly/plotly-latest.min.js"></script> | ||
| 107 | <div id="copy-benchmarks"></div> | ||
| 108 | <script> | ||
| 109 | (function(){ | ||
| 110 | var request = new XMLHttpRequest(); | ||
| 111 | request.open("GET", "/files/do-fuse/copy-benchmarks.tsv", true); | ||
| 112 | request.onload = function() { | ||
| 113 | if (request.status >= 200 && request.status < 400) { | ||
| 114 | var payload = request.responseText.trim(); | ||
| 115 | var tsv = payload.split("\n"); | ||
| 116 | for (var i=0; i<tsv.length; i++) { tsv[i] = tsv[i].split("\t"); } | ||
| 117 | var traces = []; | ||
| 118 | var headers = tsv[0]; | ||
| 119 | tsv.shift(); | ||
| 120 | Array.prototype.forEach.call(headers, function(el, idx) { | ||
| 121 | var x = []; | ||
| 122 | var y = []; | ||
| 123 | for (var j=0; j<tsv.length; j++) { | ||
| 124 | x.push(j); | ||
| 125 | y.push(parseFloat(tsv[j][idx].replace(",", "."))); | ||
| 126 | } | ||
| 127 | traces.push({ x: x, y: y, type: "scatter", name: el, line: { width: 1, shape: "spline" } }); | ||
| 128 | }); | ||
| 129 | var copy = Plotly.newPlot("copy-benchmarks", traces, { legend: {"orientation": "h"}, height: 400, margin: { l: 40, r: 0, b: 20, t: 30, pad: 0 }, yaxis: { title: "execution time in seconds", titlefont: { size: 12 } }, xaxis: { title: "fn(i)", titlefont: { size: 12 } } }); | ||
| 130 | } else { } | ||
| 131 | }; | ||
| 132 | request.onerror = function() { }; | ||
| 133 | request.send(null); | ||
| 134 | })(); | ||
| 135 | </script> | ||
| 136 | |||
| 137 | As far as these tests show, performance is quite stable and can be predicted which is fantastic. But this is a small test and spans only over couple of hours. So you should not completely trust them. | ||
| 138 | |||
| 139 | ### Measurement experiment 2: SQLite performanse | ||
| 140 | |||
| 141 | I was unable to use database file directly from mounted drive so this is a no-go as I suspected. So I executed code below on a local disk just to get some benchmarks. I inserted 1000 records with DROPTABLE, CREATETABLE, INSERTMANY, FETCHALL, COMMIT for 1000 times to generate statistics. As you can see performance of SQLite is quite amazing. You could then potentially just copy file to mounted drive and be done with it. | ||
| 142 | |||
| 143 | ```python | ||
| 144 | import time | ||
| 145 | import sqlite3 | ||
| 146 | import sys | ||
| 147 | |||
| 148 | if len(sys.argv) < 3: | ||
| 149 | print("usage: python sqlite-benchmark.py DB_PATH NUM_RECORDS REPEAT") | ||
| 150 | exit() | ||
| 151 | |||
| 152 | def data_iter(x): | ||
| 153 | for i in range(x): | ||
| 154 | yield "m" + str(i), "f" + str(i*i) | ||
| 155 | |||
| 156 | header_line = "%s\t%s\t%s\t%s\t%s\n" % ("DROPTABLE", "CREATETABLE", "INSERTMANY", "FETCHALL", "COMMIT") | ||
| 157 | with open("sqlite-benchmarks.tsv", "w") as fp: | ||
| 158 | fp.write(header_line) | ||
| 159 | |||
| 160 | start_time = time.time() | ||
| 161 | conn = sqlite3.connect(sys.argv[1]) | ||
| 162 | c = conn.cursor() | ||
| 163 | end_time = time.time() | ||
| 164 | result_time = CONNECT = end_time - start_time | ||
| 165 | print("CONNECT: %g seconds" % (result_time)) | ||
| 166 | |||
| 167 | start_time = time.time() | ||
| 168 | c.execute("PRAGMA journal_mode=WAL") | ||
| 169 | c.execute("PRAGMA temp_store=MEMORY") | ||
| 170 | c.execute("PRAGMA synchronous=OFF") | ||
| 171 | result_time = PRAGMA = end_time - start_time | ||
| 172 | print("PRAGMA: %g seconds" % (result_time)) | ||
| 173 | |||
| 174 | for i in range(int(sys.argv[3])): | ||
| 175 | print("#%i" % (i)) | ||
| 176 | |||
| 177 | start_time = time.time() | ||
| 178 | c.execute("drop table if exists test") | ||
| 179 | end_time = time.time() | ||
| 180 | result_time = DROPTABLE = end_time - start_time | ||
| 181 | print("DROPTABLE: %g seconds" % (result_time)) | ||
| 182 | |||
| 183 | start_time = time.time() | ||
| 184 | c.execute("create table if not exists test(a,b)") | ||
| 185 | end_time = time.time() | ||
| 186 | result_time = CREATETABLE = end_time - start_time | ||
| 187 | print("CREATETABLE: %g seconds" % (result_time)) | ||
| 188 | |||
| 189 | start_time = time.time() | ||
| 190 | c.executemany("INSERT INTO test VALUES (?, ?)", data_iter(int(sys.argv[2]))) | ||
| 191 | end_time = time.time() | ||
| 192 | result_time = INSERTMANY = end_time - start_time | ||
| 193 | print("INSERTMANY: %g seconds" % (result_time)) | ||
| 194 | |||
| 195 | start_time = time.time() | ||
| 196 | c.execute("select count(*) from test") | ||
| 197 | res = c.fetchall() | ||
| 198 | end_time = time.time() | ||
| 199 | result_time = FETCHALL = end_time - start_time | ||
| 200 | print("FETCHALL: %g seconds" % (result_time)) | ||
| 201 | |||
| 202 | start_time = time.time() | ||
| 203 | conn.commit() | ||
| 204 | end_time = time.time() | ||
| 205 | result_time = COMMIT = end_time - start_time | ||
| 206 | print("COMMIT: %g seconds" % (result_time)) | ||
| 207 | |||
| 208 | |||
| 209 | log_line = "%f\t%f\t%f\t%f\t%f\n" % (DROPTABLE, CREATETABLE, INSERTMANY, FETCHALL, COMMIT) | ||
| 210 | with open("sqlite-benchmarks.tsv", "a") as fp: | ||
| 211 | fp.write(log_line) | ||
| 212 | |||
| 213 | start_time = time.time() | ||
| 214 | conn.close() | ||
| 215 | end_time = time.time() | ||
| 216 | result_time = CLOSE = end_time - start_time | ||
| 217 | print("CLOSE: %g seconds" % (result_time)) | ||
| 218 | ``` | ||
| 219 | |||
| 220 | You can download [raw result here](/files/do-fuse/sqlite-benchmarks.tsv). And again, these results are done on a local block storage and do not represent capabilities of object storage. With my current approach and state of the test code these can not be done. I would need to make Python code much more robust and check locking etc. | ||
| 221 | |||
| 222 | <div id="sqlite-benchmarks"></div> | ||
| 223 | <script> | ||
| 224 | (function(){ | ||
| 225 | var request = new XMLHttpRequest(); | ||
| 226 | request.open("GET", "/files/do-fuse/sqlite-benchmarks.tsv", true); | ||
| 227 | request.onload = function() { | ||
| 228 | if (request.status >= 200 && request.status < 400) { | ||
| 229 | var payload = request.responseText.trim(); | ||
| 230 | var tsv = payload.split("\n"); | ||
| 231 | for (var i=0; i<tsv.length; i++) { tsv[i] = tsv[i].split("\t"); } | ||
| 232 | var traces = []; | ||
| 233 | var headers = tsv[0]; | ||
| 234 | tsv.shift(); | ||
| 235 | Array.prototype.forEach.call(headers, function(el, idx) { | ||
| 236 | var x = []; | ||
| 237 | var y = []; | ||
| 238 | for (var j=0; j<tsv.length; j++) { | ||
| 239 | x.push(j); | ||
| 240 | y.push(parseFloat(tsv[j][idx].replace(",", "."))); | ||
| 241 | } | ||
| 242 | traces.push({ x: x, y: y, type: "scatter", name: el, line: { width: 1, shape: "spline" } }); | ||
| 243 | }); | ||
| 244 | var sqlite = Plotly.newPlot("sqlite-benchmarks", traces, { legend: {"orientation": "h"}, height: 400, margin: { l: 50, r: 0, b: 20, t: 30, pad: 0 }, yaxis: { title: "execution time in seconds", titlefont: { size: 12 } } }); | ||
| 245 | } else { } | ||
| 246 | }; | ||
| 247 | request.onerror = function() { }; | ||
| 248 | request.send(null); | ||
| 249 | })(); | ||
| 250 | </script> | ||
| 251 | |||
| 252 | ## Can storage be mounted on multiple machines at the same time and be writable? | ||
| 253 | |||
| 254 | Well, this one didn't take long to test. And the answer is **YES**. I mounted space on both machines and measured same performance on both machines. But because file is downloaded before write and then uploaded on complete there could potentially be problems is another process is trying to access the same file. | ||
| 255 | |||
| 256 | ## Observations and conslusion | ||
| 257 | |||
| 258 | Using Spaces in this way makes it easier to access and manage files. But besides that you would need to write additional code to make this one play nice with you applications. | ||
| 259 | |||
| 260 | Nevertheless, this was extremely simple to setup and use and this is just another excellent product in DigitalOcean product line. I found this exercise very valuable and am thinking about implementing some sort of mechanism for SQLite, so data can be stored on Spaces and accessed by many VM's. For a project where data doesn't need to be accessible in real-time and can have couple of minutes old data this would be very interesting. If any of you find this proposal interesting please write in a comment box below or shoot me an email and I will keep you posted. | ||
