}
}
}
private int sendBackups(BackupAwareOperation backupAwareOp) throws Exception {
Operation op = (Operation) backupAwareOp;
boolean returnsResponse = op.returnsResponse();
InternalPartitionService partitionService = nodeEngine.getPartitionService();
int maxBackupCount = InternalPartition.MAX_BACKUP_COUNT;
int maxPossibleBackupCount = Math.min(partitionService.getMemberGroupsSize() - 1, maxBackupCount);
int requestedSyncBackupCount = backupAwareOp.getSyncBackupCount() > 0
? Math.min(maxBackupCount, backupAwareOp.getSyncBackupCount()) : 0;
int requestedAsyncBackupCount = backupAwareOp.getAsyncBackupCount() > 0
? Math.min(maxBackupCount - requestedSyncBackupCount, backupAwareOp.getAsyncBackupCount()) : 0;
int totalRequestedBackupCount = requestedSyncBackupCount + requestedAsyncBackupCount;
if (totalRequestedBackupCount == 0) {
return 0;
}
int partitionId = op.getPartitionId();
long[] replicaVersions = partitionService.incrementPartitionReplicaVersions(partitionId, totalRequestedBackupCount);
int syncBackupCount = Math.min(maxPossibleBackupCount, requestedSyncBackupCount);
int asyncBackupCount = Math.min(maxPossibleBackupCount - syncBackupCount, requestedAsyncBackupCount);
if (!returnsResponse) {
asyncBackupCount += syncBackupCount;
syncBackupCount = 0;
}
int totalBackupCount = syncBackupCount + asyncBackupCount;
if (totalBackupCount == 0) {
return 0;
}
int sentSyncBackupCount = 0;
String serviceName = op.getServiceName();
InternalPartition partition = partitionService.getPartition(partitionId);
for (int replicaIndex = 1; replicaIndex <= totalBackupCount; replicaIndex++) {
Address target = partition.getReplicaAddress(replicaIndex);
if (target != null) {
if (target.equals(node.getThisAddress())) {
throw new IllegalStateException("Normally shouldn't happen! Owner node and backup node " +
"are the same! " + partition);
} else {
Operation backupOp = backupAwareOp.getBackupOperation();
if (backupOp == null) {
throw new IllegalArgumentException("Backup operation should not be null!");
}
backupOp.setPartitionId(partitionId).setReplicaIndex(replicaIndex).setServiceName(serviceName);
Data backupOpData = nodeEngine.getSerializationService().toData(backupOp);
boolean isSyncBackup = replicaIndex <= syncBackupCount;
Backup backup = new Backup(backupOpData, op.getCallerAddress(), replicaVersions, isSyncBackup);
backup.setPartitionId(partitionId).setReplicaIndex(replicaIndex).setServiceName(serviceName)