use of org.apache.ignite.internal.binary.BinaryMetadata in project ignite by apache.
the class CacheObjectBinaryProcessorImpl method addMeta.
/**
* {@inheritDoc}
*/
@Override
public void addMeta(final int typeId, final BinaryType newMeta, boolean failIfUnregistered) throws BinaryObjectException {
assert newMeta != null;
assert newMeta instanceof BinaryTypeImpl;
BinaryMetadata newMeta0 = ((BinaryTypeImpl) newMeta).metadata();
if (failIfUnregistered) {
failIfUnregistered(typeId, newMeta0);
return;
}
try {
GridFutureAdapter<MetadataUpdateResult> fut = transport.requestMetadataUpdate(newMeta0);
if (fut == null) {
if (log.isDebugEnabled()) {
log.debug("Metadata update was skipped [typeId=" + typeId + ", typeName=" + newMeta.typeName() + ']');
}
return;
}
long t0 = System.nanoTime();
MetadataUpdateResult res = fut.get();
if (log.isDebugEnabled()) {
IgniteInternalTx tx = ctx.cache().context().tm().tx();
log.debug("Completed metadata update [typeId=" + typeId + ", typeName=" + newMeta.typeName() + ", waitTime=" + MILLISECONDS.convert(System.nanoTime() - t0, NANOSECONDS) + "ms" + ", fut=" + fut + ", tx=" + CU.txString(tx) + ']');
}
assert res != null;
if (res.rejected())
throw res.error();
else if (!ctx.clientNode())
metadataFileStore.waitForWriteCompletion(typeId, res.typeVersion());
} catch (IgniteCheckedException e) {
IgniteCheckedException ex = e;
if (ctx.isStopping()) {
ex = new NodeStoppingException("Node is stopping.");
ex.addSuppressed(e);
}
throw new BinaryObjectException("Failed to update metadata for type: " + newMeta.typeName(), ex);
}
}
use of org.apache.ignite.internal.binary.BinaryMetadata in project ignite by apache.
the class BinaryMetadataTransport method requestMetadataUpdate.
/**
* Sends request to cluster proposing update for given metadata.
*
* @param newMeta Metadata proposed for update.
* @return Future to wait for update result on.
*/
GridFutureAdapter<MetadataUpdateResult> requestMetadataUpdate(BinaryMetadata newMeta) {
int typeId = newMeta.typeId();
MetadataUpdateResultFuture resFut;
do {
BinaryMetadataHolder metaHolder = metaLocCache.get(typeId);
if (metaHolder != null && metaHolder.removing())
throw new IgniteException("The metadata is removing for type [typeId=" + typeId + ']');
BinaryMetadata oldMeta = Optional.ofNullable(metaHolder).map(BinaryMetadataHolder::metadata).orElse(null);
BinaryMetadata mergedMeta = mergeMetadata(oldMeta, newMeta, null);
if (mergedMeta == oldMeta) {
if (metaHolder.pendingVersion() == metaHolder.acceptedVersion())
return null;
return awaitMetadataUpdate(typeId, metaHolder.pendingVersion());
}
resFut = new MetadataUpdateResultFuture(typeId);
} while (!putAndWaitPendingUpdate(typeId, resFut));
BinaryMetadataHolder metadataHolder = metaLocCache.get(typeId);
BinaryMetadata oldMeta = Optional.ofNullable(metadataHolder).map(BinaryMetadataHolder::metadata).orElse(null);
Set<Integer> changedSchemas = new LinkedHashSet<>();
// Ensure after putting pending future, metadata still has difference.
BinaryMetadata mergedMeta = mergeMetadata(oldMeta, newMeta, changedSchemas);
if (mergedMeta == oldMeta) {
resFut.onDone(MetadataUpdateResult.createSuccessfulResult(-1));
return null;
}
if (log.isDebugEnabled()) {
log.debug("Requesting metadata update [typeId=" + typeId + ", typeName=" + mergedMeta.typeName() + ", changedSchemas=" + changedSchemas + ", holder=" + metadataHolder + ", fut=" + resFut + ']');
}
try {
synchronized (this) {
unlabeledFutures.add(resFut);
if (!stopping)
discoMgr.sendCustomEvent(new MetadataUpdateProposedMessage(mergedMeta, ctx.localNodeId()));
else
resFut.onDone(MetadataUpdateResult.createUpdateDisabledResult());
}
} catch (Exception e) {
resFut.onDone(MetadataUpdateResult.createUpdateDisabledResult(), e);
}
if (ctx.clientDisconnected())
onDisconnected();
return resFut;
}
use of org.apache.ignite.internal.binary.BinaryMetadata in project ignite by apache.
the class BinaryMetadataFileStore method restoreMetadata.
/**
* Restores metadata on startup of {@link CacheObjectBinaryProcessorImpl} but before starting discovery.
*/
void restoreMetadata() {
if (!isPersistenceEnabled)
return;
for (File file : metadataDir.listFiles()) {
try (FileInputStream in = new FileInputStream(file)) {
BinaryMetadata meta = U.unmarshal(ctx.config().getMarshaller(), in, U.resolveClassLoader(ctx.config()));
metadataLocCache.put(meta.typeId(), new BinaryMetadataHolder(meta, 0, 0));
} catch (Exception e) {
U.warn(log, "Failed to restore metadata from file: " + file.getName() + "; exception was thrown: " + e.getMessage());
}
}
}
use of org.apache.ignite.internal.binary.BinaryMetadata in project ignite by apache.
the class GridQueryProcessor method registerPlatformTypeLocally.
/**
* Registers platform type locally.
*
* @param clsName Class name.
* @param binProc Binary processor.
*/
private void registerPlatformTypeLocally(String clsName, CacheObjectBinaryProcessorImpl binProc) {
PlatformProcessor platformProc = ctx.platform();
if (platformProc == null || !platformProc.hasContext())
return;
PlatformContext platformCtx = platformProc.context();
BinaryMetadata meta = platformCtx.getBinaryType(clsName);
if (meta != null)
binProc.binaryContext().registerClassLocally(meta.wrap(binProc.binaryContext()), false, platformCtx.getMarshallerPlatformId());
}
use of org.apache.ignite.internal.binary.BinaryMetadata in project ignite by apache.
the class BinaryMetadataConcurrentUpdateWithIndexesTest method testMissingSchemaUpdate.
/**
*/
@Test
public void testMissingSchemaUpdate() throws Exception {
// Start order is important.
Ignite node0 = startGrid("node0");
Ignite node1 = startGrid("node1");
IgniteEx client0 = startClientGrid("client0");
CacheObjectBinaryProcessorImpl.TestBinaryContext clientCtx = (CacheObjectBinaryProcessorImpl.TestBinaryContext) ((CacheObjectBinaryProcessorImpl) client0.context().cacheObjects()).binaryContext();
clientCtx.addListener(new CacheObjectBinaryProcessorImpl.TestBinaryContext.TestBinaryContextListener() {
@Override
public void onAfterMetadataRequest(int typeId, BinaryType type) {
if (syncMeta) {
try {
initMetaReq.countDown();
initMetaReq.await();
} catch (Exception e) {
throw new BinaryObjectException(e);
}
}
}
@Override
public void onBeforeMetadataUpdate(int typeId, BinaryMetadata metadata) {
// Delay one of updates until schema is locally updated on propose message.
if (delayMetadataUpdateThreadLoc.get() != null)
await(localMetaUpdatedLatch, 5000);
}
});
Ignite node2 = startGrid("node2");
Ignite node3 = startGrid("node3");
startGrid("node4");
node0.cluster().active(true);
awaitPartitionMapExchange();
syncMeta = true;
CountDownLatch clientProposeMsgBlockedLatch = new CountDownLatch(1);
AtomicBoolean clientWait = new AtomicBoolean();
final Object clientMux = new Object();
AtomicBoolean srvWait = new AtomicBoolean();
final Object srvMux = new Object();
((BlockTcpDiscoverySpi) node1.configuration().getDiscoverySpi()).setClosure((snd, msg) -> {
if (msg instanceof MetadataUpdateProposedMessage) {
if (Thread.currentThread().getName().contains("client")) {
log.info("Block custom message to client0: [locNode=" + snd + ", msg=" + msg + ']');
clientProposeMsgBlockedLatch.countDown();
// Message to client
synchronized (clientMux) {
while (!clientWait.get()) try {
clientMux.wait();
} catch (InterruptedException e) {
fail();
}
}
}
}
return null;
});
((BlockTcpDiscoverySpi) node2.configuration().getDiscoverySpi()).setClosure((snd, msg) -> {
if (msg instanceof MetadataUpdateProposedMessage) {
MetadataUpdateProposedMessage msg0 = (MetadataUpdateProposedMessage) msg;
int pendingVer = U.field(msg0, "pendingVer");
// Should not block propose messages until they reach coordinator.
if (pendingVer == 0)
return null;
log.info("Block custom message to next server: [locNode=" + snd + ", msg=" + msg + ']');
// Message to client
synchronized (srvMux) {
while (!srvWait.get()) try {
srvMux.wait();
} catch (InterruptedException e) {
fail();
}
}
}
return null;
});
Integer key = primaryKey(node3.cache(DEFAULT_CACHE_NAME));
IgniteInternalFuture fut0 = runAsync(() -> {
try (Transaction tx = client0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
client0.cache(DEFAULT_CACHE_NAME).put(key, build(client0, "val", 0));
tx.commit();
} catch (Throwable t) {
log.error("err", t);
}
});
// Implements test logic.
IgniteInternalFuture fut1 = runAsync(() -> {
// Wait for initial metadata received. It should be initial version: pending=0, accepted=0
await(initMetaReq, 5000);
// Wait for blocking proposal message to client node.
await(clientProposeMsgBlockedLatch, 5000);
// Unblock proposal message to client.
clientWait.set(true);
synchronized (clientMux) {
clientMux.notify();
}
// Give some time to apply update.
doSleep(3000);
// Unblock second metadata update.
localMetaUpdatedLatch.countDown();
// Give some time for tx to complete (success or fail). fut2 will throw an error if tx has failed on commit.
doSleep(3000);
// Unblock metadata message and allow for correct version acceptance.
srvWait.set(true);
synchronized (srvMux) {
srvMux.notify();
}
});
IgniteInternalFuture fut2 = runAsync(() -> {
delayMetadataUpdateThreadLoc.set(true);
try (Transaction tx = client0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, 0, 1)) {
client0.cache(DEFAULT_CACHE_NAME).put(key, build(client0, "val", 0));
tx.commit();
}
});
fut0.get();
fut1.get();
fut2.get();
}
Aggregations