Live data from Hacker News

Show HN: WAL Implementation in Golang

github.com

1–10 of 30 posts

Re: Show HN: WAL Implementation in Golang

#3

Did I miss it or is there no call to os.File.Sync(), i.e. fsync, anywhere? Since you mention etcd/wal: https://github.com/etcd-io/etcd/blob/v3.3.27/wal/wal.go#L671 https://github.com/etcd-io/etcd/blob/v3.3.27/pkg/fileutil/sy...

I used the bufio flush mechanism https://pkg.go.dev/bufio#Writer.Flush

Thanks for your comment, I'll definitely check it out. It was my first attempt at this. How can I make it better?

Re: Show HN: WAL Implementation in Golang

#4
post #3

Did I miss it or is there no call to os.File.Sync(), i.e. fsync, anywhere? Since you mention etcd/wal: https://github.com/etcd-io/etcd/blob/v3.3.27/wal/wal.go#L671 https://github.com/etcd-io/etcd/blob/v3.3.27/pkg/fileutil/sy...

I used the bufio flush mechanism https://pkg.go.dev/bufio#Writer.Flush Thanks for your comment, I'll definitely check it out. It was my first attempt at this. How can I make it better?

Essentially, unless you `fsync`, there's no guarantee that your data will be durably written to disk. This is because the operating system keeps data buffered in in-memory caches, so if the machine crashes you may lose some data. The `fsync` system call forces the data to be flushed from the in-memory OS cache to the disk. As far as I could tell, the Flush you use does not `fsync`.

Re: Show HN: WAL Implementation in Golang

#5
Besides what Phil mentioned below, I can't write more than one record to the WAL. You're closing the file after every write, the second time you write the error `seek data/rebuf.tmp: file already closed` is returned.

I also think your rotation will delete the wrong segment when you have more than ten segments - imagine you're writing rebuf-1 to rebuf-10 - what's the "oldest file" to delete now? Besides, should you really delete those files?

Re: Show HN: WAL Implementation in Golang

#6

Besides what Phil mentioned below, I can't write more than one record to the WAL. You're closing the file after every write, the second time you write the error `seek data/rebuf.tmp: file already closed` is returned. I also think your rotation will delete the wrong segment when you have more than ten segments - imagine you're writing rebuf-1 to rebuf-10 - what's the "oldest file" to delete now? Besides, should you re…

Yes there are a lot of bugs since I just wrote this in one sitting today. Will be fixing all of this. For log rotation, I'll sort by the last_modified_at ts and then purge those

Re: Show HN: WAL Implementation in Golang

#7
post #4
post #3

Earlier quoted context omitted.

I used the bufio flush mechanism https://pkg.go.dev/bufio#Writer.Flush Thanks for your comment, I'll definitely check it out. It was my first attempt at this. How can I make it better?

Essentially, unless you `fsync`, there's no guarantee that your data will be durably written to disk. This is because the operating system keeps data buffered in in-memory caches, so if the machine crashes you may lose some data. The `fsync` system call forces the data to be flushed from the in-memory OS cache to the disk. As far as I could tell, the Flush you use does not `fsync`.

Thanks for your input @sakras. I'll fix this

Re: Show HN: WAL Implementation in Golang

#8
OP here! Pls feel free to raise any bugs you encounter! I'll be doing the following immmediate fixes:

1. Use fsync for durable writes in case of system crashes

2. Fix log-rotation-purging logic

3. Fix `file already closed` bug on consecutive writes

4. Add CRC checksum

Re: Show HN: WAL Implementation in Golang

#10
post #6

Besides what Phil mentioned below, I can't write more than one record to the WAL. You're closing the file after every write, the second time you write the error `seek data/rebuf.tmp: file already closed` is returned. I also think your rotation will delete the wrong segment when you have more than ten segments - imagine you're writing rebuf-1 to rebuf-10 - what's the "oldest file" to delete now? Besides, should you re…

Yes there are a lot of bugs since I just wrote this in one sitting today. Will be fixing all of this. For log rotation, I'll sort by the last_modified_at ts and then purge those

Your generational approach to segment numbering is fine, if you prepend enough zeros to format the files properly then you're also able to sort them correctly. etcd uses the same trick.
Post reply on HN