2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* CommitTransaction.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed 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
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FLOW_FDBCLIENT_COMMITTRANSACTION_H
|
|
|
|
#define FLOW_FDBCLIENT_COMMITTRANSACTION_H
|
|
|
|
#pragma once
|
|
|
|
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbclient/FDBTypes.h"
|
2021-05-30 23:28:26 +08:00
|
|
|
#include "fdbclient/Knobs.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-09-06 02:30:02 +08:00
|
|
|
// The versioned message has wire format : -1, version, messages
|
|
|
|
static const int32_t VERSION_HEADER = -1;
|
|
|
|
|
2019-01-31 17:23:32 +08:00
|
|
|
static const char* typeString[] = { "SetValue",
|
|
|
|
"ClearRange",
|
|
|
|
"AddValue",
|
|
|
|
"DebugKeyRange",
|
|
|
|
"DebugKey",
|
|
|
|
"NoOp",
|
|
|
|
"And",
|
|
|
|
"Or",
|
|
|
|
"Xor",
|
|
|
|
"AppendIfFits",
|
|
|
|
"AvailableForReuse",
|
|
|
|
"Reserved_For_LogProtocolMessage",
|
|
|
|
"Max",
|
|
|
|
"Min",
|
|
|
|
"SetVersionstampedKey",
|
|
|
|
"SetVersionstampedValue",
|
|
|
|
"ByteMin",
|
|
|
|
"ByteMax",
|
|
|
|
"MinV2",
|
|
|
|
"AndV2",
|
2020-04-02 01:49:51 +08:00
|
|
|
"CompareAndClear",
|
2020-08-28 06:11:16 +08:00
|
|
|
"Reserved_For_SpanContextMessage",
|
2020-04-02 01:49:51 +08:00
|
|
|
"MAX_ATOMIC_OP" };
|
2017-11-18 15:36:09 +08:00
|
|
|
|
2020-02-12 03:44:53 +08:00
|
|
|
struct MutationRef {
|
2021-03-11 02:06:03 +08:00
|
|
|
static const int OVERHEAD_BYTES = 12; // 12 is the size of Header in MutationList entries
|
2019-01-31 17:23:32 +08:00
|
|
|
enum Type : uint8_t {
|
|
|
|
SetValue = 0,
|
|
|
|
ClearRange,
|
|
|
|
AddValue,
|
|
|
|
DebugKeyRange,
|
|
|
|
DebugKey,
|
|
|
|
NoOp,
|
|
|
|
And,
|
|
|
|
Or,
|
|
|
|
Xor,
|
|
|
|
AppendIfFits,
|
|
|
|
AvailableForReuse,
|
|
|
|
Reserved_For_LogProtocolMessage /* See fdbserver/LogProtocolMessage.h */,
|
|
|
|
Max,
|
|
|
|
Min,
|
|
|
|
SetVersionstampedKey,
|
|
|
|
SetVersionstampedValue,
|
|
|
|
ByteMin,
|
|
|
|
ByteMax,
|
|
|
|
MinV2,
|
|
|
|
AndV2,
|
|
|
|
CompareAndClear,
|
2020-08-28 06:11:16 +08:00
|
|
|
Reserved_For_SpanContextMessage /* See fdbserver/SpanContextMessage.h */,
|
2019-01-31 17:23:32 +08:00
|
|
|
MAX_ATOMIC_OP
|
|
|
|
};
|
2017-05-26 04:48:44 +08:00
|
|
|
// This is stored this way for serialization purposes.
|
|
|
|
uint8_t type;
|
|
|
|
StringRef param1, param2;
|
|
|
|
|
|
|
|
MutationRef() {}
|
2021-03-11 02:06:03 +08:00
|
|
|
MutationRef(Type t, StringRef a, StringRef b) : type(t), param1(a), param2(b) {}
|
|
|
|
MutationRef(Arena& to, Type t, StringRef a, StringRef b) : type(t), param1(to, a), param2(to, b) {}
|
|
|
|
MutationRef(Arena& to, const MutationRef& from)
|
|
|
|
: type(from.type), param1(to, from.param1), param2(to, from.param2) {}
|
2020-02-12 03:44:53 +08:00
|
|
|
int totalSize() const { return OVERHEAD_BYTES + param1.size() + param2.size(); }
|
2017-05-26 04:48:44 +08:00
|
|
|
int expectedSize() const { return param1.size() + param2.size(); }
|
2020-02-12 03:44:53 +08:00
|
|
|
int weightedTotalSize() const {
|
2020-02-19 08:41:19 +08:00
|
|
|
// AtomicOp can cause more workload to FDB cluster than the same-size set mutation;
|
|
|
|
// Amplify atomicOp size to consider such extra workload.
|
|
|
|
// A good value for FASTRESTORE_ATOMICOP_WEIGHT needs experimental evaluations.
|
2020-02-12 03:44:53 +08:00
|
|
|
if (isAtomicOp()) {
|
2021-05-30 23:28:26 +08:00
|
|
|
return totalSize() * CLIENT_KNOBS->FASTRESTORE_ATOMICOP_WEIGHT;
|
2020-02-12 03:44:53 +08:00
|
|
|
} else {
|
|
|
|
return totalSize();
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
std::string toString() const {
|
2020-04-14 05:50:43 +08:00
|
|
|
return format("code: %s param1: %s param2: %s",
|
2021-03-11 02:06:03 +08:00
|
|
|
type < MutationRef::MAX_ATOMIC_OP ? typeString[(int)type] : "Unset",
|
|
|
|
printable(param1).c_str(),
|
2020-04-14 05:50:43 +08:00
|
|
|
printable(param2).c_str());
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2020-02-19 08:41:59 +08:00
|
|
|
bool isAtomicOp() const { return (ATOMIC_MASK & (1 << type)) != 0; }
|
2020-02-12 03:44:53 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
template <class Ar>
|
2021-03-11 02:06:03 +08:00
|
|
|
void serialize(Ar& ar) {
|
2020-05-05 07:07:04 +08:00
|
|
|
if (ar.isSerializing && type == ClearRange && equalsKeyAfter(param1, param2)) {
|
2020-05-01 02:13:59 +08:00
|
|
|
StringRef empty;
|
|
|
|
serializer(ar, type, param2, empty);
|
2020-01-04 09:46:24 +08:00
|
|
|
} else {
|
|
|
|
serializer(ar, type, param1, param2);
|
|
|
|
}
|
2020-05-01 02:13:59 +08:00
|
|
|
if (ar.isDeserializing && type == ClearRange && param2 == StringRef() && param1 != StringRef()) {
|
2021-03-11 02:06:03 +08:00
|
|
|
ASSERT(param1[param1.size() - 1] == '\x00');
|
2020-01-04 09:46:24 +08:00
|
|
|
param2 = param1;
|
2021-03-11 02:06:03 +08:00
|
|
|
param1 = param2.substr(0, param2.size() - 1);
|
2020-01-04 09:46:24 +08:00
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// These masks define which mutation types have particular properties (they are used to implement
|
|
|
|
// isSingleKeyMutation() etc)
|
2019-01-31 17:23:32 +08:00
|
|
|
enum {
|
|
|
|
ATOMIC_MASK = (1 << AddValue) | (1 << And) | (1 << Or) | (1 << Xor) | (1 << AppendIfFits) | (1 << Max) |
|
|
|
|
(1 << Min) | (1 << SetVersionstampedKey) | (1 << SetVersionstampedValue) | (1 << ByteMin) |
|
|
|
|
(1 << ByteMax) | (1 << MinV2) | (1 << AndV2) | (1 << CompareAndClear),
|
|
|
|
SINGLE_KEY_MASK = ATOMIC_MASK | (1 << SetValue),
|
|
|
|
NON_ASSOCIATIVE_MASK = (1 << AddValue) | (1 << Or) | (1 << Xor) | (1 << Max) | (1 << Min) |
|
|
|
|
(1 << SetVersionstampedKey) | (1 << SetVersionstampedValue) | (1 << MinV2) |
|
|
|
|
(1 << CompareAndClear)
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
template <>
|
Clean up and rework the debugMutation API.
As a relatively unknown debugging tool for simulation tests, one could
have simulation print when a particular key is handled in various stages
of the commit process. This functionality was enabled by changing a 0
to a 1 in an #if, and changing a constant to the key in question.
As a proxy and storage server handle mutations, they call debugMutation
or debugKeyRange, which then checks against the mutation against the key
in question, and logs if they match. A mixture of printfs and
TraceEvents would then be emitted, and for this to actually be usable,
one also needs to comment out some particularly spammy debugKeyRange()
calls.
This PR reworks the API of debugMutation/debugKeyRange, pulls it out
into its own file, and trims what is logged by default into something
useful and understandable:
* debugMutation() now returns a TraceEvent, that one can add more details to before it is logged.
* Data distribution and storage server cleanup operations are no longer logged by default
2020-03-27 16:35:26 +08:00
|
|
|
struct Traceable<MutationRef> : std::true_type {
|
2021-03-11 02:06:03 +08:00
|
|
|
static std::string toString(MutationRef const& value) { return value.toString(); }
|
Clean up and rework the debugMutation API.
As a relatively unknown debugging tool for simulation tests, one could
have simulation print when a particular key is handled in various stages
of the commit process. This functionality was enabled by changing a 0
to a 1 in an #if, and changing a constant to the key in question.
As a proxy and storage server handle mutations, they call debugMutation
or debugKeyRange, which then checks against the mutation against the key
in question, and logs if they match. A mixture of printfs and
TraceEvents would then be emitted, and for this to actually be usable,
one also needs to comment out some particularly spammy debugKeyRange()
calls.
This PR reworks the API of debugMutation/debugKeyRange, pulls it out
into its own file, and trims what is logged by default into something
useful and understandable:
* debugMutation() now returns a TraceEvent, that one can add more details to before it is logged.
* Data distribution and storage server cleanup operations are no longer logged by default
2020-03-27 16:35:26 +08:00
|
|
|
};
|
|
|
|
|
2020-04-06 06:00:36 +08:00
|
|
|
static inline std::string getTypeString(MutationRef::Type type) {
|
|
|
|
return type < MutationRef::MAX_ATOMIC_OP ? typeString[(int)type] : "Unset";
|
|
|
|
}
|
|
|
|
|
2020-04-14 05:50:43 +08:00
|
|
|
static inline std::string getTypeString(uint8_t type) {
|
|
|
|
return type < MutationRef::MAX_ATOMIC_OP ? typeString[type] : "Unset";
|
|
|
|
}
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
// A 'single key mutation' is one which affects exactly the value of the key specified by its param1
|
|
|
|
static inline bool isSingleKeyMutation(MutationRef::Type type) {
|
2021-03-11 02:06:03 +08:00
|
|
|
return (MutationRef::SINGLE_KEY_MASK & (1 << type)) != 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the given type can be safely cast to MutationRef::Type and used as a parameter to
|
|
|
|
// isSingleKeyMutation, isAtomicOp, etc. It does NOT mean that the type is a valid type of a MutationRef in any
|
|
|
|
// particular context.
|
|
|
|
static inline bool isValidMutationType(uint32_t type) {
|
|
|
|
return (type < MutationRef::MAX_ATOMIC_OP);
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// An 'atomic operation' is a single key mutation which sets the key specified by its param1 to a
|
|
|
|
// nontrivial function of the previous value of the key and param2, and thus requires a
|
2017-05-26 04:48:44 +08:00
|
|
|
// read/modify/write to implement. (Basically a single key mutation other than a set)
|
|
|
|
static inline bool isAtomicOp(MutationRef::Type mutationType) {
|
2021-03-11 02:06:03 +08:00
|
|
|
return (MutationRef::ATOMIC_MASK & (1 << mutationType)) != 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true for operations which do not obey the associative law (i.e. a*(b*c) == (a*b)*c) in all cases
|
|
|
|
// unless a, b, and c have equal lengths, in which case even these operations are associative.
|
|
|
|
static inline bool isNonAssociativeOp(MutationRef::Type mutationType) {
|
2021-03-11 02:06:03 +08:00
|
|
|
return (MutationRef::NON_ASSOCIATIVE_MASK & (1 << mutationType)) != 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct CommitTransactionRef {
|
2020-03-25 00:48:03 +08:00
|
|
|
CommitTransactionRef() : read_snapshot(0), report_conflicting_keys(false) {}
|
2020-03-27 06:52:30 +08:00
|
|
|
CommitTransactionRef(Arena& a, const CommitTransactionRef& from)
|
|
|
|
: read_conflict_ranges(a, from.read_conflict_ranges), write_conflict_ranges(a, from.write_conflict_ranges),
|
|
|
|
mutations(a, from.mutations), read_snapshot(from.read_snapshot),
|
|
|
|
report_conflicting_keys(from.report_conflicting_keys) {}
|
2021-03-11 02:06:03 +08:00
|
|
|
VectorRef<KeyRangeRef> read_conflict_ranges;
|
|
|
|
VectorRef<KeyRangeRef> write_conflict_ranges;
|
|
|
|
VectorRef<MutationRef> mutations;
|
2017-05-26 04:48:44 +08:00
|
|
|
Version read_snapshot;
|
2020-03-25 00:48:03 +08:00
|
|
|
bool report_conflicting_keys;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
template <class Ar>
|
2020-03-25 07:25:14 +08:00
|
|
|
force_inline void serialize(Ar& ar) {
|
|
|
|
if constexpr (is_fb_function<Ar>) {
|
2021-03-11 02:06:03 +08:00
|
|
|
serializer(
|
|
|
|
ar, read_conflict_ranges, write_conflict_ranges, mutations, read_snapshot, report_conflicting_keys);
|
2020-03-25 07:25:14 +08:00
|
|
|
} else {
|
|
|
|
serializer(ar, read_conflict_ranges, write_conflict_ranges, mutations, read_snapshot);
|
|
|
|
if (ar.protocolVersion().hasReportConflictingKeys()) {
|
|
|
|
serializer(ar, report_conflicting_keys);
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience for internal code required to manipulate these without the Native API
|
2021-03-11 02:06:03 +08:00
|
|
|
void set(Arena& arena, KeyRef const& key, ValueRef const& value) {
|
2017-05-26 04:48:44 +08:00
|
|
|
mutations.push_back_deep(arena, MutationRef(MutationRef::SetValue, key, value));
|
|
|
|
write_conflict_ranges.push_back(arena, singleKeyRange(key, arena));
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
void clear(Arena& arena, KeyRangeRef const& keys) {
|
2017-05-26 04:48:44 +08:00
|
|
|
mutations.push_back_deep(arena, MutationRef(MutationRef::ClearRange, keys.begin, keys.end));
|
|
|
|
write_conflict_ranges.push_back_deep(arena, keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t expectedSize() const {
|
|
|
|
return read_conflict_ranges.expectedSize() + write_conflict_ranges.expectedSize() + mutations.expectedSize();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-15 03:58:37 +08:00
|
|
|
struct MutationsAndVersionRef {
|
|
|
|
VectorRef<MutationRef> mutations;
|
2021-10-26 02:38:48 +08:00
|
|
|
Version version = invalidVersion;
|
|
|
|
Version knownCommittedVersion = invalidVersion;
|
2021-10-15 03:58:37 +08:00
|
|
|
|
|
|
|
MutationsAndVersionRef() {}
|
|
|
|
explicit MutationsAndVersionRef(Version version, Version knownCommittedVersion)
|
|
|
|
: version(version), knownCommittedVersion(knownCommittedVersion) {}
|
|
|
|
MutationsAndVersionRef(VectorRef<MutationRef> mutations, Version version, Version knownCommittedVersion)
|
|
|
|
: mutations(mutations), version(version), knownCommittedVersion(knownCommittedVersion) {}
|
|
|
|
MutationsAndVersionRef(Arena& to, VectorRef<MutationRef> mutations, Version version, Version knownCommittedVersion)
|
|
|
|
: mutations(to, mutations), version(version), knownCommittedVersion(knownCommittedVersion) {}
|
|
|
|
MutationsAndVersionRef(Arena& to, const MutationsAndVersionRef& from)
|
|
|
|
: mutations(to, from.mutations), version(from.version), knownCommittedVersion(from.knownCommittedVersion) {}
|
|
|
|
int expectedSize() const { return mutations.expectedSize(); }
|
|
|
|
|
|
|
|
struct OrderByVersion {
|
|
|
|
bool operator()(MutationsAndVersionRef const& a, MutationsAndVersionRef const& b) const {
|
|
|
|
return a.version < b.version;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
|
|
|
serializer(ar, mutations, version, knownCommittedVersion);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
#endif
|