1 /*
2  * MasterInterface.h
3  *
4  * This source file is part of the FoundationDB open source project
5  *
6  * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef FDBSERVER_MASTERINTERFACE_H
22 #define FDBSERVER_MASTERINTERFACE_H
23 #pragma once
24 
25 #include "fdbclient/FDBTypes.h"
26 #include "fdbclient/StorageServerInterface.h"
27 #include "fdbclient/CommitTransaction.h"
28 #include "fdbclient/DatabaseConfiguration.h"
29 #include "fdbserver/TLogInterface.h"
30 
31 typedef uint64_t DBRecoveryCount;
32 
33 struct MasterInterface {
34 	LocalityData locality;
35 	RequestStream< ReplyPromise<Void> > waitFailure;
36 	RequestStream< struct TLogRejoinRequest > tlogRejoin; // sent by tlog (whether or not rebooted) to communicate with a new master
37 	RequestStream< struct ChangeCoordinatorsRequest > changeCoordinators;
38 	RequestStream< struct GetCommitVersionRequest > getCommitVersion;
39 
addressMasterInterface40 	NetworkAddress address() const { return changeCoordinators.getEndpoint().getPrimaryAddress(); }
41 
idMasterInterface42 	UID id() const { return changeCoordinators.getEndpoint().token; }
43 	template <class Archive>
serializeMasterInterface44 	void serialize(Archive& ar) {
45 		ASSERT( ar.protocolVersion() >= 0x0FDB00A200040001LL );
46 		serializer(ar, locality, waitFailure, tlogRejoin, changeCoordinators, getCommitVersion);
47 	}
48 
initEndpointsMasterInterface49 	void initEndpoints() {
50 		getCommitVersion.getEndpoint( TaskProxyGetConsistentReadVersion );
51 	}
52 };
53 
54 struct TLogRejoinRequest {
55 	TLogInterface myInterface;
56 	ReplyPromise<bool> reply;   // false means someone else registered, so we should re-register.  true means this master is recovered, so don't send again to the same master.
57 
TLogRejoinRequestTLogRejoinRequest58 	TLogRejoinRequest() { }
TLogRejoinRequestTLogRejoinRequest59 	explicit TLogRejoinRequest(const TLogInterface &interf) : myInterface(interf) { }
60 	template <class Ar>
serializeTLogRejoinRequest61 	void serialize(Ar& ar) {
62 		serializer(ar, myInterface, reply);
63 	}
64 };
65 
66 struct ChangeCoordinatorsRequest {
67 	Standalone<StringRef> newConnectionString;
68 	ReplyPromise<Void> reply;  // normally throws even on success!
69 
ChangeCoordinatorsRequestChangeCoordinatorsRequest70 	ChangeCoordinatorsRequest() {}
ChangeCoordinatorsRequestChangeCoordinatorsRequest71 	ChangeCoordinatorsRequest(Standalone<StringRef> newConnectionString) : newConnectionString(newConnectionString) {}
72 
73 	template <class Ar>
serializeChangeCoordinatorsRequest74 	void serialize(Ar& ar) {
75 		serializer(ar, newConnectionString, reply);
76 	}
77 };
78 
79 struct ResolverMoveRef {
80 	KeyRangeRef range;
81 	int dest;
82 
ResolverMoveRefResolverMoveRef83 	ResolverMoveRef() : dest(0) {}
ResolverMoveRefResolverMoveRef84 	ResolverMoveRef(KeyRangeRef const& range, int dest) : range(range), dest(dest) {}
ResolverMoveRefResolverMoveRef85 	ResolverMoveRef( Arena& a, const ResolverMoveRef& copyFrom ) : range(a, copyFrom.range), dest(copyFrom.dest) {}
86 
87 	bool operator == ( ResolverMoveRef const& rhs ) const {
88 		return range == rhs.range && dest == rhs.dest;
89 	}
90 	bool operator != ( ResolverMoveRef const& rhs ) const {
91 		return range != rhs.range || dest != rhs.dest;
92 	}
93 
expectedSizeResolverMoveRef94 	size_t expectedSize() const {
95 		return range.expectedSize();
96 	}
97 
98 	template <class Ar>
serializeResolverMoveRef99 	void serialize( Ar& ar ) {
100 		serializer(ar, range, dest);
101 	}
102 };
103 
104 struct GetCommitVersionReply {
105 	Standalone<VectorRef<ResolverMoveRef>> resolverChanges;
106 	Version resolverChangesVersion;
107 	Version version;
108 	Version prevVersion;
109 	uint64_t requestNum;
110 
GetCommitVersionReplyGetCommitVersionReply111 	GetCommitVersionReply() : resolverChangesVersion(0), version(0), prevVersion(0), requestNum(0) {}
GetCommitVersionReplyGetCommitVersionReply112 	explicit GetCommitVersionReply( Version version, Version prevVersion, uint64_t requestNum ) : version(version), prevVersion(prevVersion), resolverChangesVersion(0), requestNum(requestNum) {}
113 
114 	template <class Ar>
serializeGetCommitVersionReply115 	void serialize(Ar& ar) {
116 		serializer(ar, resolverChanges, resolverChangesVersion, version, prevVersion, requestNum);
117 	}
118 };
119 
120 struct GetCommitVersionRequest {
121 	uint64_t requestNum;
122 	uint64_t mostRecentProcessedRequestNum;
123 	UID requestingProxy;
124 	ReplyPromise<GetCommitVersionReply> reply;
125 
GetCommitVersionRequestGetCommitVersionRequest126 	GetCommitVersionRequest() { }
GetCommitVersionRequestGetCommitVersionRequest127 	GetCommitVersionRequest(uint64_t requestNum, uint64_t mostRecentProcessedRequestNum, UID requestingProxy)
128 		: requestNum(requestNum), mostRecentProcessedRequestNum(mostRecentProcessedRequestNum), requestingProxy(requestingProxy) {}
129 
130 	template <class Ar>
serializeGetCommitVersionRequest131 	void serialize(Ar& ar) {
132 		serializer(ar, requestNum, mostRecentProcessedRequestNum, requestingProxy, reply);
133 	}
134 };
135 
136 struct LifetimeToken {
137 	UID ccID;
138 	int64_t count;
139 
LifetimeTokenLifetimeToken140 	LifetimeToken() : count(0) {}
141 
isStillValidLifetimeToken142 	bool isStillValid( LifetimeToken const& latestToken, bool isLatestID ) const {
143 		return ccID == latestToken.ccID && (count >= latestToken.count || isLatestID);
144 	}
toStringLifetimeToken145 	std::string toString() const {
146 		return ccID.shortString() + format("#%lld", count);
147 	}
148 	void operator++() {
149 		++count;
150 	}
151 
152 	template <class Ar>
serializeLifetimeToken153 	void serialize(Ar& ar) {
154 		serializer(ar, ccID, count);
155 	}
156 };
157 
158 #endif
159