Apache Mina + DNS

May 31st, 2008

I’m currently implementing DNS on top of Mina 2. If you are interested, feel free to take a look at the makeshift page.

Maybe I’ll create a DNS based load balancing solution from this somewhen, featuring a nice Web backend for editing and adding routines for node failure detection to it (maybe Rhino based?). So, when a server is down, DNS servers will notice this and deactivate the corresponding nodes until they are back online. In combination with a good clustered filesystem this could be the base for a highly available cluster on cheap linux boxes without huge configuration needs (ok, a ~10 second downtime of single nodes still needs to be tollerated). I think I’ll look for a domain company supporting real NS entries soon :-).

Update:
Ok, I got most of the features that I personally need ready. Now you can define a datasource for DNS records and for now this can be either “Memory” (records created at runtime and fully cached) or “Database” (a JDBC layer). With the database datasource it is possible to update records on the fly while the caching features of “Memory” still remain as long as records do not change (think I’ll create a quick PHP backend for this). You could also run status daemons in the background that automatically deactivate unreachable hosts (UPDATE dns SET active=’0′ WHERE …).

You can also use every database that is supported by JDBC (e.g. MySQL, PGSQL, MSSQL, Java DB, SQLite etc.) - I think this is a big feature plus that I never saw before for a DNS server. Of course you can also implement your own custom datasources (maybe LDAP) and I am thinking about adding a simple zone file datasource so you can use the DNS server as a BIND replacement easily.

Jemand hier, der aktuell das Verteilte Systeme-Praktikum besucht oder es noch vor sich hat? Falls ja, kleiner Exkurs:

Wir kennen ja alle das altbewährte Framework inkl. Network Simulator, das innerhalb des Praktikums eingesetzt wird und tausende Fenster auf dem Bildschirm ungeordnen öffnet - so ziemlich für jedes Objekt zwei Stück. Grundsätzlich ist diese Lösung für wenige Komponenten und all diejenigen in Ordnung, die sich mehr in C als in Java beheimatet fühlen. Beim letzten Praktikum (vspra5) wird es allerdings ziemlich grausam, z.B. drei Clients, einen RegistryServer, drei Zeitserver und den Simulator zu überblicken - wohl auch aus dem Grund, dass man bei jedem Durchlauf alle Fenster neu positionieren muss und auch kaum die Übersicht behalten kann, welches Objekt mit welchem vs_comm etc. Objekt kommuniziert, welches Ausgabefenster verwendet und und und…

Habe mich der Problematik daher einmal angenommen und ein Framework speziell für den Einsatz im letzten Praktikum erstellt, da dieses ja ziemlich viel Freiraum lässt. Herausgekommen ist aus Studenten-Sicht ein einfaches Client-Server-Modell, das automatisch den integrierten Simulator nutzt und in einem einzigen Fenster mit strukturierten Tabs für Clients, Server und anderes (z.B. der Simulator selbst) läuft. Auf Basis des Frameworks ist es sowohl für diejenigen mit Java-Erfahrung als auch für nicht-C-Programmierer, die sich lieber mit Java als mit der reinen Aneinanderreihung von Funktionsaufrufen auseinandersetzen möchten, ein Leichtes, die entsprechenden Server und Client zu implementieren. Einen kleinen Screenshot hätte ich auch anzubieten:

Das Framework besteht aus drei kleinen Paketen:

vs.common beinhaltet den Simulator und alle übrigen notwendigen Klassen, mit denen man sich selbst bei der Implementierung nicht mehr herumschlagen muss - der Simulator bleibt vollständig im Hintergrund. Clients und Server implementieren eine abstrakte Komponente, die jeweils ein entsprechendes Tab im Hauptfenster anlegt und zusätzlich Funktionalitäten wie den Einfachen Zugriff aufs Ausgabefenster wie auch auf die Eingabezeile (für eigene Befehle) mit sich bringt - muss man sich aber auch nicht mit auseinandersetzen, ist einfach verfügbar direkt in den eigenen Objekten. Grundsätzlich ermöglicht das Ganze den Umgang mit dem Simulator, als wäre er ein echtes Netzwerk. Dazu erlaubt der Simulator ebenfalls die Umkonfiguration des Netzwerkes direkt über die entsprechende Befehlszeile (die Befehle werden beim Start entsprechend aufgelistet).

vs.client beinhaltet nur einen Client, der die Methoden println(String text), send(DatagramPacket packet), receive() und receive(int timeout) implementiert. Ein einfaches “TimeClient extends Client” und das Hinzufügen der entsprechenden knappen Logik genügen - die Beschäftigung mit Kommunikationsobjekten, das Zuweisen von Ausgabefenstern etc. entfällt vollständig, da alles automatisch vom Framework erstellt und zugewiesen wird.

vs.server beinhaltet analog einen abstrakten Server, der es erlaubt, die Methoden onReceive(DatagramPacket packet) und onTick() hinzuzufügen, um die entsprechende Programmlogik für einen TimeServer oder RegistryServer zu implementieren (RegistryServer extends Server etc.). Ebenfalls verfügbar sind Methoden zum einfachen Starten und Stoppen eines Servers sowohl die implementierbaren Methoden onStart() und onStop(), z.B. für die Registrierung der TimeServer beim RegistryServer, immer wenn ein Server startet.

Sowohl Clients als auch Server können zudem die Methode onCommand(String command, String[] args) überschreiben, um eigene Befehle aus der Befehlszeile abzugreifen - ist also vollständig modular, man braucht keine Knöpfe und regelt alles in einem einzigen Fenster.

Falls jemand Interesse hat, einfach melden…und wenn nicht, trotzdem viel Spaß ;-).

Not long ago, Sun released a bundled package of their “Sun Java System Application Server” (Glassfish) and MySQL. I took this as the reason to try out PHP on Glassfish with enabled MySQL support. The initial installation was quite simple but there were some problems with documentation about the usage of MySQL. For those of you who already have a MySQL server running and just want ot test your application on Glassfish, the installation basically is:

  1. Download Glassfish/MySQL Bundle
    Or if you already have glassfish, add the Mysql Connector/J (the jar from the archive) to GLASSFISH/lib
  2. Download Quercus (most recent) and unpack the following files from the archives:
    quercus.jar
    resin-util.jar           to       GLASSFISH/domains/domain1/lib
    script-10.jar
  3. Add the following to GLASSFISH/domains/domain1/config/default-web.xml:
      <servlet>
        <servlet-name>Quercus Servlet</servlet-name>
        <servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
        <init-param>
          <param-name>script-encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
          <param-name>database</param-name>
          <param-value>ExampleMySQL</param-value><!– See below –>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>Quercus Servlet</servlet-name>
        <servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
        <url-pattern>*.php</url-pattern>
        <init>
          <!– <php-ini>
            <sendmail_from>my_email_address</sendmail_from>
            <smtp_username>my_email_username</smtp_username>
            <smtp_password>my_email_password</smtp_password>
          </php-ini> –>
        </init>
      </servlet-mapping>
  4. Launch Glassfish and log in to the administration interface (by default: http://localhost:4848)
    Howto: Glassfish Quickstart Quide
  5. On Resources / JDBC / … in the administration interface add a new connection pool of type MySQL and set User/Password/Port/URI etc. in its properties (look through them when creating and don’t leave them blank). When done, “ping” (the button) your MySQL server from the connection pool view to test if it worked.
    Also add a new “JDBC Resource” that links to the pool and give it a name, in this case ExampleMySQL.
  6. Add a PHP file of your choice to GLASSFISH/domains/domain1/docroot and maybe restart Glassfish once to be sure the database changes are live.
  7. Run your MySQL-enabled PHP script (e.g. http://localhost:8080/index.php).

That’s basically everything. It took me some time to find out about the correct MySQL configuration (it seems that other tutorials I discovered at Google are quite outdated). Now, go and have fun!

Errrm: There are also enterprise clustering features implemented into Glassfish. I am wondering if this can scale a huge PHP site as well as lighttpd/php-fcgi. If anyone knows more details about this, please let me know. And mh: Just a thought: What about Glassfish + Quercus + MySQL + GridGain to scale at least the Webserver part into new dimensions? ;-)

FastCGI for Apache Mina

May 9th, 2008

One of my recent activities focused on the excelent Apache Mina NIO framework. I played around with it while creating an improved version of the current chat server for Schlach.com and besides added FastCGI support to Mina to try out the possibilities. With the library it is now possible to develop efficient FastCGI server applications easily and I also added all the tools to talk to remote FastCGI servers like PHP/Ruby etc.

If you are interested in trying this out, take a look on its makeshift page. The client side code seems to be still a little slow, so if you find some improvements especially to using the IoConnector, please let me know.