sanpwc commented on code in PR #759:
URL: https://github.com/apache/ignite-3/pull/759#discussion_r857834576


##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/raft/RebalanceRaftGroupEventsListener.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.table.distributed.raft;
+
+import static 
org.apache.ignite.internal.metastorage.client.Conditions.notExists;
+import static 
org.apache.ignite.internal.metastorage.client.Conditions.revision;
+import static org.apache.ignite.internal.metastorage.client.Operations.ops;
+import static org.apache.ignite.internal.metastorage.client.Operations.put;
+import static org.apache.ignite.internal.metastorage.client.Operations.remove;
+import static 
org.apache.ignite.internal.utils.RebalanceUtil.partAssignmentsPendingKey;
+import static 
org.apache.ignite.internal.utils.RebalanceUtil.partAssignmentsPlannedKey;
+import static 
org.apache.ignite.internal.utils.RebalanceUtil.partAssignmentsStableKey;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.ignite.configuration.schemas.table.TableConfiguration;
+import org.apache.ignite.internal.configuration.schema.ExtendedTableChange;
+import org.apache.ignite.internal.metastorage.MetaStorageManager;
+import org.apache.ignite.internal.metastorage.client.Entry;
+import org.apache.ignite.internal.metastorage.client.If;
+import org.apache.ignite.internal.raft.server.RaftGroupEventsListener;
+import org.apache.ignite.internal.util.ByteUtils;
+import org.apache.ignite.lang.ByteArray;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.raft.jraft.Status;
+import org.apache.ignite.raft.jraft.entity.PeerId;
+
+/**
+ * Listener for the raft group events, which must provide correct error 
handling of rebalance process
+ * and start new rebalance after the current one finished.
+ */
+public class RebalanceRaftGroupEventsListener implements 
RaftGroupEventsListener {
+
+    /** Meta storage manager. */
+    private final MetaStorageManager metaStorageMgr;
+
+    /** Table configuration instance. */
+    private final TableConfiguration tblConfiguration;
+
+    /** Unique partition id. */
+    private final String partId;
+
+    /** Partition number. */
+    private final int partNum;
+
+    /**
+     * Constructs new listener.
+     *
+     * @param metaStorageMgr Meta storage manager.
+     * @param tblConfiguration Table configuration.
+     * @param partId Partition id.
+     * @param partNum Partition number.
+     */
+    public RebalanceRaftGroupEventsListener(MetaStorageManager metaStorageMgr, 
TableConfiguration tblConfiguration, String partId,
+            int partNum) {
+        this.metaStorageMgr = metaStorageMgr;
+        this.tblConfiguration = tblConfiguration;
+        this.partId = partId;
+        this.partNum = partNum;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void onLeaderElected() {
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void onNewPeersConfigurationApplied(List<PeerId> peers) {
+        Map<ByteArray, Entry> keys = metaStorageMgr.getAll(
+                Set.of(partAssignmentsPlannedKey(partId), 
partAssignmentsPendingKey(partId))).join();
+
+        Entry plannedEntry = keys.get(partAssignmentsPlannedKey(partId));
+        Entry pendingEntry = keys.get(partAssignmentsPendingKey(partId));
+
+        tblConfiguration.change(ch -> {
+            List<List<ClusterNode>> assignments =
+                    (List<List<ClusterNode>>) 
ByteUtils.fromBytes(((ExtendedTableChange) ch).assignments());
+            assignments.set(partNum, ((List<ClusterNode>) 
ByteUtils.fromBytes(pendingEntry.value())));

Review Comment:
   According to rebalance doc, instead of `pendingEntry` we should use applied 
peers.
   ```
   **Trigger**: When leader applied new Configuration with list of resulting 
peers `<applied peer>`, it calls `onChangePeersCommitted(<applied peers>)`
   
   **Pseudocode**:
   ```
   metastoreInvoke: \\ atomic
       partition.assignments.stable = appliedPeers
   ```



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