Volumes, Needles & File IDs
Haystack-style storage: assigning ids, writing to volume servers, and reading replicas
Underneath the S3 API, SeaweedFS stores needles in volumes, the
model from Facebook's Haystack paper. labs/needles.py skips the S3
gateway and the filer and talks to the master and the volume servers
directly.
docker compose -f compose_prod.yaml run --rm labs python needles.py
assign -> fid 4,01def127163a on volume-1:8080
volume id 4, needle key + cookie 01def127163a (cookie stops id guessing)
upload: {'name': 'hello.txt', 'size': 19, 'eTag': 'f1d919b5', 'mime': 'text/plain'}
volume 4 is on ['volume-1:8080', 'volume-3:8080'] (replication 010: two copies, different racks)
GET http://volume-1:8080/4,01def127163a -> hello from a needle
GET http://volume-3:8080/4,01def127163a -> hello from a needle
rack1/volume-1:8080: volume 4 size 171184 files 4 replication {'rack': 1}
rack3/volume-3:8080: volume 4 size 171184 files 4 replication {'rack': 1}The file id
- Volume id (
4): which volume. The master maps it to servers. - Needle key (
01de): the file's number inside the volume, the key of the volume's in-memory index. - Cookie (
f127163a): 32 random bits stored with the needle and checked on every read. Without it, anyone could read every file by counting keys upward.
The four steps
An application can use this layer directly and store the fid in its own database. That's the fastest way to use SeaweedFS (no filer lookup per read) and how it's used for very large photo or thumbnail stores. Most applications are better served by S3, which adds names, buckets, permissions and portability.
Inside a volume
Writes are appends. Deleting a file writes a tombstone and leaves the
bytes in place; overwriting a key appends a new needle. The space comes
back only when the volume is vacuumed (compacted): copied without the
dead needles. The master triggers vacuum when a volume's garbage passes
-garbageThreshold (30% by default), and you can run volume.vacuum in
weed shell.
Volume files are large on purpose. A file system with a billion small files spends most of its time on metadata: directory lookups, inodes, more seeks than reads. A volume server with a billion needles holds thirty thousand files and an in-memory index of about 16 bytes per needle, so any read is one seek.
Volumes fill up
A volume becomes read-only when it reaches the size limit, and the
master grows new volumes to keep enough writable ones per collection. A
server with -max=30 can hold 30 volumes. When every server is full,
assign fails and so do writes, even though plenty of other volumes still
have free space for reads. The Writable volumes by collection panel
on the dashboard watches exactly this.
Lab
- Run
needles.pytwice. Do both runs get the same volume? - Change one character of the cookie in the fid and GET it. What do you get?
- In
weed shell(lockfirst), runvolume.listand find volume 4 on both servers. Delete some objects, then runvolume.vacuum -garbageThreshold=0.0001and compare sizes.