Search in sources :

Example 6 with ToDataException

use of org.apache.geode.ToDataException in project geode by apache.

the class DirectReplySender method putOutgoing.

public Set putOutgoing(DistributionMessage msg) {
    Assert.assertTrue(!this.sentReply, "Trying to reply twice to a message");
    // Using an ArrayList, rather than Collections.singletonList here, because the MsgStreamer
    // mutates the list when it has exceptions.
    // fix for bug #42199 - cancellation check
    this.conn.getConduit().getDM().getCancelCriterion().checkCancelInProgress(null);
    if (logger.isTraceEnabled(LogMarker.DM)) {
        logger.trace(LogMarker.DM, "Sending a direct reply {} to {}", msg, conn.getRemoteAddress());
    }
    ArrayList<Connection> conns = new ArrayList<Connection>(1);
    conns.add(conn);
    MsgStreamer ms = (MsgStreamer) MsgStreamer.create(conns, msg, false, DUMMY_STATS);
    try {
        ms.writeMessage();
        ConnectExceptions ce = ms.getConnectExceptions();
        if (ce != null && !ce.getMembers().isEmpty()) {
            Assert.assertTrue(ce.getMembers().size() == 1);
            logger.warn(LocalizedMessage.create(LocalizedStrings.DirectChannel_FAILURE_SENDING_DIRECT_REPLY, ce.getMembers().iterator().next()));
            return Collections.singleton(ce.getMembers().iterator().next());
        }
        sentReply = true;
        return Collections.emptySet();
    } catch (NotSerializableException e) {
        throw new InternalGemFireException(e);
    } catch (ToDataException e) {
        // exception from user code
        throw e;
    } catch (IOException ex) {
        throw new InternalGemFireException(LocalizedStrings.DirectChannel_UNKNOWN_ERROR_SERIALIZING_MESSAGE.toLocalizedString(), ex);
    } finally {
        try {
            ms.close();
        } catch (IOException e) {
            throw new InternalGemFireException("Unknown error serializing message", e);
        }
    }
}
Also used : NotSerializableException(java.io.NotSerializableException) InternalGemFireException(org.apache.geode.InternalGemFireException) ToDataException(org.apache.geode.ToDataException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 7 with ToDataException

use of org.apache.geode.ToDataException in project geode by apache.

the class Bug40751DUnitTest method testRR.

@Test
public void testRR() {
    System.setProperty("p2p.nodirectBuffers", "true");
    try {
        Host host = Host.getHost(0);
        VM vm0 = host.getVM(1);
        VM vm1 = host.getVM(2);
        SerializableRunnable createDataRegion = new SerializableRunnable("createRegion") {

            public void run() {
                Cache cache = getCache();
                AttributesFactory attr = new AttributesFactory();
                attr.setScope(Scope.DISTRIBUTED_ACK);
                attr.setDataPolicy(DataPolicy.REPLICATE);
                attr.setMulticastEnabled(true);
                cache.createRegion("region1", attr.create());
            }
        };
        vm0.invoke(createDataRegion);
        SerializableRunnable createEmptyRegion = new SerializableRunnable("createRegion") {

            public void run() {
                Cache cache = getCache();
                AttributesFactory attr = new AttributesFactory();
                attr.setScope(Scope.DISTRIBUTED_ACK);
                attr.setDataPolicy(DataPolicy.EMPTY);
                Region region = cache.createRegion("region1", attr.create());
                try {
                    region.put("A", new MyClass());
                    fail("expected ToDataException");
                } catch (ToDataException ex) {
                    if (!(ex.getCause() instanceof RuntimeException)) {
                        fail("expected RuntimeException instead of " + ex.getCause());
                    }
                }
            }
        };
        vm1.invoke(createEmptyRegion);
    } finally {
        Invoke.invokeInEveryVM(new SerializableCallable() {

            public Object call() throws Exception {
                System.getProperties().remove("p2p.oldIO");
                System.getProperties().remove("p2p.nodirectBuffers");
                return null;
            }
        });
        System.getProperties().remove("p2p.oldIO");
        System.getProperties().remove("p2p.nodirectBuffers");
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) VM(org.apache.geode.test.dunit.VM) ToDataException(org.apache.geode.ToDataException) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) ToDataException(org.apache.geode.ToDataException) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) MembershipTest(org.apache.geode.test.junit.categories.MembershipTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 8 with ToDataException

use of org.apache.geode.ToDataException in project geode by apache.

the class DistributionManager method sendMessage.

/**
   * @return recipients who did not receive the message
   * @throws NotSerializableException If <codE>message</code> cannot be serialized
   */
Set sendMessage(DistributionMessage message) throws NotSerializableException {
    Set result = null;
    try {
        // Verify we're not too far into the shutdown
        stopper.checkCancelInProgress(null);
        // avoid race condition during startup
        waitUntilReadyToSendMsgs(message);
        result = sendOutgoing(message);
    } catch (NotSerializableException ex) {
        // serialization error in user data
        throw ex;
    } catch (ToDataException ex) {
        // serialization error in user data
        throw ex;
    } catch (ReenteredConnectException ex) {
        // Recursively tried to get the same connection
        throw ex;
    } catch (CancelException ex) {
        // bug 37194, shutdown conditions
        throw ex;
    } catch (InvalidDeltaException ide) {
        logger.info(LocalizedMessage.create(LocalizedStrings.DistributionManager_CAUGHT_EXCEPTION_WHILE_SENDING_DELTA), ide.getCause());
        throw (RuntimeException) ide.getCause();
    } catch (Exception ex) {
        DistributionManager.this.exceptionInThreads = true;
        String receiver = "NULL";
        if (message != null) {
            receiver = message.getRecipientsDescription();
        }
        logger.fatal(LocalizedMessage.create(LocalizedStrings.DistributionManager_WHILE_PUSHING_MESSAGE_0_TO_1, new Object[] { message, receiver }), ex);
        if (message == null || message.forAll())
            return null;
        result = new HashSet();
        for (int i = 0; i < message.getRecipients().length; i++) result.add(message.getRecipients()[i]);
        return result;
    /*
       * if (ex instanceof org.apache.geode.GemFireIpcResourceException) { return; }
       */
    }
    return result;
}
Also used : ReenteredConnectException(org.apache.geode.internal.tcp.ReenteredConnectException) InvalidDeltaException(org.apache.geode.InvalidDeltaException) NotSerializableException(java.io.NotSerializableException) Set(java.util.Set) HashSet(java.util.HashSet) ToDataException(org.apache.geode.ToDataException) CancelException(org.apache.geode.CancelException) IncompatibleSystemException(org.apache.geode.IncompatibleSystemException) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) CancelException(org.apache.geode.CancelException) InternalGemFireException(org.apache.geode.InternalGemFireException) InvalidDeltaException(org.apache.geode.InvalidDeltaException) ForcedDisconnectException(org.apache.geode.ForcedDisconnectException) SystemConnectException(org.apache.geode.SystemConnectException) NoSuchElementException(java.util.NoSuchElementException) NotSerializableException(java.io.NotSerializableException) UnknownHostException(java.net.UnknownHostException) ReenteredConnectException(org.apache.geode.internal.tcp.ReenteredConnectException) ToDataException(org.apache.geode.ToDataException) HashSet(java.util.HashSet)

Example 9 with ToDataException

use of org.apache.geode.ToDataException in project geode by apache.

the class InternalDataSerializer method invokeToData.

/**
   * For backward compatibility this method should be used to invoke toData on a DSFID or
   * DataSerializable. It will invoke the correct toData method based on the class's version
   * information. This method does not write information about the class of the object. When
   * deserializing use the method invokeFromData to read the contents of the object.
   * 
   * @param ds the object to write
   * @param out the output stream.
   */
public static void invokeToData(Object ds, DataOutput out) throws IOException {
    boolean isDSFID = ds instanceof DataSerializableFixedID;
    try {
        boolean invoked = false;
        Version v = InternalDataSerializer.getVersionForDataStreamOrNull(out);
        if (v != null && v != Version.CURRENT) {
            // get versions where DataOutput was upgraded
            Version[] versions = null;
            if (ds instanceof SerializationVersions) {
                SerializationVersions sv = (SerializationVersions) ds;
                versions = sv.getSerializationVersions();
            }
            // there has been a change in the message
            if (versions != null && versions.length > 0) {
                for (Version version : versions) {
                    // if peer version is less than the greatest upgraded version
                    if (v.compareTo(version) < 0) {
                        ds.getClass().getMethod("toDataPre_" + version.getMethodSuffix(), new Class[] { DataOutput.class }).invoke(ds, out);
                        invoked = true;
                        break;
                    }
                }
            }
        }
        if (!invoked) {
            if (isDSFID) {
                ((DataSerializableFixedID) ds).toData(out);
            } else {
                ((DataSerializable) ds).toData(out);
            }
        }
    } catch (IOException io) {
        // as a problem with the plugin code
        if (isDSFID) {
            throw io;
        } else {
            throw new ToDataException("toData failed on DataSerializable " + ds.getClass(), io);
        }
    } catch (CancelException | ToDataException | GemFireRethrowable ex) {
        // Serializing a PDX can result in a cache closed exception. Just rethrow
        throw ex;
    } 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();
        throw new ToDataException("toData failed on DataSerializable " + ds.getClass(), t);
    }
}
Also used : DataOutput(java.io.DataOutput) IOException(java.io.IOException) GemFireIOException(org.apache.geode.GemFireIOException) DataSerializable(org.apache.geode.DataSerializable) GemFireRethrowable(org.apache.geode.GemFireRethrowable) ToDataException(org.apache.geode.ToDataException) ObjectStreamClass(java.io.ObjectStreamClass) CancelException(org.apache.geode.CancelException)

Example 10 with ToDataException

use of org.apache.geode.ToDataException in project geode by apache.

the class PdxAttributesJUnitTest method testLazyLoner.

/**
   * Test that loner VMs lazily determine if they are a client or a peer.
   * 
   * @throws Exception
   */
@Test
public void testLazyLoner() throws Exception {
    // Test that we can become a peer registry
    {
        CacheFactory cf = new CacheFactory();
        cf.set(MCAST_PORT, "0");
        Cache cache = cf.create();
        // This should work, because this is a peer.
        defineAType();
    }
    tearDown();
    setUp();
    // Test that we can become a client registry.
    {
        CacheFactory cf = new CacheFactory();
        cf.set(MCAST_PORT, "0");
        Cache cache = cf.create();
        int port = AvailablePortHelper.getRandomAvailableTCPPort();
        PoolManager.createFactory().addServer("localhost", port).create("pool");
        try {
            defineAType();
            throw new RuntimeException("Should have failed, this is a client that can't connect to a server");
        } catch (ToDataException expected) {
        // do nothing.
        }
    }
}
Also used : ToDataException(org.apache.geode.ToDataException) CacheFactory(org.apache.geode.cache.CacheFactory) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

ToDataException (org.apache.geode.ToDataException)11 CancelException (org.apache.geode.CancelException)7 IOException (java.io.IOException)6 GemFireRethrowable (org.apache.geode.GemFireRethrowable)5 NotSerializableException (java.io.NotSerializableException)3 GemFireIOException (org.apache.geode.GemFireIOException)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 InternalGemFireException (org.apache.geode.InternalGemFireException)2 Cache (org.apache.geode.cache.Cache)2 DistributedSystemDisconnectedException (org.apache.geode.distributed.DistributedSystemDisconnectedException)2 NonPortableClassException (org.apache.geode.pdx.NonPortableClassException)2 PdxOutputStream (org.apache.geode.pdx.internal.PdxOutputStream)2 PdxWriterImpl (org.apache.geode.pdx.internal.PdxWriterImpl)2 TypeRegistry (org.apache.geode.pdx.internal.TypeRegistry)2 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)2 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)2 DataOutput (java.io.DataOutput)1 ObjectStreamClass (java.io.ObjectStreamClass)1 UnknownHostException (java.net.UnknownHostException)1