use of org.apache.ignite.internal.binary.BinaryContext in project ignite by apache.
the class JdbcThinTcpIo method handshake.
/**
* Used for versions: 2.1.5 and 2.3.0. The protocol version is changed but handshake format isn't changed.
*
* @param ver JDBC client version.
* @throws IOException On IO error.
* @throws SQLException On connection reject.
*/
private HandshakeResult handshake(ClientListenerProtocolVersion ver) throws IOException, SQLException {
BinaryContext ctx = new BinaryContext(BinaryCachingMetadataHandler.create(), new IgniteConfiguration(), null);
BinaryMarshaller marsh = new BinaryMarshaller();
marsh.setContext(new MarshallerContextImpl(null, null));
ctx.configure(marsh);
BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, new BinaryHeapOutputStream(HANDSHAKE_MSG_SIZE), null, null);
writer.writeByte((byte) ClientListenerRequest.HANDSHAKE);
writer.writeShort(ver.major());
writer.writeShort(ver.minor());
writer.writeShort(ver.maintenance());
writer.writeByte(ClientListenerNioListener.JDBC_CLIENT);
writer.writeBoolean(connProps.isDistributedJoins());
writer.writeBoolean(connProps.isEnforceJoinOrder());
writer.writeBoolean(connProps.isCollocated());
writer.writeBoolean(connProps.isReplicatedOnly());
writer.writeBoolean(connProps.isAutoCloseServerCursor());
writer.writeBoolean(connProps.isLazy());
writer.writeBoolean(connProps.isSkipReducerOnUpdate());
if (ver.compareTo(VER_2_7_0) >= 0)
writer.writeString(connProps.nestedTxMode());
if (ver.compareTo(VER_2_8_0) >= 0) {
writer.writeByte(nullableBooleanToByte(connProps.isDataPageScanEnabled()));
JdbcUtils.writeNullableInteger(writer, connProps.getUpdateBatchSize());
}
if (ver.compareTo(VER_2_9_0) >= 0) {
String userAttrs = connProps.getUserAttributesFactory();
if (F.isEmpty(userAttrs))
writer.writeMap(null);
else {
try {
Class<Factory<Map<String, String>>> cls = (Class<Factory<Map<String, String>>>) JdbcThinSSLUtil.class.getClassLoader().loadClass(userAttrs);
Map<String, String> attrs = cls.newInstance().create();
writer.writeMap(attrs);
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new SQLException("Could not found user attributes factory class: " + userAttrs, SqlStateCode.CLIENT_CONNECTION_FAILED, e);
}
}
writer.writeByteArray(ThinProtocolFeature.featuresAsBytes(enabledFeatures()));
}
if (!F.isEmpty(connProps.getUsername())) {
assert ver.compareTo(VER_2_5_0) >= 0 : "Authentication is supported since 2.5";
writer.writeString(connProps.getUsername());
writer.writeString(connProps.getPassword());
}
send(writer.array());
BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, new BinaryHeapInputStream(read()), null, null, false);
boolean accepted = reader.readBoolean();
if (accepted) {
HandshakeResult handshakeRes = new HandshakeResult();
if (reader.available() > 0) {
byte maj = reader.readByte();
byte min = reader.readByte();
byte maintenance = reader.readByte();
String stage = reader.readString();
long ts = reader.readLong();
byte[] hash = reader.readByteArray();
if (ver.compareTo(VER_2_8_0) >= 0)
handshakeRes.nodeId(reader.readUuid());
handshakeRes.igniteVersion(new IgniteProductVersion(maj, min, maintenance, stage, ts, hash));
if (ver.compareTo(VER_2_9_0) >= 0) {
byte[] srvFeatures = reader.readByteArray();
EnumSet<JdbcThinFeature> features = JdbcThinFeature.enumSet(srvFeatures);
handshakeRes.features(features);
}
} else {
handshakeRes.igniteVersion(new IgniteProductVersion((byte) 2, (byte) 0, (byte) 0, "Unknown", 0L, null));
}
handshakeRes.serverProtocolVersion(ver);
return handshakeRes;
} else {
short maj = reader.readShort();
short min = reader.readShort();
short maintenance = reader.readShort();
String err = reader.readString();
ClientListenerProtocolVersion srvProtoVer0 = ClientListenerProtocolVersion.create(maj, min, maintenance);
if (srvProtoVer0.compareTo(VER_2_5_0) < 0 && !F.isEmpty(connProps.getUsername())) {
throw new SQLException("Authentication doesn't support by remote server[driverProtocolVer=" + CURRENT_VER + ", remoteNodeProtocolVer=" + srvProtoVer0 + ", err=" + err + ", url=" + connProps.getUrl() + " address=" + sockAddr + ']', SqlStateCode.CONNECTION_REJECTED);
}
if (VER_2_8_0.equals(srvProtoVer0) || VER_2_7_0.equals(srvProtoVer0) || VER_2_5_0.equals(srvProtoVer0) || VER_2_4_0.equals(srvProtoVer0) || VER_2_3_0.equals(srvProtoVer0) || VER_2_1_5.equals(srvProtoVer0))
return handshake(srvProtoVer0);
else if (VER_2_1_0.equals(srvProtoVer0))
return handshake_2_1_0();
else {
throw new SQLException("Handshake failed [driverProtocolVer=" + CURRENT_VER + ", remoteNodeProtocolVer=" + srvProtoVer0 + ", err=" + err + ']', SqlStateCode.CONNECTION_REJECTED);
}
}
}
use of org.apache.ignite.internal.binary.BinaryContext in project ignite by apache.
the class JdbcThinConnection method createBinaryCtx.
/**
* Create new binary context.
*/
private BinaryContext createBinaryCtx(JdbcBinaryMetadataHandler metaHnd, JdbcMarshallerContext marshCtx) {
BinaryMarshaller marsh = new BinaryMarshaller();
marsh.setContext(marshCtx);
BinaryConfiguration binCfg = new BinaryConfiguration().setCompactFooter(true);
BinaryContext ctx = new BinaryContext(metaHnd, new IgniteConfiguration(), new NullLogger());
ctx.configure(marsh, binCfg);
ctx.registerUserTypesSchema();
return ctx;
}
use of org.apache.ignite.internal.binary.BinaryContext in project ignite by apache.
the class CacheObjectBinaryProcessorImpl method start.
/**
* {@inheritDoc}
*/
@Override
public void start() throws IgniteCheckedException {
if (marsh instanceof BinaryMarshaller) {
if (!ctx.clientNode()) {
metadataFileStore = new BinaryMetadataFileStore(metadataLocCache, ctx, log, CU.isPersistenceEnabled(ctx.config()) && binaryMetadataFileStoreDir == null ? resolveBinaryWorkDir(ctx.config().getWorkDirectory(), ctx.pdsFolderResolver().resolveFolders().folderName()) : binaryMetadataFileStoreDir);
metadataFileStore.start();
}
BinaryMetadataHandler metaHnd = new BinaryMetadataHandler() {
@Override
public void addMeta(int typeId, BinaryType newMeta, boolean failIfUnregistered) 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 = mergeMetadata(oldMeta, ((BinaryTypeImpl) newMeta).metadata());
if (oldMeta != mergedMeta)
metadataLocCache.put(typeId, new BinaryMetadataHolder(mergedMeta, 0, 0));
return;
}
BinaryMetadata newMeta0 = ((BinaryTypeImpl) newMeta).metadata();
CacheObjectBinaryProcessorImpl.this.addMeta(typeId, newMeta0.wrap(binaryCtx), failIfUnregistered);
}
@Override
public void addMetaLocally(int typeId, BinaryType meta, boolean failIfUnregistered) throws BinaryObjectException {
CacheObjectBinaryProcessorImpl.this.addMetaLocally(typeId, meta);
}
@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);
}
@Override
public Collection<BinaryType> metadata() throws BinaryObjectException {
return CacheObjectBinaryProcessorImpl.this.metadata();
}
};
BinaryMarshaller bMarsh0 = (BinaryMarshaller) marsh;
binaryCtx = useTestBinaryCtx ? new TestBinaryContext(metaHnd, ctx.config(), ctx.log(BinaryContext.class)) : new BinaryContext(metaHnd, ctx.config(), ctx.log(BinaryContext.class));
transport = new BinaryMetadataTransport(metadataLocCache, metadataFileStore, binaryCtx, ctx, log);
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);
}
}
if (!ctx.clientNode())
metadataFileStore.restoreMetadata();
}
}
use of org.apache.ignite.internal.binary.BinaryContext in project ignite by apache.
the class IgniteClusterSnapshotCheckTest method testClusterSnapshotCheckFailsOnPartitionDataDiffers.
/**
* @throws Exception If fails.
*/
@Test
public void testClusterSnapshotCheckFailsOnPartitionDataDiffers() throws Exception {
CacheConfiguration<Integer, Value> ccfg = txCacheConfig(new CacheConfiguration<Integer, Value>(DEFAULT_CACHE_NAME)).setAffinity(new RendezvousAffinityFunction(false, 1));
IgniteEx ignite = startGridsWithoutCache(2);
ignite.getOrCreateCache(ccfg).put(1, new Value(new byte[2000]));
forceCheckpoint(ignite);
GridCacheSharedContext<?, ?> cctx = ignite.context().cache().context();
GridCacheDatabaseSharedManager db = (GridCacheDatabaseSharedManager) cctx.database();
BinaryContext binCtx = ((CacheObjectBinaryProcessorImpl) ignite.context().cacheObjects()).binaryContext();
GridCacheAdapter<?, ?> cache = ignite.context().cache().internalCache(dfltCacheCfg.getName());
long partCtr = cache.context().topology().localPartition(PART_ID, NONE, false).dataStore().updateCounter();
AtomicBoolean done = new AtomicBoolean();
db.addCheckpointListener(new CheckpointListener() {
@Override
public void onMarkCheckpointBegin(Context ctx) throws IgniteCheckedException {
// Change the cache value only at on of the cluster node to get hash conflict when the check command ends.
if (!done.compareAndSet(false, true))
return;
GridIterator<CacheDataRow> it = cache.context().offheap().partitionIterator(PART_ID);
assertTrue(it.hasNext());
CacheDataRow row0 = it.nextX();
AffinityTopologyVersion topVer = cctx.exchange().readyAffinityVersion();
GridCacheEntryEx cached = cache.entryEx(row0.key(), topVer);
byte[] bytes = new byte[2000];
new Random().nextBytes(bytes);
try {
BinaryObjectImpl newVal = new BinaryObjectImpl(binCtx, binCtx.marshaller().marshal(new Value(bytes)), 0);
boolean success = cached.initialValue(newVal, new GridCacheVersion(row0.version().topologyVersion(), row0.version().nodeOrder(), row0.version().order() + 1), null, null, TxState.NA, TxState.NA, TTL_ETERNAL, row0.expireTime(), true, topVer, DR_NONE, false, false, null);
assertTrue(success);
long newPartCtr = cache.context().topology().localPartition(PART_ID, NONE, false).dataStore().updateCounter();
assertEquals(newPartCtr, partCtr);
} catch (Exception e) {
throw new IgniteCheckedException(e);
}
}
@Override
public void onCheckpointBegin(Context ctx) throws IgniteCheckedException {
}
@Override
public void beforeCheckpointBegin(Context ctx) throws IgniteCheckedException {
}
});
db.waitForCheckpoint("test-checkpoint");
ignite.snapshot().createSnapshot(SNAPSHOT_NAME).get();
Path part0 = U.searchFileRecursively(snp(ignite).snapshotLocalDir(SNAPSHOT_NAME).toPath(), getPartitionFileName(PART_ID));
assertNotNull(part0);
assertTrue(part0.toString(), part0.toFile().exists());
IdleVerifyResultV2 res = snp(ignite).checkSnapshot(SNAPSHOT_NAME).get();
StringBuilder b = new StringBuilder();
res.print(b::append, true);
assertTrue(F.isEmpty(res.exceptions()));
assertContains(log, b.toString(), "The check procedure has failed, conflict partitions has been found: [counterConflicts=0, hashConflicts=1]");
}
use of org.apache.ignite.internal.binary.BinaryContext in project ignite by apache.
the class IgniteTestResources method getMarshaller.
/**
* @return Marshaller.
* @throws IgniteCheckedException If failed.
*/
public static synchronized Marshaller getMarshaller() throws IgniteCheckedException {
String marshallerName = System.getProperty(MARSH_CLASS_NAME);
Marshaller marsh;
if (marshallerName == null)
marsh = new BinaryMarshaller();
else {
try {
Class<? extends Marshaller> cls = (Class<? extends Marshaller>) Class.forName(marshallerName);
marsh = cls.newInstance();
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new IgniteCheckedException("Failed to create test marshaller [marshaller=" + marshallerName + ']', e);
}
}
marsh.setContext(new MarshallerContextTestImpl());
if (marsh instanceof BinaryMarshaller) {
BinaryMarshaller binaryMarsh = (BinaryMarshaller) marsh;
BinaryContext ctx = new BinaryContext(BinaryCachingMetadataHandler.create(), new IgniteConfiguration(), new NullLogger());
binaryMarsh.setBinaryContext(ctx, new IgniteConfiguration());
}
return marsh;
}
Aggregations