mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
27.25.2013 93610df181b2bae16dfda4a8aee38809ad966738
Fixed the broken tests in ReplicationServerTest.

ReplicationDbEnv.java:
Decoupled the call to start() from building the object + Made start() a public method.
Inlined the code from readDomainBaseDNGenerationIDRecords() and readServerIdDomainBaseDNRecords() into start() to avoid reading twice the DB on startup.
Extracted method openDatabase().

ReplicationServer.java:
In initialize(), called ReplicationDbEnv.start().
2 files modified
153 ■■■■■ changed files
opends/src/server/org/opends/server/replication/server/ReplicationServer.java 5 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/replication/server/changelog/je/ReplicationDbEnv.java 148 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/replication/server/ReplicationServer.java
@@ -523,11 +523,10 @@
    try
    {
      /*
       * Initialize the replicationServer database.
       */
      // Initialize the replicationServer database.
      dbEnv = new ReplicationDbEnv(getFileForPath(dbDirname).getAbsolutePath(),
          this);
      dbEnv.start();
      setServerURL();
      listenSocket = new ServerSocket();
opends/src/server/org/opends/server/replication/server/changelog/je/ReplicationDbEnv.java
@@ -50,9 +50,9 @@
 */
public class ReplicationDbEnv
{
  private Environment dbEnvironment = null;
  private Database stateDb = null;
  private ReplicationServer replicationServer = null;
  private Environment dbEnvironment;
  private Database stateDb;
  private ReplicationServer replicationServer;
  private static final String GENERATION_ID_TAG = "GENID";
  private static final String FIELD_SEPARATOR = " ";
  /**
@@ -141,12 +141,7 @@
       * the topology. The database "changelogstate" is used to store the list
       * of all the servers that have been seen in the past.
       */
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setAllowCreate(true);
      dbConfig.setTransactional(true);
      stateDb = dbEnvironment.openDatabase(null, "changelogstate", dbConfig);
      start();
      stateDb = openDatabase("changelogstate");
    }
    catch (RuntimeException e)
    {
@@ -154,13 +149,21 @@
    }
  }
  private Database openDatabase(String databaseName) throws RuntimeException
  {
    final DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setAllowCreate(true);
    dbConfig.setTransactional(true);
    return dbEnvironment.openDatabase(null, databaseName, dbConfig);
  }
  /**
   * Read the list of known servers from the database and start dbHandler
   * for each of them.
   *
   * @throws ChangelogException in case of underlying Exception
   */
  private void start() throws ChangelogException, DatabaseException
  public void start() throws ChangelogException
  {
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry data = new DatabaseEntry();
@@ -168,8 +171,47 @@
    try
    {
      readDomainBaseDNGenerationIDRecords(key, data, cursor);
      readServerIdDomainBaseDNRecords(key, data, cursor);
      OperationStatus status = cursor.getFirst(key, data, LockMode.DEFAULT);
      while (status == OperationStatus.SUCCESS)
      {
        String stringData = toString(data.getData());
        if (debugEnabled())
          TRACER.debugInfo("In " + replicationServer.getMonitorInstanceName()
              + " Read (tag/baseDn/generationId) OR (serverId/baseDN): "
              + stringData);
        String[] str = stringData.split(FIELD_SEPARATOR, 3);
        if (str[0].equals(GENERATION_ID_TAG))
        {
          long generationId = toLong(str[1]);
          String baseDn = str[2];
          if (debugEnabled())
            TRACER.debugInfo("In " + replicationServer.getMonitorInstanceName()
                + " Has read baseDn=" + baseDn + " generationId="
                + generationId);
          replicationServer.initDomainGenerationID(baseDn, generationId);
        }
        else
        {
          int serverId = toInt(str[0]);
          String baseDn = str[1];
          if (debugEnabled())
            TRACER.debugInfo("In " + replicationServer.getMonitorInstanceName()
                + " Has read: baseDn=" + baseDn + " serverId=" + serverId);
          replicationServer.addServerIdToDomain(serverId, baseDn);
        }
        status = cursor.getNext(key, data, LockMode.DEFAULT);
      }
    }
    catch (RuntimeException e)
    {
      throw new ChangelogException(e);
    }
    finally
    {
@@ -177,71 +219,6 @@
    }
  }
  private void readDomainBaseDNGenerationIDRecords(DatabaseEntry key,
      DatabaseEntry data, Cursor cursor) throws ChangelogException,
      DatabaseException
  {
    // Get the domain base DN/ generationIDs records
    OperationStatus status = cursor.getFirst(key, data, LockMode.DEFAULT);
    while (status == OperationStatus.SUCCESS)
    {
      String stringData = toString(data.getData());
      if (debugEnabled())
        TRACER.debugInfo("In "
            + this.replicationServer.getMonitorInstanceName()
            + " Read tag baseDn generationId=" + stringData);
      String[] str = stringData.split(FIELD_SEPARATOR, 3);
      if (str[0].equals(GENERATION_ID_TAG))
      {
        long generationId = toLong(str[1]);
        String baseDn = str[2];
        if (debugEnabled())
          TRACER.debugInfo("In "
              + this.replicationServer.getMonitorInstanceName()
              + " Has read baseDn=" + baseDn + " generationId=" + generationId);
        replicationServer.initDomainGenerationID(baseDn, generationId);
      }
      status = cursor.getNext(key, data, LockMode.DEFAULT);
    }
  }
  private void readServerIdDomainBaseDNRecords(DatabaseEntry key,
      DatabaseEntry data, Cursor cursor) throws ChangelogException,
      DatabaseException
  {
    // Get the server Id / domain base DN records
    OperationStatus status = cursor.getFirst(key, data, LockMode.DEFAULT);
    while (status == OperationStatus.SUCCESS)
    {
      String stringData = toString(data.getData());
      if (debugEnabled())
        TRACER.debugInfo("In "
            + this.replicationServer.getMonitorInstanceName()
            + " Read serverId BaseDN=" + stringData);
      String[] str = stringData.split(FIELD_SEPARATOR, 2);
      if (!str[0].equals(GENERATION_ID_TAG))
      {
        int serverId = toInt(str[0]);
        String baseDn = str[1];
        if (debugEnabled())
          TRACER.debugInfo("In "
              + this.replicationServer.getMonitorInstanceName()
              + " Has read: baseDn=" + baseDn + " serverId=" + serverId);
        replicationServer.addServerIdToDomain(serverId, baseDn);
      }
      status = cursor.getNext(key, data, LockMode.DEFAULT);
    }
  }
  private int toInt(String data) throws ChangelogException
  {
    try
@@ -309,10 +286,7 @@
        // Opens the database for the changes received from this server
        // on this domain. Create it if it does not already exist.
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setAllowCreate(true);
        dbConfig.setTransactional(true);
        Database db = dbEnvironment.openDatabase(null, serverIdKey, dbConfig);
        Database db = openDatabase(serverIdKey);
        // Creates the record serverId/domain base Dn in the stateDb
        // if it does not already exist.
@@ -545,17 +519,11 @@
     */
    public Database getOrCreateDraftCNDb() throws ChangelogException
    {
      String stringId = "draftcndb";
      // Opens the database for seqnum associated to this domain.
      // Create it if it does not already exist.
      DatabaseConfig dbConfig = new DatabaseConfig();
      dbConfig.setAllowCreate(true);
      dbConfig.setTransactional(true);
      try
      {
        return dbEnvironment.openDatabase(null, stringId, dbConfig);
        // Opens the database for seqnum associated to this domain.
        // Create it if it does not already exist.
        return openDatabase("draftcndb");
      }
      catch (RuntimeException e)
      {