Chapter 4. Replica versus Master Processes
Prev |  | Next

## Chapter 4. Replica versus Master Processes

Every environment participating in a replicated application must know whether it is a master or replica. The reason for this is because, simply, the master can modify the database while replicas cannot. As a result, not only will you open databases differently depended on whether the environment is running as a master, but the environment will frequently behave quit a bit differently depending on whether it thinks it is operating as the read/write interface for your database.

Moreover, an environment must also be capable of gracefully switching between master and replica states. This means that the environment must be able to detect when it has switched states.

Not surprisingly, a large part of your application's code will be tied up in knowing which state a given environment is in and then in the logic of how to behave depending on its state.

This chapter shows you how to determine your environment's state, and it then shows you some sample code on how an application might behave depending on whether it is a master or a replica in a replicated application.

## Determining State

In order to determine whether your code is running as a master or a replica, you must write your application as an implementation of `com.sleepycat.db.EventHandler`. This class gives you a single method, `handleEvent()`, within which you can detect and respond to various events that occur in your DB code.

Note that `EventHandler` responds to a number of `com.sleepycat.db.EventType` events, only some of which are related to replication. For replication, the events that we care about are:

- `EventType.REP_MASTER`

  The local environment is now a master.

- `EventType.REP_CLIENT`

  The local environment is now a replica.

- `EventType.REP_STARTUPDONE`

  The replica has completed startup synchronization and is now processing log records received from the master.

- `EventType.REP_NEWMASTER`

  An election was held and a new environment was made a master. However, the current environment is not the master. This event exists so that you can cause your code to take some unique action in the event that the replication groups switches masters.

Note that these events are raised whenever the state is established. That is, when the current environment becomes a client, and that includes at application startup, the event is raised. Also, when an election is held and a client is elected to be a master, then the event occurs.

The `EventHandler` implementation is fairly simple. First you detect the event, and then you record the state change in some data member maintained in a location that is convenient to you.

For example:

    package db.repquote;

    // We make our main class an EventHandler implementation
    ...
    import com.sleepycat.db.EventHandler;
    ...

    public class MyReplicationClass implements EventHandler
    {

    ...

    // Somewhere we provide a data member that is used to track
    // whether we are a master server. This could be in our main
    // class, or it could be part of a supporting class.
    private boolean isMaster;

    ...

    isMaster = false;

    ...

    // In the code where we open our environment and start replication,
    // we must identify the class that is the event handler. In this
    // example, we are performing this from within the class that
    // implements com.sleepycat.db.EventHandler so we identify
    // "this" class as the event handler
    envConfig.setEventHandler(this);

That done, we still need to implement the `handleEvent` method. This implementation can be fairly trivial.

        public int handleEvent(EventType event)
        {
            int ret = 0;
            if (event == EventType.REP_MASTER)
                dbenv.setIsMaster(true);
            else if (event == EventType.REP_CLIENT)
                dbenv.setIsMaster(false);
            else if (event == EventType.REP_NEW_MASTER) {
                // ignored for now.
            } else {
                System.err.println("Unknown event callback received.\n");
                ret = 1;
            }
            return ret;
        }

Of course, this only gives us the current state of the environment. We still need the code that determines what to do when the environment changes state and how to behave depending on the state (described in the next section).

Prev | Up | Next
Managing Heartbeats | Home | Processing Loop