vldpyatkov commented on code in PR #927:
URL: https://github.com/apache/ignite-3/pull/927#discussion_r921005077


##########
modules/replicator/src/main/java/org/apache/ignite/internal/replicator/Replica.java:
##########
@@ -0,0 +1,161 @@
+/*
+ * 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.replicator;
+
+import org.apache.ignite.internal.replicator.message.ReadOnlyMultyEntryRequest;
+import org.apache.ignite.internal.replicator.message.ReadOnlyRequest;
+import 
org.apache.ignite.internal.replicator.message.ReadOnlySingleEntryRequest;
+import 
org.apache.ignite.internal.replicator.message.ReadWriteMultiEntryRequest;
+import org.apache.ignite.internal.replicator.message.ReadWriteRequest;
+import 
org.apache.ignite.internal.replicator.message.ReadWriteSingleEntryRequest;
+import org.apache.ignite.internal.replicator.message.ReplicaMessagesFactory;
+import org.apache.ignite.internal.replicator.message.ReplicaRequest;
+import org.apache.ignite.internal.replicator.message.ReplicaResponse;
+import 
org.apache.ignite.internal.replicator.message.container.MultiEntryContainer;
+import 
org.apache.ignite.internal.replicator.message.container.SingleEntryContainer;
+import org.apache.ignite.lang.IgniteInternalException;
+import org.apache.ignite.lang.IgniteStringFormatter;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.network.TopologyService;
+
+/**
+ * Replica server.
+ * TODO:IGNITE-17257 Implement Replica server-side logic.
+ */
+public class Replica implements AutoCloseable {
+    /** Replicator network message factory. */
+    private static final ReplicaMessagesFactory REPLICA_MESSAGES_FACTORY = new 
ReplicaMessagesFactory();
+
+    /** Replica group identity, this id is the same as the considered 
partition's id. */
+    private final String partitionId;
+
+    /** Local cluster node. */
+    private final ClusterNode localNode;
+
+    /** Replica listener. */
+    private final ReplicaListener listener;
+
+    /**
+     * The constructor of replica server.
+     *
+     * @param partitionId Identity.
+     * @param topologyService Topology service.
+     * @param listener Replica listener.
+     */
+    public Replica(
+            String partitionId,
+            TopologyService topologyService,
+            ReplicaListener listener
+    ) {
+        this.partitionId = partitionId;
+        this.localNode = topologyService.localMember();
+        this.listener = listener;
+    }
+
+    /**
+     * It is an internal method to applies a RW request to replicator.
+     *
+     * @param req Request to apply in replicator.
+     * @return Response.
+     */
+    private ReplicaResponse applyReadWriteCommandInternal(ReadWriteRequest 
req) {
+        //TODO: Check locks here.
+        if (req instanceof ReadWriteSingleEntryRequest) {
+            var request = (ReadWriteSingleEntryRequest) req;
+
+            var data = (SingleEntryContainer) 
listener.applyReadWrite(req.txId(), request.entryContainer(), req.opType());
+
+            return REPLICA_MESSAGES_FACTORY
+                    .singleEntryResponse()
+                    .partitionId(partitionId)
+                    .entryContainer(data)
+                    .build();
+        } else if (req instanceof ReadWriteMultiEntryRequest) {
+            var request = (ReadWriteMultiEntryRequest) req;
+
+            var data = (MultiEntryContainer) 
listener.applyReadWrite(req.txId(), request.entryContainer(), req.opType());
+
+            return REPLICA_MESSAGES_FACTORY
+                    .multiEntryResponse()
+                    .partitionId(partitionId)
+                    .entryContainer(data)
+                    .build();
+        } else {
+            throw new IgniteInternalException(
+                    IgniteStringFormatter.format("Unsupported request type 
[type={}]", req.getClass().getSimpleName()));
+        }
+    }
+
+    /**
+     * It is an internal method to applies a RO request to replicator.
+     *
+     * @param req Request to apply in replicator.
+     * @return Response.
+     */
+    private ReplicaResponse applyReadOnlyCommandInternal(ReadOnlyRequest req) {
+        //TODO: Check locks here.
+        if (req instanceof ReadOnlySingleEntryRequest) {
+            var request = (ReadOnlySingleEntryRequest) req;
+
+            var data = (SingleEntryContainer) 
listener.applyReadOnly(req.timestamp(), request.entryContainer(), req.opType());
+
+            return REPLICA_MESSAGES_FACTORY
+                    .singleEntryResponse()
+                    .partitionId(partitionId)
+                    .entryContainer(data)
+                    .build();
+        } else if (req instanceof ReadOnlyMultyEntryRequest) {
+            var request = (ReadOnlyMultyEntryRequest) req;
+
+            var data = (MultiEntryContainer) 
listener.applyReadOnly(req.timestamp(), request.entryContainer(), req.opType());
+
+            return REPLICA_MESSAGES_FACTORY
+                    .multiEntryResponse()
+                    .partitionId(partitionId)
+                    .entryContainer(data)
+                    .build();
+        } else {
+            throw new IgniteInternalException(
+                    IgniteStringFormatter.format("Unsupported request type 
[type={}]", req.getClass().getSimpleName()));
+        }
+    }
+
+    /**
+     * Process a replication request on the replica.
+     *
+     * @param request Request to replication.
+     * @return Response.
+     */
+    public ReplicaResponse processRequest(ReplicaRequest request) { // define 
proper set of exceptions that might be thrown.
+        assert partitionId.equals(request.partitionId()) : 
IgniteStringFormatter.format(
+                "Request does not match to the replica [reqId={}, 
replicaId={}]", request.partitionId(), partitionId);
+
+        //TODO: Check replica is alive.
+        if (request instanceof ReadWriteRequest) {
+            return applyReadWriteCommandInternal((ReadWriteRequest) request);
+        }
+
+        return applyReadOnlyCommandInternal((ReadOnlyRequest) request);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void close() throws Exception {
+        //Close all resources.

Review Comment:
   There are no resources yet.
   Do you mean the best way is refusing of AutoClosable interface until the 
resources will be appeared?



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