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
|
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 FDBCLIENT_READYOURWRITES_H
|
|
|
|
#define FDBCLIENT_READYOURWRITES_H
|
|
|
|
#pragma once
|
|
|
|
|
2019-02-18 07:41:16 +08:00
|
|
|
#include "fdbclient/NativeAPI.actor.h"
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbclient/KeyRangeMap.h"
|
|
|
|
#include "fdbclient/RYWIterator.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
#include <list>
|
|
|
|
|
|
|
|
//SOMEDAY: Optimize getKey to avoid using getRange
|
|
|
|
|
|
|
|
struct ReadYourWritesTransactionOptions {
|
|
|
|
bool readYourWritesDisabled : 1;
|
|
|
|
bool readAheadDisabled : 1;
|
|
|
|
bool readSystemKeys : 1;
|
|
|
|
bool writeSystemKeys : 1;
|
|
|
|
bool nextWriteDisableConflictRange : 1;
|
|
|
|
bool debugRetryLogging : 1;
|
|
|
|
bool disableUsedDuringCommitProtection : 1;
|
|
|
|
double timeoutInSeconds;
|
|
|
|
int maxRetries;
|
|
|
|
int snapshotRywEnabled;
|
|
|
|
|
|
|
|
ReadYourWritesTransactionOptions() {}
|
2019-03-22 04:47:12 +08:00
|
|
|
explicit ReadYourWritesTransactionOptions(Transaction const& tr);
|
2019-03-20 00:15:41 +08:00
|
|
|
void reset(Transaction const& tr);
|
2019-03-22 06:48:40 +08:00
|
|
|
void fullReset(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() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
//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
|
|
|
|
class ReadYourWritesTransaction : NonCopyable, public ReferenceCounted<ReadYourWritesTransaction>, public FastAllocated<ReadYourWritesTransaction> {
|
|
|
|
public:
|
|
|
|
static ReadYourWritesTransaction* allocateOnForeignThread() {
|
|
|
|
ReadYourWritesTransaction *tr = (ReadYourWritesTransaction*)ReadYourWritesTransaction::operator new( sizeof(ReadYourWritesTransaction) );
|
|
|
|
tr->tr.preinitializeOnForeignThread();
|
|
|
|
return tr;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit ReadYourWritesTransaction( Database const& cx );
|
|
|
|
~ReadYourWritesTransaction();
|
|
|
|
|
|
|
|
void setVersion( Version v ) { tr.setVersion(v); }
|
|
|
|
Future<Version> getReadVersion();
|
|
|
|
Future< Optional<Value> > get( const Key& key, bool snapshot = false );
|
|
|
|
Future< Key > getKey( const KeySelector& key, bool snapshot = false );
|
|
|
|
Future< Standalone<RangeResultRef> > getRange( const KeySelector& begin, const KeySelector& end, int limit, bool snapshot = false, bool reverse = false );
|
|
|
|
Future< Standalone<RangeResultRef> > getRange( KeySelector begin, KeySelector end, GetRangeLimits limits, bool snapshot = false, bool reverse = false );
|
|
|
|
Future< Standalone<RangeResultRef> > getRange( const KeyRange& keys, int limit, bool snapshot = false, bool reverse = false ) {
|
|
|
|
return getRange( KeySelector( firstGreaterOrEqual(keys.begin), keys.arena() ),
|
|
|
|
KeySelector( firstGreaterOrEqual(keys.end), keys.arena() ), limit, snapshot, reverse );
|
|
|
|
}
|
|
|
|
Future< Standalone<RangeResultRef> > getRange( const KeyRange& keys, GetRangeLimits limits, bool snapshot = false, bool reverse = false ) {
|
|
|
|
return getRange( KeySelector( firstGreaterOrEqual(keys.begin), keys.arena() ),
|
|
|
|
KeySelector( firstGreaterOrEqual(keys.end), keys.arena() ), limits, snapshot, reverse );
|
|
|
|
}
|
|
|
|
|
|
|
|
Future< Standalone<VectorRef<const char*>> > getAddressesForKey(const Key& key);
|
|
|
|
|
|
|
|
void addReadConflictRange( KeyRangeRef const& keys );
|
|
|
|
void makeSelfConflicting() { tr.makeSelfConflicting(); }
|
|
|
|
|
|
|
|
void atomicOp( const KeyRef& key, const ValueRef& operand, uint32_t operationType );
|
|
|
|
void set( const KeyRef& key, const ValueRef& value );
|
|
|
|
void clear( const KeyRangeRef& range );
|
|
|
|
void clear( const KeyRef& key );
|
|
|
|
|
|
|
|
Future<Void> watch(const Key& key);
|
|
|
|
|
|
|
|
void addWriteConflictRange( KeyRangeRef const& keys );
|
|
|
|
|
|
|
|
Future<Void> commit();
|
|
|
|
Version getCommittedVersion() { return tr.getCommittedVersion(); }
|
2018-02-01 05:47:36 +08:00
|
|
|
Future<Standalone<StringRef>> getVersionstamp();
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
void setOption( FDBTransactionOptions::Option option, Optional<StringRef> value = Optional<StringRef>() );
|
|
|
|
|
|
|
|
Future<Void> onError( Error const& e );
|
|
|
|
|
|
|
|
// These are to permit use as state variables in actors:
|
|
|
|
ReadYourWritesTransaction() : cache(&arena), writes(&arena) {}
|
2019-01-26 08:49:59 +08:00
|
|
|
void operator=(ReadYourWritesTransaction&& r) BOOST_NOEXCEPT;
|
|
|
|
ReadYourWritesTransaction(ReadYourWritesTransaction&& r) BOOST_NOEXCEPT;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
virtual void addref() { ReferenceCounted<ReadYourWritesTransaction>::addref(); }
|
|
|
|
virtual void delref() { ReferenceCounted<ReadYourWritesTransaction>::delref(); }
|
|
|
|
|
|
|
|
void cancel();
|
|
|
|
void reset();
|
|
|
|
void debugTransaction(UID dID) { tr.debugTransaction(dID); }
|
|
|
|
|
|
|
|
Future<Void> debug_onIdle() { return reading; }
|
|
|
|
|
|
|
|
// Used by ThreadSafeTransaction for exceptions thrown in void methods
|
2018-09-22 06:58:14 +08:00
|
|
|
Error deferredError;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2018-09-22 06:58:14 +08:00
|
|
|
void checkDeferredError() { tr.checkDeferredError(); if (deferredError.code() != invalid_error_code) throw deferredError; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
void getWriteConflicts( KeyRangeMap<bool> *result );
|
|
|
|
|
2019-03-20 00:15:41 +08:00
|
|
|
Database getDatabase() {
|
2017-05-26 04:48:44 +08:00
|
|
|
return tr.getDatabase();
|
|
|
|
}
|
|
|
|
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
|
|
|
|
Promise<Void> resetPromise;
|
|
|
|
AndFuture reading;
|
|
|
|
int retries;
|
|
|
|
Future<Void> timeoutActor;
|
|
|
|
double creationTime;
|
|
|
|
bool commitStarted;
|
|
|
|
|
|
|
|
Reference<TransactionDebugInfo> transactionDebugInfo;
|
|
|
|
|
2019-03-22 02:45:43 +08:00
|
|
|
void resetTimeout();
|
2017-05-26 04:48:44 +08:00
|
|
|
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 );
|
|
|
|
|
|
|
|
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>());
|
|
|
|
|
|
|
|
ReadYourWritesTransactionOptions options;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|