use of org.apache.catalina.tribes.UniqueId in project tomcat70 by apache.
the class NonBlockingCoordinator method createElectionMsg.
private CoordinationMessage createElectionMsg(MemberImpl local, MemberImpl[] others, MemberImpl leader) {
Membership m = new Membership(local, AbsoluteOrder.comp, true);
Arrays.fill(m, others);
MemberImpl[] mbrs = m.getMembers();
m.reset();
CoordinationMessage msg = new CoordinationMessage(leader, local, mbrs, new UniqueId(UUIDGenerator.randomUUID(true)), COORD_REQUEST);
return msg;
}
use of org.apache.catalina.tribes.UniqueId in project tomcat by apache.
the class NioReplicationTask method drainChannel.
/**
* The actual code which drains the channel associated with
* the given key. This method assumes the key has been
* modified prior to invocation to turn off selection
* interest in OP_READ. When this method completes it
* re-enables OP_READ and calls wakeup() on the selector
* so the selector will resume watching this channel.
* @param key The key to process
* @param reader The reader
* @throws Exception IO error
*/
protected void drainChannel(final SelectionKey key, ObjectReader reader) throws Exception {
reader.access();
ReadableByteChannel channel = (ReadableByteChannel) key.channel();
int count = -1;
// make buffer empty
buffer.clear();
SocketAddress saddr = null;
if (channel instanceof SocketChannel) {
// loop while data available, channel is non-blocking
while ((count = channel.read(buffer)) > 0) {
// make buffer readable
buffer.flip();
if (buffer.hasArray()) {
reader.append(buffer.array(), 0, count, false);
} else {
reader.append(buffer, count, false);
}
// make buffer empty
buffer.clear();
// do we have at least one package?
if (reader.hasPackage()) {
break;
}
}
} else if (channel instanceof DatagramChannel) {
DatagramChannel dchannel = (DatagramChannel) channel;
saddr = dchannel.receive(buffer);
// make buffer readable
buffer.flip();
if (buffer.hasArray()) {
reader.append(buffer.array(), 0, buffer.limit() - buffer.position(), false);
} else {
reader.append(buffer, buffer.limit() - buffer.position(), false);
}
// make buffer empty
buffer.clear();
// did we get a package
count = reader.hasPackage() ? 1 : -1;
}
int pkgcnt = reader.count();
if (count < 0 && pkgcnt == 0) {
// end of stream, and no more packages to process
remoteEof(key);
return;
}
ChannelMessage[] msgs = pkgcnt == 0 ? ChannelData.EMPTY_DATA_ARRAY : reader.execute();
// register to read new data, before we send it off to avoid dead locks
registerForRead(key, reader);
for (ChannelMessage msg : msgs) {
/**
* Use send ack here if you want to ack the request to the remote
* server before completing the request
* This is considered an asynchronous request
*/
if (ChannelData.sendAckAsync(msg.getOptions())) {
sendAck(key, (WritableByteChannel) channel, Constants.ACK_COMMAND, saddr);
}
try {
if (Logs.MESSAGES.isTraceEnabled()) {
try {
Logs.MESSAGES.trace("NioReplicationThread - Received msg:" + new UniqueId(msg.getUniqueId()) + " at " + new java.sql.Timestamp(System.currentTimeMillis()));
} catch (Throwable t) {
}
}
// process the message
getCallback().messageDataReceived(msg);
/**
* Use send ack here if you want the request to complete on this
* server before sending the ack to the remote server
* This is considered a synchronized request
*/
if (ChannelData.sendAckSync(msg.getOptions())) {
sendAck(key, (WritableByteChannel) channel, Constants.ACK_COMMAND, saddr);
}
} catch (RemoteProcessException e) {
if (log.isDebugEnabled()) {
log.error(sm.getString("nioReplicationTask.process.clusterMsg.failed"), e);
}
if (ChannelData.sendAckSync(msg.getOptions())) {
sendAck(key, (WritableByteChannel) channel, Constants.FAIL_ACK_COMMAND, saddr);
}
} catch (Exception e) {
log.error(sm.getString("nioReplicationTask.process.clusterMsg.failed"), e);
if (ChannelData.sendAckSync(msg.getOptions())) {
sendAck(key, (WritableByteChannel) channel, Constants.FAIL_ACK_COMMAND, saddr);
}
}
if (getUseBufferPool()) {
BufferPool.getBufferPool().returnBuffer(msg.getMessage());
msg.setMessage(null);
}
}
if (count < 0) {
remoteEof(key);
}
}
use of org.apache.catalina.tribes.UniqueId in project tomcat by apache.
the class ParallelNioSender method doLoop.
private SendResult doLoop(long selectTimeOut, int maxAttempts, boolean waitForAck, ChannelMessage msg) throws ChannelException {
SendResult result = new SendResult();
int selectedKeys;
try {
selectedKeys = state.selector.select(selectTimeOut);
} catch (IOException ioe) {
throw new ChannelException(sm.getString("parallelNioSender.send.failed"), ioe);
}
if (selectedKeys == 0) {
return result;
}
Iterator<SelectionKey> it = state.selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey sk = it.next();
it.remove();
int readyOps = sk.readyOps();
sk.interestOps(sk.interestOps() & ~readyOps);
NioSender sender = (NioSender) sk.attachment();
try {
if (sender.process(sk, waitForAck)) {
sender.setComplete(true);
result.complete(sender);
if (Logs.MESSAGES.isTraceEnabled()) {
Logs.MESSAGES.trace("ParallelNioSender - Sent msg:" + new UniqueId(msg.getUniqueId()) + " at " + new java.sql.Timestamp(System.currentTimeMillis()) + " to " + sender.getDestination().getName());
}
SenderState.getSenderState(sender.getDestination()).setReady();
}
// end if
} catch (Exception x) {
if (log.isTraceEnabled()) {
log.trace("Error while processing send to " + sender.getDestination().getName(), x);
}
SenderState state = SenderState.getSenderState(sender.getDestination());
int attempt = sender.getAttempt() + 1;
boolean retry = (attempt <= maxAttempts && maxAttempts > 0);
synchronized (state) {
// sk.cancel();
if (state.isSuspect()) {
state.setFailing();
}
if (state.isReady()) {
state.setSuspect();
if (retry) {
log.warn(sm.getString("parallelNioSender.send.fail.retrying", sender.getDestination().getName()));
} else {
log.warn(sm.getString("parallelNioSender.send.fail", sender.getDestination().getName()), x);
}
}
}
if (!isConnected()) {
log.warn(sm.getString("parallelNioSender.sender.disconnected.notRetry", sender.getDestination().getName()));
ChannelException cx = new ChannelException(sm.getString("parallelNioSender.sender.disconnected.sendFailed"), x);
cx.addFaultyMember(sender.getDestination(), x);
result.failed(cx);
break;
}
byte[] data = sender.getMessage();
if (retry) {
try {
sender.disconnect();
sender.connect();
sender.setAttempt(attempt);
sender.setMessage(data);
} catch (Exception ignore) {
state.setFailing();
}
} else {
ChannelException cx = new ChannelException(sm.getString("parallelNioSender.sendFailed.attempt", Integer.toString(sender.getAttempt()), Integer.toString(maxAttempts)), x);
cx.addFaultyMember(sender.getDestination(), x);
result.failed(cx);
}
// end if
}
}
return result;
}
use of org.apache.catalina.tribes.UniqueId in project tomcat by apache.
the class NonBlockingCoordinator method createElectionMsg.
private CoordinationMessage createElectionMsg(Member local, Member[] others, Member leader) {
Membership m = new Membership(local, AbsoluteOrder.comp, true);
Arrays.fill(m, others);
Member[] mbrs = m.getMembers();
m.reset();
CoordinationMessage msg = new CoordinationMessage(leader, local, mbrs, new UniqueId(UUIDGenerator.randomUUID(true)), COORD_REQUEST);
return msg;
}
use of org.apache.catalina.tribes.UniqueId in project tomcat by apache.
the class TwoPhaseCommitInterceptor method messageReceived.
@Override
public void messageReceived(ChannelMessage msg) {
if (okToProcess(msg.getOptions())) {
if (msg.getMessage().getLength() == (START_DATA.length + msg.getUniqueId().length + END_DATA.length) && Arrays.contains(msg.getMessage().getBytesDirect(), 0, START_DATA, 0, START_DATA.length) && Arrays.contains(msg.getMessage().getBytesDirect(), START_DATA.length + msg.getUniqueId().length, END_DATA, 0, END_DATA.length)) {
UniqueId id = new UniqueId(msg.getMessage().getBytesDirect(), START_DATA.length, msg.getUniqueId().length);
MapEntry original = messages.get(id);
if (original != null) {
super.messageReceived(original.msg);
messages.remove(id);
} else {
log.warn(sm.getString("twoPhaseCommitInterceptor.originalMessage.missing", Arrays.toString(id.getBytes())));
}
} else {
UniqueId id = new UniqueId(msg.getUniqueId());
MapEntry entry = new MapEntry((ChannelMessage) msg.deepclone(), id, System.currentTimeMillis());
messages.put(id, entry);
}
} else {
super.messageReceived(msg);
}
}
Aggregations