Search in sources :

Example 16 with BinaryContext

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);
        }
    }
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) SQLException(java.sql.SQLException) Factory(javax.cache.configuration.Factory) IgniteProductVersion(org.apache.ignite.lang.IgniteProductVersion) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) MarshallerContextImpl(org.apache.ignite.internal.MarshallerContextImpl) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) ClientListenerProtocolVersion(org.apache.ignite.internal.processors.odbc.ClientListenerProtocolVersion) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) JdbcThinFeature(org.apache.ignite.internal.processors.odbc.jdbc.JdbcThinFeature) Map(java.util.Map)

Example 17 with BinaryContext

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;
}
Also used : NullLogger(org.apache.ignite.logger.NullLogger) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) BinaryContext(org.apache.ignite.internal.binary.BinaryContext)

Example 18 with BinaryContext

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();
    }
}
Also used : BinaryTypeImpl(org.apache.ignite.internal.binary.BinaryTypeImpl) BinaryType(org.apache.ignite.binary.BinaryType) GridBinaryMarshaller(org.apache.ignite.internal.binary.GridBinaryMarshaller) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) 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) IncompleteCacheObject(org.apache.ignite.internal.processors.cache.IncompleteCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 19 with BinaryContext

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]");
}
Also used : CacheObjectBinaryProcessorImpl(org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Random(java.util.Random) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) Path(java.nio.file.Path) IgniteSnapshotManager.databaseRelativePath(org.apache.ignite.internal.processors.cache.persistence.snapshot.IgniteSnapshotManager.databaseRelativePath) BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) CheckpointListener(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointListener) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteDataIntegrityViolationException(org.apache.ignite.internal.processors.cache.persistence.wal.crc.IgniteDataIntegrityViolationException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteEx(org.apache.ignite.internal.IgniteEx) GridIterator(org.apache.ignite.internal.util.lang.GridIterator) IdleVerifyResultV2(org.apache.ignite.internal.processors.cache.verify.IdleVerifyResultV2) Test(org.junit.Test)

Example 20 with BinaryContext

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;
}
Also used : BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) Marshaller(org.apache.ignite.marshaller.Marshaller) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) MarshallerContextTestImpl(org.apache.ignite.marshaller.MarshallerContextTestImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) NullLogger(org.apache.ignite.logger.NullLogger) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryContext(org.apache.ignite.internal.binary.BinaryContext)

Aggregations

BinaryContext (org.apache.ignite.internal.binary.BinaryContext)22 BinaryMarshaller (org.apache.ignite.internal.binary.BinaryMarshaller)14 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)12 NullLogger (org.apache.ignite.logger.NullLogger)10 GridBinaryMarshaller (org.apache.ignite.internal.binary.GridBinaryMarshaller)7 BinaryConfiguration (org.apache.ignite.configuration.BinaryConfiguration)6 MarshallerContextTestImpl (org.apache.ignite.marshaller.MarshallerContextTestImpl)6 BinaryMetadata (org.apache.ignite.internal.binary.BinaryMetadata)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 IgniteException (org.apache.ignite.IgniteException)3 MarshallerContextImpl (org.apache.ignite.internal.MarshallerContextImpl)3 BinaryWriterExImpl (org.apache.ignite.internal.binary.BinaryWriterExImpl)3 BinaryHeapOutputStream (org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream)3 CacheObjectBinaryProcessorImpl (org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl)3 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BinaryObject (org.apache.ignite.binary.BinaryObject)2 BinaryType (org.apache.ignite.binary.BinaryType)2 BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2