Node.cpp 1.9 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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.

S
starlord 已提交
18
#include "scheduler/resource/Node.h"
X
xj.lin 已提交
19 20

#include <atomic>
S
starlord 已提交
21
#include <utility>
X
xj.lin 已提交
22 23 24

namespace zilliz {
namespace milvus {
W
wxyu 已提交
25
namespace scheduler {
X
xj.lin 已提交
26 27 28 29 30 31

Node::Node() {
    static std::atomic_uint_fast8_t counter(0);
    id_ = counter++;
}

S
starlord 已提交
32 33
std::vector<Neighbour>
Node::GetNeighbours() {
X
xj.lin 已提交
34 35
    std::lock_guard<std::mutex> lk(mutex_);
    std::vector<Neighbour> ret;
S
starlord 已提交
36
    for (auto& e : neighbours_) {
X
xj.lin 已提交
37 38 39 40 41
        ret.push_back(e.second);
    }
    return ret;
}

S
starlord 已提交
42 43
std::string
Node::Dump() {
W
wxyu 已提交
44 45
    std::stringstream ss;
    ss << "<Node, id=" << std::to_string(id_) << ">::neighbours:" << std::endl;
S
starlord 已提交
46
    for (auto& neighbour : neighbours_) {
W
wxyu 已提交
47 48 49 50
        ss << "\t<Neighbour, id=" << std::to_string(neighbour.first);
        ss << ", connection: " << neighbour.second.connection.Dump() << ">" << std::endl;
    }
    return ss.str();
X
xj.lin 已提交
51 52
}

S
starlord 已提交
53
void
S
starlord 已提交
54
Node::AddNeighbour(const NeighbourNodePtr& neighbour_node, Connection& connection) {
X
xj.lin 已提交
55 56
    std::lock_guard<std::mutex> lk(mutex_);
    if (auto s = neighbour_node.lock()) {
W
wxyu 已提交
57
        neighbours_.emplace(std::make_pair(s->id_, Neighbour(neighbour_node, connection)));
X
xj.lin 已提交
58 59 60 61
    }
    // else do nothing, consider it..
}

S
starlord 已提交
62 63 64
}  // namespace scheduler
}  // namespace milvus
}  // namespace zilliz