1 /*
2  * LogProtocolMessage.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_LOGPROTOCOLMESSAGE_H
22 #define FDBSERVER_LOGPROTOCOLMESSAGE_H
23 #pragma once
24 
25 #include "fdbclient/FDBTypes.h"
26 #include "fdbclient/CommitTransaction.h"
27 
28 template <class Ar, class VersionOptions>
applyVersionStartingHere(Ar & ar,VersionOptions vo)29 typename Ar::READER& applyVersionStartingHere(Ar& ar, VersionOptions vo) {
30 	vo.read(ar);
31 	return ar;
32 }
33 
34 template <class Ar, class VersionOptions>
applyVersionStartingHere(Ar & ar,VersionOptions vo)35 typename Ar::WRITER& applyVersionStartingHere(Ar& ar, VersionOptions vo) {
36 	vo.write(ar);
37 	return ar;
38 }
39 
40 struct LogProtocolMessage {
41 	// This message is pushed into the log system tag for each storage server to inform it what protocol version
42 	// should be used to deserialize subsequent MutationRefs.
43 
44 	// It's legitimate to add extra information here (using the protocol version for backward compatibility) but
45 	// currently there is none.
46 
47 	// This mechanism passes various ASSERTs in simulation, but has never been used in anger (because MutationRef's
48 	// serialization format has never changed) so think about testing when it does change.
49 
50 	// Storage servers merging these messages with MutationRefs need to distinguish the two, so the first byte of
51 	// the serialization of this message is a type code which is reserved in the MutationRef::Type enum and will thus
52 	// never be the first byte of a MutationRef message.
53 
LogProtocolMessageLogProtocolMessage54 	LogProtocolMessage() {}
55 
toStringLogProtocolMessage56 	std::string toString() const {
57 		return format("code: %d", MutationRef::Reserved_For_LogProtocolMessage);
58 	}
59 
60 	template <class Ar>
serializeLogProtocolMessage61 	void serialize(Ar& ar) {
62 		uint8_t poly = MutationRef::Reserved_For_LogProtocolMessage;
63 		serializer(ar, poly);
64 		applyVersionStartingHere(ar, IncludeVersion());
65 	}
66 
startsLogProtocolMessageLogProtocolMessage67 	static bool startsLogProtocolMessage(uint8_t byte) {
68 		return byte == MutationRef::Reserved_For_LogProtocolMessage;
69 	}
isNextInLogProtocolMessage70 	template <class Ar> static bool isNextIn(Ar& ar) { return startsLogProtocolMessage(*(const uint8_t*)ar.peekBytes(1)); }
71 };
72 
73 #endif
74