This post is part of an ongoing series building Soil, a file-based, transactional object database for Pharo, from the ground up — one real mechanism at a time.

Read the whole Soil series →

Soil: the many kinds of silence

The residents post showed how Soil starts, stops and restarts processes that live in the database, and it mentioned the tick only in passing: a process of the supervisor that looks at every resident now and then. This post is about that look, and about the question behind it. How do you know that a process nobody is watching is healthy?

The difficult part is not the failure. It is the silence. A resident that has nothing to do says nothing. So does a resident whose process died, one that dies and is revived over and over, one that is stuck in a step that never comes back, and a supervisor whose tick is gone. From the outside these five look exactly the same, and only one of them is fine. Everything below exists to tell them apart.

One rule from the residents post holds here as well: Soil reports, the embedding application decides. Nothing in this post makes a decision on its own. It produces lines and answers, and somebody else acts on them.

The tick

The supervisor has a process of its own, and in an interval passed in at the start it nudges every resident:

tick
	self residents do: [ :each |
		(each isStopping or: [ each isStopped or: [ each isOutOfService ] ]) ifFalse: [
			[ each tick ]
				on: Error
				do: [ :err | each emit: 'failed on tick: ', err messageText ] ] ].
	self reportIfDue

The tick

A few decisions are visible in these lines. The tick runs in the supervisor's process and not in a resident's, because a resident whose process hangs cannot revive itself. It runs serially, one resident after the other, and an error in one does not abort the run for the others. And it only nudges: the work of a resident happens in the resident's own process, otherwise one expensive resident would delay the tick for all the others.

The method is called tick and not healthCheck on purpose. The supervisor does not know what healthy means for a resident. Only the resident knows whether its process should be running, so SoilResident>>tick is an extension point, and the default does nothing.

The tick has one weakness that cannot be designed away: a process that has been terminated cannot bring itself back. If the tick process itself dies, somebody outside has to notice. ensureTickProcess starts it again if it is gone, counts that and says so, but it has to be called from a path the embedder walks anyway, for instance its own maintenance.

A process that died

An active resident uses its tick for the most obvious check, whether its process still runs:

tick
	self hasRunningProcess ifFalse: [
		self mayRestartProcess
			ifTrue: [
				self noteProcessRestart.
				self startProcess ]
			ifFalse: [ self markFailedAfterRestartLimit ] ].
	self reportWorkSinceLastTick

Reviving a process is easy. The important part is noteProcessRestart, which counts and reports it. A resident that is restarted silently looks exactly like a healthy one, and a resident in a crash loop would look healthy forever.

Counting alone does not end a crash loop, so there is a limit, maxProcessRestarts. Its default is nil, no limit, and that is deliberate: which number is right depends on what a resident does. One that calls an external service on every step fails for other reasons than one that writes back a collected window, and that is knowledge of the embedder, not of Soil. A concrete resident overrides the method or reads the number from its description. What is counted are revivals, not deaths: with a limit of three, three restarts happen, and the fourth death is the one that gives up. Giving up means the state failed, with a SoilResidentRestartLimitReached as the error, the same way a failed start is kept.

This is also why the supervisor's tick walks past residents that are failed or disabled. An active resident revives its process without asking what state it is in. If the tick reached a failed resident, it would start a loop whose prepareIn: never completed, failed would last exactly until the next tick, and the restart counter would be driven by the tick instead of by real deaths, which would make any limit on it meaningless. A switched-off resident would be switched on again the same way. A resident that is out of service stays out of service until the embedder brings it back with restartResidentNamed:in:timeout:.

With a limit of one and the process of the test resident from Soil's test package killed twice, the log says:

resident active: process restarted (1)
resident active: gave up restarting active after 1 restart

The heartbeat

Every line so far depends on an event: a start, a stop, a restart. A database in which nothing happens therefore says nothing at all, and the silence of a living tick reads exactly like the silence of a dead one. The only way out is a line that is written whether or not anything happened.

That is the report. At the start the supervisor says once that it is ticking, how often and for whom. After that it writes one line per report interval in which every resident contributes a short status:

residents: ticking every 200 milliseconds, reporting every 600 milliseconds, for active
...
residents: active started, 6 steps, 0 errors

Two details make it useful. It is one line per database, not one per resident, because at this interval what somebody wants is a glance. And the interval is measured against a deadline, not against a number of ticks, so changing the tick interval does not change how often the report comes. ApptiveGrid reports every 30 minutes, which is 48 lines per day and database, and in return a gap of an hour means something.

The status line carries the counters: steps and errors, restarts once there are any, and the error for a resident that failed. The lines are cumulative, so a series of reports also gives the rate.

Between two reports an active resident says how many steps it did since the last tick, and only if it did any. Silence on a tick therefore means "nothing to do", which is a statement, and not "no idea", which is the silence this was built to end. One level of reporting grows with the amount of work, a line for every single step. It is on by default and the embedder can switch it off with emitsEveryStep:.

A step that does not come back

One case is still missing. A resident whose process lives, but whose step never returns, for instance because it waits for an external service that does not answer. The restart counter does not see it, because the process is not dead. And the step counter does not move, exactly as with a resident that has nothing to do.

The information to tell them apart is there anyway. An active resident counts a step and remembers its start time before it begins it, in stepStartedAt. When the tick finds that no step was finished since the last tick and the resident is inside one, it says which and since when:

reportWorkSinceLastTick
	| done |
	done := stepCount - reportedStepCount.
	done = 0 ifTrue: [
		self isWorking ifTrue: [
			self emit: 'still in ', self currentStepName, ', started ',
				self currentStepDurationString, ' ago' ].
		^ self ].
	...

Busy or stuck

The binding to "nothing was finished" is the actual decision. A healthy resident under load is inside a step at tick time as well, and "still in step 4711, started 2 milliseconds ago" at every tick would be noise about a resident that is visibly making progress. Only nothing finished and inside a step is the signal. The status line of the report adds the same information, so a hanging step shows up where somebody looks first. A test resident told to hang in its eighth step, with a tick every 300 milliseconds:

resident active: still in step 8, started 0 seconds ago
resident active: still in step 8, started 1 second ago
resident active: still in step 8, started 2 seconds ago
residents: active started, 8 steps, 0 errors, in step 8 for 2 seconds

What does not happen is anything beyond that. There is no timeout after which the step is cut off. What is long for a call to an external service is short for a step that writes a collected window, and a step that is terminated in the middle is a decision with consequences. It is reported, and the embedder decides.

Telling silence apart

With all of this in place, the five silences from the beginning look different:

Situation What tells it apart
nothing to do the report keeps coming, the step count stays, no "still in step"
process died process restarted (n) on the next tick, restarts in the status line
crash loop the restart count climbs, then gave up restarting and failed
step does not come back still in step n, started … ago, and in step n for … in the report
tick is gone the report stops; ensureTickProcess says the tick process was gone

None of these lines decides anything. A supervisor that terminated hanging steps or closed a database on its own would be making choices it has no basis for. It collects what each resident can tell about itself and passes it on, and what follows is up to the application that knows what its residents do.

The code is on github if you want to read ahead.

Found something worth flagging? Send feedback.
This work is licensed under CC BY 4.0.