Change license to Apache2 and reformat

This commit is contained in:
rbygrave committed 2012-09-15 00:00:08 +12:00
1 parent 96ce4c0ddf
commit 7aae897def
560 files changed
+73292 -85907

No files matched your search

@@ -1,83 +1,64 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
/**
* Represents a relatively small independent message.
* <p>
* In general terms we break up a potentially large object like
* RemoteTransactionEvent into many smaller BinaryMessages. This is so that if
* they don't all fit on a single Packet we can easily break them up and put
* them on multiple packets.
* </p>
* <p>
* Also note that for the Multicast approach a Packet will generally contain
* many messages each directed to different members of the cluster. So it would
* be common for many Ack, Resend and Control messages to all be contained in a
* single packet.
* </p>
*
* @author rbygrave
*
*/
public class BinaryMessage {
public static final int TYPE_MSGCONTROL = 0;
public static final int TYPE_BEANIUD = 1;
public static final int TYPE_TABLEIUD = 2;
public static final int TYPE_BEANDELTA = 3;
public static final int TYPE_BEANPATHUPDATE = 4;
public static final int TYPE_INDEX_INVALIDATE = 6;
public static final int TYPE_INDEX = 7;
public static final int TYPE_MSGACK = 8;
public static final int TYPE_MSGRESEND = 9;
private final ByteArrayOutputStream buffer;
private final DataOutputStream os;
private byte[] bytes;
/**
* Create with an estimated buffer size.
*/
public BinaryMessage(int bufSize) {
this.buffer = new ByteArrayOutputStream(bufSize);
this.os = new DataOutputStream(buffer);
}
/**
* Return the DataOutputStream to write content to.
*/
public DataOutputStream getOs() {
return os;
}
/**
* Return all the content as a byte array.
*/
public byte[] getByteArray() {
if (bytes == null) {
bytes = buffer.toByteArray();
}
return bytes;
}
}
package com.avaje.ebeaninternal.server.cluster;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
/**
* Represents a relatively small independent message.
* <p>
* In general terms we break up a potentially large object like
* RemoteTransactionEvent into many smaller BinaryMessages. This is so that if
* they don't all fit on a single Packet we can easily break them up and put
* them on multiple packets.
* </p>
* <p>
* Also note that for the Multicast approach a Packet will generally contain
* many messages each directed to different members of the cluster. So it would
* be common for many Ack, Resend and Control messages to all be contained in a
* single packet.
* </p>
*
* @author rbygrave
*
*/
public class BinaryMessage {
public static final int TYPE_MSGCONTROL = 0;
public static final int TYPE_BEANIUD = 1;
public static final int TYPE_TABLEIUD = 2;
public static final int TYPE_BEANDELTA = 3;
public static final int TYPE_BEANPATHUPDATE = 4;
public static final int TYPE_INDEX_INVALIDATE = 6;
public static final int TYPE_INDEX = 7;
public static final int TYPE_MSGACK = 8;
public static final int TYPE_MSGRESEND = 9;
private final ByteArrayOutputStream buffer;
private final DataOutputStream os;
private byte[] bytes;
/**
* Create with an estimated buffer size.
*/
public BinaryMessage(int bufSize) {
this.buffer = new ByteArrayOutputStream(bufSize);
this.os = new DataOutputStream(buffer);
}
/**
* Return the DataOutputStream to write content to.
*/
public DataOutputStream getOs() {
return os;
}
/**
* Return all the content as a byte array.
*/
public byte[] getByteArray() {
if (bytes == null) {
bytes = buffer.toByteArray();
}
return bytes;
}
}
@@ -1,42 +1,23 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster;
import java.util.ArrayList;
import java.util.List;
/**
* Holds a List of BinaryMessage's.
*
* @author rbygrave
*/
public class BinaryMessageList {
ArrayList<BinaryMessage> list = new ArrayList<BinaryMessage>();
public void add(BinaryMessage msg) {
list.add(msg);
}
public List<BinaryMessage> getList() {
return list;
}
}
package com.avaje.ebeaninternal.server.cluster;
import java.util.ArrayList;
import java.util.List;
/**
* Holds a List of BinaryMessage's.
*
* @author rbygrave
*/
public class BinaryMessageList {
ArrayList<BinaryMessage> list = new ArrayList<BinaryMessage>();
public void add(BinaryMessage msg) {
list.add(msg);
}
public List<BinaryMessage> getList() {
return list;
}
}
@@ -1,45 +1,28 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster;
import com.avaje.ebeaninternal.server.transaction.RemoteTransactionEvent;
/**
* Sends messages to the cluster members.
*/
public interface ClusterBroadcast {
/**
* Inform the other cluster members that this instance has come online and
* start any listeners etc.
*/
public void startup(ClusterManager clusterManager);
/**
* Inform the other cluster members that this instance is leaving and
* shutdown any listeners.
*/
public void shutdown();
/**
* Send a transaction event to all the members of the cluster.
*/
public void broadcast(RemoteTransactionEvent remoteTransEvent);
}
package com.avaje.ebeaninternal.server.cluster;
import com.avaje.ebeaninternal.server.transaction.RemoteTransactionEvent;
/**
* Sends messages to the cluster members.
*/
public interface ClusterBroadcast {
/**
* Inform the other cluster members that this instance has come online and
* start any listeners etc.
*/
public void startup(ClusterManager clusterManager);
/**
* Inform the other cluster members that this instance is leaving and
* shutdown any listeners.
*/
public void shutdown();
/**
* Send a transaction event to all the members of the cluster.
*/
public void broadcast(RemoteTransactionEvent remoteTransEvent);
}
@@ -1,122 +1,105 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.config.GlobalProperties;
import com.avaje.ebeaninternal.api.ClassUtil;
import com.avaje.ebeaninternal.server.cluster.mcast.McastClusterManager;
import com.avaje.ebeaninternal.server.cluster.socket.SocketClusterBroadcast;
import com.avaje.ebeaninternal.server.transaction.RemoteTransactionEvent;
/**
* Manages the cluster service.
*/
public class ClusterManager {
private static final Logger logger = Logger.getLogger(ClusterManager.class.getName());
private final ConcurrentHashMap<String, EbeanServer> serverMap = new ConcurrentHashMap<String, EbeanServer>();
private final Object monitor = new Object();
private final ClusterBroadcast broadcast;
private boolean started;
public ClusterManager() {
String clusterType = GlobalProperties.get("ebean.cluster.type", null);
if (clusterType == null || clusterType.trim().length() == 0) {
// not clustering this instance
this.broadcast = null;
} else {
try {
if ("mcast".equalsIgnoreCase(clusterType)) {
this.broadcast = new McastClusterManager();
} else if ("socket".equalsIgnoreCase(clusterType)) {
this.broadcast = new SocketClusterBroadcast();
} else {
logger.info("Clustering using [" + clusterType + "]");
this.broadcast = (ClusterBroadcast) ClassUtil.newInstance(clusterType);
}
} catch (Exception e) {
String msg = "Error initialising ClusterManager type [" + clusterType + "]";
logger.log(Level.SEVERE, msg, e);
throw new RuntimeException(e);
}
}
}
public void registerServer(EbeanServer server) {
synchronized (monitor) {
if (!started) {
startup();
}
serverMap.put(server.getName(), server);
}
}
public EbeanServer getServer(String name) {
synchronized (monitor) {
return serverMap.get(name);
}
}
private void startup() {
started = true;
if (broadcast != null) {
broadcast.startup(this);
}
}
/**
* Return true if clustering is on.
*/
public boolean isClustering() {
return broadcast != null;
}
/**
* Send the message headers and payload to every server in the cluster.
*/
public void broadcast(RemoteTransactionEvent remoteTransEvent) {
if (broadcast != null) {
broadcast.broadcast(remoteTransEvent);
}
}
/**
* Shutdown the service and Deregister from the cluster.
*/
public void shutdown() {
if (broadcast != null) {
logger.info("ClusterManager shutdown ");
broadcast.shutdown();
}
}
}
package com.avaje.ebeaninternal.server.cluster;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.config.GlobalProperties;
import com.avaje.ebeaninternal.api.ClassUtil;
import com.avaje.ebeaninternal.server.cluster.mcast.McastClusterManager;
import com.avaje.ebeaninternal.server.cluster.socket.SocketClusterBroadcast;
import com.avaje.ebeaninternal.server.transaction.RemoteTransactionEvent;
/**
* Manages the cluster service.
*/
public class ClusterManager {
private static final Logger logger = Logger.getLogger(ClusterManager.class.getName());
private final ConcurrentHashMap<String, EbeanServer> serverMap = new ConcurrentHashMap<String, EbeanServer>();
private final Object monitor = new Object();
private final ClusterBroadcast broadcast;
private boolean started;
public ClusterManager() {
String clusterType = GlobalProperties.get("ebean.cluster.type", null);
if (clusterType == null || clusterType.trim().length() == 0) {
// not clustering this instance
this.broadcast = null;
} else {
try {
if ("mcast".equalsIgnoreCase(clusterType)) {
this.broadcast = new McastClusterManager();
} else if ("socket".equalsIgnoreCase(clusterType)) {
this.broadcast = new SocketClusterBroadcast();
} else {
logger.info("Clustering using [" + clusterType + "]");
this.broadcast = (ClusterBroadcast) ClassUtil.newInstance(clusterType);
}
} catch (Exception e) {
String msg = "Error initialising ClusterManager type [" + clusterType + "]";
logger.log(Level.SEVERE, msg, e);
throw new RuntimeException(e);
}
}
}
public void registerServer(EbeanServer server) {
synchronized (monitor) {
if (!started) {
startup();
}
serverMap.put(server.getName(), server);
}
}
public EbeanServer getServer(String name) {
synchronized (monitor) {
return serverMap.get(name);
}
}
private void startup() {
started = true;
if (broadcast != null) {
broadcast.startup(this);
}
}
/**
* Return true if clustering is on.
*/
public boolean isClustering() {
return broadcast != null;
}
/**
* Send the message headers and payload to every server in the cluster.
*/
public void broadcast(RemoteTransactionEvent remoteTransEvent) {
if (broadcast != null) {
broadcast.broadcast(remoteTransEvent);
}
}
/**
* Shutdown the service and Deregister from the cluster.
*/
public void shutdown() {
if (broadcast != null) {
logger.info("ClusterManager shutdown ");
broadcast.shutdown();
}
}
}
@@ -1,43 +1,24 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster;
import java.io.Serializable;
/**
* Simple holder of binary data.
* Used to use Packet based serialisation of RemoteTransactionEvent
* with simple Java Serialisation of the DataHolder.
*/
public class DataHolder implements Serializable {
private static final long serialVersionUID = 9090748723571322192L;
private final byte[] data;
public DataHolder(byte[] data) {
this.data = data;
}
public byte[] getData() {
return data;
}
}
package com.avaje.ebeaninternal.server.cluster;
import java.io.Serializable;
/**
* Simple holder of binary data.
* Used to use Packet based serialisation of RemoteTransactionEvent
* with simple Java Serialisation of the DataHolder.
*/
public class DataHolder implements Serializable {
private static final long serialVersionUID = 9090748723571322192L;
private final byte[] data;
public DataHolder(byte[] data) {
this.data = data;
}
public byte[] getData() {
return data;
}
}
@@ -1,212 +1,193 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutputStream;
import java.io.IOException;
/**
* Represents the contents sent as a single DatagramPacket.
* <p>
* The contents is typically multiple messages (ACK,PING etc) or all or part of
* a RemoteTransactionEvent.
* </p>
* <p>
* Due to the hard limit on the size of UDP packets a RemoteTransactionEvent
* with lots of information could be broken up into multiple packets.
* </p>
*
* @author rbygrave
*/
public class Packet {
/**
* A Packet that holds protocol messages like ACK, PING etc.
*/
public static final short TYPE_MESSAGES = 1;
/**
* A Packet that holds TransactionEvent information such as Bean
* and or Table IUD information.
*/
public static final short TYPE_TRANSEVENT = 2;
/**
* The type of Packet.
*/
protected short packetType;
/**
* The PacketId.
*/
protected long packetId;
/**
* The timestamp the Packet was created.
*/
protected long timestamp;
/**
* The EbeanServer name this relates to if relevant.
*/
protected String serverName;
protected ByteArrayOutputStream buffer;
protected DataOutputStream dataOut;
protected byte[] bytes;
/**
* The number of messages in this Packet.
*/
private int messageCount;
/**
* The number of times this Packet was resent.
*/
private int resendCount;
/**
* Create a Packet for writing messages to.
*/
public static Packet forWrite(short packetType, long packetId, long timestamp, String serverName) throws IOException {
return new Packet(true, packetType, packetId, timestamp, serverName);
}
/**
* Create a Packet just reading the Header information.
*/
public static Packet readHeader(DataInput dataInput) throws IOException {
short packetType = dataInput.readShort();
long packetId = dataInput.readLong();
long timestamp = dataInput.readLong();
String serverName = dataInput.readUTF();
return new Packet(false, packetType, packetId, timestamp, serverName);
}
protected Packet(boolean write, short packetType, long packetId, long timestamp, String serverName) throws IOException{
this.packetType = packetType;
this.packetId = packetId;
this.timestamp = timestamp;
this.serverName = serverName;
if (write){
this.buffer = new ByteArrayOutputStream();
this.dataOut = new DataOutputStream(buffer);
writeHeader();
} else {
this.buffer = null;
this.dataOut = null;
}
}
private void writeHeader() throws IOException {
dataOut.writeShort(packetType);
dataOut.writeLong(packetId);
dataOut.writeLong(timestamp);
dataOut.writeUTF(serverName);
}
public int incrementResendCount() {
return resendCount++;
}
public short getPacketType() {
return packetType;
}
public long getPacketId() {
return packetId;
}
public long getTimestamp() {
return timestamp;
}
public String getServerName() {
return serverName;
}
public void writeEof() throws IOException {
dataOut.writeBoolean(false);
}
public void read(DataInput dataInput) throws IOException {
boolean more = dataInput.readBoolean();
while (more){
int msgType = dataInput.readInt();
readMessage(dataInput, msgType);
// see if there is more information
more = dataInput.readBoolean();
}
}
/**
* Overridden by more specific Packet implementations to read the messages.
*/
protected void readMessage(DataInput dataInput, int msgType) throws IOException {
}
/**
* Write a binary message to this packet returning true if there was
* enough room to do so. Return false if the message was too large for
* the remaining space left - in this case another Packet should be
* created to put that message into.
*/
public boolean writeBinaryMessage(BinaryMessage msg, int maxPacketSize) throws IOException {
byte[] bytes = msg.getByteArray();
if (messageCount > 0 && (bytes.length + buffer.size() > maxPacketSize)){
// we are actually going to ignore the maxPacketSize iff we have one
// large message.
// false = no more messages
dataOut.writeBoolean(false);
return false;
}
++messageCount;
// true = another message follows
dataOut.writeBoolean(true);
dataOut.write(bytes);
return true;
}
public int getSize() {
return getBytes().length;
}
/**
* Return the Packet as raw bytes.
*/
public byte[] getBytes() {
if (bytes == null){
bytes = buffer.toByteArray();
buffer = null;
dataOut = null;
}
return bytes;
}
}
package com.avaje.ebeaninternal.server.cluster;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutputStream;
import java.io.IOException;
/**
* Represents the contents sent as a single DatagramPacket.
* <p>
* The contents is typically multiple messages (ACK,PING etc) or all or part of
* a RemoteTransactionEvent.
* </p>
* <p>
* Due to the hard limit on the size of UDP packets a RemoteTransactionEvent
* with lots of information could be broken up into multiple packets.
* </p>
*
* @author rbygrave
*/
public class Packet {
/**
* A Packet that holds protocol messages like ACK, PING etc.
*/
public static final short TYPE_MESSAGES = 1;
/**
* A Packet that holds TransactionEvent information such as Bean
* and or Table IUD information.
*/
public static final short TYPE_TRANSEVENT = 2;
/**
* The type of Packet.
*/
protected short packetType;
/**
* The PacketId.
*/
protected long packetId;
/**
* The timestamp the Packet was created.
*/
protected long timestamp;
/**
* The EbeanServer name this relates to if relevant.
*/
protected String serverName;
protected ByteArrayOutputStream buffer;
protected DataOutputStream dataOut;
protected byte[] bytes;
/**
* The number of messages in this Packet.
*/
private int messageCount;
/**
* The number of times this Packet was resent.
*/
private int resendCount;
/**
* Create a Packet for writing messages to.
*/
public static Packet forWrite(short packetType, long packetId, long timestamp, String serverName) throws IOException {
return new Packet(true, packetType, packetId, timestamp, serverName);
}
/**
* Create a Packet just reading the Header information.
*/
public static Packet readHeader(DataInput dataInput) throws IOException {
short packetType = dataInput.readShort();
long packetId = dataInput.readLong();
long timestamp = dataInput.readLong();
String serverName = dataInput.readUTF();
return new Packet(false, packetType, packetId, timestamp, serverName);
}
protected Packet(boolean write, short packetType, long packetId, long timestamp, String serverName) throws IOException{
this.packetType = packetType;
this.packetId = packetId;
this.timestamp = timestamp;
this.serverName = serverName;
if (write){
this.buffer = new ByteArrayOutputStream();
this.dataOut = new DataOutputStream(buffer);
writeHeader();
} else {
this.buffer = null;
this.dataOut = null;
}
}
private void writeHeader() throws IOException {
dataOut.writeShort(packetType);
dataOut.writeLong(packetId);
dataOut.writeLong(timestamp);
dataOut.writeUTF(serverName);
}
public int incrementResendCount() {
return resendCount++;
}
public short getPacketType() {
return packetType;
}
public long getPacketId() {
return packetId;
}
public long getTimestamp() {
return timestamp;
}
public String getServerName() {
return serverName;
}
public void writeEof() throws IOException {
dataOut.writeBoolean(false);
}
public void read(DataInput dataInput) throws IOException {
boolean more = dataInput.readBoolean();
while (more){
int msgType = dataInput.readInt();
readMessage(dataInput, msgType);
// see if there is more information
more = dataInput.readBoolean();
}
}
/**
* Overridden by more specific Packet implementations to read the messages.
*/
protected void readMessage(DataInput dataInput, int msgType) throws IOException {
}
/**
* Write a binary message to this packet returning true if there was
* enough room to do so. Return false if the message was too large for
* the remaining space left - in this case another Packet should be
* created to put that message into.
*/
public boolean writeBinaryMessage(BinaryMessage msg, int maxPacketSize) throws IOException {
byte[] bytes = msg.getByteArray();
if (messageCount > 0 && (bytes.length + buffer.size() > maxPacketSize)){
// we are actually going to ignore the maxPacketSize iff we have one
// large message.
// false = no more messages
dataOut.writeBoolean(false);
return false;
}
++messageCount;
// true = another message follows
dataOut.writeBoolean(true);
dataOut.write(bytes);
return true;
}
public int getSize() {
return getBytes().length;
}
/**
* Return the Packet as raw bytes.
*/
public byte[] getBytes() {
if (bytes == null){
bytes = buffer.toByteArray();
buffer = null;
dataOut = null;
}
return bytes;
}
}
@@ -1,88 +1,69 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster;
import java.io.DataInput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebeaninternal.server.cluster.mcast.Message;
import com.avaje.ebeaninternal.server.cluster.mcast.MessageAck;
import com.avaje.ebeaninternal.server.cluster.mcast.MessageControl;
import com.avaje.ebeaninternal.server.cluster.mcast.MessageResend;
/**
* A Packet that contains Ack, Resend and Control messages.
*
* @author rbygrave
*/
public class PacketMessages extends Packet {
private final ArrayList<Message> messages;
public static PacketMessages forWrite(long packetId, long timestamp, String serverName) throws IOException {
return new PacketMessages(true, packetId, timestamp, serverName);
}
public static PacketMessages forRead(Packet header) throws IOException {
return new PacketMessages(header);
}
private PacketMessages(boolean write, long packetId, long timestamp, String serverName) throws IOException {
super(write, TYPE_MESSAGES, packetId, timestamp, serverName);
this.messages = null;
}
private PacketMessages(Packet header) throws IOException {
super(false, TYPE_MESSAGES, header.packetId, header.timestamp, header.serverName);
this.messages = new ArrayList<Message>();
}
/**
* Return the messages contained in this Packet.
*/
public List<Message> getMessages() {
return messages;
}
/**
* Read the messages (Ack, Resend or Control) contained in this packet.
*/
protected void readMessage(DataInput dataInput, int msgType) throws IOException {
switch (msgType) {
case BinaryMessage.TYPE_MSGCONTROL:
messages.add(MessageControl.readBinaryMessage(dataInput));
break;
case BinaryMessage.TYPE_MSGACK:
messages.add(MessageAck.readBinaryMessage(dataInput));
break;
case BinaryMessage.TYPE_MSGRESEND:
messages.add(MessageResend.readBinaryMessage(dataInput));
break;
default:
throw new RuntimeException("Invalid Transaction msgType "+msgType);
}
}
}
package com.avaje.ebeaninternal.server.cluster;
import java.io.DataInput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebeaninternal.server.cluster.mcast.Message;
import com.avaje.ebeaninternal.server.cluster.mcast.MessageAck;
import com.avaje.ebeaninternal.server.cluster.mcast.MessageControl;
import com.avaje.ebeaninternal.server.cluster.mcast.MessageResend;
/**
* A Packet that contains Ack, Resend and Control messages.
*
* @author rbygrave
*/
public class PacketMessages extends Packet {
private final ArrayList<Message> messages;
public static PacketMessages forWrite(long packetId, long timestamp, String serverName) throws IOException {
return new PacketMessages(true, packetId, timestamp, serverName);
}
public static PacketMessages forRead(Packet header) throws IOException {
return new PacketMessages(header);
}
private PacketMessages(boolean write, long packetId, long timestamp, String serverName) throws IOException {
super(write, TYPE_MESSAGES, packetId, timestamp, serverName);
this.messages = null;
}
private PacketMessages(Packet header) throws IOException {
super(false, TYPE_MESSAGES, header.packetId, header.timestamp, header.serverName);
this.messages = new ArrayList<Message>();
}
/**
* Return the messages contained in this Packet.
*/
public List<Message> getMessages() {
return messages;
}
/**
* Read the messages (Ack, Resend or Control) contained in this packet.
*/
protected void readMessage(DataInput dataInput, int msgType) throws IOException {
switch (msgType) {
case BinaryMessage.TYPE_MSGCONTROL:
messages.add(MessageControl.readBinaryMessage(dataInput));
break;
case BinaryMessage.TYPE_MSGACK:
messages.add(MessageAck.readBinaryMessage(dataInput));
break;
case BinaryMessage.TYPE_MSGRESEND:
messages.add(MessageResend.readBinaryMessage(dataInput));
break;
default:
throw new RuntimeException("Invalid Transaction msgType "+msgType);
}
}
}
@@ -1,94 +1,75 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster;
import java.io.DataInput;
import java.io.IOException;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.api.TransactionEventTable.TableIUD;
import com.avaje.ebeaninternal.server.transaction.BeanDelta;
import com.avaje.ebeaninternal.server.transaction.BeanPersistIds;
import com.avaje.ebeaninternal.server.transaction.IndexEvent;
import com.avaje.ebeaninternal.server.transaction.RemoteTransactionEvent;
/**
* A Packet holding TransactionEvent data.
* <p>
* Due to the hard limit for UDP packet sizes a RemoteTransactionEvent
* is actually broken up into smaller messages.
* </p>
* @author rbygrave
*/
public class PacketTransactionEvent extends Packet {
private final SpiEbeanServer server;
private final RemoteTransactionEvent event;
public static PacketTransactionEvent forWrite(long packetId, long timestamp, String serverName) throws IOException {
return new PacketTransactionEvent(true, packetId, timestamp, serverName);
}
private PacketTransactionEvent(boolean write, long packetId, long timestamp, String serverName) throws IOException {
super(write, TYPE_TRANSEVENT, packetId, timestamp, serverName);
this.server = null;
this.event = null;
}
private PacketTransactionEvent(Packet header, SpiEbeanServer server) throws IOException {
super(false, TYPE_TRANSEVENT, header.packetId, header.timestamp, header.serverName);
this.server = server;
this.event = new RemoteTransactionEvent(server);
}
public static PacketTransactionEvent forRead(Packet header, SpiEbeanServer server) throws IOException {
return new PacketTransactionEvent(header, server);
}
public RemoteTransactionEvent getEvent() {
return event;
}
protected void readMessage(DataInput dataInput, int msgType) throws IOException {
switch (msgType) {
case BinaryMessage.TYPE_BEANIUD:
event.addBeanPersistIds(BeanPersistIds.readBinaryMessage(server, dataInput));
break;
case BinaryMessage.TYPE_TABLEIUD:
event.addTableIUD(TableIUD.readBinaryMessage(dataInput));
break;
case BinaryMessage.TYPE_BEANDELTA:
event.addBeanDelta(BeanDelta.readBinaryMessage(server, dataInput));
break;
case BinaryMessage.TYPE_INDEX:
event.addIndexEvent(IndexEvent.readBinaryMessage(dataInput));
break;
default:
throw new RuntimeException("Invalid Transaction msgType "+msgType);
}
}
}
package com.avaje.ebeaninternal.server.cluster;
import java.io.DataInput;
import java.io.IOException;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.api.TransactionEventTable.TableIUD;
import com.avaje.ebeaninternal.server.transaction.BeanDelta;
import com.avaje.ebeaninternal.server.transaction.BeanPersistIds;
import com.avaje.ebeaninternal.server.transaction.IndexEvent;
import com.avaje.ebeaninternal.server.transaction.RemoteTransactionEvent;
/**
* A Packet holding TransactionEvent data.
* <p>
* Due to the hard limit for UDP packet sizes a RemoteTransactionEvent
* is actually broken up into smaller messages.
* </p>
* @author rbygrave
*/
public class PacketTransactionEvent extends Packet {
private final SpiEbeanServer server;
private final RemoteTransactionEvent event;
public static PacketTransactionEvent forWrite(long packetId, long timestamp, String serverName) throws IOException {
return new PacketTransactionEvent(true, packetId, timestamp, serverName);
}
private PacketTransactionEvent(boolean write, long packetId, long timestamp, String serverName) throws IOException {
super(write, TYPE_TRANSEVENT, packetId, timestamp, serverName);
this.server = null;
this.event = null;
}
private PacketTransactionEvent(Packet header, SpiEbeanServer server) throws IOException {
super(false, TYPE_TRANSEVENT, header.packetId, header.timestamp, header.serverName);
this.server = server;
this.event = new RemoteTransactionEvent(server);
}
public static PacketTransactionEvent forRead(Packet header, SpiEbeanServer server) throws IOException {
return new PacketTransactionEvent(header, server);
}
public RemoteTransactionEvent getEvent() {
return event;
}
protected void readMessage(DataInput dataInput, int msgType) throws IOException {
switch (msgType) {
case BinaryMessage.TYPE_BEANIUD:
event.addBeanPersistIds(BeanPersistIds.readBinaryMessage(server, dataInput));
break;
case BinaryMessage.TYPE_TABLEIUD:
event.addTableIUD(TableIUD.readBinaryMessage(dataInput));
break;
case BinaryMessage.TYPE_BEANDELTA:
event.addBeanDelta(BeanDelta.readBinaryMessage(server, dataInput));
break;
case BinaryMessage.TYPE_INDEX:
event.addIndexEvent(IndexEvent.readBinaryMessage(dataInput));
break;
default:
throw new RuntimeException("Invalid Transaction msgType "+msgType);
}
}
}
@@ -1,22 +1,3 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster;
import java.io.IOException;
@@ -1,22 +1,3 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster;
import java.io.ByteArrayInputStream;
@@ -1,62 +1,43 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.util.ArrayList;
import java.util.List;
/**
* Holds a list of ACK and RESEND messages that should be sent out.
*
* @author rbygrave
*/
public class AckResendMessages {
ArrayList<Message> messages = new ArrayList<Message>();
public String toString() {
return messages.toString();
}
public int size() {
return messages.size();
}
/**
* Add a ACK message to send.
*/
public void add(MessageAck ack){
messages.add(ack);
}
/**
* Add a RESEND message to send.
*/
public void add(MessageResend resend){
messages.add(resend);
}
/**
* Return all the messages to be sent out.
*/
public List<Message> getMessages() {
return messages;
}
}
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.util.ArrayList;
import java.util.List;
/**
* Holds a list of ACK and RESEND messages that should be sent out.
*
* @author rbygrave
*/
public class AckResendMessages {
ArrayList<Message> messages = new ArrayList<Message>();
public String toString() {
return messages.toString();
}
public int size() {
return messages.size();
}
/**
* Add a ACK message to send.
*/
public void add(MessageAck ack){
messages.add(ack);
}
/**
* Add a RESEND message to send.
*/
public void add(MessageResend resend){
messages.add(resend);
}
/**
* Return all the messages to be sent out.
*/
public List<Message> getMessages() {
return messages;
}
}
@@ -1,72 +1,53 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.util.HashMap;
import java.util.List;
/**
* For this node this holds the ACK gotAllPoint for each member in the cluster.
* <p>
* As we receive messages from other members of the cluster periodically we need
* to send them ACK messages to say we got all the packets up to the gotAllPoint.
* </p>
* Thread Safety note: Object only used by McastClusterBroadcast Manager thread.
* So Single Threaded access.
*
* @author rbygrave
*/
public class IncomingPacketsLastAck {
private HashMap<String,MessageAck> lastAckMap = new HashMap<String, MessageAck>();
public String toString() {
return lastAckMap.values().toString();
}
/**
* Remove a member of the cluster who has left.
*/
public void remove(String memberHostPort) {
lastAckMap.remove(memberHostPort);
}
/**
* Get the last Ack point for a given member of the cluster.
*/
public MessageAck getLastAck(String memberHostPort) {
return lastAckMap.get(memberHostPort);
}
/**
* For the ACK messages in AckResendMessages update the
* last Ack packetId.
*/
public void updateLastAck(AckResendMessages ackResendMessages) {
List<Message> messages = ackResendMessages.getMessages();
for (int i = 0; i < messages.size(); i++) {
Message msg = messages.get(i);
if (msg instanceof MessageAck){
MessageAck lastAck = (MessageAck)msg;
lastAckMap.put(lastAck.getToHostPort(), lastAck);
}
}
}
}
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.util.HashMap;
import java.util.List;
/**
* For this node this holds the ACK gotAllPoint for each member in the cluster.
* <p>
* As we receive messages from other members of the cluster periodically we need
* to send them ACK messages to say we got all the packets up to the gotAllPoint.
* </p>
* Thread Safety note: Object only used by McastClusterBroadcast Manager thread.
* So Single Threaded access.
*
* @author rbygrave
*/
public class IncomingPacketsLastAck {
private HashMap<String,MessageAck> lastAckMap = new HashMap<String, MessageAck>();
public String toString() {
return lastAckMap.values().toString();
}
/**
* Remove a member of the cluster who has left.
*/
public void remove(String memberHostPort) {
lastAckMap.remove(memberHostPort);
}
/**
* Get the last Ack point for a given member of the cluster.
*/
public MessageAck getLastAck(String memberHostPort) {
return lastAckMap.get(memberHostPort);
}
/**
* For the ACK messages in AckResendMessages update the
* last Ack packetId.
*/
public void updateLastAck(AckResendMessages ackResendMessages) {
List<Message> messages = ackResendMessages.getMessages();
for (int i = 0; i < messages.size(); i++) {
Message msg = messages.get(i);
if (msg instanceof MessageAck){
MessageAck lastAck = (MessageAck)msg;
lastAckMap.put(lastAck.getToHostPort(), lastAck);
}
}
}
}
@@ -1,292 +1,273 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
/**
* For Incoming Packets remembers the packets we have received and processed.
* <p>
* This determines the gotAllPoint per cluster member and identifies missing
* packets (gap between gotAllPoint and gotMaxPoint).
* </p>
* <p>
* This information is used by the managerThread so send ACK's for messages we
* have received and RESEND messages to fill the missing packets we have
* detected.
* </p>
*
* @author rbygrave
*
*/
public class IncomingPacketsProcessed {
private final ConcurrentHashMap<String, GotAllPoint> mapByMember = new ConcurrentHashMap<String, GotAllPoint>();
private final int maxResendIncoming;
public IncomingPacketsProcessed(int maxResendIncoming) {
this.maxResendIncoming = maxResendIncoming;
}
public void removeMember(String memberKey) {
mapByMember.remove(memberKey);
}
/**
* Return true if we should process this packet. Return false if we have
* already processed the packet.
*/
public boolean isProcessPacket(String memberKey, long packetId) {
GotAllPoint memberPackets = getMemberPackets(memberKey);
return memberPackets.processPacket(packetId);
}
/**
* Build the list of ACK and RESEND messages that we should send out
* to the other members of the cluster.
*/
public AckResendMessages getAckResendMessages(IncomingPacketsLastAck lastAck) {
// Called by the McastClusterBroadcast manager thread
AckResendMessages response = new AckResendMessages();
for (GotAllPoint member : mapByMember.values()) {
MessageAck lastAckMessage = lastAck.getLastAck(member.getMemberKey());
member.addAckResendMessages(response, lastAckMessage);
}
return response;
}
private GotAllPoint getMemberPackets(String memberKey) {
// This method is only called single threaded
// by the listener thread so I'm happy that this
// put into mapByMember is ok.
GotAllPoint memberGotAllPoint = mapByMember.get(memberKey);
if (memberGotAllPoint == null) {
memberGotAllPoint = new GotAllPoint(memberKey, maxResendIncoming);
mapByMember.put(memberKey, memberGotAllPoint);
}
return memberGotAllPoint;
}
/**
* Keeps track of packets received from a particular member of the cluster.
* <p>
* It notes the packetIds of the packets received and uses those to maintain
* the 'gotAllPoint'. The 'gotAllPoint' is the packetId which we know we
* received all the previous packets.
* </p>
*/
public static class GotAllPoint {
private static final Logger logger = Logger.getLogger(GotAllPoint.class.getName());
private final String memberKey;
private final int maxResendIncoming;
private long gotAllPoint;
private long gotMaxPoint;
/**
* Packets received out of order.
*/
private ArrayList<Long> outOfOrderList = new ArrayList<Long>();
private HashMap<Long,Integer> resendCountMap = new HashMap<Long,Integer>();
public GotAllPoint(String memberKey, int maxResendIncoming) {
this.memberKey = memberKey;
this.maxResendIncoming = maxResendIncoming;
}
/**
* Add ACK and RESEND messages if required.
*/
public void addAckResendMessages(AckResendMessages response, MessageAck lastAckMessage) {
synchronized (this) {
if (lastAckMessage != null && lastAckMessage.getGotAllPacketId() >= gotAllPoint) {
// nothing has changed
} else {
// ACK that we have got every packet up to gotAllPoint
response.add(new MessageAck(memberKey, gotAllPoint));
}
if (getMissingPacketCount() > 0) {
// Ask for these Packets to be RESENT
List<Long> missingPackets = getMissingPackets();
response.add(new MessageResend(memberKey, missingPackets));
}
}
}
public String getMemberKey() {
return memberKey;
}
public long getGotAllPoint() {
synchronized (this) {
return gotAllPoint;
}
}
public long getGotMaxPoint() {
synchronized (this) {
return gotMaxPoint;
}
}
private int getMissingPacketCount() {
if (gotMaxPoint <= gotAllPoint) {
if (!resendCountMap.isEmpty()) {
resendCountMap.clear();
}
return 0;
}
return (int) (gotMaxPoint - gotAllPoint) - outOfOrderList.size();
}
public List<Long> getMissingPackets() {
synchronized (this) {
ArrayList<Long> missingList = new ArrayList<Long>();
// this is not particularly efficient but expecting
// the outOfOrderList to be relatively small
boolean lostPacket = false;
for (long i = gotAllPoint + 1; i < gotMaxPoint; i++) {
Long packetId = Long.valueOf(i);
if (!outOfOrderList.contains(packetId)) {
if (incrementResendCount(packetId)) {
// request this packet be resent
missingList.add(packetId);
} else {
lostPacket = true;
}
}
}
if (lostPacket){
checkOutOfOrderList();
}
return missingList;
}
}
/**
* Return true if this packet has not yet exceeded the maxResendCount.
*/
private boolean incrementResendCount(Long packetId){
Integer resendCount = resendCountMap.get(packetId);
if (resendCount != null){
int i = resendCount.intValue() + 1;
if (i > maxResendIncoming){
// we are going to give up trying to get this packet now
logger.warning("Exceeded maxResendIncoming["+maxResendIncoming+"] for packet["+packetId+"]. Giving up on requesting it.");
resendCountMap.remove(packetId);
outOfOrderList.add(packetId);
return false;
}
resendCount = Integer.valueOf(i);
resendCountMap.put(packetId, resendCount);
} else {
resendCountMap.put(packetId, ONE);
}
return true;
}
private static final Integer ONE = Integer.valueOf(1);
public boolean processPacket(long packetId) {
synchronized (this) {
if (gotAllPoint == 0) {
gotAllPoint = packetId;
return true;
}
if (packetId <= gotAllPoint) {
// already processed this packet
return false;
}
if (!resendCountMap.isEmpty()){
resendCountMap.remove(Long.valueOf(packetId));
}
if (packetId == gotAllPoint + 1) {
gotAllPoint = packetId;
} else {
if (packetId > gotMaxPoint) {
gotMaxPoint = packetId;
}
outOfOrderList.add(Long.valueOf(packetId));
}
checkOutOfOrderList();
return true;
}
}
private void checkOutOfOrderList() {
if (outOfOrderList.size() == 0) {
return;
}
boolean continueCheck;
do {
continueCheck = false;
long nextPoint = gotAllPoint + 1;
Iterator<Long> it = outOfOrderList.iterator();
while (it.hasNext()) {
Long id = it.next();
if (id.longValue() == nextPoint) {
// we found the next one in the outOfOrderList
it.remove();
gotAllPoint = nextPoint;
continueCheck = true;
break;
}
}
} while (continueCheck);
}
}
}
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
/**
* For Incoming Packets remembers the packets we have received and processed.
* <p>
* This determines the gotAllPoint per cluster member and identifies missing
* packets (gap between gotAllPoint and gotMaxPoint).
* </p>
* <p>
* This information is used by the managerThread so send ACK's for messages we
* have received and RESEND messages to fill the missing packets we have
* detected.
* </p>
*
* @author rbygrave
*
*/
public class IncomingPacketsProcessed {
private final ConcurrentHashMap<String, GotAllPoint> mapByMember = new ConcurrentHashMap<String, GotAllPoint>();
private final int maxResendIncoming;
public IncomingPacketsProcessed(int maxResendIncoming) {
this.maxResendIncoming = maxResendIncoming;
}
public void removeMember(String memberKey) {
mapByMember.remove(memberKey);
}
/**
* Return true if we should process this packet. Return false if we have
* already processed the packet.
*/
public boolean isProcessPacket(String memberKey, long packetId) {
GotAllPoint memberPackets = getMemberPackets(memberKey);
return memberPackets.processPacket(packetId);
}
/**
* Build the list of ACK and RESEND messages that we should send out
* to the other members of the cluster.
*/
public AckResendMessages getAckResendMessages(IncomingPacketsLastAck lastAck) {
// Called by the McastClusterBroadcast manager thread
AckResendMessages response = new AckResendMessages();
for (GotAllPoint member : mapByMember.values()) {
MessageAck lastAckMessage = lastAck.getLastAck(member.getMemberKey());
member.addAckResendMessages(response, lastAckMessage);
}
return response;
}
private GotAllPoint getMemberPackets(String memberKey) {
// This method is only called single threaded
// by the listener thread so I'm happy that this
// put into mapByMember is ok.
GotAllPoint memberGotAllPoint = mapByMember.get(memberKey);
if (memberGotAllPoint == null) {
memberGotAllPoint = new GotAllPoint(memberKey, maxResendIncoming);
mapByMember.put(memberKey, memberGotAllPoint);
}
return memberGotAllPoint;
}
/**
* Keeps track of packets received from a particular member of the cluster.
* <p>
* It notes the packetIds of the packets received and uses those to maintain
* the 'gotAllPoint'. The 'gotAllPoint' is the packetId which we know we
* received all the previous packets.
* </p>
*/
public static class GotAllPoint {
private static final Logger logger = Logger.getLogger(GotAllPoint.class.getName());
private final String memberKey;
private final int maxResendIncoming;
private long gotAllPoint;
private long gotMaxPoint;
/**
* Packets received out of order.
*/
private ArrayList<Long> outOfOrderList = new ArrayList<Long>();
private HashMap<Long,Integer> resendCountMap = new HashMap<Long,Integer>();
public GotAllPoint(String memberKey, int maxResendIncoming) {
this.memberKey = memberKey;
this.maxResendIncoming = maxResendIncoming;
}
/**
* Add ACK and RESEND messages if required.
*/
public void addAckResendMessages(AckResendMessages response, MessageAck lastAckMessage) {
synchronized (this) {
if (lastAckMessage != null && lastAckMessage.getGotAllPacketId() >= gotAllPoint) {
// nothing has changed
} else {
// ACK that we have got every packet up to gotAllPoint
response.add(new MessageAck(memberKey, gotAllPoint));
}
if (getMissingPacketCount() > 0) {
// Ask for these Packets to be RESENT
List<Long> missingPackets = getMissingPackets();
response.add(new MessageResend(memberKey, missingPackets));
}
}
}
public String getMemberKey() {
return memberKey;
}
public long getGotAllPoint() {
synchronized (this) {
return gotAllPoint;
}
}
public long getGotMaxPoint() {
synchronized (this) {
return gotMaxPoint;
}
}
private int getMissingPacketCount() {
if (gotMaxPoint <= gotAllPoint) {
if (!resendCountMap.isEmpty()) {
resendCountMap.clear();
}
return 0;
}
return (int) (gotMaxPoint - gotAllPoint) - outOfOrderList.size();
}
public List<Long> getMissingPackets() {
synchronized (this) {
ArrayList<Long> missingList = new ArrayList<Long>();
// this is not particularly efficient but expecting
// the outOfOrderList to be relatively small
boolean lostPacket = false;
for (long i = gotAllPoint + 1; i < gotMaxPoint; i++) {
Long packetId = Long.valueOf(i);
if (!outOfOrderList.contains(packetId)) {
if (incrementResendCount(packetId)) {
// request this packet be resent
missingList.add(packetId);
} else {
lostPacket = true;
}
}
}
if (lostPacket){
checkOutOfOrderList();
}
return missingList;
}
}
/**
* Return true if this packet has not yet exceeded the maxResendCount.
*/
private boolean incrementResendCount(Long packetId){
Integer resendCount = resendCountMap.get(packetId);
if (resendCount != null){
int i = resendCount.intValue() + 1;
if (i > maxResendIncoming){
// we are going to give up trying to get this packet now
logger.warning("Exceeded maxResendIncoming["+maxResendIncoming+"] for packet["+packetId+"]. Giving up on requesting it.");
resendCountMap.remove(packetId);
outOfOrderList.add(packetId);
return false;
}
resendCount = Integer.valueOf(i);
resendCountMap.put(packetId, resendCount);
} else {
resendCountMap.put(packetId, ONE);
}
return true;
}
private static final Integer ONE = Integer.valueOf(1);
public boolean processPacket(long packetId) {
synchronized (this) {
if (gotAllPoint == 0) {
gotAllPoint = packetId;
return true;
}
if (packetId <= gotAllPoint) {
// already processed this packet
return false;
}
if (!resendCountMap.isEmpty()){
resendCountMap.remove(Long.valueOf(packetId));
}
if (packetId == gotAllPoint + 1) {
gotAllPoint = packetId;
} else {
if (packetId > gotMaxPoint) {
gotMaxPoint = packetId;
}
outOfOrderList.add(Long.valueOf(packetId));
}
checkOutOfOrderList();
return true;
}
}
private void checkOutOfOrderList() {
if (outOfOrderList.size() == 0) {
return;
}
boolean continueCheck;
do {
continueCheck = false;
long nextPoint = gotAllPoint + 1;
Iterator<Long> it = outOfOrderList.iterator();
while (it.hasNext()) {
Long id = it.next();
if (id.longValue() == nextPoint) {
// we found the next one in the outOfOrderList
it.remove();
gotAllPoint = nextPoint;
continueCheck = true;
break;
}
}
} while (continueCheck);
}
}
}
@@ -1,22 +1,3 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.io.IOException;
@@ -1,22 +1,3 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.io.DataInput;
@@ -1,135 +1,116 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.avaje.ebeaninternal.server.cluster.Packet;
/**
* Handles the sending of Packets via DatagramPacket.
*
* @author rbygrave
*/
public class McastSender {
private static final Logger logger = Logger.getLogger(McastSender.class.getName());
private final int port;
private final InetAddress inetAddress;
private final DatagramSocket sock;
private final InetSocketAddress sendAddr;
private final String senderHostPort;
public McastSender(int port, String address, int sendPort, String sendAddress) {
try {
this.port = port;
this.inetAddress = InetAddress.getByName(address);
InetAddress sendInetAddress = null;
if (sendAddress != null) {
sendInetAddress = InetAddress.getByName(sendAddress);
} else {
sendInetAddress = InetAddress.getLocalHost();
}
if (sendPort > 0) {
this.sock = new DatagramSocket(sendPort, sendInetAddress);
} else {
this.sock = new DatagramSocket(new InetSocketAddress(sendInetAddress, 0));
}
String msg = "Cluster Multicast Sender on["+sendInetAddress.getHostAddress()+":"+sock.getLocalPort()+"]";
logger.info(msg);
this.sendAddr = new InetSocketAddress(sendInetAddress, sock.getLocalPort());
this.senderHostPort = sendInetAddress.getHostAddress()+":"+sock.getLocalPort();
} catch (Exception e) {
String msg = "McastSender port:" + port + " sendPort:" + sendPort + " " + address;
throw new RuntimeException(msg, e);
}
}
/**
* Return the send Address so that if we have loopback messages we can
* detect if they where sent by this local sender and hence should be
* ignored.
*/
public InetSocketAddress getAddress() {
return sendAddr;
}
/**
* Return the Host and Port of the sender. This is used to uniquely identify
* this instance in the cluster.
*/
public String getSenderHostPort() {
return senderHostPort;
}
/**
* Send the packet.
*/
public int sendPacket(Packet packet) throws IOException {
byte[] pktBytes = packet.getBytes();
if (logger.isLoggable(Level.FINE)){
logger.fine("OUTGOING packet: " + packet.getPacketId() + " size:" + pktBytes.length);
}
if (pktBytes.length > 65507){
logger.warning("OUTGOING packet: " + packet.getPacketId() + " size:" + pktBytes.length
+" likely to be truncated using UDP with a MAXIMUM length of 65507");
}
DatagramPacket pack = new DatagramPacket(pktBytes, pktBytes.length, inetAddress, port);
sock.send(pack);
return pktBytes.length;
}
/**
* Send the list of Packets.
*/
public int sendPackets(List<Packet> packets) throws IOException {
int totalBytes = 0;
for (int i = 0; i < packets.size(); i++) {
totalBytes += sendPacket(packets.get(i));
}
return totalBytes;
}
}
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.avaje.ebeaninternal.server.cluster.Packet;
/**
* Handles the sending of Packets via DatagramPacket.
*
* @author rbygrave
*/
public class McastSender {
private static final Logger logger = Logger.getLogger(McastSender.class.getName());
private final int port;
private final InetAddress inetAddress;
private final DatagramSocket sock;
private final InetSocketAddress sendAddr;
private final String senderHostPort;
public McastSender(int port, String address, int sendPort, String sendAddress) {
try {
this.port = port;
this.inetAddress = InetAddress.getByName(address);
InetAddress sendInetAddress = null;
if (sendAddress != null) {
sendInetAddress = InetAddress.getByName(sendAddress);
} else {
sendInetAddress = InetAddress.getLocalHost();
}
if (sendPort > 0) {
this.sock = new DatagramSocket(sendPort, sendInetAddress);
} else {
this.sock = new DatagramSocket(new InetSocketAddress(sendInetAddress, 0));
}
String msg = "Cluster Multicast Sender on["+sendInetAddress.getHostAddress()+":"+sock.getLocalPort()+"]";
logger.info(msg);
this.sendAddr = new InetSocketAddress(sendInetAddress, sock.getLocalPort());
this.senderHostPort = sendInetAddress.getHostAddress()+":"+sock.getLocalPort();
} catch (Exception e) {
String msg = "McastSender port:" + port + " sendPort:" + sendPort + " " + address;
throw new RuntimeException(msg, e);
}
}
/**
* Return the send Address so that if we have loopback messages we can
* detect if they where sent by this local sender and hence should be
* ignored.
*/
public InetSocketAddress getAddress() {
return sendAddr;
}
/**
* Return the Host and Port of the sender. This is used to uniquely identify
* this instance in the cluster.
*/
public String getSenderHostPort() {
return senderHostPort;
}
/**
* Send the packet.
*/
public int sendPacket(Packet packet) throws IOException {
byte[] pktBytes = packet.getBytes();
if (logger.isLoggable(Level.FINE)){
logger.fine("OUTGOING packet: " + packet.getPacketId() + " size:" + pktBytes.length);
}
if (pktBytes.length > 65507){
logger.warning("OUTGOING packet: " + packet.getPacketId() + " size:" + pktBytes.length
+" likely to be truncated using UDP with a MAXIMUM length of 65507");
}
DatagramPacket pack = new DatagramPacket(pktBytes, pktBytes.length, inetAddress, port);
sock.send(pack);
return pktBytes.length;
}
/**
* Send the list of Packets.
*/
public int sendPackets(List<Packet> packets) throws IOException {
int totalBytes = 0;
for (int i = 0; i < packets.size(); i++) {
totalBytes += sendPacket(packets.get(i));
}
return totalBytes;
}
}
@@ -1,155 +1,136 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.mcast;
/**
* Gives an overall status of this Cluster instance.
* <p>
* Ideally you want to see relatively low Re-send statistics.
* </p>
*
* @author rbygrave
*
*/
public class McastStatus {
private final long totalTxnEventsSent;
private final long totalTxnEventsReceived;
private final long totalPacketsSent;
private final long totalPacketsResent;
private final long totalPacketsReceived;
private final long totalBytesSent;
private final long totalBytesResent;
private final long totalBytesReceived;
private final int currentGroupSize;
private final int outgoingPacketsCacheSize;
private final long currentPacketId;
private final long minAckedPacketId;
private final String lastOutgoingAcks;
public String getSummary() {
StringBuilder sb = new StringBuilder(80);
sb.append("txnOut:").append(totalTxnEventsSent).append("; ");
sb.append("txnIn:").append(totalTxnEventsReceived).append("; ");
sb.append("outPackets:").append(totalPacketsSent).append("; ");
sb.append("outBytes:").append(totalBytesSent).append("; ");
sb.append("inPackets:").append(totalPacketsReceived).append("; ");
sb.append("inBytes:").append(totalBytesReceived).append("; ");
sb.append("resentPackets:").append(totalPacketsResent).append("; ");
sb.append("resentBytes:").append(totalBytesResent).append("; ");
sb.append("groupSize:").append(currentGroupSize).append("; ");
sb.append("cache:").append(outgoingPacketsCacheSize).append("; ");
sb.append("currentPacket:").append(currentPacketId).append("; ");
sb.append("minAckedPacket:").append(minAckedPacketId).append("; ");
sb.append("lastAck:").append(lastOutgoingAcks).append("; ");
return sb.toString();
}
public McastStatus(int currentGroupSize,
int outgoingPacketsCacheSize,
long currentPacketId,
long minAckedPacketId,
String lastOutgoingAcks,
long totalTransEventsSent,
long totalTransEventsReceived,
long totalPacketsSent,
long totalPacketsResent,
long totalPacketsReceived,
long totalBytesSent,
long totalBytesResent,
long totalBytesReceived) {
this.currentGroupSize = currentGroupSize;
this.outgoingPacketsCacheSize = outgoingPacketsCacheSize;
this.currentPacketId = currentPacketId;
this.minAckedPacketId = minAckedPacketId;
this.lastOutgoingAcks = lastOutgoingAcks;
this.totalTxnEventsSent = totalTransEventsSent;
this.totalTxnEventsReceived = totalTransEventsReceived;
this.totalPacketsSent = totalPacketsSent;
this.totalPacketsResent = totalPacketsResent;
this.totalPacketsReceived = totalPacketsReceived;
this.totalBytesSent = totalBytesSent;
this.totalBytesResent = totalBytesResent;
this.totalBytesReceived = totalBytesReceived;
}
public long getTotalTxnEventsReceived() {
return totalTxnEventsReceived;
}
public long getTotalPacketsReceived() {
return totalPacketsReceived;
}
public long getTotalBytesSent() {
return totalBytesSent;
}
public long getTotalBytesResent() {
return totalBytesResent;
}
public long getTotalBytesReceived() {
return totalBytesReceived;
}
public String getLastOutgoingAcks() {
return lastOutgoingAcks;
}
public int getOutgoingPacketsCacheSize() {
return outgoingPacketsCacheSize;
}
public long getCurrentPacketId() {
return currentPacketId;
}
public long getMinAckedPacketId() {
return minAckedPacketId;
}
public long getTotalTxnEventsSent() {
return totalTxnEventsSent;
}
public long getTotalPacketsSent() {
return totalPacketsSent;
}
public long getTotalPacketsResent() {
return totalPacketsResent;
}
public long getCurrentGroupSize() {
return currentGroupSize;
}
}
package com.avaje.ebeaninternal.server.cluster.mcast;
/**
* Gives an overall status of this Cluster instance.
* <p>
* Ideally you want to see relatively low Re-send statistics.
* </p>
*
* @author rbygrave
*
*/
public class McastStatus {
private final long totalTxnEventsSent;
private final long totalTxnEventsReceived;
private final long totalPacketsSent;
private final long totalPacketsResent;
private final long totalPacketsReceived;
private final long totalBytesSent;
private final long totalBytesResent;
private final long totalBytesReceived;
private final int currentGroupSize;
private final int outgoingPacketsCacheSize;
private final long currentPacketId;
private final long minAckedPacketId;
private final String lastOutgoingAcks;
public String getSummary() {
StringBuilder sb = new StringBuilder(80);
sb.append("txnOut:").append(totalTxnEventsSent).append("; ");
sb.append("txnIn:").append(totalTxnEventsReceived).append("; ");
sb.append("outPackets:").append(totalPacketsSent).append("; ");
sb.append("outBytes:").append(totalBytesSent).append("; ");
sb.append("inPackets:").append(totalPacketsReceived).append("; ");
sb.append("inBytes:").append(totalBytesReceived).append("; ");
sb.append("resentPackets:").append(totalPacketsResent).append("; ");
sb.append("resentBytes:").append(totalBytesResent).append("; ");
sb.append("groupSize:").append(currentGroupSize).append("; ");
sb.append("cache:").append(outgoingPacketsCacheSize).append("; ");
sb.append("currentPacket:").append(currentPacketId).append("; ");
sb.append("minAckedPacket:").append(minAckedPacketId).append("; ");
sb.append("lastAck:").append(lastOutgoingAcks).append("; ");
return sb.toString();
}
public McastStatus(int currentGroupSize,
int outgoingPacketsCacheSize,
long currentPacketId,
long minAckedPacketId,
String lastOutgoingAcks,
long totalTransEventsSent,
long totalTransEventsReceived,
long totalPacketsSent,
long totalPacketsResent,
long totalPacketsReceived,
long totalBytesSent,
long totalBytesResent,
long totalBytesReceived) {
this.currentGroupSize = currentGroupSize;
this.outgoingPacketsCacheSize = outgoingPacketsCacheSize;
this.currentPacketId = currentPacketId;
this.minAckedPacketId = minAckedPacketId;
this.lastOutgoingAcks = lastOutgoingAcks;
this.totalTxnEventsSent = totalTransEventsSent;
this.totalTxnEventsReceived = totalTransEventsReceived;
this.totalPacketsSent = totalPacketsSent;
this.totalPacketsResent = totalPacketsResent;
this.totalPacketsReceived = totalPacketsReceived;
this.totalBytesSent = totalBytesSent;
this.totalBytesResent = totalBytesResent;
this.totalBytesReceived = totalBytesReceived;
}
public long getTotalTxnEventsReceived() {
return totalTxnEventsReceived;
}
public long getTotalPacketsReceived() {
return totalPacketsReceived;
}
public long getTotalBytesSent() {
return totalBytesSent;
}
public long getTotalBytesResent() {
return totalBytesResent;
}
public long getTotalBytesReceived() {
return totalBytesReceived;
}
public String getLastOutgoingAcks() {
return lastOutgoingAcks;
}
public int getOutgoingPacketsCacheSize() {
return outgoingPacketsCacheSize;
}
public long getCurrentPacketId() {
return currentPacketId;
}
public long getMinAckedPacketId() {
return minAckedPacketId;
}
public long getTotalTxnEventsSent() {
return totalTxnEventsSent;
}
public long getTotalPacketsSent() {
return totalPacketsSent;
}
public long getTotalPacketsResent() {
return totalPacketsResent;
}
public long getCurrentGroupSize() {
return currentGroupSize;
}
}
@@ -1,33 +1,14 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.io.IOException;
import com.avaje.ebeaninternal.server.cluster.BinaryMessageList;
public interface Message {
public void writeBinaryMessage(BinaryMessageList msgList) throws IOException;
public boolean isControlMessage();
public String getToHostPort();
}
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.io.IOException;
import com.avaje.ebeaninternal.server.cluster.BinaryMessageList;
public interface Message {
public void writeBinaryMessage(BinaryMessageList msgList) throws IOException;
public boolean isControlMessage();
public String getToHostPort();
}
@@ -1,76 +1,57 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.io.DataInput;
import java.io.DataOutputStream;
import java.io.IOException;
import com.avaje.ebeaninternal.server.cluster.BinaryMessage;
import com.avaje.ebeaninternal.server.cluster.BinaryMessageList;
public class MessageAck implements Message {
private final String toHostPort;
private final long gotAllPacketId;
public MessageAck(String toHostPort, long gotAllPacketId) {
this.toHostPort = toHostPort;
this.gotAllPacketId = gotAllPacketId;
}
public String toString() {
return "Ack "+toHostPort+" "+gotAllPacketId;
}
public boolean isControlMessage() {
return false;
}
public String getToHostPort() {
return toHostPort;
}
public long getGotAllPacketId() {
return gotAllPacketId;
}
public static MessageAck readBinaryMessage(DataInput dataInput) throws IOException {
String hostPort = dataInput.readUTF();
long gotAllPacketId = dataInput.readLong();
return new MessageAck(hostPort, gotAllPacketId);
}
public void writeBinaryMessage(BinaryMessageList msgList) throws IOException {
BinaryMessage m = new BinaryMessage(toHostPort.length() * 2 + 20);
DataOutputStream os = m.getOs();
os.writeInt(BinaryMessage.TYPE_MSGACK);
os.writeUTF(toHostPort);
os.writeLong(gotAllPacketId);
os.flush();
msgList.add(m);
}
}
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.io.DataInput;
import java.io.DataOutputStream;
import java.io.IOException;
import com.avaje.ebeaninternal.server.cluster.BinaryMessage;
import com.avaje.ebeaninternal.server.cluster.BinaryMessageList;
public class MessageAck implements Message {
private final String toHostPort;
private final long gotAllPacketId;
public MessageAck(String toHostPort, long gotAllPacketId) {
this.toHostPort = toHostPort;
this.gotAllPacketId = gotAllPacketId;
}
public String toString() {
return "Ack "+toHostPort+" "+gotAllPacketId;
}
public boolean isControlMessage() {
return false;
}
public String getToHostPort() {
return toHostPort;
}
public long getGotAllPacketId() {
return gotAllPacketId;
}
public static MessageAck readBinaryMessage(DataInput dataInput) throws IOException {
String hostPort = dataInput.readUTF();
long gotAllPacketId = dataInput.readLong();
return new MessageAck(hostPort, gotAllPacketId);
}
public void writeBinaryMessage(BinaryMessageList msgList) throws IOException {
BinaryMessage m = new BinaryMessage(toHostPort.length() * 2 + 20);
DataOutputStream os = m.getOs();
os.writeInt(BinaryMessage.TYPE_MSGACK);
os.writeUTF(toHostPort);
os.writeLong(gotAllPacketId);
os.flush();
msgList.add(m);
}
}
@@ -1,92 +1,73 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.io.DataInput;
import java.io.DataOutputStream;
import java.io.IOException;
import com.avaje.ebeaninternal.server.cluster.BinaryMessage;
import com.avaje.ebeaninternal.server.cluster.BinaryMessageList;
public class MessageControl implements Message {
public static final short TYPE_JOIN = 1;
public static final short TYPE_LEAVE = 2;
public static final short TYPE_PING = 3;
public static final short TYPE_JOINRESPONSE = 7;
public static final short TYPE_PINGRESPONSE = 8;
private final short controlType;
private final String fromHostPort;
public static MessageControl readBinaryMessage(DataInput dataInput) throws IOException {
short controlType = dataInput.readShort();
String hostPort = dataInput.readUTF();
return new MessageControl(controlType, hostPort);
}
public MessageControl(short controlType, String helloFromHostPort) {
this.controlType = controlType;
this.fromHostPort = helloFromHostPort;
}
public String toString() {
switch (controlType) {
case TYPE_JOIN: return "Join "+fromHostPort;
case TYPE_LEAVE: return "Leave "+fromHostPort;
case TYPE_PING: return "Ping "+fromHostPort;
case TYPE_PINGRESPONSE: return "PingResponse "+fromHostPort;
default:
throw new RuntimeException("Invalid controlType "+controlType);
}
}
public boolean isControlMessage() {
return true;
}
public short getControlType() {
return controlType;
}
public String getToHostPort() {
return "*";
}
public String getFromHostPort() {
return fromHostPort;
}
public void writeBinaryMessage(BinaryMessageList msgList) throws IOException {
BinaryMessage m = new BinaryMessage(fromHostPort.length() * 2 + 10);
DataOutputStream os = m.getOs();
os.writeInt(BinaryMessage.TYPE_MSGCONTROL);
os.writeShort(controlType);
os.writeUTF(fromHostPort);
os.flush();
msgList.add(m);
}
}
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.io.DataInput;
import java.io.DataOutputStream;
import java.io.IOException;
import com.avaje.ebeaninternal.server.cluster.BinaryMessage;
import com.avaje.ebeaninternal.server.cluster.BinaryMessageList;
public class MessageControl implements Message {
public static final short TYPE_JOIN = 1;
public static final short TYPE_LEAVE = 2;
public static final short TYPE_PING = 3;
public static final short TYPE_JOINRESPONSE = 7;
public static final short TYPE_PINGRESPONSE = 8;
private final short controlType;
private final String fromHostPort;
public static MessageControl readBinaryMessage(DataInput dataInput) throws IOException {
short controlType = dataInput.readShort();
String hostPort = dataInput.readUTF();
return new MessageControl(controlType, hostPort);
}
public MessageControl(short controlType, String helloFromHostPort) {
this.controlType = controlType;
this.fromHostPort = helloFromHostPort;
}
public String toString() {
switch (controlType) {
case TYPE_JOIN: return "Join "+fromHostPort;
case TYPE_LEAVE: return "Leave "+fromHostPort;
case TYPE_PING: return "Ping "+fromHostPort;
case TYPE_PINGRESPONSE: return "PingResponse "+fromHostPort;
default:
throw new RuntimeException("Invalid controlType "+controlType);
}
}
public boolean isControlMessage() {
return true;
}
public short getControlType() {
return controlType;
}
public String getToHostPort() {
return "*";
}
public String getFromHostPort() {
return fromHostPort;
}
public void writeBinaryMessage(BinaryMessageList msgList) throws IOException {
BinaryMessage m = new BinaryMessage(fromHostPort.length() * 2 + 10);
DataOutputStream os = m.getOs();
os.writeInt(BinaryMessage.TYPE_MSGCONTROL);
os.writeShort(controlType);
os.writeUTF(fromHostPort);
os.flush();
msgList.add(m);
}
}
@@ -1,96 +1,77 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.io.DataInput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebeaninternal.server.cluster.BinaryMessage;
import com.avaje.ebeaninternal.server.cluster.BinaryMessageList;
public class MessageResend implements Message {
private final String toHostPort;
private final List<Long> resendPacketIds;
public MessageResend(String toHostPort, List<Long> resendPacketIds) {
this.toHostPort = toHostPort;
this.resendPacketIds = resendPacketIds;
}
public MessageResend(String toHostPort) {
this(toHostPort, new ArrayList<Long>(4));
}
public String toString() {
return "Resend "+toHostPort+" "+resendPacketIds;
}
public boolean isControlMessage() {
return false;
}
public String getToHostPort() {
return toHostPort;
}
public void add(long packetId){
resendPacketIds.add(Long.valueOf(packetId));
}
public List<Long> getResendPacketIds() {
return resendPacketIds;
}
public static MessageResend readBinaryMessage(DataInput dataInput) throws IOException {
String hostPort = dataInput.readUTF();
MessageResend msg = new MessageResend(hostPort);
int numberOfPacketIds = dataInput.readInt();
for (int i = 0; i < numberOfPacketIds; i++) {
long packetId = dataInput.readLong();
msg.add(packetId);
}
return msg;
}
public void writeBinaryMessage(BinaryMessageList msgList) throws IOException {
BinaryMessage m = new BinaryMessage(toHostPort.length() * 2 + 20);
DataOutputStream os = m.getOs();
os.writeInt(BinaryMessage.TYPE_MSGRESEND);
os.writeUTF(toHostPort);
os.writeInt(resendPacketIds.size());
for (int i = 0; i < resendPacketIds.size(); i++) {
Long packetId = resendPacketIds.get(i);
os.writeLong(packetId.longValue());
}
os.flush();
msgList.add(m);
}
}
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.io.DataInput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.avaje.ebeaninternal.server.cluster.BinaryMessage;
import com.avaje.ebeaninternal.server.cluster.BinaryMessageList;
public class MessageResend implements Message {
private final String toHostPort;
private final List<Long> resendPacketIds;
public MessageResend(String toHostPort, List<Long> resendPacketIds) {
this.toHostPort = toHostPort;
this.resendPacketIds = resendPacketIds;
}
public MessageResend(String toHostPort) {
this(toHostPort, new ArrayList<Long>(4));
}
public String toString() {
return "Resend "+toHostPort+" "+resendPacketIds;
}
public boolean isControlMessage() {
return false;
}
public String getToHostPort() {
return toHostPort;
}
public void add(long packetId){
resendPacketIds.add(Long.valueOf(packetId));
}
public List<Long> getResendPacketIds() {
return resendPacketIds;
}
public static MessageResend readBinaryMessage(DataInput dataInput) throws IOException {
String hostPort = dataInput.readUTF();
MessageResend msg = new MessageResend(hostPort);
int numberOfPacketIds = dataInput.readInt();
for (int i = 0; i < numberOfPacketIds; i++) {
long packetId = dataInput.readLong();
msg.add(packetId);
}
return msg;
}
public void writeBinaryMessage(BinaryMessageList msgList) throws IOException {
BinaryMessage m = new BinaryMessage(toHostPort.length() * 2 + 20);
DataOutputStream os = m.getOs();
os.writeInt(BinaryMessage.TYPE_MSGRESEND);
os.writeUTF(toHostPort);
os.writeInt(resendPacketIds.size());
for (int i = 0; i < resendPacketIds.size(); i++) {
Long packetId = resendPacketIds.get(i);
os.writeLong(packetId.longValue());
}
os.flush();
msgList.add(m);
}
}
@@ -1,125 +1,106 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.util.HashMap;
import java.util.Map;
public class OutgoingPacketsAcked {
private long minimumGotAllPacketId;
private Map<String, GroupMemberAck> recievedByMap = new HashMap<String, GroupMemberAck>();
public int getGroupSize() {
synchronized (this) {
return recievedByMap.size();
}
}
public long getMinimumGotAllPacketId() {
synchronized (this) {
return minimumGotAllPacketId;
}
}
public void removeMember(String groupMember){
synchronized (this) {
recievedByMap.remove(groupMember);
resetGotAllMin();
}
}
private boolean resetGotAllMin() {
long tempMin;
if (recievedByMap.isEmpty()){
//System.out.println(" -- -- -- -- "+recievedByMap.isEmpty());
tempMin = Long.MAX_VALUE;
} else {
tempMin = Long.MAX_VALUE;
}
for (GroupMemberAck groupMemAck : recievedByMap.values()) {
long memberMin = groupMemAck.getGotAllPacketId();
if (memberMin < tempMin){
//System.out.println(" -- new tmpMin "+memberMin);
tempMin = memberMin;
}
}
if (tempMin != minimumGotAllPacketId) {
minimumGotAllPacketId = tempMin;
return true;
} else {
return false;
}
}
public long receivedAck(String groupMember, MessageAck ack) {
synchronized (this) {
boolean checkMin = false;
GroupMemberAck groupMemberAck = recievedByMap.get(groupMember);
if (groupMemberAck == null) {
//System.out.println(" -- new groupMemberAck");
groupMemberAck = new GroupMemberAck();
groupMemberAck.setIfBigger(ack.getGotAllPacketId());
recievedByMap.put(groupMember, groupMemberAck);
checkMin = true;
} else {
checkMin = groupMemberAck.getGotAllPacketId() == minimumGotAllPacketId;
//System.out.println(" -- existing groupMemberAck, checkMin:"+checkMin+" "+groupMemberAck.getGotAllPacketId());
groupMemberAck.setIfBigger(ack.getGotAllPacketId());
}
boolean minChanged = false;
//System.out.println(" -- checkMin:"+checkMin+" minimumGotAllPacketId:"+minimumGotAllPacketId);
if (checkMin || minimumGotAllPacketId == 0){
minChanged = resetGotAllMin();
//System.out.println(" -- minChanged:"+minChanged+" minimumGotAllPacketId:"+minimumGotAllPacketId);
}
return minChanged ? minimumGotAllPacketId : 0;
}
}
private static class GroupMemberAck {
private long gotAllPacketId;
private GroupMemberAck() {
}
private long getGotAllPacketId() {
return gotAllPacketId;
}
private void setIfBigger(long newGotAll) {
if (newGotAll > gotAllPacketId) {
gotAllPacketId = newGotAll;
}
}
}
}
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.util.HashMap;
import java.util.Map;
public class OutgoingPacketsAcked {
private long minimumGotAllPacketId;
private Map<String, GroupMemberAck> recievedByMap = new HashMap<String, GroupMemberAck>();
public int getGroupSize() {
synchronized (this) {
return recievedByMap.size();
}
}
public long getMinimumGotAllPacketId() {
synchronized (this) {
return minimumGotAllPacketId;
}
}
public void removeMember(String groupMember){
synchronized (this) {
recievedByMap.remove(groupMember);
resetGotAllMin();
}
}
private boolean resetGotAllMin() {
long tempMin;
if (recievedByMap.isEmpty()){
//System.out.println(" -- -- -- -- "+recievedByMap.isEmpty());
tempMin = Long.MAX_VALUE;
} else {
tempMin = Long.MAX_VALUE;
}
for (GroupMemberAck groupMemAck : recievedByMap.values()) {
long memberMin = groupMemAck.getGotAllPacketId();
if (memberMin < tempMin){
//System.out.println(" -- new tmpMin "+memberMin);
tempMin = memberMin;
}
}
if (tempMin != minimumGotAllPacketId) {
minimumGotAllPacketId = tempMin;
return true;
} else {
return false;
}
}
public long receivedAck(String groupMember, MessageAck ack) {
synchronized (this) {
boolean checkMin = false;
GroupMemberAck groupMemberAck = recievedByMap.get(groupMember);
if (groupMemberAck == null) {
//System.out.println(" -- new groupMemberAck");
groupMemberAck = new GroupMemberAck();
groupMemberAck.setIfBigger(ack.getGotAllPacketId());
recievedByMap.put(groupMember, groupMemberAck);
checkMin = true;
} else {
checkMin = groupMemberAck.getGotAllPacketId() == minimumGotAllPacketId;
//System.out.println(" -- existing groupMemberAck, checkMin:"+checkMin+" "+groupMemberAck.getGotAllPacketId());
groupMemberAck.setIfBigger(ack.getGotAllPacketId());
}
boolean minChanged = false;
//System.out.println(" -- checkMin:"+checkMin+" minimumGotAllPacketId:"+minimumGotAllPacketId);
if (checkMin || minimumGotAllPacketId == 0){
minChanged = resetGotAllMin();
//System.out.println(" -- minChanged:"+minChanged+" minimumGotAllPacketId:"+minimumGotAllPacketId);
}
return minChanged ? minimumGotAllPacketId : 0;
}
}
private static class GroupMemberAck {
private long gotAllPacketId;
private GroupMemberAck() {
}
private long getGotAllPacketId() {
return gotAllPacketId;
}
private void setIfBigger(long newGotAll) {
if (newGotAll > gotAllPacketId) {
gotAllPacketId = newGotAll;
}
}
}
}
@@ -1,85 +1,66 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.avaje.ebeaninternal.server.cluster.Packet;
/**
* Cache of the outgoing packets.
* <p>
* These are held until we receive ACKs from the other members of the cluster to
* say they have received the packets.
* </p>
*
* @author rbygrave
*
*/
public class OutgoingPacketsCache {
private final Map<Long, Packet> packetMap = new TreeMap<Long, Packet>();
public int size() {
return packetMap.size();
}
public Packet getPacket(Long packetId) {
return packetMap.get(packetId);
}
public String toString() {
return packetMap.keySet().toString();
}
/**
* Remove the packet when we give up trying to send it out.
*/
public void remove(Packet packet) {
packetMap.remove(packet.getPacketId());
}
public void registerPackets(List<Packet> packets) {
for (int i = 0; i < packets.size(); i++) {
Packet p = packets.get(i);
packetMap.put(p.getPacketId(), p);
}
}
public int trimAll() {
int size = packetMap.size();
packetMap.clear();
return size;
}
public void trimAcknowledgedMessages(long minAcked) {
Iterator<Long> it = packetMap.keySet().iterator();
while (it.hasNext()) {
Long pktId = it.next();
if (minAcked >= pktId.longValue()) {
it.remove();
}
}
}
}
package com.avaje.ebeaninternal.server.cluster.mcast;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.avaje.ebeaninternal.server.cluster.Packet;
/**
* Cache of the outgoing packets.
* <p>
* These are held until we receive ACKs from the other members of the cluster to
* say they have received the packets.
* </p>
*
* @author rbygrave
*
*/
public class OutgoingPacketsCache {
private final Map<Long, Packet> packetMap = new TreeMap<Long, Packet>();
public int size() {
return packetMap.size();
}
public Packet getPacket(Long packetId) {
return packetMap.get(packetId);
}
public String toString() {
return packetMap.keySet().toString();
}
/**
* Remove the packet when we give up trying to send it out.
*/
public void remove(Packet packet) {
packetMap.remove(packet.getPacketId());
}
public void registerPackets(List<Packet> packets) {
for (int i = 0; i < packets.size(); i++) {
Packet p = packets.get(i);
packetMap.put(p.getPacketId(), p);
}
}
public int trimAll() {
int size = packetMap.size();
packetMap.clear();
return size;
}
public void trimAcknowledgedMessages(long minAcked) {
Iterator<Long> it = packetMap.keySet().iterator();
while (it.hasNext()) {
Long pktId = it.next();
if (minAcked >= pktId.longValue()) {
it.remove();
}
}
}
}
@@ -1,76 +1,59 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.socket;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This parses and dispatches a request to the appropriate handler.
* <p>
* Looks up the appropriate RequestHandler
* and then gets it to process the Client request.<P>
* </p>
* Note that this is a Runnable because it is assigned to the ThreadPool.
*/
class RequestProcessor implements Runnable {
private static final Logger logger = Logger.getLogger(RequestProcessor.class.getName());
private final Socket clientSocket;
private final SocketClusterBroadcast owner;
/**
* Create including the Listener (used to lookup the Request Handler) and
* the socket itself.
*/
public RequestProcessor(SocketClusterBroadcast owner, Socket clientSocket) {
this.clientSocket = clientSocket;
this.owner = owner;
}
/**
* This will parse out the command. Lookup the appropriate Handler and
* pass the information to the handler for processing.
* <P>Dev Note: the command parsing is processed here so that it is preformed
* by the assigned thread rather than the listeners thread.</P>
*/
public void run() {
try {
SocketConnection sc = new SocketConnection(clientSocket);
while(true){
if (owner.process(sc)) {
// got the offline message or timeout
break;
}
}
sc.disconnect();
} catch (IOException e) {
logger.log(Level.SEVERE, null, e);
} catch (ClassNotFoundException e) {
logger.log(Level.SEVERE, null, e);
}
}
};
package com.avaje.ebeaninternal.server.cluster.socket;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This parses and dispatches a request to the appropriate handler.
* <p>
* Looks up the appropriate RequestHandler
* and then gets it to process the Client request.<P>
* </p>
* Note that this is a Runnable because it is assigned to the ThreadPool.
*/
class RequestProcessor implements Runnable {
private static final Logger logger = Logger.getLogger(RequestProcessor.class.getName());
private final Socket clientSocket;
private final SocketClusterBroadcast owner;
/**
* Create including the Listener (used to lookup the Request Handler) and
* the socket itself.
*/
public RequestProcessor(SocketClusterBroadcast owner, Socket clientSocket) {
this.clientSocket = clientSocket;
this.owner = owner;
}
/**
* This will parse out the command. Lookup the appropriate Handler and
* pass the information to the handler for processing.
* <P>Dev Note: the command parsing is processed here so that it is preformed
* by the assigned thread rather than the listeners thread.</P>
*/
public void run() {
try {
SocketConnection sc = new SocketConnection(clientSocket);
while(true){
if (owner.process(sc)) {
// got the offline message or timeout
break;
}
}
sc.disconnect();
} catch (IOException e) {
logger.log(Level.SEVERE, null, e);
} catch (ClassNotFoundException e) {
logger.log(Level.SEVERE, null, e);
}
}
};
@@ -1,151 +1,134 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.socket;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* The client side of the socket clustering.
*/
class SocketClient {
private static final Logger logger = Logger.getLogger(SocketClient.class.getName());
private final InetSocketAddress address;
private final String hostPort;
private boolean online;
private Socket socket;
private OutputStream os;
private ObjectOutputStream oos;
/**
* Construct with an IP address and port.
*/
public SocketClient(InetSocketAddress address) {
this.address = address;
this.hostPort = address.getHostName()+":"+address.getPort();
}
public String getHostPort() {
return hostPort;
}
public int getPort() {
return address.getPort();
}
public boolean isOnline() {
return online;
}
public void setOnline(boolean online) throws IOException {
if (online){
setOnline();
} else {
disconnect();
}
}
/**
* Set whether the client is thought to be online.
*/
private void setOnline() throws IOException {
connect();
this.online = true;
}
public void reconnect() throws IOException {
disconnect();
connect();
}
private void connect() throws IOException {
if (socket != null){
throw new IllegalStateException("Already got a socket connection?");
}
Socket s = new Socket();
s.setKeepAlive(true);
s.connect(address);
this.socket = s;
this.os = socket.getOutputStream();
}
public void disconnect() {
this.online = false;
if (socket != null){
try {
socket.close();
} catch (IOException e) {
String msg = "Error disconnecting from Cluster member "+hostPort;
logger.log(Level.INFO, msg, e);
}
os = null;
oos = null;
socket = null;
}
}
public boolean register(SocketClusterMessage registerMsg) {
try {
setOnline();
send(registerMsg);
return true;
} catch (IOException e) {
disconnect();
return false;
}
}
public boolean send(SocketClusterMessage msg) throws IOException {
if (online){
writeObject(msg);
return true;
} else {
return false;
}
}
private void writeObject(Object object) throws IOException {
if (oos == null){
this.oos = new ObjectOutputStream(os);
}
oos.writeObject(object);
oos.flush();
}
}
package com.avaje.ebeaninternal.server.cluster.socket;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* The client side of the socket clustering.
*/
class SocketClient {
private static final Logger logger = Logger.getLogger(SocketClient.class.getName());
private final InetSocketAddress address;
private final String hostPort;
private boolean online;
private Socket socket;
private OutputStream os;
private ObjectOutputStream oos;
/**
* Construct with an IP address and port.
*/
public SocketClient(InetSocketAddress address) {
this.address = address;
this.hostPort = address.getHostName()+":"+address.getPort();
}
public String getHostPort() {
return hostPort;
}
public int getPort() {
return address.getPort();
}
public boolean isOnline() {
return online;
}
public void setOnline(boolean online) throws IOException {
if (online){
setOnline();
} else {
disconnect();
}
}
/**
* Set whether the client is thought to be online.
*/
private void setOnline() throws IOException {
connect();
this.online = true;
}
public void reconnect() throws IOException {
disconnect();
connect();
}
private void connect() throws IOException {
if (socket != null){
throw new IllegalStateException("Already got a socket connection?");
}
Socket s = new Socket();
s.setKeepAlive(true);
s.connect(address);
this.socket = s;
this.os = socket.getOutputStream();
}
public void disconnect() {
this.online = false;
if (socket != null){
try {
socket.close();
} catch (IOException e) {
String msg = "Error disconnecting from Cluster member "+hostPort;
logger.log(Level.INFO, msg, e);
}
os = null;
oos = null;
socket = null;
}
}
public boolean register(SocketClusterMessage registerMsg) {
try {
setOnline();
send(registerMsg);
return true;
} catch (IOException e) {
disconnect();
return false;
}
}
public boolean send(SocketClusterMessage msg) throws IOException {
if (online){
writeObject(msg);
return true;
} else {
return false;
}
}
private void writeObject(Object object) throws IOException {
if (oos == null){
this.oos = new ObjectOutputStream(os);
}
oos.writeObject(object);
oos.flush();
}
}
@@ -1,265 +1,248 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.socket;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.PersistenceException;
import com.avaje.ebean.config.GlobalProperties;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.server.cluster.ClusterBroadcast;
import com.avaje.ebeaninternal.server.cluster.ClusterManager;
import com.avaje.ebeaninternal.server.cluster.DataHolder;
import com.avaje.ebeaninternal.server.cluster.SerialiseTransactionHelper;
import com.avaje.ebeaninternal.server.lib.util.StringHelper;
import com.avaje.ebeaninternal.server.transaction.RemoteTransactionEvent;
/**
* Broadcast messages across the cluster using sockets.
*/
public class SocketClusterBroadcast implements ClusterBroadcast {
private static final Logger logger = Logger.getLogger(SocketClusterBroadcast.class.getName());
private final SocketClient local;
private final HashMap<String,SocketClient> clientMap;
private final SocketClusterListener listener;
private SocketClient[] members;
private ClusterManager clusterManager;
private final TxnSerialiseHelper txnSerialiseHelper = new TxnSerialiseHelper();
private final AtomicInteger txnOutgoing = new AtomicInteger();
private final AtomicInteger txnIncoming = new AtomicInteger();
public SocketClusterBroadcast( ){
String localHostPort = GlobalProperties.get("ebean.cluster.local", null);
String members = GlobalProperties.get("ebean.cluster.members", null);
logger.info("Clustering using Sockets local["+localHostPort+"] members["+members+"]");
this.local = new SocketClient(parseFullName(localHostPort));
this.clientMap = new HashMap<String, SocketClient>();
String[] memArray = StringHelper.delimitedToArray(members, ",", false);
for (int i = 0; i < memArray.length; i++) {
InetSocketAddress member = parseFullName(memArray[i]);
SocketClient client = new SocketClient(member);
if (!local.getHostPort().equalsIgnoreCase(client.getHostPort())) {
// don't add the local one ...
clientMap.put(client.getHostPort(), client);
}
}
this.members = clientMap.values().toArray(new SocketClient[clientMap.size()]);
this.listener = new SocketClusterListener(this, local.getPort());
}
/**
* Return the current status of this instance.
*/
public SocketClusterStatus getStatus() {
// count of online members
int currentGroupSize = 0;
for (int i = 0; i < members.length; i++) {
if (members[i].isOnline()) {
++currentGroupSize;
}
}
int txnIn = txnIncoming.get();
int txnOut = txnOutgoing.get();
return new SocketClusterStatus(currentGroupSize, txnIn, txnOut);
}
public void startup(ClusterManager clusterManager) {
this.clusterManager = clusterManager;
try {
listener.startListening();
register();
} catch (IOException e) {
throw new PersistenceException(e);
}
}
public void shutdown() {
deregister();
listener.shutdown();
}
/**
* Register with all the other members of the Cluster.
*/
private void register() {
SocketClusterMessage h = SocketClusterMessage.register(local.getHostPort(), true);
for (int i = 0; i < members.length; i++) {
boolean online = members[i].register(h);
String msg = "Cluster Member ["+members[i].getHostPort()+"] online["+online+"]";
logger.info(msg);
}
}
protected void setMemberOnline(String fullName, boolean online) throws IOException {
synchronized (clientMap) {
String msg = "Cluster Member ["+fullName+"] online["+online+"]";
logger.info(msg);
SocketClient member = clientMap.get(fullName);
member.setOnline(online);
}
}
private void send(SocketClient client, SocketClusterMessage msg) {
try {
// alternative would be to connect/disconnect here
// but prefer to use keepalive
client.send(msg);
} catch (Exception ex){
logger.log(Level.SEVERE, "Error sending message", ex);
try {
client.reconnect();
} catch (IOException e) {
logger.log(Level.SEVERE, "Error trying to reconnect", ex);
}
}
}
/**
* Send the payload to all the members of the cluster.
*/
public void broadcast(RemoteTransactionEvent remoteTransEvent) {
try {
txnOutgoing.incrementAndGet();
DataHolder dataHolder = txnSerialiseHelper.createDataHolder(remoteTransEvent);
SocketClusterMessage msg = SocketClusterMessage.transEvent(dataHolder);
broadcast(msg);
} catch (Exception e){
String msg = "Error sending RemoteTransactionEvent "+remoteTransEvent+" to cluster members.";
logger.log(Level.SEVERE, msg, e);
}
}
protected void broadcast(SocketClusterMessage msg) {
for (int i = 0; i < members.length; i++) {
send(members[i], msg);
}
}
/**
* Leave the cluster.
*/
private void deregister() {
SocketClusterMessage h = SocketClusterMessage.register(local.getHostPort(), false);
broadcast(h);
for (int i = 0; i < members.length; i++) {
members[i].disconnect();
}
}
/**
* Process a Cluster message.
*/
protected boolean process(SocketConnection request) throws IOException, ClassNotFoundException {
try {
SocketClusterMessage h = (SocketClusterMessage)request.readObject();
if (h.isRegisterEvent()){
setMemberOnline(h.getRegisterHost(), h.isRegister());
} else {
txnIncoming.incrementAndGet();
DataHolder dataHolder = h.getDataHolder();
RemoteTransactionEvent transEvent = txnSerialiseHelper.read(dataHolder);
transEvent.run();
}
if (h.isRegisterEvent() && !h.isRegister()){
// instance shutting down
return true;
} else {
return false;
}
} catch (InterruptedIOException e) {
String msg = "Timeout waiting for message";
logger.log(Level.INFO, msg, e);
try {
request.disconnect();
} catch (IOException ex){
logger.log(Level.INFO, "Error disconnecting after timeout", ex);
}
return true;
}
}
/**
* Parse a host:port into a InetSocketAddress.
*/
private InetSocketAddress parseFullName(String hostAndPort) {
try {
hostAndPort = hostAndPort.trim();
int colonPos = hostAndPort.indexOf(":");
if (colonPos == -1) {
String msg = "No colon \":\" in "+hostAndPort;
throw new IllegalArgumentException(msg);
}
String host = hostAndPort.substring(0, colonPos);
String sPort = hostAndPort.substring(colonPos + 1, hostAndPort.length());
int port = Integer.parseInt(sPort);
return new InetSocketAddress(host, port);
} catch (Exception ex){
throw new RuntimeException("Error parsing ["+hostAndPort+"] for the form [host:port]", ex);
}
}
class TxnSerialiseHelper extends SerialiseTransactionHelper {
@Override
public SpiEbeanServer getEbeanServer(String serverName) {
return (SpiEbeanServer)clusterManager.getServer(serverName);
}
}
}
package com.avaje.ebeaninternal.server.cluster.socket;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.PersistenceException;
import com.avaje.ebean.config.GlobalProperties;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.server.cluster.ClusterBroadcast;
import com.avaje.ebeaninternal.server.cluster.ClusterManager;
import com.avaje.ebeaninternal.server.cluster.DataHolder;
import com.avaje.ebeaninternal.server.cluster.SerialiseTransactionHelper;
import com.avaje.ebeaninternal.server.lib.util.StringHelper;
import com.avaje.ebeaninternal.server.transaction.RemoteTransactionEvent;
/**
* Broadcast messages across the cluster using sockets.
*/
public class SocketClusterBroadcast implements ClusterBroadcast {
private static final Logger logger = Logger.getLogger(SocketClusterBroadcast.class.getName());
private final SocketClient local;
private final HashMap<String,SocketClient> clientMap;
private final SocketClusterListener listener;
private SocketClient[] members;
private ClusterManager clusterManager;
private final TxnSerialiseHelper txnSerialiseHelper = new TxnSerialiseHelper();
private final AtomicInteger txnOutgoing = new AtomicInteger();
private final AtomicInteger txnIncoming = new AtomicInteger();
public SocketClusterBroadcast( ){
String localHostPort = GlobalProperties.get("ebean.cluster.local", null);
String members = GlobalProperties.get("ebean.cluster.members", null);
logger.info("Clustering using Sockets local["+localHostPort+"] members["+members+"]");
this.local = new SocketClient(parseFullName(localHostPort));
this.clientMap = new HashMap<String, SocketClient>();
String[] memArray = StringHelper.delimitedToArray(members, ",", false);
for (int i = 0; i < memArray.length; i++) {
InetSocketAddress member = parseFullName(memArray[i]);
SocketClient client = new SocketClient(member);
if (!local.getHostPort().equalsIgnoreCase(client.getHostPort())) {
// don't add the local one ...
clientMap.put(client.getHostPort(), client);
}
}
this.members = clientMap.values().toArray(new SocketClient[clientMap.size()]);
this.listener = new SocketClusterListener(this, local.getPort());
}
/**
* Return the current status of this instance.
*/
public SocketClusterStatus getStatus() {
// count of online members
int currentGroupSize = 0;
for (int i = 0; i < members.length; i++) {
if (members[i].isOnline()) {
++currentGroupSize;
}
}
int txnIn = txnIncoming.get();
int txnOut = txnOutgoing.get();
return new SocketClusterStatus(currentGroupSize, txnIn, txnOut);
}
public void startup(ClusterManager clusterManager) {
this.clusterManager = clusterManager;
try {
listener.startListening();
register();
} catch (IOException e) {
throw new PersistenceException(e);
}
}
public void shutdown() {
deregister();
listener.shutdown();
}
/**
* Register with all the other members of the Cluster.
*/
private void register() {
SocketClusterMessage h = SocketClusterMessage.register(local.getHostPort(), true);
for (int i = 0; i < members.length; i++) {
boolean online = members[i].register(h);
String msg = "Cluster Member ["+members[i].getHostPort()+"] online["+online+"]";
logger.info(msg);
}
}
protected void setMemberOnline(String fullName, boolean online) throws IOException {
synchronized (clientMap) {
String msg = "Cluster Member ["+fullName+"] online["+online+"]";
logger.info(msg);
SocketClient member = clientMap.get(fullName);
member.setOnline(online);
}
}
private void send(SocketClient client, SocketClusterMessage msg) {
try {
// alternative would be to connect/disconnect here
// but prefer to use keepalive
client.send(msg);
} catch (Exception ex){
logger.log(Level.SEVERE, "Error sending message", ex);
try {
client.reconnect();
} catch (IOException e) {
logger.log(Level.SEVERE, "Error trying to reconnect", ex);
}
}
}
/**
* Send the payload to all the members of the cluster.
*/
public void broadcast(RemoteTransactionEvent remoteTransEvent) {
try {
txnOutgoing.incrementAndGet();
DataHolder dataHolder = txnSerialiseHelper.createDataHolder(remoteTransEvent);
SocketClusterMessage msg = SocketClusterMessage.transEvent(dataHolder);
broadcast(msg);
} catch (Exception e){
String msg = "Error sending RemoteTransactionEvent "+remoteTransEvent+" to cluster members.";
logger.log(Level.SEVERE, msg, e);
}
}
protected void broadcast(SocketClusterMessage msg) {
for (int i = 0; i < members.length; i++) {
send(members[i], msg);
}
}
/**
* Leave the cluster.
*/
private void deregister() {
SocketClusterMessage h = SocketClusterMessage.register(local.getHostPort(), false);
broadcast(h);
for (int i = 0; i < members.length; i++) {
members[i].disconnect();
}
}
/**
* Process a Cluster message.
*/
protected boolean process(SocketConnection request) throws IOException, ClassNotFoundException {
try {
SocketClusterMessage h = (SocketClusterMessage)request.readObject();
if (h.isRegisterEvent()){
setMemberOnline(h.getRegisterHost(), h.isRegister());
} else {
txnIncoming.incrementAndGet();
DataHolder dataHolder = h.getDataHolder();
RemoteTransactionEvent transEvent = txnSerialiseHelper.read(dataHolder);
transEvent.run();
}
if (h.isRegisterEvent() && !h.isRegister()){
// instance shutting down
return true;
} else {
return false;
}
} catch (InterruptedIOException e) {
String msg = "Timeout waiting for message";
logger.log(Level.INFO, msg, e);
try {
request.disconnect();
} catch (IOException ex){
logger.log(Level.INFO, "Error disconnecting after timeout", ex);
}
return true;
}
}
/**
* Parse a host:port into a InetSocketAddress.
*/
private InetSocketAddress parseFullName(String hostAndPort) {
try {
hostAndPort = hostAndPort.trim();
int colonPos = hostAndPort.indexOf(":");
if (colonPos == -1) {
String msg = "No colon \":\" in "+hostAndPort;
throw new IllegalArgumentException(msg);
}
String host = hostAndPort.substring(0, colonPos);
String sPort = hostAndPort.substring(colonPos + 1, hostAndPort.length());
int port = Integer.parseInt(sPort);
return new InetSocketAddress(host, port);
} catch (Exception ex){
throw new RuntimeException("Error parsing ["+hostAndPort+"] for the form [host:port]", ex);
}
}
class TxnSerialiseHelper extends SerialiseTransactionHelper {
@Override
public SpiEbeanServer getEbeanServer(String serverName) {
return (SpiEbeanServer)clusterManager.getServer(serverName);
}
}
}
@@ -1,181 +1,164 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.socket;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.avaje.ebeaninternal.server.lib.thread.ThreadPool;
import com.avaje.ebeaninternal.server.lib.thread.ThreadPoolManager;
/**
* Serverside multithreaded socket listener. Accepts connections and dispatches
* them to an appropriate handler.
* <p>
* This is designed as a single port listener, where part of the connection
* protocol determines which service the client is requesting (rather than a
* port per service).
* </p>
* <p>
* It has its own daemon background thread that handles the accept() loop on the
* ServerSocket.
* </p>
*/
class SocketClusterListener implements Runnable {
private static final Logger logger = Logger.getLogger(SocketClusterListener.class.getName());
/**
* The port the SocketListener uses.
*/
private final int port;
/**
* The length of the socket accept timeout.
*/
private final int listenTimeout = 60000;
/**
* The server socket used to listen for requests.
*/
private final ServerSocket serverListenSocket;
/**
* The listening thread.
*/
private final Thread listenerThread;
/**
* The pool of threads that actually do the parsing execution of requests.
*/
private final ThreadPool threadPool;
private final SocketClusterBroadcast owner;
/**
* shutting down flag.
*/
boolean doingShutdown;
/**
* Whether the listening thread is busy assigning a request to a thread.
*/
boolean isActive;
/**
* Construct with a given thread pool name.
*/
public SocketClusterListener(SocketClusterBroadcast owner, int port) {
this.owner = owner;
this.threadPool = ThreadPoolManager.getThreadPool("EbeanClusterMember");
this.port = port;
try {
this.serverListenSocket = new ServerSocket(port);
this.serverListenSocket.setSoTimeout(listenTimeout);
this.listenerThread = new Thread(this, "EbeanClusterListener");
} catch (IOException e){
String msg = "Error starting cluster socket listener on port "+port;
throw new RuntimeException(msg,e);
}
}
/**
* Returns the port the listener is using.
*/
public int getPort() {
return port;
}
/**
* Start listening for requests.
*/
public void startListening() throws IOException {
this.listenerThread.setDaemon(true);
this.listenerThread.start();
}
/**
* Shutdown this listener.
*/
public void shutdown() {
doingShutdown = true;
try {
if (isActive) {
synchronized (listenerThread) {
try {
listenerThread.wait(1000);
} catch (InterruptedException e) {
// OK to ignore as expected to Interrupt for shutdown.
;
}
}
}
listenerThread.interrupt();
serverListenSocket.close();
} catch (IOException e) {
logger.log(Level.SEVERE, null, e);
}
}
/**
* This is a runnable and so this must be public. Don't call this externally
* but rather call the startListening() method.
*/
public void run() {
// run in loop until doingShutdown is true...
while (!doingShutdown) {
try {
synchronized (listenerThread) {
Socket clientSocket = serverListenSocket.accept();
isActive = true;
Runnable request = new RequestProcessor(owner, clientSocket);
threadPool.assign(request, true);
isActive = false;
}
} catch (SocketException e) {
if (doingShutdown) {
String msg = "doingShutdown and accept threw:"+ e.getMessage();
logger.info(msg);
} else {
logger.log(Level.SEVERE, null, e);
}
} catch (InterruptedIOException e) {
// this will happen when the server is very quiet.
// that is, no requests
logger.fine("Possibly expected due to accept timeout?" + e.getMessage());
} catch (IOException e) {
// log it and continue in the loop...
logger.log(Level.SEVERE, null, e);
}
}
}
}
package com.avaje.ebeaninternal.server.cluster.socket;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.avaje.ebeaninternal.server.lib.thread.ThreadPool;
import com.avaje.ebeaninternal.server.lib.thread.ThreadPoolManager;
/**
* Serverside multithreaded socket listener. Accepts connections and dispatches
* them to an appropriate handler.
* <p>
* This is designed as a single port listener, where part of the connection
* protocol determines which service the client is requesting (rather than a
* port per service).
* </p>
* <p>
* It has its own daemon background thread that handles the accept() loop on the
* ServerSocket.
* </p>
*/
class SocketClusterListener implements Runnable {
private static final Logger logger = Logger.getLogger(SocketClusterListener.class.getName());
/**
* The port the SocketListener uses.
*/
private final int port;
/**
* The length of the socket accept timeout.
*/
private final int listenTimeout = 60000;
/**
* The server socket used to listen for requests.
*/
private final ServerSocket serverListenSocket;
/**
* The listening thread.
*/
private final Thread listenerThread;
/**
* The pool of threads that actually do the parsing execution of requests.
*/
private final ThreadPool threadPool;
private final SocketClusterBroadcast owner;
/**
* shutting down flag.
*/
boolean doingShutdown;
/**
* Whether the listening thread is busy assigning a request to a thread.
*/
boolean isActive;
/**
* Construct with a given thread pool name.
*/
public SocketClusterListener(SocketClusterBroadcast owner, int port) {
this.owner = owner;
this.threadPool = ThreadPoolManager.getThreadPool("EbeanClusterMember");
this.port = port;
try {
this.serverListenSocket = new ServerSocket(port);
this.serverListenSocket.setSoTimeout(listenTimeout);
this.listenerThread = new Thread(this, "EbeanClusterListener");
} catch (IOException e){
String msg = "Error starting cluster socket listener on port "+port;
throw new RuntimeException(msg,e);
}
}
/**
* Returns the port the listener is using.
*/
public int getPort() {
return port;
}
/**
* Start listening for requests.
*/
public void startListening() throws IOException {
this.listenerThread.setDaemon(true);
this.listenerThread.start();
}
/**
* Shutdown this listener.
*/
public void shutdown() {
doingShutdown = true;
try {
if (isActive) {
synchronized (listenerThread) {
try {
listenerThread.wait(1000);
} catch (InterruptedException e) {
// OK to ignore as expected to Interrupt for shutdown.
;
}
}
}
listenerThread.interrupt();
serverListenSocket.close();
} catch (IOException e) {
logger.log(Level.SEVERE, null, e);
}
}
/**
* This is a runnable and so this must be public. Don't call this externally
* but rather call the startListening() method.
*/
public void run() {
// run in loop until doingShutdown is true...
while (!doingShutdown) {
try {
synchronized (listenerThread) {
Socket clientSocket = serverListenSocket.accept();
isActive = true;
Runnable request = new RequestProcessor(owner, clientSocket);
threadPool.assign(request, true);
isActive = false;
}
} catch (SocketException e) {
if (doingShutdown) {
String msg = "doingShutdown and accept threw:"+ e.getMessage();
logger.info(msg);
} else {
logger.log(Level.SEVERE, null, e);
}
} catch (InterruptedIOException e) {
// this will happen when the server is very quiet.
// that is, no requests
logger.fine("Possibly expected due to accept timeout?" + e.getMessage());
} catch (IOException e) {
// log it and continue in the loop...
logger.log(Level.SEVERE, null, e);
}
}
}
}
@@ -1,95 +1,78 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.socket;
import java.io.Serializable;
import com.avaje.ebeaninternal.server.cluster.DataHolder;
import com.avaje.ebeaninternal.server.cluster.Packet;
/**
* The messages broadcast around the cluster.
*/
public class SocketClusterMessage implements Serializable {
private static final long serialVersionUID = 2993350408394934473L;
private final String registerHost;
private final boolean register;
private final DataHolder dataHolder;
public static SocketClusterMessage register(String registerHost, boolean register){
return new SocketClusterMessage(registerHost, register);
}
public static SocketClusterMessage transEvent(DataHolder transEvent){
return new SocketClusterMessage(transEvent);
}
public static SocketClusterMessage packet(Packet packet){
DataHolder d = new DataHolder(packet.getBytes());
return new SocketClusterMessage(d);
}
/**
* Used to construct a Child AttributeMap.
*/
private SocketClusterMessage(String registerHost, boolean register) {
this.registerHost = registerHost;
this.register = register;
this.dataHolder = null;
}
private SocketClusterMessage(DataHolder dataHolder) {
this.dataHolder = dataHolder;
this.registerHost = null;
this.register = false;
}
public String toString() {
StringBuilder sb = new StringBuilder();
if (registerHost != null){
sb.append("register ");
sb.append(register);
sb.append(" ");
sb.append(registerHost);
} else {
sb.append("transEvent ");
}
return sb.toString();
}
public boolean isRegisterEvent() {
return registerHost != null;
}
public String getRegisterHost() {
return registerHost;
}
public boolean isRegister() {
return register;
}
public DataHolder getDataHolder() {
return dataHolder;
}
}
package com.avaje.ebeaninternal.server.cluster.socket;
import java.io.Serializable;
import com.avaje.ebeaninternal.server.cluster.DataHolder;
import com.avaje.ebeaninternal.server.cluster.Packet;
/**
* The messages broadcast around the cluster.
*/
public class SocketClusterMessage implements Serializable {
private static final long serialVersionUID = 2993350408394934473L;
private final String registerHost;
private final boolean register;
private final DataHolder dataHolder;
public static SocketClusterMessage register(String registerHost, boolean register){
return new SocketClusterMessage(registerHost, register);
}
public static SocketClusterMessage transEvent(DataHolder transEvent){
return new SocketClusterMessage(transEvent);
}
public static SocketClusterMessage packet(Packet packet){
DataHolder d = new DataHolder(packet.getBytes());
return new SocketClusterMessage(d);
}
/**
* Used to construct a Child AttributeMap.
*/
private SocketClusterMessage(String registerHost, boolean register) {
this.registerHost = registerHost;
this.register = register;
this.dataHolder = null;
}
private SocketClusterMessage(DataHolder dataHolder) {
this.dataHolder = dataHolder;
this.registerHost = null;
this.register = false;
}
public String toString() {
StringBuilder sb = new StringBuilder();
if (registerHost != null){
sb.append("register ");
sb.append(register);
sb.append(" ");
sb.append(registerHost);
} else {
sb.append("transEvent ");
}
return sb.toString();
}
public boolean isRegisterEvent() {
return registerHost != null;
}
public String getRegisterHost() {
return registerHost;
}
public boolean isRegister() {
return register;
}
public DataHolder getDataHolder() {
return dataHolder;
}
}
@@ -1,60 +1,41 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.socket;
/**
* The current state of this cluster member.
*
* @author rbygrave
*/
public class SocketClusterStatus {
private final int currentGroupSize;
private final int txnIncoming;
private final int txtOutgoing;
public SocketClusterStatus(int currentGroupSize, int txnIncoming, int txnOutgoing) {
this.currentGroupSize = currentGroupSize;
this.txnIncoming = txnIncoming;
this.txtOutgoing = txnOutgoing;
}
/**
* Return the number of members of the cluster currently online.
*/
public int getCurrentGroupSize() {
return currentGroupSize;
}
/**
* Return the number of Remote transactions received.
*/
public int getTxnIncoming() {
return txnIncoming;
}
/**
* Return the number of transactions sent to the cluster.
*/
public int getTxtOutgoing() {
return txtOutgoing;
}
}
package com.avaje.ebeaninternal.server.cluster.socket;
/**
* The current state of this cluster member.
*
* @author rbygrave
*/
public class SocketClusterStatus {
private final int currentGroupSize;
private final int txnIncoming;
private final int txtOutgoing;
public SocketClusterStatus(int currentGroupSize, int txnIncoming, int txnOutgoing) {
this.currentGroupSize = currentGroupSize;
this.txnIncoming = txnIncoming;
this.txtOutgoing = txnOutgoing;
}
/**
* Return the number of members of the cluster currently online.
*/
public int getCurrentGroupSize() {
return currentGroupSize;
}
/**
* Return the number of Remote transactions received.
*/
public int getTxnIncoming() {
return txnIncoming;
}
/**
* Return the number of transactions sent to the cluster.
*/
public int getTxtOutgoing() {
return txtOutgoing;
}
}
@@ -1,146 +1,129 @@
/**
* Copyright (C) 2006 Robin Bygrave
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.cluster.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
* The client side of a TCP Sockect connection.
*/
class SocketConnection {
/**
* The object underlying objectOutputStream.
*/
ObjectOutputStream oos;
/**
* The underlying ObjectInputStream.
*/
ObjectInputStream ois;
/**
* The underlying inputStream.
*/
InputStream is;
/**
* The underlying outputStream.
*/
OutputStream os;
/**
* The underlying socket.
*/
Socket socket;
/**
* Create for a given Socket.
*/
public SocketConnection(Socket socket) throws IOException {
this.is = socket.getInputStream();
this.os = socket.getOutputStream();
this.socket = socket;
}
/**
* Disconnect from the server.
*/
public void disconnect() throws IOException {
os.flush();
socket.close();
}
/**
* Flush the outputStream.
*/
public void flush() throws IOException {
os.flush();
}
/**
* Read an object from the object input stream.
*/
public Object readObject() throws IOException, ClassNotFoundException {
return getObjectInputStream().readObject();
}
/**
* Write an object to the object output stream.
*/
public ObjectOutputStream writeObject(Object object) throws IOException {
ObjectOutputStream oos = getObjectOutputStream();
oos.writeObject(object);
return oos;
}
/**
* Get the object output stream.
*/
public ObjectOutputStream getObjectOutputStream() throws IOException {
if (oos == null){
oos = new ObjectOutputStream(os);
}
return oos;
}
/**
* Get the object input stream.
*/
public ObjectInputStream getObjectInputStream() throws IOException {
if (ois == null){
ois = new ObjectInputStream(is);
}
return ois;
}
/**
* Set the ObjectInputStream to use.
*/
public void setObjectInputStream(ObjectInputStream ois) {
this.ois = ois;
}
/**
* Set the ObjectOutputStream to use.
*/
public void setObjectOutputStream(ObjectOutputStream oos) {
this.oos = oos;
}
/**
* Return the underlying input stream.
*/
public InputStream getInputStream() throws IOException {
return is;
}
/**
* Return the underlying output stream.
*/
public OutputStream getOutputStream() throws IOException {
return os;
}
}
package com.avaje.ebeaninternal.server.cluster.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
* The client side of a TCP Sockect connection.
*/
class SocketConnection {
/**
* The object underlying objectOutputStream.
*/
ObjectOutputStream oos;
/**
* The underlying ObjectInputStream.
*/
ObjectInputStream ois;
/**
* The underlying inputStream.
*/
InputStream is;
/**
* The underlying outputStream.
*/
OutputStream os;
/**
* The underlying socket.
*/
Socket socket;
/**
* Create for a given Socket.
*/
public SocketConnection(Socket socket) throws IOException {
this.is = socket.getInputStream();
this.os = socket.getOutputStream();
this.socket = socket;
}
/**
* Disconnect from the server.
*/
public void disconnect() throws IOException {
os.flush();
socket.close();
}
/**
* Flush the outputStream.
*/
public void flush() throws IOException {
os.flush();
}
/**
* Read an object from the object input stream.
*/
public Object readObject() throws IOException, ClassNotFoundException {
return getObjectInputStream().readObject();
}
/**
* Write an object to the object output stream.
*/
public ObjectOutputStream writeObject(Object object) throws IOException {
ObjectOutputStream oos = getObjectOutputStream();
oos.writeObject(object);
return oos;
}
/**
* Get the object output stream.
*/
public ObjectOutputStream getObjectOutputStream() throws IOException {
if (oos == null){
oos = new ObjectOutputStream(os);
}
return oos;
}
/**
* Get the object input stream.
*/
public ObjectInputStream getObjectInputStream() throws IOException {
if (ois == null){
ois = new ObjectInputStream(is);
}
return ois;
}
/**
* Set the ObjectInputStream to use.
*/
public void setObjectInputStream(ObjectInputStream ois) {
this.ois = ois;
}
/**
* Set the ObjectOutputStream to use.
*/
public void setObjectOutputStream(ObjectOutputStream oos) {
this.oos = oos;
}
/**
* Return the underlying input stream.
*/
public InputStream getInputStream() throws IOException {
return is;
}
/**
* Return the underlying output stream.
*/
public OutputStream getOutputStream() throws IOException {
return os;
}
}