Search in sources :

Example 21 with DistributionMessage

use of org.apache.geode.distributed.internal.DistributionMessage in project geode by apache.

the class Connection method runOioReader.

private void runOioReader() {
    InputStream input = null;
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Socket is of type: {}", getSocket().getClass());
        }
        input = new BufferedInputStream(getSocket().getInputStream(), INITIAL_CAPACITY);
    } catch (IOException io) {
        if (stopped || owner.getConduit().getCancelCriterion().isCancelInProgress()) {
            // bug 37520: exit run loop (and thread)
            return;
        }
        logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_UNABLE_TO_GET_INPUT_STREAM), io);
        stopped = true;
    }
    if (!stopped) {
        Assert.assertTrue(owner != null, LocalizedStrings.Connection_OWNER_SHOULD_NOT_BE_NULL.toLocalizedString());
        if (logger.isDebugEnabled()) {
            logger.debug("Starting {}", p2pReaderName());
        }
    }
    byte[] lenbytes = new byte[MSG_HEADER_BYTES];
    final ByteArrayDataInput dis = new ByteArrayDataInput();
    while (!stopped) {
        try {
            if (SystemFailure.getFailure() != null) {
                // Allocate no objects here!
                Socket s = this.socket;
                if (s != null) {
                    try {
                        s.close();
                    } catch (IOException e) {
                    // don't care
                    }
                }
                // throws
                SystemFailure.checkFailure();
            }
            if (this.owner.getConduit().getCancelCriterion().isCancelInProgress()) {
                break;
            }
            int len = 0;
            if (readFully(input, lenbytes, lenbytes.length) < 0) {
                stopped = true;
                continue;
            }
            // long recvNanos = DistributionStats.getStatTime();
            len = ((lenbytes[MSG_HEADER_SIZE_OFFSET] & 0xff) * 0x1000000) + ((lenbytes[MSG_HEADER_SIZE_OFFSET + 1] & 0xff) * 0x10000) + ((lenbytes[MSG_HEADER_SIZE_OFFSET + 2] & 0xff) * 0x100) + (lenbytes[MSG_HEADER_SIZE_OFFSET + 3] & 0xff);
            /* byte msgHdrVersion = */
            calcHdrVersion(len);
            len = calcMsgByteSize(len);
            int msgType = lenbytes[MSG_HEADER_TYPE_OFFSET];
            short msgId = (short) (((lenbytes[MSG_HEADER_ID_OFFSET] & 0xff) << 8) + (lenbytes[MSG_HEADER_ID_OFFSET + 1] & 0xff));
            boolean myDirectAck = (msgType & DIRECT_ACK_BIT) != 0;
            if (myDirectAck) {
                // clear the bit
                msgType &= ~DIRECT_ACK_BIT;
            }
            // Following validation fixes bug 31145
            if (!validMsgType(msgType)) {
                logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_UNKNOWN_P2P_MESSAGE_TYPE_0, Integer.valueOf(msgType)));
                this.readerShuttingDown = true;
                requestClose(LocalizedStrings.Connection_UNKNOWN_P2P_MESSAGE_TYPE_0.toLocalizedString(Integer.valueOf(msgType)));
                break;
            }
            if (logger.isTraceEnabled())
                logger.trace("{} reading {} bytes", conduitIdStr, len);
            byte[] bytes = new byte[len];
            if (readFully(input, bytes, len) < 0) {
                stopped = true;
                continue;
            }
            boolean interrupted = Thread.interrupted();
            try {
                if (this.handshakeRead) {
                    if (msgType == NORMAL_MSG_TYPE) {
                        // DMStats stats = this.owner.getConduit().stats;
                        // long start = DistributionStats.getStatTime();
                        this.owner.getConduit().stats.incMessagesBeingReceived(true, len);
                        dis.initialize(bytes, this.remoteVersion);
                        DistributionMessage msg = null;
                        try {
                            ReplyProcessor21.initMessageRPId();
                            long startSer = this.owner.getConduit().stats.startMsgDeserialization();
                            msg = (DistributionMessage) InternalDataSerializer.readDSFID(dis);
                            this.owner.getConduit().stats.endMsgDeserialization(startSer);
                            if (dis.available() != 0) {
                                logger.warn(LocalizedMessage.create(LocalizedStrings.Connection_MESSAGE_DESERIALIZATION_OF_0_DID_NOT_READ_1_BYTES, new Object[] { msg, Integer.valueOf(dis.available()) }));
                            }
                            // stats.incBatchCopyTime(start);
                            try {
                                // start = DistributionStats.getStatTime();
                                if (!dispatchMessage(msg, len, myDirectAck)) {
                                    continue;
                                }
                            // stats.incBatchSendTime(start);
                            } catch (MemberShunnedException e) {
                                continue;
                            } catch (Exception de) {
                                // bug
                                this.owner.getConduit().getCancelCriterion().checkCancelInProgress(de);
                                // 37101
                                logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_ERROR_DISPATCHING_MESSAGE), de);
                            }
                        } catch (VirtualMachineError err) {
                            SystemFailure.initiateFailure(err);
                            // now, so don't let this thread continue.
                            throw err;
                        } catch (Throwable e) {
                            // Whenever you catch Error or Throwable, you must also
                            // catch VirtualMachineError (see above). However, there is
                            // _still_ a possibility that you are dealing with a cascading
                            // error condition, so you also need to check to see if the JVM
                            // is still usable:
                            SystemFailure.checkFailure();
                            // In particular I want OutOfMem to be caught here
                            if (!myDirectAck) {
                                String reason = LocalizedStrings.Connection_ERROR_DESERIALIZING_MESSAGE.toLocalizedString();
                                sendFailureReply(ReplyProcessor21.getMessageRPId(), reason, e, myDirectAck);
                            }
                            if (e instanceof CancelException) {
                                if (!(e instanceof CacheClosedException)) {
                                    // CacheClosedException; see bug 43543
                                    throw (CancelException) e;
                                }
                            }
                            logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_ERROR_DESERIALIZING_MESSAGE), e);
                        // requestClose();
                        // return;
                        } finally {
                            ReplyProcessor21.clearMessageRPId();
                        }
                    } else if (msgType == CHUNKED_MSG_TYPE) {
                        MsgDestreamer md = obtainMsgDestreamer(msgId, remoteVersion);
                        this.owner.getConduit().stats.incMessagesBeingReceived(md.size() == 0, len);
                        try {
                            md.addChunk(bytes);
                        } catch (IOException ex) {
                            logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_FAILED_HANDLING_CHUNK_MESSAGE), ex);
                        }
                    } else /* (messageType == END_CHUNKED_MSG_TYPE) */
                    {
                        MsgDestreamer md = obtainMsgDestreamer(msgId, remoteVersion);
                        this.owner.getConduit().stats.incMessagesBeingReceived(md.size() == 0, len);
                        try {
                            md.addChunk(bytes);
                        } catch (IOException ex) {
                            logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_FAILED_HANDLING_END_CHUNK_MESSAGE), ex);
                        }
                        DistributionMessage msg = null;
                        int msgLength = 0;
                        String failureMsg = null;
                        Throwable failureEx = null;
                        int rpId = 0;
                        try {
                            msg = md.getMessage();
                        } catch (ClassNotFoundException ex) {
                            this.owner.getConduit().stats.decMessagesBeingReceived(md.size());
                            failureEx = ex;
                            rpId = md.getRPid();
                            logger.warn(LocalizedMessage.create(LocalizedStrings.Connection_CLASSNOTFOUND_DESERIALIZING_MESSAGE_0, ex));
                        } catch (IOException ex) {
                            this.owner.getConduit().stats.decMessagesBeingReceived(md.size());
                            failureMsg = LocalizedStrings.Connection_IOEXCEPTION_DESERIALIZING_MESSAGE.toLocalizedString();
                            failureEx = ex;
                            rpId = md.getRPid();
                            logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_IOEXCEPTION_DESERIALIZING_MESSAGE), failureEx);
                        } catch (InterruptedException ex) {
                            Thread.currentThread().interrupt();
                            // caught by outer try
                            throw ex;
                        } catch (VirtualMachineError err) {
                            SystemFailure.initiateFailure(err);
                            // now, so don't let this thread continue.
                            throw err;
                        } catch (Throwable ex) {
                            // Whenever you catch Error or Throwable, you must also
                            // catch VirtualMachineError (see above). However, there is
                            // _still_ a possibility that you are dealing with a cascading
                            // error condition, so you also need to check to see if the JVM
                            // is still usable:
                            SystemFailure.checkFailure();
                            this.owner.getConduit().stats.decMessagesBeingReceived(md.size());
                            failureMsg = LocalizedStrings.Connection_UNEXPECTED_FAILURE_DESERIALIZING_MESSAGE.toLocalizedString();
                            failureEx = ex;
                            rpId = md.getRPid();
                            logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_UNEXPECTED_FAILURE_DESERIALIZING_MESSAGE), failureEx);
                        } finally {
                            msgLength = md.size();
                            releaseMsgDestreamer(msgId, md);
                        }
                        if (msg != null) {
                            try {
                                if (!dispatchMessage(msg, msgLength, myDirectAck)) {
                                    continue;
                                }
                            } catch (MemberShunnedException e) {
                                continue;
                            } catch (Exception de) {
                                this.owner.getConduit().getCancelCriterion().checkCancelInProgress(de);
                                logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_ERROR_DISPATCHING_MESSAGE), de);
                            } catch (ThreadDeath td) {
                                throw td;
                            } catch (VirtualMachineError err) {
                                SystemFailure.initiateFailure(err);
                                // now, so don't let this thread continue.
                                throw err;
                            } catch (Throwable t) {
                                // Whenever you catch Error or Throwable, you must also
                                // catch VirtualMachineError (see above). However, there is
                                // _still_ a possibility that you are dealing with a cascading
                                // error condition, so you also need to check to see if the JVM
                                // is still usable:
                                SystemFailure.checkFailure();
                                logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_THROWABLE_DISPATCHING_MESSAGE), t);
                            }
                        } else if (failureEx != null) {
                            sendFailureReply(rpId, failureMsg, failureEx, myDirectAck);
                        }
                    }
                } else {
                    dis.initialize(bytes, null);
                    if (!this.isReceiver) {
                        this.replyCode = dis.readUnsignedByte();
                        if (this.replyCode != REPLY_CODE_OK && this.replyCode != REPLY_CODE_OK_WITH_ASYNC_INFO) {
                            Integer replyCodeInteger = Integer.valueOf(this.replyCode);
                            String err = LocalizedStrings.Connection_UNKNOWN_HANDSHAKE_REPLY_CODE_0.toLocalizedString(replyCodeInteger);
                            if (this.replyCode == 0) {
                                // bug 37113
                                if (logger.isDebugEnabled()) {
                                    logger.debug("{} (peer probably departed ungracefully)", err);
                                }
                            } else {
                                logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_UNKNOWN_HANDSHAKE_REPLY_CODE_0, replyCodeInteger));
                            }
                            this.readerShuttingDown = true;
                            requestClose(err);
                            break;
                        }
                        if (this.replyCode == REPLY_CODE_OK_WITH_ASYNC_INFO) {
                            this.asyncDistributionTimeout = dis.readInt();
                            this.asyncQueueTimeout = dis.readInt();
                            this.asyncMaxQueueSize = (long) dis.readInt() * (1024 * 1024);
                            if (this.asyncDistributionTimeout != 0) {
                                logger.info(LocalizedMessage.create(LocalizedStrings.Connection_0_ASYNC_CONFIGURATION_RECEIVED_1, new Object[] { p2pReaderName(), " asyncDistributionTimeout=" + this.asyncDistributionTimeout + " asyncQueueTimeout=" + this.asyncQueueTimeout + " asyncMaxQueueSize=" + (this.asyncMaxQueueSize / (1024 * 1024)) }));
                            }
                            // read the product version ordinal for on-the-fly serialization
                            // transformations (for rolling upgrades)
                            this.remoteVersion = Version.readVersion(dis, true);
                        }
                        notifyHandshakeWaiter(true);
                    } else {
                        byte b = dis.readByte();
                        if (b != 0) {
                            throw new IllegalStateException(LocalizedStrings.Connection_DETECTED_OLD_VERSION_PRE_5_0_1_OF_GEMFIRE_OR_NONGEMFIRE_DURING_HANDSHAKE_DUE_TO_INITIAL_BYTE_BEING_0.toLocalizedString(new Byte(b)));
                        }
                        byte handShakeByte = dis.readByte();
                        if (handShakeByte != HANDSHAKE_VERSION) {
                            throw new IllegalStateException(LocalizedStrings.Connection_DETECTED_WRONG_VERSION_OF_GEMFIRE_PRODUCT_DURING_HANDSHAKE_EXPECTED_0_BUT_FOUND_1.toLocalizedString(new Object[] { new Byte(HANDSHAKE_VERSION), new Byte(handShakeByte) }));
                        }
                        InternalDistributedMember remote = DSFIDFactory.readInternalDistributedMember(dis);
                        setRemoteAddr(remote);
                        Thread.currentThread().setName(LocalizedStrings.Connection_P2P_MESSAGE_READER_FOR_0.toLocalizedString(this.remoteAddr, this.socket.getPort()));
                        this.sharedResource = dis.readBoolean();
                        this.preserveOrder = dis.readBoolean();
                        this.uniqueId = dis.readLong();
                        // read the product version ordinal for on-the-fly serialization
                        // transformations (for rolling upgrades)
                        this.remoteVersion = Version.readVersion(dis, true);
                        int dominoNumber = 0;
                        if (this.remoteVersion == null || (this.remoteVersion.compareTo(Version.GFE_80) >= 0)) {
                            dominoNumber = dis.readInt();
                            if (this.sharedResource) {
                                dominoNumber = 0;
                            }
                            dominoCount.set(dominoNumber);
                            // this.senderName = dis.readUTF();
                            setThreadName(dominoNumber);
                        }
                        if (!this.sharedResource) {
                            if (tipDomino()) {
                                logger.info(LocalizedMessage.create(LocalizedStrings.Connection_THREAD_OWNED_RECEIVER_FORCING_ITSELF_TO_SEND_ON_THREAD_OWNED_SOCKETS));
                            // bug #49565 - if domino count is >= 2 use shared resources.
                            // Also see DistributedCacheOperation#supportsDirectAck
                            } else {
                                // if (dominoNumber < 2){
                                ConnectionTable.threadWantsOwnResources();
                                if (logger.isDebugEnabled()) {
                                    logger.debug("thread-owned receiver with domino count of {} will prefer sending on thread-owned sockets", dominoNumber);
                                }
                            // } else {
                            // ConnectionTable.threadWantsSharedResources();
                            // logger.fine("thread-owned receiver with domino count of " + dominoNumber + "
                            // will prefer shared sockets");
                            }
                            this.conduit.stats.incThreadOwnedReceivers(1L, dominoNumber);
                        }
                        if (logger.isDebugEnabled()) {
                            logger.debug("{} remoteAddr is {} {}", p2pReaderName(), this.remoteAddr, (this.remoteVersion != null ? " (" + this.remoteVersion + ')' : ""));
                        }
                        String authInit = System.getProperty(DistributionConfigImpl.SECURITY_SYSTEM_PREFIX + SECURITY_PEER_AUTH_INIT);
                        boolean isSecure = authInit != null && authInit.length() != 0;
                        if (isSecure) {
                            // ARB: wait till member authentication has been confirmed?
                            if (owner.getConduit().waitForMembershipCheck(this.remoteAddr)) {
                                // fix for bug 33224
                                sendOKHandshakeReply();
                                notifyHandshakeWaiter(true);
                            } else {
                                // ARB: throw exception??
                                notifyHandshakeWaiter(false);
                                logger.warn(LocalizedMessage.create(LocalizedStrings.Connection_0_TIMED_OUT_DURING_A_MEMBERSHIP_CHECK, p2pReaderName()));
                            }
                        } else {
                            // fix for bug 33224
                            sendOKHandshakeReply();
                            notifyHandshakeWaiter(true);
                        }
                    }
                    if (!this.isReceiver && (this.handshakeRead || this.handshakeCancelled)) {
                        if (logger.isDebugEnabled()) {
                            if (this.handshakeRead) {
                                logger.debug("{} handshake has been read {}", p2pReaderName(), this);
                            } else {
                                logger.debug("{} handshake has been cancelled {}", p2pReaderName(), this);
                            }
                        }
                        // Once we have read the handshake the reader can go away
                        break;
                    }
                    continue;
                }
            } catch (InterruptedException e) {
                interrupted = true;
                this.owner.getConduit().getCancelCriterion().checkCancelInProgress(e);
                logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_0_STRAY_INTERRUPT_READING_MESSAGE, p2pReaderName()), e);
                continue;
            } catch (Exception ioe) {
                // bug 37101
                this.owner.getConduit().getCancelCriterion().checkCancelInProgress(ioe);
                if (!stopped) {
                    logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_0_ERROR_READING_MESSAGE, p2pReaderName()), ioe);
                }
                continue;
            } finally {
                if (interrupted) {
                    Thread.currentThread().interrupt();
                }
            }
        } catch (CancelException e) {
            if (logger.isDebugEnabled()) {
                String ccMsg = p2pReaderName() + " Cancelled: " + this;
                if (e.getMessage() != null) {
                    ccMsg += ": " + e.getMessage();
                }
                logger.debug(ccMsg);
            }
            this.readerShuttingDown = true;
            try {
                requestClose(LocalizedStrings.Connection_CACHECLOSED_IN_CHANNEL_READ_0.toLocalizedString(e));
            } catch (Exception ex) {
            }
            this.stopped = true;
        } catch (IOException io) {
            // needed
            boolean closed = isSocketClosed() || "Socket closed".equalsIgnoreCase(io.getMessage());
            // 1.4.2_08
            if (!closed) {
                if (logger.isDebugEnabled() && !isIgnorableIOException(io)) {
                    logger.debug("{} io exception for {}", p2pReaderName(), this, io);
                }
            }
            this.readerShuttingDown = true;
            try {
                requestClose(LocalizedStrings.Connection_IOEXCEPTION_RECEIVED_0.toLocalizedString(io));
            } catch (Exception ex) {
            }
            if (closed) {
                stopped = true;
            } else {
                // sleep a bit to avoid a hot error loop
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    if (this.owner.getConduit().getCancelCriterion().isCancelInProgress()) {
                        return;
                    }
                    break;
                }
            }
        }// IOException
         catch (Exception e) {
            if (this.owner.getConduit().getCancelCriterion().isCancelInProgress()) {
                // bug 37101
                return;
            }
            if (!stopped && !(e instanceof InterruptedException)) {
                logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_0_EXCEPTION_RECEIVED, p2pReaderName()), e);
            }
            if (isSocketClosed()) {
                stopped = true;
            } else {
                this.readerShuttingDown = true;
                try {
                    requestClose(LocalizedStrings.Connection_0_EXCEPTION_RECEIVED.toLocalizedString(e));
                } catch (Exception ex) {
                }
                // sleep a bit to avoid a hot error loop
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CacheClosedException(org.apache.geode.cache.CacheClosedException) ByteArrayDataInput(org.apache.geode.internal.ByteArrayDataInput) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) CancelException(org.apache.geode.CancelException) CancelledKeyException(java.nio.channels.CancelledKeyException) InterruptedIOException(java.io.InterruptedIOException) SocketException(java.net.SocketException) CacheClosedException(org.apache.geode.cache.CacheClosedException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ReplyException(org.apache.geode.distributed.internal.ReplyException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) CancelException(org.apache.geode.CancelException) Socket(java.net.Socket)

Example 22 with DistributionMessage

use of org.apache.geode.distributed.internal.DistributionMessage in project geode by apache.

the class Connection method writeAsync.

private void writeAsync(SocketChannel channel, ByteBuffer buffer, boolean forceAsync, DistributionMessage p_msg, final DMStats stats) throws IOException {
    DistributionMessage msg = p_msg;
    // async/non-blocking
    boolean socketWriteStarted = false;
    long startSocketWrite = 0;
    int retries = 0;
    int totalAmtWritten = 0;
    try {
        synchronized (this.outLock) {
            if (!forceAsync) {
                // check one more time while holding outLock in case a pusher was created
                if (this.asyncQueuingInProgress) {
                    if (addToQueue(buffer, msg, false)) {
                        return;
                    }
                // fall through
                }
            }
            socketWriteStarted = true;
            startSocketWrite = stats.startSocketWrite(false);
            long now = System.currentTimeMillis();
            int waitTime = 1;
            long distributionTimeoutTarget = 0;
            // as soon as we do a non blocking socket write that returns 0
            if (this.asyncDistributionTimeout != 1) {
                distributionTimeoutTarget = now + this.asyncDistributionTimeout;
            }
            long queueTimeoutTarget = now + this.asyncQueueTimeout;
            channel.configureBlocking(false);
            try {
                do {
                    this.owner.getConduit().getCancelCriterion().checkCancelInProgress(null);
                    retries++;
                    int amtWritten;
                    if (FORCE_ASYNC_QUEUE) {
                        amtWritten = 0;
                    } else {
                        amtWritten = channel.write(buffer);
                    }
                    if (amtWritten == 0) {
                        now = System.currentTimeMillis();
                        long timeoutTarget;
                        if (!forceAsync) {
                            if (now > distributionTimeoutTarget) {
                                if (logger.isDebugEnabled()) {
                                    if (distributionTimeoutTarget == 0) {
                                        logger.debug("Starting async pusher to handle async queue because distribution-timeout is 1 and the last socket write would have blocked.");
                                    } else {
                                        long blockedMs = now - distributionTimeoutTarget;
                                        blockedMs += this.asyncDistributionTimeout;
                                        logger.debug("Blocked for {}ms which is longer than the max of {}ms so starting async pusher to handle async queue.", blockedMs, this.asyncDistributionTimeout);
                                    }
                                }
                                stats.incAsyncDistributionTimeoutExceeded();
                                if (totalAmtWritten > 0) {
                                    // we have written part of the msg to the socket buffer
                                    // and we are going to queue the remainder.
                                    // We set msg to null so that will not make
                                    // the partial msg a candidate for conflation.
                                    msg = null;
                                }
                                if (handleBlockedWrite(buffer, msg)) {
                                    return;
                                }
                            }
                            timeoutTarget = distributionTimeoutTarget;
                        } else {
                            boolean disconnectNeeded = false;
                            long curQueuedBytes = this.queuedBytes;
                            if (curQueuedBytes > this.asyncMaxQueueSize) {
                                logger.warn(LocalizedMessage.create(LocalizedStrings.Connection_QUEUED_BYTES_0_EXCEEDS_MAX_OF_1_ASKING_SLOW_RECEIVER_2_TO_DISCONNECT, new Object[] { Long.valueOf(curQueuedBytes), Long.valueOf(this.asyncMaxQueueSize), this.remoteAddr }));
                                stats.incAsyncQueueSizeExceeded(1);
                                disconnectNeeded = true;
                            }
                            if (now > queueTimeoutTarget) {
                                // we have waited long enough
                                // the pusher has been idle too long!
                                long blockedMs = now - queueTimeoutTarget;
                                blockedMs += this.asyncQueueTimeout;
                                logger.warn(LocalizedMessage.create(LocalizedStrings.Connection_BLOCKED_FOR_0_MS_WHICH_IS_LONGER_THAN_THE_MAX_OF_1_MS_ASKING_SLOW_RECEIVER_2_TO_DISCONNECT, new Object[] { Long.valueOf(blockedMs), Integer.valueOf(this.asyncQueueTimeout), this.remoteAddr }));
                                stats.incAsyncQueueTimeouts(1);
                                disconnectNeeded = true;
                            }
                            if (disconnectNeeded) {
                                disconnectSlowReceiver();
                                synchronized (this.outgoingQueue) {
                                    this.asyncQueuingInProgress = false;
                                    // for bug 42330
                                    this.outgoingQueue.notifyAll();
                                }
                                return;
                            }
                            timeoutTarget = queueTimeoutTarget;
                        }
                        {
                            long msToWait = waitTime;
                            long msRemaining = timeoutTarget - now;
                            if (msRemaining > 0) {
                                msRemaining /= 2;
                            }
                            if (msRemaining < msToWait) {
                                msToWait = msRemaining;
                            }
                            if (msToWait <= 0) {
                                Thread.yield();
                            } else {
                                boolean interrupted = Thread.interrupted();
                                try {
                                    Thread.sleep(msToWait);
                                } catch (InterruptedException ex) {
                                    interrupted = true;
                                    this.owner.getConduit().getCancelCriterion().checkCancelInProgress(ex);
                                } finally {
                                    if (interrupted) {
                                        Thread.currentThread().interrupt();
                                    }
                                }
                            }
                        }
                        if (waitTime < MAX_WAIT_TIME) {
                            // double it since it is not yet the max
                            waitTime <<= 1;
                        }
                    } else // amtWritten == 0
                    {
                        totalAmtWritten += amtWritten;
                        // reset queueTimeoutTarget since we made some progress
                        queueTimeoutTarget = System.currentTimeMillis() + this.asyncQueueTimeout;
                        waitTime = 1;
                    }
                } while (buffer.remaining() > 0);
            } finally {
                channel.configureBlocking(true);
            }
        }
    } finally {
        if (socketWriteStarted) {
            if (retries > 0) {
                retries--;
            }
            stats.endSocketWrite(false, startSocketWrite, totalAmtWritten, retries);
        }
    }
}
Also used : DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage)

Example 23 with DistributionMessage

use of org.apache.geode.distributed.internal.DistributionMessage in project geode by apache.

the class JGroupsMessengerJUnitTest method alertMessagesBypassFlowControl.

@Test
public void alertMessagesBypassFlowControl() throws Exception {
    initMocks(false);
    Message jgmsg = new Message();
    DistributionMessage dmsg = mock(DistributionMessage.class);
    when(dmsg.getProcessorType()).thenReturn(DistributionManager.SERIAL_EXECUTOR);
    messenger.setMessageFlags(dmsg, jgmsg);
    assertFalse("expected no_fc to not be set in " + jgmsg.getFlags(), jgmsg.isFlagSet(Message.Flag.NO_FC));
    AlertAppender.setIsAlerting(true);
    try {
        messenger.setMessageFlags(dmsg, jgmsg);
        assertTrue("expected no_fc to be set in " + jgmsg.getFlags(), jgmsg.isFlagSet(Message.Flag.NO_FC));
    } finally {
        AlertAppender.setIsAlerting(false);
    }
}
Also used : DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) JoinRequestMessage(org.apache.geode.distributed.internal.membership.gms.messages.JoinRequestMessage) LeaveRequestMessage(org.apache.geode.distributed.internal.membership.gms.messages.LeaveRequestMessage) InstallViewMessage(org.apache.geode.distributed.internal.membership.gms.messages.InstallViewMessage) JoinResponseMessage(org.apache.geode.distributed.internal.membership.gms.messages.JoinResponseMessage) SerialAckedMessage(org.apache.geode.distributed.internal.SerialAckedMessage) Message(org.jgroups.Message) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) Test(org.junit.Test) MembershipTest(org.apache.geode.test.junit.categories.MembershipTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 24 with DistributionMessage

use of org.apache.geode.distributed.internal.DistributionMessage in project geode by apache.

the class BackupDUnitTest method testBackupStatusCleanedUpAfterFailureOnOneMember.

@Test
public void testBackupStatusCleanedUpAfterFailureOnOneMember() throws Throwable {
    IgnoredException.addIgnoredException("Uncaught exception");
    IgnoredException.addIgnoredException("Stop processing");
    Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final VM vm2 = host.getVM(2);
    // Create an observer that will fail a backup
    // When this member receives a prepare
    DistributionMessageObserver observer = new SerializableDistributionMessageObserver() {

        @Override
        public void beforeProcessMessage(DistributionManager dm, DistributionMessage message) {
            if (message instanceof PrepareBackupRequest) {
                DistributionMessageObserver.setInstance(null);
                IOException exception = new IOException("Backup in progess");
                AdminFailureResponse response = AdminFailureResponse.create(dm, message.getSender(), exception);
                response.setMsgId(((PrepareBackupRequest) message).getMsgId());
                dm.putOutgoing(response);
                throw new RuntimeException("Stop processing");
            }
        }
    };
    vm0.invoke(() -> {
        disconnectFromDS();
        DistributionMessageObserver.setInstance(observer);
    });
    createPersistentRegion(vm0);
    createPersistentRegion(vm1);
    createData(vm0, 0, 5, "A", "region1");
    createData(vm0, 0, 5, "B", "region2");
    try {
        backup(vm2);
        fail("Backup should have failed with in progress exception");
    } catch (Exception expected) {
    // that's ok, hte backup should have failed
    }
    // A second backup should succeed because the observer
    // has been cleared and the backup state should be cleared.
    BackupStatus status = backup(vm2);
    assertEquals(2, status.getBackedUpDiskStores().size());
    assertEquals(Collections.emptySet(), status.getOfflineDiskStores());
}
Also used : AdminFailureResponse(org.apache.geode.internal.admin.remote.AdminFailureResponse) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) VM(org.apache.geode.test.dunit.VM) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) DistributionMessageObserver(org.apache.geode.distributed.internal.DistributionMessageObserver) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) FileNotFoundException(java.io.FileNotFoundException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) IOException(java.io.IOException) PrepareBackupRequest(org.apache.geode.admin.internal.PrepareBackupRequest) BackupStatus(org.apache.geode.admin.BackupStatus) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 25 with DistributionMessage

use of org.apache.geode.distributed.internal.DistributionMessage in project geode by apache.

the class PersistentPartitionedRegionDUnitTest method testCrashDuringBucketGII2.

/**
   * Another test for bug 41436. If the GII source crashes before the GII is complete, we need to
   * make sure that later we can recover redundancy.
   * 
   * In this test case, we bring the GII down before we bring the source back up, to make sure the
   * source still discovers that the GII target is no longer hosting the bucket.
   * 
   * @throws InterruptedException
   */
@Test
public void testCrashDuringBucketGII2() throws InterruptedException {
    IgnoredException.addIgnoredException("PartitionOfflineException");
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    createPR(vm0, 1);
    createData(vm0, 0, 1, "value");
    // Add an observer which will close the cache when the GII starts
    vm0.invoke(new SerializableRunnable("Set crashing observer") {

        public void run() {
            DistributionMessageObserver.setInstance(new DistributionMessageObserver() {

                @Override
                public void beforeProcessMessage(DistributionManager dm, DistributionMessage message) {
                    if (message instanceof RequestImageMessage) {
                        RequestImageMessage rim = (RequestImageMessage) message;
                        if (rim.regionPath.contains("_0")) {
                            DistributionMessageObserver.setInstance(null);
                            getCache().close();
                        }
                    }
                }
            });
        }
    });
    createPR(vm1, 1);
    // Make sure vm1 didn't create the bucket
    assertEquals(Collections.emptySet(), getBucketList(vm1));
    closeCache(vm1);
    AsyncInvocation async0 = createPRAsync(vm0, 1, 0, 113);
    async0.join(500);
    // vm0 should get stuck waiting for vm1 to recover from disk,
    // because vm0 thinks vm1 has the bucket
    assertTrue(async0.isAlive());
    createPR(vm1, 1, 0);
    // Make sure vm0 recovers the bucket
    assertEquals(Collections.singleton(0), getBucketList(vm0));
    // vm1 should satisfy redundancy for the bucket as well
    WaitCriterion ev = new WaitCriterion() {

        public boolean done() {
            return (Collections.singleton(0).equals(getBucketList(vm1)));
        }

        public String description() {
            return null;
        }
    };
    Wait.waitForCriterion(ev, 30 * 1000, 200, true);
    assertEquals(Collections.singleton(0), getBucketList(vm1));
}
Also used : WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) RequestImageMessage(org.apache.geode.internal.cache.InitialImageOperation.RequestImageMessage) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) DistributionMessageObserver(org.apache.geode.distributed.internal.DistributionMessageObserver) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Aggregations

DistributionMessage (org.apache.geode.distributed.internal.DistributionMessage)51 Test (org.junit.Test)34 DistributionManager (org.apache.geode.distributed.internal.DistributionManager)29 DistributionMessageObserver (org.apache.geode.distributed.internal.DistributionMessageObserver)29 VM (org.apache.geode.test.dunit.VM)28 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)28 Host (org.apache.geode.test.dunit.Host)27 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)22 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)13 IOException (java.io.IOException)11 AsyncInvocation (org.apache.geode.test.dunit.AsyncInvocation)11 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)11 Cache (org.apache.geode.cache.Cache)9 IgnoredException (org.apache.geode.test.dunit.IgnoredException)9 DistributedSystemDisconnectedException (org.apache.geode.distributed.DistributedSystemDisconnectedException)8 DataInputStream (java.io.DataInputStream)7 RequestImageMessage (org.apache.geode.internal.cache.InitialImageOperation.RequestImageMessage)7 Properties (java.util.Properties)6 CacheClosedException (org.apache.geode.cache.CacheClosedException)6 Region (org.apache.geode.cache.Region)6