2021-10-11 11:44:56 +08:00
|
|
|
/*
|
|
|
|
* ClusterConnectionKey.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-11 11:44:56 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fdbclient/ClusterConnectionKey.actor.h"
|
|
|
|
#include "flow/actorcompiler.h" // has to be last include
|
|
|
|
|
|
|
|
// Creates a cluster connection record with a given connection string and saves it to the specified key. Needs to be
|
|
|
|
// persisted should be set to true unless this ClusterConnectionKey is being created with the value read from the
|
|
|
|
// key.
|
|
|
|
ClusterConnectionKey::ClusterConnectionKey(Database db,
|
|
|
|
Key connectionStringKey,
|
|
|
|
ClusterConnectionString const& contents,
|
2021-10-12 05:39:05 +08:00
|
|
|
ConnectionStringNeedsPersisted needsToBePersisted)
|
2021-10-26 07:59:54 +08:00
|
|
|
: IClusterConnectionRecord(needsToBePersisted), db(db), connectionStringKey(connectionStringKey) {
|
2021-10-22 04:44:17 +08:00
|
|
|
if (!needsToBePersisted) {
|
|
|
|
lastPersistedConnectionString = ValueRef(contents.toString());
|
|
|
|
}
|
2021-10-26 07:59:54 +08:00
|
|
|
cs = contents;
|
2021-10-22 04:44:17 +08:00
|
|
|
}
|
2021-10-11 11:44:56 +08:00
|
|
|
|
|
|
|
// Loads and parses the connection string at the specified key, throwing errors if the file cannot be read or the
|
|
|
|
// format is invalid.
|
|
|
|
ACTOR Future<Reference<ClusterConnectionKey>> ClusterConnectionKey::loadClusterConnectionKey(Database db,
|
|
|
|
Key connectionStringKey) {
|
|
|
|
state Transaction tr(db);
|
|
|
|
loop {
|
|
|
|
try {
|
|
|
|
Optional<Value> v = wait(tr.get(connectionStringKey));
|
|
|
|
if (!v.present()) {
|
|
|
|
throw connection_string_invalid();
|
|
|
|
}
|
2021-10-12 05:39:05 +08:00
|
|
|
return makeReference<ClusterConnectionKey>(db,
|
|
|
|
connectionStringKey,
|
|
|
|
ClusterConnectionString(v.get().toString()),
|
|
|
|
ConnectionStringNeedsPersisted::False);
|
2021-10-11 11:44:56 +08:00
|
|
|
} catch (Error& e) {
|
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 05:39:05 +08:00
|
|
|
// Sets the connections string held by this object and persists it.
|
2022-01-28 09:43:56 +08:00
|
|
|
Future<Void> ClusterConnectionKey::setAndPersistConnectionString(ClusterConnectionString const& connectionString) {
|
2021-10-11 11:44:56 +08:00
|
|
|
cs = connectionString;
|
|
|
|
return success(persist());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the connection string stored in the database.
|
|
|
|
ACTOR Future<ClusterConnectionString> ClusterConnectionKey::getStoredConnectionStringImpl(
|
|
|
|
Reference<ClusterConnectionKey> self) {
|
|
|
|
Reference<ClusterConnectionKey> cck =
|
|
|
|
wait(ClusterConnectionKey::loadClusterConnectionKey(self->db, self->connectionStringKey));
|
|
|
|
return cck->cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<ClusterConnectionString> ClusterConnectionKey::getStoredConnectionString() {
|
|
|
|
return getStoredConnectionStringImpl(Reference<ClusterConnectionKey>::addRef(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR Future<bool> ClusterConnectionKey::upToDateImpl(Reference<ClusterConnectionKey> self,
|
|
|
|
ClusterConnectionString* connectionString) {
|
|
|
|
try {
|
|
|
|
// the cluster file hasn't been created yet so there's nothing to check
|
|
|
|
if (self->needsToBePersisted())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Reference<ClusterConnectionKey> temp =
|
|
|
|
wait(ClusterConnectionKey::loadClusterConnectionKey(self->db, self->connectionStringKey));
|
|
|
|
*connectionString = temp->getConnectionString();
|
|
|
|
return connectionString->toString() == self->cs.toString();
|
|
|
|
} catch (Error& e) {
|
|
|
|
TraceEvent(SevWarnAlways, "ClusterKeyError").error(e).detail("Key", self->connectionStringKey);
|
|
|
|
return false; // Swallow the error and report that the file is out of date
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether the connection string in the database matches the connection string stored in memory. The cluster
|
|
|
|
// string stored in the database is returned via the reference parameter connectionString.
|
|
|
|
Future<bool> ClusterConnectionKey::upToDate(ClusterConnectionString& connectionString) {
|
|
|
|
return upToDateImpl(Reference<ClusterConnectionKey>::addRef(this), &connectionString);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the key where the connection string is stored.
|
|
|
|
std::string ClusterConnectionKey::getLocation() const {
|
|
|
|
return printable(connectionStringKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a copy of this object with a modified connection string but that isn't persisted.
|
|
|
|
Reference<IClusterConnectionRecord> ClusterConnectionKey::makeIntermediateRecord(
|
|
|
|
ClusterConnectionString const& connectionString) const {
|
|
|
|
return makeReference<ClusterConnectionKey>(db, connectionStringKey, connectionString);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a string representation of this cluster connection record. This will include the type of record and the
|
|
|
|
// key where the record is stored.
|
|
|
|
std::string ClusterConnectionKey::toString() const {
|
2021-10-22 04:44:17 +08:00
|
|
|
return "fdbkey://" + printable(connectionStringKey);
|
2021-10-11 11:44:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR Future<bool> ClusterConnectionKey::persistImpl(Reference<ClusterConnectionKey> self) {
|
|
|
|
self->setPersisted();
|
2021-10-22 04:44:17 +08:00
|
|
|
state Value newConnectionString = ValueRef(self->cs.toString());
|
2021-10-11 11:44:56 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
state Transaction tr(self->db);
|
|
|
|
loop {
|
|
|
|
try {
|
2021-10-22 04:44:17 +08:00
|
|
|
Optional<Value> existingConnectionString = wait(tr.get(self->connectionStringKey));
|
|
|
|
// Someone has already updated the connection string to what we want
|
|
|
|
if (existingConnectionString.present() && existingConnectionString.get() == newConnectionString) {
|
|
|
|
self->lastPersistedConnectionString = newConnectionString;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Someone has updated the connection string to something we didn't expect, in which case we leave it
|
|
|
|
// alone. It's possible this could result in the stored string getting stuck if the connection string
|
|
|
|
// changes twice and only the first change is recorded. If the process that wrote the first change dies
|
|
|
|
// and no other process attempts to write the intermediate state, then only a newly opened connection
|
|
|
|
// key would be able to update the state.
|
|
|
|
else if (existingConnectionString.present() &&
|
|
|
|
existingConnectionString != self->lastPersistedConnectionString) {
|
|
|
|
TraceEvent(SevWarnAlways, "UnableToChangeConnectionKeyDueToMismatch")
|
|
|
|
.detail("ConnectionKey", self->connectionStringKey)
|
|
|
|
.detail("NewConnectionString", newConnectionString)
|
|
|
|
.detail("ExpectedStoredConnectionString", self->lastPersistedConnectionString)
|
|
|
|
.detail("ActualStoredConnectionString", existingConnectionString);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
tr.set(self->connectionStringKey, newConnectionString);
|
2021-10-11 11:44:56 +08:00
|
|
|
wait(tr.commit());
|
2021-10-22 04:44:17 +08:00
|
|
|
|
|
|
|
self->lastPersistedConnectionString = newConnectionString;
|
2021-10-11 11:44:56 +08:00
|
|
|
return true;
|
|
|
|
} catch (Error& e) {
|
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Error& e) {
|
|
|
|
TraceEvent(SevWarnAlways, "UnableToChangeConnectionKey")
|
|
|
|
.error(e)
|
|
|
|
.detail("ConnectionKey", self->connectionStringKey)
|
2021-10-22 04:44:17 +08:00
|
|
|
.detail("ConnectionString", self->cs.toString());
|
2021-10-11 11:44:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Writes the connection string to the database
|
|
|
|
Future<bool> ClusterConnectionKey::persist() {
|
|
|
|
return persistImpl(Reference<ClusterConnectionKey>::addRef(this));
|
|
|
|
}
|