bshashikant commented on a change in pull request #1784: URL: https://github.com/apache/ozone/pull/1784#discussion_r556393031
########## File path: hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMContext.java ########## @@ -0,0 +1,164 @@ +/* + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.hadoop.hdds.scm.ha; + +import com.google.common.base.Preconditions; +import org.apache.hadoop.hdds.scm.safemode.SCMSafeModeManager.SafeModeStatus; +import org.apache.hadoop.hdds.scm.server.StorageContainerManager; +import org.apache.hadoop.hdds.server.events.EventHandler; +import org.apache.hadoop.hdds.server.events.EventPublisher; +import org.apache.ratis.protocol.exceptions.NotLeaderException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * SCMContext is the single source of truth for some key information shared + * across all components within SCM, including: + * - RaftServer related info, e.g., isLeader, term. + * - SafeMode related info, e.g., inSafeMode, preCheckComplete. + */ +public class SCMContext implements EventHandler<SafeModeStatus> { + private static final Logger LOG = LoggerFactory.getLogger(SCMContext.class); + + private static final SCMContext EMPTY_CONTEXT + = new SCMContext(true, 0, new SafeModeStatus(false, true), null); + + /** + * Used by non-HA mode SCM, Recon and Unit Tests. + */ + public static SCMContext emptyContext() { + return EMPTY_CONTEXT; + } + + /** + * Raft related info. + */ + private boolean isLeader; + private long term; + + /** + * Safe mode related info. + */ + private SafeModeStatus safeModeStatus; + + private final StorageContainerManager scm; + private final ReadWriteLock lock = new ReentrantReadWriteLock(); + + SCMContext(boolean isLeader, long term, + final SafeModeStatus safeModeStatus, + final StorageContainerManager scm) { + this.isLeader = isLeader; + this.term = term; + this.safeModeStatus = safeModeStatus; + this.scm = scm; + } + + /** + * Creates SCMContext instance from StorageContainerManager. + */ + public SCMContext(final StorageContainerManager scm) { + this(false, 0, new SafeModeStatus(true, false), scm); + Preconditions.checkNotNull(scm, "scm is null"); + } + + /** + * + * @param newIsLeader : is leader or not + * @param newTerm : term if current SCM becomes leader + */ + public void updateIsLeaderAndTerm(boolean newIsLeader, long newTerm) { + lock.writeLock().lock(); + try { + LOG.info("update <isLeader,term> from <{},{}> to <{},{}>", + isLeader, term, newIsLeader, newTerm); + + isLeader = newIsLeader; + term = newTerm; + } finally { + lock.writeLock().unlock(); + } + } + + /** + * Check whether current SCM is leader or not. + * + * @return isLeader + */ + public boolean isLeader() { + lock.readLock().lock(); + try { + return isLeader; + } finally { + lock.readLock().unlock(); + } + } + + /** + * Get term of current leader SCM. + * + * @return term + * @throws NotLeaderException if isLeader is false + */ + public long getTerm() throws NotLeaderException { Review comment: I think the function name shoulld be changed to getTermOfLeader() or something like this to make it intuitive enough as this is supposed to be only a leader executed code ########## File path: hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMServiceManager.java ########## @@ -0,0 +1,125 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.hadoop.hdds.scm.ha; + +import com.google.common.base.Preconditions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.hadoop.hdds.scm.ha.SCMService.*; + +import java.util.ArrayList; +import java.util.List; + +/** + * Manipulate background services in SCM, including ReplicationManager, + * SCMBlockDeletingService and BackgroundPipelineCreator. + */ +public class SCMServiceManager { + private static final Logger LOG = + LoggerFactory.getLogger(SCMServiceManager.class); + + // Cached latest raft condition and safe mode condition. + private RaftCondition raftCondition; + private SafeModeCondition safeModeCondition; + + private final List<SCMService> services = new ArrayList<>(); + + /** + * Start as a follower SCM in safe mode. + */ + public SCMServiceManager() { + raftCondition = RaftCondition.LEADER_TO_FOLLOWER; + safeModeCondition = SafeModeCondition.ENTERING_SAFE_MODE; + } + + /** + * Register a SCMService to SCMServiceManager. + */ + public synchronized void register(SCMService service) { + Preconditions.checkNotNull(service); + LOG.info("register Service {}", service.getServiceName()); + services.add(service); + } + + /** + * Current SCM becomes leader. + */ + public synchronized void followerToLeader() { + LOG.info("followerToLeader is called."); + raftCondition = RaftCondition.FOLLOWER_TO_LEADER; + notifyRaftCondOrSafeModeCondChanged(); + } + + /** + * Current SCM steps down. + */ + public synchronized void leaderToFollower() { + LOG.info("leaderToFollower is called."); + raftCondition = RaftCondition.FOLLOWER_TO_LEADER; Review comment: RaftCondition.FOLLOWER_TO_LEADER -> RaftCondition.LEADER_TO_FOLLOWER ########## File path: hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMContext.java ########## @@ -0,0 +1,164 @@ +/* + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.hadoop.hdds.scm.ha; + +import com.google.common.base.Preconditions; +import org.apache.hadoop.hdds.scm.safemode.SCMSafeModeManager.SafeModeStatus; +import org.apache.hadoop.hdds.scm.server.StorageContainerManager; +import org.apache.hadoop.hdds.server.events.EventHandler; +import org.apache.hadoop.hdds.server.events.EventPublisher; +import org.apache.ratis.protocol.exceptions.NotLeaderException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * SCMContext is the single source of truth for some key information shared + * across all components within SCM, including: + * - RaftServer related info, e.g., isLeader, term. + * - SafeMode related info, e.g., inSafeMode, preCheckComplete. + */ +public class SCMContext implements EventHandler<SafeModeStatus> { + private static final Logger LOG = LoggerFactory.getLogger(SCMContext.class); + + private static final SCMContext EMPTY_CONTEXT + = new SCMContext(true, 0, new SafeModeStatus(false, true), null); + + /** + * Used by non-HA mode SCM, Recon and Unit Tests. + */ + public static SCMContext emptyContext() { + return EMPTY_CONTEXT; + } + + /** + * Raft related info. + */ + private boolean isLeader; + private long term; + + /** + * Safe mode related info. + */ + private SafeModeStatus safeModeStatus; + + private final StorageContainerManager scm; + private final ReadWriteLock lock = new ReentrantReadWriteLock(); + + SCMContext(boolean isLeader, long term, + final SafeModeStatus safeModeStatus, + final StorageContainerManager scm) { + this.isLeader = isLeader; + this.term = term; + this.safeModeStatus = safeModeStatus; + this.scm = scm; + } + + /** + * Creates SCMContext instance from StorageContainerManager. + */ + public SCMContext(final StorageContainerManager scm) { + this(false, 0, new SafeModeStatus(true, false), scm); + Preconditions.checkNotNull(scm, "scm is null"); + } + + /** + * + * @param newIsLeader : is leader or not + * @param newTerm : term if current SCM becomes leader + */ + public void updateIsLeaderAndTerm(boolean newIsLeader, long newTerm) { Review comment: updateIsLeaderAndTerm -> updateLeaderAndTerm ########## File path: hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMContext.java ########## @@ -0,0 +1,164 @@ +/* + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.hadoop.hdds.scm.ha; + +import com.google.common.base.Preconditions; +import org.apache.hadoop.hdds.scm.safemode.SCMSafeModeManager.SafeModeStatus; +import org.apache.hadoop.hdds.scm.server.StorageContainerManager; +import org.apache.hadoop.hdds.server.events.EventHandler; +import org.apache.hadoop.hdds.server.events.EventPublisher; +import org.apache.ratis.protocol.exceptions.NotLeaderException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * SCMContext is the single source of truth for some key information shared + * across all components within SCM, including: + * - RaftServer related info, e.g., isLeader, term. + * - SafeMode related info, e.g., inSafeMode, preCheckComplete. + */ +public class SCMContext implements EventHandler<SafeModeStatus> { + private static final Logger LOG = LoggerFactory.getLogger(SCMContext.class); + + private static final SCMContext EMPTY_CONTEXT + = new SCMContext(true, 0, new SafeModeStatus(false, true), null); + + /** + * Used by non-HA mode SCM, Recon and Unit Tests. + */ + public static SCMContext emptyContext() { + return EMPTY_CONTEXT; + } + + /** + * Raft related info. + */ + private boolean isLeader; + private long term; + + /** + * Safe mode related info. + */ + private SafeModeStatus safeModeStatus; + + private final StorageContainerManager scm; + private final ReadWriteLock lock = new ReentrantReadWriteLock(); + + SCMContext(boolean isLeader, long term, + final SafeModeStatus safeModeStatus, + final StorageContainerManager scm) { + this.isLeader = isLeader; + this.term = term; + this.safeModeStatus = safeModeStatus; + this.scm = scm; + } + + /** + * Creates SCMContext instance from StorageContainerManager. + */ + public SCMContext(final StorageContainerManager scm) { + this(false, 0, new SafeModeStatus(true, false), scm); + Preconditions.checkNotNull(scm, "scm is null"); + } + + /** + * + * @param newIsLeader : is leader or not + * @param newTerm : term if current SCM becomes leader + */ + public void updateIsLeaderAndTerm(boolean newIsLeader, long newTerm) { Review comment: newIsLeader doesn't make much sense here ########## File path: hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMService.java ########## @@ -0,0 +1,85 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.hadoop.hdds.scm.ha; + +/** + * Interface for background services in SCM, including ReplicationManager, + * SCMBlockDeletingService and BackgroundPipelineCreator. + * + * Provide a fine-grained way to manipulate the status of these background + * services. + */ +public interface SCMService { + /** + * @param raftCondition latest raft condition + * @param safeModeCondition latest safe mode condition + */ + void notifyRaftCondOrSafeModeCondChanged( Review comment: Can we add some more info here, regarding what would be one time transitional events and what would be recurring which needs to be notified ? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
