This is an automated email from the ASF dual-hosted git repository.
amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new 73cbbfba86 libswoc: Update Errata use in cache tool. (#9878)
73cbbfba86 is described below
commit 73cbbfba8655f6b3247418e6b4ab66bcfb8de1da
Author: Alan M. Carroll <[email protected]>
AuthorDate: Tue Jul 18 17:19:52 2023 -0500
libswoc: Update Errata use in cache tool. (#9878)
---
include/tscpp/util/ts_errata.h | 11 +++++
src/traffic_cache_tool/CacheDefs.cc | 23 ++++++-----
src/traffic_cache_tool/CacheDefs.h | 13 +++---
src/traffic_cache_tool/CacheScan.cc | 12 +++---
src/traffic_cache_tool/CacheTool.cc | 80 ++++++++++++++++++-------------------
5 files changed, 77 insertions(+), 62 deletions(-)
diff --git a/include/tscpp/util/ts_errata.h b/include/tscpp/util/ts_errata.h
index 42bdf11b74..e9b6d7d2ae 100644
--- a/include/tscpp/util/ts_errata.h
+++ b/include/tscpp/util/ts_errata.h
@@ -40,5 +40,16 @@ static constexpr std::array<swoc::TextView, 9>
Severity_Names{
{"Diag", "Debug", "Status", "Note", "Warn", "Error", "Fatal", "Alert",
"Emergency"}
};
+inline std::error_code
+make_errno_code()
+{
+ return {errno, std::system_category()};
+}
+inline std::error_code
+make_errno_code(int err)
+{
+ return {err, std::system_category()};
+}
+
// Temporary string for formatting.
inline thread_local std::string bw_dbg;
diff --git a/src/traffic_cache_tool/CacheDefs.cc
b/src/traffic_cache_tool/CacheDefs.cc
index ff63dd9d24..b6a62a50e5 100644
--- a/src/traffic_cache_tool/CacheDefs.cc
+++ b/src/traffic_cache_tool/CacheDefs.cc
@@ -28,9 +28,12 @@
using namespace std;
using namespace ts;
-using ts::Errata;
namespace ts
{
+
+using swoc::Errata;
+using swoc::Rv;
+
std::ostream &
operator<<(std::ostream &s, Bytes const &n)
{
@@ -305,7 +308,7 @@ Stripe::updateHeaderFooter()
InitializeMeta();
if (!OPEN_RW_FLAG) {
- zret.push(0, 1, "Writing Not Enabled.. Please use --write to enable
writing to disk");
+ zret.note("Writing Not Enabled.. Please use --write to enable writing to
disk");
return zret;
}
@@ -319,9 +322,9 @@ Stripe::updateHeaderFooter()
ssize_t n = pwrite(_span->_fd, meta_t, hdr_size, _meta_pos[i][HEAD]);
if (n < hdr_size) {
+ zret = swoc::Errata(make_errno_code(), "Failed to write stripe header ");
std::cout << "problem writing header to disk: " << strerror(errno) << ":"
<< " " << n << "<" << hdr_size << std::endl;
- zret = Errata::Message(0, errno, "Failed to write stripe header ");
ats_free(meta_t);
return zret;
}
@@ -330,9 +333,9 @@ Stripe::updateHeaderFooter()
memcpy(meta_t, (char *)dir, dir_size);
n = pwrite(_span->_fd, meta_t, dir_size, _meta_pos[i][HEAD] + hdr_size); //
if (n < dir_size) {
+ zret = Errata(make_errno_code(), "Failed to write stripe header ");
std::cout << "problem writing dir to disk: " << strerror(errno) << ":"
<< " " << n << "<" << dir_size << std::endl;
- zret = Errata::Message(0, errno, "Failed to write stripe header ");
ats_free(meta_t);
return zret;
}
@@ -343,9 +346,9 @@ Stripe::updateHeaderFooter()
int64_t footer_size = ROUND_TO_STORE_BLOCK(sizeof(StripeMeta));
n = pwrite(_span->_fd, meta_t, footer_size,
_meta_pos[i][FOOT]);
if (n < footer_size) {
+ zret = Errata(make_errno_code(), "Failed to write stripe header ");
std::cout << "problem writing footer to disk: " << strerror(errno) << ":"
<< " " << n << "<" << footer_size << std::endl;
- zret = Errata::Message(0, errno, "Failed to write stripe header ");
ats_free(meta_t);
return zret;
}
@@ -897,8 +900,8 @@ Stripe::loadMeta()
alignas(SBSIZE) char stripe_buff[SBSIZE]; // Use when reading a
single stripe block.
alignas(SBSIZE) char stripe_buff2[SBSIZE]; // use to save the
stripe freelist
if (io_align > SBSIZE) {
- return Errata::Message(0, 1, "Cannot load stripe ", _idx, " on span ",
_span->_path.string(),
- " because the I/O block alignment ", io_align, " is
larger than the buffer alignment ", SBSIZE);
+ return Errata("Cannot load stripe {} on span [} because the I/O block
alignment {} is larger than the buffer alignment {}",
+ _idx, _span->_path.string(), io_align, SBSIZE);
}
_directory._start = pos;
@@ -940,7 +943,7 @@ Stripe::loadMeta()
}
}
} else {
- zret.push(0, 1, "Header A not found");
+ zret.note("Header A not found");
}
pos = _meta_pos[A][FOOT];
// Technically if Copy A is valid, Copy B is not needed. But at this point
it's cheap to retrieve
@@ -975,8 +978,8 @@ Stripe::loadMeta()
} else if (_meta_pos[B][FOOT] > 0 && _meta[B][HEAD].sync_serial ==
_meta[B][FOOT].sync_serial) {
this->updateLiveData(B);
} else {
- zret.push(0, 1, "Invalid stripe data - candidates found but sync serial
data not valid. ", _meta[A][HEAD].sync_serial, ":",
- _meta[A][FOOT].sync_serial, ":", _meta[B][HEAD].sync_serial,
":", _meta[B][FOOT].sync_serial);
+ zret.note("Invalid stripe data - candidates found but sync serial data
not valid. {}:{}:{}:{}", _meta[A][HEAD].sync_serial,
+ _meta[A][FOOT].sync_serial, _meta[B][HEAD].sync_serial,
_meta[B][FOOT].sync_serial);
}
}
diff --git a/src/traffic_cache_tool/CacheDefs.h
b/src/traffic_cache_tool/CacheDefs.h
index 3d9cad8c2e..bee3e01a5f 100644
--- a/src/traffic_cache_tool/CacheDefs.h
+++ b/src/traffic_cache_tool/CacheDefs.h
@@ -29,12 +29,12 @@
#include "swoc/swoc_file.h"
#include "swoc/Scalar.h"
+#include "swoc/TextView.h"
+#include "tscpp/util/ts_errata.h"
#include "tscore/I_Version.h"
#include "tscore/ink_memory.h"
#include "tscore/Regex.h"
-#include "tscore/Errata.h"
-#include "swoc/TextView.h"
#include "tscore/ink_file.h"
#include "tscore/CryptoHash.h"
@@ -228,7 +228,7 @@ class URLparser
{
public:
bool verifyURL(std::string &url1);
- Errata parseURL(swoc::TextView URI);
+ swoc::Errata parseURL(swoc::TextView URI);
int getPort(std::string &fullURL, int &port_ptr, int &port_len);
private:
@@ -344,15 +344,16 @@ private:
DFA regex;
};
+using swoc::Errata;
+using swoc::MemSpan;
+
using ts::Bytes;
using ts::Megabytes;
using ts::CacheStoreBlocks;
using ts::CacheStripeBlocks;
using ts::StripeMeta;
using ts::CacheStripeDescriptor;
-using ts::Errata;
using ts::CacheDirEntry;
-using swoc::MemSpan;
using ts::Doc;
constexpr int ESTIMATED_OBJECT_SIZE = 8000;
@@ -454,7 +455,7 @@ struct Span {
/// This is broken and needs to be cleaned up.
void clearPermanently();
- ts::Rv<Stripe *> allocStripe(int vol_idx, const CacheStripeBlocks &len);
+ swoc::Rv<Stripe *> allocStripe(int vol_idx, const CacheStripeBlocks &len);
Errata updateHeader(); ///< Update serialized header and write to disk.
swoc::file::path _path; ///< File system location of span.
diff --git a/src/traffic_cache_tool/CacheScan.cc
b/src/traffic_cache_tool/CacheScan.cc
index de1f698b3d..5fcd53698d 100644
--- a/src/traffic_cache_tool/CacheScan.cc
+++ b/src/traffic_cache_tool/CacheScan.cc
@@ -86,7 +86,7 @@ CacheScan::unmarshal(HTTPHdrImpl *obj, intptr_t offset)
} else if (obj->m_polarity == HTTP_TYPE_RESPONSE) {
HDR_UNMARSHAL_STR(obj->u.resp.m_ptr_reason, offset);
} else {
- zret.push(0, 0, "Unknown Polarity of HTTPHdrImpl* obj");
+ zret.note("Unknown Polarity of HTTPHdrImpl* obj");
return zret;
}
@@ -131,7 +131,7 @@ CacheScan::unmarshal(MIMEFieldBlockImpl *mf, intptr_t
offset)
// check if out of bounds
if (!mf_mem.contains(reinterpret_cast<char *>(field))) {
- zret.push(0, 0, "Out of bounds memory in the deserialized
MIMEFieldBlockImpl");
+ zret.note("Out of bounds memory in the deserialized MIMEFieldBlockImpl");
return zret;
}
if (field && field->m_readiness == MIME_FIELD_SLOT_READINESS_LIVE) {
@@ -279,7 +279,7 @@ CacheScan::unmarshal(char *buf, int len, RefCountObj
*block_ref)
// stuff that didn't fit in the integral slots.
int extra = sizeof(uint64_t) * alt->m_frag_offset_count -
sizeof(alt->m_integral_frag_offsets);
if (extra >= len || extra < 0) {
- zret.push(0, 0, "Invalid Fragment Count ", extra);
+ zret.note("Invalid Fragment Count {}", extra);
return zret;
}
char *extra_src = buf + reinterpret_cast<intptr_t>(alt->m_frag_offsets);
@@ -311,7 +311,7 @@ CacheScan::unmarshal(char *buf, int len, RefCountObj
*block_ref)
if (heap != nullptr && (reinterpret_cast<char *>(heap) - buf) < len) {
tmp = this->unmarshal(heap, len, HDR_HEAP_OBJ_HTTP_HEADER,
reinterpret_cast<HdrHeapObjImpl **>(&hh), block_ref);
if (hh == nullptr || tmp < 0) {
- zret.push(0, 0, "HTTPInfo::request unmarshal failed");
+ zret.note("HTTPInfo::request unmarshal failed");
return zret;
}
len -= tmp;
@@ -327,7 +327,7 @@ CacheScan::unmarshal(char *buf, int len, RefCountObj
*block_ref)
if (heap != nullptr && (reinterpret_cast<char *>(heap) - buf) < len) {
tmp = this->unmarshal(heap, len, HDR_HEAP_OBJ_HTTP_HEADER,
reinterpret_cast<HdrHeapObjImpl **>(&hh), block_ref);
if (hh == nullptr || tmp < 0) {
- zret.push(0, 0, "HTTPInfo::response unmarshal failed");
+ zret.note("HTTPInfo::response unmarshal failed");
return zret;
}
len -= tmp;
@@ -372,7 +372,7 @@ CacheScan::get_alternates(const char *buf, int length, bool
search)
if (a->m_magic == CACHE_ALT_MAGIC_MARSHALED) {
zret = this->unmarshal(const_cast<char *>(buf), length, block_ref);
- if (zret.size()) {
+ if (zret.length()) {
std::cerr << zret << std::endl;
return zret;
} else if (!a->m_request_hdr.m_http) {
diff --git a/src/traffic_cache_tool/CacheTool.cc
b/src/traffic_cache_tool/CacheTool.cc
index a3559a7fe7..28e29d1ee5 100644
--- a/src/traffic_cache_tool/CacheTool.cc
+++ b/src/traffic_cache_tool/CacheTool.cc
@@ -46,15 +46,16 @@
#include "CacheDefs.h"
#include "CacheScan.h"
+using swoc::MemSpan;
+using swoc::Errata;
+
using ts::Bytes;
using ts::Megabytes;
using ts::CacheStoreBlocks;
using ts::CacheStripeBlocks;
using ts::StripeMeta;
using ts::CacheStripeDescriptor;
-using ts::Errata;
using ts::CacheDirEntry;
-using swoc::MemSpan;
using ts::Doc;
enum { SILENT = 0, NORMAL, VERBOSE } Verbosity = NORMAL;
@@ -215,10 +216,10 @@ Cache::allocStripe(Span *span, int vol_idx, const
CacheStripeBlocks &len)
{
auto rv = span->allocStripe(vol_idx, len);
std::cout << span->_path.string() << ":" << vol_idx << std::endl;
- if (rv.isOK()) {
+ if (rv.is_ok()) {
_volumes[vol_idx]._stripes.push_back(rv);
}
- return rv.errata();
+ return std::move(rv.errata());
}
#if 0
@@ -293,10 +294,10 @@ VolumeAllocator::load(swoc::file::path const &spanFile,
swoc::file::path const &
Errata zret;
if (volumeFile.empty()) {
- zret.push(0, 9, "Volume config file not set");
+ zret.note("Volume config file not set");
}
if (spanFile.empty()) {
- zret.push(0, 9, "Span file not set");
+ zret.note("Span file not set");
}
if (zret) {
@@ -347,7 +348,7 @@ VolumeAllocator::allocateSpan(swoc::file::path const
&input_file_path)
if (span->_path.view() == input_file_path.view()) {
std::cout << "===============================" << std::endl;
if (span->_header) {
- zret.push(0, 1, "Disk already initialized with valid header");
+ zret.note("Disk already initialized with valid header");
} else {
this->allocateFor(*span);
span->updateHeader();
@@ -460,9 +461,9 @@ Cache::loadSpan(swoc::file::path const &path)
auto fs = swoc::file::status(path, ec);
if (path.empty()) {
- zret = Errata::Message(0, EINVAL, "A span file specified by --spans is
required");
+ zret = Errata(make_errno_code(EINVAL), "A span file specified by --spans
is required");
} else if (!swoc::file::is_readable(path)) {
- zret = Errata::Message(0, EPERM, '\'', path.string(), "' is not
readable.");
+ zret = Errata(make_errno_code(EPERM), R"("{}" is not readable.)", path);
} else if (swoc::file::is_regular_file(fs)) {
zret = this->loadSpanConfig(path);
} else {
@@ -536,7 +537,7 @@ Cache::loadSpanConfig(swoc::file::path const &path)
auto n = swoc::svtoi(value, &text);
if (text == value && 0 < n && n < 256) {
} else {
- zret.push(0, 0, "Invalid volume index '", value, "'");
+ zret.note("Invalid volume index '{}'", value);
}
}
}
@@ -545,7 +546,7 @@ Cache::loadSpanConfig(swoc::file::path const &path)
}
}
} else {
- zret = Errata::Message(0, EBADF, "Unable to load ", path.string());
+ zret = Errata(make_errno_code(EBADF), "Unable to load {}", path);
}
return zret;
}
@@ -580,7 +581,7 @@ Cache::loadURLs(swoc::file::path const &path)
}
}
} else {
- zret = Errata::Message(0, EBADF, "Unable to load ", path.string());
+ zret = Errata(make_errno_code(EBADF), "Unable to load ", path.string());
}
return zret;
}
@@ -700,13 +701,13 @@ Span::load()
auto fs = swoc::file::status(_path, ec);
if (!swoc::file::is_readable(_path)) {
- zret = Errata::Message(0, EPERM, _path.string(), " is not readable.");
+ zret = Errata(make_errno_code(EPERM), R"("{}" is not readable.)", _path);
} else if (swoc::file::is_char_device(fs) ||
swoc::file::is_block_device(fs)) {
zret = this->loadDevice();
} else if (swoc::file::is_dir(fs)) {
- zret.push(0, 1, "Directory support not yet available");
+ zret.note("Directory support not yet available");
} else {
- zret.push(0, EBADF, _path.string(), " is not a valid file type");
+ zret = Errata(make_errno_code(EBADF), R"("{}" is not a valid file type)",
_path);
}
return zret;
}
@@ -754,7 +755,7 @@ Span::loadDevice()
}
_len = _header->num_blocks;
} else {
- zret = Errata::Message(0, 0, _path.string(), " header is
uninitialized or invalid");
+ zret.note("{} header is uninitialized or invalid", _path);
std::cout << "Span: " << _path.string() << " header is uninitialized
or invalid" << std::endl;
_len = round_down(_geometry.totalsz) - _base;
}
@@ -762,18 +763,18 @@ Span::loadDevice()
_fd = fd.release();
_offset = _base + span_hdr_size;
} else {
- zret = Errata::Message(0, errno, "Failed to read from ",
_path.string(), '[', errno, ':', strerror(errno), ']');
+ zret = Errata(make_errno_code(), "Failed to read from {}", _path);
}
} else {
- zret = Errata::Message(0, 23, "Unable to get device geometry for ",
_path.string());
+ zret = Errata("Unable to get device geometry for {}", _path);
}
} else {
- zret = Errata::Message(0, errno, "Unable to open ", _path.string());
+ zret = Errata(make_errno_code(), "Unable to open {}", _path);
}
return zret;
}
-ts::Rv<Stripe *>
+swoc::Rv<Stripe *>
Span::allocStripe(int vol_idx, const CacheStripeBlocks &len)
{
for (auto spot = _stripes.begin(), limit = _stripes.end(); spot != limit;
++spot) {
@@ -797,8 +798,7 @@ Span::allocStripe(int vol_idx, const CacheStripeBlocks &len)
}
}
}
- return ts::Rv<Stripe *>(nullptr,
- Errata::Message(0, 15, "Failed to allocate stripe of
size ", len, " - no free block large enough"));
+ return Errata("Failed to allocate stripe of size {} - no free block large
enough", len);
}
bool
@@ -869,7 +869,7 @@ Span::updateHeader()
if (OPEN_RW_FLAG) {
ssize_t r = pwrite(_fd, hdr, hdr_size, ts::CacheSpan::OFFSET);
if (r < ts::CacheSpan::OFFSET) {
- zret.push(0, errno, "Failed to update span - ", strerror(errno));
+ zret = Errata(make_errno_code(errno), "Failed to update span.");
}
} else {
std::cout << "Writing not enabled, no updates performed" << std::endl;
@@ -1055,10 +1055,10 @@ VolumeConfig::load(swoc::file::path const &path)
swoc::TextView value(line.take_prefix_if(&isspace));
swoc::TextView tag(value.take_prefix_at('='));
if (tag.empty()) {
- zret.push(0, 1, "Line ", ln, " is invalid");
+ zret.note("Line {} is invalid", ln);
} else if (0 == strcasecmp(tag, TAG_SIZE)) {
if (v.hasSize()) {
- zret.push(0, 5, "Line ", ln, " has field ", TAG_SIZE, " more than
once");
+ zret.note("Line {} has field {} more than once", ln, TAG_SIZE);
} else {
swoc::TextView text;
auto n = swoc::svtoi(value, &text);
@@ -1067,27 +1067,27 @@ VolumeConfig::load(swoc::file::path const &path)
if (percent.empty()) {
v._size = CacheStripeBlocks(round_up(Megabytes(n)));
if (v._size.count() != n) {
- zret.push(0, 0, "Line ", ln, " size ", n, " was rounded up
to ", v._size);
+ zret.note("Line {} size {} was rounded up to {}", ln, n,
v._size);
}
} else if ('%' == *percent && percent.size() == 1) {
v._percent = n;
} else {
- zret.push(0, 3, "Line ", ln, " has invalid value '", value, "'
for ", TAG_SIZE, " field");
+ zret.note("Line {} has invalid value '{}' for {} field", ln,
value, TAG_SIZE);
}
} else {
- zret.push(0, 2, "Line ", ln, " has invalid value '", value, "'
for ", TAG_SIZE, " field");
+ zret.note("Line {} has invalid value '{}' for {} field", ln,
value, TAG_SIZE);
}
}
} else if (0 == strcasecmp(tag, TAG_VOL)) {
if (v.hasIndex()) {
- zret.push(0, 6, "Line ", ln, " has field ", TAG_VOL, " more than
once");
+ zret.note("Line {} has field {} more than once", ln, TAG_VOL);
} else {
swoc::TextView text;
auto n = swoc::svtoi(value, &text);
if (text == value) {
v._idx = n;
} else {
- zret.push(0, 4, "Line ", ln, " has invalid value '", value, "'
for ", TAG_VOL, " field");
+ zret.note("Line {} has invalid value '{}' for {} field", ln,
value, TAG_VOL);
}
}
}
@@ -1096,15 +1096,15 @@ VolumeConfig::load(swoc::file::path const &path)
_volumes.push_back(std::move(v));
} else {
if (!v.hasSize()) {
- zret.push(0, 7, "Line ", ln, " does not have the required field ",
TAG_SIZE);
+ zret.note("Line {} does not have the required field {}", ln,
TAG_SIZE);
}
if (!v.hasIndex()) {
- zret.push(0, 8, "Line ", ln, " does not have the required field ",
TAG_VOL);
+ zret.note("Line {} does not have the required field {}", ln,
TAG_VOL);
}
}
}
} else {
- zret = Errata::Message(0, EBADF, "Unable to load ", path.string());
+ zret = Errata(make_errno_code(EBADF), "Unable to load ", path);
}
return zret;
}
@@ -1139,14 +1139,14 @@ Simulate_Span_Allocation()
VolumeAllocator va;
if (VolumeFile.empty()) {
- err.push(0, 9, "Volume config file not set");
+ err.note("Volume config file not set");
}
if (SpanFile.empty()) {
- err.push(0, 9, "Span file not set");
+ err.note("Span file not set");
}
if (err) {
- if ((err = va.load(SpanFile, VolumeFile)).isOK()) {
+ if ((err = va.load(SpanFile, VolumeFile)).is_ok()) {
err = va.fillAllSpans();
va.dumpVolumes();
}
@@ -1159,7 +1159,7 @@ Clear_Spans()
Cache cache;
if (!OPEN_RW_FLAG) {
- err.push(0, 1, "Writing Not Enabled.. Please use --write to enable writing
to disk");
+ err.note("Writing Not Enabled.. Please use --write to enable writing to
disk");
return;
}
@@ -1283,7 +1283,7 @@ Init_disk(swoc::file::path const &input_file_path)
VolumeAllocator va;
if (!OPEN_RW_FLAG) {
- err.push(0, 1, "Writing Not Enabled.. Please use --write to enable writing
to disk");
+ err.note("Writing Not Enabled.. Please use --write to enable writing to
disk");
return;
}
@@ -1349,7 +1349,7 @@ Scan_Cache(swoc::file::path const ®ex_path)
Cache cache;
std::vector<std::thread> threadPool;
if ((err = cache.loadSpan(SpanFile))) {
- if (err.size()) {
+ if (err.length()) {
return;
}
cache.dumpSpans(Cache::SpanDumpDepth::SPAN);
@@ -1424,7 +1424,7 @@ main(int argc, const char *argv[])
arguments.invoke();
}
- if (err.size()) {
+ if (err.length()) {
std::cerr << err;
exit(1);
}