use of com.sun.messaging.jmq.jmsserver.core.Destination in project openmq by eclipse-ee4j.
the class BrokerConsumers method removeRemoteDeadMessage.
private boolean removeRemoteDeadMessage(int ackType, PacketReference ref, ConsumerUID cuid, ConsumerUID storedid, Map optionalProps) throws BrokerException {
if (ref == null) {
return true;
}
Destination d = ref.getDestination();
Queue[] qs = DL.getDMQ(ref.getPartitionedStore());
if (d == qs[0]) {
// already gone, ignore
return true;
}
// first pull out properties
String comment = null;
Exception ex = null;
Integer deliverCnt = null;
Integer reasonInt = null;
String deadbkr = null;
if (optionalProps != null) {
comment = (String) optionalProps.get(DMQ.UNDELIVERED_COMMENT);
ex = (Exception) optionalProps.get(DMQ.UNDELIVERED_EXCEPTION);
deliverCnt = (Integer) optionalProps.get(Destination.TEMP_CNT);
reasonInt = (Integer) optionalProps.get("REASON");
deadbkr = (String) optionalProps.get(DMQ.DEAD_BROKER);
}
RemoveReason rr = null;
if (ackType == ClusterBroadcast.MSG_UNDELIVERABLE) {
rr = RemoveReason.UNDELIVERABLE;
} else {
rr = RemoveReason.ERROR;
if (reasonInt != null) {
rr = RemoveReason.findReason(reasonInt.intValue());
}
}
if (comment == null) {
comment = "none";
}
if (ref.markDead(cuid, storedid, comment, ex, rr, (deliverCnt == null ? 0 : deliverCnt.intValue()), deadbkr)) {
try {
if (ref.isDead()) {
if (getDEBUG()) {
Globals.getLogger().log(logger.INFO, "Remove dead message " + ref + " for remote consumer " + cuid + " on destination " + d + " with reason " + rr);
}
try {
d.removeDeadMessage(ref);
} catch (Exception e) {
logger.log(logger.WARNING, "Unable to remove dead[" + rr + ", " + deadbkr + "] message " + ref + "[" + cuid + "]: " + e.getMessage(), e);
}
}
} finally {
ref.postAcknowledgedRemoval();
}
return true;
}
return false;
}
use of com.sun.messaging.jmq.jmsserver.core.Destination in project openmq by eclipse-ee4j.
the class BrokerConsumers method removeConsumer.
public void removeConsumer(com.sun.messaging.jmq.jmsserver.core.ConsumerUID uid, Map<TransactionUID, LinkedHashMap<SysMessageID, Integer>> pendingMsgs, boolean cleanup) throws BrokerException {
if (getDEBUG()) {
logger.log(logger.INFO, "BrokerConsumers.removeConsumer(" + uid + ", " + pendingMsgs + ", " + cleanup + ")");
}
Consumer c = null;
synchronized (removeConsumerLock) {
c = (Consumer) consumers.remove(uid);
}
if (c == null && !cleanup) {
return;
}
Destination d = null;
if (c != null) {
c.pause("MultiBroker - removing consumer");
// remove it from the destination
Destination[] ds = DL.getDestination(null, c.getDestinationUID());
// PART
d = ds[0];
// quit listening for busy events
Object listener = listeners.remove(uid);
if (listener != null) {
c.removeEventListener(listener);
}
// remove it from the active list
activeConsumers.remove(c);
}
Set destroySet = new LinkedHashSet();
LinkedHashSet openacks = new LinkedHashSet();
Map<PartitionedStore, LinkedHashSet> openmsgmp = new LinkedHashMap<>();
Map<TransactionUID, Set<AckEntry>> mypending = new LinkedHashMap<>();
boolean haspendingtxn = false;
// we sent the messages directly and must explicitly ack
synchronized (deliveredMessages) {
if (c != null) {
cleanupList.put(uid, c.getParentList());
}
Map cparentmp = (Map) cleanupList.get(uid);
if (getDEBUG()) {
logger.log(logger.INFO, "BrokerConsumers.removeConsumer:" + uid + ", pending=" + pendingMsgs + ", cleanup=" + cleanup + ", cparentmp=" + cparentmp);
}
Iterator itr = deliveredMessages.values().iterator();
while (itr.hasNext()) {
AckEntry e = (AckEntry) itr.next();
if (!e.getConsumerUID().equals(uid)) {
continue;
}
if (pendingMsgs != null) {
Iterator<Map.Entry<TransactionUID, LinkedHashMap<SysMessageID, Integer>>> itr1 = pendingMsgs.entrySet().iterator();
Map.Entry<TransactionUID, LinkedHashMap<SysMessageID, Integer>> pair = null;
TransactionUID tid = null;
LinkedHashMap<SysMessageID, Integer> sysiddcts = null;
Set<AckEntry> ackentries = null;
boolean found = false;
Integer deliverCnt = null;
while (itr1.hasNext()) {
pair = itr1.next();
tid = pair.getKey();
sysiddcts = pair.getValue();
deliverCnt = sysiddcts.get(e.getSysMessageID());
if (deliverCnt != null) {
if (tid != null && tid.longValue() == 0L) {
updateConsumed(e, deliverCnt, false);
continue;
}
if (cleanup || e.getTUID() != null) {
updateConsumed(e, deliverCnt, false);
break;
}
found = true;
ackentries = mypending.get(tid);
if (ackentries == null) {
ackentries = new LinkedHashSet();
mypending.put(tid, ackentries);
}
if (tid != null && !haspendingtxn) {
haspendingtxn = true;
}
ackentries.add(e);
updateConsumed(e, deliverCnt, false);
break;
}
}
if (found) {
continue;
}
}
if (e.getTUID() != null) {
continue;
}
if (getDEBUG()) {
logger.log(logger.DEBUG, "BrokerConsumers.removeConsumer:" + uid + ", remove AckEntry=" + e + ", c=" + c);
}
itr.remove();
if (cleanup) {
updateConsumed(e, Integer.valueOf(1), true);
}
if (c != null) {
if (c.isFalconRemote()) {
e.acknowledged(false);
} else {
PacketReference ref = e.getReference();
if (ref != null) {
ref.removeInDelivery(e.getStoredConsumerUID());
destroySet.add(ref);
}
}
continue;
}
PacketReference ref = e.getReference();
if (ref != null) {
ref.removeInDelivery(e.getStoredConsumerUID());
}
openacks.add(e);
}
itr = openacks.iterator();
while (itr.hasNext()) {
AckEntry e = (AckEntry) itr.next();
if (cparentmp == null || cparentmp.size() == 0) {
e.acknowledged(false);
} else {
PacketReference ref = e.getReference();
if (ref != null) {
PartitionedStore ps = null;
if (!ref.getDestinationUID().isQueue()) {
ps = new NoPersistPartitionedStoreImpl(e.getStoredConsumerUID());
} else {
ps = ref.getPartitionedStore();
}
LinkedHashSet set = openmsgmp.get(ps);
if (set == null) {
set = new LinkedHashSet();
openmsgmp.put(ps, set);
}
set.add(ref);
}
}
}
if (cparentmp != null && cparentmp.size() > 0 && openmsgmp.size() > 0) {
itr = openmsgmp.entrySet().iterator();
PartitionedStore ps = null;
Prioritized pl = null;
Map.Entry pair = null;
while (itr.hasNext()) {
pair = (Map.Entry) itr.next();
ps = (PartitionedStore) pair.getKey();
pl = (Prioritized) cparentmp.get(ps);
if (pl != null) {
pl.addAllOrdered((Set) pair.getValue());
} else {
logger.log(logger.WARNING, "Message(s) " + cparentmp.get(ps) + "[" + ps + "] parentlist not found on removing consumer " + uid);
}
}
}
if (cleanup || pendingMsgs == null) {
cleanupList.remove(uid);
pendingConsumerUIDs.remove(uid);
} else if (mypending.size() > 0) {
pendingConsumerUIDs.put(uid, mypending);
}
}
if (c != null) {
c.destroyConsumer(destroySet, false, false);
if (d != null) {
d.removeConsumer(uid, false);
}
}
List<Set<AckEntry>> l = null;
synchronized (pendingConsumerUIDs) {
l = new ArrayList<>(mypending.values());
}
Set<AckEntry> entries = null;
Iterator<Set<AckEntry>> itr = l.iterator();
while (itr.hasNext()) {
entries = itr.next();
AckEntry entry = null;
Iterator<AckEntry> itr1 = entries.iterator();
while (itr1.hasNext()) {
entry = itr1.next();
entry.pendingStarted();
}
}
if (haspendingtxn) {
notifyPendingCheckTimer();
}
}
use of com.sun.messaging.jmq.jmsserver.core.Destination in project openmq by eclipse-ee4j.
the class BrokerConsumers method acknowledgeMessageFromRemote2P.
public void acknowledgeMessageFromRemote2P(int ackType, SysMessageID[] sysids, com.sun.messaging.jmq.jmsserver.core.ConsumerUID[] cuids, Map optionalProps, Long txnID, com.sun.messaging.jmq.jmsserver.core.BrokerAddress txnHomeBroker) throws BrokerException {
if (txnID == null) {
throw new BrokerException("Internal Error: call with null txnID");
}
// handle 2-phase remote ack
TransactionUID tid = new TransactionUID(txnID.longValue());
if (ackType == ClusterBroadcast.MSG_PREPARE) {
PartitionedStore refpstore = null;
TransactionAcknowledgement ta = null;
ArrayList<TransactionAcknowledgement> tltas = null;
TransactionAcknowledgement[] tas = null;
HashMap<TransactionList, ArrayList<TransactionAcknowledgement>> tltasmap = new HashMap<>();
TransactionList tl = null;
AckEntry entry = null, value = null;
StringBuilder dbuf = new StringBuilder();
AckEntryNotFoundException ae = null;
synchronized (deliveredMessages) {
for (int i = 0; i < sysids.length; i++) {
entry = new AckEntry(sysids[i], cuids[i], null);
value = (AckEntry) deliveredMessages.get(entry);
if (value == null) {
// XXX
String emsg = "[" + sysids[i] + ":" + cuids[i] + "]TID=" + tid + " not found, maybe rerouted";
if (ae == null) {
ae = new AckEntryNotFoundException(emsg);
}
ae.addAckEntry(sysids[i], cuids[i]);
logger.log(logger.WARNING, "[" + sysids[i] + ":" + cuids[i] + "] not found for preparing remote transaction " + tid + ", maybe rerouted");
continue;
}
if (value.getTUID() != null) {
String emsg = "[" + sysids[i] + ":" + cuids[i] + "]TID=" + tid + " has been rerouted";
if (ae == null) {
ae = new AckEntryNotFoundException(emsg);
}
ae.addAckEntry(sysids[i], cuids[i]);
logger.log(logger.WARNING, "[" + sysids[i] + ":" + cuids[i] + "] for preparing remote transaction " + tid + " conflict with transaction " + value.getTUID());
continue;
}
PacketReference ref = value.getReference();
if (ref == null) {
deliveredMessages.remove(entry);
String emsg = "Unable to prepare [" + sysids[i] + ":" + cuids[i] + "]TID=" + tid + " because the message has been removed";
if (ae == null) {
ae = new AckEntryNotFoundException(emsg);
}
ae.addAckEntry(sysids[i], cuids[i]);
logger.log(logger.WARNING, emsg);
continue;
}
com.sun.messaging.jmq.jmsserver.core.ConsumerUID scuid = value.getStoredConsumerUID();
ta = new TransactionAcknowledgement(sysids[i], cuids[i], scuid);
if (!scuid.shouldStore() || !ref.isPersistent()) {
ta.setShouldStore(false);
}
if (!Globals.getDestinationList().isPartitionMode()) {
TransactionList[] tls = DL.getTransactionList(Globals.getStore().getPrimaryPartition());
tl = tls[0];
} else {
refpstore = ref.getPartitionedStore();
TransactionList[] tls = Globals.getDestinationList().getTransactionList(refpstore);
tl = tls[0];
if (tl == null) {
deliveredMessages.remove(entry);
String emsg = "Unable to prepare [" + sysids[i] + ":" + cuids[i] + "]TID=" + tid + " because transaction list for partition " + refpstore + " not found";
if (ae == null) {
ae = new AckEntryNotFoundException(emsg);
}
ae.addAckEntry(sysids[i], cuids[i]);
logger.log(logger.WARNING, emsg);
continue;
}
}
tltas = tltasmap.get(tl);
if (tltas == null) {
tltas = new ArrayList<>();
tltasmap.put(tl, tltas);
}
tltas.add(ta);
if (getDEBUG()) {
dbuf.append("\n\t[" + ta + "]" + tl);
}
}
if (ae != null) {
throw ae;
}
Iterator<Map.Entry<TransactionList, ArrayList<TransactionAcknowledgement>>> itr = tltasmap.entrySet().iterator();
Map.Entry<TransactionList, ArrayList<TransactionAcknowledgement>> pair = null;
while (itr.hasNext()) {
pair = itr.next();
tl = pair.getKey();
tltas = pair.getValue();
tas = tltas.toArray(new TransactionAcknowledgement[tltas.size()]);
TransactionState ts = new TransactionState(AutoRollbackType.NOT_PREPARED, 0L, true);
ts.setState(TransactionState.PREPARED);
if (getDEBUG()) {
logger.log(logger.INFO, "Preparing remote transaction " + tid + " for [" + tltas + "]" + tl + " from " + txnHomeBroker);
}
tl.logRemoteTransaction(tid, ts, tas, txnHomeBroker, false, true, true);
}
for (int i = 0; i < sysids.length; i++) {
entry = new AckEntry(sysids[i], cuids[i], null);
value = (AckEntry) deliveredMessages.get(entry);
value.setTUID(tid);
}
}
Iterator<TransactionList> itr = tltasmap.keySet().iterator();
while (itr.hasNext()) {
tl = itr.next();
tl.pendingStartedForRemotePreparedTransaction(tid);
}
notifyPendingCheckTimer();
if (getDEBUG()) {
logger.log(logger.INFO, "Prepared remote transaction " + tid + " from " + txnHomeBroker + dbuf.toString());
}
return;
}
if (ackType == ClusterBroadcast.MSG_ROLLEDBACK) {
if (getDEBUG()) {
logger.log(logger.INFO, "Rolling back remote transaction " + tid + " from " + txnHomeBroker);
}
List<Object[]> list = TransactionList.getTransListsAndRemoteTranStates(tid);
if (list == null) {
if (getDEBUG()) {
logger.log(logger.INFO, "Rolling back non-prepared remote transaction " + tid + " from " + txnHomeBroker);
}
List<AckEntry> entries = null;
synchronized (deliveredMessages) {
entries = getPendingConsumerUID(tid);
Iterator<AckEntry> itr = entries.iterator();
AckEntry e = null;
while (itr.hasNext()) {
e = itr.next();
if (deliveredMessages.get(e) == null) {
itr.remove();
continue;
}
if (consumers.get(e.getConsumerUID()) != null) {
itr.remove();
continue;
}
}
if (e != null) {
deliveredMessages.remove(e);
cleanupPendingConsumerUID(e.getConsumerUID(), e.getSysMessageID());
}
}
if (entries.size() == 0) {
logger.log(logger.INFO, br.getKString(br.I_ROLLBACK_REMOTE_TXN_NOT_FOUND, tid, txnHomeBroker));
return;
}
Iterator<AckEntry> itr = entries.iterator();
while (itr.hasNext()) {
AckEntry e = itr.next();
SysMessageID sysid = e.getSysMessageID();
com.sun.messaging.jmq.jmsserver.core.ConsumerUID cuid = e.getConsumerUID();
com.sun.messaging.jmq.jmsserver.core.ConsumerUID suid = e.getStoredConsumerUID();
if (suid == null) {
suid = cuid;
}
// PART
PacketReference ref = DL.get(null, sysid);
if (ref == null) {
if (getDEBUG()) {
logger.log(logger.INFO, "[" + sysid + ":" + cuid + "] reference not found in rolling back remote non-prepared transaction " + tid);
}
continue;
}
ref.removeInDelivery(suid);
ref.getDestination().forwardOrphanMessage(ref, suid);
}
return;
}
// if list == null
TransactionList tl = null;
Object[] oo = null;
for (int li = 0; li < list.size(); li++) {
oo = list.get(li);
tl = (TransactionList) oo[0];
if (!tl.updateRemoteTransactionState(tid, TransactionState.ROLLEDBACK, false, false, true)) {
return;
}
if (tl.getRecoveryRemoteTransactionAcks(tid) != null) {
rollbackRecoveryRemoteTransaction(tl, tid, txnHomeBroker);
}
RemoteTransactionAckEntry tae = tl.getRemoteTransactionAcks(tid);
if (tae == null) {
logger.log(logger.INFO, Globals.getBrokerResources().getKString(BrokerResources.I_NO_NONRECOVERY_TXNACK_TO_ROLLBACK, tid));
} else if (tae.processed()) {
logger.log(logger.INFO, Globals.getBrokerResources().getKString(BrokerResources.I_NO_MORE_TXNACK_TO_ROLLBACK, tid));
} else if (!tae.isLocalRemote()) {
TransactionAcknowledgement[] tas = tae.getAcks();
Set s = new LinkedHashSet();
AckEntry entry = null, value = null;
for (int i = 0; i < tas.length; i++) {
SysMessageID sysid = tas[i].getSysMessageID();
com.sun.messaging.jmq.jmsserver.core.ConsumerUID uid = tas[i].getConsumerUID();
com.sun.messaging.jmq.jmsserver.core.ConsumerUID suid = tas[i].getStoredConsumerUID();
if (suid == null) {
suid = uid;
}
synchronized (deliveredMessages) {
entry = new AckEntry(sysid, uid, null);
value = (AckEntry) deliveredMessages.get(entry);
if (value == null) {
if (getDEBUG()) {
logger.log(logger.INFO, "[" + sysid + ":" + uid + "] not found in rolling back remote transaction " + tid);
}
continue;
}
if (value.getTUID() == null || !value.getTUID().equals(tid)) {
if (getDEBUG()) {
logger.log(logger.INFO, "[" + sysid + ":" + uid + "] with TUID=" + value.getTUID() + ", in confict for rolling back remote transaction " + tid);
}
continue;
}
if (consumers.get(uid) == null) {
deliveredMessages.remove(entry);
cleanupPendingConsumerUID(uid, sysid);
s.add(tas[i]);
} else {
value.setTUID(null);
}
}
}
Iterator itr = s.iterator();
while (itr.hasNext()) {
TransactionAcknowledgement ta = (TransactionAcknowledgement) itr.next();
SysMessageID sysid = ta.getSysMessageID();
com.sun.messaging.jmq.jmsserver.core.ConsumerUID cuid = ta.getConsumerUID();
com.sun.messaging.jmq.jmsserver.core.ConsumerUID suid = ta.getStoredConsumerUID();
if (suid == null) {
suid = cuid;
}
// PART
PacketReference ref = DL.get(null, sysid);
if (ref == null) {
if (getDEBUG()) {
logger.log(logger.INFO, "[" + sysid + ":" + cuid + "] reference not found in rolling back remote transaction " + tid);
}
continue;
}
ref.removeInDelivery(suid);
ref.getDestination().forwardOrphanMessage(ref, suid);
}
}
try {
tl.removeRemoteTransactionAck(tid);
} catch (Exception e) {
logger.log(logger.WARNING, "Unable to remove transaction ack for rolledback transaction " + tid + ": " + e.getMessage());
}
try {
tl.removeRemoteTransactionID(tid, true);
} catch (Exception e) {
logger.log(logger.WARNING, "Unable to remove rolledback remote transaction " + tid + ": " + e.getMessage());
}
}
// for
return;
}
int cLogRecordCount = 0;
ArrayList cLogDstList = null;
ArrayList cLogMsgList = null;
ArrayList cLogIntList = null;
if (ackType == ClusterBroadcast.MSG_ACKNOWLEDGED) {
if (getDEBUG()) {
logger.log(logger.INFO, "Committing remote transaction " + tid + " from " + txnHomeBroker);
}
List<Object[]> list = TransactionList.getTransListsAndRemoteTranStates(tid);
if (list == null) {
throw new BrokerException("Committing remote transaction " + tid + " not found", Status.NOT_FOUND);
}
TransactionList tl = null;
Object[] oo = null;
for (int li = 0; li < list.size(); li++) {
oo = list.get(li);
tl = (TransactionList) oo[0];
if (!tl.updateRemoteTransactionState(tid, TransactionState.COMMITTED, (sysids == null), true, true)) {
if (getDEBUG()) {
logger.log(logger.INFO, "Remote transaction " + tid + " already committed, from " + txnHomeBroker);
}
continue;
}
boolean done = true;
if (tl.getRecoveryRemoteTransactionAcks(tid) != null) {
done = commitRecoveryRemoteTransaction(tl, tid, txnHomeBroker);
}
RemoteTransactionAckEntry tae = tl.getRemoteTransactionAcks(tid);
if (tae == null) {
logger.log(logger.INFO, "No non-recovery transaction acks to process for committing remote transaction " + tid);
} else if (tae.processed()) {
logger.log(logger.INFO, "No more transaction acks to process for committing remote transaction " + tid);
} else if (!tae.isLocalRemote()) {
boolean found = false;
TransactionAcknowledgement[] tas = tae.getAcks();
for (int i = 0; i < tas.length; i++) {
SysMessageID sysid = tas[i].getSysMessageID();
com.sun.messaging.jmq.jmsserver.core.ConsumerUID cuid = tas[i].getConsumerUID();
if (sysids != null && !found) {
if (sysid.equals(sysids[0]) && cuid.equals(cuids[0])) {
found = true;
}
}
String dstName = null;
if (Globals.txnLogEnabled()) {
if (cLogDstList == null) {
cLogDstList = new ArrayList();
cLogMsgList = new ArrayList();
cLogIntList = new ArrayList();
}
PacketReference ref = DL.get(null, sysid);
if (ref != null && !ref.isDestroyed() && !ref.isInvalid()) {
Destination[] ds = DL.getDestination(ref.getPartitionedStore(), ref.getDestinationUID());
Destination dst = ds[0];
dstName = dst.getUniqueName();
}
}
if (acknowledgeMessageFromRemote(ackType, sysid, cuid, optionalProps)) {
if (dstName != null) {
// keep track for consumer txn log
com.sun.messaging.jmq.jmsserver.core.ConsumerUID suid = tas[i].getStoredConsumerUID();
if (suid == null) {
suid = cuid;
}
cLogRecordCount++;
cLogDstList.add(dstName);
cLogMsgList.add(sysid);
cLogIntList.add(suid);
}
} else {
done = false;
}
}
// notify that message acks have been written to store
if (Globals.isNewTxnLogEnabled()) {
if (DL.isPartitionMode()) {
throw new BrokerException("Partition mode not supported if newTxnLog enabled");
}
((TxnLoggingStore) Globals.getStore().getPrimaryPartition()).loggedCommitWrittenToMessageStore(tid, BaseTransaction.REMOTE_TRANSACTION_TYPE);
}
if (sysids != null && !found) {
logger.log(logger.ERROR, "Internal Error: [" + sysids[0] + ":" + cuids[0] + "] not found in remote transaction " + tid);
done = false;
}
}
// tae != null
if (done) {
try {
tl.removeRemoteTransactionAck(tid);
} catch (Exception e) {
logger.logStack(logger.WARNING, "Unable to remove transaction ack for committed remote transaction " + tid, e);
}
try {
tl.removeRemoteTransactionID(tid, true);
} catch (Exception e) {
logger.logStack(logger.WARNING, "Unable to remove committed remote transaction " + tid, e);
}
} else if (Globals.getHAEnabled()) {
throw new BrokerException("Remote transaction processing incomplete, TUID=" + tid);
}
}
// log to txn log if enabled
try {
if (Globals.txnLogEnabled() && (cLogRecordCount > 0)) {
// Log all acks for consuming txn
ByteArrayOutputStream bos = new ByteArrayOutputStream((cLogRecordCount * (32 + SysMessageID.ID_SIZE + 8)) + 12);
DataOutputStream dos = new DataOutputStream(bos);
// Transaction ID (8 bytes)
dos.writeLong(tid.longValue());
// Number of acks (4 bytes)
dos.writeInt(cLogRecordCount);
for (int i = 0; i < cLogRecordCount; i++) {
String dst = (String) cLogDstList.get(i);
// Destination
dos.writeUTF(dst);
SysMessageID sysid = (SysMessageID) cLogMsgList.get(i);
// SysMessageID
sysid.writeID(dos);
long intid = ((com.sun.messaging.jmq.jmsserver.core.ConsumerUID) cLogIntList.get(i)).longValue();
// ConsumerUID
dos.writeLong(intid);
}
dos.close();
bos.close();
// PART
((TxnLoggingStore) Globals.getStore().getPrimaryPartition()).logTxn(TransactionLogType.CONSUME_TRANSACTION, bos.toByteArray());
}
} catch (IOException ex) {
logger.logStack(Logger.ERROR, Globals.getBrokerResources().E_INTERNAL_BROKER_ERROR, "Got exception while writing to transaction log", ex);
throw new BrokerException("Internal Error: Got exception while writing to transaction log", ex);
}
return;
}
throw new BrokerException("acknowledgeMessageFromRemotePriv:Unexpected ack type:" + ackType);
}
use of com.sun.messaging.jmq.jmsserver.core.Destination in project openmq by eclipse-ee4j.
the class MultibrokerRouter method handleJMSMsg.
@Override
public void handleJMSMsg(Packet p, Map<ConsumerUID, Integer> consumers, BrokerAddress sender, boolean sendMsgDeliveredAck) throws BrokerException {
Map<ConsumerUID, Integer> deliveryCnts = consumers;
boolean hasflowcontrol = true;
ArrayList<Consumer> targetVector = new ArrayList<>();
ArrayList ignoreVector = new ArrayList();
// PART
PartitionedStore pstore = Globals.getStore().getPrimaryPartition();
Iterator<Map.Entry<ConsumerUID, Integer>> itr = consumers.entrySet().iterator();
Map.Entry<ConsumerUID, Integer> entry = null;
while (itr.hasNext()) {
entry = itr.next();
ConsumerUID uid = entry.getKey();
Consumer interest = Consumer.getConsumer(uid);
if (interest != null && interest.isValid()) {
// we need the interest for updating the ref
targetVector.add(interest);
ConsumerUID suid = interest.getStoredConsumerUID();
if ((suid == null || suid.equals(uid)) && interest.getSubscription() == null) {
hasflowcontrol = false;
}
} else {
ignoreVector.add(uid);
}
}
if (targetVector.isEmpty()) {
sendIgnoreAck(p.getSysMessageID(), null, sender, ignoreVector);
return;
}
boolean exists = false;
// PART
PacketReference ref = DL.get(null, p.getSysMessageID());
if (ref != null) {
BrokerAddress addr = ref.getBrokerAddress();
if (addr == null || !addr.equals(sender)) {
if (DEBUG) {
logger.log(Logger.INFO, "Remote message " + ref.getSysMessageID() + " home broker " + addr + " changed to " + sender);
}
DL.remoteCheckMessageHomeChange(ref, sender);
if (addr == null) {
Object[] args = { ref.getSysMessageID(), sender, targetVector };
logger.log(logger.WARNING, Globals.getBrokerResources().getKString(BrokerResources.W_IGNORE_REMOTE_MSG_SENT_FROM, args));
ignoreVector.addAll(targetVector);
targetVector.clear();
sendIgnoreAck(p.getSysMessageID(), ref, sender, ignoreVector);
return;
}
}
}
List dsts = null;
// PART
ref = DL.get(null, p.getSysMessageID());
boolean acquiredWriteLock = false;
try {
if (ref != null) {
ref.acquireDestroyRemoteWriteLock();
acquiredWriteLock = true;
if (ref.isInvalid() || ref.isDestroyed()) {
ref.clearDestroyRemoteWriteLock();
acquiredWriteLock = false;
// PART
ref = DL.get(null, p.getSysMessageID());
if (ref != null) {
ref.acquireDestroyRemoteWriteLock();
acquiredWriteLock = true;
}
}
}
if (ref != null) {
if (ref.getBrokerAddress() == null) {
ref.clearDestroyRemoteWriteLock();
acquiredWriteLock = false;
Object[] args = { ref.getSysMessageID(), sender, targetVector };
logger.log(logger.WARNING, Globals.getBrokerResources().getKString(BrokerResources.W_IGNORE_REMOTE_MSG_SENT_FROM, args));
ignoreVector.addAll(targetVector);
targetVector.clear();
sendIgnoreAck(p.getSysMessageID(), ref, sender, ignoreVector);
return;
} else {
exists = true;
ref.setBrokerAddress(sender);
if (p.getRedelivered()) {
ref.overrideRedeliver();
}
}
} else {
// PART
ref = PacketReference.createReference(pstore, p, null);
ref.setBrokerAddress(sender);
}
// consumer
if (sendMsgDeliveredAck) {
for (int i = 0; i < targetVector.size(); i++) {
Consumer c = targetVector.get(i);
// ref.addMessageDeliveredAck(c.getStoredConsumerUID());
ref.addMessageDeliveredAck(c.getConsumerUID());
}
}
try {
if (ref.getDestinationUID().isWildcard()) {
List[] dss = DL.findMatchingIDs(pstore, ref.getDestinationUID());
dsts = dss[0];
} else {
// ok, autocreate the destination if necessary
Destination[] ds = DL.getDestination(pstore, ref.getDestinationUID().getName(), ref.getDestinationUID().isQueue() ? DestType.DEST_TYPE_QUEUE : DestType.DEST_TYPE_TOPIC, true, true);
Destination d = ds[0];
if (d != null) {
dsts = new ArrayList();
dsts.add(d.getDestinationUID());
}
}
if (dsts == null || dsts.isEmpty()) {
ignoreVector.addAll(targetVector);
targetVector.clear();
} else {
if (!exists && !targetVector.isEmpty()) {
ref.setNeverStore(true);
// OK .. we dont need to route .. its already happened
ref.storeRemoteInterests(targetVector, deliveryCnts);
itr = dsts.iterator();
while (itr.hasNext()) {
DestinationUID did = (DestinationUID) itr.next();
Destination[] ds = DL.getDestination(pstore, did);
Destination d = ds[0];
if (DEBUG) {
logger.log(logger.INFO, "Route remote message " + ref + " sent from " + sender + " to destination(s) " + did + " for consumer(s) " + targetVector + " hasflowcontrol=" + hasflowcontrol + ", enforcelimit=" + ENFORCE_REMOTE_DEST_LIMIT);
}
// add to message count
d.acquireQueueRemoteLock();
try {
PacketReference newref = d.getMessage(p.getSysMessageID());
if (newref != null) {
ignoreVector.addAll(targetVector);
targetVector.clear();
Object[] args = { p.getSysMessageID(), "" + d.getDestinationUID(), sender, newref + "[" + newref.getBrokerAddress() + "]" };
logger.log(logger.INFO, Globals.getBrokerResources().getKString(BrokerResources.I_REMOTE_NEW_MSG_ROUTED_ALREADY, args));
break;
}
d.queueMessage(ref, false, ENFORCE_REMOTE_DEST_LIMIT);
} finally {
d.clearQueueRemoteLock();
}
}
} else if (exists) {
ref.addRemoteInterests(targetVector);
}
}
} catch (Exception ex) {
Object[] args = { "" + ref, sender, targetVector };
String emsg = Globals.getBrokerResources().getKString(BrokerResources.W_EXCEPTION_PROCESS_REMOTE_MSG, args);
if (!(ex instanceof BrokerException)) {
logger.logStack(logger.WARNING, emsg, ex);
} else {
BrokerException be = (BrokerException) ex;
int status = be.getStatusCode();
if (status != Status.RESOURCE_FULL && status != Status.ENTITY_TOO_LARGE) {
logger.logStack(logger.WARNING, emsg, ex);
} else {
Object[] args1 = { sender, targetVector };
emsg = Globals.getBrokerResources().getKString(BrokerResources.W_PROCESS_REMOTE_MSG_DST_LIMIT, args1);
int level = Logger.DEBUG;
if (!loggedFullDestsOnHandleJMSMsg.contains(ref.getDestinationUID())) {
level = Logger.WARNING;
loggedFullDestsOnHandleJMSMsg.add(ref.getDestinationUID());
}
logger.log(level, emsg + (level == Logger.DEBUG ? ": " + ex.getMessage() : ""));
}
}
}
} finally {
if (ref != null && acquiredWriteLock) {
ref.clearDestroyRemoteWriteLock();
}
}
// Now deliver the message...
StringBuilder debugString = new StringBuilder();
debugString.append('\n');
int i;
for (i = 0; i < targetVector.size(); i++) {
Consumer interest = targetVector.get(i);
if (!interest.routeMessage(ref, false)) {
// it disappeard on us, take care of it
try {
if (ref.acknowledged(interest.getConsumerUID(), interest.getStoredConsumerUID(), true, false)) {
try {
if (dsts == null) {
continue;
}
itr = dsts.iterator();
while (itr.hasNext()) {
DestinationUID did = (DestinationUID) itr.next();
Destination[] ds = DL.getDestination(pstore, did);
Destination d = ds[0];
d.removeRemoteMessage(ref.getSysMessageID(), RemoveReason.ACKNOWLEDGED, ref);
}
} finally {
ref.postAcknowledgedRemoval();
}
}
} catch (Exception ex) {
logger.log(logger.INFO, "Internal error processing ack", ex);
} finally {
ref.removeRemoteConsumerUID(interest.getStoredConsumerUID(), interest.getConsumerUID());
}
}
if (DEBUG) {
debugString.append("\t" + interest.getConsumerUID() + "\n");
}
}
if (DEBUG) {
logger.log(logger.DEBUGHIGH, "MessageBus: Delivering message to : {0}", debugString.toString());
}
sendIgnoreAck(p.getSysMessageID(), ref, sender, ignoreVector);
}
use of com.sun.messaging.jmq.jmsserver.core.Destination in project openmq by eclipse-ee4j.
the class DBTool method doBackup.
/**
* Backup the JDBC store to filebased backup files.
*/
private void doBackup() throws BrokerException {
if (!Globals.getHAEnabled()) {
throw new BrokerException(br.getKString(BrokerResources.I_HA_NOT_ENABLE, dbmgr.getBrokerID()));
}
// instantiate the file store.
Properties props = System.getProperties();
String clusterID = Globals.getClusterID();
String backupDir = (String) props.get(Globals.IMQ + ".backupdir") + File.separator + clusterID;
logger.logToAll(Logger.INFO, "Backup persistent store for HA cluster " + clusterID);
final FileStore fileStore = new FileStore(backupDir, false);
// for backup, need to clear the store before storing anything.
fileStore.clearAll(false);
JDBCStore jdbcStore = null;
try {
jdbcStore = (JDBCStore) StoreManager.getStore();
/*
* For data that are not broker specific, i.e. properties, change records, consumers, brokers, and sessions, we will
* store those data on the top level directory (a.k.a cluster filestore).
*/
// Properties table
Properties properties = jdbcStore.getAllProperties();
Iterator propItr = properties.entrySet().iterator();
while (propItr.hasNext()) {
Map.Entry entry = (Map.Entry) propItr.next();
fileStore.updateProperty((String) entry.getKey(), entry.getValue(), false);
}
propItr = null;
properties = null;
// Configuration Change Record table
List<ChangeRecordInfo> records = jdbcStore.getAllConfigRecords();
for (int i = 0, len = records.size(); i < len; i++) {
fileStore.storeConfigChangeRecord(records.get(i).getTimestamp(), records.get(i).getRecord(), false);
}
records = null;
// Consumer table
Consumer[] consumerArray = jdbcStore.getAllInterests();
for (int i = 0; i < consumerArray.length; i++) {
fileStore.storeInterest(consumerArray[i], false);
}
consumerArray = null;
// Broker & Session table - store all HABrokerInfo as a property
HashMap bkrMap = jdbcStore.getAllBrokerInfos(true);
List haBrokers = new ArrayList(bkrMap.values());
fileStore.updateProperty(STORE_PROPERTY_HABROKERS, haBrokers, true);
/*
* For each broker in the cluster, we will store broker specific data, destinations, messages, transactions,
* acknowledgements in their own filestore (a.k.a broker filestore).
*/
Iterator bkrItr = haBrokers.iterator();
while (bkrItr.hasNext()) {
// Backup data for each broker
HABrokerInfo bkrInfo = (HABrokerInfo) bkrItr.next();
String brokerID = bkrInfo.getId();
logger.logToAll(Logger.INFO, "Backup persistent data for broker " + brokerID);
FileStore bkrFS = null;
JMSBridgeStore jmsbridgeStore = null;
try {
String instanceRootDir = backupDir + File.separator + brokerID;
bkrFS = new FileStore(instanceRootDir, false);
bkrFS.clearAll(false);
// Destination table.
Destination[] dstArray = jdbcStore.getAllDestinations(brokerID);
for (int i = 0, len = dstArray.length; i < len; i++) {
DestinationUID did = dstArray[i].getDestinationUID();
Destination dst = bkrFS.getDestination(did);
if (dst == null) {
// Store the destination if not found
bkrFS.storeDestination(dstArray[i], false);
}
}
// Storing messages for each destination.
for (int i = 0, len = dstArray.length; i < len; i++) {
Enumeration e = jdbcStore.messageEnumeration(dstArray[i]);
try {
for (; e.hasMoreElements(); ) {
DestinationUID did = dstArray[i].getDestinationUID();
Packet message = (Packet) e.nextElement();
SysMessageID mid = message.getSysMessageID();
// Get interest states for the message; Consumer State table
HashMap stateMap = jdbcStore.getInterestStates(did, mid);
if (stateMap == null || stateMap.isEmpty()) {
bkrFS.storeMessage(did, message, false);
} else {
int size = stateMap.size();
ConsumerUID[] iids = new ConsumerUID[size];
int[] states = new int[size];
Iterator stateItr = stateMap.entrySet().iterator();
int j = 0;
while (stateItr.hasNext()) {
Map.Entry entry = (Map.Entry) stateItr.next();
iids[j] = (ConsumerUID) entry.getKey();
states[j] = ((Integer) entry.getValue()).intValue();
j++;
}
bkrFS.storeMessage(did, message, iids, states, false);
}
}
} finally {
jdbcStore.closeEnumeration(e);
}
}
// Transaction table
Collection txnList = jdbcStore.getTransactions(brokerID);
Iterator txnItr = txnList.iterator();
while (txnItr.hasNext()) {
TransactionUID tid = (TransactionUID) txnItr.next();
TransactionInfo txnInfo = jdbcStore.getTransactionInfo(tid);
TransactionAcknowledgement[] txnAck = jdbcStore.getTransactionAcks(tid);
bkrFS.storeTransaction(tid, txnInfo, false);
for (int i = 0, len = txnAck.length; i < len; i++) {
bkrFS.storeTransactionAck(tid, txnAck[i], false);
}
}
/**
************************************************
* JMSBridge
*************************************************
*/
Properties bp = new Properties();
bp.setProperty("instanceRootDir", instanceRootDir);
bp.setProperty("reset", "true");
bp.setProperty("logdomain", "imqdbmgr");
bp.setProperty("identityName", Globals.getIdentityName());
BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
List bnames = jdbcStore.getJMSBridgesByBroker(brokerID, null);
Collections.sort(bnames);
String bname = null;
String currbname = null;
Iterator itr = bnames.iterator();
while (itr.hasNext()) {
bname = (String) itr.next();
if (currbname == null || !currbname.equals(bname)) {
currbname = bname;
bp.setProperty("jmsbridge", bname);
if (jmsbridgeStore != null) {
jmsbridgeStore.closeJMSBridgeStore();
jmsbridgeStore = null;
}
logger.logToAll(logger.INFO, "Backup JMS bridge " + bname);
jmsbridgeStore = (JMSBridgeStore) BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
List lrs = jdbcStore.getLogRecordsByNameByBroker(bname, brokerID, null);
logger.logToAll(logger.INFO, "\tBackup JMS bridge " + bname + " with " + lrs.size() + " TM log records");
byte[] lr = null;
Iterator itr1 = lrs.iterator();
while (itr1.hasNext()) {
lr = (byte[]) itr1.next();
jmsbridgeStore.storeTMLogRecord(null, lr, currbname, true, null);
}
}
}
} finally {
bkrFS.close();
if (jmsbridgeStore != null) {
jmsbridgeStore.closeJMSBridgeStore();
}
}
}
logger.logToAll(Logger.INFO, "Backup persistent store complete.");
} catch (Throwable ex) {
ex.printStackTrace();
throw new BrokerException(ex.getMessage());
} finally {
assert fileStore != null;
try {
fileStore.close();
} catch (Exception e) {
e.printStackTrace();
}
StoreManager.releaseStore(true);
}
}
Aggregations