<commit_before>/*
 * Copyright 2007-2019 Content Management AG
 * All rights reserved.
 *
 * author: <NAME> <<EMAIL>>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "Control.hxx"
#include "Instance.hxx"
#include "fcache.hxx"
#include "nfs/Cache.hxx"
#include "control/Distribute.hxx"
#include "control/Server.hxx"
#include "control/Local.hxx"
#include "translation/Request.hxx"
#include "translation/Cache.hxx"
#include "translation/Protocol.hxx"
#include "translation/InvalidateParser.hxx"
#include "pool/tpool.hxx"
#include "pool/pool.hxx"
#include "net/UdpDistribute.hxx"
#include "net/SocketAddress.hxx"
#include "net/IPv4Address.hxx"
#include "io/Logger.hxx"
#include "util/ConstBuffer.hxx"
#include "util/Exception.hxx"
#include "util/Macros.hxx"
#include "util/WritableBuffer.hxx"
#include "stopwatch.hxx"

#include <assert.h>
#include <string.h>

using namespace BengProxy;

static void
control_tcache_invalidate(BpInstance *instance, ConstBuffer<void> payload)
{
	if (instance->translation_cache == nullptr)
		return;

	if (payload.empty()) {
		/* flush the translation cache if the payload is empty */
		instance->translation_cache->Flush();
		return;
	}

	const TempPoolLease tpool;

	TranslationInvalidateRequest request;

	try {
		request = ParseTranslationInvalidateRequest(*tpool,
							    payload.data, payload.size);
	} catch (...) {
		LogConcat(2, "control",
			  "malformed TCACHE_INVALIDATE control packet: ",
			  std::current_exception());
		return;
	}

	instance->translation_cache->Invalidate(request,
						ConstBuffer<TranslationCommand>(request.commands.raw(),
										request.commands.size()),
						request.site);
}

static void
query_stats(BpInstance *instance, ControlServer *server,
	    SocketAddress address)
{
	if (address.GetSize() == 0)
		/* TODO: this packet was forwarded by the master process, and
		   has no source address; however, the master process must get
		   statistics from all worker processes (even those that have
		   exited already) */
		return;

	const auto stats = instance->GetStats();

	try {
		server->Reply(address,
			      ControlCommand::STATS, &stats, sizeof(stats));
	} catch (...) {
		LogConcat(3, "control", std::current_exception());
	}
}

static void
HandleStopwatchPipe(ConstBuffer<void> payload,
		    WritableBuffer<UniqueFileDescriptor> fds)
{
	if (!payload.empty() || fds.size != 1)
		throw std::runtime_error("Malformed STOPWATCH_PIPE packet");<commit_msg>bp/Control: verify that the STOPWATCH_PIPE fd is a pipe<commit_after>/*
 * Copyright 2007-2019 Content Management AG
 * All rights reserved.
 *
 * author: <NAME> <<EMAIL>>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "Control.hxx"
#include "Instance.hxx"
#include "fcache.hxx"
#include "nfs/Cache.hxx"
#include "control/Distribute.hxx"
#include "control/Server.hxx"
#include "control/Local.hxx"
#include "translation/Request.hxx"
#include "translation/Cache.hxx"
#include "translation/Protocol.hxx"
#include "translation/InvalidateParser.hxx"
#include "pool/tpool.hxx"
#include "pool/pool.hxx"
#include "net/UdpDistribute.hxx"
#include "net/SocketAddress.hxx"
#include "net/IPv4Address.hxx"
#include "io/Logger.hxx"
#include "util/ConstBuffer.hxx"
#include "util/Exception.hxx"
#include "util/Macros.hxx"
#include "util/WritableBuffer.hxx"
#include "stopwatch.hxx"

#include <assert.h>
#include <string.h>

using namespace BengProxy;

static void
control_tcache_invalidate(BpInstance *instance, ConstBuffer<void> payload)
{
	if (instance->translation_cache == nullptr)
		return;

	if (payload.empty()) {
		/* flush the translation cache if the payload is empty */
		instance->translation_cache->Flush();
		return;
	}

	const TempPoolLease tpool;

	TranslationInvalidateRequest request;

	try {
		request = ParseTranslationInvalidateRequest(*tpool,
							    payload.data, payload.size);
	} catch (...) {
		LogConcat(2, "control",
			  "malformed TCACHE_INVALIDATE control packet: ",
			  std::current_exception());
		return;
	}

	instance->translation_cache->Invalidate(request,
						ConstBuffer<TranslationCommand>(request.commands.raw(),
										request.commands.size()),
						request.site);
}

static void
query_stats(BpInstance *instance, ControlServer *server,
	    SocketAddress address)
{
	if (address.GetSize() == 0)
		/* TODO: this packet was forwarded by the master process, and
		   has no source address; however, the master process must get
		   statistics from all worker processes (even those that have
		   exited already) */
		return;

	const auto stats = instance->GetStats();

	try {
		server->Reply(address,
			      ControlCommand::STATS, &stats, sizeof(stats));
	} catch (...) {
		LogConcat(3, "control", std::current_exception());
	}
}

static void
HandleStopwatchPipe(ConstBuffer<void> payload,
		    WritableBuffer<UniqueFileDescriptor> fds)
{
	if (!payload.empty() || fds.size != 1 || !fds.front().IsPipe())
		throw std::runtime_error("Malformed STOPWATCH_PIPE packet");
{'fraction_non_alphanumeric': 0.10362347907474262, 'fraction_numerical': 0.003476400588313946, 'mean_word_length': 4.590433482810164, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 35, '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': '1157543', 'n_tokens_mistral': 2368, 'n_tokens_neox': 2127, 'n_words': 730}