use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.
the class CacheLateAffinityAssignmentTest method calculateAffinity.
/**
* @param topVer Topology version.
* @param filterByRcvd If {@code true} filters caches by 'receivedFrom' property.
* @param cur Optional current affinity.
* @throws Exception If failed.
* @return {@code True} if some primary node changed comparing to given affinity.
*/
private boolean calculateAffinity(long topVer, boolean filterByRcvd, @Nullable Map<String, List<List<ClusterNode>>> cur) throws Exception {
List<Ignite> all = G.allGrids();
IgniteKernal ignite = (IgniteKernal) Collections.min(all, new Comparator<Ignite>() {
@Override
public int compare(Ignite n1, Ignite n2) {
return Long.compare(n1.cluster().localNode().order(), n2.cluster().localNode().order());
}
});
assert !all.isEmpty();
Map<Integer, List<List<ClusterNode>>> assignments = idealAff.get(topVer);
if (assignments == null)
idealAff.put(topVer, assignments = new HashMap<>());
GridKernalContext ctx = ignite.context();
GridCacheSharedContext cctx = ctx.cache().context();
AffinityTopologyVersion topVer0 = new AffinityTopologyVersion(topVer);
cctx.discovery().topologyFuture(topVer).get();
List<GridDhtPartitionsExchangeFuture> futs = cctx.exchange().exchangeFutures();
DiscoveryEvent evt = null;
long stopTime = System.currentTimeMillis() + 10_000;
boolean primaryChanged = false;
do {
for (int i = futs.size() - 1; i >= 0; i--) {
GridDhtPartitionsExchangeFuture fut = futs.get(i);
if (fut.initialVersion().equals(topVer0)) {
evt = fut.firstEvent();
break;
}
}
if (evt == null) {
U.sleep(500);
futs = cctx.exchange().exchangeFutures();
} else
break;
} while (System.currentTimeMillis() < stopTime);
assertNotNull("Failed to find exchange future:", evt);
Collection<ClusterNode> allNodes = ctx.discovery().serverNodes(topVer0);
for (DynamicCacheDescriptor cacheDesc : ctx.cache().cacheDescriptors().values()) {
if (assignments.get(cacheDesc.cacheId()) != null)
continue;
if (filterByRcvd && cacheDesc.receivedFrom() != null && ctx.discovery().node(topVer0, cacheDesc.receivedFrom()) == null)
continue;
AffinityFunction func = cacheDesc.cacheConfiguration().getAffinity();
func = cctx.cache().clone(func);
cctx.kernalContext().resource().injectGeneric(func);
List<ClusterNode> affNodes = new ArrayList<>();
IgnitePredicate<ClusterNode> filter = cacheDesc.cacheConfiguration().getNodeFilter();
for (ClusterNode n : allNodes) {
if (!n.isClient() && (filter == null || filter.apply(n)))
affNodes.add(n);
}
Collections.sort(affNodes, NodeOrderComparator.getInstance());
AffinityFunctionContext affCtx = new GridAffinityFunctionContextImpl(affNodes, previousAssignment(topVer, cacheDesc.cacheId()), evt, topVer0, cacheDesc.cacheConfiguration().getBackups());
List<List<ClusterNode>> assignment = func.assignPartitions(affCtx);
if (cur != null) {
List<List<ClusterNode>> prev = cur.get(cacheDesc.cacheConfiguration().getName());
assertEquals(prev.size(), assignment.size());
if (!primaryChanged) {
for (int p = 0; p < prev.size(); p++) {
List<ClusterNode> nodes0 = prev.get(p);
List<ClusterNode> nodes1 = assignment.get(p);
if (!nodes0.isEmpty() && !nodes1.isEmpty()) {
ClusterNode p0 = nodes0.get(0);
ClusterNode p1 = nodes1.get(0);
if (allNodes.contains(p0) && !p0.equals(p1)) {
primaryChanged = true;
log.info("Primary changed [cache=" + cacheDesc.cacheConfiguration().getName() + ", part=" + p + ", prev=" + F.nodeIds(nodes0) + ", new=" + F.nodeIds(nodes1) + ']');
break;
}
}
}
}
}
assignments.put(cacheDesc.cacheId(), assignment);
}
return primaryChanged;
}
use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.
the class IgnitePageMemReplaceDelayedWriteUnitTest method createPageMemory.
/**
* @param cfg configuration
* @param pageWriter writer for page replacement.
* @param pageSize page size
* @return implementation for test
*/
@NotNull
private PageMemoryImpl createPageMemory(IgniteConfiguration cfg, PageStoreWriter pageWriter, int pageSize) {
IgniteCacheDatabaseSharedManager db = mock(GridCacheDatabaseSharedManager.class);
when(db.checkpointLockIsHeldByThread()).thenReturn(true);
GridCacheSharedContext sctx = Mockito.mock(GridCacheSharedContext.class);
when(sctx.gridConfig()).thenReturn(cfg);
when(sctx.pageStore()).thenReturn(new NoOpPageStoreManager());
when(sctx.wal()).thenReturn(new NoOpWALManager());
when(sctx.database()).thenReturn(db);
when(sctx.logger(any(Class.class))).thenReturn(log);
GridKernalContext kernalCtx = mock(GridKernalContext.class);
when(kernalCtx.config()).thenReturn(cfg);
when(kernalCtx.log(any(Class.class))).thenReturn(log);
when(kernalCtx.internalSubscriptionProcessor()).thenAnswer(mock -> new GridInternalSubscriptionProcessor(kernalCtx));
when(kernalCtx.encryption()).thenAnswer(mock -> new GridEncryptionManager(kernalCtx));
when(kernalCtx.metric()).thenAnswer(mock -> new GridMetricManager(kernalCtx));
when(kernalCtx.performanceStatistics()).thenAnswer(mock -> new PerformanceStatisticsProcessor(kernalCtx));
when(sctx.kernalContext()).thenReturn(kernalCtx);
when(sctx.gridEvents()).thenAnswer(invocationOnMock -> new GridEventStorageManager(kernalCtx));
DataRegionConfiguration regCfg = cfg.getDataStorageConfiguration().getDefaultDataRegionConfiguration();
DataRegionMetricsImpl memMetrics = new DataRegionMetricsImpl(regCfg, kernalCtx);
long[] sizes = prepareSegmentSizes(regCfg.getMaxSize());
DirectMemoryProvider provider = new UnsafeMemoryProvider(log);
IgniteOutClosure<CheckpointProgress> clo = () -> Mockito.mock(CheckpointProgressImpl.class);
PageMemoryImpl memory = new PageMemoryImpl(provider, sizes, sctx, sctx.pageStore(), pageSize, pageWriter, null, () -> true, memMetrics, PageMemoryImpl.ThrottlingPolicy.DISABLED, clo);
memory.start();
return memory;
}
use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.
the class GridP2PContinuousDeploymentClientDisconnectTest method blockClassLoadingRequest.
/**
* Blocks peer-class loading for {@link #P2P_TEST_OBJ_RSRC_NAME} resource.
*
* @param node The node where peer-class loading should be blocked.
*/
private void blockClassLoadingRequest(IgniteEx node) {
GridKernalContext ctx = node.context();
GridDeploymentManager deploymentMgr = ctx.deploy();
Object comm = GridTestUtils.getFieldValue(deploymentMgr, "comm");
GridMessageListener peerLsnr = GridTestUtils.getFieldValue(comm, "peerLsnr");
ctx.io().removeMessageListener(TOPIC_CLASSLOAD, peerLsnr);
GridMessageListener newPeerLsnr = new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object msg, byte plc) {
GridDeploymentRequest req = (GridDeploymentRequest) msg;
String rsrcName = GridTestUtils.getFieldValue(req, "rsrcName");
if (rsrcName.equals(P2P_TEST_OBJ_RSRC_NAME))
return;
peerLsnr.onMessage(nodeId, msg, plc);
}
};
ctx.io().addMessageListener(TOPIC_CLASSLOAD, newPeerLsnr);
}
use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.
the class IgniteWalConverterSensitiveDataTest method beforeTestsStarted.
/**
* {@inheritDoc}
*/
@Override
protected void beforeTestsStarted() throws Exception {
super.beforeTestsStarted();
sysOut = System.out;
testOut = new ByteArrayOutputStream(16 * 1024);
int nodeId = 0;
IgniteEx crd = startGrid(nodeId);
crd.cluster().active(true);
try (Transaction tx = crd.transactions().txStart()) {
IgniteCache<Object, Object> cache = crd.cache(DEFAULT_CACHE_NAME);
sensitiveValues.add(SENSITIVE_DATA_VALUE_PREFIX + 0);
sensitiveValues.add(SENSITIVE_DATA_VALUE_PREFIX + 1);
sensitiveValues.add(SENSITIVE_DATA_VALUE_PREFIX + 2);
String val0 = sensitiveValues.get(0);
String val1 = sensitiveValues.get(1);
String val2 = sensitiveValues.get(2);
cache.put(val0, val0);
cache.withKeepBinary().put(val1, val1);
cache.put(val2, new Person(1, val2));
tx.commit();
}
GridKernalContext kernalCtx = crd.context();
IgniteWriteAheadLogManager wal = kernalCtx.cache().context().wal();
for (WALRecord walRecord : withSensitiveData()) {
if (isIncludeIntoLog(walRecord))
wal.log(walRecord);
}
sensitiveValues.add(SENSITIVE_DATA_VALUE_PREFIX);
wal.flush(null, true);
IgniteConfiguration cfg = crd.configuration();
String wd = cfg.getWorkDirectory();
String wp = cfg.getDataStorageConfiguration().getWalPath();
String fn = kernalCtx.pdsFolderResolver().resolveFolders().folderName();
walDirPath = wd + File.separator + wp + File.separator + fn;
pageSize = cfg.getDataStorageConfiguration().getPageSize();
stopGrid(nodeId);
}
use of org.apache.ignite.internal.GridKernalContext in project ignite by apache.
the class GridAbstractTest method awaitTopologyChange.
/**
* @throws IgniteInterruptedCheckedException If interrupted.
*/
private void awaitTopologyChange() throws IgniteInterruptedCheckedException {
for (Ignite g : G.allGrids()) {
final GridKernalContext ctx = ((IgniteKernal) g).context();
if (ctx.isStopping() || ctx.gateway().getState() == DISCONNECTED || !g.active())
continue;
AffinityTopologyVersion topVer = ctx.discovery().topologyVersionEx();
AffinityTopologyVersion exchVer = ctx.cache().context().exchange().readyAffinityVersion();
if (!topVer.equals(exchVer)) {
info("Topology version mismatch [node=" + g.name() + ", exchVer=" + exchVer + ", topVer=" + topVer + ']');
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
AffinityTopologyVersion topVer = ctx.discovery().topologyVersionEx();
AffinityTopologyVersion exchVer = ctx.cache().context().exchange().readyAffinityVersion();
return exchVer.equals(topVer);
}
}, DFLT_TOP_WAIT_TIMEOUT);
}
}
}
Aggregations