• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef COMMUNICATOR_LINKER_H
17 #define COMMUNICATOR_LINKER_H
18 
19 #include <set>
20 #include <map>
21 #include <mutex>
22 #include <atomic>
23 #include <string>
24 #include <vector>
25 #include <cstdlib>
26 #include <functional>
27 #include "ref_object.h"
28 #include "serial_buffer.h"
29 #include "communicator_type_define.h"
30 
31 namespace DistributedDB {
32 class CommunicatorAggregator; // Forward Declaration
33 
34 class CommunicatorLinker : public virtual RefObject {
35 public:
36     explicit CommunicatorLinker(CommunicatorAggregator *inAggregator);
37     ~CommunicatorLinker();
38 
39     DISABLE_COPY_ASSIGN_MOVE(CommunicatorLinker);
40 
41     void Initialize();
42 
43     // Create async task to send out label_exchange and waiting for label_exchange_ack.
44     // If waiting timeout, pass the send&wait task to overrall timing retry task.
45     int TargetOnline(const std::string &inTarget, std::set<LabelType> &outRelatedLabels);
46 
47     // Clear all labels related to this target. Let no longer waiting for ack of this target.
48     // The caller should notify all related communicator about this target offline.
49     int TargetOffline(const std::string &inTarget, std::set<LabelType> &outRelatedLabels);
50 
51     // Add local label. Create async task to send out label_exchange and waiting for label_exchange_ack.
52     // If waiting timeout, pass the send&wait task to overrall timing retry task.
53     // Find out targets for this label that is already online.
54     // The caller should notify communicator of this label about already online target.
55     int IncreaseLocalLabel(const LabelType &inLabel, std::set<std::string> &outOnlineTarget);
56 
57     // Del local label. Create async task to send out label_exchange and waiting for label_exchange_ack.
58     // If waiting timeout, pass the send&wait task to overrall timing retry task.
59     int DecreaseLocalLabel(const LabelType &inLabel);
60 
61     // Compare the latest labels with previous Label, find out label changes.
62     // The caller should notify the target changes according to label changes.
63     // Update the online labels of this target. Send out label_exchange_ack.
64     int ReceiveLabelExchange(const std::string &inTarget, const std::set<LabelType> &inLatestLabels,
65         uint64_t inDistinctValue, uint64_t inSequenceId, std::map<LabelType, bool> &outChangeLabels);
66 
67     // Waiting finish if the ack is what linker wait by check inSequenceId
68     // Similarly, stop the retry task of this Target.
69     int ReceiveLabelExchangeAck(const std::string &inTarget, uint64_t inDistinctValue, uint64_t inSequenceId);
70 
71     std::set<std::string> GetOnlineRemoteTarget() const;
72 
73     bool IsRemoteTargetOnline(const std::string &inTarget) const;
74 private:
75     DECLARE_OBJECT_TAG(CommunicatorLinker);
76 
77     // inCountDown is in millisecond
78     void SuspendByOnceTimer(const std::function<void(void)> &inAction, uint32_t inCountDown);
79 
80     // This function should be called under protection of entireInfoMutex_
81     void DetectDistinctValueChange(const std::string &inTarget, uint64_t inDistinctValue);
82 
83     int TriggerLabelExchangeEvent(const std::string &toTarget);
84     int TriggerLabelExchangeAckEvent(const std::string &toTarget, uint64_t inSequenceId);
85 
86     void SendLabelExchange(const std::string &toTarget, SerialBuffer *inBuff, uint64_t inSequenceId,
87         uint32_t inRetransmitCount);
88     void SendLabelExchangeAck(const std::string &toTarget, SerialBuffer *inBuff, uint64_t inSequenceId,
89         uint64_t inAckTriggerId);
90 
91     uint64_t localDistinctValue_ = 0;
92     std::atomic<uint64_t> incSequenceId_;
93     std::atomic<uint64_t> incAckTriggerId_;
94     CommunicatorAggregator *aggregator_ = nullptr;
95 
96     mutable std::mutex entireInfoMutex_;
97 
98     // Point out the distinctValue for each target in order to detect malfunctioning "target offline"
99     std::map<std::string, uint64_t> targetDistinctValue_;
100 
101     // Point out the largest sequenceId of LabelExchange that ever received for each target
102     std::map<std::string, uint64_t> topRecvLabelSeq_;
103 
104     // Point out currently which sequenceId of ack is being waited for each target
105     std::map<std::string, uint64_t> waitAckSeq_;
106 
107     // Point out the largest sequenceId of LabelExchangeAck that ever received for each target
108     std::map<std::string, uint64_t> recvAckSeq_;
109 
110     // Point out the latest ackTriggerId for each target in order to abort outdated triggered event
111     std::map<std::string, uint64_t> ackTriggerId_;
112 
113     // Core Info : Online Labels
114     std::set<LabelType> localOnlineLabels_;
115     std::set<std::string> remoteOnlineTarget_;
116 
117     // remember the opened labels no matter target now online or offline
118     std::map<std::string, std::set<LabelType>> targetMapOnlineLabels_;
119 };
120 }
121 
122 #endif