NSAmelchev commented on code in PR #13649: URL: https://github.com/apache/ignite/pull/13649#discussion_r4256613693
########## modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/wal/CdcLinkManager.java: ########## @@ -0,0 +1,175 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.cache.persistence.wal; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.configuration.DataStorageConfiguration; +import org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager; +import org.apache.ignite.internal.processors.cache.GridCacheSharedContext; +import org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree; +import org.apache.ignite.internal.processors.configuration.distributed.DistributedBooleanProperty; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.util.typedef.internal.U; + +import static java.lang.String.format; +import static org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager.CDC_DISABLED; +import static org.apache.ignite.internal.processors.configuration.distributed.DistributedBooleanProperty.detachedBooleanProperty; +import static org.apache.ignite.internal.util.io.GridFileUtils.ensureHardLinkAvailable; + +/** + * Links the archived WAL segments to the CDC directory. The links are not created while CDC is disabled by the + * {@link FileWriteAheadLogManager#CDC_DISABLED} distributed property. Data regions without persistence do not log the + * changes while CDC is disabled, so on the nodes with such regions the segment that is current at the disabling is never + * linked either: the CDC application fails on the missed segment after CDC is enabled again. + */ +class CdcLinkManager { + /** */ + private final GridCacheSharedContext<?, ?> cctx; + + /** */ + private final IgniteWriteAheadLogManager wal; + + /** */ + private final IgniteLogger log; + + /** */ + private final NodeFileTree ft; + + /** + * {@code True} if a data region with CDC has no persistence. + * Unlike {@code FileWriteAheadLogManager#inMemoryCdc}, {@code true} on a node with persistence too. + */ + private final boolean inMemoryCdcRegion; + + /** CDC disabled flag. */ + private final DistributedBooleanProperty cdcDisabled = detachedBooleanProperty(CDC_DISABLED, + "CDC disabled flag. Disables CDC in the cluster to avoid disk overflow. " + + "Note that cache changes will be lost when CDC is disabled. Useful if the CDC application " + + "is down for a long time."); + + /** + * Index of the segment that was current at the last CDC disabling. It is never linked, so the CDC application detects + * the missed changes as a gap in the segments even if no segment is archived while CDC is disabled. Persistent + * regions log the changes anyway, so the gap is made only on the nodes with in-memory CDC regions. + */ + private volatile long disableSgmnt = -1; + + /** */ + CdcLinkManager(GridCacheSharedContext<?, ?> cctx, IgniteWriteAheadLogManager wal) throws IgniteCheckedException { + this.cctx = cctx; + this.wal = wal; + + log = cctx.logger(CdcLinkManager.class); + ft = cctx.kernalContext().pdsFolderResolver().fileTree(); + + DataStorageConfiguration dsCfg = cctx.gridConfig().getDataStorageConfiguration(); + + inMemoryCdcRegion = F.exist( + F.concat(false, dsCfg.getDefaultDataRegionConfiguration(), F.asList(dsCfg.getDataRegionConfigurations())), + reg -> reg.isCdcEnabled() && !reg.isPersistenceEnabled()); + + U.ensureDirectory(ft.walCdc(), "change data capture directory", log); + + ensureHardLinkAvailable(ft.walArchive().toPath(), ft.walCdc().toPath()); + + cctx.kernalContext().internalSubscriptionProcessor().registerDistributedConfigurationListener(dispatcher -> { + cdcDisabled.addListener((name, oldVal, newVal) -> { + if (log.isInfoEnabled()) + log.info(format("Distributed property '%s' was changed from '%s' to '%s'.", name, oldVal, newVal)); + + if (newVal != null && newVal) { + log.warning("CDC was disabled."); + + if (cctx.cdc() != null) + cctx.cdc().stop(true); + + if (inMemoryCdcRegion) + markDisableSegment(); + } + }); + + dispatcher.registerProperty(cdcDisabled); + }); + } + + /** + * The property listener runs before the WAL logging is resumed on a persistent node restarted with CDC disabled, and + * the gap made at the disabling is not guaranteed: the segment can be archived after CDC is enabled again. + */ + void resumeLogging() { + if (inMemoryCdcRegion && forceDisabled()) + markDisableSegment(); + } + + /** */ + private void markDisableSegment() { + // -1 before the WAL logging is resumed, see #resumeLogging. + disableSgmnt = Math.max(disableSgmnt, wal.currentSegment()); + } + + /** */ + boolean forceDisabled() { + return cdcDisabled.getOrDefault(false); + } + + /** + * The segment that was current at the CDC disabling is never linked, the changes in it are lost. It is closed before + * the next record, so the changes made after CDC is enabled again do not get into it. + */ + boolean shouldRollover(long idx) { + return idx == disableSgmnt; + } + + /** */ + void linkSegment(long idx, File segment) throws IOException { + if (forceDisabled()) + log.warning("Creation of segment CDC link skipped. '" + CDC_DISABLED + "' distributed property is 'true'."); + else if (idx == disableSgmnt) Review Comment: Kept. On master the segment is delivered only if it happens to be archived after CDC is re-enabled; during a real maintenance window the persistent region keeps logging, the segment is archived while CDC is still disabled and master skips it too. The difference exists only for a toggle shorter than the life of one segment. The loss is bounded to one segment and documented, and on a node with an in-memory CDC region a resend is required after any manual disabling anyway, since in-memory changes made meanwhile are not logged; the same resend restores the tail. The exact boundary without any loss is the follow-up with a CDC disable record. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
