ritegarg commented on code in PR #2302:
URL: https://github.com/apache/phoenix/pull/2302#discussion_r2464080231


##########
phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/HAGroupStoreClient.java:
##########
@@ -374,110 +402,403 @@ public HAGroupStoreRecord 
getHAGroupStoreRecordFromPeer() throws IOException {
         if (!isHealthy) {
             throw new IOException("HAGroupStoreClient is not healthy");
         }
-        return fetchCacheRecord(this.peerPathChildrenCache, 
ClusterType.PEER).getLeft();
+        return 
fetchCacheRecordAndPopulateZKIfNeeded(this.peerPathChildrenCache, 
ClusterType.PEER).getLeft();
     }
 
-    private void initializeZNodeIfNeeded() throws IOException,
-            StaleHAGroupStoreRecordVersionException {
+    private void initializeZNodeIfNeeded() throws IOException, SQLException {
         // Sometimes the ZNode might not be available in local cache yet, so 
better to check
         // in ZK directly if we need to initialize
         Pair<HAGroupStoreRecord, Stat> cacheRecordFromZK =
                 
phoenixHaAdmin.getHAGroupStoreRecordInZooKeeper(this.haGroupName);
         HAGroupStoreRecord haGroupStoreRecord = cacheRecordFromZK.getLeft();
-        HAGroupState defaultHAGroupState = 
this.clusterRole.getDefaultHAGroupState();
-        // Initialize lastSyncTimeInMs only if we start in ACTIVE_NOT_IN_SYNC 
state
-        //  and ZNode is not already present
-        Long lastSyncTimeInMs = 
defaultHAGroupState.equals(HAGroupState.ACTIVE_NOT_IN_SYNC)
-                                    ? System.currentTimeMillis()
-                                    : null;
-        HAGroupStoreRecord newHAGroupStoreRecord = new HAGroupStoreRecord(
-            HAGroupStoreRecord.DEFAULT_PROTOCOL_VERSION,
-            haGroupName,
-            this.clusterRole.getDefaultHAGroupState(),
-            lastSyncTimeInMs
-        );
-        // Only update current ZNode if it doesn't have the same role as 
present in System Table.
-        // If not exists, then create ZNode
-        // TODO: Discuss if this approach is what reader needs.
-        if (haGroupStoreRecord != null &&
-                !haGroupStoreRecord.getClusterRole().equals(this.clusterRole)) 
{
-            phoenixHaAdmin.updateHAGroupStoreRecordInZooKeeper(haGroupName,
-                    newHAGroupStoreRecord, 
cacheRecordFromZK.getRight().getVersion());
-        } else if (haGroupStoreRecord == null) {
+        // Only if the ZNode is not present, we need to create it from System 
Table
+        if (haGroupStoreRecord == null) {
+            SystemTableHAGroupRecord systemTableRecord
+                    = getSystemTableHAGroupRecord(this.haGroupName);
+            Preconditions.checkNotNull(systemTableRecord,
+                    "System Table HAGroupRecord cannot be null");
+            HAGroupStoreRecord.HAGroupState defaultHAGroupState
+                    = 
systemTableRecord.getClusterRole().getDefaultHAGroupState();
+            Long lastSyncTimeInMs
+                    = 
defaultHAGroupState.equals(HAGroupStoreRecord.HAGroupState.ACTIVE_NOT_IN_SYNC)
+                    ? System.currentTimeMillis()

Review Comment:
   > i.e. last timestamp when cluster was in sync - at the time when API is 
called
   This might be hard to maintain as there can be sometime elapsed between 
calling and returning the response. It is better to keep as 0. 
   
   I propose we can adopt this convention
   0 (long default value) -> Sync
   -1 (Explicitly added when HAGroup ZNode is initialized) -> Unknown as 
cluster started in this mode
   Any other timestamp (long) -> Last known sync time. 
   
   Standby will observe any updates sent from Active(via ZK listener) and will 
just copy the timestamp in its HAGroupStoreRecord on standby ZK cluster. For 
eg. when cluster moves from AIS  to ANIS, standby will listen to this change 
and update local HAGroupStoreRecord in standby ZK with same value. When the 
record moves back from ANIS to AIS, standby peer will then detect the 
transition and just update the local ZK with same value. WDYT?
   



-- 
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]

Reply via email to