Monday, December 15, 2008

The New Upstart State Machine

Awhile ago I started working on describing a new "service state machine" for Upstart. This would be a departure from the event driven model, and offer a richer mechanism to describe what should be running at any given time. I think I more or less have the design down.

Now if only I could explain it to someone else :)

This series of posts will attempt to explain to mere mortals the abstract workings of this new wonder. Previous descriptions have proven a bit verbose and mathy, so I'm going to take it very slow this time. It may get a bit too easy, but I'm going to take baby steps no matter what.

I should probably note beforehand that the objective here is to develop a body of logic that can describe services, and then from that produce a program that will govern them. This post and ones after will not describe how the state machine should be implemented but how it should behave. For the curious, implementing it has been somewhat crudely and quite inefficiently prototyped here.

So, first we need to define a state. A state consists of 1) a name, 2) a series of properties.

mystate(crunchy: yes, decaf: no, flavor: immitation_beef)

Also, a state is either up or down. Pay attention to those colors, as I will use them consistently here.

There's a lot of comparing of states that will go on shortly, so it is important that we understand what makes states equal.

mystate(crunchy: yes) != mystate(crunchy: no)
mystate(crunchy: yes) != thystate(crunchy: yes)
mystate(crunchy: yes) == mystate(crunchy: yes)
mystate(crunchy: yes, chewy: yes) == mystate(chewy: yes, crunchy: yes)
mystate(crunchy: yes) == mystate(crunchy: yes)

That last one is important. A state being up or down does not effect its identity for the purposes of comparison.

Now we get into a bit of a mathy idea. Its not hugely complex, but its not entirely intuitive from a computer standpoint. Suppose I were to list a few states inside the state machine:

mystate(crunchy: yes)
mystate(crunchy: no, cheesy: yes)
mystate(crunchy: no)
thystate()
thystate(chewy: perhaps)
thystate(cheesy: thursdays)

You know that there are states called mystate and thystate. Depending on the context in which this list appeared, it might be safe to assume that a state called frystate doesn't make sense. For any particular state machine, there are only a few possible valid names states can have.

That doesn't seem too rough, but as is so often the case, what isn't said is what's tricky. The list of states above would have the same meaning if you appended the following:

mystate(crunchy: maybe)
mystate(crunchy: level_1)
mystate(crunchy: level_2)
mystate(crunchy: level_3)
mystate(crunchy: yes, threebears: eleventeen, number: threeve)
...
mystate(complete_works_of_shakespeare: ...)

The long and short of it is if you see mystate(anything) you can assume that there exists a mystate with any possible set of parameters you can imagine. The corollary to this is that there are an infinite number of states in any state machine.

5 comments:

ivazqueznet said...

What does it mean for a state to be "up" or "down"?

Casey said...

That kind of comes later :) It /can/ mean that a job attached to the state should be running or not.

skvidal said...

Are these states intended to be used by sysadmins to set the states of startup applications and processes?

Casey said...

They reflect various conditions about the system.

Some states will shadow services, so there might be an httpd state that you can bring up to start apache and bring down to stop it (and crashes will bring the state down automatically).

Other states might reflect the availability of a piece of hardware. For example plugging in a web cam might bring a particular state up. States will also come up when udev discovers hard disks.

The reason all of those things are expressed as states is to relate them to each other. You can have a state for a piece of bluetooth hardware set off the state for the bluetooth daemon. Things of that nature.

But again, this will all make sense in a few posts.

vfdvgf said...

After being wow gold informed of wow power leveling the problem, wow power leveling their daughter's date dog apparel said he could get the peanut out.wow power leveling With that, Wow Power Level the pilot threw open dog clothing the door and jumped from the plane.flyff power leveling the young man's Atlantica power leveling sunburn started power leveling acting up again.dog clothes He asked to be excused,dog clothes wholesale went into the kitchen power leveling The executoner said that if pet clothing this happens a second archlord power leveling time throws out a grenade and says, "i'm in the army, world of warcraft gold i can get these whenever i need them."dog clothes so they all land pet clothes safely