initial replication end-to-end works for the first time for a more or less empty server

This commit is contained in:
Axel Uhl
2012-04-25 15:20:33 +02:00
parent 7c0448fb85
commit f40ecf63ac
5 changed files with 91 additions and 29 deletions
@@ -1,37 +1,72 @@
package com.sap.sailing.server.replication;
import java.util.Date;
import java.io.Serializable;
import java.net.InetAddress;
import java.util.UUID;
public class ReplicaDescriptor {
private UUID uuid;
private String ipAddress;
private Date registrationTime;
import com.sap.sailing.domain.base.impl.MillisecondsTimePoint;
import com.sap.sailing.domain.common.TimePoint;
public ReplicaDescriptor(UUID uuid) {
this.uuid = uuid;
this.registrationTime = new Date();
/**
* Describes a replica by remembering its IP address as well as the replication time and a UUID. Hash code and equality
* are based solely on the UUID.
*
* @author Frank Mittag, Axel Uhl (d043530)
*
*/
public class ReplicaDescriptor implements Serializable {
private static final long serialVersionUID = -5451556877949921454L;
private final UUID uuid;
private final InetAddress ipAddress;
private final TimePoint registrationTime;
/**
* Sets the registration time to now.
*/
public ReplicaDescriptor(InetAddress ipAddress) {
this.uuid = UUID.randomUUID();
this.registrationTime = MillisecondsTimePoint.now();
this.ipAddress = ipAddress;
}
public UUID getUuid() {
return uuid;
}
public String getIpAddress() {
public InetAddress getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public Date getRegistrationTime() {
public TimePoint getRegistrationTime() {
return registrationTime;
}
public void setRegistrationTime(Date registrationTime) {
this.registrationTime = registrationTime;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ReplicaDescriptor other = (ReplicaDescriptor) obj;
if (uuid == null) {
if (other.uuid != null)
return false;
} else if (!uuid.equals(other.uuid))
return false;
return true;
}
}
@@ -19,5 +19,5 @@ public interface ReplicationMasterDescriptor {
URL getInitialLoadURL() throws MalformedURLException;
TopicSubscriber getTopicSubscriber() throws JMSException, UnknownHostException;
TopicSubscriber getTopicSubscriber(String clientID) throws JMSException, UnknownHostException;
}
@@ -42,9 +42,10 @@ public class ReplicationMasterDescriptorImpl implements ReplicationMasterDescrip
}
@Override
public TopicSubscriber getTopicSubscriber() throws JMSException, UnknownHostException {
public TopicSubscriber getTopicSubscriber(String clientID) throws JMSException, UnknownHostException {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://" + hostname + ":" + jmsPort);
connectionFactory.setClientID(clientID);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
@@ -6,8 +6,10 @@ import java.io.ObjectInputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
@@ -52,9 +54,15 @@ public class ReplicationServiceImpl implements ReplicationService, OperationExec
private ServiceTracker<RacingEventService, RacingEventService> racingEventServiceTracker;
/**
* The UUIDs with which this replica is registered by the master identified by the corresponding key
*/
private final Map<ReplicationMasterDescriptor, String> replicaUUIDs;
public ReplicationServiceImpl(final ReplicationInstancesManager replicationInstancesManager,
final MessageBrokerManager messageBrokerManager) {
this.replicationInstancesManager = replicationInstancesManager;
replicaUUIDs = new HashMap<ReplicationMasterDescriptor, String>();
this.messageBrokerManager = messageBrokerManager;
racingEventServiceTracker = new ServiceTracker<RacingEventService, RacingEventService>(
Activator.getDefaultContext(), RacingEventService.class.getName(), null);
@@ -124,15 +132,15 @@ public class ReplicationServiceImpl implements ReplicationService, OperationExec
List<String> result = new ArrayList<String>();
for (Iterator<ReplicaDescriptor> i=replicationInstancesManager.getReplicaDescriptors(); i.hasNext(); ) {
ReplicaDescriptor d = i.next();
result.add(d.getIpAddress());
result.add(d.getIpAddress().getHostAddress());
}
return result;
}
@Override
public void startToReplicateFrom(ReplicationMasterDescriptor master) throws IOException, ClassNotFoundException, JMSException {
registerReplicaWithMaster(master);
TopicSubscriber replicationSubscription = master.getTopicSubscriber();
String uuid = registerReplicaWithMaster(master);
TopicSubscriber replicationSubscription = master.getTopicSubscriber(uuid);
URL initialLoadURL = master.getInitialLoadURL();
replicationSubscription.setMessageListener(new Replicator(master, racingEventServiceTracker));
InputStream is = initialLoadURL.openStream();
@@ -140,11 +148,24 @@ public class ReplicationServiceImpl implements ReplicationService, OperationExec
getRacingEventService().initiallyFillFrom(ois);
}
private void registerReplicaWithMaster(ReplicationMasterDescriptor master) throws IOException {
/**
* @return the UUID that the master generated for this client which is alsy entered into {@link #replicaUUIDs}
*/
private String registerReplicaWithMaster(ReplicationMasterDescriptor master) throws IOException, ClassNotFoundException {
URL replicationRegistrationRequestURL = master.getReplicationRegistrationRequestURL();
final URLConnection registrationRequestConnection = replicationRegistrationRequestURL.openConnection();
registrationRequestConnection.connect();
registrationRequestConnection.getContent();
InputStream content = (InputStream) registrationRequestConnection.getContent();
StringBuilder uuid = new StringBuilder();
byte[] buf = new byte[256];
int read = content.read(buf);
while (read != -1) {
uuid.append(new String(buf, 0, read));
read = content.read(buf);
}
String replicaUUID = uuid.toString();
replicaUUIDs.put(master, replicaUUID);
return replicaUUID;
}
@Override
@@ -2,6 +2,8 @@ package com.sap.sailing.server.replication.impl;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import javax.jms.JMSException;
@@ -71,13 +73,16 @@ public class ReplicationServlet extends Servlet {
}
}
private void registerClientWithReplicationService(HttpServletRequest req, HttpServletResponse resp) throws JMSException {
private void registerClientWithReplicationService(HttpServletRequest req, HttpServletResponse resp)
throws JMSException, IOException {
ReplicaDescriptor replica = getReplicaDescriptor(req);
getReplicationService().registerReplica(replica);
resp.setContentType("text/plain");
resp.getWriter().print(replica.getUuid());
}
private ReplicaDescriptor getReplicaDescriptor(HttpServletRequest req) {
// TODO Auto-generated method stub
return null;
private ReplicaDescriptor getReplicaDescriptor(HttpServletRequest req) throws UnknownHostException {
InetAddress ipAddress = InetAddress.getByName(req.getRemoteAddr());
return new ReplicaDescriptor(ipAddress);
}
}