PersistentServerState.java revision 0d397efc4b781ef5b60108708fa1131467d2c3c8
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2006-2009 Sun Microsystems, Inc.
*/
/**
* This class implements a ServerState that is stored on the backends
* used to store the synchronized data and that is therefore persistent
* across server reboot.
*/
public class PersistentServerState
{
private final InternalClientConnection conn =
private final ByteString asn1BaseDn;
private final int serverId;
private final ServerState state;
/**
* The attribute name used to store the state in the backend.
*/
/**
* The attribute name used to store the entryUUID.
*/
/**
* The attribute name used to store the RUV elements.
*/
/**
* create a new ServerState.
* @param baseDn The baseDN for which the ServerState is created
* @param serverId The serverId
*/
{
this.state = new ServerState();
loadState();
}
/**
* Create a new PersistenServerState based on an already existing ServerState.
*
* @param baseDn The baseDN for which the ServerState is created.
* @param serverId The serverId.
* @param state The serverState.
*/
{
loadState();
}
/**
* Update the Server State with a ChangeNumber.
* All operations with smaller CSN and the same serverID must be committed
* before calling this method.
*
* @param changeNumber The committed ChangeNumber.
*
* @return a boolean indicating if the update was meaningful.
*/
{
}
/**
* Save this object to persistent storage.
*/
public void save()
{
return;
{
}
}
/**
* Load the ServerState from the backing entry in database to memory.
*/
public void loadState()
{
// try to load the state from the base entry.
if (stateEntry == null)
{
// The base entry does not exist yet
// in the database or was deleted. Try to read the ServerState
// from the configuration instead.
}
if (stateEntry != null)
{
}
/*
* In order to make sure that the replication never looses changes,
* the server needs to search all the entries that have been
* updated after the last write of the ServerState.
* Inconsistencies may append after a crash.
*/
}
/**
* Run a search operation to find the base entry
* of the replication domain for which this ServerState was created.
*
* @return The base entry or null if no entry was found;
*/
private SearchResultEntry searchBaseEntry()
{
try
{
} catch (LDAPException e)
{
// can not happen
return null;
}
/*
* Search the database entry that is used to periodically
* save the ServerState
*/
{
return null;
}
{
/*
* Read the serverState from the REPLICATION_STATE attribute
*/
{
}
}
return stateEntry;
}
/**
* Run a search operation to find the entry with the configuration
* of the replication domain for which this ServerState was created.
*
* @return The configuration Entry or null if no entry was found;
*/
private SearchResultEntry searchConfigEntry()
{
try
{
"(&(objectclass=ds-cfg-replication-domain)"
{
/*
* Read the serverState from the REPLICATION_STATE attribute
*/
if (!resultEntries.isEmpty())
{
return resultEntry;
}
}
return null;
} catch (DirectoryException e)
{
// can not happen
return null;
}
}
/**
* Update this ServerState from the provided entry.
*
* @param resultEntry The entry that should be used to update this
* ServerState.
*/
{
{
{
}
}
}
/**
* Save the current values of this PersistentState object
* in the appropriate entry of the database.
*
* @return a ResultCode indicating if the method was successful.
*/
private ResultCode updateStateEntry()
{
/*
* Generate a modify operation on the Server State baseD Entry.
*/
{
// The base entry does not exist yet in the database or
// has been deleted, save the state to the config entry instead.
if (configEntry != null)
{
}
}
return result;
}
/**
* Run a modify operation to update the entry whose DN is given as
* a parameter with the serverState information.
*
* @param serverStateEntryDN The DN of the entry to be updated.
*
* @return A ResultCode indicating if the operation was successful.
*/
{
mods);
op.setInternalOperation(true);
op.setSynchronizationOperation(true);
op.setDontSynchronize(true);
{
}
return op.getResultCode();
}
/**
* Empty the ServerState.
* After this call the Server State will be in the same state
* as if it was just created.
*/
public void clearInMemory()
{
}
/**
* Empty the ServerState.
* After this call the Server State will be in the same state
* as if it was just created.
*/
public void clear()
{
save();
}
/**
* The ServerState is saved to the database periodically,
* therefore in case of crash it is possible that is does not contain
* the latest changes that have been processed and saved to the
* database.
* In order to make sure that we don't loose them, search all the entries
* that have been updated after this entry.
* This is done by using the HistoricalCsnOrderingMatchingRule
* and an ordering index for historical attribute
*/
public final void checkAndUpdateServerState() {
final AttributeType histType =
// Retrieves the entries that have changed since the
// maxCn stored in the serverState
synchronized (this)
{
if (serverStateMaxCn == null)
return;
try {
}
catch (Exception e)
{
return;
}
{
// An error happened trying to search for the updates
// Log an error
}
else
{
{
try
{
while (true)
{
{
// compare the csn regarding the maxCn we know and
// store the biggest
{
}
}
}
}
catch(Exception e)
{
}
}
{
// Update the serverState with the new maxCn
// present in the database
}
}
}
}
/**
* Check if a ReplicaUpdateVector entry is present
* if so, translate the ruv into a serverState and
* a generationId.
* @return the generationId translated from the RUV
* entry, 0 if no RUV is present
*/
public Long checkRUVCompat() {
// Search the RUV in the DB
ruvEntry = searchRUVEntry();
return null;
// Check if the serverState is already initialized
if( !isServerStateInitilized())
{
// Translate the ruv to serverState
// and GenerationId
}
// In any case, remove the RUV entry
// if it exists
// Run the internal operation
del.setInternalOperation(true);
del.setSynchronizationOperation(true);
del.setDontSynchronize(true);
return genId;
}
/**
* Initialize the serverState and the GenerationId based on a RUV
* entry.
* @param ruvEntry the entry to translate into a serverState.
* @return the generationId translated from the RUV entry.
*/
if (ruvElementType == null)
return null;
{
{
// Search for the GenerationId
{
// Get only the timestamp present in the CSN
}
else
{
// Translate the other elements into serverState
{
{
// No need to take into account the subSeqNum
}
}
}
}
}
return genId;
}
/**
* Check if the server State is initialized by searching
* the attribute type REPLICATION_STATE in the root entry.
* @return true if the serverState is initialized, false
* otherwise
*/
private boolean isServerStateInitilized() {
}
/**
* Search the database entry that represent a serverState
* using the RUV format (compatibility mode).
* @return the corresponding RUV entry, null otherwise
*/
private SearchResultEntry searchRUVEntry() {
// Search the RUV entry
try
{
} catch (LDAPException e)
{
// can not happen
return null;
}
return null;
{
/*
* Search the ldapSubEntry with the entryUUID equals
* to "ffffffff-ffff-ffff-ffff-ffffffffffff"
*/
{
{
{
equalsIgnoreCase("ffffffff-ffff-ffff-ffff-ffffffffffff"))
{
break;
}
}
}
}
}
return ruvEntry;
}
/**
* Get the largest ChangeNumber seen for a given LDAP server ID.
*
* @param serverID The server ID.
*
* @return The largest ChangeNumber seen.
*/
{
}
}