Loading...
Searching...
No Matches
system_info.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
21
22#include <glog/logging.h>
23
24namespace resdb {
25
26SystemInfo::SystemInfo() : primary_id_(1), view_(1) {}
27
29 : primary_id_(config.GetReplicaInfos()[0].id()), view_(1) {
31 LOG(ERROR) << "get primary id:" << primary_id_;
32}
33
34uint32_t SystemInfo::GetPrimaryId() const { return primary_id_; }
35
36void SystemInfo::SetPrimary(uint32_t id) { primary_id_ = id; }
37
38uint64_t SystemInfo::GetCurrentView() const { return view_; }
39
40void SystemInfo::SetCurrentView(uint64_t view_id) { view_ = view_id; }
41
42std::vector<ReplicaInfo> SystemInfo::GetReplicas() const { return replicas_; }
43
44void SystemInfo::SetReplicas(const std::vector<ReplicaInfo>& replicas) {
45 replicas_ = replicas;
46}
47
48void SystemInfo::AddReplica(const ReplicaInfo& replica) {
49 if (replica.id() == 0 || replica.ip().empty() || replica.port() == 0) {
50 return;
51 }
52 for (const auto& cur_replica : replicas_) {
53 if (cur_replica.id() == replica.id()) {
54 LOG(ERROR) << " replica exist:" << replica.id();
55 return;
56 }
57 }
58 LOG(ERROR) << "add new replica:" << replica.DebugString();
59 replicas_.push_back(replica);
60}
61
62void SystemInfo::ProcessRequest(const SystemInfoRequest& request) {
63 switch (request.type()) {
64 case SystemInfoRequest::ADD_REPLICA: {
65 NewReplicaRequest info;
66 if (info.ParseFromString(request.request())) {
67 AddReplica(info.replica_info());
68 }
69 } break;
70 default:
71 break;
72 }
73}
74
75} // namespace resdb
const std::vector< ReplicaInfo > & GetReplicaInfos() const
void SetCurrentView(uint64_t)
uint32_t GetPrimaryId() const
void SetPrimary(uint32_t id)
void SetReplicas(const std::vector< ReplicaInfo > &replicas)
std::vector< ReplicaInfo > GetReplicas() const
void AddReplica(const ReplicaInfo &replica)
void ProcessRequest(const SystemInfoRequest &request)
uint64_t GetCurrentView() const