Search in sources :

Example 11 with BinaryType

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

the class GridCacheClientNodeBinaryObjectMetadataMultinodeTest method testClientMetadataInitialization.

/**
     * @throws Exception If failed.
     */
public void testClientMetadataInitialization() throws Exception {
    startGrids(2);
    final AtomicBoolean stop = new AtomicBoolean();
    final ConcurrentHashSet<String> allTypes = new ConcurrentHashSet<>();
    IgniteInternalFuture<?> fut;
    try {
        // Update binary metadata concurrently with client nodes start.
        fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                IgniteBinary binaries = ignite(0).binary();
                IgniteCache<Object, Object> cache = ignite(0).cache(DEFAULT_CACHE_NAME).withKeepBinary();
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < 1000; i++) {
                    log.info("Iteration: " + i);
                    String type = "binary-type-" + i;
                    allTypes.add(type);
                    for (int f = 0; f < 10; f++) {
                        BinaryObjectBuilder builder = binaries.builder(type);
                        String fieldName = "f" + f;
                        builder.setField(fieldName, i);
                        cache.put(rnd.nextInt(0, 100_000), builder.build());
                        if (f % 100 == 0)
                            log.info("Put iteration: " + f);
                    }
                    if (stop.get())
                        break;
                }
                return null;
            }
        }, 5, "update-thread");
    } finally {
        stop.set(true);
    }
    client = true;
    startGridsMultiThreaded(2, 5);
    fut.get();
    assertFalse(allTypes.isEmpty());
    log.info("Expected binary types: " + allTypes.size());
    assertEquals(7, ignite(0).cluster().nodes().size());
    for (int i = 0; i < 7; i++) {
        log.info("Check metadata on node: " + i);
        boolean client = i > 1;
        assertEquals((Object) client, ignite(i).configuration().isClientMode());
        IgniteBinary binaries = ignite(i).binary();
        Collection<BinaryType> metaCol = binaries.types();
        assertEquals(allTypes.size(), metaCol.size());
        Set<String> names = new HashSet<>();
        for (BinaryType meta : metaCol) {
            info("Binary type: " + meta);
            assertTrue(names.add(meta.typeName()));
            assertNull(meta.affinityKeyFieldName());
            assertEquals(10, meta.fieldNames().size());
        }
        assertEquals(allTypes.size(), names.size());
    }
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType) Callable(java.util.concurrent.Callable) IgniteBinary(org.apache.ignite.IgniteBinary) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConcurrentHashSet(org.eclipse.jetty.util.ConcurrentHashSet) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) ConcurrentHashSet(org.eclipse.jetty.util.ConcurrentHashSet) HashSet(java.util.HashSet)

Example 12 with BinaryType

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

the class BinaryMarshallerSelfTest method testDuplicateFields.

/**
     * Test duplicate fields.
     *
     * @throws Exception If failed.
     */
public void testDuplicateFields() throws Exception {
    BinaryMarshaller marsh = binaryMarshaller();
    DuplicateFieldsB obj = new DuplicateFieldsB(1, 2);
    BinaryObjectImpl objBin = marshal(obj, marsh);
    String fieldName = "x";
    String fieldNameA = DuplicateFieldsA.class.getName() + "." + fieldName;
    String fieldNameB = DuplicateFieldsB.class.getName() + "." + fieldName;
    // Check "hasField".
    assert !objBin.hasField(fieldName);
    assert objBin.hasField(fieldNameA);
    assert objBin.hasField(fieldNameB);
    // Check direct field access.
    assertNull(objBin.field(fieldName));
    assertEquals(Integer.valueOf(1), objBin.field(fieldNameA));
    assertEquals(Integer.valueOf(2), objBin.field(fieldNameB));
    // Check metadata.
    BinaryType type = objBin.type();
    Collection<String> fieldNames = type.fieldNames();
    assertEquals(2, fieldNames.size());
    assert !fieldNames.contains(fieldName);
    assert fieldNames.contains(fieldNameA);
    assert fieldNames.contains(fieldNameB);
    // Check field access through type.
    BinaryField field = type.field(fieldName);
    BinaryField fieldA = type.field(fieldNameA);
    BinaryField fieldB = type.field(fieldNameB);
    assert !field.exists(objBin);
    assert fieldA.exists(objBin);
    assert fieldB.exists(objBin);
    assertNull(field.value(objBin));
    assertEquals(Integer.valueOf(1), fieldA.value(objBin));
    assertEquals(Integer.valueOf(2), fieldB.value(objBin));
    // Check object deserialization.
    DuplicateFieldsB deserialized = objBin.deserialize();
    assertEquals(obj.xA(), deserialized.xA());
    assertEquals(obj.xB(), deserialized.xB());
}
Also used : BinaryField(org.apache.ignite.binary.BinaryField) BinaryType(org.apache.ignite.binary.BinaryType)

Example 13 with BinaryType

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

the class CacheObjectBinaryProcessorImpl method affinityKey.

/**
     * @param po Binary object.
     * @return Affinity key.
     */
public Object affinityKey(BinaryObject po) {
    // Fast path for already cached field.
    if (po instanceof BinaryObjectEx) {
        int typeId = ((BinaryObjectEx) po).typeId();
        T1<BinaryField> fieldHolder = affKeyFields.get(typeId);
        if (fieldHolder != null) {
            BinaryField field = fieldHolder.get();
            return field != null ? field.value(po) : po;
        }
    }
    // Slow path if affinity field is not cached yet.
    try {
        BinaryType meta = po instanceof BinaryObjectEx ? ((BinaryObjectEx) po).rawType() : po.type();
        if (meta != null) {
            String name = meta.affinityKeyFieldName();
            if (name != null) {
                BinaryField field = meta.field(name);
                affKeyFields.putIfAbsent(meta.typeId(), new T1<>(field));
                return field.value(po);
            } else
                affKeyFields.putIfAbsent(meta.typeId(), new T1<BinaryField>(null));
        } else if (po instanceof BinaryObjectEx) {
            int typeId = ((BinaryObjectEx) po).typeId();
            String name = binaryCtx.affinityKeyFieldName(typeId);
            if (name != null)
                return po.field(name);
        }
    } catch (BinaryObjectException e) {
        U.error(log, "Failed to get affinity field from binary object: " + po, e);
    }
    return po;
}
Also used : BinaryField(org.apache.ignite.binary.BinaryField) BinaryType(org.apache.ignite.binary.BinaryType) BinaryObjectEx(org.apache.ignite.internal.binary.BinaryObjectEx) T1(org.apache.ignite.internal.util.typedef.T1) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 14 with BinaryType

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

the class CacheObjectBinaryProcessorImpl method start.

/** {@inheritDoc} */
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
    if (marsh instanceof BinaryMarshaller) {
        if (ctx.clientNode())
            ctx.event().addLocalEventListener(clientDisconLsnr, EVT_CLIENT_NODE_DISCONNECTED);
        transport = new BinaryMetadataTransport(metadataLocCache, ctx, log);
        BinaryMetadataHandler metaHnd = new BinaryMetadataHandler() {

            @Override
            public void addMeta(int typeId, BinaryType newMeta) throws BinaryObjectException {
                assert newMeta != null;
                assert newMeta instanceof BinaryTypeImpl;
                if (!discoveryStarted) {
                    BinaryMetadataHolder holder = metadataLocCache.get(typeId);
                    BinaryMetadata oldMeta = holder != null ? holder.metadata() : null;
                    BinaryMetadata mergedMeta = BinaryUtils.mergeMetadata(oldMeta, ((BinaryTypeImpl) newMeta).metadata());
                    if (oldMeta != mergedMeta)
                        metadataLocCache.putIfAbsent(typeId, new BinaryMetadataHolder(mergedMeta, 0, 0));
                    return;
                }
                BinaryMetadata newMeta0 = ((BinaryTypeImpl) newMeta).metadata();
                CacheObjectBinaryProcessorImpl.this.addMeta(typeId, newMeta0.wrap(binaryCtx));
            }

            @Override
            public BinaryType metadata(int typeId) throws BinaryObjectException {
                return CacheObjectBinaryProcessorImpl.this.metadata(typeId);
            }

            @Override
            public BinaryMetadata metadata0(int typeId) throws BinaryObjectException {
                return CacheObjectBinaryProcessorImpl.this.metadata0(typeId);
            }

            @Override
            public BinaryType metadata(int typeId, int schemaId) throws BinaryObjectException {
                return CacheObjectBinaryProcessorImpl.this.metadata(typeId, schemaId);
            }
        };
        BinaryMarshaller bMarsh0 = (BinaryMarshaller) marsh;
        binaryCtx = new BinaryContext(metaHnd, ctx.config(), ctx.log(BinaryContext.class));
        IgniteUtils.invoke(BinaryMarshaller.class, bMarsh0, "setBinaryContext", binaryCtx, ctx.config());
        binaryMarsh = new GridBinaryMarshaller(binaryCtx);
        binaries = new IgniteBinaryImpl(ctx, this);
        if (!getBoolean(IGNITE_SKIP_CONFIGURATION_CONSISTENCY_CHECK)) {
            BinaryConfiguration bCfg = ctx.config().getBinaryConfiguration();
            if (bCfg != null) {
                Map<String, Object> map = new HashMap<>();
                map.put("globIdMapper", bCfg.getIdMapper() != null ? bCfg.getIdMapper().getClass().getName() : null);
                map.put("globSerializer", bCfg.getSerializer() != null ? bCfg.getSerializer().getClass() : null);
                map.put("compactFooter", bCfg.isCompactFooter());
                if (bCfg.getTypeConfigurations() != null) {
                    Map<Object, Object> typeCfgsMap = new HashMap<>();
                    for (BinaryTypeConfiguration c : bCfg.getTypeConfigurations()) {
                        typeCfgsMap.put(c.getTypeName() != null, Arrays.asList(c.getIdMapper() != null ? c.getIdMapper().getClass() : null, c.getSerializer() != null ? c.getSerializer().getClass() : null, c.isEnum()));
                        if (c.isEnum())
                            BinaryUtils.validateEnumValues(c.getTypeName(), c.getEnumValues());
                    }
                    map.put("typeCfgs", typeCfgsMap);
                }
                ctx.addNodeAttribute(IgniteNodeAttributes.ATTR_BINARY_CONFIGURATION, map);
            }
        }
    }
}
Also used : BinaryTypeImpl(org.apache.ignite.internal.binary.BinaryTypeImpl) BinaryType(org.apache.ignite.binary.BinaryType) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) GridBinaryMarshaller(org.apache.ignite.internal.binary.GridBinaryMarshaller) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) BinaryMetadataHandler(org.apache.ignite.internal.binary.BinaryMetadataHandler) GridBinaryMarshaller(org.apache.ignite.internal.binary.GridBinaryMarshaller) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 15 with BinaryType

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

the class CacheObjectBinaryProcessorImpl method metadata.

/** {@inheritDoc} */
@Override
public Map<Integer, BinaryType> metadata(Collection<Integer> typeIds) throws BinaryObjectException {
    try {
        Collection<BinaryMetadataKey> keys = new ArrayList<>(typeIds.size());
        for (Integer typeId : typeIds) keys.add(new BinaryMetadataKey(typeId));
        Map<Integer, BinaryType> res = U.newHashMap(metadataLocCache.size());
        for (Map.Entry<Integer, BinaryMetadataHolder> e : metadataLocCache.entrySet()) res.put(e.getKey(), e.getValue().metadata().wrap(binaryCtx));
        return res;
    } catch (CacheException e) {
        throw new BinaryObjectException(e);
    }
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Aggregations

BinaryType (org.apache.ignite.binary.BinaryType)24 BinaryObject (org.apache.ignite.binary.BinaryObject)8 BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)4 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 BinaryField (org.apache.ignite.binary.BinaryField)3 BinaryObjectEx (org.apache.ignite.internal.binary.BinaryObjectEx)3 BinaryObjectBuilderImpl (org.apache.ignite.internal.binary.builder.BinaryObjectBuilderImpl)3 GridBinaryTestClasses (org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses)3 HashSet (java.util.HashSet)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Ignite (org.apache.ignite.Ignite)2 IgniteBinary (org.apache.ignite.IgniteBinary)2 BinaryMetadata (org.apache.ignite.internal.binary.BinaryMetadata)2 SB (org.apache.ignite.internal.util.typedef.internal.SB)2 ConcurrentHashSet (org.eclipse.jetty.util.ConcurrentHashSet)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1