foundationdb/fdbclient/ReadYourWrites.h

247 lines
10 KiB
C
Raw Normal View History

2017-05-26 04:48:44 +08:00
/*
* ReadYourWrites.h
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
*
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
*
2017-05-26 04:48:44 +08:00
* http://www.apache.org/licenses/LICENSE-2.0
*
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 FDBCLIENT_READYOURWRITES_H
#define FDBCLIENT_READYOURWRITES_H
#pragma once
#include "fdbclient/NativeAPI.actor.h"
#include "fdbclient/KeyRangeMap.h"
#include "fdbclient/RYWIterator.h"
2021-04-30 13:31:16 +08:00
#include "fdbclient/ISingleThreadTransaction.h"
2017-05-26 04:48:44 +08:00
#include <list>
// SOMEDAY: Optimize getKey to avoid using getRange
2017-05-26 04:48:44 +08:00
struct ReadYourWritesTransactionOptions {
bool readYourWritesDisabled : 1;
bool readAheadDisabled : 1;
bool readSystemKeys : 1;
bool writeSystemKeys : 1;
bool nextWriteDisableConflictRange : 1;
bool debugRetryLogging : 1;
bool disableUsedDuringCommitProtection : 1;
2020-05-06 02:41:18 +08:00
bool specialKeySpaceRelaxed : 1;
bool specialKeySpaceChangeConfiguration : 1;
2017-05-26 04:48:44 +08:00
double timeoutInSeconds;
int maxRetries;
int snapshotRywEnabled;
bool bypassUnreadable : 1;
2017-05-26 04:48:44 +08:00
ReadYourWritesTransactionOptions() {}
explicit ReadYourWritesTransactionOptions(Transaction const& tr);
void reset(Transaction const& tr);
2017-05-26 04:48:44 +08:00
bool getAndResetWriteConflictDisabled();
};
struct TransactionDebugInfo : public ReferenceCounted<TransactionDebugInfo> {
std::string transactionName;
double lastRetryLogTime;
TransactionDebugInfo() : transactionName(""), lastRetryLogTime() {}
2017-05-26 04:48:44 +08:00
};
// Values returned by a ReadYourWritesTransaction will contain a reference to the transaction's arena. Therefore,
// keeping a reference to a value longer than its creating transaction would hold all of the memory generated by the
// transaction
2020-11-28 02:10:41 +08:00
class ReadYourWritesTransaction final : NonCopyable,
2021-04-30 13:31:16 +08:00
public ISingleThreadTransaction,
2020-11-28 02:10:41 +08:00
public FastAllocated<ReadYourWritesTransaction> {
2017-05-26 04:48:44 +08:00
public:
explicit ReadYourWritesTransaction(Database const& cx);
2017-05-26 04:48:44 +08:00
~ReadYourWritesTransaction();
void setDatabase(Database const&) override;
2021-04-30 13:31:16 +08:00
void setVersion(Version v) override { tr.setVersion(v); }
Future<Version> getReadVersion() override;
Optional<Version> getCachedReadVersion() const override { return tr.getCachedReadVersion(); }
Future<Optional<Value>> get(const Key& key, Snapshot = Snapshot::False) override;
Future<Key> getKey(const KeySelector& key, Snapshot = Snapshot::False) override;
Future<RangeResult> getRange(const KeySelector& begin,
const KeySelector& end,
int limit,
Snapshot = Snapshot::False,
Reverse = Reverse::False) override;
Future<RangeResult> getRange(KeySelector begin,
KeySelector end,
GetRangeLimits limits,
Snapshot = Snapshot::False,
Reverse = Reverse::False) override;
Future<RangeResult> getRange(const KeyRange& keys,
int limit,
Snapshot snapshot = Snapshot::False,
Reverse reverse = Reverse::False) {
return getRange(KeySelector(firstGreaterOrEqual(keys.begin), keys.arena()),
KeySelector(firstGreaterOrEqual(keys.end), keys.arena()),
limit,
snapshot,
reverse);
2017-05-26 04:48:44 +08:00
}
2021-05-04 04:14:16 +08:00
Future<RangeResult> getRange(const KeyRange& keys,
GetRangeLimits limits,
Snapshot snapshot = Snapshot::False,
Reverse reverse = Reverse::False) {
return getRange(KeySelector(firstGreaterOrEqual(keys.begin), keys.arena()),
KeySelector(firstGreaterOrEqual(keys.end), keys.arena()),
limits,
snapshot,
reverse);
2017-05-26 04:48:44 +08:00
}
Future<RangeResult> getRangeAndFlatMap(KeySelector begin,
KeySelector end,
Key mapper,
GetRangeLimits limits,
Snapshot = Snapshot::False,
Reverse = Reverse::False) override;
2017-05-26 04:48:44 +08:00
2021-04-30 13:31:16 +08:00
[[nodiscard]] Future<Standalone<VectorRef<const char*>>> getAddressesForKey(const Key& key) override;
Future<Standalone<VectorRef<KeyRef>>> getRangeSplitPoints(const KeyRange& range, int64_t chunkSize) override;
Future<int64_t> getEstimatedRangeSizeBytes(const KeyRange& keys) override;
2017-05-26 04:48:44 +08:00
Future<Standalone<VectorRef<KeyRangeRef>>> getBlobGranuleRanges(const KeyRange& range) override;
Future<Standalone<VectorRef<BlobGranuleChunkRef>>> readBlobGranules(const KeyRange& range,
Version begin,
Optional<Version> readVersion,
Version* readVersionOut) override;
2021-04-30 13:31:16 +08:00
void addReadConflictRange(KeyRangeRef const& keys) override;
void makeSelfConflicting() override { tr.makeSelfConflicting(); }
2017-05-26 04:48:44 +08:00
2021-04-30 13:31:16 +08:00
void atomicOp(const KeyRef& key, const ValueRef& operand, uint32_t operationType) override;
void set(const KeyRef& key, const ValueRef& value) override;
void clear(const KeyRangeRef& range) override;
void clear(const KeyRef& key) override;
2017-05-26 04:48:44 +08:00
2021-04-30 13:31:16 +08:00
[[nodiscard]] Future<Void> watch(const Key& key) override;
2017-05-26 04:48:44 +08:00
2021-04-30 13:31:16 +08:00
void addWriteConflictRange(KeyRangeRef const& keys) override;
2017-05-26 04:48:44 +08:00
2021-04-30 13:31:16 +08:00
[[nodiscard]] Future<Void> commit() override;
Version getCommittedVersion() const override { return tr.getCommittedVersion(); }
int64_t getApproximateSize() const override { return approximateSize; }
2021-04-30 13:31:16 +08:00
[[nodiscard]] Future<Standalone<StringRef>> getVersionstamp() override;
2017-05-26 04:48:44 +08:00
2021-04-30 13:31:16 +08:00
void setOption(FDBTransactionOptions::Option option, Optional<StringRef> value = Optional<StringRef>()) override;
2017-05-26 04:48:44 +08:00
2021-04-30 13:31:16 +08:00
[[nodiscard]] Future<Void> onError(Error const& e) override;
2017-05-26 04:48:44 +08:00
// These are to permit use as state variables in actors:
ReadYourWritesTransaction() : cache(&arena), writes(&arena) {}
2020-06-10 08:33:41 +08:00
void operator=(ReadYourWritesTransaction&& r) noexcept;
ReadYourWritesTransaction(ReadYourWritesTransaction&& r) noexcept;
2017-05-26 04:48:44 +08:00
2021-04-30 13:31:16 +08:00
void cancel() override;
void reset() override;
void debugTransaction(UID dID) override { tr.debugTransaction(dID); }
2017-05-26 04:48:44 +08:00
2020-04-30 02:52:26 +08:00
Future<Void> debug_onIdle() { return reading; }
2020-04-29 03:44:34 +08:00
// Wait for all reads that are currently pending to complete
2020-04-30 02:52:26 +08:00
Future<Void> pendingReads() { return resetPromise.getFuture() || reading; }
// Throws before the lifetime of this transaction ends
Future<Void> resetFuture() { return resetPromise.getFuture(); }
2017-05-26 04:48:44 +08:00
void checkDeferredError() const override {
tr.checkDeferredError();
if (deferredError.code() != invalid_error_code)
throw deferredError;
}
2017-05-26 04:48:44 +08:00
2021-04-30 13:31:16 +08:00
void getWriteConflicts(KeyRangeMap<bool>* result) override;
2017-05-26 04:48:44 +08:00
Database getDatabase() const { return tr.getDatabase(); }
Reference<const TransactionState> getTransactionState() const { return tr.trState; }
2020-12-04 06:06:11 +08:00
void setTransactionID(uint64_t id);
void setToken(uint64_t token);
2020-04-30 05:43:37 +08:00
// Read from the special key space readConflictRangeKeysRange
2021-05-04 04:14:16 +08:00
RangeResult getReadConflictRangeIntersecting(KeyRangeRef kr);
2020-04-30 05:43:37 +08:00
// Read from the special key space writeConflictRangeKeysRange
2021-05-04 04:14:16 +08:00
RangeResult getWriteConflictRangeIntersecting(KeyRangeRef kr);
2020-05-06 02:41:18 +08:00
bool specialKeySpaceRelaxed() const { return options.specialKeySpaceRelaxed; }
bool specialKeySpaceChangeConfiguration() const { return options.specialKeySpaceChangeConfiguration; }
2020-05-06 02:41:18 +08:00
KeyRangeMap<std::pair<bool, Optional<Value>>>& getSpecialKeySpaceWriteMap() { return specialKeySpaceWriteMap; }
bool readYourWritesDisabled() const { return options.readYourWritesDisabled; }
const Optional<std::string>& getSpecialKeySpaceErrorMsg() { return specialKeySpaceErrorMsg; }
void setSpecialKeySpaceErrorMsg(const std::string& msg) { specialKeySpaceErrorMsg = msg; }
Transaction& getTransaction() { return tr; }
2020-05-06 02:41:18 +08:00
2021-08-12 09:39:47 +08:00
// used in template functions as returned Future type
template <typename Type>
using FutureT = Future<Type>;
2017-05-26 04:48:44 +08:00
private:
friend class RYWImpl;
Arena arena;
Transaction tr;
SnapshotCache cache;
WriteMap writes;
CoalescedKeyRefRangeMap<bool> readConflicts;
Map<Key, std::vector<Reference<Watch>>> watchMap; // Keys that are being watched in this transaction
2017-05-26 04:48:44 +08:00
Promise<Void> resetPromise;
AndFuture reading;
int retries;
int64_t approximateSize;
2017-05-26 04:48:44 +08:00
Future<Void> timeoutActor;
double creationTime;
bool commitStarted;
// For reading conflict ranges from the special key space
VectorRef<KeyRef> versionStampKeys;
Future<Standalone<StringRef>> versionStampFuture;
Standalone<VectorRef<KeyRangeRef>>
nativeReadRanges; // Used to read conflict ranges after committing an ryw disabled transaction
Standalone<VectorRef<KeyRangeRef>>
nativeWriteRanges; // Used to read conflict ranges after committing an ryw disabled transaction
2017-05-26 04:48:44 +08:00
Reference<TransactionDebugInfo> transactionDebugInfo;
KeyRangeMap<std::pair<bool, Optional<Value>>> specialKeySpaceWriteMap;
Optional<std::string> specialKeySpaceErrorMsg;
void resetTimeout();
void updateConflictMap(KeyRef const& key, WriteMap::iterator& it); // pre: it.segmentContains(key)
void updateConflictMap(
KeyRangeRef const& keys,
WriteMap::iterator& it); // pre: it.segmentContains(keys.begin), keys are already inside this->arena
void writeRangeToNativeTransaction(KeyRangeRef const& keys);
2017-05-26 04:48:44 +08:00
void resetRyow(); // doesn't reset the encapsulated transaction, or creation time/retry state
KeyRef getMaxReadKey();
KeyRef getMaxWriteKey();
bool checkUsedDuringCommit();
void debugLogRetries(Optional<Error> error = Optional<Error>());
void setOptionImpl(FDBTransactionOptions::Option option, Optional<StringRef> value = Optional<StringRef>());
void applyPersistentOptions();
std::vector<std::pair<FDBTransactionOptions::Option, Optional<Standalone<StringRef>>>> persistentOptions;
2017-05-26 04:48:44 +08:00
ReadYourWritesTransactionOptions options;
};
#endif