aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/apps/vespa-feed-bm/storage_api_chain_bm_feed_handler.cpp
blob: 82cf2df065f271170d76ea6ee520629b23a81db9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "storage_api_chain_bm_feed_handler.h"
#include "pending_tracker.h"
#include "storage_reply_error_checker.h"
#include "bm_storage_link_context.h"
#include "bm_storage_link.h"
#include <vespa/document/fieldvalue/document.h>
#include <vespa/document/update/documentupdate.h>
#include <vespa/storageapi/messageapi/storagemessage.h>
#include <vespa/storageapi/message/persistence.h>
#include <vespa/storageapi/message/state.h>
#include <vespa/vdslib/state/clusterstate.h>
#include <vespa/vdslib/state/cluster_state_bundle.h>
#include <cassert>

using document::Document;
using document::DocumentId;
using document::DocumentUpdate;

namespace feedbm {

namespace {

std::shared_ptr<storage::api::StorageCommand> make_set_cluster_state_cmd() {
    storage::lib::ClusterStateBundle bundle(storage::lib::ClusterState("version:2 distributor:1 storage:1"));
    auto cmd = std::make_shared<storage::api::SetSystemStateCommand>(bundle);
    cmd->setPriority(storage::api::StorageMessage::VERYHIGH);
    return cmd;
}

}

StorageApiChainBmFeedHandler::StorageApiChainBmFeedHandler(std::shared_ptr<BmStorageLinkContext> context, bool distributor)
    : IBmFeedHandler(),
      _name(vespalib::string("StorageApiChainBmFeedHandler(") + (distributor ? "distributor" : "service-layer") + ")"),
      _distributor(distributor),
      _context(std::move(context))
{
    auto cmd = make_set_cluster_state_cmd();
    PendingTracker tracker(1);
    send_msg(std::move(cmd), tracker);
    tracker.drain();
}

StorageApiChainBmFeedHandler::~StorageApiChainBmFeedHandler() = default;

void
StorageApiChainBmFeedHandler::send_msg(std::shared_ptr<storage::api::StorageCommand> cmd, PendingTracker& pending_tracker)
{
    cmd->setSourceIndex(0);
    auto bm_link = _context->bm_link;
    bm_link->retain(cmd->getMsgId(), pending_tracker);
    bm_link->sendDown(std::move(cmd));
}

void
StorageApiChainBmFeedHandler::put(const document::Bucket& bucket, std::unique_ptr<Document> document, uint64_t timestamp, PendingTracker& tracker)
{
    auto cmd = std::make_unique<storage::api::PutCommand>(bucket, std::move(document), timestamp);
    send_msg(std::move(cmd), tracker);
}

void
StorageApiChainBmFeedHandler::update(const document::Bucket& bucket, std::unique_ptr<DocumentUpdate> document_update, uint64_t timestamp, PendingTracker& tracker)
{
    auto cmd = std::make_unique<storage::api::UpdateCommand>(bucket, std::move(document_update), timestamp);
    send_msg(std::move(cmd), tracker);
}

void
StorageApiChainBmFeedHandler::remove(const document::Bucket& bucket, const DocumentId& document_id,  uint64_t timestamp, PendingTracker& tracker)
{
    auto cmd = std::make_unique<storage::api::RemoveCommand>(bucket, document_id, timestamp);
    send_msg(std::move(cmd), tracker);
}

void
StorageApiChainBmFeedHandler::get(const document::Bucket& bucket, vespalib::stringref field_set_string, const document::DocumentId& document_id, PendingTracker& tracker)
{
    auto cmd = std::make_unique<storage::api::GetCommand>(bucket, document_id, field_set_string);
    send_msg(std::move(cmd), tracker);
}

void
StorageApiChainBmFeedHandler::attach_bucket_info_queue(PendingTracker&)
{
}

uint32_t
StorageApiChainBmFeedHandler::get_error_count() const
{
    return _context->bm_link->get_error_count();
}

const vespalib::string&
StorageApiChainBmFeedHandler::get_name() const
{
    return _name;
}

bool
StorageApiChainBmFeedHandler::manages_timestamp() const
{
    return _distributor;
}

}