News for hackers. Stuff that matters.
Re:Website hosted on a 24 year old Linux server by ElectricalUnion on Thursday April 06, 2023 @11:39AM (#35469897)
> All you need to store this kind of data is a simple file.

You mostly likely need a WAL Mode SQLite database. Most of the time, it's way simpler that handling state handling in concurrent situations yourself. (also, bindings are often available - if not outright bundled by default - under most common languages)

The "easy" way is all fun and games until your file is accessed in a concurrent fashion, and then your options are:

a) flat out die when concurrent things happens (file locks; default sqlite3 behaviour);

b) just write blindly to file and pretend concurrecy doesn't exist, but randomly lose data - (write to files directly like a crazy person; sqlite3 PRAGMA schema.synchronous = OFF)

c) allow reading at anytime, but serialize writing somehow (file locks + write + move atomic file operations; append writing and a journal; sqlite3 PRAGMA journal_mode=WAL)

Re:Website hosted on a 24 year old Linux server by Supermancho on Thursday April 06, 2023 @11:53AM (#35470155)
> You mostly likely need a WAL Mode SQLite database

Or redis.

Re:Website hosted on a 24 year old Linux server by doublerabbit on Thursday April 06, 2023 @11:53AM (#35470159)
> You mostly likely need a WAL Mode SQLite database.

Overkill, If you are writing to the text file only to increment a number of visitors, none of what you mentioned above is even required.

Create a "counter-hit" file for each visitor, count the number of files in the resources directory. Populate the master file with the file count and delete all the temporary files. Configure it on a crontab for say every three seconds.

Online now was even easier. IFrame refresh within the homepage. Upon refresh, populate the file with something like "online=true". Read the file every X, if the file modifcation hasn't been refreshed within 30 seconds, nerf the text file and mark that user as offline. psuedo-dynamic, but times were different then. I coded these in perl back in the day for my RTCW clan and how I miss it much.

Being a webmaster was art, an art that's now lost..

Re:Website hosted on a 24 year old Linux server by artificial on Thursday April 06, 2023 @12:32PM (#35470802)
64 Player Depot. I always had a fondness for Tram. Played CAL Main. Good times. v43
Re:Website hosted on a 24 year old Linux server by jcalvinowens on Thursday April 06, 2023 @12:07PM (#35470412)
It can be much simpler if you're willing to write a couple lines of assembler: use an 8-byte file that contains one 64-bit counter, mmap() it, mlock() it, and use atomic CPU instructions to increment and read it.
Re:Website hosted on a 24 year old Linux server by andoma on Thursday April 06, 2023 @06:12PM (#35475242)
Genuine question: Does it have to be mlocked for this to work?
Re:Website hosted on a 24 year old Linux server by wahern on Thursday April 06, 2023 @08:37PM (#35476569)
You don't need mlock for the interprocess atomicity. There may be a race if you intend the file to be persistent; write-back to disk wouldn't necessarily be atomic. But mlock doesn't solve that problem, it just might make it slightly less likely to result in corruption if it means less flushing. To truly solve it you need a proper synchronization mechanism, e.g. a mutex to quiet all writers, then a blocking sync. OTOH, write-back for a single native integer may actually be atomic, assuming it doesn't cross a cache line, so in practice a simple mmap may be sufficient. (I emphasize may, though. I'm not that familiar with the inner mechanics of DMA, block device buffering, etc.)

You don't need assembly, either, as standard atomic built-ins in C, C++, etc, will work just fine, as will FFI to such built-ins.

Re:Website hosted on a 24 year old Linux server by jcalvinowens on Friday April 07, 2023 @09:15PM (#35489374)
Great question: it does not! It just removes a potential source of stalls.

If the page isn't resident in the page cache, the thread(s) executing the atomic increment will take a page fault and be blocked until the file data is read from storage. The latency of the fault might be significant, but the counter will remain accurate.

There's an important caveat I should have added: the counter file might be very stale after a power failure. If you care about the counter integrity, you have to msync(MS_SYNC) periodically, and that's expensive. It might actually stall all threads interacting with the page, depending on the filesystem; that used to be true but I'm not certain it still is (see https://lwn.net/Articles/486311/). Where writeback is allowed to race with writes, whether you would be guaranteed the 8-byte value written back wasn't "torn" without explicitly blocking increments while syncing is also an interesting question if DMA is involved...

Re:Website hosted on a 24 year old Linux server by malborodog on Thursday April 06, 2023 @07:51PM (#35476175)
I am going to ask gpt 4 to implement this for me so I can see what it looks like. Amazing.
Re:Website hosted on a 24 year old Linux server by Gud on Friday April 07, 2023 @08:55AM (#35481094)
How did it go?
Re:Website hosted on a 24 year old Linux server by jcalvinowens on Friday April 07, 2023 @09:39PM (#35489535)
I have to go to dinner so this isn't finished, but this is most of the code to prove it works: https://gist.github.com/jcalvinowens/0d7a5c327d863fca7c84daa...
Re:Website hosted on a 24 year old Linux server by henrydark on Thursday April 06, 2023 @12:19PM (#35470599)
Sqlite?? MS Access for the win!
Re:Website hosted on a 24 year old Linux server by seba_dos1 on Thursday April 06, 2023 @12:34PM (#35470831)
For guest book, you don't need anything like that. A simple solution could be to create a file for each entry. It's append-only anyway.

For a view counter - yes, you can use a full blown database to concurrently increment a simple integer value; or, you could do it in a few lines of code by yourself too.

Re:Website hosted on a 24 year old Linux server by loo on Thursday April 06, 2023 @01:04PM (#35471267)
$ apt install redis-server

$ redis-cli INCR hit-count

Re:Website hosted on a 24 year old Linux server by seba_dos1 on Thursday April 06, 2023 @02:49PM (#35472679)
Still a huge overkill, and you need to properly set up persistence if you use it this way.

If you know that you'll need a cannon anyway, then sure, go for it, but when all we deal with are flies there's no reason to go that far. You could easily set up this kind of website on servers so simple that getting redis running on them would inflate their complexity quite considerably.

Re:Website hosted on a 24 year old Linux server by SoftTalker on Thursday April 06, 2023 @05:32PM (#35474799)
> b) just write blindly to file and pretend concurrecy doesn't exist

This is likely what was done. Nobody really cares if a hit counter on a web page loses a few updates.

Re:Website hosted on a 24 year old Linux server by mappu on Thursday April 06, 2023 @06:21PM (#35475317)
Given it's a single-threaded CPU, there's a fair chance the web server isn't concurrent anyway.

Linux got proper POSIX thread support with NPTL only in 2002.

Re:Website hosted on a 24 year old Linux server by electroly on Thursday April 06, 2023 @08:06PM (#35476300)
The RaQ line used Apache which definitely supported concurrent requests. We're talking about 1999 here. Maybe this isn't as obvious to people today looking back, but concurrent request handling was absolutely required from the beginning: consider how slow your clients are! One single client on a slow dial-up modem can't be allowed to drag the whole site down.
Re:Website hosted on a 24 year old Linux server by johannes1234321 on Thursday April 06, 2023 @08:07PM (#35476316)
It will be a forking web server. But the CPU scheduler will schedule process A while B is opening the file and reading and that. While B is then increasing the counter, leading to multiple processes with "wrong" data.l racing for the writes.
Re:Website hosted on a 24 year old Linux server by randomdata on Thursday April 06, 2023 @09:05PM (#35476773)
As I remember it, you would flock the file. You didn’t care if another process blocked for the short period it was held. It is not like you were getting huge amounts of traffic.
Re:Website hosted on a 24 year old Linux server by jszymborski on Thursday April 06, 2023 @06:36PM (#35475462)
You can do (c) in python with flock

https://www.php.net/manual/en/function.flock.php

Re:Website hosted on a 24 year old Linux server by johannes1234321 on Thursday April 06, 2023 @08:12PM (#35476362)
I think you can avoid the lock and all else, if all you need is a counter: Open a file in append mode (O_APPEND) and then write a byte for each visit. To get the count take the file size.

Of course you have to make sure to not run out of diskspace or max file size of the OS/filesystem (2GB on a 32bit system?)

Re:Website hosted on a 24 year old Linux server by error503 on Thursday April 06, 2023 @08:13PM (#35476373)
d) write to a temp file and atomically move it onto the read path. you will miss counts, but nothing will break.
Re:Website hosted on a 24 year old Linux server by anacrolix on Friday April 07, 2023 @03:30AM (#35479099)
I think you mean journal mode off. Synchronous off is nothing to do with concurrency.