use of org.apache.ignite.cluster.ClusterState in project ignite by apache.
the class GridDhtPartitionsExchangeFuture method finishExchangeOnCoordinator.
/**
* @param sndResNodes Additional nodes to send finish message to.
*/
private void finishExchangeOnCoordinator(@Nullable Collection<ClusterNode> sndResNodes) {
if (isDone() || !enterBusy())
return;
try {
if (!F.isEmpty(exchangeGlobalExceptions) && dynamicCacheStartExchange() && isRollbackSupported()) {
sendExchangeFailureMessage();
return;
}
AffinityTopologyVersion resTopVer = exchCtx.events().topologyVersion();
if (log.isInfoEnabled()) {
log.info("finishExchangeOnCoordinator [topVer=" + initialVersion() + ", resVer=" + resTopVer + ']');
}
Map<Integer, CacheGroupAffinityMessage> idealAffDiff = null;
// Reserve at least 2 threads for system operations.
int parallelismLvl = U.availableThreadCount(cctx.kernalContext(), GridIoPolicy.SYSTEM_POOL, 2);
if (exchCtx.mergeExchanges()) {
synchronized (mux) {
if (mergedJoinExchMsgs != null) {
for (Map.Entry<UUID, GridDhtPartitionsSingleMessage> e : mergedJoinExchMsgs.entrySet()) {
msgs.put(e.getKey(), e.getValue());
updatePartitionSingleMap(e.getKey(), e.getValue());
}
}
}
assert exchCtx.events().hasServerJoin() || exchCtx.events().hasServerLeft();
exchCtx.events().processEvents(this);
if (exchCtx.events().hasServerLeft())
idealAffDiff = cctx.affinity().onServerLeftWithExchangeMergeProtocol(this);
else
cctx.affinity().onServerJoinWithExchangeMergeProtocol(this, true);
doInParallel(parallelismLvl, cctx.kernalContext().pools().getSystemExecutorService(), cctx.affinity().cacheGroups().values(), desc -> {
if (desc.config().getCacheMode() == CacheMode.LOCAL)
return null;
CacheGroupContext grp = cctx.cache().cacheGroup(desc.groupId());
GridDhtPartitionTopology top = grp != null ? grp.topology() : cctx.exchange().clientTopology(desc.groupId(), events().discoveryCache());
top.beforeExchange(this, true, true);
return null;
});
}
span.addLog(() -> "Affinity recalculation (crd)");
timeBag.finishGlobalStage("Affinity recalculation (crd)");
Map<Integer, CacheGroupAffinityMessage> joinedNodeAff = new ConcurrentHashMap<>(cctx.cache().cacheGroups().size());
doInParallel(parallelismLvl, cctx.kernalContext().pools().getSystemExecutorService(), msgs.values(), msg -> {
processSingleMessageOnCrdFinish(msg, joinedNodeAff);
return null;
});
timeBag.finishGlobalStage("Collect update counters and create affinity messages");
if (firstDiscoEvt.type() == EVT_DISCOVERY_CUSTOM_EVT) {
assert firstDiscoEvt instanceof DiscoveryCustomEvent;
if (activateCluster() || changedBaseline())
assignPartitionsStates(null);
DiscoveryCustomMessage discoveryCustomMessage = ((DiscoveryCustomEvent) firstDiscoEvt).customMessage();
if (discoveryCustomMessage instanceof DynamicCacheChangeBatch) {
if (exchActions != null) {
Set<String> caches = exchActions.cachesToResetLostPartitions();
if (!F.isEmpty(caches))
resetLostPartitions(caches);
Set<Integer> cacheGroupsToResetOwners = concat(exchActions.cacheGroupsToStart().stream().map(grp -> grp.descriptor().groupId()), exchActions.cachesToResetLostPartitions().stream().map(CU::cacheId)).collect(Collectors.toSet());
assignPartitionsStates(cacheGroupsToResetOwners);
}
} else if (discoveryCustomMessage instanceof SnapshotDiscoveryMessage && ((SnapshotDiscoveryMessage) discoveryCustomMessage).needAssignPartitions()) {
markAffinityReassign();
assignPartitionsStates(null);
}
} else if (exchCtx.events().hasServerJoin())
assignPartitionsStates(null);
else if (exchCtx.events().hasServerLeft())
assignPartitionsStates(emptySet());
// Validation should happen after resetting owners to avoid false desync reporting.
validatePartitionsState();
// Recalculate new affinity based on partitions availability.
if (!exchCtx.mergeExchanges() && forceAffReassignment) {
idealAffDiff = cctx.affinity().onCustomEventWithEnforcedAffinityReassignment(this);
timeBag.finishGlobalStage("Ideal affinity diff calculation (enforced)");
}
for (CacheGroupContext grpCtx : cctx.cache().cacheGroups()) {
if (!grpCtx.isLocal())
grpCtx.topology().applyUpdateCounters();
}
timeBag.finishGlobalStage("Apply update counters");
updateLastVersion(cctx.versions().last());
cctx.versions().onExchange(lastVer.get().order());
IgniteProductVersion minVer = exchCtx.events().discoveryCache().minimumNodeVersion();
GridDhtPartitionsFullMessage msg = createPartitionsMessage(true, minVer.compareToIgnoreTimestamp(PARTIAL_COUNTERS_MAP_SINCE) >= 0);
if (!cctx.affinity().rebalanceRequired() && !deactivateCluster())
msg.rebalanced(true);
if (exchCtx.mergeExchanges()) {
assert !centralizedAff;
msg.resultTopologyVersion(resTopVer);
if (exchCtx.events().hasServerLeft())
msg.idealAffinityDiff(idealAffDiff);
} else if (forceAffReassignment)
msg.idealAffinityDiff(idealAffDiff);
msg.prepareMarshal(cctx);
timeBag.finishGlobalStage("Full message preparing");
synchronized (mux) {
finishState = new FinishState(crd.id(), resTopVer, msg);
state = ExchangeLocalState.DONE;
}
if (centralizedAff) {
assert !exchCtx.mergeExchanges();
IgniteInternalFuture<Map<Integer, Map<Integer, List<UUID>>>> fut = cctx.affinity().initAffinityOnNodeLeft(this);
if (!fut.isDone())
fut.listen(this::onAffinityInitialized);
else
onAffinityInitialized(fut);
} else {
Set<ClusterNode> nodes;
Map<UUID, GridDhtPartitionsSingleMessage> mergedJoinExchMsgs0;
synchronized (mux) {
srvNodes.remove(cctx.localNode());
nodes = new LinkedHashSet<>(srvNodes);
mergedJoinExchMsgs0 = mergedJoinExchMsgs;
if (mergedJoinExchMsgs != null) {
for (Map.Entry<UUID, GridDhtPartitionsSingleMessage> e : mergedJoinExchMsgs.entrySet()) {
if (e.getValue() != null) {
ClusterNode node = cctx.discovery().node(e.getKey());
if (node != null)
nodes.add(node);
}
}
} else
mergedJoinExchMsgs0 = Collections.emptyMap();
if (!F.isEmpty(sndResNodes))
nodes.addAll(sndResNodes);
}
if (msg.rebalanced())
markRebalanced();
if (!nodes.isEmpty())
sendAllPartitions(msg, nodes, mergedJoinExchMsgs0, joinedNodeAff);
timeBag.finishGlobalStage("Full message sending");
discoveryLag = calculateDiscoveryLag(msgs, mergedJoinExchMsgs0);
if (!stateChangeExchange())
onDone(exchCtx.events().topologyVersion(), null);
for (Map.Entry<UUID, GridDhtPartitionsSingleMessage> e : pendingSingleMsgs.entrySet()) {
if (log.isInfoEnabled()) {
log.info("Process pending message on coordinator [node=" + e.getKey() + ", ver=" + initialVersion() + ", resVer=" + resTopVer + ']');
}
processSingleMessage(e.getKey(), e.getValue());
}
}
if (stateChangeExchange()) {
StateChangeRequest req = exchActions.stateChangeRequest();
assert req != null : exchActions;
boolean stateChangeErr = false;
if (!F.isEmpty(exchangeGlobalExceptions)) {
stateChangeErr = true;
cctx.kernalContext().state().onStateChangeError(exchangeGlobalExceptions, req);
} else {
boolean hasMoving = !partsToReload.isEmpty();
Set<Integer> waitGrps = cctx.affinity().waitGroups();
if (!hasMoving) {
for (CacheGroupContext grpCtx : cctx.cache().cacheGroups()) {
if (waitGrps.contains(grpCtx.groupId()) && grpCtx.topology().hasMovingPartitions()) {
hasMoving = true;
break;
}
}
}
cctx.kernalContext().state().onExchangeFinishedOnCoordinator(this, hasMoving);
}
if (!cctx.kernalContext().state().clusterState().localBaselineAutoAdjustment()) {
ClusterState state = stateChangeErr ? ClusterState.INACTIVE : req.state();
ChangeGlobalStateFinishMessage stateFinishMsg = new ChangeGlobalStateFinishMessage(req.requestId(), state, !stateChangeErr);
cctx.discovery().sendCustomEvent(stateFinishMsg);
}
timeBag.finishGlobalStage("State finish message sending");
if (!centralizedAff)
onDone(exchCtx.events().topologyVersion(), null);
}
// Try switch late affinity right now if an exchange has been completed normally.
if (!centralizedAff && isDone() && error() == null && !cctx.kernalContext().isStopping())
cctx.exchange().checkRebalanceState();
} catch (IgniteCheckedException e) {
if (reconnectOnError(e))
onDone(new IgniteNeedReconnectException(cctx.localNode(), e));
else
onDone(e);
} finally {
leaveBusy();
}
}
use of org.apache.ignite.cluster.ClusterState in project ignite by apache.
the class JmxExporterSpiTest method testIgniteKernal.
/**
* @throws Exception If failed.
*/
@Test
public void testIgniteKernal() throws Exception {
DynamicMBean mbn = metricRegistry(ignite.name(), null, IGNITE_METRICS);
assertNotNull(mbn);
assertEquals(36, mbn.getMBeanInfo().getAttributes().length);
assertFalse(stream(mbn.getMBeanInfo().getAttributes()).anyMatch(a -> F.isEmpty(a.getDescription())));
assertFalse(F.isEmpty((String) mbn.getAttribute("fullVersion")));
assertFalse(F.isEmpty((String) mbn.getAttribute("copyright")));
assertFalse(F.isEmpty((String) mbn.getAttribute("osInformation")));
assertFalse(F.isEmpty((String) mbn.getAttribute("jdkInformation")));
assertFalse(F.isEmpty((String) mbn.getAttribute("vmName")));
assertFalse(F.isEmpty((String) mbn.getAttribute("discoverySpiFormatted")));
assertFalse(F.isEmpty((String) mbn.getAttribute("communicationSpiFormatted")));
assertFalse(F.isEmpty((String) mbn.getAttribute("deploymentSpiFormatted")));
assertFalse(F.isEmpty((String) mbn.getAttribute("checkpointSpiFormatted")));
assertFalse(F.isEmpty((String) mbn.getAttribute("collisionSpiFormatted")));
assertFalse(F.isEmpty((String) mbn.getAttribute("eventStorageSpiFormatted")));
assertFalse(F.isEmpty((String) mbn.getAttribute("failoverSpiFormatted")));
assertFalse(F.isEmpty((String) mbn.getAttribute("loadBalancingSpiFormatted")));
assertEquals(System.getProperty("user.name"), (String) mbn.getAttribute("osUser"));
assertNotNull(DateFormat.getDateTimeInstance().parse((String) mbn.getAttribute("startTimestampFormatted")));
assertNotNull(LocalTime.parse((String) mbn.getAttribute("uptimeFormatted")));
assertTrue((boolean) mbn.getAttribute("isRebalanceEnabled"));
assertTrue((boolean) mbn.getAttribute("isNodeInBaseline"));
assertTrue((boolean) mbn.getAttribute("active"));
assertTrue((long) mbn.getAttribute("startTimestamp") > 0);
assertTrue((long) mbn.getAttribute("uptime") > 0);
assertEquals(ignite.name(), (String) mbn.getAttribute("instanceName"));
assertEquals(Collections.emptyList(), mbn.getAttribute("userAttributesFormatted"));
assertEquals(Collections.emptyList(), mbn.getAttribute("lifecycleBeansFormatted"));
assertEquals(Collections.emptyMap(), mbn.getAttribute("longJVMPauseLastEvents"));
assertEquals(0L, mbn.getAttribute("longJVMPausesCount"));
assertEquals(0L, mbn.getAttribute("longJVMPausesTotalDuration"));
long clusterStateChangeTime = (long) mbn.getAttribute("lastClusterStateChangeTime");
assertTrue(0 < clusterStateChangeTime && clusterStateChangeTime < System.currentTimeMillis());
assertEquals(String.valueOf(ignite.configuration().getPublicThreadPoolSize()), mbn.getAttribute("executorServiceFormatted"));
assertEquals(ignite.configuration().isPeerClassLoadingEnabled(), mbn.getAttribute("isPeerClassLoadingEnabled"));
assertTrue(((String) mbn.getAttribute("currentCoordinatorFormatted")).contains(ignite.localNode().id().toString()));
assertEquals(ignite.configuration().getIgniteHome(), (String) mbn.getAttribute("igniteHome"));
assertEquals(ignite.localNode().id(), mbn.getAttribute("localNodeId"));
assertEquals(ignite.configuration().getGridLogger().toString(), (String) mbn.getAttribute("gridLoggerFormatted"));
assertEquals(ignite.configuration().getMBeanServer().toString(), (String) mbn.getAttribute("mBeanServerFormatted"));
assertEquals(ClusterState.ACTIVE.toString(), mbn.getAttribute("clusterState"));
}
use of org.apache.ignite.cluster.ClusterState in project ignite by apache.
the class ClusterActivationStartedEventTest method testActivationDeactivationStartedLocalEvents.
/**
*/
@Test
public void testActivationDeactivationStartedLocalEvents() throws Exception {
AtomicBoolean activationStarted = new AtomicBoolean();
AtomicBoolean activationFinished = new AtomicBoolean();
AtomicBoolean deactivationStarted = new AtomicBoolean();
AtomicBoolean deactivationFinished = new AtomicBoolean();
IgniteEx ignite = startGrid(0);
ignite.cluster().active(false);
ignite.events().localListen(event -> {
ClusterStateChangeStartedEvent changeStartedEvt = (ClusterStateChangeStartedEvent) event;
ClusterState clusterState = changeStartedEvt.state();
ClusterState prevState = changeStartedEvt.previousState();
if (clusterState == ClusterState.ACTIVE) {
assertEquals(ClusterState.INACTIVE, prevState);
activationStarted.set(true);
} else if (clusterState == ClusterState.INACTIVE) {
assertEquals(ClusterState.ACTIVE, prevState);
deactivationStarted.set(true);
} else
fail("Unexpected event state: " + clusterState);
return true;
}, EventType.EVT_CLUSTER_STATE_CHANGE_STARTED);
ignite.events().localListen(event -> {
assertTrue(activationStarted.get());
activationFinished.set(true);
return true;
}, EventType.EVT_CLUSTER_ACTIVATED);
ignite.events().localListen(event -> {
assertTrue(deactivationStarted.get());
deactivationFinished.set(true);
return true;
}, EventType.EVT_CLUSTER_DEACTIVATED);
ignite.cluster().active(true);
assertTrue(activationStarted.get());
assertTrue(GridTestUtils.waitForCondition(activationFinished::get, 5_000));
assertFalse(deactivationStarted.get());
activationStarted.set(false);
activationFinished.set(false);
ignite.cluster().active(true);
assertFalse(activationStarted.get());
ignite.cluster().active(false);
assertTrue(deactivationStarted.get());
assertTrue(GridTestUtils.waitForCondition(deactivationFinished::get, 5_000));
assertFalse(activationStarted.get());
deactivationStarted.set(false);
deactivationFinished.set(false);
ignite.cluster().active(false);
assertFalse(deactivationStarted.get());
}
use of org.apache.ignite.cluster.ClusterState in project ignite by apache.
the class IgniteClusterActivateDeactivateTestWithPersistence method testDeactivateClusterWithPersistentCache.
/**
* Tests "soft" deactivation (without using the --force flag)
* when the client node does not have the configured data storage and the cluster contains persistent caches.
*
* Expected behavior: the cluster should be deactivated successfully (there is no data loss).
*
* @throws Exception If failed.
*/
@Test
public void testDeactivateClusterWithPersistentCache() throws Exception {
IgniteEx srv = startGrid(0);
IgniteEx clientNode = startClientGrid(1);
clientNode.cluster().state(ACTIVE);
DataRegionConfiguration dfltDataRegion = srv.configuration().getDataStorageConfiguration().getDefaultDataRegionConfiguration();
assertTrue("It is assumed that the default data storage region is persistent.", dfltDataRegion.isPersistenceEnabled());
// Create new caches that are placed into the default pesristent data region.
clientNode.getOrCreateCache(new CacheConfiguration<>("test-client-cache-default-region-implicit"));
clientNode.getOrCreateCache(new CacheConfiguration<>("test-client-cache-default-region-explicit").setDataRegionName(dfltDataRegion.getName()));
// Try to deactivate the cluster without the `force` flag.
IgniteInternalFuture<?> deactivateFut = srv.context().state().changeGlobalState(INACTIVE, false, Collections.emptyList(), false);
try {
deactivateFut.get(10, SECONDS);
} catch (IgniteCheckedException e) {
log.error("Failed to deactivate the cluster.", e);
fail("Failed to deactivate the cluster. [err=" + e.getMessage() + ']');
}
awaitPartitionMapExchange();
// Let's check that all nodes in the cluster have the same state.
for (Ignite node : G.allGrids()) {
IgniteEx n = (IgniteEx) node;
ClusterState state = n.context().state().clusterState().state();
assertTrue("Node must be in inactive state. " + "[node=" + n.configuration().getIgniteInstanceName() + ", actual=" + state + ']', INACTIVE == state);
}
}
use of org.apache.ignite.cluster.ClusterState in project ignite by apache.
the class StateCommand method execute.
/**
* Print cluster state.
*
* @param clientCfg Client configuration.
* @throws Exception If failed to print state.
*/
@Override
public Object execute(GridClientConfiguration clientCfg, Logger log) throws Exception {
try (GridClient client = Command.startClient(clientCfg)) {
GridClientClusterState state = client.state();
UUID id = state.id();
String tag = state.tag();
log.info("Cluster ID: " + id);
log.info("Cluster tag: " + tag);
log.info(CommandHandler.DELIM);
ClusterState clusterState = state.state();
switch(clusterState) {
case ACTIVE:
log.info("Cluster is active");
break;
case INACTIVE:
log.info("Cluster is inactive");
break;
case ACTIVE_READ_ONLY:
log.info("Cluster is active (read-only)");
break;
default:
throw new IllegalStateException("Unknown state: " + clusterState);
}
} catch (Throwable e) {
if (!CommandHandler.isAuthError(e))
log.severe("Failed to get cluster state.");
throw e;
}
return null;
}
Aggregations