/*
** Copyright (C) 2022-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#include <glib.h>
#include <string>
#include <thread>
#include <vector>
#include <iostream>
#include <fstream>

#include <utils/mu-utils.hh>
#include <utils/mu-regex.hh>
#include <mu-store.hh>
#include "mu-maildir.hh"

#include "utils/mu-test-utils.hh"

using namespace Mu;

constexpr auto test_msg =
R"(Return-Path: <htcondor-users-bounces@cs.wisc.edu>
Received: from pop3.web.de [212.227.17.177]
	by localhost with POP3 (fetchmail-6.4.6)
	for <arne@localhost> (single-drop); Fri, 26 Jun 2020 12:56:08 +0200 (CEST)
Received: from jeeves.cs.wisc.edu ([128.105.6.16]) by mx-ha.web.de (mxweb112
 [212.227.17.8]) with ESMTPS (Nemesis) id 1MdMYE-1jFXaM2gnA-00ZKvt for
 <@ID@@web.de>; Fri, 26 Jun 2020 01:28:11 +0200
Received: from jeeves.cs.wisc.edu (localhost [127.0.0.1])
	by jeeves.cs.wisc.edu (8.14.4/8.14.4) with ESMTP id 05PNLgek013419;
	Thu, 25 Jun 2020 18:22:23 -0500
Received: from shale.cs.wisc.edu (shale.cs.wisc.edu [128.105.6.25])
	by jeeves.cs.wisc.edu (8.14.4/8.14.4) with ESMTP id 05PNLaf0013414
	(version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256 verify=OK)
	for <htcondor-users@jeeves.cs.wisc.edu>; Thu, 25 Jun 2020 18:21:36 -0500
Received: from smtp7.wiscmail.wisc.edu (wmmta4.doit.wisc.edu [144.92.197.245])
	by shale.cs.wisc.edu (8.14.4/8.14.4) with ESMTP id 05PNLaMK013694
	(version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-GCM-SHA256 bits=128
	verify=NO)
	for <htcondor-users@cs.wisc.edu>; Thu, 25 Jun 2020 18:21:36 -0500
Received: from USG02-CY1-obe.outbound.protection.office365.us
	([23.103.209.108]) by smtp7.wiscmail.wisc.edu
	(Oracle Communications Messaging Server 8.0.2.4.20190812 64bit (built
	Aug 12
	2019)) with ESMTPS id <0QCI042LC8VUXFC0@smtp7.wiscmail.wisc.edu> for
	htcondor-users@cs.wisc.edu (ORCPT htcondor-users@cs.wisc.edu); Thu,
	25 Jun 2020 18:21:31 -0500 (CDT)
X-Spam-Report: IsSpam=no, Probability=11%, Hits= RETURN_RECEIPT 0.5,
	FROM_US_TLD 0.1, HTML_00_01 0.05, HTML_00_10 0.05, SUPERLONG_LINE 0.05,
	BODYTEXTP_SIZE_3000_LESS 0, BODY_SIZE_10000_PLUS 0, DKIM_SIGNATURE 0,
	KNOWN_MTA_TFX 0, NO_URI_HTTPS 0, SPF_PASS 0, SXL_IP_TFX_WM 0,
	WEBMAIL_SOURCE 0, WEBMAIL_XOIP 0, WEBMAIL_X_IP_HDR 0, __ANY_URI 0,
	__ARCAUTH_DKIM_PASSED 0, __ARCAUTH_DMARC_PASSED 0, __ARCAUTH_PASSED 0,
	__ATTACHMENT_SIZE_0_10K 0, __ATTACHMENT_SIZE_10_25K 0,
	__BODY_NO_MAILTO 0,
	__CT 0, __CTYPE_HAS_BOUNDARY 0, __CTYPE_MULTIPART 0, __HAS_ATTACHMENT 0,
	__HAS_ATTACHMENT1 0, __HAS_ATTACHMENT2 0, __HAS_FROM 0, __HAS_MSGID 0,
	__HAS_XOIP 0, __HIGHBITS 0, __MIME_TEXT_P 0, __MIME_TEXT_P1 0,
	__MIME_TEXT_P2 0, __MIME_VERSION 0, __MULTIPLE_RCPTS_TO_X2 0,
	__NO_HTML_TAG_RAW 0, __RETURN_RECEIPT_TO 0, __SANE_MSGID 0,
	__TO_MALFORMED_2 0, __TO_NAME 0, __TO_NAME_DIFF_FROM_ACC 0,
	__TO_NO_NAME 0,
	__TO_REAL_NAMES 0, __URI_IN_BODY 0, __URI_MAILTO 0, __URI_NOT_IMG 0,
	__URI_NO_PATH 0, __URI_NS , __URI_WITHOUT_PATH 0
X-Wisc-Doma: @ID@X@numerica.us,numerica.us
X-Wisc-Env-From-B64: d2VzbGV5LnRheWxvckBudW1lcmljYS51cw==
X-Spam-PmxInfo: Server=avs-13, Version=6.4.7.2805085,
	Antispam-Engine: 2.7.2.2107409, Antispam-Data: 2020.6.25.231519,
	AntiVirus-Engine: 5.74.0, AntiVirus-Data: 2020.6.25.5740002,
	SenderIP=[23.103.209.108]
X-Wisc-DKIM-Verify: @ID@XXXXXXX@numerica.us,numericaus.onmicrosoft.com!pass
X-Spam-Score: *
ARC-Seal: i=1; a=rsa-sha256; s=arcselector5401; d=microsoft.com; cv=none;
	b=KyXoddJsnsHsBwhdlO5rcljgMRaylJAUAxWTjG4jQL1C8XJAMgeERtH2sRffdjibYUFfSuDUNJmrTrvrbjKGUt2I8J2M2MgUB/upMoroVPNBrP1Fy9wMeZJQuSS4r4KjZZktsl2i8eq667pzOZO6+wX2IA5M7YtxDqglcWOE6btWzbABVjx+9eCXMt0eMd1+UI6ABK8Frd33EFQLKT0h/cxidWR9l+0gCMAcRxsLrQ82+ckU606AIV/DA1E4Tq7ADe/+CRv4QszDN93pWL/1N2/OOh9vFTs9g9ZG6uXjN+Km/IAdylPbfHgKW60ev3/Bvv6N3pA7DjpuiKj6BnW7mQ==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
	s=arcselector5401;
	h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
	bh=OZrj1we1ZUH0xBMhJ5/F6EQnB0cmitFs2xZW1fLMRNs=;
	b=Pq07a3u26s2UdpucJuVQ0h68272wx46Wp61x/30TelPPFLCRxVjmlH1U3IBmIsZ1jOEtGXFJRv65L3HmwGxRUdLlMOdPRB64BBfHQ9NGWUBykKQmOrJNGJs635nEdpugpzngzIdcg1PS5vHxPJAnOeqoo71OVPI3JqPrPEn2TJJgb9J6PApexkqIbVl35prGPsyS/t2IlYw3/ihWzORG6wvqJeqedgpJTBXeGaDoMa+MQ1BeUsdvybh8+hau4ASpM5lwyeXlGmJ5mUTZi39jp+dFdDrmCj/VM4ezeuXeH9+HFtDjKLZJaTDWUID0IBcr91BaoQE/4r6y+lpkah6LLQ==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
	smtp.mailfrom=numerica.us;
	dmarc=pass action=none header.from=numerica.us;
	dkim=pass header.d=numerica.us; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
	d=numericaus.onmicrosoft.com; s=selector1-numericaus-onmicrosoft-com;
	h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
	bh=OZrj1we1ZUH0xBMhJ5/F6EQnB0cmitFs2xZW1fLMRNs=;
	b=cFn0eL5k2IKry9U8qa8mbVaxRiyicUAWzRc3NUtj+VEbgShfrz8SO6FPX20WTQQJg/Fu/3isqsSEUt+9NSEEbgd5eQ1EVz5E/JVeNjPe9GXR0JEF/g3f6yM7CO+kKTvXSRvQjce683U0j7Aj1pSDEktoVNP4xvOS2Gx9VjdWTmc=
Received: from DM3P110MB0474.NAMP110.PROD.OUTLOOK.COM (2001:489a:200:413::10)
	by DM3P110MB0490.NAMP110.PROD.OUTLOOK.COM (2001:489a:200:413::14)
	with Microsoft SMTP Server
	(version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384)
	id 15.20.3109.25; Thu, 25 Jun 2020 23:21:07 +0000
Received: from DM3P110MB0474.NAMP110.PROD.OUTLOOK.COM
	([fe80::f548:f084:9867:9375]) by DM3P110MB0474.NAMP110.PROD.OUTLOOK.COM
	([fe80::f548:f084:9867:9375%11]) with mapi id 15.20.3131.024; Thu,
	25 Jun 2020 23:21:07 +0000
From: Raul Endymion <XXXXXXXXXXXXXXXXXXXX@numerica.us>
To: "'htcondor-users@cs.wisc.edu'" <htcondor-users@cs.wisc.edu>
Thread-topic: OPINIONS WANTED: Are there any blatent downsides I am missing to
	the following Condor configuration
Thread-index: AdZLRbEvYoEDBZChS62aOHgPzKD8kw==
Date: Thu, 25 Jun 2020 23:21:06 +0000
Message-id: <DM3P110MB04746CDBA55B3E597EFD1877FA920@DM3P110MB0474.NAMP110.PROD.OUTLOOK.COM>
Accept-Language: en-US
Content-language: en-US
X-MS-Has-Attach: yes
X-MS-TNEF-Correlator:
X-Originating-IP: [50.233.29.54]
x-ms-publictraffictype: Email
x-ms-office365-filtering-correlation-id: f4edecdf-5582-4b2d-226a-08d8195e7007
x-ms-traffictypediagnostic: DM3P110MB0490:
x-microsoft-antispam-prvs: <DM3P110MB04907C313C4243B4FE42A20CFA920@DM3P110MB0490.NAMP110.PROD.OUTLOOK.COM>
x-ms-oob-tlc-oobclassifiers: OLM:10000;
x-forefront-prvs: 0445A82F82
x-ms-exchange-senderadcheck: 1
x-microsoft-antispam: BCL:0;
X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
	IPV:NLI; SFV:NSPM; H:DM3P110MB0474.NAMP110.PROD.OUTLOOK.COM;
	PTR:; CAT:NONE; SFTY:;
	SFS:(346002)(366004)(6916009)(83380400001)(71200400001)(8936002)(55016002)(5660300002)(8676002)(9686003)(66616009)(64756008)(33656002)(52536014)(66446008)(66476007)(66556008)(66946007)(99936003)(76116006)(186003)(86362001)(26005)(7696005)(6506007)(44832011)(508600001)(2906002)(80162005)(80862006)(491001)(554374003);
	DIR:OUT; SFP:1102;
x-ms-exchange-transport-forked: True
MIME-version: 1.0
X-OriginatorOrg: numerica.us
X-MS-Exchange-CrossTenant-Network-Message-Id: f4edecdf-5582-4b2d-226a-08d8195e7007
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Jun 2020 23:21:06.8341 (UTC)
X-MS-Exchange-CrossTenant-fromentityheader: Hosted
X-MS-Exchange-CrossTenant-id: fae7a2ae-df1d-444e-91be-babb0900b9c2
X-MS-Exchange-CrossTenant-mailboxtype: HOSTED
X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM3P110MB0490
Subject: [HTCondor-users] OPINIONS WANTED: Are there any blatent downsides I
 am missing to the following Condor configuration
X-BeenThere: htcondor-users@cs.wisc.edu
X-Mailman-Version: 2.1.19
Precedence: list
List-Id: HTCondor-Users Mail List <htcondor-users.cs.wisc.edu>
List-Unsubscribe: <https://lists.cs.wisc.edu/mailman/options/htcondor-users>,
	<mailto:htcondor-users-request@cs.wisc.edu?subject=unsubscribe>
List-Archive: <https://www-auth.cs.wisc.edu/lists/htcondor-users/>
List-Post: <mailto:htcondor-users@cs.wisc.edu>
List-Help: <mailto:htcondor-users-request@cs.wisc.edu?subject=help>
List-Subscribe: <https://lists.cs.wisc.edu/mailman/listinfo/htcondor-users>,
	<mailto:htcondor-users-request@cs.wisc.edu?subject=subscribe>
Reply-To: HTCondor-Users Mail List <htcondor-users@cs.wisc.edu>
Content-Type: multipart/mixed; boundary="===============0678627779074767862=="
Errors-To: htcondor-users-bounces@cs.wisc.edu
Sender: "HTCondor-users" <htcondor-users-bounces@cs.wisc.edu>
Envelope-To: <@ID@XXXXX@web.de>
X-UI-Filterresults: unknown:2;V03:K0:cdojl5YHfkg=:jhTbQXp38SL2za/LB4M7aUwpyw
 5rDHoN1+/ScH/O9/G1fKWbGryQ203thF+1ZrHUOOwq8MVOc5SsoqzSTsaNbEAdthFcDDz3Oui
 SHxX1hdpV3UOjZEHzWlpjEjRe7t74g2RI/ESELmkPuLg/LZC7SjAsg70cTJBIfDPYxJkJAcUl
 9W6OEBsmtTDO0va/EQRYjfkpoF9tjfmfMNw9KSKHuDdqZu2Xfak8mQKnWsoxWeUkD31r60iPC
 yikbj7KP5AlHa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YXRi
 a7LIu3LI2LTU13C/LE7j9hmxP6TApyiXi14f0GSa2sbF6HWp2v2rhYM7h67AAn3SQgvcJLpgb
 Hz5ABb/OAk6ABVEl+a483zexJ6iT2P0gYc08zmewy8Jf8AD9r846k9pGZuhBaOHREx3bA16Bj
 uWYh3QzSI6MQoJM3XbBGLVkX36Lfj54T9kk97lLaxfbGPuNoyOV9iTBKxts3m2KD+52iH3EEi
 glbH6HNIUHyCHdEXsXyGVFwfM9V7OQcVO/g266KIQ74wU16x/Zdsq4p/1PcRXHRnoMxP/pUrj
 EOLWzFU71qzC/OSkYWRil9HXUyucTFGQ0N08jZNXctI9lElWtgq3iI+Cz2F20rz+LJGhSHSkZ
 0G5JgXrtspeJN5yoH6TOE0hblr5sZcAM0wiSP7x/hPBeYHswzTA5/laWMn++9aTPVgpPaJ9/x
 wyLm55OZr4Jl+StWd3MqLCgiRB3cNGrDX7f8Eqnj4wfCHiGIUHewD4qrfXraZQhIk17W+9JyD
 osmUiVD9ZRdNCY2eNnu8ZkJ4uzKl44lwLL43sInKBjdAHlnoxrR2FOrYXbnU31ujwxdeUr6Hs
 xPFy0Git0CpWCWYmaz37KA8GW7PE4ffWzcfCmz6AKBrbHcCreeUnyqnSEDy9ubnz7mcLRnu3W
 RAWi6diI8gcS9g0+r4z5PtZX9rveXRekHJ4k08VuYVmdiz3gjXmHPlm9IKPEAbygP2EYgjwGE
 RbReLc8xHJlfLbwdXyGw0HU=

--===============0678627779074767862==
Content-language: en-US
Content-type: multipart/signed; protocol="application/x-pkcs7-signature";
 micalg=2.16.840.1.101.3.4.2.3;
 boundary="----=_NextPart_000_0018_01D64B14.F58791A0"

------=_NextPart_000_0018_01D64B14.F58791A0
Content-Type: text/plain;
	charset="utf-8"
Content-Transfer-Encoding: quoted-printable

Hey!

I am architecting our final HTCondor configuration over here and I have =
an idea I am unsure about and I would like to ask some experienced users =
for their opinion.

Background, we have a small, relatively homogenous cluster (with no =
special universes) and less than 10 users. Since each user has their own =
workstation separate from our cluster I thought the following =
configuration would suit our needs, but I want to make sure there isn't =
a huge disadvantage I am missing:

1. Set the Central Manager to be highly available to the point of =
tolerating N cluster machine failures
2. Put a Submit on each of the users' workstations (I am a little =
worried about the resource usage of the condor_shadow and condor_schedd, =
my users are already running into RAM consumption issues over time as it =
is)
3. Place an Execute on each of the cluster machines, which would lead to =
the central manager being on a machine that is also executing jobs

Fortunately both my users' and cluster machines all have access to the =
same network storage, and we have centralized authentication so we can =
just use our users' credentials to authenticate everywhere.=20

Before I set this in dry mud, does anyone have any retrospective =
recommendations I could benefit hearing from, since I am still pretty =
new to the project?

Thank you!
-Raul

Raul Endymion =E2=80=93 Cluster Manager
Numerica Corporation (www.numerica.us)
5042 Technology Parkway #100
Fort Collins, Colorado 80528
=E2=98=8E=EF=B8=8F (970) 207 2233
=F0=9F=93=A7 @ID@XXXXXXXXXXXXXXX@numerica.us



------=_NextPart_000_0018_01D64B14.F58791A0
Content-Type: application/pkcs7-signature;
	name="smime.p7s"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
	filename="smime.p7s"



------=_NextPart_000_0018_01D64B14.F58791A0--

--===============0678627779074767862==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
HTCondor-users mailing list
To unsubscribe, send a message to htcondor-users-request@cs.wisc.edu with a
subject: Unsubscribe
You can also unsubscribe by visiting
https://lists.cs.wisc.edu/mailman/listinfo/htcondor-users

The archives can be found at:
https://lists.cs.wisc.edu/archive/htcondor-users/
--===============0678627779074767862==--)";

static std::string
message(const Regex& rx, size_t id)
{
	char buf[16];
	::snprintf(buf, sizeof(buf), "%zu", id);

	return to_string_gchar(
		g_regex_replace(rx, test_msg, -1, 0, buf,
				G_REGEX_MATCH_DEFAULT, {}));
}

struct TestData {
	size_t num_maildirs;
	size_t num_messages;
	size_t num_threads;
};


static void
setup(const TestData& tdata)
{
	/* create toplevel */
	auto top_maildir = std::string{BENCH_MAILDIRS};
	int res = g_mkdir_with_parents(top_maildir.c_str(), 0700);
	g_assert_cmpuint(res,==, 0);

	/* create maildirs */
	for (size_t i = 0; i != tdata.num_maildirs; ++i) {
		const auto mdir = mu_format("{}/maildir-{}", top_maildir, i);
		auto res = maildir_mkdir(mdir);
		g_assert(!!res);
	}
	const auto rx = Regex::make("@ID@");
	/* create messages */
	for (size_t n = 0; n != tdata.num_messages; ++n) {
		auto mpath = mu_format("{}/maildir-{}/cur/msg-{}:2,S",
				    top_maildir, n % tdata.num_maildirs,
				    n);
		std::ofstream stream(mpath);
		auto msg = message(*rx, n);
		stream.write(msg.c_str(), msg.size());
		g_assert_true(stream.good());
	}
}

static void
tear_down()
{
	/* ugly */
	GError *err{};
	const auto cmd{mu_format("/bin/rm -rf '{}' '{}'", BENCH_MAILDIRS, BENCH_STORE)};
	if (!g_spawn_command_line_sync(cmd.c_str(), NULL, NULL, NULL, &err)) {
		mu_warning("error: {}", err ? err->message : "?");
		g_clear_error(&err);
	}
}

void
black_hole(void)
{
	return; /* do nothing */
}

static void
benchmark_indexer(gconstpointer testdata)
{
	using namespace std::chrono_literals;
	using Clock = std::chrono::steady_clock;
	const auto tdata = reinterpret_cast<const TestData*>(testdata);

	setup(*tdata);
	auto start = Clock::now();

	{
		auto store{Store::make_new(BENCH_STORE, BENCH_MAILDIRS)};
		g_assert_true(!!store);
		Indexer::Config conf{};

		auto res = store->indexer().start(conf);
		g_assert_true(res);
		while(store->indexer().is_running()) {
			std::this_thread::sleep_for(100ms);
		}
		g_assert_cmpuint(store->size(),==, tdata->num_messages);
	}

	const auto elapsed = Clock::now() - start;
	std::cout << "indexed " << tdata->num_messages << " messages in "
		  << tdata->num_maildirs << " maildirs in "
		  << to_ms(elapsed) << "ms; "
		  << to_us(elapsed) / tdata->num_messages << " μs/message; "
		  << static_cast<size_t>(1000*tdata->num_messages / to_ms(elapsed))
		  << " messages/s"
		  << " (" << tdata->num_threads << " thread(s))\n";

	tear_down();
}

int
main(int argc, char *argv[])
{
	size_t num_maildirs{}, num_messages{};

	mu_test_init(&argc, &argv);

	if (g_test_perf()) {
		num_maildirs = 20;
		num_messages = 5000;
	} else {
		num_maildirs = 10;
		num_messages = 1000;
	}

	g_log_set_handler(
	    NULL,
	    (GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
	    (GLogFunc)black_hole,
	    NULL);


	size_t thread_num{};
	const auto tnum = g_getenv("THREAD_NUM");
	if (tnum)
		thread_num = ::strtol(tnum, NULL, 10);

	if (thread_num != 0) {
		/* THREAD_NUM specified */
		static TestData tdata{num_maildirs, num_messages, thread_num};
		char *name = g_strdup_printf("/bench/indexer/%zu-cores", thread_num);
		g_test_add_data_func(name, &tdata, benchmark_indexer);
		g_free(name);
	} else {
		/* no THREAD_NUM specified */

		const size_t hw_threads = std::thread::hardware_concurrency();

		{
			static TestData tdata{num_maildirs, num_messages, 1};
			g_test_add_data_func("/bench/indexer/1-core", &tdata, benchmark_indexer);
		}

		if (hw_threads > 2) {
			static TestData tdata{num_maildirs, num_messages, hw_threads/2};
			char *name = g_strdup_printf("/bench/indexer/%zu-cores", hw_threads/2);
			g_test_add_data_func(name, &tdata, benchmark_indexer);
			g_free(name);
		}

		if (hw_threads > 1) {
			static TestData tdata{num_maildirs, num_messages, hw_threads};
			char *name = g_strdup_printf("/bench/indexer/%zu-cores", hw_threads);
			g_test_add_data_func(name, &tdata, benchmark_indexer);
			g_free(name);
		}
	}

	tear_down();

	return g_test_run();
}
