private void changelogChaining() throws Exception
{
debugInfo("Starting changelogChaining");
for (int itest = 0; itest <2; itest++)
{
ReplicationBroker broker2 = null;
boolean emptyOldChanges = true;
// - Create 2 connected replicationServer
ReplicationServer[] changelogs = new ReplicationServer[2];
int[] changelogPorts = new int[2];
int[] changelogIds = new int[2];
int[] brokerIds = new int[2];
ServerSocket socket = null;
// Find 2 free ports
for (int i = 0; i <= 1; i++)
{
// find a free port
socket = TestCaseUtils.bindFreePort();
changelogPorts[i] = socket.getLocalPort();
changelogIds[i] = i + 80;
brokerIds[i] = 100 + i;
if ((itest==0) || (i ==0))
socket.close();
}
for (int i = 0; i <= ((itest == 0) ? 1 : 0); i++)
{
changelogs[i] = null;
// for itest=0, create the 2 connected replicationServer
// for itest=1, create the 1rst replicationServer, the second
// one will be created later
SortedSet<String> servers = new TreeSet<String>();
servers.add(
"localhost:" + ((i == 0) ? changelogPorts[1] : changelogPorts[0]));
ReplServerFakeConfiguration conf =
new ReplServerFakeConfiguration(changelogPorts[i], "replicationServerTestChangelogChainingDb"+i, 0,
changelogIds[i], 0, 100, servers);
changelogs[i] = new ReplicationServer(conf);
}
ReplicationBroker broker1 = null;
try
{
// For itest=0, create and connect client1 to changelog1
// and client2 to changelog2
// For itest=1, only create and connect client1 to changelog1
// client2 will be created later
broker1 = openReplicationSession(DN.decode(TEST_ROOT_DN_STRING),
brokerIds[0], 100, changelogPorts[0], 1000, !emptyOldChanges);
assertTrue(broker1.isConnected());
if (itest == 0)
{
broker2 = openReplicationSession(DN.decode(TEST_ROOT_DN_STRING),
brokerIds[1], 100, changelogPorts[0], 1000, !emptyOldChanges);
assertTrue(broker2.isConnected());
}
// - Test messages between clients by publishing now
// - Delete
long time = TimeThread.getTime();
int ts = 1;
ChangeNumber cn = new ChangeNumber(time, ts++, brokerIds[0]);
DeleteMsg delMsg = new DeleteMsg("o=example" + itest + "," + TEST_ROOT_DN_STRING, cn, "uid");
broker1.publish(delMsg);
String user1entryUUID = "33333333-3333-3333-3333-333333333333";
String baseUUID = "22222222-2222-2222-2222-222222222222";
// - Add
String lentry = new String("dn: o=example," + TEST_ROOT_DN_STRING + "\n"
+ "objectClass: top\n" + "objectClass: domain\n"
+ "entryUUID: 11111111-1111-1111-1111-111111111111\n");
Entry entry = TestCaseUtils.entryFromLdifString(lentry);
cn = new ChangeNumber(time, ts++, brokerIds[0]);
AddMsg addMsg = new AddMsg(cn, "o=example," + TEST_ROOT_DN_STRING,
user1entryUUID, baseUUID, entry.getObjectClassAttribute(), entry
.getAttributes(), new ArrayList<Attribute>());
broker1.publish(addMsg);
// - Modify
Attribute attr1 = Attributes.create("description", "new value");
Modification mod1 = new Modification(ModificationType.REPLACE, attr1);
List<Modification> mods = new ArrayList<Modification>();
mods.add(mod1);
cn = new ChangeNumber(time, ts++, brokerIds[0]);
ModifyMsg modMsg = new ModifyMsg(cn, DN
.decode("o=example," + TEST_ROOT_DN_STRING), mods, "fakeuniqueid");
broker1.publish(modMsg);
// - ModifyDN
cn = new ChangeNumber(time, ts++, brokerIds[0]);
ModifyDNOperationBasis op = new ModifyDNOperationBasis(connection, 1, 1, null, DN
.decode("o=example," + TEST_ROOT_DN_STRING), RDN.decode("o=example2"), true,
null);
op.setAttachment(SYNCHROCONTEXT, new ModifyDnContext(cn, "uniqueid",
"newparentId"));
LocalBackendModifyDNOperation localOp =
new LocalBackendModifyDNOperation(op);
ModifyDNMsg modDNMsg = new ModifyDNMsg(localOp);
broker1.publish(modDNMsg);
if (itest > 0)
{
socket.close();
SortedSet<String> servers = new TreeSet<String>();
servers.add("localhost:"+changelogPorts[0]);
ReplServerFakeConfiguration conf =
new ReplServerFakeConfiguration(changelogPorts[1], null, 0,
changelogIds[1], 0, 0, null);
changelogs[1] = new ReplicationServer(conf);
// Connect broker 2 to changelog2
broker2 = openReplicationSession(DN.decode(TEST_ROOT_DN_STRING),
brokerIds[1], 100, changelogPorts[1], 2000, !emptyOldChanges);
assertTrue(broker2.isConnected());
}
// - Check msg receives by broker, through changeLog2
while (ts > 1)
{
ReplicationMsg msg2;
try
{
msg2 = broker2.receive();
if (msg2 == null)
break;
broker2.updateWindowAfterReplay();
}
catch (Exception e)
{
fail("Broker receive failed: " + e.getMessage() + "#Msg:" + ts + "#itest:" + itest);
break;
}
if (msg2 instanceof DeleteMsg)
{
DeleteMsg delMsg2 = (DeleteMsg) msg2;
if (delMsg2.toString().equals(delMsg.toString()))
ts--;
}
else if (msg2 instanceof AddMsg)
{
AddMsg addMsg2 = (AddMsg) msg2;
if (addMsg2.toString().equals(addMsg.toString()))
ts--;
}
else if (msg2 instanceof ModifyMsg)
{
ModifyMsg modMsg2 = (ModifyMsg) msg2;
if (modMsg.equals(modMsg2))
ts--;
}
else if (msg2 instanceof ModifyDNMsg)
{
ModifyDNMsg modDNMsg2 = (ModifyDNMsg) msg2;
if (modDNMsg.equals(modDNMsg2))
ts--;
}
else if (msg2 instanceof TopologyMsg)
{
// Nothing to test here.
}
else
{
fail("ReplicationServer transmission failed: no expected message" +
" class: " + msg2);
break;
}
}
// Check that everything expected has been received
assertTrue(ts == 1, "Broker2 did not receive the complete set of"
+ " expected messages: #msg received " + ts);
debugInfo("Ending changelogChaining");
}
finally
{
if (changelogs[0] != null)
changelogs[0].remove();
if (changelogs[1] != null)
changelogs[1].remove();
if (broker1 != null)
broker1.stop();
if (broker2 != null)
broker2.stop();
}
}
}