ServerState.java revision 9a216edaba16b28f240832cbbb25a5e6b367ac86
/*
* 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 legal-notices/CDDLv1_0.txt
* 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 legal-notices/CDDLv1_0.txt.
* 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-2010 Sun Microsystems, Inc.
* Portions Copyright 2011-2015 ForgeRock AS
*/
/**
* This class is used to associate serverIds with {@link CSN}s.
* <p>
* For example, it is exchanged with the replication servers at connection
* establishment time to communicate "which CSNs was last seen by a serverId".
*/
{
/** Associates a serverId with a CSN. */
/**
* Whether the state has been saved to persistent storage. It starts at true,
* and moves to false when an update is made to the current object.
*/
private volatile boolean saved = true;
/**
* Creates a new empty ServerState.
*/
public ServerState()
{
super();
}
/**
* Empty the ServerState.
* After this call the Server State will be in the same state
* as if it was just created.
*/
public void clear()
{
}
/**
* Forward update the Server State with a CSN. The provided CSN will be put on
* the current object only if it is newer than the existing CSN for the same
* serverId or if there is no existing CSN.
*
* @param csn
* The committed CSN.
* @return a boolean indicating if the update was meaningful.
*/
{
{
return false;
}
saved = false;
while (true)
{
if (existingCSN == null)
{
{
return true;
}
// oops, a concurrent modification happened, run the same process again
continue;
}
{
{
return true;
}
// oops, a concurrent modification happened, run the same process again
continue;
}
return false;
}
}
/**
* Update the Server State with a Server State. Every CSN of this object is
* updated with the CSN of the passed server state if it is newer.
*
* @param serverState the server state to use for the update.
* @return a boolean indicating if the update was meaningful.
*/
{
if (serverState == null)
{
return false;
}
boolean updated = false;
{
{
updated = true;
}
}
return updated;
}
/**
* Removes the mapping to the provided CSN if it is present in this
* ServerState.
*
* @param expectedCSN
* the CSN to be removed
* @return true if the CSN could be removed, false otherwise.
*/
{
if (expectedCSN == null)
{
return false;
}
{
saved = false;
return true;
}
return false;
}
/**
* Replace the Server State with another ServerState.
*
* @param serverState The ServerState.
*
* @return a boolean indicating if the update was meaningful.
*/
if (serverState == null) {
return false;
}
clear();
return update(serverState);
}
/**
* Return a Set of String usable as a textual representation of
* a Server state.
* format : time seqnum id
*
* example :
* 1 00000109e4666da600220001
* 2 00000109e44567a600220002
*
* @return the representation of the Server state
*/
{
{
}
return result;
}
/**
* Return an ArrayList of ANS1OctetString encoding the CSNs
* contained in the ServerState.
* @return an ArrayList of ANS1OctetString encoding the CSNs
* contained in the ServerState.
*/
{
{
}
return values;
}
/**
* Encodes this server state to the provided ASN1 writer.
*
* @param writer
* The ASN1 writer.
* @param protocolVersion
* The replication protocol version.
* @throws IOException
* If an error occurred during encoding.
*/
throws IOException
{
{
{
}
}
else
{
{
}
}
}
/**
* Returns a snapshot of this object.
*
* @return an unmodifiable List representing a snapshot of this object.
*/
{
if (serverIdToCSN.isEmpty())
{
return Collections.emptyList();
}
}
/**
* Return the text representation of ServerState.
* @return the text representation of ServerState
*/
{
}
/**
* Appends the text representation of ServerState.
* @param buffer The buffer to which the information should be appended.
*/
{
boolean first = true;
{
if (!first)
{
}
first = false;
}
}
/**
* Returns the {@code CSN} contained in this server state which corresponds to
* the provided server ID.
*
* @param serverId
* The server ID.
* @return The {@code CSN} contained in this server state which
* corresponds to the provided server ID.
*/
{
}
/**
* Returns a copy of this ServerState's content as a Map of serverId => CSN.
*
* @return a copy of this ServerState's content as a Map of serverId => CSN.
*/
{
// copy to protect from concurrent updates
// that could change the number of elements in the Map
return new HashMap<>(serverIdToCSN);
}
/** {@inheritDoc} */
{
}
/**
* Check that all the CSNs in the covered serverState are also in this
* serverState.
*
* @param covered The ServerState that needs to be checked.
* @return A boolean indicating if this ServerState covers the ServerState
* given in parameter.
*/
{
{
if (!cover(coveredChange))
{
return false;
}
}
return true;
}
/**
* Checks that the CSN given as a parameter is in this ServerState.
*
* @param covered The CSN that should be checked.
* @return A boolean indicating if this ServerState contains the CSN given in
* parameter.
*/
{
}
/**
* Tests if the state is empty.
*
* @return True if the state is empty.
*/
public boolean isEmpty()
{
return serverIdToCSN.isEmpty();
}
/**
* Make a duplicate of this state.
* @return The duplicate of this state.
*/
public ServerState duplicate()
{
return newState;
}
/**
* Computes the number of changes a first server state has in advance
* compared to a second server state.
* @param ss1 The server state supposed to be newer than the second one
* @param ss2 The server state supposed to be older than the first one
* @return The difference of changes (sum of the differences for each server
* id changes). 0 If no gap between 2 states.
* @throws IllegalArgumentException If one of the passed state is null
*/
throws IllegalArgumentException
{
{
throw new IllegalArgumentException("Null server state(s)");
}
int diff = 0;
{
{
{
}
else
{
// ss2 does not have a change for this server id but ss1, so the
// server holding ss1 has every changes represented in csn1 in advance
// compared to server holding ss2, add this amount
}
}
}
return diff;
}
/**
* Set the saved status of this ServerState.
*
* @param b A boolean indicating if the State has been safely stored.
*/
public void setSaved(boolean b)
{
saved = b;
}
/**
* Get the saved status of this ServerState.
*
* @return The saved status of this ServerState.
*/
public boolean isSaved()
{
return saved;
}
}