Search in sources :

Example 16 with HeapDataOutputStream

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

the class JGroupsMessenger method createJGMessage.

/**
   * This is the constructor to use to create a JGroups message holding a GemFire
   * DistributionMessage. It sets the appropriate flags in the Message and properly serializes the
   * DistributionMessage for the recipient's product version
   * 
   * @param gfmsg the DistributionMessage
   * @param src the sender address
   * @param version the version of the recipient
   * @return the new message
   */
Message createJGMessage(DistributionMessage gfmsg, JGAddress src, short version) {
    if (gfmsg instanceof DirectReplyMessage) {
        ((DirectReplyMessage) gfmsg).registerProcessor();
    }
    Message msg = new Message();
    msg.setDest(null);
    msg.setSrc(src);
    setMessageFlags(gfmsg, msg);
    try {
        long start = services.getStatistics().startMsgSerialization();
        HeapDataOutputStream out_stream = new HeapDataOutputStream(Version.fromOrdinalOrCurrent(version));
        Version.CURRENT.writeOrdinal(out_stream, true);
        if (encrypt != null) {
            out_stream.writeBoolean(true);
            writeEncryptedMessage(gfmsg, version, out_stream);
        } else {
            out_stream.writeBoolean(false);
            serializeMessage(gfmsg, out_stream);
        }
        msg.setBuffer(out_stream.toByteArray());
        services.getStatistics().endMsgSerialization(start);
    } catch (IOException | GemFireIOException ex) {
        logger.warn("Error serializing message", ex);
        if (ex instanceof GemFireIOException) {
            throw (GemFireIOException) ex;
        } else {
            GemFireIOException ioe = new GemFireIOException("Error serializing message");
            ioe.initCause(ex);
            throw ioe;
        }
    } catch (Exception ex) {
        logger.warn("Error serializing message", ex);
        GemFireIOException ioe = new GemFireIOException("Error serializing message");
        ioe.initCause(ex.getCause());
        throw ioe;
    }
    return msg;
}
Also used : DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) JoinRequestMessage(org.apache.geode.distributed.internal.membership.gms.messages.JoinRequestMessage) DirectReplyMessage(org.apache.geode.internal.cache.DirectReplyMessage) JoinResponseMessage(org.apache.geode.distributed.internal.membership.gms.messages.JoinResponseMessage) LocalizedMessage(org.apache.geode.internal.logging.log4j.LocalizedMessage) Message(org.jgroups.Message) HighPriorityDistributionMessage(org.apache.geode.distributed.internal.HighPriorityDistributionMessage) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) GemFireIOException(org.apache.geode.GemFireIOException) GemFireIOException(org.apache.geode.GemFireIOException) IOException(java.io.IOException) MemberShunnedException(org.apache.geode.internal.tcp.MemberShunnedException) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ForcedDisconnectException(org.apache.geode.ForcedDisconnectException) GemFireIOException(org.apache.geode.GemFireIOException) SystemConnectException(org.apache.geode.SystemConnectException) GemFireConfigException(org.apache.geode.GemFireConfigException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DirectReplyMessage(org.apache.geode.internal.cache.DirectReplyMessage)

Example 17 with HeapDataOutputStream

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

the class CachedDeserializableFactory method calcSerializedSize.

/**
   * Return an estimate of the number of bytes this object will consume when serialized. This is the
   * number of bytes that will be written on the wire including the 4 bytes needed to encode the
   * length.
   */
public static int calcSerializedSize(Object o) {
    int result;
    if (o instanceof byte[]) {
        result = getByteSize((byte[]) o) - Sizeable.PER_OBJECT_OVERHEAD;
    } else if (o instanceof byte[][]) {
        result = getArrayOfBytesSize((byte[][]) o, false);
    } else if (o instanceof CachedDeserializable) {
        result = ((CachedDeserializable) o).getSizeInBytes() + 4 - overhead();
    } else if (o instanceof Sizeable) {
        result = ((Sizeable) o).getSizeInBytes() + 4;
    } else if (o instanceof HeapDataOutputStream) {
        result = ((HeapDataOutputStream) o).size() + 4;
    } else {
        result = 4;
        NullDataOutputStream dos = new NullDataOutputStream();
        try {
            DataSerializer.writeObject(o, dos);
            result += dos.size();
        } catch (IOException ex) {
            RuntimeException ex2 = new IllegalArgumentException(LocalizedStrings.CachedDeserializableFactory_COULD_NOT_CALCULATE_SIZE_OF_OBJECT.toLocalizedString());
            ex2.initCause(ex);
            throw ex2;
        }
    }
    // RuntimeException("STACK"));
    return result;
}
Also used : Sizeable(org.apache.geode.internal.cache.lru.Sizeable) NullDataOutputStream(org.apache.geode.internal.NullDataOutputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) IOException(java.io.IOException)

Example 18 with HeapDataOutputStream

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

the class DiskInitFile method writeIFRecord.

private void writeIFRecord(byte b, DiskRegionView dr, String s) {
    assert lock.isHeldByCurrentThread();
    try {
        int hdosSize = 1 + DR_ID_MAX_BYTES + estimateByteSize(s) + 1;
        if (hdosSize < 32) {
            hdosSize = 32;
        }
        HeapDataOutputStream hdos = new HeapDataOutputStream(hdosSize, Version.CURRENT);
        hdos.write(b);
        writeDiskRegionID(hdos, dr.getId());
        hdos.writeUTF(s);
        hdos.write(END_OF_RECORD_ID);
        writeIFRecord(hdos, true);
    } catch (IOException ex) {
        DiskAccessException dae = new DiskAccessException(LocalizedStrings.DiskInitFile_FAILED_INIT_FILE_WRITE_BECAUSE_0.toLocalizedString(ex), this.parent);
        if (!this.compactInProgress) {
            this.parent.handleDiskAccessException(dae);
        }
        throw dae;
    }
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) DiskAccessException(org.apache.geode.cache.DiskAccessException) IOException(java.io.IOException)

Example 19 with HeapDataOutputStream

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

the class DiskInitFile method writeClearRecord.

/**
   * Write a clear with an RVV record.
   */
private void writeClearRecord(DiskRegionView dr, RegionVersionVector rvv) {
    try {
        HeapDataOutputStream hdos = new HeapDataOutputStream(32, Version.CURRENT);
        hdos.write(IFREC_CLEAR_REGION_WITH_RVV_ID);
        writeDiskRegionID(hdos, dr.getId());
        // We only need the memberToVersionMap for clear purposes
        Map<DiskStoreID, RegionVersionHolder> memberToVersion = rvv.getMemberToVersion();
        hdos.writeInt(memberToVersion.size());
        for (Map.Entry<DiskStoreID, RegionVersionHolder> entry : memberToVersion.entrySet()) {
            InternalDataSerializer.invokeToData(entry.getKey(), hdos);
            synchronized (entry.getValue()) {
                InternalDataSerializer.invokeToData(entry.getValue(), hdos);
            }
        }
        hdos.write(END_OF_RECORD_ID);
        // don't do stats for these small records
        writeIFRecord(hdos, false);
    } catch (IOException ex) {
        DiskAccessException dae = new DiskAccessException(LocalizedStrings.DiskInitFile_FAILED_INIT_FILE_WRITE_BECAUSE_0.toLocalizedString(ex), this.parent);
        if (!this.compactInProgress) {
            this.parent.handleDiskAccessException(dae);
        }
        throw dae;
    }
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) DiskAccessException(org.apache.geode.cache.DiskAccessException) RegionVersionHolder(org.apache.geode.internal.cache.versions.RegionVersionHolder) IOException(java.io.IOException) DiskStoreID(org.apache.geode.internal.cache.persistence.DiskStoreID) Map(java.util.Map) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap)

Example 20 with HeapDataOutputStream

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

the class DiskInitFile method writeRevokedMember.

private void writeRevokedMember(PersistentMemberPattern revoked) {
    try {
        HeapDataOutputStream hdos = new HeapDataOutputStream(32, Version.CURRENT);
        hdos.write(IFREC_REVOKE_DISK_STORE_ID);
        InternalDataSerializer.invokeToData(revoked, hdos);
        hdos.write(END_OF_RECORD_ID);
        // don't do stats for these small records
        writeIFRecord(hdos, false);
    } catch (IOException ex) {
        DiskAccessException dae = new DiskAccessException(LocalizedStrings.DiskInitFile_FAILED_INIT_FILE_WRITE_BECAUSE_0.toLocalizedString(ex), this.parent);
        if (!this.compactInProgress) {
            this.parent.handleDiskAccessException(dae);
        }
        throw dae;
    }
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) DiskAccessException(org.apache.geode.cache.DiskAccessException) IOException(java.io.IOException)

Aggregations

HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)134 Test (org.junit.Test)55 IOException (java.io.IOException)40 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)36 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)33 DataInputStream (java.io.DataInputStream)29 ByteArrayInputStream (java.io.ByteArrayInputStream)23 UnitTest (org.apache.geode.test.junit.categories.UnitTest)15 DiskAccessException (org.apache.geode.cache.DiskAccessException)12 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)11 PdxSerializerObject (org.apache.geode.internal.PdxSerializerObject)10 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)8 Version (org.apache.geode.internal.Version)8 DataInput (java.io.DataInput)7 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)7 OutputStream (java.io.OutputStream)6 Properties (java.util.Properties)6 ByteBuffer (java.nio.ByteBuffer)5 HashMap (java.util.HashMap)5 InternalGemFireException (org.apache.geode.InternalGemFireException)5