mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
#628 - Refactor Clustering - extract ClusterBroadcast implementations (TCP and Multicast)
This commit is contained in:
1 parent
30a5769973
commit
9a912390d4
40 files changed
+134
-3976
No files matched your search
@@ -28,17 +28,15 @@ import java.util.List;
|
||||
* size of data sent around the network.
|
||||
* </p>
|
||||
*/
|
||||
public class BeanPersistIds implements Serializable {
|
||||
public class BeanPersistIds {
|
||||
|
||||
private static final long serialVersionUID = 8389469180931531409L;
|
||||
|
||||
private transient BeanDescriptor<?> beanDescriptor;
|
||||
private final BeanDescriptor<?> beanDescriptor;
|
||||
|
||||
private final String descriptorId;
|
||||
|
||||
private ArrayList<Serializable> insertIds;
|
||||
private ArrayList<Serializable> updateIds;
|
||||
private ArrayList<Serializable> deleteIds;
|
||||
private List<Object> insertIds;
|
||||
private List<Object> updateIds;
|
||||
private List<Object> deleteIds;
|
||||
|
||||
/**
|
||||
* Create the payload.
|
||||
@@ -62,8 +60,7 @@ public class BeanPersistIds implements Serializable {
|
||||
IdBinder idBinder = beanDescriptor.getIdBinder();
|
||||
|
||||
int iudType = dataInput.readInt();
|
||||
ArrayList<Serializable> idList = readIdList(dataInput, idBinder);
|
||||
|
||||
List<Object> idList = readIdList(dataInput, idBinder);
|
||||
switch (iudType) {
|
||||
case 0:
|
||||
insertIds = idList;
|
||||
@@ -89,24 +86,23 @@ public class BeanPersistIds implements Serializable {
|
||||
* across multiple Packets.
|
||||
* </p>
|
||||
*/
|
||||
public void writeBinaryMessage(BinaryMessageList msgList) throws IOException {
|
||||
void writeBinaryMessage(BinaryMessageList msgList) throws IOException {
|
||||
|
||||
writeIdList(beanDescriptor, 0, insertIds, msgList);
|
||||
writeIdList(beanDescriptor, 1, updateIds, msgList);
|
||||
writeIdList(beanDescriptor, 2, deleteIds, msgList);
|
||||
|
||||
}
|
||||
|
||||
private ArrayList<Serializable> readIdList(DataInput dataInput, IdBinder idBinder) throws IOException {
|
||||
private List<Object> readIdList(DataInput dataInput, IdBinder idBinder) throws IOException {
|
||||
|
||||
int count = dataInput.readInt();
|
||||
if (count < 1) {
|
||||
return null;
|
||||
}
|
||||
ArrayList<Serializable> idList = new ArrayList<Serializable>(count);
|
||||
List<Object> idList = new ArrayList<Object>(count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
Object id = idBinder.readData(dataInput);
|
||||
idList.add((Serializable) id);
|
||||
idList.add(id);
|
||||
}
|
||||
return idList;
|
||||
}
|
||||
@@ -121,8 +117,7 @@ public class BeanPersistIds implements Serializable {
|
||||
* Packets.
|
||||
* </p>
|
||||
*/
|
||||
private void writeIdList(BeanDescriptor<?> desc, int iudType, ArrayList<Serializable> idList,
|
||||
BinaryMessageList msgList) throws IOException {
|
||||
private void writeIdList(BeanDescriptor<?> desc, int iudType, List<Object> idList, BinaryMessageList msgList) throws IOException {
|
||||
|
||||
IdBinder idBinder = desc.getIdBinder();
|
||||
|
||||
@@ -144,8 +139,7 @@ public class BeanPersistIds implements Serializable {
|
||||
os.writeInt(count);
|
||||
|
||||
for (; i < endOfLoop; i++) {
|
||||
Serializable idValue = idList.get(i);
|
||||
idBinder.writeData(os, idValue);
|
||||
idBinder.writeData(os, idList.get(i));
|
||||
}
|
||||
|
||||
os.flush();
|
||||
@@ -174,7 +168,7 @@ public class BeanPersistIds implements Serializable {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void addId(PersistRequest.Type type, Serializable id) {
|
||||
void addId(PersistRequest.Type type, Serializable id) {
|
||||
switch (type) {
|
||||
case INSERT:
|
||||
addInsertId(id);
|
||||
@@ -194,21 +188,21 @@ public class BeanPersistIds implements Serializable {
|
||||
|
||||
private void addInsertId(Serializable id) {
|
||||
if (insertIds == null) {
|
||||
insertIds = new ArrayList<Serializable>();
|
||||
insertIds = new ArrayList<Object>();
|
||||
}
|
||||
insertIds.add(id);
|
||||
}
|
||||
|
||||
private void addUpdateId(Serializable id) {
|
||||
if (updateIds == null) {
|
||||
updateIds = new ArrayList<Serializable>();
|
||||
updateIds = new ArrayList<Object>();
|
||||
}
|
||||
updateIds.add(id);
|
||||
}
|
||||
|
||||
private void addDeleteId(Serializable id) {
|
||||
if (deleteIds == null) {
|
||||
deleteIds = new ArrayList<Serializable>();
|
||||
deleteIds = new ArrayList<Object>();
|
||||
}
|
||||
deleteIds.add(id);
|
||||
}
|
||||
@@ -217,19 +211,15 @@ public class BeanPersistIds implements Serializable {
|
||||
return beanDescriptor;
|
||||
}
|
||||
|
||||
public List<Serializable> getDeleteIds() {
|
||||
List<Object> getDeleteIds() {
|
||||
return deleteIds;
|
||||
}
|
||||
|
||||
public void setBeanDescriptor(BeanDescriptor<?> beanDescriptor) {
|
||||
this.beanDescriptor = beanDescriptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the cache and local BeanPersistListener of this event that came
|
||||
* from another server in the cluster.
|
||||
*/
|
||||
public void notifyCacheAndListener() {
|
||||
void notifyCacheAndListener() {
|
||||
|
||||
BeanPersistListener listener = beanDescriptor.getPersistListener();
|
||||
|
||||
@@ -238,7 +228,6 @@ public class BeanPersistIds implements Serializable {
|
||||
|
||||
if (insertIds != null) {
|
||||
if (listener != null) {
|
||||
// notify listener
|
||||
for (int i = 0; i < insertIds.size(); i++) {
|
||||
listener.remoteInsert(insertIds.get(i));
|
||||
}
|
||||
@@ -246,25 +235,19 @@ public class BeanPersistIds implements Serializable {
|
||||
}
|
||||
if (updateIds != null) {
|
||||
for (int i = 0; i < updateIds.size(); i++) {
|
||||
Serializable id = updateIds.get(i);
|
||||
|
||||
// remove from cache
|
||||
Object id = updateIds.get(i);
|
||||
beanDescriptor.cacheBeanRemove(id);
|
||||
if (listener != null) {
|
||||
// notify listener
|
||||
listener.remoteInsert(id);
|
||||
listener.remoteUpdate(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (deleteIds != null) {
|
||||
for (int i = 0; i < deleteIds.size(); i++) {
|
||||
Serializable id = deleteIds.get(i);
|
||||
|
||||
// remove from cache
|
||||
Object id = deleteIds.get(i);
|
||||
beanDescriptor.cacheBeanRemove(id);
|
||||
if (listener != null) {
|
||||
// notify listener
|
||||
listener.remoteInsert(id);
|
||||
listener.remoteDelete(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public final class DeleteByIdMap {
|
||||
public void notifyCache() {
|
||||
for (BeanPersistIds deleteIds : beanMap.values()) {
|
||||
BeanDescriptor<?> d = deleteIds.getBeanDescriptor();
|
||||
List<Serializable> idValues = deleteIds.getDeleteIds();
|
||||
List<Object> idValues = deleteIds.getDeleteIds();
|
||||
if (idValues != null) {
|
||||
d.queryCacheClear();
|
||||
for (int i = 0; i < idValues.size(); i++) {
|
||||
@@ -79,7 +79,7 @@ public final class DeleteByIdMap {
|
||||
/**
|
||||
* Add the deletes to the DocStoreUpdates.
|
||||
*/
|
||||
public void addDocStoreUpdates(DocStoreUpdates docStoreUpdates, DocStoreMode txnIndexMode) {
|
||||
void addDocStoreUpdates(DocStoreUpdates docStoreUpdates, DocStoreMode txnIndexMode) {
|
||||
for (BeanPersistIds deleteIds : beanMap.values()) {
|
||||
BeanDescriptor<?> desc = deleteIds.getBeanDescriptor();
|
||||
DocStoreMode mode = desc.getDocStoreMode(PersistRequest.Type.DELETE, txnIndexMode);
|
||||
@@ -87,7 +87,7 @@ public final class DeleteByIdMap {
|
||||
// Add to queue or bulk update entries
|
||||
boolean queue = (DocStoreMode.QUEUE == mode);
|
||||
String queueId = desc.getDocStoreQueueId();
|
||||
List<Serializable> idValues = deleteIds.getDeleteIds();
|
||||
List<Object> idValues = deleteIds.getDeleteIds();
|
||||
if (idValues != null) {
|
||||
for (int i = 0; i < idValues.size(); i++) {
|
||||
if (queue) {
|
||||
|
||||
@@ -78,12 +78,11 @@ public final class PostCommitProcessing {
|
||||
this.remoteTransactionEvent = createRemoteTransactionEvent();
|
||||
}
|
||||
|
||||
public void notifyLocalCacheIndex() {
|
||||
|
||||
// notify cache with bulk insert/update/delete statements
|
||||
/**
|
||||
* Notify the local part of L2 cache.
|
||||
*/
|
||||
void notifyLocalCache() {
|
||||
processTableEvents(event.getEventTables());
|
||||
|
||||
// notify cache with bean changes
|
||||
event.notifyCache();
|
||||
}
|
||||
|
||||
@@ -105,7 +104,7 @@ public final class PostCommitProcessing {
|
||||
/**
|
||||
* Process any document store updates.
|
||||
*/
|
||||
protected void processDocStoreUpdates() {
|
||||
private void processDocStoreUpdates() {
|
||||
|
||||
if (isDocStoreUpdate()) {
|
||||
// collect 'bulk update' and 'queue' events
|
||||
@@ -129,7 +128,7 @@ public final class PostCommitProcessing {
|
||||
return manager.isDocStoreActive() && (txnDocStoreMode == null || txnDocStoreMode != DocStoreMode.IGNORE);
|
||||
}
|
||||
|
||||
public void notifyCluster() {
|
||||
private void notifyCluster() {
|
||||
if (remoteTransactionEvent != null && !remoteTransactionEvent.isEmpty()) {
|
||||
// send the interesting events to the cluster
|
||||
if (logger.isDebugEnabled()) {
|
||||
@@ -140,10 +139,14 @@ public final class PostCommitProcessing {
|
||||
}
|
||||
}
|
||||
|
||||
public Runnable notifyPersistListeners() {
|
||||
/**
|
||||
* In background notify persist listeners, cluster and document store.
|
||||
*/
|
||||
Runnable backgroundNotify() {
|
||||
return new Runnable() {
|
||||
public void run() {
|
||||
localPersistListenersNotify();
|
||||
notifyCluster();
|
||||
processDocStoreUpdates();
|
||||
}
|
||||
};
|
||||
|
||||
+13
-12
@@ -33,11 +33,16 @@ public class RemoteTransactionEvent implements Runnable {
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(beanPersistList);
|
||||
StringBuilder sb = new StringBuilder(100);
|
||||
if (!beanPersistList.isEmpty()) {
|
||||
sb.append(beanPersistList);
|
||||
}
|
||||
if (tableList != null) {
|
||||
sb.append(tableList);
|
||||
}
|
||||
if (deleteByIdMap != null) {
|
||||
sb.append(deleteByIdMap.values());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@@ -55,15 +60,15 @@ public class RemoteTransactionEvent implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
if (beanPersistList != null) {
|
||||
for (int i = 0; i < beanPersistList.size(); i++) {
|
||||
beanPersistList.get(i).writeBinaryMessage(msgList);
|
||||
}
|
||||
for (int i = 0; i < beanPersistList.size(); i++) {
|
||||
beanPersistList.get(i).writeBinaryMessage(msgList);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return beanPersistList.isEmpty() && (tableList == null || tableList.isEmpty());
|
||||
return beanPersistList.isEmpty()
|
||||
&& (tableList == null || tableList.isEmpty())
|
||||
&& (deleteByIdMap == null || deleteByIdMap.isEmpty());
|
||||
}
|
||||
|
||||
public void addBeanPersistIds(BeanPersistIds beanPersist) {
|
||||
@@ -89,11 +94,7 @@ public class RemoteTransactionEvent implements Runnable {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public DeleteByIdMap getDeleteByIdMap() {
|
||||
return deleteByIdMap;
|
||||
}
|
||||
|
||||
public void setDeleteByIdMap(DeleteByIdMap deleteByIdMap) {
|
||||
void setDeleteByIdMap(DeleteByIdMap deleteByIdMap) {
|
||||
this.deleteByIdMap = deleteByIdMap;
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,8 @@ public class TransactionManager {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(TransactionManager.class);
|
||||
|
||||
public static final Logger clusterLogger = LoggerFactory.getLogger("org.avaje.ebean.Cluster");
|
||||
|
||||
public static final Logger SQL_LOGGER = LoggerFactory.getLogger("org.avaje.ebean.SQL");
|
||||
|
||||
public static final Logger SUM_LOGGER = LoggerFactory.getLogger("org.avaje.ebean.SUM");
|
||||
@@ -396,12 +398,8 @@ public class TransactionManager {
|
||||
}
|
||||
|
||||
PostCommitProcessing postCommit = new PostCommitProcessing(clusterManager, this, transaction);
|
||||
|
||||
postCommit.notifyLocalCacheIndex();
|
||||
postCommit.notifyCluster();
|
||||
|
||||
// cluster and text indexing
|
||||
backgroundExecutor.execute(postCommit.notifyPersistListeners());
|
||||
postCommit.notifyLocalCache();
|
||||
backgroundExecutor.execute(postCommit.backgroundNotify());
|
||||
|
||||
for (TransactionEventListener listener : transactionEventListeners) {
|
||||
listener.postTransactionCommit(transaction);
|
||||
@@ -425,11 +423,8 @@ public class TransactionManager {
|
||||
event.add(tableEvents);
|
||||
|
||||
PostCommitProcessing postCommit = new PostCommitProcessing(clusterManager, this, event);
|
||||
|
||||
// invalidate parts of local cache and index
|
||||
postCommit.notifyLocalCacheIndex();
|
||||
|
||||
backgroundExecutor.execute(postCommit.notifyPersistListeners());
|
||||
postCommit.notifyLocalCache();
|
||||
backgroundExecutor.execute(postCommit.backgroundNotify());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -437,8 +432,8 @@ public class TransactionManager {
|
||||
*/
|
||||
public void remoteTransactionEvent(RemoteTransactionEvent remoteEvent) {
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Cluster Received: " + remoteEvent.toString());
|
||||
if (clusterLogger.isDebugEnabled()) {
|
||||
clusterLogger.debug("processing {}", toString());
|
||||
}
|
||||
|
||||
List<TableIUD> tableIUDList = remoteEvent.getTableIUDList();
|
||||
@@ -449,11 +444,12 @@ public class TransactionManager {
|
||||
}
|
||||
}
|
||||
|
||||
// note DeleteById is written as BeanPersistIds and getBeanPersistList()
|
||||
// processes both Bean IUD and DeleteById
|
||||
List<BeanPersistIds> beanPersistList = remoteEvent.getBeanPersistList();
|
||||
if (beanPersistList != null) {
|
||||
for (int i = 0; i < beanPersistList.size(); i++) {
|
||||
BeanPersistIds beanPersist = beanPersistList.get(i);
|
||||
beanPersist.notifyCacheAndListener();
|
||||
beanPersistList.get(i).notifyCacheAndListener();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user