ritegarg commented on code in PR #2224: URL: https://github.com/apache/phoenix/pull/2224#discussion_r2211336899
########## phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/ClusterRoleRecord.java: ########## @@ -55,18 +65,136 @@ public class ClusterRoleRecord { private static final Logger LOG = LoggerFactory.getLogger(ClusterRoleRecord.class); + private static final Set<ClusterRole> CAN_CONNECT + = ImmutableSet.of(ClusterRole.ACTIVE, ClusterRole.ACTIVE_TO_STANDBY, + ClusterRole.STANDBY, ClusterRole.ACTIVE_NOT_IN_SYNC, + ClusterRole.ABORT_TO_ACTIVE, ClusterRole.ABORT_TO_STANDBY, + ClusterRole.DEGRADED_STANDBY, ClusterRole.DEGRADED_STANDBY_FOR_READER, + ClusterRole.DEGRADED_STANDBY_FOR_WRITER, ClusterRole.ACTIVE_WITH_OFFLINE_PEER, + ClusterRole.ACTIVE_NOT_IN_SYNC_TO_STANDBY, ClusterRole.STANDBY_TO_ACTIVE, + ClusterRole.ACTIVE_NOT_IN_SYNC_WITH_OFFLINE_PEER); + private static final Set<ClusterRole> IS_ACTIVE = ImmutableSet.of(ClusterRole.ACTIVE, + ClusterRole.ACTIVE_TO_STANDBY, ClusterRole.ACTIVE_NOT_IN_SYNC, + ClusterRole.ACTIVE_NOT_IN_SYNC_TO_STANDBY, ClusterRole.ACTIVE_WITH_OFFLINE_PEER, + ClusterRole.ACTIVE_NOT_IN_SYNC_WITH_OFFLINE_PEER); + private static final Set<ClusterRole> IS_STANDBY = ImmutableSet.of(ClusterRole.STANDBY, + ClusterRole.DEGRADED_STANDBY, ClusterRole.DEGRADED_STANDBY_FOR_READER, + ClusterRole.DEGRADED_STANDBY_FOR_WRITER, ClusterRole.STANDBY_TO_ACTIVE); + private static final Set<ClusterRole> IS_MUTATION_BLOCKED = ImmutableSet.of(ClusterRole.ACTIVE_TO_STANDBY, + ClusterRole.ACTIVE_NOT_IN_SYNC_TO_STANDBY); + /** * Enum for the current state of the cluster. Exact meaning depends on the Policy but in general Active clusters * take traffic, standby and offline do not, and unknown is used if the state cannot be determined. */ public enum ClusterRole { - ACTIVE, STANDBY, OFFLINE, UNKNOWN, ACTIVE_TO_STANDBY; + ABORT_TO_ACTIVE, + ABORT_TO_STANDBY, + ACTIVE, + ACTIVE_NOT_IN_SYNC, + ACTIVE_NOT_IN_SYNC_TO_STANDBY, + ACTIVE_NOT_IN_SYNC_WITH_OFFLINE_PEER, + ACTIVE_TO_STANDBY, + ACTIVE_WITH_OFFLINE_PEER, + DEGRADED_STANDBY, + DEGRADED_STANDBY_FOR_READER, + DEGRADED_STANDBY_FOR_WRITER, + OFFLINE, + STANDBY, + STANDBY_TO_ACTIVE, + UNKNOWN; + + private Set<ClusterRole> allowedTransitions; + + static { + // Initialize allowed transitions + ACTIVE_NOT_IN_SYNC.allowedTransitions = ImmutableSet.of( + ACTIVE_NOT_IN_SYNC, ACTIVE, + ACTIVE_NOT_IN_SYNC_TO_STANDBY, ACTIVE_NOT_IN_SYNC_WITH_OFFLINE_PEER + ); + + ACTIVE.allowedTransitions = ImmutableSet.of( + ACTIVE_NOT_IN_SYNC, ACTIVE_WITH_OFFLINE_PEER, ACTIVE_TO_STANDBY + ); + + STANDBY.allowedTransitions = ImmutableSet.of(STANDBY_TO_ACTIVE, + DEGRADED_STANDBY_FOR_READER, DEGRADED_STANDBY_FOR_WRITER); + // This needs to be manually recovered by operator + OFFLINE.allowedTransitions = ImmutableSet.of(); + // This needs to be manually recovered by operator + UNKNOWN.allowedTransitions = ImmutableSet.of(); + ACTIVE_TO_STANDBY.allowedTransitions = ImmutableSet.of(ABORT_TO_ACTIVE, STANDBY); + STANDBY_TO_ACTIVE.allowedTransitions = ImmutableSet.of(ABORT_TO_STANDBY, ACTIVE); + DEGRADED_STANDBY.allowedTransitions + = ImmutableSet.of(DEGRADED_STANDBY_FOR_READER, DEGRADED_STANDBY_FOR_WRITER); + DEGRADED_STANDBY_FOR_WRITER.allowedTransitions = ImmutableSet.of(STANDBY, DEGRADED_STANDBY); + DEGRADED_STANDBY_FOR_READER.allowedTransitions = ImmutableSet.of(STANDBY, DEGRADED_STANDBY); + ACTIVE_WITH_OFFLINE_PEER.allowedTransitions = ImmutableSet.of(ACTIVE); + ABORT_TO_ACTIVE.allowedTransitions = ImmutableSet.of(ACTIVE, ACTIVE_NOT_IN_SYNC); + ABORT_TO_STANDBY.allowedTransitions = ImmutableSet.of(STANDBY); + ACTIVE_NOT_IN_SYNC_TO_STANDBY.allowedTransitions = ImmutableSet.of(ACTIVE_TO_STANDBY, ACTIVE_NOT_IN_SYNC); + ACTIVE_NOT_IN_SYNC_WITH_OFFLINE_PEER.allowedTransitions = ImmutableSet.of(ACTIVE_NOT_IN_SYNC); + } + + /** + * Get the wait time required to transition from this role to the target role, + * reading from configuration. + * @param targetRole the role to transition to + * @param conf configuration to read from + * @return wait time in milliseconds, or 0 if transition is not allowed + */ + public long checkTransitionAndGetWaitTime(ClusterRole targetRole, Configuration conf) + throws InvalidClusterRoleTransitionException { + if (!allowedTransitions.contains(targetRole)) { + throw new InvalidClusterRoleTransitionException("Cannot transition from " + this + " to " + targetRole); + } + + // Read wait times from configuration based on the transition + switch (this) { + case ACTIVE_NOT_IN_SYNC: + if (targetRole == ACTIVE) { + return conf.getLong(HA_SYNC_MODE_REFRESH_INTERVAL_MS, DEFAULT_HA_SYNC_MODE_REFRESH_INTERVAL_MS); Review Comment: Extracted required values in constructor for HAGroupStoreClient and now only checking if transition is valid or not. -- 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: issues-unsubscr...@phoenix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org