Search in sources :

Example 71 with BinaryObjectBuilder

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

the class BinaryMetadataUpdatesFlowTest method testConcurrentMetadataUpdates.

/**
 * @throws Exception If failed.
 */
public void testConcurrentMetadataUpdates() throws Exception {
    startGrid(0);
    final Ignite client = startGrid(getConfiguration("client").setClientMode(true));
    final IgniteCache<Integer, Object> cache = client.cache(DEFAULT_CACHE_NAME).withKeepBinary();
    int threadsNum = 10;
    final int updatesNum = 2000;
    List<IgniteInternalFuture> futs = new ArrayList<>();
    for (int i = 0; i < threadsNum; i++) {
        final int threadId = i;
        IgniteInternalFuture fut = runAsync(new Runnable() {

            @Override
            public void run() {
                try {
                    for (int j = 0; j < updatesNum; j++) {
                        BinaryObjectBuilder bob = client.binary().builder(BINARY_TYPE_NAME);
                        bob.setField("field" + j, threadId);
                        cache.put(threadId, bob.build());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "updater-" + i);
        futs.add(fut);
    }
    for (IgniteInternalFuture fut : futs) fut.get();
}
Also used : ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException)

Example 72 with BinaryObjectBuilder

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

the class GridCacheBinaryObjectsAbstractSelfTest method checkTransform.

/**
 * @param key Key.
 * @throws Exception If failed.
 */
private void checkTransform(Integer key) throws Exception {
    log.info("Transform: " + key);
    IgniteCache<Integer, BinaryObject> c = keepBinaryCache();
    try {
        c.invoke(key, new EntryProcessor<Integer, BinaryObject, Void>() {

            @Override
            public Void process(MutableEntry<Integer, BinaryObject> e, Object... args) {
                BinaryObject val = e.getValue();
                assertNull("Unexpected value: " + val, val);
                return null;
            }
        });
        jcache(0).put(key, new TestObject(1));
        c.invoke(key, new EntryProcessor<Integer, BinaryObject, Void>() {

            @Override
            public Void process(MutableEntry<Integer, BinaryObject> e, Object... args) {
                BinaryObject val = e.getValue();
                assertNotNull("Unexpected value: " + val, val);
                assertEquals(new Integer(1), val.field("val"));
                Ignite ignite = e.unwrap(Ignite.class);
                IgniteBinary binaries = ignite.binary();
                BinaryObjectBuilder builder = binaries.builder(val);
                builder.setField("val", 2);
                e.setValue(builder.build());
                return null;
            }
        });
        BinaryObject obj = c.get(key);
        assertEquals(new Integer(2), obj.field("val"));
        c.invoke(key, new EntryProcessor<Integer, BinaryObject, Void>() {

            @Override
            public Void process(MutableEntry<Integer, BinaryObject> e, Object... args) {
                BinaryObject val = e.getValue();
                assertNotNull("Unexpected value: " + val, val);
                assertEquals(new Integer(2), val.field("val"));
                e.setValue(val);
                return null;
            }
        });
        obj = c.get(key);
        assertEquals(new Integer(2), obj.field("val"));
        c.invoke(key, new EntryProcessor<Integer, BinaryObject, Void>() {

            @Override
            public Void process(MutableEntry<Integer, BinaryObject> e, Object... args) {
                BinaryObject val = e.getValue();
                assertNotNull("Unexpected value: " + val, val);
                assertEquals(new Integer(2), val.field("val"));
                e.remove();
                return null;
            }
        });
        assertNull(c.get(key));
    } finally {
        c.remove(key);
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) Ignite(org.apache.ignite.Ignite) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) IgniteBinary(org.apache.ignite.IgniteBinary)

Example 73 with BinaryObjectBuilder

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

the class GridDataStreamerImplSelfTest method testAddBinaryCreatedWithBuilder.

/**
 *  Tries to propagate cache with binary objects created using the builder.
 *
 * @throws Exception If failed.
 */
public void testAddBinaryCreatedWithBuilder() throws Exception {
    try {
        binaries = true;
        startGrids(2);
        awaitPartitionMapExchange();
        Ignite g0 = grid(0);
        IgniteDataStreamer<Integer, BinaryObject> dataLdr = g0.dataStreamer(DEFAULT_CACHE_NAME);
        for (int i = 0; i < 500; i++) {
            BinaryObjectBuilder obj = g0.binary().builder("NoExistedClass");
            obj.setField("id", i);
            obj.setField("name", String.valueOf("name = " + i));
            dataLdr.addData(i, obj.build());
        }
        dataLdr.close(false);
        assertEquals(500, g0.cache(DEFAULT_CACHE_NAME).size(CachePeekMode.ALL));
        assertEquals(500, grid(1).cache(DEFAULT_CACHE_NAME).size(CachePeekMode.ALL));
    } finally {
        G.stopAll(true);
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) Ignite(org.apache.ignite.Ignite) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Example 74 with BinaryObjectBuilder

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

the class GridCacheBinaryObjectMetadataExchangeMultinodeTest method addStringField.

/**
 * Adds field of String type to fixed binary type.
 *
 * @param ignite Ignite.
 * @param fieldName Field name.
 * @param fieldVal Field value.
 * @param cacheIdx Cache index.
 */
private void addStringField(Ignite ignite, String fieldName, String fieldVal, int cacheIdx) {
    BinaryObjectBuilder builder = ignite.binary().builder(BINARY_TYPE_NAME);
    IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME).withKeepBinary();
    builder.setField(fieldName, fieldVal);
    cache.put(cacheIdx, builder.build());
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Example 75 with BinaryObjectBuilder

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

the class GridCacheClientNodeBinaryObjectMetadataMultinodeTest method testFailoverOnStart.

/**
 * @throws Exception If failed.
 */
public void testFailoverOnStart() throws Exception {
    startGrids(4);
    IgniteBinary binaries = ignite(0).binary();
    IgniteCache<Object, Object> cache = ignite(0).cache(DEFAULT_CACHE_NAME).withKeepBinary();
    for (int i = 0; i < 1000; i++) {
        BinaryObjectBuilder builder = binaries.builder("type-" + i);
        builder.setField("f0", i);
        cache.put(i, builder.build());
    }
    client = true;
    final CyclicBarrier barrier = new CyclicBarrier(6);
    final AtomicInteger startIdx = new AtomicInteger(4);
    IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            barrier.await();
            Ignite ignite = startGrid(startIdx.getAndIncrement());
            assertTrue(ignite.configuration().isClientMode());
            log.info("Started node: " + ignite.name());
            return null;
        }
    }, 5, "start-thread");
    barrier.await();
    U.sleep(ThreadLocalRandom.current().nextInt(10, 100));
    for (int i = 0; i < 3; i++) stopGrid(i);
    fut.get();
    assertEquals(6, ignite(3).cluster().nodes().size());
    for (int i = 3; i < 7; i++) {
        log.info("Check metadata on node: " + i);
        boolean client = i > 3;
        assertEquals((Object) client, ignite(i).configuration().isClientMode());
        binaries = ignite(i).binary();
        final IgniteBinary p0 = binaries;
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                Collection<BinaryType> metaCol = p0.types();
                return metaCol.size() == 1000;
            }
        }, getTestTimeout());
        Collection<BinaryType> metaCol = binaries.types();
        assertEquals(1000, metaCol.size());
        Set<String> names = new HashSet<>();
        for (BinaryType meta : metaCol) {
            assertTrue(names.add(meta.typeName()));
            assertNull(meta.affinityKeyFieldName());
            assertEquals(1, meta.fieldNames().size());
        }
        assertEquals(1000, names.size());
    }
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteBinary(org.apache.ignite.IgniteBinary) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) ConcurrentHashSet(org.eclipse.jetty.util.ConcurrentHashSet) HashSet(java.util.HashSet)

Aggregations

BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)83 BinaryObject (org.apache.ignite.binary.BinaryObject)63 Ignite (org.apache.ignite.Ignite)11 IgniteBinary (org.apache.ignite.IgniteBinary)8 GridBinaryTestClasses (org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses)7 Date (java.util.Date)4 HashMap (java.util.HashMap)4 BinaryType (org.apache.ignite.binary.BinaryType)4 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)3 GridQueryTypeDescriptor (org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)3 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 Column (org.h2.table.Column)3 Field (java.lang.reflect.Field)2 SQLException (java.sql.SQLException)2 LinkedHashMap (java.util.LinkedHashMap)2 UUID (java.util.UUID)2