Thursday, December 25, 2008

Usage of Scala at Twitter

Here is a nice presentation about the use of the Scala language at Twitter:  

Besides the counless features of Scala (type interfence, Traits, Pattern Matching, ...), I found the reasons interessing the speaker gave against Ruby (Twitter was once a major landmark project for Ruby and Rails) : "Ruby's poor VM performance, monkeypatching and cultural issues, questionable for large systems". I found Monkeypatching terrible. Is there an equivalent to Liskovs principle for monkeypatching?

Remote Actors in Scala

A well known feature of Scala is it support for message passing concurrency via Actors similar to Erlang. In Chapter 30 of "Programming in Scala" Scala's Actors is described in detail.

However, it is not so well-known that Scala also supports actors distributed across different nodes (Remote Actors). Here is the Ping-Pong example with the Ping actor and the Pong actor running in different processes on (possibly) different (cluster) nodes. The example also shows the actor-linking support of Scala. Linking an actor a to an actor b, means that a is notified if b terminated.

Messages:

package de.dirkmeister.pingpong

case object Ping case object Pong case object Quit

Ping:
package de.dirkmeister.pingpong

import scala.actors.Actor import scala.actors.Actor._ import scala.actors.Exit import scala.actors.remote.RemoteActor._ import scala.actors.remote.Node

object RemotePingApp { def main(args: Array[String]) : Unit = { val port = args(0).toInt val peer = Node(args(1), args(2).toInt) val ping = new RemotePing(port, peer, 16) ping.start() } } class RemotePing(port: Int, peer: Node, count: Int) extends Actor { trapExit = true // (1)

def act() {
alive(port)     // (2)
register('Ping, self) // (3)

val pong = select(peer, 'Pong) // (4)
link(pong)             // (5)

var pingsLeft = count - 1
pong ! Ping     // (6)
while (true) {
  receive {     // (7)
    case Pong =>
      Console.println("Ping: pong")
      if (pingsLeft > 0) {
        pong ! Ping
        pingsLeft -= 1
      } else {
        Console.println("Ping: start termination")
        pong ! Quit     // (8)
        // Terminate ping after Pong exited (by linking)
      }
    case Exit(pong, 'normal) => // (9)
        Console.println("Ping: stop")
        exit()
  }
}

} }

Pong:
package de.dirkmeister.pingpong

import scala.actors.Actor import scala.actors.Actor._ import scala.actors.remote.RemoteActor._

object RemotePongApp { def main(args: Array[String]) : Unit = { val port = args(0).toInt val pong = new RemotePong(port) pong.start() } } class RemotePong(port: Int) extends Actor { def act() { alive(port) register('Pong, self)

while (true) {
  receive {
    case Ping =>
      Console.println("Pong: ping")
      sender ! Pong
    case Quit =>
      Console.println("Pong: stop")
      exit()    // (10)
  }
}

} }

Notes: (1) By setting trapExit, the linked actor is notified by sending an Exit(sender, reason) message. Otherwise the termination is either ignored (if reason is 'normal) or the linked actor is terminated, too (reason != 'normal). (2) alive(port) (member of the RemoteActor object) starts the remote service listening on the given port (3) register(symbol, actor) (member of the RemoteActor object) registers the given actor using the symbol. The other actors can then lookup the actor by the hostname and port and this symbol. (4) This lookup is done by the select(node, symbol) method that returns an proxy actor, which managed the complete transmission. (5) The link method links the Ping actor with the Pong actor, so that the current actor is notified if the Pong actor is terminated. This example shows that this also works remotely. (6) and (7) That proxy actor is used to send messages to the remote node. Sending and receiving remote messages is similar to local messages. Well, everything must be serializable, but the use of case classes is recommended anyway. (8) The Quit message stops they Pong actor. See (10) (9) When the Pong actor terminates, an Exit(sender, reason) message is sent to the Ping actor. This is linking system is used for error handling, here it is used to terminate the Ping actor, too. (10) The Pong actor calls the exit() method, which terminates the actor with the reason 'normal.

I think Scala's Remote Actors are really nice. Well, it lacks Erlang's capability to spawn actors on a different node and maybe other things, but it has the property that it isn't written in a 20 years old language: It is written in a modern, OO/functional-hybrid language.

According to a comment on the Lambda blog, there is (or was?) and effort to use the Java P2P protocol JXTA for remote actors. That would be cool, but I found nothing newer about that effort. Here is a description about clustering Scala actors via Terracotta. Here one using Oracle Coherence for that.

Wednesday, December 24, 2008

Twitter

So richtig offiziell habe ich es hier im Blog nach gar nicht gesagt, aber seit einiger Zeit habe (und benutze mit unregelmäßigen Abständen) einen Account bei Twitter. Ab heute sogar mit Profil-Foto.

Unterverständnis über das Qimonda-Hilfspaket

Laut heise.de will sich das Land Sachsen mit 150 Millionen Euro für den Speicherchiphersteller Qimonda beteiligen. Durch die Pleite wären die Arbeitsplätze von 3200 Mitarbeiten bedroht.

Das sind mal eben 46.000 Euro pro Arbeitsplatz, die vom Steuerzahler aufgebracht werden müssen. Das sind Größenordnungen, die fast an die Kohlesubventionen im Ruhrgebiet herankommen.

Der Haushalt des Landes beträgt 3,4 Milliarden (Quelle). Das bedeutet, dass 4% des gesamten Haushalts in ein Unternehmen gepumpt wird, dass "seit Geschäftsbeginn im Jahr 2005 [...] Milliardenverluste einfährt" und deren "strukturellen Probleme seit Langem bekannt sind" (Quelle).

Mir ist nicht klar, was außer reinem Populismus Politiker dazu bringt, so das Geld aus dem Fenster zu werfen. Sie können doch nicht ernsthaft glauben, dass die Strukurprobleme der Firma irgendwie gelöst werden, wenn man mal eben etwas Bargeld hineinpumpt. Wenn die Firma dann 2010 anstatt 2009 insolvent geht, dann ist das Geschrei wegen der "Heuschrecken" und den bösen Unternehmen wieder riesig. Das ist doch abzusehen. Ich kann, wenn ich die Nachrichten im Moment verfolge, nur den Kopf schütteln. Mit 150 Millionen lässt sich so viel sinnvolles anstellen, aber der Firma ein oder zwei Jahre zu schenken, gehört ganz bestimmt nicht dazu. Aus Holzmann und Co. wurde offensichtlich nicht gelernt.

Tuesday, December 23, 2008

Latex-Tipps für doppelseitige Diplomarbeiten

In der letzten Minute der Masterarbeit hab ich noch Latex-Probleme bekommen: Warum?

Bei normalem Buchdruck die erste Seite links ist, da dies die Rückseite des Buchrückens ist. Damit sind ungerade Seite links und gerade rechts. In Diplomarbeiten (oder Bachelorarbeiten, Masterarbeiten, what ever) ist die erste Seite (die Titelseite) rechts, weil es das erste gebundene Blatt ist. So sind gerade Seiten links und ungerade Seiten rechts.

Wenn man dort nicht aufpasst und nur "twopage" in "documentclass" verwendet, hat man schnell ein Dokument gesetzt, was jede Seite einzeln betrachtet  richtig aussieht, aber global falsch gesetzt ist. Die größeren Rändern sind außen anstatt innen. Das Ergebnis ist hier zu sehen:

thesis1

Daher müssen die bei Rändern so umgestellt werden, dass sie bei den geraden Seiten links größer sind und auf den ungeraden Seiten rechts größer. Ich habe dafür diese Befehle von Robort Close übernommen: setlength{voffset}{-10pt} setlength{topmargin}{10pt} setlength{headheight}{14pt} setlength{headsep}{25pt} setlength{textheight}{660pt} setlength{footskip}{30pt} setlength{hoffset}{0pt} setlength{oddsidemargin}{1.4cm} setlength{textwidth}{426pt} setlength{marginparsep}{0pt} setlength{marginparwidth}{0pt} setlength{marginparpush}{0pt} setlength{headwidth}{textwidth} setlength{evensidemargin}{paperwidth} addtolength{evensidemargin}{-textwidth} addtolength{evensidemargin}{-2.0in} addtolength{evensidemargin}{-oddsidemargin}

Danach sieht es so aus:

thesis2

Der zentrale Tipp ist es, sich die Datei (wie in den Screenshots) mit "Vorschau" anzusehen. Dort wird das Titelblatt richtig (rechts) angezeigt und alle nachfolgende Seiten (gerade links und ungerade rechts). Damit lässt sich erkennen, ob das Dokument richtig gesetzt ist.

Interessanterweise zeigt bei der Doppelseitigen Ansicht Adobe Reader die Titelseite links zusammen mit der 1. geraden Seite rechts und danach jede ungerade Seite links und jede gerade Seite rechts. Also genau wie es i.d.R. für Diplomarbeiten falsch ist. Daher Tipp nur 2): Wenn es in Adobe Reader richtig aussieht, ist es für eine Diplomarbeit in der Regel falsch.