<commit_before>/* Copyright (c) 2011-2015 Stanford University
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include<fstream>
#include "Common.h"
#include "BackupService.h"
#include "CycleCounter.h"
#include "Cycles.h"
#include "MasterService.h"
#include "RawMetrics.h"
#include "ShortMacros.h"
#include "PerfStats.h"
#include "PingClient.h"
#include "PingService.h"
#include "ServerList.h"
#include "TimeTrace.h"
#include "CacheTrace.h"

namespace RAMCloud {

/**
 * Construct a PingService.
 *
 * \param context
 *      Overall information about the RAMCloud server. The caller is assumed
 *      to have associated a serverList with this context; if not, this service
 *      will not return a valid ServerList version in response to pings.
 *      The new service will be registered in this context.
 */
PingService::PingService(Context* context)
    : context(context)
    , ignoreKill(false)
    , returnUnknownId(false)
{
    context->services[WireFormat::PING_SERVICE] = this;
}

PingService::~PingService()
{
    context->services[WireFormat::PING_SERVICE] = NULL;
}

/**
 * Top-level service method to handle the GET_METRICS request.
 *
 * \copydetails Service::ping
 */
void
PingService::getMetrics(const WireFormat::GetMetrics::Request* reqHdr,
             WireFormat::GetMetrics::Response* respHdr,
             Rpc* rpc)
{
    string serialized;
    metrics->serialize(serialized);
    respHdr->messageLength = downCast<uint32_t>(serialized.length());
    rpc->replyPayload->appendCopy(serialized.c_str(), respHdr->messageLength);
}

/**
 * Top-level service method to handle the GET_SERVER_ID request.
 *
 * \copydetails Service::ping
 */
void
PingService::getServerId(const WireFormat::GetServerId::Request* reqHdr,
             WireFormat::GetServerId::Response* respHdr,
             Rpc* rpc)
{
    if (returnUnknownId) {
        returnUnknownId = false;
        respHdr->serverId = ServerId().getId();
    } else {
        respHdr->serverId = serverId.getId();
    }
}

/**
 * Top-level service method to handle the PING request.
 *
 * \copydetails Service::ping
 */
void
PingService::ping(const WireFormat::Ping::Request* reqHdr,
             WireFormat::Ping::Response* respHdr,
             Rpc* rpc)
{
    uint64_t ticks = 0;
    CycleCounter<> counter(&ticks);

    string callerId = ServerId(reqHdr->callerId).toString();

    ServerId serverId(reqHdr->callerId);
    if (serverId.isValid()) {
        // Careful, turning this into a real log message causes spurious
        // ping timeouts.
        TEST_LOG("Received ping request from server %s",
                 serverId.toString().c_str());
        if (!context->serverList->isUp(serverId)) {
            LOG(WARNING, "Received ping from server not in cluster: %s",
                    serverId.toString().c_str());
            respHdr->common.status = STATUS_CALLER_NOT_IN_CLUSTER;
        }<commit_msg>In PingService::getServerId, log when returning invalid id<commit_after>/* Copyright (c) 2011-2015 Stanford University
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include<fstream>
#include "Common.h"
#include "BackupService.h"
#include "CycleCounter.h"
#include "Cycles.h"
#include "MasterService.h"
#include "RawMetrics.h"
#include "ShortMacros.h"
#include "PerfStats.h"
#include "PingClient.h"
#include "PingService.h"
#include "ServerList.h"
#include "TimeTrace.h"
#include "CacheTrace.h"

namespace RAMCloud {

/**
 * Construct a PingService.
 *
 * \param context
 *      Overall information about the RAMCloud server. The caller is assumed
 *      to have associated a serverList with this context; if not, this service
 *      will not return a valid ServerList version in response to pings.
 *      The new service will be registered in this context.
 */
PingService::PingService(Context* context)
    : context(context)
    , ignoreKill(false)
    , returnUnknownId(false)
{
    context->services[WireFormat::PING_SERVICE] = this;
}

PingService::~PingService()
{
    context->services[WireFormat::PING_SERVICE] = NULL;
}

/**
 * Top-level service method to handle the GET_METRICS request.
 *
 * \copydetails Service::ping
 */
void
PingService::getMetrics(const WireFormat::GetMetrics::Request* reqHdr,
             WireFormat::GetMetrics::Response* respHdr,
             Rpc* rpc)
{
    string serialized;
    metrics->serialize(serialized);
    respHdr->messageLength = downCast<uint32_t>(serialized.length());
    rpc->replyPayload->appendCopy(serialized.c_str(), respHdr->messageLength);
}

/**
 * Top-level service method to handle the GET_SERVER_ID request.
 *
 * \copydetails Service::ping
 */
void
PingService::getServerId(const WireFormat::GetServerId::Request* reqHdr,
             WireFormat::GetServerId::Response* respHdr,
             Rpc* rpc)
{
    if (returnUnknownId) {
        returnUnknownId = false;
        respHdr->serverId = ServerId().getId();
    } else {
        respHdr->serverId = serverId.getId();
        if (!serverId.isValid()) {
            RAMCLOUD_LOG(NOTICE, "Returning invalid server id");
        }
    }
}

/**
 * Top-level service method to handle the PING request.
 *
 * \copydetails Service::ping
 */
void
PingService::ping(const WireFormat::Ping::Request* reqHdr,
             WireFormat::Ping::Response* respHdr,
             Rpc* rpc)
{
    uint64_t ticks = 0;
    CycleCounter<> counter(&ticks);

    string callerId = ServerId(reqHdr->callerId).toString();

    ServerId serverId(reqHdr->callerId);
    if (serverId.isValid()) {
        // Careful, turning this into a real log message causes spurious
        // ping timeouts.
        TEST_LOG("Received ping request from server %s",
                 serverId.toString().c_str());
        if (!context->serverList->isUp(serverId)) {
            LOG(WARNING, "Received ping from server not in cluster: %s",
                    serverId.toString().c_str());
            respHdr->common.status = STATUS_CALLER_NOT_IN_CLUSTER;
        }
{'fraction_non_alphanumeric': 0.10666849240038341, 'fraction_numerical': 0.003560180747637957, 'mean_word_length': 3.5058605798889575, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5959890', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2113, 'n_words': 647}