hazelcast-master/hazelcast/src/main/java/com/hazelcast/cp/internal/operation/RaftReplicateOp.java
/*
 * Copyright (c) 2008-2023, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed 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 com.hazelcast.cp.internal.operation;

import com.hazelcast.cp.CPGroupId;
import com.hazelcast.cp.exception.CPGroupDestroyedException;
import com.hazelcast.cp.exception.NotLeaderException;
import com.hazelcast.cp.internal.RaftOp;
import com.hazelcast.cp.internal.RaftService;
import com.hazelcast.cp.internal.RaftSystemOperation;
import com.hazelcast.cp.internal.raft.impl.RaftNode;
import com.hazelcast.cp.internal.raft.impl.RaftNodeStatus;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
import com.hazelcast.spi.impl.InternalCompletableFuture;
import com.hazelcast.spi.impl.operationservice.Operation;

import java.io.IOException;
import java.util.function.BiConsumer;

import static com.hazelcast.cp.internal.RaftService.CP_SUBSYSTEM_EXECUTOR;
import static com.hazelcast.internal.util.ConcurrencyUtil.CALLER_RUNS;

/**
 * The base class that replicates the given {@link RaftOp}
 * to the target Raft group
 * <p>
 * Please note that this operation is not a {@link RaftOp},
 * so it is not handled via the Raft layer.
 */
public abstract class RaftReplicateOp extends Operation implements IdentifiedDataSerializable, RaftSystemOperation,
                                                                   BiConsumer<Object, Throwable> {

    private CPGroupId groupId;

    RaftReplicateOp() {
    }

    RaftReplicateOp(CPGroupId groupId) {
        this.groupId = groupId;
    }

    @Override
    public final void run() {
        RaftService service = getService();
        RaftNode raftNode = service.getOrInitRaftNode(groupId);
        if (raftNode == null) {
            onNullRaftNode(service);
            return;
        }
        if (raftNode.getStatus() == RaftNodeStatus.STEPPED_DOWN) {
            sendResponse(new NotLeaderException(groupId, service.getLocalCPEndpoint(), null));
            getNodeEngine().getExecutionService().execute(CP_SUBSYSTEM_EXECUTOR, () -> service.stepDownRaftNode(groupId));
            return;
        }

        replicate(raftNode).whenCompleteAsync(this, CALLER_RUNS);
    }

    private void onNullRaftNode(RaftService service) {
        if (service.isRaftGroupDestroyed(groupId)) {
            sendResponse(new CPGroupDestroyedException(groupId));
        } else if (!groupId.equals(service.getMetadataGroupId())
                && service.isDiscoveryCompleted() && service.isStartCompleted()) {
            sendResponseAfterMetadataCheck(service);
        } else {
            sendResponse(new NotLeaderException(groupId, service.getLocalCPEndpoint(), null));
        }
    }

    private void sendResponseAfterMetadataCheck(RaftService service) {
        getNodeEngine().getExecutionService().execute(CP_SUBSYSTEM_EXECUTOR, () ->
            service.getCPGroup(groupId).whenCompleteAsync((group, throwable) -> {
                        // Checking if groupId is known by the METADATA group.
                        // If it is unaware, it means that the CP Subsystem has lost information about CPGroup after
                        // the restart, and we can respond with a non-retriable exception.
                        if (group == null && throwable == null) {
                            getLogger().info("There is no info about " + groupId + " in the METADATA group");
                            sendResponse(new CPGroupDestroyedException(groupId));
                        } else {
                            sendResponse(new NotLeaderException(groupId, service.getLocalCPEndpoint(), null));
                        }
                    },
                    CALLER_RUNS)
        );
    }

    protected abstract InternalCompletableFuture replicate(RaftNode raftNode);

    @Override
    public void accept(Object response, Throwable throwable) {
        if (throwable == null) {
            sendResponse(response);
        } else {
            sendResponse(throwable);
        }
    }

    @Override
    public final boolean returnsResponse() {
        return false;
    }

    @Override
    public final boolean validatesTarget() {
        return false;
    }

    @Override
    public final String getServiceName() {
        return RaftService.SERVICE_NAME;
    }

    @Override
    protected void writeInternal(ObjectDataOutput out) throws IOException {
        super.writeInternal(out);
        out.writeObject(groupId);
    }

    @Override
    protected void readInternal(ObjectDataInput in) throws IOException {
        super.readInternal(in);
        groupId = in.readObject();
    }

    @Override
    protected void toString(StringBuilder sb) {
        super.toString(sb);
        sb.append(", groupId=").append(groupId);
    }
}
