foundationdb/fdbserver/workloads/ApiWorkload.h

355 lines
14 KiB
C
Raw Normal View History

2017-05-26 04:48:44 +08:00
/*
* ApiWorkload.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 FDBSERVER_APIWORKLOAD_H
#define FDBSERVER_APIWORKLOAD_H
#pragma once
#include "fdbserver/workloads/workloads.actor.h"
#include "fdbclient/ClusterConnectionMemoryRecord.h"
2017-05-26 04:48:44 +08:00
#include "fdbclient/ReadYourWrites.h"
#include "fdbclient/ThreadSafeTransaction.h"
#include "fdbserver/workloads/MemoryKeyValueStore.h"
#include "flow/actorcompiler.h"
2017-05-26 04:48:44 +08:00
// an enumeration of apis being tested
enum TransactionType { NATIVE, READ_YOUR_WRITES, THREAD_SAFE, MULTI_VERSION };
2017-05-26 04:48:44 +08:00
// A wrapper interface for dealing with different Transaction implementations
2017-05-26 04:48:44 +08:00
struct TransactionWrapper : public ReferenceCounted<TransactionWrapper> {
virtual ~TransactionWrapper() {}
2017-05-26 04:48:44 +08:00
// Sets a key-value pair in the database
virtual void set(KeyRef& key, ValueRef& value) = 0;
2017-05-26 04:48:44 +08:00
// Commits modifications to the database
2017-05-26 04:48:44 +08:00
virtual Future<Void> commit() = 0;
// Gets a value associated with a given key from the database
virtual Future<Optional<Value>> get(KeyRef& key) = 0;
2017-05-26 04:48:44 +08:00
// Gets a range of key-value pairs from the database specified by a key range
2021-07-03 12:41:50 +08:00
virtual Future<RangeResult> getRange(KeyRangeRef& keys, int limit, Reverse reverse) = 0;
2017-05-26 04:48:44 +08:00
// Gets a range of key-value pairs from the database specified by a pair of key selectors
2021-07-03 12:41:50 +08:00
virtual Future<RangeResult> getRange(KeySelectorRef& begin, KeySelectorRef& end, int limit, Reverse reverse) = 0;
2017-05-26 04:48:44 +08:00
// Gets the key from the database specified by a given key selector
virtual Future<Key> getKey(KeySelectorRef& key) = 0;
2017-05-26 04:48:44 +08:00
// Clears a key from the database
virtual void clear(KeyRef& key) = 0;
2017-05-26 04:48:44 +08:00
// Clears a range of keys from the database
virtual void clear(KeyRangeRef& range) = 0;
2017-05-26 04:48:44 +08:00
// Processes transaction error conditions
2017-05-26 04:48:44 +08:00
virtual Future<Void> onError(Error const& e) = 0;
// Gets the read version of a transaction
2017-05-26 04:48:44 +08:00
virtual Future<Version> getReadVersion() = 0;
// Gets the committed version of a transaction
2017-05-26 04:48:44 +08:00
virtual Version getCommittedVersion() = 0;
// Prints debugging messages for a transaction; not implemented for all transaction types
2017-05-26 04:48:44 +08:00
virtual void debugTransaction(UID debugId) {}
virtual void addReadConflictRange(KeyRangeRef const& keys) = 0;
2017-05-26 04:48:44 +08:00
};
// A wrapper class for flow based transactions (NativeAPI, ReadYourWrites)
template <class T>
2017-05-26 04:48:44 +08:00
struct FlowTransactionWrapper : public TransactionWrapper {
Database cx;
Database extraDB;
bool useExtraDB;
2017-05-26 04:48:44 +08:00
T transaction;
T lastTransaction;
FlowTransactionWrapper(Database cx, Database extraDB, bool useExtraDB)
: cx(cx), extraDB(extraDB), useExtraDB(useExtraDB), transaction(cx) {
if (useExtraDB && deterministicRandom()->random01() < 0.5) {
transaction = T(extraDB);
}
}
~FlowTransactionWrapper() override {}
2017-05-26 04:48:44 +08:00
// Sets a key-value pair in the database
void set(KeyRef& key, ValueRef& value) override { transaction.set(key, value); }
2017-05-26 04:48:44 +08:00
// Commits modifications to the database
Future<Void> commit() override { return transaction.commit(); }
2017-05-26 04:48:44 +08:00
// Gets a value associated with a given key from the database
Future<Optional<Value>> get(KeyRef& key) override { return transaction.get(key); }
2017-05-26 04:48:44 +08:00
// Gets a range of key-value pairs from the database specified by a key range
2021-07-03 12:41:50 +08:00
Future<RangeResult> getRange(KeyRangeRef& keys, int limit, Reverse reverse) override {
return transaction.getRange(keys, limit, Snapshot::False, reverse);
2017-05-26 04:48:44 +08:00
}
// Gets a range of key-value pairs from the database specified by a pair of key selectors
2021-07-03 12:41:50 +08:00
Future<RangeResult> getRange(KeySelectorRef& begin, KeySelectorRef& end, int limit, Reverse reverse) override {
return transaction.getRange(begin, end, limit, Snapshot::False, reverse);
2017-05-26 04:48:44 +08:00
}
// Gets the key from the database specified by a given key selector
Future<Key> getKey(KeySelectorRef& key) override { return transaction.getKey(key); }
2017-05-26 04:48:44 +08:00
// Clears a key from the database
void clear(KeyRef& key) override { transaction.clear(key); }
2017-05-26 04:48:44 +08:00
// Clears a range of keys from the database
void clear(KeyRangeRef& range) override { transaction.clear(range); }
2017-05-26 04:48:44 +08:00
// Processes transaction error conditions
Future<Void> onError(Error const& e) override {
Future<Void> returnVal = transaction.onError(e);
if (useExtraDB) {
lastTransaction = std::move(transaction);
transaction = T(deterministicRandom()->random01() < 0.5 ? extraDB : cx);
}
return returnVal;
2017-05-26 04:48:44 +08:00
}
// Gets the read version of a transaction
Future<Version> getReadVersion() override { return transaction.getReadVersion(); }
2017-05-26 04:48:44 +08:00
// Gets the committed version of a transaction
Version getCommittedVersion() override { return transaction.getCommittedVersion(); }
2017-05-26 04:48:44 +08:00
// Prints debugging messages for a transaction
void debugTransaction(UID debugId) override { transaction.debugTransaction(debugId); }
2017-05-26 04:48:44 +08:00
void addReadConflictRange(KeyRangeRef const& keys) override { transaction.addReadConflictRange(keys); }
2017-05-26 04:48:44 +08:00
};
// A wrapper class for ThreadSafeTransactions. Converts ThreadFutures into Futures for interchangeability with flow
// transactions
2017-05-26 04:48:44 +08:00
struct ThreadTransactionWrapper : public TransactionWrapper {
Reference<ITransaction> transaction;
ThreadTransactionWrapper(Reference<IDatabase> db, Reference<IDatabase> extraDB, bool useExtraDB)
: transaction(db->createTransaction()) {}
~ThreadTransactionWrapper() override {}
2017-05-26 04:48:44 +08:00
// Sets a key-value pair in the database
void set(KeyRef& key, ValueRef& value) override { transaction->set(key, value); }
2017-05-26 04:48:44 +08:00
// Commits modifications to the database
Future<Void> commit() override { return unsafeThreadFutureToFuture(transaction->commit()); }
2017-05-26 04:48:44 +08:00
// Gets a value associated with a given key from the database
Future<Optional<Value>> get(KeyRef& key) override { return unsafeThreadFutureToFuture(transaction->get(key)); }
2017-05-26 04:48:44 +08:00
// Gets a range of key-value pairs from the database specified by a key range
2021-07-03 12:41:50 +08:00
Future<RangeResult> getRange(KeyRangeRef& keys, int limit, Reverse reverse) override {
return unsafeThreadFutureToFuture(transaction->getRange(keys, limit, Snapshot::False, reverse));
2017-05-26 04:48:44 +08:00
}
// Gets a range of key-value pairs from the database specified by a pair of key selectors
2021-07-03 12:41:50 +08:00
Future<RangeResult> getRange(KeySelectorRef& begin, KeySelectorRef& end, int limit, Reverse reverse) override {
return unsafeThreadFutureToFuture(transaction->getRange(begin, end, limit, Snapshot::False, reverse));
2017-05-26 04:48:44 +08:00
}
// Gets the key from the database specified by a given key selector
Future<Key> getKey(KeySelectorRef& key) override { return unsafeThreadFutureToFuture(transaction->getKey(key)); }
2017-05-26 04:48:44 +08:00
// Clears a key from the database
void clear(KeyRef& key) override { transaction->clear(key); }
2017-05-26 04:48:44 +08:00
// Clears a range of keys from the database
void clear(KeyRangeRef& range) override { transaction->clear(range); }
2017-05-26 04:48:44 +08:00
// Processes transaction error conditions
Future<Void> onError(Error const& e) override { return unsafeThreadFutureToFuture(transaction->onError(e)); }
2017-05-26 04:48:44 +08:00
// Gets the read version of a transaction
Future<Version> getReadVersion() override { return unsafeThreadFutureToFuture(transaction->getReadVersion()); }
2017-05-26 04:48:44 +08:00
// Gets the committed version of a transaction
Version getCommittedVersion() override { return transaction->getCommittedVersion(); }
2017-05-26 04:48:44 +08:00
void addReadConflictRange(KeyRangeRef const& keys) override { transaction->addReadConflictRange(keys); }
2017-05-26 04:48:44 +08:00
};
// A factory interface for creating different kinds of TransactionWrappers
2017-05-26 04:48:44 +08:00
struct TransactionFactoryInterface : public ReferenceCounted<TransactionFactoryInterface> {
virtual ~TransactionFactoryInterface() {}
2017-05-26 04:48:44 +08:00
// Creates a new transaction
2017-05-26 04:48:44 +08:00
virtual Reference<TransactionWrapper> createTransaction() = 0;
};
// Templated implementation of TransactionFactoryInterface which creates a specific type of TransactionWrapper
template <class T, class DB>
2017-05-26 04:48:44 +08:00
struct TransactionFactory : public TransactionFactoryInterface {
// The database used to create transaction (of type Database, Reference<ThreadSafeDatabase>, etc.)
2017-05-26 04:48:44 +08:00
DB dbHandle;
DB extraDbHandle;
bool useExtraDB;
2017-05-26 04:48:44 +08:00
TransactionFactory(DB dbHandle, DB extraDbHandle, bool useExtraDB)
: dbHandle(dbHandle), extraDbHandle(extraDbHandle), useExtraDB(useExtraDB) {}
~TransactionFactory() override {}
2017-05-26 04:48:44 +08:00
// Creates a new transaction
Reference<TransactionWrapper> createTransaction() override {
return Reference<TransactionWrapper>(new T(dbHandle, extraDbHandle, useExtraDB));
2017-05-26 04:48:44 +08:00
}
};
struct ApiWorkload : TestWorkload {
bool useExtraDB;
Database extraDB;
2017-05-26 04:48:44 +08:00
ApiWorkload(WorkloadContext const& wcx, int maxClients = -1)
2021-07-23 13:48:27 +08:00
: TestWorkload(wcx), maxClients(maxClients), success(true), transactionFactory(nullptr) {
2017-05-26 04:48:44 +08:00
clientPrefixInt = getOption(options, LiteralStringRef("clientId"), clientId);
clientPrefix = format("%010d", clientPrefixInt);
numKeys = getOption(options, LiteralStringRef("numKeys"), 5000);
onlyLowerCase = getOption(options, LiteralStringRef("onlyLowerCase"), false);
shortKeysRatio = getOption(options, LiteralStringRef("shortKeysRatio"), 0.5);
minShortKeyLength = getOption(options, LiteralStringRef("minShortKeyLength"), 1);
maxShortKeyLength = getOption(options, LiteralStringRef("maxShortKeyLength"), 3);
minLongKeyLength = getOption(options, LiteralStringRef("minLongKeyLength"), 1);
maxLongKeyLength = getOption(options, LiteralStringRef("maxLongKeyLength"), 128);
minValueLength = getOption(options, LiteralStringRef("minValueLength"), 1);
maxValueLength = getOption(options, LiteralStringRef("maxValueLength"), 10000);
2020-08-19 05:18:50 +08:00
useExtraDB = g_simulator.extraDB != nullptr;
if (useExtraDB) {
auto extraFile = makeReference<ClusterConnectionMemoryRecord>(*g_simulator.extraDB);
extraDB = Database::createDatabase(extraFile, -1);
}
2017-05-26 04:48:44 +08:00
}
2020-10-08 02:46:23 +08:00
Future<Void> setup(Database const& cx) override;
Future<Void> start(Database const& cx) override;
Future<bool> check(Database const& cx) override;
2017-05-26 04:48:44 +08:00
// Compares the contents of this client's key-space in the database with the in-memory key-value store
2017-05-26 04:48:44 +08:00
Future<bool> compareDatabaseToMemory();
// Verifies that the results of a getRange are the same in the database and in memory
2017-05-26 04:48:44 +08:00
bool compareResults(VectorRef<KeyValueRef> dbResults, VectorRef<KeyValueRef> storeResults, Version readVersion);
// Generates a set of random key-value pairs with an optional prefix
Standalone<VectorRef<KeyValueRef>> generateData(int numKeys,
int minKeyLength,
int maxKeyLength,
int minValueLength,
int maxValueLength,
std::string prefix = "",
bool allowDuplicates = true);
2017-05-26 04:48:44 +08:00
// Generates a random key
2017-05-26 04:48:44 +08:00
Key generateKey(VectorRef<KeyValueRef> const& data, int minKeyLength, int maxKeyLength, std::string prefix = "");
// Generates a random key selector with a specified maximum offset
2017-05-26 04:48:44 +08:00
KeySelector generateKeySelector(VectorRef<KeyValueRef> const& data, int maxOffset);
// Selects a random key. There is a <probabilityKeyExists> probability that the key will be chosen from the keyset
// in data, otherwise the key will be a randomly generated key
2017-05-26 04:48:44 +08:00
Key selectRandomKey(VectorRef<KeyValueRef> const& data, double probabilityKeyExists);
// Generates a random value
2017-05-26 04:48:44 +08:00
Value generateValue(int minValueLength, int maxValueLength);
// Generates a random value
2017-05-26 04:48:44 +08:00
Value generateValue();
// Convenience function for reporting a test failure to trace log and stdout
2017-05-26 04:48:44 +08:00
void testFailure(std::string reason);
// Creates a random transaction factory to produce transaction of one of the TransactionType choices
2017-05-26 04:48:44 +08:00
Future<Void> chooseTransactionFactory(Database const& cx, std::vector<TransactionType> const& choices);
// Creates a new transaction using the current transaction factory
2017-05-26 04:48:44 +08:00
Reference<TransactionWrapper> createTransaction();
// Implemented by subclasses; called during the setup function to prepare the database
2017-05-26 04:48:44 +08:00
virtual Future<Void> performSetup(Database const& cx) = 0;
// Implemented by subclasses; called during the start function to run the tests
2017-05-26 04:48:44 +08:00
virtual Future<Void> performTest(Database const& cx, Standalone<VectorRef<KeyValueRef>> const& data) = 0;
// Returns whether or not success is false
2017-05-26 04:48:44 +08:00
bool hasFailed();
// Clears the keyspace used by this test
2017-05-26 04:48:44 +08:00
Future<Void> clearKeyspace();
// The maximum number of tester clients that will run the test
2017-05-26 04:48:44 +08:00
int maxClients;
// A key prefix used by this client. This is so each client can operate on a key space without worrying about
// the operations of other clients. Otherwise, it would be challenging to maintain an in-memory representation
// of what the database should contain
2017-05-26 04:48:44 +08:00
std::string clientPrefix;
int clientPrefixInt;
// Whether or not the test passed
2017-05-26 04:48:44 +08:00
bool success;
// How many keys each client should generate to put in the database. This may not be exact, as some keys may be
// duplicates of each other
2017-05-26 04:48:44 +08:00
int numKeys;
// The ratio of keys which should have small length (to encourage collisions)
2017-05-26 04:48:44 +08:00
double shortKeysRatio;
// The minimum length of a short key
2017-05-26 04:48:44 +08:00
int minShortKeyLength;
// The maximum length of a short key
2017-05-26 04:48:44 +08:00
int maxShortKeyLength;
// The minimum length of a long key
2017-05-26 04:48:44 +08:00
int minLongKeyLength;
// The maximum length of a long key
2017-05-26 04:48:44 +08:00
int maxLongKeyLength;
// The minimum length of a value
2017-05-26 04:48:44 +08:00
int minValueLength;
// The maximum length of a value
2017-05-26 04:48:44 +08:00
int maxValueLength;
// If true, then random keys will only contain lower case letters. Otherwise, they will contain all character
// values
2017-05-26 04:48:44 +08:00
bool onlyLowerCase;
// The in-memory representation of this client's key space
2017-05-26 04:48:44 +08:00
MemoryKeyValueStore store;
// The transaction factory used to create transactions in this run
2017-05-26 04:48:44 +08:00
Reference<TransactionFactoryInterface> transactionFactory;
};
#include "flow/unactorcompiler.h"
2017-05-26 04:48:44 +08:00
#endif