<commit_before>/*
 * 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.
 */
/*
 * Copyright 2015 Cloudius Systems
 *
 * Modified by Cloudius Systems
 */
package org.apache.cassandra.net;

import java.lang.management.ManagementFactory;
import java.net.*;
import java.util.*;
import java.util.Map.Entry;

import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.apache.cassandra.metrics.DroppedMessageMetrics;

import com.scylladb.jmx.api.APIClient;
import com.yammer.metrics.core.APISettableMeter;

public final class MessagingService implements MessagingServiceMBean {
    static final int INTERVAL = 1000; // update every 1second
    public static final String MBEAN_NAME = "org.apache.cassandra.net:type=MessagingService";
    private static final java.util.logging.Logger logger = java.util.logging.Logger
            .getLogger(MessagingService.class.getName());
    Map<String, DroppedMessageMetrics> dropped;
    private APIClient c = new APIClient();
    Map<String, Long> resent_timeout = new HashMap<String, Long>();
    private final ObjectName jmxObjectName;
    private long recentTimeoutCount;

    /* All verb handler identifiers */
    public enum Verb
    {
        MUTATION,
        @Deprecated BINARY,
        READ_REPAIR,
        READ,
        REQUEST_RESPONSE, // client-initiated reads and writes
        @Deprecated STREAM_INITIATE,
        @Deprecated STREAM_INITIATE_DONE,
        @Deprecated STREAM_REPLY,
        @Deprecated STREAM_REQUEST,
        RANGE_SLICE,
        @Deprecated BOOTSTRAP_TOKEN,
        @Deprecated TREE_REQUEST,
        @Deprecated TREE_RESPONSE,
        @Deprecated JOIN,
        GOSSIP_DIGEST_SYN,
        GOSSIP_DIGEST_ACK,
        GOSSIP_DIGEST_ACK2,
        @Deprecated DEFINITIONS_ANNOUNCE,
        DEFINITIONS_UPDATE,
        TRUNCATE,
        SCHEMA_CHECK,
        @Deprecated INDEX_SCAN,
        REPLICATION_FINISHED,
        INTERNAL_RESPONSE, // responses to internal calls
        COUNTER_MUTATION,
        @Deprecated STREAMING_REPAIR_REQUEST,
        @Deprecated STREAMING_REPAIR_RESPONSE,
        SNAPSHOT, // Similar to nt snapshot
        MIGRATION_REQUEST,
        GOSSIP_SHUTDOWN,
        _TRACE, // dummy verb so we can use MS.droppedMessages
        ECHO,
        REPAIR_MESSAGE,
        // use as padding for backwards compatability where a previous version needs to validate a verb from the future.
        PAXOS_PREPARE,
        PAXOS_PROPOSE,
        PAXOS_COMMIT,
        PAGED_RANGE,
        // remember to add new verbs at the end, since we serialize by ordinal
        UNUSED_1,
        UNUSED_2,
        UNUSED_3,
        ;
    }

    public void log(String str) {
        logger.finest(str);
    }

    private static Timer timer = new Timer("Dropped messages");

    public MessagingService() {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        try {
            jmxObjectName = new ObjectName(MBEAN_NAME);
            mbs.registerMBean(this, jmxObjectName);
            // mbs.registerMBean(StreamManager.instance, new ObjectName(
            // StreamManager.OBJECT_NAME));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        timer.schedule(new CheckDroppedMessages(), INTERVAL, INTERVAL);
    }

    static MessagingService instance = new MessagingService();

    private static final class CheckDroppedMessages extends TimerTask {
        @Override
        public void run() {
            if (instance.dropped == null) {
                instance.dropped = new HashMap<String, DroppedMessageMetrics>();
                for (Verb v : Verb.values()) {
                    instance.dropped.put(v.name(), new DroppedMessageMetrics(v));
                }
            }
            Map<String, Integer> val = instance.getDroppedMessages();
            for (String k : val.keySet()) {
                APISettableMeter meter = instance.dropped.get(k).getMeter();
                meter.set(val.get(k));
                meter.tick();
            }
        }
    }

    public static MessagingService getInstance() {
        return instance;
    }

    /**
     * Pending tasks for Command(Mutations, Read etc) TCP Connections
     */
    public Map<String, Integer> getCommandPendingTasks() {
        log(" getCommandPendingTasks()");
        return c.getMapStringIntegerValue("/messaging_service/messages/pending");
    }

    /**
     * Completed tasks for Command(Mutations, Read etc) TCP Connections
     */<commit_msg>MessagingService: Ignore exception on the dropped messages thread<commit_after>/*
 * 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.
 */
/*
 * Copyright 2015 Cloudius Systems
 *
 * Modified by Cloudius Systems
 */
package org.apache.cassandra.net;

import java.lang.management.ManagementFactory;
import java.net.*;
import java.util.*;
import java.util.Map.Entry;

import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.ws.rs.ProcessingException;

import org.apache.cassandra.metrics.DroppedMessageMetrics;

import com.scylladb.jmx.api.APIClient;
import com.yammer.metrics.core.APISettableMeter;

public final class MessagingService implements MessagingServiceMBean {
    static final int INTERVAL = 1000; // update every 1second
    public static final String MBEAN_NAME = "org.apache.cassandra.net:type=MessagingService";
    private static final java.util.logging.Logger logger = java.util.logging.Logger
            .getLogger(MessagingService.class.getName());
    Map<String, DroppedMessageMetrics> dropped;
    private APIClient c = new APIClient();
    Map<String, Long> resent_timeout = new HashMap<String, Long>();
    private final ObjectName jmxObjectName;
    private long recentTimeoutCount;

    /* All verb handler identifiers */
    public enum Verb
    {
        MUTATION,
        @Deprecated BINARY,
        READ_REPAIR,
        READ,
        REQUEST_RESPONSE, // client-initiated reads and writes
        @Deprecated STREAM_INITIATE,
        @Deprecated STREAM_INITIATE_DONE,
        @Deprecated STREAM_REPLY,
        @Deprecated STREAM_REQUEST,
        RANGE_SLICE,
        @Deprecated BOOTSTRAP_TOKEN,
        @Deprecated TREE_REQUEST,
        @Deprecated TREE_RESPONSE,
        @Deprecated JOIN,
        GOSSIP_DIGEST_SYN,
        GOSSIP_DIGEST_ACK,
        GOSSIP_DIGEST_ACK2,
        @Deprecated DEFINITIONS_ANNOUNCE,
        DEFINITIONS_UPDATE,
        TRUNCATE,
        SCHEMA_CHECK,
        @Deprecated INDEX_SCAN,
        REPLICATION_FINISHED,
        INTERNAL_RESPONSE, // responses to internal calls
        COUNTER_MUTATION,
        @Deprecated STREAMING_REPAIR_REQUEST,
        @Deprecated STREAMING_REPAIR_RESPONSE,
        SNAPSHOT, // Similar to nt snapshot
        MIGRATION_REQUEST,
        GOSSIP_SHUTDOWN,
        _TRACE, // dummy verb so we can use MS.droppedMessages
        ECHO,
        REPAIR_MESSAGE,
        // use as padding for backwards compatability where a previous version needs to validate a verb from the future.
        PAXOS_PREPARE,
        PAXOS_PROPOSE,
        PAXOS_COMMIT,
        PAGED_RANGE,
        // remember to add new verbs at the end, since we serialize by ordinal
        UNUSED_1,
        UNUSED_2,
        UNUSED_3,
        ;
    }

    public void log(String str) {
        logger.finest(str);
    }

    private static Timer timer = new Timer("Dropped messages");

    public MessagingService() {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        try {
            jmxObjectName = new ObjectName(MBEAN_NAME);
            mbs.registerMBean(this, jmxObjectName);
            // mbs.registerMBean(StreamManager.instance, new ObjectName(
            // StreamManager.OBJECT_NAME));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        timer.schedule(new CheckDroppedMessages(), INTERVAL, INTERVAL);
    }

    static MessagingService instance = new MessagingService();

    private static final class CheckDroppedMessages extends TimerTask {
        int connection_failure = 0;
        int report_error = 1;
        @Override
        public void run() {
            if (instance.dropped == null) {
                instance.dropped = new HashMap<String, DroppedMessageMetrics>();
                for (Verb v : Verb.values()) {
                    instance.dropped.put(v.name(), new DroppedMessageMetrics(v));
                }
            }
            try {
                Map<String, Integer> val = instance.getDroppedMessages();
                for (String k : val.keySet()) {
                    APISettableMeter meter = instance.dropped.get(k).getMeter();
                    meter.set(val.get(k));
                    meter.tick();
                }
                connection_failure = 0;
                report_error = 1;
            }  catch (ProcessingException e) {
              // Connection problem, No need to do anything, just retry.
            } catch (Exception e) {
                connection_failure++;
                if ((connection_failure & report_error) == report_error) {
                    logger.info("Dropped messages failed with " + e.getMessage() + " total error reported " + connection_failure);
                    report_error <<= 1;
                }
            }
        }
    }

    public static MessagingService getInstance() {
        return instance;
    }

    /**
     * Pending tasks for Command(Mutations, Read etc) TCP Connections
     */
    public Map<String, Integer> getCommandPendingTasks() {
        log(" getCommandPendingTasks()");
        return c.getMapStringIntegerValue("/messaging_service/messages/pending");
    }

    /**
     * Completed tasks for Command(Mutations, Read etc) TCP Connections
     */
{'fraction_non_alphanumeric': 0.07884988534133004, 'fraction_numerical': 0.0034397600987828543, 'mean_word_length': 2.518150791188334, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '2651167', 'n_tokens_mistral': 3204, 'n_tokens_neox': 3074, 'n_words': 877}