override fun masterOperation()

in src/main/kotlin/org/opensearch/replication/action/autofollow/TransportAutoFollowMasterNodeAction.kt [65:96]


    override fun masterOperation(masterNodeReq: AutoFollowMasterNodeRequest, state: ClusterState, listener: ActionListener<AcknowledgedResponse>) {
        val request = masterNodeReq.autofollowReq
        var user = masterNodeReq.user
        launch(threadPool.coroutineContext()) {
            listener.completeWith {
                if (request.action == UpdateAutoFollowPatternRequest.Action.REMOVE) {
                    // Stopping the tasks and removing the context information from the cluster state
                    stopAutoFollowTask(request.connection, request.patternName)
                    metadataManager.deleteAutofollowMetadata(request.patternName, request.connection)
                }

                if (request.action == UpdateAutoFollowPatternRequest.Action.ADD) {
                    // Should start the task if there were no follow patterns before adding this
                    if(request.pattern == null) {
                        throw ReplicationException("Failed to update empty autofollow pattern")
                    }
                    // Pattern is same for leader and follower
                    val followerClusterRole = request.useRoles?.get(ReplicateIndexRequest.FOLLOWER_CLUSTER_ROLE)
                    val leaderClusterRole = request.useRoles?.get(ReplicateIndexRequest.LEADER_CLUSTER_ROLE)

                    indexScopedSettings.validate(request.settings,
                            false,
                            false)

                    metadataManager.addAutofollowMetadata(request.patternName, request.connection, request.pattern!!,
                            ReplicationOverallState.RUNNING, user, followerClusterRole, leaderClusterRole, request.settings)
                    startAutoFollowTask(request.connection, request.patternName)
                }
                AcknowledgedResponse(true)
            }
        }
    }