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());
}
}
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());
}
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;
}
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);
}
}
}
}
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);
}
}
Aggregations