Search in sources :

Example 66 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryObjectBuilderAdditionalSelfTest method testWrongMetadataNullField.

/**
 */
@Test
public void testWrongMetadataNullField() {
    BinaryObjectBuilder builder = binaries().builder("SomeType");
    builder.setField("dateField", null);
    builder.setField("objectField", null, Integer.class);
    builder.build();
    try {
        builder = binaries().builder("SomeType");
        builder.setField("dateField", new Date());
        builder.build();
    } catch (BinaryObjectException ex) {
        assertTrue(ex.getMessage().startsWith("Wrong value has been set"));
    }
    builder = binaries().builder("SomeType");
    try {
        builder.setField("objectField", new GridBinaryTestClasses.Company());
        builder.build();
        fail("BinaryObjectBuilder accepted wrong metadata");
    } catch (BinaryObjectException ex) {
        assertTrue(ex.getMessage().startsWith("Wrong value has been set"));
    }
}
Also used : BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) GridBinaryTestClasses(org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses) Date(java.util.Date) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) Test(org.junit.Test)

Example 67 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class VisorQueryUtils method binaryToString.

/**
 * Convert Binary object to string.
 *
 * @param obj Binary object.
 * @return String representation of Binary object.
 */
public static String binaryToString(BinaryObject obj) {
    int hash = obj.hashCode();
    if (obj instanceof BinaryObjectEx) {
        BinaryObjectEx objEx = (BinaryObjectEx) obj;
        BinaryType meta;
        try {
            meta = ((BinaryObjectEx) obj).rawType();
        } catch (BinaryObjectException ignore) {
            meta = null;
        }
        if (meta != null) {
            if (meta.isEnum()) {
                try {
                    return obj.deserialize().toString();
                } catch (BinaryObjectException ignore) {
                // NO-op.
                }
            }
            SB buf = new SB(meta.typeName());
            if (meta.fieldNames() != null) {
                buf.a(" [hash=").a(hash);
                for (String name : meta.fieldNames()) {
                    Object val = objEx.field(name);
                    buf.a(", ").a(name).a('=').a(val);
                }
                buf.a(']');
                return buf.toString();
            }
        }
    }
    return S.toString(obj.getClass().getSimpleName(), "hash", hash, false, "typeId", obj.type().typeId(), true);
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType) BinaryObjectEx(org.apache.ignite.internal.binary.BinaryObjectEx) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) SB(org.apache.ignite.internal.util.typedef.internal.SB)

Example 68 with BinaryObjectException

use of org.apache.ignite.binary.BinaryObjectException in project ignite by apache.

the class BinaryMetadataConcurrentUpdateWithIndexesTest method testMissingSchemaUpdate.

/**
 */
@Test
public void testMissingSchemaUpdate() throws Exception {
    // Start order is important.
    Ignite node0 = startGrid("node0");
    Ignite node1 = startGrid("node1");
    IgniteEx client0 = startClientGrid("client0");
    CacheObjectBinaryProcessorImpl.TestBinaryContext clientCtx = (CacheObjectBinaryProcessorImpl.TestBinaryContext) ((CacheObjectBinaryProcessorImpl) client0.context().cacheObjects()).binaryContext();
    clientCtx.addListener(new CacheObjectBinaryProcessorImpl.TestBinaryContext.TestBinaryContextListener() {

        @Override
        public void onAfterMetadataRequest(int typeId, BinaryType type) {
            if (syncMeta) {
                try {
                    initMetaReq.countDown();
                    initMetaReq.await();
                } catch (Exception e) {
                    throw new BinaryObjectException(e);
                }
            }
        }

        @Override
        public void onBeforeMetadataUpdate(int typeId, BinaryMetadata metadata) {
            // Delay one of updates until schema is locally updated on propose message.
            if (delayMetadataUpdateThreadLoc.get() != null)
                await(localMetaUpdatedLatch, 5000);
        }
    });
    Ignite node2 = startGrid("node2");
    Ignite node3 = startGrid("node3");
    startGrid("node4");
    node0.cluster().active(true);
    awaitPartitionMapExchange();
    syncMeta = true;
    CountDownLatch clientProposeMsgBlockedLatch = new CountDownLatch(1);
    AtomicBoolean clientWait = new AtomicBoolean();
    final Object clientMux = new Object();
    AtomicBoolean srvWait = new AtomicBoolean();
    final Object srvMux = new Object();
    ((BlockTcpDiscoverySpi) node1.configuration().getDiscoverySpi()).setClosure((snd, msg) -> {
        if (msg instanceof MetadataUpdateProposedMessage) {
            if (Thread.currentThread().getName().contains("client")) {
                log.info("Block custom message to client0: [locNode=" + snd + ", msg=" + msg + ']');
                clientProposeMsgBlockedLatch.countDown();
                // Message to client
                synchronized (clientMux) {
                    while (!clientWait.get()) try {
                        clientMux.wait();
                    } catch (InterruptedException e) {
                        fail();
                    }
                }
            }
        }
        return null;
    });
    ((BlockTcpDiscoverySpi) node2.configuration().getDiscoverySpi()).setClosure((snd, msg) -> {
        if (msg instanceof MetadataUpdateProposedMessage) {
            MetadataUpdateProposedMessage msg0 = (MetadataUpdateProposedMessage) msg;
            int pendingVer = U.field(msg0, "pendingVer");
            // Should not block propose messages until they reach coordinator.
            if (pendingVer == 0)
                return null;
            log.info("Block custom message to next server: [locNode=" + snd + ", msg=" + msg + ']');
            // Message to client
            synchronized (srvMux) {
                while (!srvWait.get()) try {
                    srvMux.wait();
                } catch (InterruptedException e) {
                    fail();
                }
            }
        }
        return null;
    });
    Integer key = primaryKey(node3.cache(DEFAULT_CACHE_NAME));
    IgniteInternalFuture fut0 = runAsync(() -> {
        try (Transaction tx = client0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
            client0.cache(DEFAULT_CACHE_NAME).put(key, build(client0, "val", 0));
            tx.commit();
        } catch (Throwable t) {
            log.error("err", t);
        }
    });
    // Implements test logic.
    IgniteInternalFuture fut1 = runAsync(() -> {
        // Wait for initial metadata received. It should be initial version: pending=0, accepted=0
        await(initMetaReq, 5000);
        // Wait for blocking proposal message to client node.
        await(clientProposeMsgBlockedLatch, 5000);
        // Unblock proposal message to client.
        clientWait.set(true);
        synchronized (clientMux) {
            clientMux.notify();
        }
        // Give some time to apply update.
        doSleep(3000);
        // Unblock second metadata update.
        localMetaUpdatedLatch.countDown();
        // Give some time for tx to complete (success or fail). fut2 will throw an error if tx has failed on commit.
        doSleep(3000);
        // Unblock metadata message and allow for correct version acceptance.
        srvWait.set(true);
        synchronized (srvMux) {
            srvMux.notify();
        }
    });
    IgniteInternalFuture fut2 = runAsync(() -> {
        delayMetadataUpdateThreadLoc.set(true);
        try (Transaction tx = client0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, 0, 1)) {
            client0.cache(DEFAULT_CACHE_NAME).put(key, build(client0, "val", 0));
            tx.commit();
        }
    });
    fut0.get();
    fut1.get();
    fut2.get();
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType) CacheObjectBinaryProcessorImpl(org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) MetadataUpdateProposedMessage(org.apache.ignite.internal.processors.cache.binary.MetadataUpdateProposedMessage) BlockTcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.BlockTcpDiscoverySpi) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)68 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)13 BinaryObject (org.apache.ignite.binary.BinaryObject)12 BinaryMetadata (org.apache.ignite.internal.binary.BinaryMetadata)9 IOException (java.io.IOException)7 BinaryType (org.apache.ignite.binary.BinaryType)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 Test (org.junit.Test)6 Date (java.util.Date)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 TreeMap (java.util.TreeMap)5 IgniteException (org.apache.ignite.IgniteException)5 BigInteger (java.math.BigInteger)3 Time (java.sql.Time)3 Timestamp (java.sql.Timestamp)3 BinaryEnumObjectImpl (org.apache.ignite.internal.binary.BinaryEnumObjectImpl)3 BinaryTypeImpl (org.apache.ignite.internal.binary.BinaryTypeImpl)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2