2021-10-20 04:56:52 +08:00
|
|
|
/*
|
|
|
|
* ChangeFeedCommand.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
2022-03-22 04:36:23 +08:00
|
|
|
* Copyright 2013-2022 Apple Inc. and the FoundationDB project authors
|
2021-10-20 04:56:52 +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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-06-24 04:37:35 +08:00
|
|
|
#include "fmt/format.h"
|
2021-11-18 09:03:32 +08:00
|
|
|
|
2021-10-20 04:56:52 +08:00
|
|
|
#include "fdbcli/fdbcli.actor.h"
|
|
|
|
|
|
|
|
#include "fdbclient/FDBOptions.g.h"
|
|
|
|
#include "fdbclient/IClientApi.h"
|
|
|
|
#include "fdbclient/Knobs.h"
|
|
|
|
#include "fdbclient/Schemas.h"
|
|
|
|
#include "fdbclient/ManagementAPI.actor.h"
|
|
|
|
|
|
|
|
#include "flow/Arena.h"
|
|
|
|
#include "flow/FastRef.h"
|
|
|
|
#include "flow/ThreadHelper.actor.h"
|
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
ACTOR Future<Void> changeFeedList(Database db) {
|
|
|
|
state ReadYourWritesTransaction tr(db);
|
|
|
|
loop {
|
|
|
|
try {
|
|
|
|
tr.setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS);
|
|
|
|
tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE);
|
|
|
|
|
|
|
|
RangeResult result = wait(tr.getRange(changeFeedKeys, CLIENT_KNOBS->TOO_MANY));
|
|
|
|
// shouldn't have many quarantined TSSes
|
|
|
|
ASSERT(!result.more);
|
|
|
|
printf("Found %d range feeds%s\n", result.size(), result.size() == 0 ? "." : ":");
|
|
|
|
for (auto& it : result) {
|
|
|
|
auto range = std::get<0>(decodeChangeFeedValue(it.value));
|
2022-01-27 06:41:47 +08:00
|
|
|
printf(" %s: `%s' - `%s'\n",
|
2021-10-20 04:56:52 +08:00
|
|
|
it.key.removePrefix(changeFeedPrefix).toString().c_str(),
|
2022-01-27 06:41:47 +08:00
|
|
|
printable(range.begin).c_str(),
|
|
|
|
printable(range.end).c_str());
|
2021-10-20 04:56:52 +08:00
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
} catch (Error& e) {
|
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace fdb_cli {
|
|
|
|
|
2021-11-15 11:08:46 +08:00
|
|
|
ACTOR Future<Void> requestVersionUpdate(Database localDb, Reference<ChangeFeedData> feedData) {
|
|
|
|
loop {
|
|
|
|
wait(delay(5.0));
|
|
|
|
Transaction tr(localDb);
|
|
|
|
state Version ver = wait(tr.getReadVersion());
|
2021-11-18 06:45:48 +08:00
|
|
|
fmt::print("Requesting version {}\n", ver);
|
2021-11-15 11:08:46 +08:00
|
|
|
wait(feedData->whenAtLeast(ver));
|
2021-11-18 06:45:48 +08:00
|
|
|
fmt::print("Feed at version {}\n", ver);
|
2021-11-15 11:08:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 07:25:51 +08:00
|
|
|
ACTOR Future<bool> changeFeedCommandActor(Database localDb,
|
|
|
|
Optional<TenantMapEntry> tenantEntry,
|
|
|
|
std::vector<StringRef> tokens,
|
|
|
|
Future<Void> warn) {
|
2021-10-20 04:56:52 +08:00
|
|
|
if (tokens.size() == 1) {
|
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (tokencmp(tokens[1], "list")) {
|
|
|
|
if (tokens.size() != 2) {
|
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wait(changeFeedList(localDb));
|
|
|
|
return true;
|
|
|
|
} else if (tokencmp(tokens[1], "register")) {
|
|
|
|
if (tokens.size() != 5) {
|
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-20 07:25:51 +08:00
|
|
|
|
|
|
|
KeyRange range;
|
|
|
|
if (tenantEntry.present()) {
|
|
|
|
range = KeyRangeRef(tokens[3], tokens[4]).withPrefix(tenantEntry.get().prefix);
|
|
|
|
} else {
|
|
|
|
range = KeyRangeRef(tokens[3], tokens[4]);
|
|
|
|
}
|
|
|
|
|
|
|
|
wait(updateChangeFeed(localDb, tokens[2], ChangeFeedStatus::CHANGE_FEED_CREATE, range));
|
2021-10-20 04:56:52 +08:00
|
|
|
} else if (tokencmp(tokens[1], "stop")) {
|
|
|
|
if (tokens.size() != 3) {
|
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wait(updateChangeFeed(localDb, tokens[2], ChangeFeedStatus::CHANGE_FEED_STOP));
|
|
|
|
} else if (tokencmp(tokens[1], "destroy")) {
|
|
|
|
if (tokens.size() != 3) {
|
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wait(updateChangeFeed(localDb, tokens[2], ChangeFeedStatus::CHANGE_FEED_DESTROY));
|
|
|
|
} else if (tokencmp(tokens[1], "stream")) {
|
|
|
|
if (tokens.size() < 3 || tokens.size() > 5) {
|
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Version begin = 0;
|
|
|
|
Version end = std::numeric_limits<Version>::max();
|
|
|
|
if (tokens.size() > 3) {
|
|
|
|
int n = 0;
|
2022-03-01 10:19:52 +08:00
|
|
|
if (sscanf(tokens[3].toString().c_str(), "%" PRId64 "%n", &begin, &n) != 1 || n != tokens[3].size()) {
|
2021-10-20 04:56:52 +08:00
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tokens.size() > 4) {
|
|
|
|
int n = 0;
|
2021-11-18 06:45:48 +08:00
|
|
|
if (sscanf(tokens[4].toString().c_str(), "%" PRId64 "%n", &end, &n) != 1 || n != tokens[4].size()) {
|
2021-10-20 04:56:52 +08:00
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (warn.isValid()) {
|
|
|
|
warn.cancel();
|
|
|
|
}
|
2021-11-15 11:08:46 +08:00
|
|
|
state Reference<ChangeFeedData> feedData = makeReference<ChangeFeedData>();
|
|
|
|
state Future<Void> feed = localDb->getChangeFeedStream(feedData, tokens[2], begin, end);
|
|
|
|
state Future<Void> versionUpdates = requestVersionUpdate(localDb, feedData);
|
2021-10-20 04:56:52 +08:00
|
|
|
printf("\n");
|
|
|
|
try {
|
|
|
|
state Future<Void> feedInterrupt = LineNoise::onKeyboardInterrupt();
|
|
|
|
loop {
|
|
|
|
choose {
|
2021-11-15 11:08:46 +08:00
|
|
|
when(Standalone<VectorRef<MutationsAndVersionRef>> res =
|
|
|
|
waitNext(feedData->mutations.getFuture())) {
|
2021-10-20 04:56:52 +08:00
|
|
|
for (auto& it : res) {
|
|
|
|
for (auto& it2 : it.mutations) {
|
2021-11-18 09:03:32 +08:00
|
|
|
fmt::print("{0} {1}\n", it.version, it2.toString());
|
2021-10-20 04:56:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
when(wait(feedInterrupt)) {
|
|
|
|
feedInterrupt = Future<Void>();
|
|
|
|
feed.cancel();
|
2021-11-15 11:08:46 +08:00
|
|
|
feedData = makeReference<ChangeFeedData>();
|
2021-10-20 04:56:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (Error& e) {
|
|
|
|
if (e.code() == error_code_end_of_stream) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
} else if (tokencmp(tokens[1], "pop")) {
|
|
|
|
if (tokens.size() != 4) {
|
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Version v;
|
|
|
|
int n = 0;
|
2022-03-01 10:19:52 +08:00
|
|
|
if (sscanf(tokens[3].toString().c_str(), "%" PRId64 "%n", &v, &n) != 1 || n != tokens[3].size()) {
|
2021-10-20 04:56:52 +08:00
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
wait(localDb->popChangeFeedMutations(tokens[2], v));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandFactory changeFeedFactory(
|
|
|
|
"changefeed",
|
|
|
|
CommandHelp("changefeed <register|destroy|stop|stream|pop|list> <RANGEID> <BEGIN> <END>", "", ""));
|
|
|
|
} // namespace fdb_cli
|