Search in sources :

Example 1 with ByteArrayDataInput

use of org.apache.geode.internal.ByteArrayDataInput in project geode by apache.

the class PutAllPRMessage method fromData.

@Override
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
    super.fromData(in);
    this.bucketId = (int) InternalDataSerializer.readSignedVL(in);
    if ((flags & HAS_BRIDGE_CONTEXT) != 0) {
        this.bridgeContext = DataSerializer.readObject(in);
    }
    this.callbackArg = DataSerializer.readObject(in);
    this.putAllPRDataSize = (int) InternalDataSerializer.readUnsignedVL(in);
    this.putAllPRData = new PutAllEntryData[putAllPRDataSize];
    if (this.putAllPRDataSize > 0) {
        final Version version = InternalDataSerializer.getVersionForDataStreamOrNull(in);
        final ByteArrayDataInput bytesIn = new ByteArrayDataInput();
        for (int i = 0; i < this.putAllPRDataSize; i++) {
            this.putAllPRData[i] = new PutAllEntryData(in, null, i, version, bytesIn);
        }
        boolean hasTags = in.readBoolean();
        if (hasTags) {
            EntryVersionsList versionTags = EntryVersionsList.create(in);
            for (int i = 0; i < this.putAllPRDataSize; i++) {
                this.putAllPRData[i].versionTag = versionTags.get(i);
            }
        }
    }
}
Also used : EntryVersionsList(org.apache.geode.internal.cache.DistributedPutAllOperation.EntryVersionsList) Version(org.apache.geode.internal.Version) ByteArrayDataInput(org.apache.geode.internal.ByteArrayDataInput) PutAllEntryData(org.apache.geode.internal.cache.DistributedPutAllOperation.PutAllEntryData)

Example 2 with ByteArrayDataInput

use of org.apache.geode.internal.ByteArrayDataInput in project geode by apache.

the class DistTxEntryEvent method removeAllFromData.

/**
   * @param in
   * @throws IOException
   * @throws ClassNotFoundException
   */
private void removeAllFromData(DataInput in) throws IOException, ClassNotFoundException {
    int removeAllSize = DataSerializer.readInteger(in);
    final RemoveAllEntryData[] removeAllData = new RemoveAllEntryData[removeAllSize];
    final Version version = InternalDataSerializer.getVersionForDataStreamOrNull(in);
    final ByteArrayDataInput bytesIn = new ByteArrayDataInput();
    for (int i = 0; i < removeAllSize; i++) {
        removeAllData[i] = new RemoveAllEntryData(in, this.eventID, i, version, bytesIn);
    }
    boolean hasTags = in.readBoolean();
    if (hasTags) {
        EntryVersionsList versionTags = EntryVersionsList.create(in);
        for (int i = 0; i < removeAllSize; i++) {
            removeAllData[i].versionTag = versionTags.get(i);
        }
    }
    // TODO DISTTX: release this event
    EntryEventImpl e = EntryEventImpl.create(this.region, Operation.REMOVEALL_DESTROY, null, null, null, true, this.getDistributedMember(), true, true);
    this.removeAllOp = new DistributedRemoveAllOperation(e, removeAllSize, false);
    this.removeAllOp.setRemoveAllEntryData(removeAllData);
}
Also used : EntryVersionsList(org.apache.geode.internal.cache.DistributedPutAllOperation.EntryVersionsList) DistributedRemoveAllOperation(org.apache.geode.internal.cache.DistributedRemoveAllOperation) Version(org.apache.geode.internal.Version) EntryEventImpl(org.apache.geode.internal.cache.EntryEventImpl) RemoveAllEntryData(org.apache.geode.internal.cache.DistributedRemoveAllOperation.RemoveAllEntryData) ByteArrayDataInput(org.apache.geode.internal.ByteArrayDataInput)

Example 3 with ByteArrayDataInput

use of org.apache.geode.internal.ByteArrayDataInput in project geode by apache.

the class Oplog method readKrf.

private boolean readKrf(OplogEntryIdSet deletedIds, boolean recoverValues, boolean recoverValuesSync, Set<Oplog> oplogsNeedingValueRecovery, boolean latestOplog) {
    File f = new File(this.diskFile.getPath() + KRF_FILE_EXT);
    if (!f.exists()) {
        return false;
    }
    if (!getParent().getDiskInitFile().hasKrf(this.oplogId)) {
        logger.info(LocalizedMessage.create(LocalizedStrings.Oplog_REMOVING_INCOMPLETE_KRF, new Object[] { f.getName(), this.oplogId, getParent().getName() }));
        f.delete();
    }
    // Set krfCreated to true since we have a krf.
    this.krfCreated.set(true);
    // so that we don't try to recreate the krf.
    if (recoverValuesSync) {
        return false;
    }
    FileInputStream fis;
    try {
        fis = new FileInputStream(f);
    } catch (FileNotFoundException ignore) {
        return false;
    }
    try {
        if (getParent().isOffline() && !getParent().FORCE_KRF_RECOVERY) {
            return false;
        }
        logger.info(LocalizedMessage.create(LocalizedStrings.DiskRegion_RECOVERING_OPLOG_0_1_2, new Object[] { toString(), f.getAbsolutePath(), getParent().getName() }));
        this.recoverNewEntryId = DiskStoreImpl.INVALID_ID;
        this.recoverModEntryId = DiskStoreImpl.INVALID_ID;
        this.recoverModEntryIdHWM = DiskStoreImpl.INVALID_ID;
        long oplogKeyIdHWM = DiskStoreImpl.INVALID_ID;
        int krfEntryCount = 0;
        DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 1024 * 1024));
        final Version version = getProductVersionIfOld();
        final ByteArrayDataInput in = new ByteArrayDataInput();
        try {
            try {
                validateOpcode(dis, OPLOG_MAGIC_SEQ_ID);
                readOplogMagicSeqRecord(dis, f, OPLOG_TYPE.KRF);
                validateOpcode(dis, OPLOG_DISK_STORE_ID);
                readDiskStoreRecord(dis, f);
            } catch (DiskAccessException ignore) {
                // Failed to read the file. There are two possibilities. Either this
                // file is in old format which does not have a magic seq in the
                // beginning or this is not a valid file at all. Try reading it as a
                // file in old format
                fis.close();
                fis = new FileInputStream(f);
                dis = new DataInputStream(new BufferedInputStream(fis, 1024 * 1024));
                readDiskStoreRecord(dis, f);
            } catch (IllegalStateException ignore) {
                // Failed to read the file. There are two possibilities. Either this
                // is in new format which has a magic seq in the beginning or this is
                // not a valid file at all
                fis.close();
                fis = new FileInputStream(f);
                dis = new DataInputStream(new BufferedInputStream(fis, 1024 * 1024));
                readDiskStoreRecord(dis, f);
            }
            readGemfireVersionRecord(dis, f);
            readTotalCountRecord(dis, f);
            readRVVRecord(dis, f, false, latestOplog);
            long lastOffset = 0;
            byte[] keyBytes = DataSerializer.readByteArray(dis);
            while (keyBytes != null) {
                byte userBits = dis.readByte();
                int valueLength = InternalDataSerializer.readArrayLength(dis);
                byte[] valueBytes = null;
                long drId = DiskInitFile.readDiskRegionID(dis);
                DiskRecoveryStore drs = getOplogSet().getCurrentlyRecovering(drId);
                // read version
                VersionTag tag = null;
                if (EntryBits.isWithVersions(userBits)) {
                    tag = readVersionsFromOplog(dis);
                    if (drs != null && !drs.getDiskRegionView().getFlags().contains(DiskRegionFlag.IS_WITH_VERSIONING)) {
                        // 50044 Remove version tag from entry if we don't want versioning
                        // for this region
                        tag = null;
                        userBits = EntryBits.setWithVersions(userBits, false);
                    } else {
                        // Update the RVV with the new entry
                        if (drs != null) {
                            drs.recordRecoveredVersionTag(tag);
                        }
                    }
                }
                long oplogKeyId = InternalDataSerializer.readVLOld(dis);
                long oplogOffset;
                if (EntryBits.isAnyInvalid(userBits) || EntryBits.isTombstone(userBits)) {
                    oplogOffset = -1;
                } else {
                    oplogOffset = lastOffset + InternalDataSerializer.readVLOld(dis);
                    lastOffset = oplogOffset;
                }
                if (oplogKeyId > oplogKeyIdHWM) {
                    oplogKeyIdHWM = oplogKeyId;
                }
                if (okToSkipModifyRecord(deletedIds, drId, drs, oplogKeyId, true, tag).skip()) {
                    if (logger.isTraceEnabled(LogMarker.PERSIST_RECOVERY)) {
                        logger.trace(LogMarker.PERSIST_RECOVERY, "readNewEntry skipping oplogKeyId=<{}> drId={} userBits={} oplogOffset={} valueLen={}", oplogKeyId, drId, userBits, oplogOffset, valueLength);
                    }
                    this.stats.incRecoveryRecordsSkipped();
                    incSkipped();
                } else {
                    if (EntryBits.isAnyInvalid(userBits)) {
                        if (EntryBits.isInvalid(userBits)) {
                            valueBytes = DiskEntry.INVALID_BYTES;
                        } else {
                            valueBytes = DiskEntry.LOCAL_INVALID_BYTES;
                        }
                    } else if (EntryBits.isTombstone(userBits)) {
                        valueBytes = DiskEntry.TOMBSTONE_BYTES;
                    }
                    Object key = deserializeKey(keyBytes, version, in);
                    {
                        Object oldValue = getRecoveryMap().put(oplogKeyId, key);
                        if (oldValue != null) {
                            throw new AssertionError(LocalizedStrings.Oplog_DUPLICATE_CREATE.toLocalizedString(oplogKeyId));
                        }
                    }
                    DiskEntry de = drs.getDiskEntry(key);
                    if (de == null) {
                        if (logger.isTraceEnabled(LogMarker.PERSIST_RECOVERY)) {
                            logger.trace(LogMarker.PERSIST_RECOVERY, "readNewEntry oplogKeyId=<{}> drId={} userBits={} oplogOffset={} valueLen={}", oplogKeyId, drId, userBits, oplogOffset, valueLength);
                        }
                        DiskEntry.RecoveredEntry re = createRecoveredEntry(valueBytes, valueLength, userBits, getOplogId(), oplogOffset, oplogKeyId, false, version, in);
                        if (tag != null) {
                            re.setVersionTag(tag);
                        }
                        initRecoveredEntry(drs.getDiskRegionView(), drs.initializeRecoveredEntry(key, re));
                        drs.getDiskRegionView().incRecoveredEntryCount();
                        this.stats.incRecoveredEntryCreates();
                        krfEntryCount++;
                    } else {
                        DiskId curdid = de.getDiskId();
                        // assert curdid.getOplogId() != getOplogId();
                        if (logger.isTraceEnabled(LogMarker.PERSIST_RECOVERY)) {
                            logger.trace(LogMarker.PERSIST_RECOVERY, "ignore readNewEntry because getOplogId()={} != curdid.getOplogId()={} for drId={} key={}", getOplogId(), curdid.getOplogId(), drId, key);
                        }
                    }
                }
                keyBytes = DataSerializer.readByteArray(dis);
            }
            // while
            setRecoverNewEntryId(oplogKeyIdHWM);
        } catch (IOException ex) {
            try {
                fis.close();
                fis = null;
            } catch (IOException ignore) {
            }
            throw new DiskAccessException("Unable to recover from krf file for oplogId=" + oplogId + ", file=" + f.getName() + ". This file is corrupt, but may be safely deleted.", ex, getParent());
        }
        if (recoverValues && krfEntryCount > 0) {
            oplogsNeedingValueRecovery.add(this);
        // TODO optimize this code and make it async
        // It should also honor the lru limit
        // The fault in logic might not work until
        // the region is actually created.
        // Instead of reading the crf it might be better to iterate the live
        // entry
        // list that was built during KRF recovery. Just fault values in until
        // we
        // hit the LRU limit (if we have one). Only fault in values for entries
        // recovered from disk that are still in this oplog.
        // Defer faulting in values until all oplogs for the ds have been
        // recovered.
        }
    } finally {
        // fix for bug 42776
        if (fis != null) {
            try {
                fis.close();
                fis = null;
            } catch (IOException ignore) {
            }
        }
    }
    return true;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ByteArrayDataInput(org.apache.geode.internal.ByteArrayDataInput) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) Version(org.apache.geode.internal.Version) DiskRecoveryStore(org.apache.geode.internal.cache.persistence.DiskRecoveryStore) DiskAccessException(org.apache.geode.cache.DiskAccessException) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) StoredObject(org.apache.geode.internal.offheap.StoredObject) File(java.io.File) UninterruptibleRandomAccessFile(org.apache.geode.internal.cache.persistence.UninterruptibleRandomAccessFile)

Example 4 with ByteArrayDataInput

use of org.apache.geode.internal.ByteArrayDataInput in project geode by apache.

the class RemotePutAllMessage method fromData.

@Override
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
    super.fromData(in);
    this.eventId = (EventID) DataSerializer.readObject(in);
    this.callbackArg = DataSerializer.readObject(in);
    this.posDup = (flags & POS_DUP) != 0;
    if ((flags & HAS_BRIDGE_CONTEXT) != 0) {
        this.bridgeContext = DataSerializer.readObject(in);
    }
    this.skipCallbacks = (flags & SKIP_CALLBACKS) != 0;
    this.putAllDataCount = (int) InternalDataSerializer.readUnsignedVL(in);
    this.putAllData = new PutAllEntryData[putAllDataCount];
    if (this.putAllDataCount > 0) {
        final Version version = InternalDataSerializer.getVersionForDataStreamOrNull(in);
        final ByteArrayDataInput bytesIn = new ByteArrayDataInput();
        for (int i = 0; i < this.putAllDataCount; i++) {
            this.putAllData[i] = new PutAllEntryData(in, this.eventId, i, version, bytesIn);
        }
        boolean hasTags = in.readBoolean();
        if (hasTags) {
            EntryVersionsList versionTags = EntryVersionsList.create(in);
            for (int i = 0; i < this.putAllDataCount; i++) {
                this.putAllData[i].versionTag = versionTags.get(i);
            }
        }
    }
}
Also used : EntryVersionsList(org.apache.geode.internal.cache.DistributedPutAllOperation.EntryVersionsList) Version(org.apache.geode.internal.Version) ByteArrayDataInput(org.apache.geode.internal.ByteArrayDataInput) PutAllEntryData(org.apache.geode.internal.cache.DistributedPutAllOperation.PutAllEntryData)

Example 5 with ByteArrayDataInput

use of org.apache.geode.internal.ByteArrayDataInput 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)

Aggregations

ByteArrayDataInput (org.apache.geode.internal.ByteArrayDataInput)15 Version (org.apache.geode.internal.Version)8 EntryVersionsList (org.apache.geode.internal.cache.DistributedPutAllOperation.EntryVersionsList)6 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)5 BufferedInputStream (java.io.BufferedInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DataOutputStream (java.io.DataOutputStream)3 IOException (java.io.IOException)3 InterruptedIOException (java.io.InterruptedIOException)3 VersionedDataOutputStream (org.apache.geode.internal.VersionedDataOutputStream)3 PutAllEntryData (org.apache.geode.internal.cache.DistributedPutAllOperation.PutAllEntryData)3 RemoveAllEntryData (org.apache.geode.internal.cache.DistributedRemoveAllOperation.RemoveAllEntryData)3 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)3 Test (org.junit.Test)3 DataInputStream (java.io.DataInputStream)2 FileInputStream (java.io.FileInputStream)2 CancelException (org.apache.geode.CancelException)2 DiskAccessException (org.apache.geode.cache.DiskAccessException)2 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)2 EntryEventImpl (org.apache.geode.internal.cache.EntryEventImpl)2