2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* MultiVersionTransaction.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_MULTIVERSIONTRANSACTION_H
|
|
|
|
#define FDBCLIENT_MULTIVERSIONTRANSACTION_H
|
|
|
|
#pragma once
|
|
|
|
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbclient/FDBOptions.g.h"
|
|
|
|
#include "fdbclient/FDBTypes.h"
|
|
|
|
#include "fdbclient/IClientApi.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
#include "flow/ThreadHelper.actor.h"
|
|
|
|
|
2021-04-15 03:50:30 +08:00
|
|
|
// FdbCApi is used as a wrapper around the FoundationDB C API that gets loaded from an external client library.
|
|
|
|
// All of the required functions loaded from that external library are stored in function pointers in this struct.
|
2017-05-26 04:48:44 +08:00
|
|
|
struct FdbCApi : public ThreadSafeReferenceCounted<FdbCApi> {
|
|
|
|
typedef struct future FDBFuture;
|
|
|
|
typedef struct cluster FDBCluster;
|
|
|
|
typedef struct database FDBDatabase;
|
|
|
|
typedef struct transaction FDBTransaction;
|
|
|
|
|
|
|
|
#pragma pack(push, 4)
|
2020-06-19 00:41:50 +08:00
|
|
|
typedef struct key {
|
|
|
|
const uint8_t* key;
|
|
|
|
int keyLength;
|
|
|
|
} FDBKey;
|
2017-05-26 04:48:44 +08:00
|
|
|
typedef struct keyvalue {
|
2021-03-11 02:06:03 +08:00
|
|
|
const void* key;
|
2017-05-26 04:48:44 +08:00
|
|
|
int keyLength;
|
2021-03-11 02:06:03 +08:00
|
|
|
const void* value;
|
2017-05-26 04:48:44 +08:00
|
|
|
int valueLength;
|
|
|
|
} FDBKeyValue;
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
typedef int fdb_error_t;
|
|
|
|
typedef int fdb_bool_t;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
typedef void (*FDBCallback)(FDBFuture* future, void* callback_parameter);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Network
|
2017-05-26 04:48:44 +08:00
|
|
|
fdb_error_t (*selectApiVersion)(int runtimeVersion, int headerVersion);
|
|
|
|
const char* (*getClientVersion)();
|
2021-03-11 02:06:03 +08:00
|
|
|
fdb_error_t (*setNetworkOption)(FDBNetworkOptions::Option option, uint8_t const* value, int valueLength);
|
2017-05-26 04:48:44 +08:00
|
|
|
fdb_error_t (*setupNetwork)();
|
|
|
|
fdb_error_t (*runNetwork)();
|
|
|
|
fdb_error_t (*stopNetwork)();
|
2021-03-11 02:06:03 +08:00
|
|
|
fdb_error_t* (*createDatabase)(const char* clusterFilePath, FDBDatabase** db);
|
|
|
|
|
|
|
|
// Database
|
|
|
|
fdb_error_t (*databaseCreateTransaction)(FDBDatabase* database, FDBTransaction** tr);
|
|
|
|
fdb_error_t (*databaseSetOption)(FDBDatabase* database,
|
|
|
|
FDBDatabaseOptions::Option option,
|
|
|
|
uint8_t const* value,
|
|
|
|
int valueLength);
|
|
|
|
void (*databaseDestroy)(FDBDatabase* database);
|
|
|
|
FDBFuture* (*databaseRebootWorker)(FDBDatabase* database,
|
|
|
|
uint8_t const* address,
|
|
|
|
int addressLength,
|
|
|
|
fdb_bool_t check,
|
|
|
|
int duration);
|
|
|
|
FDBFuture* (*databaseForceRecoveryWithDataLoss)(FDBDatabase* database, uint8_t const* dcid, int dcidLength);
|
|
|
|
FDBFuture* (*databaseCreateSnapshot)(FDBDatabase* database,
|
|
|
|
uint8_t const* uid,
|
|
|
|
int uidLength,
|
|
|
|
uint8_t const* snapshotCommmand,
|
|
|
|
int snapshotCommandLength);
|
2021-03-16 07:23:56 +08:00
|
|
|
double (*databaseGetMainThreadBusyness)(FDBDatabase* database);
|
2021-04-15 03:50:30 +08:00
|
|
|
FDBFuture* (*databaseGetServerProtocol)(FDBDatabase* database, uint64_t expectedVersion);
|
2021-03-11 02:06:03 +08:00
|
|
|
|
|
|
|
// Transaction
|
|
|
|
fdb_error_t (*transactionSetOption)(FDBTransaction* tr,
|
|
|
|
FDBTransactionOptions::Option option,
|
|
|
|
uint8_t const* value,
|
|
|
|
int valueLength);
|
|
|
|
void (*transactionDestroy)(FDBTransaction* tr);
|
|
|
|
|
|
|
|
void (*transactionSetReadVersion)(FDBTransaction* tr, int64_t version);
|
|
|
|
FDBFuture* (*transactionGetReadVersion)(FDBTransaction* tr);
|
|
|
|
|
|
|
|
FDBFuture* (*transactionGet)(FDBTransaction* tr, uint8_t const* keyName, int keyNameLength, fdb_bool_t snapshot);
|
|
|
|
FDBFuture* (*transactionGetKey)(FDBTransaction* tr,
|
|
|
|
uint8_t const* keyName,
|
|
|
|
int keyNameLength,
|
|
|
|
fdb_bool_t orEqual,
|
|
|
|
int offset,
|
|
|
|
fdb_bool_t snapshot);
|
|
|
|
FDBFuture* (*transactionGetAddressesForKey)(FDBTransaction* tr, uint8_t const* keyName, int keyNameLength);
|
|
|
|
FDBFuture* (*transactionGetRange)(FDBTransaction* tr,
|
|
|
|
uint8_t const* beginKeyName,
|
|
|
|
int beginKeyNameLength,
|
|
|
|
fdb_bool_t beginOrEqual,
|
|
|
|
int beginOffset,
|
|
|
|
uint8_t const* endKeyName,
|
|
|
|
int endKeyNameLength,
|
|
|
|
fdb_bool_t endOrEqual,
|
|
|
|
int endOffset,
|
|
|
|
int limit,
|
|
|
|
int targetBytes,
|
|
|
|
FDBStreamingModes::Option mode,
|
|
|
|
int iteration,
|
|
|
|
fdb_bool_t snapshot,
|
|
|
|
fdb_bool_t reverse);
|
2017-05-26 04:48:44 +08:00
|
|
|
FDBFuture* (*transactionGetVersionstamp)(FDBTransaction* tr);
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
void (*transactionSet)(FDBTransaction* tr,
|
|
|
|
uint8_t const* keyName,
|
|
|
|
int keyNameLength,
|
|
|
|
uint8_t const* value,
|
|
|
|
int valueLength);
|
|
|
|
void (*transactionClear)(FDBTransaction* tr, uint8_t const* keyName, int keyNameLength);
|
|
|
|
void (*transactionClearRange)(FDBTransaction* tr,
|
|
|
|
uint8_t const* beginKeyName,
|
|
|
|
int beginKeyNameLength,
|
|
|
|
uint8_t const* endKeyName,
|
|
|
|
int endKeyNameLength);
|
|
|
|
void (*transactionAtomicOp)(FDBTransaction* tr,
|
|
|
|
uint8_t const* keyName,
|
|
|
|
int keyNameLength,
|
|
|
|
uint8_t const* param,
|
|
|
|
int paramLength,
|
|
|
|
FDBMutationTypes::Option operationType);
|
|
|
|
|
|
|
|
FDBFuture* (*transactionGetEstimatedRangeSizeBytes)(FDBTransaction* tr,
|
|
|
|
uint8_t const* begin_key_name,
|
|
|
|
int begin_key_name_length,
|
|
|
|
uint8_t const* end_key_name,
|
|
|
|
int end_key_name_length);
|
|
|
|
|
|
|
|
FDBFuture* (*transactionGetRangeSplitPoints)(FDBTransaction* tr,
|
|
|
|
uint8_t const* begin_key_name,
|
|
|
|
int begin_key_name_length,
|
|
|
|
uint8_t const* end_key_name,
|
|
|
|
int end_key_name_length,
|
|
|
|
int64_t chunkSize);
|
|
|
|
|
|
|
|
FDBFuture* (*transactionCommit)(FDBTransaction* tr);
|
|
|
|
fdb_error_t (*transactionGetCommittedVersion)(FDBTransaction* tr, int64_t* outVersion);
|
|
|
|
FDBFuture* (*transactionGetApproximateSize)(FDBTransaction* tr);
|
|
|
|
FDBFuture* (*transactionWatch)(FDBTransaction* tr, uint8_t const* keyName, int keyNameLength);
|
|
|
|
FDBFuture* (*transactionOnError)(FDBTransaction* tr, fdb_error_t error);
|
|
|
|
void (*transactionReset)(FDBTransaction* tr);
|
|
|
|
void (*transactionCancel)(FDBTransaction* tr);
|
|
|
|
|
|
|
|
fdb_error_t (*transactionAddConflictRange)(FDBTransaction* tr,
|
|
|
|
uint8_t const* beginKeyName,
|
|
|
|
int beginKeyNameLength,
|
|
|
|
uint8_t const* endKeyName,
|
|
|
|
int endKeyNameLength,
|
|
|
|
FDBConflictRangeTypes::Option);
|
|
|
|
|
|
|
|
// Future
|
|
|
|
fdb_error_t (*futureGetDatabase)(FDBFuture* f, FDBDatabase** outDb);
|
|
|
|
fdb_error_t (*futureGetInt64)(FDBFuture* f, int64_t* outValue);
|
|
|
|
fdb_error_t (*futureGetUInt64)(FDBFuture* f, uint64_t* outValue);
|
|
|
|
fdb_error_t (*futureGetBool)(FDBFuture* f, bool* outValue);
|
|
|
|
fdb_error_t (*futureGetError)(FDBFuture* f);
|
|
|
|
fdb_error_t (*futureGetKey)(FDBFuture* f, uint8_t const** outKey, int* outKeyLength);
|
|
|
|
fdb_error_t (*futureGetValue)(FDBFuture* f, fdb_bool_t* outPresent, uint8_t const** outValue, int* outValueLength);
|
|
|
|
fdb_error_t (*futureGetStringArray)(FDBFuture* f, const char*** outStrings, int* outCount);
|
2020-06-19 00:41:50 +08:00
|
|
|
fdb_error_t (*futureGetKeyArray)(FDBFuture* f, FDBKey const** outKeys, int* outCount);
|
2021-03-11 02:06:03 +08:00
|
|
|
fdb_error_t (*futureGetKeyValueArray)(FDBFuture* f, FDBKeyValue const** outKV, int* outCount, fdb_bool_t* outMore);
|
|
|
|
fdb_error_t (*futureSetCallback)(FDBFuture* f, FDBCallback callback, void* callback_parameter);
|
|
|
|
void (*futureCancel)(FDBFuture* f);
|
|
|
|
void (*futureDestroy)(FDBFuture* f);
|
|
|
|
|
|
|
|
// Legacy Support
|
|
|
|
FDBFuture* (*createCluster)(const char* clusterFilePath);
|
|
|
|
FDBFuture* (*clusterCreateDatabase)(FDBCluster* cluster, uint8_t* dbName, int dbNameLength);
|
|
|
|
void (*clusterDestroy)(FDBCluster* cluster);
|
|
|
|
fdb_error_t (*futureGetCluster)(FDBFuture* f, FDBCluster** outCluster);
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2021-04-15 03:50:30 +08:00
|
|
|
// An implementation of ITransaction that wraps a transaction object created on an externally loaded client library.
|
|
|
|
// All API calls to that transaction are routed through the external library.
|
2017-05-26 04:48:44 +08:00
|
|
|
class DLTransaction : public ITransaction, ThreadSafeReferenceCounted<DLTransaction> {
|
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
DLTransaction(Reference<FdbCApi> api, FdbCApi::FDBTransaction* tr) : api(api), tr(tr) {}
|
2021-01-26 09:55:43 +08:00
|
|
|
~DLTransaction() override { api->transactionDestroy(tr); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void cancel() override;
|
|
|
|
void setVersion(Version v) override;
|
|
|
|
ThreadFuture<Version> getReadVersion() override;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ThreadFuture<Optional<Value>> get(const KeyRef& key, bool snapshot = false) override;
|
|
|
|
ThreadFuture<Key> getKey(const KeySelectorRef& key, bool snapshot = false) override;
|
2021-05-04 04:14:16 +08:00
|
|
|
ThreadFuture<RangeResult> getRange(const KeySelectorRef& begin,
|
|
|
|
const KeySelectorRef& end,
|
|
|
|
int limit,
|
|
|
|
bool snapshot = false,
|
|
|
|
bool reverse = false) override;
|
|
|
|
ThreadFuture<RangeResult> getRange(const KeySelectorRef& begin,
|
|
|
|
const KeySelectorRef& end,
|
|
|
|
GetRangeLimits limits,
|
|
|
|
bool snapshot = false,
|
|
|
|
bool reverse = false) override;
|
|
|
|
ThreadFuture<RangeResult> getRange(const KeyRangeRef& keys,
|
|
|
|
int limit,
|
|
|
|
bool snapshot = false,
|
|
|
|
bool reverse = false) override;
|
|
|
|
ThreadFuture<RangeResult> getRange(const KeyRangeRef& keys,
|
|
|
|
GetRangeLimits limits,
|
|
|
|
bool snapshot = false,
|
|
|
|
bool reverse = false) override;
|
2019-06-26 07:32:27 +08:00
|
|
|
ThreadFuture<Standalone<VectorRef<const char*>>> getAddressesForKey(const KeyRef& key) override;
|
|
|
|
ThreadFuture<Standalone<StringRef>> getVersionstamp() override;
|
2020-01-17 09:39:23 +08:00
|
|
|
ThreadFuture<int64_t> getEstimatedRangeSizeBytes(const KeyRangeRef& keys) override;
|
2020-06-19 00:41:50 +08:00
|
|
|
ThreadFuture<Standalone<VectorRef<KeyRef>>> getRangeSplitPoints(const KeyRangeRef& range,
|
|
|
|
int64_t chunkSize) override;
|
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void addReadConflictRange(const KeyRangeRef& keys) override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void atomicOp(const KeyRef& key, const ValueRef& value, uint32_t operationType) override;
|
|
|
|
void set(const KeyRef& key, const ValueRef& value) override;
|
|
|
|
void clear(const KeyRef& begin, const KeyRef& end) override;
|
|
|
|
void clear(const KeyRangeRef& range) override;
|
|
|
|
void clear(const KeyRef& key) override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
ThreadFuture<Void> watch(const KeyRef& key) override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void addWriteConflictRange(const KeyRangeRef& keys) override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
ThreadFuture<Void> commit() override;
|
|
|
|
Version getCommittedVersion() override;
|
2019-06-29 01:15:37 +08:00
|
|
|
ThreadFuture<int64_t> getApproximateSize() override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
void setOption(FDBTransactionOptions::Option option, Optional<StringRef> value = Optional<StringRef>()) override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
ThreadFuture<Void> onError(Error const& e) override;
|
|
|
|
void reset() override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void addref() override { ThreadSafeReferenceCounted<DLTransaction>::addref(); }
|
|
|
|
void delref() override { ThreadSafeReferenceCounted<DLTransaction>::delref(); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const Reference<FdbCApi> api;
|
|
|
|
FdbCApi::FDBTransaction* const tr;
|
|
|
|
};
|
|
|
|
|
2021-04-15 03:50:30 +08:00
|
|
|
// An implementation of IDatabase that wraps a database object created on an externally loaded client library.
|
|
|
|
// All API calls to that database are routed through the external library.
|
2017-05-26 04:48:44 +08:00
|
|
|
class DLDatabase : public IDatabase, ThreadSafeReferenceCounted<DLDatabase> {
|
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
DLDatabase(Reference<FdbCApi> api, FdbCApi::FDBDatabase* db) : api(api), db(db), ready(Void()) {}
|
2019-01-15 02:53:03 +08:00
|
|
|
DLDatabase(Reference<FdbCApi> api, ThreadFuture<FdbCApi::FDBDatabase*> dbFuture);
|
2021-01-26 09:55:43 +08:00
|
|
|
~DLDatabase() override {
|
2020-05-29 07:22:45 +08:00
|
|
|
if (db) {
|
|
|
|
api->databaseDestroy(db);
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-01-15 02:53:03 +08:00
|
|
|
ThreadFuture<Void> onReady();
|
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
Reference<ITransaction> createTransaction() override;
|
|
|
|
void setOption(FDBDatabaseOptions::Option option, Optional<StringRef> value = Optional<StringRef>()) override;
|
2021-03-16 07:23:56 +08:00
|
|
|
double getMainThreadBusyness() override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-04-16 02:45:14 +08:00
|
|
|
// Returns the protocol version reported by the coordinator this client is connected to
|
2021-04-15 03:50:30 +08:00
|
|
|
// If an expected version is given, the future won't return until the protocol version is different than expected
|
2021-04-27 05:39:27 +08:00
|
|
|
// Note: this will never return if the server is running a protocol from FDB 5.0 or older
|
2021-04-15 03:50:30 +08:00
|
|
|
ThreadFuture<ProtocolVersion> getServerProtocol(
|
|
|
|
Optional<ProtocolVersion> expectedVersion = Optional<ProtocolVersion>()) override;
|
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void addref() override { ThreadSafeReferenceCounted<DLDatabase>::addref(); }
|
|
|
|
void delref() override { ThreadSafeReferenceCounted<DLDatabase>::delref(); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-01-04 14:45:09 +08:00
|
|
|
ThreadFuture<int64_t> rebootWorker(const StringRef& address, bool check, int duration) override;
|
2021-01-21 17:11:40 +08:00
|
|
|
ThreadFuture<Void> forceRecoveryWithDataLoss(const StringRef& dcid) override;
|
2021-02-09 14:17:16 +08:00
|
|
|
ThreadFuture<Void> createSnapshot(const StringRef& uid, const StringRef& snapshot_command) override;
|
2020-08-22 05:30:52 +08:00
|
|
|
|
2021-02-09 11:11:30 +08:00
|
|
|
private:
|
2017-05-26 04:48:44 +08:00
|
|
|
const Reference<FdbCApi> api;
|
2021-03-11 02:06:03 +08:00
|
|
|
FdbCApi::FDBDatabase*
|
|
|
|
db; // Always set if API version >= 610, otherwise guaranteed to be set when onReady future is set
|
2019-01-15 02:53:03 +08:00
|
|
|
ThreadFuture<Void> ready;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2021-04-15 03:50:30 +08:00
|
|
|
// An implementation of IClientApi that re-issues API calls to the C API of an externally loaded client library.
|
|
|
|
// The DL prefix stands for "dynamic library".
|
2017-05-26 04:48:44 +08:00
|
|
|
class DLApi : public IClientApi {
|
|
|
|
public:
|
2021-02-04 05:45:35 +08:00
|
|
|
DLApi(std::string fdbCPath, bool unlinkOnLoad = false);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void selectApiVersion(int apiVersion) override;
|
|
|
|
const char* getClientVersion() override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void setNetworkOption(FDBNetworkOptions::Option option, Optional<StringRef> value = Optional<StringRef>()) override;
|
|
|
|
void setupNetwork() override;
|
|
|
|
void runNetwork() override;
|
|
|
|
void stopNetwork() override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
Reference<IDatabase> createDatabase(const char* clusterFilePath) override;
|
|
|
|
Reference<IDatabase> createDatabase609(const char* clusterFilePath); // legacy database creation
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
void addNetworkThreadCompletionHook(void (*hook)(void*), void* hookParameter) override;
|
2018-05-09 07:33:43 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
private:
|
|
|
|
const std::string fdbCPath;
|
|
|
|
const Reference<FdbCApi> api;
|
2021-02-04 05:45:35 +08:00
|
|
|
const bool unlinkOnLoad;
|
2017-05-26 04:48:44 +08:00
|
|
|
int headerVersion;
|
|
|
|
bool networkSetup;
|
|
|
|
|
2018-05-10 03:28:51 +08:00
|
|
|
Mutex lock;
|
|
|
|
std::vector<std::pair<void (*)(void*), void*>> threadCompletionHooks;
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
void init();
|
|
|
|
};
|
|
|
|
|
|
|
|
class MultiVersionDatabase;
|
|
|
|
|
2021-04-15 03:50:30 +08:00
|
|
|
// An implementation of ITransaction that wraps a transaction created either locally or through a dynamically loaded
|
|
|
|
// external client. When needed (e.g on cluster version change), the MultiVersionTransaction can automatically replace
|
|
|
|
// its wrapped transaction with one from another client.
|
2017-05-26 04:48:44 +08:00
|
|
|
class MultiVersionTransaction : public ITransaction, ThreadSafeReferenceCounted<MultiVersionTransaction> {
|
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
MultiVersionTransaction(Reference<MultiVersionDatabase> db,
|
|
|
|
UniqueOrderedOptionList<FDBTransactionOptions> defaultOptions);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void cancel() override;
|
|
|
|
void setVersion(Version v) override;
|
|
|
|
ThreadFuture<Version> getReadVersion() override;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ThreadFuture<Optional<Value>> get(const KeyRef& key, bool snapshot = false) override;
|
|
|
|
ThreadFuture<Key> getKey(const KeySelectorRef& key, bool snapshot = false) override;
|
2021-05-04 04:14:16 +08:00
|
|
|
ThreadFuture<RangeResult> getRange(const KeySelectorRef& begin,
|
|
|
|
const KeySelectorRef& end,
|
|
|
|
int limit,
|
|
|
|
bool snapshot = false,
|
|
|
|
bool reverse = false) override;
|
|
|
|
ThreadFuture<RangeResult> getRange(const KeySelectorRef& begin,
|
|
|
|
const KeySelectorRef& end,
|
|
|
|
GetRangeLimits limits,
|
|
|
|
bool snapshot = false,
|
|
|
|
bool reverse = false) override;
|
|
|
|
ThreadFuture<RangeResult> getRange(const KeyRangeRef& keys,
|
|
|
|
int limit,
|
|
|
|
bool snapshot = false,
|
|
|
|
bool reverse = false) override;
|
|
|
|
ThreadFuture<RangeResult> getRange(const KeyRangeRef& keys,
|
|
|
|
GetRangeLimits limits,
|
|
|
|
bool snapshot = false,
|
|
|
|
bool reverse = false) override;
|
2019-06-26 07:32:27 +08:00
|
|
|
ThreadFuture<Standalone<VectorRef<const char*>>> getAddressesForKey(const KeyRef& key) override;
|
|
|
|
ThreadFuture<Standalone<StringRef>> getVersionstamp() override;
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void addReadConflictRange(const KeyRangeRef& keys) override;
|
2020-01-17 09:39:23 +08:00
|
|
|
ThreadFuture<int64_t> getEstimatedRangeSizeBytes(const KeyRangeRef& keys) override;
|
2020-06-19 00:41:50 +08:00
|
|
|
ThreadFuture<Standalone<VectorRef<KeyRef>>> getRangeSplitPoints(const KeyRangeRef& range,
|
|
|
|
int64_t chunkSize) override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void atomicOp(const KeyRef& key, const ValueRef& value, uint32_t operationType) override;
|
|
|
|
void set(const KeyRef& key, const ValueRef& value) override;
|
|
|
|
void clear(const KeyRef& begin, const KeyRef& end) override;
|
|
|
|
void clear(const KeyRangeRef& range) override;
|
|
|
|
void clear(const KeyRef& key) override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
ThreadFuture<Void> watch(const KeyRef& key) override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void addWriteConflictRange(const KeyRangeRef& keys) override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
ThreadFuture<Void> commit() override;
|
|
|
|
Version getCommittedVersion() override;
|
2019-06-29 01:15:37 +08:00
|
|
|
ThreadFuture<int64_t> getApproximateSize() override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
void setOption(FDBTransactionOptions::Option option, Optional<StringRef> value = Optional<StringRef>()) override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
ThreadFuture<Void> onError(Error const& e) override;
|
|
|
|
void reset() override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void addref() override { ThreadSafeReferenceCounted<MultiVersionTransaction>::addref(); }
|
|
|
|
void delref() override { ThreadSafeReferenceCounted<MultiVersionTransaction>::delref(); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const Reference<MultiVersionDatabase> db;
|
|
|
|
ThreadSpinLock lock;
|
|
|
|
|
|
|
|
struct TransactionInfo {
|
|
|
|
Reference<ITransaction> transaction;
|
|
|
|
ThreadFuture<Void> onChange;
|
|
|
|
};
|
|
|
|
|
|
|
|
TransactionInfo transaction;
|
|
|
|
|
|
|
|
TransactionInfo getTransaction();
|
|
|
|
void updateTransaction();
|
2019-06-29 04:24:32 +08:00
|
|
|
void setDefaultOptions(UniqueOrderedOptionList<FDBTransactionOptions> options);
|
|
|
|
|
|
|
|
std::vector<std::pair<FDBTransactionOptions::Option, Optional<Standalone<StringRef>>>> persistentOptions;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2021-02-04 03:58:37 +08:00
|
|
|
struct ClientDesc {
|
|
|
|
std::string const libPath;
|
|
|
|
bool const external;
|
|
|
|
|
2021-02-13 06:42:57 +08:00
|
|
|
ClientDesc(std::string libPath, bool external) : libPath(libPath), external(external) {}
|
2021-02-04 03:58:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ClientInfo : ClientDesc, ThreadSafeReferenceCounted<ClientInfo> {
|
2019-06-19 05:49:04 +08:00
|
|
|
ProtocolVersion protocolVersion;
|
2021-03-11 02:06:03 +08:00
|
|
|
IClientApi* api;
|
2017-05-26 04:48:44 +08:00
|
|
|
bool failed;
|
2017-09-27 08:00:04 +08:00
|
|
|
std::vector<std::pair<void (*)(void*), void*>> threadCompletionHooks;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-02-23 00:47:22 +08:00
|
|
|
ClientInfo() : ClientDesc(std::string(), false), protocolVersion(0), api(nullptr), failed(true) {}
|
2021-02-13 06:42:57 +08:00
|
|
|
ClientInfo(IClientApi* api) : ClientDesc("internal", false), protocolVersion(0), api(api), failed(false) {}
|
|
|
|
ClientInfo(IClientApi* api, std::string libPath)
|
|
|
|
: ClientDesc(libPath, true), protocolVersion(0), api(api), failed(false) {}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
void loadProtocolVersion();
|
|
|
|
bool canReplace(Reference<ClientInfo> other) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MultiVersionApi;
|
|
|
|
|
2021-04-15 03:50:30 +08:00
|
|
|
// An implementation of IDatabase that wraps a database created either locally or through a dynamically loaded
|
|
|
|
// external client. The MultiVersionDatabase monitors the protocol version of the cluster and automatically
|
|
|
|
// replaces the wrapped database when the protocol version changes.
|
2020-10-08 12:58:24 +08:00
|
|
|
class MultiVersionDatabase final : public IDatabase, ThreadSafeReferenceCounted<MultiVersionDatabase> {
|
2017-05-26 04:48:44 +08:00
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
MultiVersionDatabase(MultiVersionApi* api,
|
|
|
|
int threadIdx,
|
|
|
|
std::string clusterFilePath,
|
|
|
|
Reference<IDatabase> db,
|
2021-04-16 02:45:14 +08:00
|
|
|
Reference<IDatabase> versionMonitorDb,
|
2021-02-13 06:42:57 +08:00
|
|
|
bool openConnectors = true);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-04-24 06:00:21 +08:00
|
|
|
~MultiVersionDatabase() override;
|
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
Reference<ITransaction> createTransaction() override;
|
|
|
|
void setOption(FDBDatabaseOptions::Option option, Optional<StringRef> value = Optional<StringRef>()) override;
|
2021-03-16 07:23:56 +08:00
|
|
|
double getMainThreadBusyness() override;
|
2018-09-22 06:58:14 +08:00
|
|
|
|
2021-04-16 02:45:14 +08:00
|
|
|
// Returns the protocol version reported by the coordinator this client is connected to
|
2021-04-15 03:50:30 +08:00
|
|
|
// If an expected version is given, the future won't return until the protocol version is different than expected
|
2021-04-27 05:39:27 +08:00
|
|
|
// Note: this will never return if the server is running a protocol from FDB 5.0 or older
|
2021-04-15 03:50:30 +08:00
|
|
|
ThreadFuture<ProtocolVersion> getServerProtocol(
|
|
|
|
Optional<ProtocolVersion> expectedVersion = Optional<ProtocolVersion>()) override;
|
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void addref() override { ThreadSafeReferenceCounted<MultiVersionDatabase>::addref(); }
|
|
|
|
void delref() override { ThreadSafeReferenceCounted<MultiVersionDatabase>::delref(); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-04-16 02:45:14 +08:00
|
|
|
// Create a MultiVersionDatabase that wraps an already created IDatabase object
|
|
|
|
// For internal use in testing
|
2018-09-22 06:58:14 +08:00
|
|
|
static Reference<IDatabase> debugCreateFromExistingDatabase(Reference<IDatabase> db);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-01-26 09:55:43 +08:00
|
|
|
ThreadFuture<int64_t> rebootWorker(const StringRef& address, bool check, int duration) override;
|
2021-01-21 17:11:40 +08:00
|
|
|
ThreadFuture<Void> forceRecoveryWithDataLoss(const StringRef& dcid) override;
|
2021-02-09 14:17:16 +08:00
|
|
|
ThreadFuture<Void> createSnapshot(const StringRef& uid, const StringRef& snapshot_command) override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-04-16 02:45:14 +08:00
|
|
|
// private:
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-04-24 06:00:21 +08:00
|
|
|
struct LegacyVersionMonitor;
|
|
|
|
|
2021-04-16 02:45:14 +08:00
|
|
|
// A struct that manages the current connection state of the MultiVersionDatabase. This wraps the underlying
|
|
|
|
// IDatabase object that is currently interacting with the cluster.
|
|
|
|
struct DatabaseState : ThreadSafeReferenceCounted<DatabaseState> {
|
|
|
|
DatabaseState(std::string clusterFilePath, Reference<IDatabase> versionMonitorDb);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-04-21 07:21:01 +08:00
|
|
|
// Replaces the active database connection with a new one. Must be called from the main thread.
|
|
|
|
void updateDatabase(Reference<IDatabase> newDb, Reference<ClientInfo> client);
|
|
|
|
|
2021-04-24 06:00:21 +08:00
|
|
|
// Called when a change to the protocol version of the cluster has been detected.
|
|
|
|
// Must be called from the main thread
|
2021-04-16 02:45:14 +08:00
|
|
|
void protocolVersionChanged(ProtocolVersion protocolVersion);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-04-16 02:45:14 +08:00
|
|
|
// Adds a client (local or externally loaded) that can be used to connect to the cluster
|
|
|
|
void addClient(Reference<ClientInfo> client);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-04-24 06:00:21 +08:00
|
|
|
// Watch the cluster protocol version for changes and update the database state when it does.
|
|
|
|
// Must be called from the main thread
|
2021-04-16 02:45:14 +08:00
|
|
|
ThreadFuture<Void> monitorProtocolVersion();
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-04-24 06:00:21 +08:00
|
|
|
// Starts version monitors for old client versions that don't support connect packet monitoring (<= 5.0).
|
|
|
|
// Must be called from the main thread
|
|
|
|
void startLegacyVersionMonitors();
|
|
|
|
|
|
|
|
// Cleans up state for the legacy version monitors to break reference cycles
|
|
|
|
void close();
|
|
|
|
|
2018-09-22 06:58:14 +08:00
|
|
|
Reference<IDatabase> db;
|
|
|
|
const Reference<ThreadSafeAsyncVar<Reference<IDatabase>>> dbVar;
|
2021-04-16 02:45:14 +08:00
|
|
|
std::string clusterFilePath;
|
|
|
|
|
|
|
|
// Used to monitor the cluster protocol version. Will be the same as db unless we have either not connected
|
2021-04-24 06:00:21 +08:00
|
|
|
// yet or if the client version associated with db does not support protocol monitoring. In those cases,
|
|
|
|
// this will be a specially created local db.
|
2021-04-16 02:45:14 +08:00
|
|
|
Reference<IDatabase> versionMonitorDb;
|
2018-09-22 06:58:14 +08:00
|
|
|
|
|
|
|
ThreadFuture<Void> changed;
|
|
|
|
|
|
|
|
bool cancelled;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-04-21 07:21:01 +08:00
|
|
|
ThreadFuture<Void> dbReady;
|
2021-04-16 02:45:14 +08:00
|
|
|
ThreadFuture<Void> protocolVersionMonitor;
|
2021-04-28 06:00:56 +08:00
|
|
|
|
|
|
|
// Versions older than 6.1 do not benefit from having their database connections closed. Additionally,
|
|
|
|
// there are various issues that result in negative behavior in some cases if the connections are closed.
|
|
|
|
// Therefore, we leave them open.
|
|
|
|
std::map<ProtocolVersion, Reference<IDatabase>> legacyDatabaseConnections;
|
|
|
|
|
|
|
|
// Versions 5.0 and older do not support connection packet monitoring and require alternate techniques to
|
|
|
|
// determine the cluster version.
|
2021-05-01 02:11:02 +08:00
|
|
|
std::list<Reference<LegacyVersionMonitor>> legacyVersionMonitors;
|
2021-04-28 06:00:56 +08:00
|
|
|
|
2021-04-16 02:45:14 +08:00
|
|
|
Optional<ProtocolVersion> dbProtocolVersion;
|
2021-04-27 05:39:27 +08:00
|
|
|
|
|
|
|
// This maps a normalized protocol version to the client associated with it. This prevents compatible
|
|
|
|
// differences in protocol version not matching each other.
|
2021-04-16 02:45:14 +08:00
|
|
|
std::map<ProtocolVersion, Reference<ClientInfo>> clients;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2018-09-22 06:58:14 +08:00
|
|
|
std::vector<std::pair<FDBDatabaseOptions::Option, Optional<Standalone<StringRef>>>> options;
|
2019-06-29 04:24:32 +08:00
|
|
|
UniqueOrderedOptionList<FDBTransactionOptions> transactionDefaultOptions;
|
2017-05-26 04:48:44 +08:00
|
|
|
Mutex optionLock;
|
|
|
|
};
|
|
|
|
|
2021-04-24 06:00:21 +08:00
|
|
|
// A struct that enables monitoring whether the cluster is running an old version (<= 5.0) that doesn't support
|
|
|
|
// connect packet monitoring.
|
2021-05-01 02:11:02 +08:00
|
|
|
struct LegacyVersionMonitor : ThreadSafeReferenceCounted<LegacyVersionMonitor> {
|
2021-04-27 05:39:27 +08:00
|
|
|
LegacyVersionMonitor(Reference<ClientInfo> const& client) : client(client), monitorRunning(false) {}
|
2021-05-01 02:11:02 +08:00
|
|
|
|
|
|
|
// Terminates the version monitor to break reference cycles
|
|
|
|
void close();
|
2021-04-24 06:00:21 +08:00
|
|
|
|
|
|
|
// Starts the connection monitor by creating a database object at an old version.
|
|
|
|
// Must be called from the main thread
|
|
|
|
void startConnectionMonitor(Reference<DatabaseState> dbState);
|
|
|
|
|
|
|
|
// Runs a GRV probe on the cluster to determine if the client version is compatible with the cluster.
|
|
|
|
// Must be called from main thread
|
|
|
|
void runGrvProbe(Reference<DatabaseState> dbState);
|
|
|
|
|
|
|
|
Reference<ClientInfo> client;
|
|
|
|
Reference<IDatabase> db;
|
|
|
|
Reference<ITransaction> tr;
|
|
|
|
|
|
|
|
ThreadFuture<Void> versionMonitor;
|
|
|
|
bool monitorRunning;
|
|
|
|
};
|
|
|
|
|
2018-09-22 06:58:14 +08:00
|
|
|
const Reference<DatabaseState> dbState;
|
|
|
|
friend class MultiVersionTransaction;
|
2021-04-16 02:45:14 +08:00
|
|
|
|
|
|
|
// Clients must create a database object in order to initialize some of their state.
|
|
|
|
// This needs to be done only once, and this flag tracks whether that has happened.
|
|
|
|
static std::atomic_flag externalClientsInitialized;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2021-04-15 03:50:30 +08:00
|
|
|
// An implementation of IClientApi that can choose between multiple different client implementations either provided
|
|
|
|
// locally within the primary loaded fdb_c client or through any number of dynamically loaded clients.
|
|
|
|
//
|
|
|
|
// This functionality is used to provide support for multiple protocol versions simultaneously.
|
2017-05-26 04:48:44 +08:00
|
|
|
class MultiVersionApi : public IClientApi {
|
|
|
|
public:
|
2019-06-26 07:32:27 +08:00
|
|
|
void selectApiVersion(int apiVersion) override;
|
|
|
|
const char* getClientVersion() override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-26 07:32:27 +08:00
|
|
|
void setNetworkOption(FDBNetworkOptions::Option option, Optional<StringRef> value = Optional<StringRef>()) override;
|
|
|
|
void setupNetwork() override;
|
|
|
|
void runNetwork() override;
|
|
|
|
void stopNetwork() override;
|
2021-03-11 02:06:03 +08:00
|
|
|
void addNetworkThreadCompletionHook(void (*hook)(void*), void* hookParameter) override;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-04-22 03:25:03 +08:00
|
|
|
// Creates an IDatabase object that represents a connection to the cluster
|
2021-03-11 02:06:03 +08:00
|
|
|
Reference<IDatabase> createDatabase(const char* clusterFilePath) override;
|
2017-05-26 04:48:44 +08:00
|
|
|
static MultiVersionApi* api;
|
|
|
|
|
|
|
|
Reference<ClientInfo> getLocalClient();
|
2021-03-11 02:06:03 +08:00
|
|
|
void runOnExternalClients(int threadId,
|
|
|
|
std::function<void(Reference<ClientInfo>)>,
|
2021-02-13 06:42:57 +08:00
|
|
|
bool runOnFailedClients = false);
|
|
|
|
void runOnExternalClientsAllThreads(std::function<void(Reference<ClientInfo>)>, bool runOnFailedClients = false);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
void updateSupportedVersions();
|
|
|
|
|
|
|
|
bool callbackOnMainThread;
|
|
|
|
bool localClientDisabled;
|
|
|
|
|
2019-06-29 04:24:32 +08:00
|
|
|
static bool apiVersionAtLeast(int minVersion);
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
private:
|
|
|
|
MultiVersionApi();
|
|
|
|
|
|
|
|
void loadEnvironmentVariableNetworkOptions();
|
|
|
|
|
|
|
|
void disableMultiVersionClientApi();
|
|
|
|
void setCallbacksOnExternalThreads();
|
|
|
|
void addExternalLibrary(std::string path);
|
|
|
|
void addExternalLibraryDirectory(std::string path);
|
2021-03-11 02:06:03 +08:00
|
|
|
// Return a vector of (pathname, unlink_on_close) pairs. Makes threadCount - 1 copies of the library stored in
|
|
|
|
// path, and returns a vector of length threadCount.
|
2021-02-13 07:22:58 +08:00
|
|
|
std::vector<std::pair<std::string, bool>> copyExternalLibraryPerThread(std::string path);
|
2017-05-26 04:48:44 +08:00
|
|
|
void disableLocalClient();
|
|
|
|
void setSupportedClientVersions(Standalone<StringRef> versions);
|
|
|
|
|
|
|
|
void setNetworkOptionInternal(FDBNetworkOptions::Option option, Optional<StringRef> value);
|
|
|
|
|
|
|
|
Reference<ClientInfo> localClient;
|
2021-02-04 03:58:37 +08:00
|
|
|
std::map<std::string, ClientDesc> externalClientDescriptions;
|
|
|
|
std::map<std::string, std::vector<Reference<ClientInfo>>> externalClients;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
bool networkStartSetup;
|
|
|
|
volatile bool networkSetup;
|
|
|
|
volatile bool bypassMultiClientApi;
|
|
|
|
volatile bool externalClient;
|
|
|
|
int apiVersion;
|
|
|
|
|
2021-02-10 04:56:34 +08:00
|
|
|
int nextThread = 0;
|
|
|
|
int threadCount;
|
2021-02-04 03:58:37 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
Mutex lock;
|
|
|
|
std::vector<std::pair<FDBNetworkOptions::Option, Optional<Standalone<StringRef>>>> options;
|
|
|
|
std::map<FDBNetworkOptions::Option, std::set<Standalone<StringRef>>> setEnvOptions;
|
|
|
|
volatile bool envOptionsLoaded;
|
|
|
|
};
|
|
|
|
|
2017-07-26 06:57:26 +08:00
|
|
|
#endif
|