lhotari commented on code in PR #25219: URL: https://github.com/apache/pulsar/pull/25219#discussion_r2774830369
########## pulsar-metadata/src/main/java/org/apache/pulsar/metadata/impl/DualMetadataStore.java: ########## @@ -0,0 +1,433 @@ +/* + * 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.pulsar.metadata.impl; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.netty.util.concurrent.DefaultThreadFactory; +import java.util.EnumSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Consumer; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.apache.pulsar.common.migration.MigrationPhase; +import org.apache.pulsar.common.migration.MigrationState; +import org.apache.pulsar.common.util.FutureUtil; +import org.apache.pulsar.metadata.api.GetResult; +import org.apache.pulsar.metadata.api.MetadataCache; +import org.apache.pulsar.metadata.api.MetadataCacheConfig; +import org.apache.pulsar.metadata.api.MetadataEvent; +import org.apache.pulsar.metadata.api.MetadataEventSynchronizer; +import org.apache.pulsar.metadata.api.MetadataSerde; +import org.apache.pulsar.metadata.api.MetadataStore; +import org.apache.pulsar.metadata.api.MetadataStoreConfig; +import org.apache.pulsar.metadata.api.MetadataStoreException; +import org.apache.pulsar.metadata.api.MetadataStoreLifecycle; +import org.apache.pulsar.metadata.api.Notification; +import org.apache.pulsar.metadata.api.Stat; +import org.apache.pulsar.metadata.api.extended.CreateOption; +import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended; +import org.apache.pulsar.metadata.api.extended.SessionEvent; + +/** + * Wrapper around a metadata store that provides transparent migration capability. + * + * <p>When migration is not active, all operations are forwarded to the source store. + * When migration starts (detected via flag in source store), this wrapper: + * <ul> + * <li>Initializes connection to target store</li> + * <li>Recreates ephemeral nodes in target store</li> + * <li>Routes reads/writes based on migration phase</li> + * </ul> + */ +@Slf4j +public class DualMetadataStore implements MetadataStoreExtended { + + @Getter + final MetadataStoreExtended sourceStore; + volatile MetadataStoreExtended targetStore = null; + + private volatile MigrationState migrationState = MigrationState.NOT_STARTED; + + private final MetadataStoreConfig config; + private String participantId; + private final Set<String> localEphemeralPaths = ConcurrentHashMap.newKeySet(); + + private final ScheduledExecutorService executor; + + private final MetadataCache<MigrationState> migrationStateCache; + + private final Set<Consumer<Notification>> listeners = ConcurrentHashMap.newKeySet(); + private final Set<Consumer<SessionEvent>> sessionListeners = ConcurrentHashMap.newKeySet(); + + private final AtomicInteger pendingSourceWrites = new AtomicInteger(); + + private final Set<DualMetadataCache<?>> caches = ConcurrentHashMap.newKeySet(); + + private static final IllegalStateException READ_ONLY_STATE_EXCEPTION = + new IllegalStateException("Write operations not allowed during migrations"); + + public DualMetadataStore(MetadataStore sourceStore, MetadataStoreConfig config) throws MetadataStoreException { + this.sourceStore = (MetadataStoreExtended) sourceStore; + this.config = config; + this.executor = new ScheduledThreadPoolExecutor(1, + new DefaultThreadFactory("pulsar-dual-metadata-store", true)); + this.migrationStateCache = sourceStore.getMetadataCache(MigrationState.class); + + if (sourceStore instanceof MetadataStoreLifecycle msl) { + msl.initializeCluster(); + } + + readCurrentState(); + registerAsParticipant(); + + // Watch for migration events + watchForMigrationEvents(); + } + + private void readCurrentState() throws MetadataStoreException { + try { + // Read the current state + var initialState = migrationStateCache.get(MigrationState.MIGRATION_FLAG_PATH).get(); Review Comment: since this is read only at startup time, I guess it could by pass the cache. In addition, it might be necessary to read after using [`sync`](https://github.com/apache/pulsar/blob/63220eadcfc11f43d4537327a3593603ee5be697/pulsar-metadata/src/main/java/org/apache/pulsar/metadata/api/MetadataStore.java#L59-L67) on Zookeeper to ensure that it's the most recent state. I guess it would be a small chance that this could cause actual issues. -- 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]
