<commit_before>/*
 * Copyright 2018 The Hekate Project
 *
 * The Hekate Project 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 io.hekate.coordinate.internal;

import io.hekate.cluster.ClusterHash;
import io.hekate.cluster.ClusterNode;
import io.hekate.cluster.ClusterNodeId;
import io.hekate.cluster.ClusterTopology;
import io.hekate.coordinate.CoordinationMember;
import io.hekate.coordinate.CoordinationRequestCallback;
import io.hekate.core.internal.util.ArgAssert;
import io.hekate.failover.FailoverPolicyBuilder;
import io.hekate.messaging.MessagingChannel;
import io.hekate.messaging.unicast.ReplyDecision;
import io.hekate.messaging.unicast.Response;
import io.hekate.messaging.unicast.ResponseCallback;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static io.hekate.messaging.unicast.ReplyDecision.COMPLETE;
import static io.hekate.messaging.unicast.ReplyDecision.REJECT;

class DefaultCoordinationMember implements CoordinationMember {
    private static final Logger log = LoggerFactory.getLogger(DefaultCoordinationMember.class);

    private static final boolean DEBUG = log.isDebugEnabled();

    private final String processName;

    private final ClusterNode node;

    private final ClusterTopology topology;

    private final boolean coordinator;

    private final MessagingChannel<CoordinationProtocol> channel;

    private final ExecutorService async;

    private final ReentrantLock lock = new ReentrantLock();

    private final Set<Future<?>> requestFutures = Collections.newSetFromMap(new IdentityHashMap<>());

    private final ClusterNode localNode;

    private volatile boolean disposed;

    public DefaultCoordinationMember(
        String processName,
        ClusterNode node,
        ClusterTopology topology,
        boolean coordinator,
        MessagingChannel<CoordinationProtocol> channel,
        ExecutorService async,
        long failoverDelay
    ) {
        assert processName != null : "Coordination process name is null.";
        assert node != null : "Node is null.";
        assert topology != null : "Topology is null.";
        assert channel != null : "Channel is null.";
        assert async != null : "Executor service is null.";

        this.processName = processName;
        this.localNode = topology.localNode();
        this.node = node;
        this.topology = topology;
        this.coordinator = coordinator;
        this.async = async;

        this.channel = channel.forNode(node)
            .withAffinity(processName)
            .withFailover(new FailoverPolicyBuilder()
                // Retry to death.
                .withRetryUntil(ctx -> !disposed)
                .withAlwaysRetrySameNode()
                .withConstantRetryDelay(failoverDelay)
            );

    }

    @Override
    public boolean isCoordinator() {
        return coordinator;
    }

    @Override
    public ClusterNode node() {
        return node;
    }

    @Override
    public void request(Object request, CoordinationRequestCallback callback) {
        ArgAssert.notNull(request, "Request");
        ArgAssert.notNull(callback, "Callback");

        if (DEBUG) {
            log.debug("Sending coordination request [to={}, message={}]", node, request);
        }

        CompletableFuture<Object> future = newRequestFuture(request, callback);

        boolean enqueued = false;

        lock.lock();

        try {
            if (!disposed) {
                enqueued = true;

                requestFutures.add(future);
            }
        } finally {
            lock.unlock();
        }

        if (enqueued) {
            ClusterNodeId from = localNode.id();
            ClusterHash clusterHash = topology.hash();

            CoordinationProtocol.Request req = new CoordinationProtocol.Request(processName, from, clusterHash, request);

            channel.request(req, new ResponseCallback<CoordinationProtocol>() {
                @Override
                public ReplyDecision accept(Throwable err, Response<CoordinationProtocol> reply) {
                    if (future.isDone()) {
                        if (DEBUG) {
                            log.debug("Skipped response since request future is already completed [from={}, response={}]", from, reply);
                        }

                        return COMPLETE;
                    } else if (err != null || reply.is(CoordinationProtocol.Reject.class)) {
                        if (DEBUG) {
                            log.debug("Got a reject [from={}, request={}]", from, req);
                        }

                        return REJECT;
                    } else {
                        CoordinationProtocol.Response response = reply.get(CoordinationProtocol.Response.class);

                        future.complete(response.response());
<commit_msg>Debug logging improvements in DefaultCoordinationMember.<commit_after>/*
 * Copyright 2018 The Hekate Project
 *
 * The Hekate Project 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 io.hekate.coordinate.internal;

import io.hekate.cluster.ClusterHash;
import io.hekate.cluster.ClusterNode;
import io.hekate.cluster.ClusterNodeId;
import io.hekate.cluster.ClusterTopology;
import io.hekate.coordinate.CoordinationMember;
import io.hekate.coordinate.CoordinationRequestCallback;
import io.hekate.core.internal.util.ArgAssert;
import io.hekate.failover.FailoverPolicyBuilder;
import io.hekate.messaging.MessagingChannel;
import io.hekate.messaging.unicast.ReplyDecision;
import io.hekate.messaging.unicast.Response;
import io.hekate.messaging.unicast.ResponseCallback;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static io.hekate.messaging.unicast.ReplyDecision.COMPLETE;
import static io.hekate.messaging.unicast.ReplyDecision.REJECT;

class DefaultCoordinationMember implements CoordinationMember {
    private static final Logger log = LoggerFactory.getLogger(DefaultCoordinationMember.class);

    private static final boolean DEBUG = log.isDebugEnabled();

    private final String processName;

    private final ClusterNode node;

    private final ClusterTopology topology;

    private final boolean coordinator;

    private final MessagingChannel<CoordinationProtocol> channel;

    private final ExecutorService async;

    private final ReentrantLock lock = new ReentrantLock();

    private final Set<Future<?>> requestFutures = Collections.newSetFromMap(new IdentityHashMap<>());

    private final ClusterNode localNode;

    private volatile boolean disposed;

    public DefaultCoordinationMember(
        String processName,
        ClusterNode node,
        ClusterTopology topology,
        boolean coordinator,
        MessagingChannel<CoordinationProtocol> channel,
        ExecutorService async,
        long failoverDelay
    ) {
        assert processName != null : "Coordination process name is null.";
        assert node != null : "Node is null.";
        assert topology != null : "Topology is null.";
        assert channel != null : "Channel is null.";
        assert async != null : "Executor service is null.";

        this.processName = processName;
        this.localNode = topology.localNode();
        this.node = node;
        this.topology = topology;
        this.coordinator = coordinator;
        this.async = async;

        this.channel = channel.forNode(node)
            .withAffinity(processName)
            .withFailover(new FailoverPolicyBuilder()
                // Retry to death.
                .withRetryUntil(ctx -> !disposed)
                .withAlwaysRetrySameNode()
                .withConstantRetryDelay(failoverDelay)
            );

    }

    @Override
    public boolean isCoordinator() {
        return coordinator;
    }

    @Override
    public ClusterNode node() {
        return node;
    }

    @Override
    public void request(Object request, CoordinationRequestCallback callback) {
        ArgAssert.notNull(request, "Request");
        ArgAssert.notNull(callback, "Callback");

        if (DEBUG) {
            log.debug("Sending coordination request [to={}, message={}]", node, request);
        }

        CompletableFuture<Object> future = newRequestFuture(request, callback);

        boolean enqueued = false;

        lock.lock();

        try {
            if (!disposed) {
                enqueued = true;

                requestFutures.add(future);
            }
        } finally {
            lock.unlock();
        }

        if (enqueued) {
            ClusterNodeId from = localNode.id();
            ClusterHash clusterHash = topology.hash();

            CoordinationProtocol.Request req = new CoordinationProtocol.Request(processName, from, clusterHash, request);

            channel.request(req, new ResponseCallback<CoordinationProtocol>() {
                @Override
                public ReplyDecision accept(Throwable err, Response<CoordinationProtocol> reply) {
                    if (future.isDone()) {
                        if (DEBUG) {
                            log.debug("Skipped response since request future is already completed [from={}, response={}]", from, reply);
                        }

                        return COMPLETE;
                    } else if (err != null) {
                        if (DEBUG) {
                            log.debug("Got an error [from={}, error={}, request={}]", from, err.toString(), req);
                        }

                        return REJECT;
                    } else if (reply.is(CoordinationProtocol.Reject.class)) {
                        if (DEBUG) {
                            log.debug("Got a reject [from={}, request={}]", from, req);
                        }

                        return REJECT;
                    } else {
                        CoordinationProtocol.Response response = reply.get(CoordinationProtocol.Response.class);

                        future.complete(response.response());

{'fraction_non_alphanumeric': 0.08721480843736548, 'fraction_numerical': 0.0017219113215669393, 'mean_word_length': 2.5232029117379438, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7253972', 'n_tokens_mistral': 3032, 'n_tokens_neox': 2875, 'n_words': 799}